Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
1 /* -*-mode:java; c-basic-offset:2; -*- */
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This program is based on zlib-1.1.3, so all credit should go authors
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
32 * and contributors of zlib.
35 package org.apidesign.bck2brwsr.emul.zip;
37 import org.apidesign.bck2brwsr.emul.lang.System;
41 static final private int[] inflate_mask = {
42 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
43 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
44 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
45 0x00007fff, 0x0000ffff
48 static final private int Z_OK=0;
49 static final private int Z_STREAM_END=1;
50 static final private int Z_NEED_DICT=2;
51 static final private int Z_ERRNO=-1;
52 static final private int Z_STREAM_ERROR=-2;
53 static final private int Z_DATA_ERROR=-3;
54 static final private int Z_MEM_ERROR=-4;
55 static final private int Z_BUF_ERROR=-5;
56 static final private int Z_VERSION_ERROR=-6;
58 // waiting for "i:"=input,
61 static final private int START=0; // x: set up for LEN
62 static final private int LEN=1; // i: get length/literal/eob next
63 static final private int LENEXT=2; // i: getting length extra (have base)
64 static final private int DIST=3; // i: get distance next
65 static final private int DISTEXT=4;// i: getting distance extra
66 static final private int COPY=5; // o: copying bytes in window, waiting for space
67 static final private int LIT=6; // o: got literal, waiting for output space
68 static final private int WASH=7; // o: got eob, possibly still output waiting
69 static final private int END=8; // x: got eob and all data flushed
70 static final private int BADCODE=9;// x: got error
72 int mode; // current inflate_codes mode
74 // mode dependent information
77 int[] tree; // pointer into tree
79 int need; // bits needed
83 // if EXT or COPY, where and how much
84 int get; // bits to get for extra
85 int dist; // distance back to copy from
87 byte lbits; // ltree bits decoded per branch
88 byte dbits; // dtree bits decoder per branch
89 int[] ltree; // literal/length/eob tree
90 int ltree_index; // literal/length/eob tree
91 int[] dtree; // distance tree
92 int dtree_index; // distance tree
94 private final ZStream z;
95 private final InfBlocks s;
96 InfCodes(ZStream z, InfBlocks s){
101 void init(int bl, int bd,
102 int[] tl, int tl_index,
103 int[] td, int td_index){
108 ltree_index=tl_index;
110 dtree_index=td_index;
115 int j; // temporary storage
116 int[] t; // temporary pointer
117 int tindex; // temporary pointer
118 int e; // extra bits or operation
119 int b=0; // bit buffer
120 int k=0; // bits in bit buffer
121 int p=0; // input data pointer
122 int n; // bytes available there
123 int q; // output window write pointer
124 int m; // bytes to end of window or read pointer
125 int f; // pointer to copy strings from
127 // copy input/output information to locals (UPDATE macro restores)
128 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
129 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
131 // process input and output based on current state
134 // waiting for "i:"=input, "o:"=output, "x:"=nothing
135 case START: // x: set up for LEN
136 if (m >= 258 && n >= 10){
139 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
141 r = inflate_fast(lbits, dbits,
146 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
147 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
150 mode = r == Z_STREAM_END ? WASH : BADCODE;
156 tree_index=ltree_index;
159 case LEN: // i: get length/literal/eob next
167 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
169 return s.inflate_flush(r);
172 b|=(z.next_in[p++]&0xff)<<k;
176 tindex=(tree_index+(b&inflate_mask[j]))*3;
178 b>>>=(tree[tindex+1]);
183 if(e == 0){ // literal
184 lit = tree[tindex+2];
188 if((e & 16)!=0 ){ // length
190 len = tree[tindex+2];
194 if ((e & 64) == 0){ // next table
196 tree_index = tindex/3+tree[tindex+2];
199 if ((e & 32)!=0){ // end of block
203 mode = BADCODE; // invalid code
204 z.msg = "invalid literal/length code";
208 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
210 return s.inflate_flush(r);
212 case LENEXT: // i: getting length extra (have base)
220 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
222 return s.inflate_flush(r);
224 n--; b|=(z.next_in[p++]&0xff)<<k;
228 len += (b & inflate_mask[j]);
235 tree_index=dtree_index;
237 case DIST: // i: get distance next
245 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
247 return s.inflate_flush(r);
249 n--; b|=(z.next_in[p++]&0xff)<<k;
253 tindex=(tree_index+(b & inflate_mask[j]))*3;
259 if((e & 16)!=0){ // distance
261 dist = tree[tindex+2];
265 if ((e & 64) == 0){ // next table
267 tree_index = tindex/3 + tree[tindex+2];
270 mode = BADCODE; // invalid code
271 z.msg = "invalid distance code";
275 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
277 return s.inflate_flush(r);
279 case DISTEXT: // i: getting distance extra
287 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
289 return s.inflate_flush(r);
291 n--; b|=(z.next_in[p++]&0xff)<<k;
295 dist += (b & inflate_mask[j]);
301 case COPY: // o: copying bytes in window, waiting for space
303 while(f < 0){ // modulo window size-"while" instead
304 f += s.end; // of "if" handles invalid distances
309 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
311 s.write=q; r=s.inflate_flush(r);
312 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
314 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
318 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
320 return s.inflate_flush(r);
325 s.window[q++]=s.window[f++]; m--;
333 case LIT: // o: got literal, waiting for output space
335 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
337 s.write=q; r=s.inflate_flush(r);
338 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
340 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
343 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
345 return s.inflate_flush(r);
351 s.window[q++]=(byte)lit; m--;
355 case WASH: // o: got eob, possibly more output
356 if (k > 7){ // return unused byte, if any
359 p--; // can always return one
362 s.write=q; r=s.inflate_flush(r);
363 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
365 if (s.read != s.write){
367 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
369 return s.inflate_flush(r);
375 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
377 return s.inflate_flush(r);
379 case BADCODE: // x: got error
384 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
386 return s.inflate_flush(r);
392 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
394 return s.inflate_flush(r);
399 void free(ZStream z){
403 // Called with number of bytes left to write in window at least 258
404 // (the maximum string length) and number of input bytes available
405 // at least ten. The ten bytes are six bytes for the longest length/
406 // distance pair plus four bytes for overloading the bit buffer.
408 int inflate_fast(int bl, int bd,
409 int[] tl, int tl_index,
410 int[] td, int td_index,
411 InfBlocks s, ZStream z){
412 int t; // temporary pointer
413 int[] tp; // temporary pointer
414 int tp_index; // temporary pointer
415 int e; // extra bits or operation
417 int k; // bits in bit buffer
418 int p; // input data pointer
419 int n; // bytes available there
420 int q; // output window write pointer
421 int m; // bytes to end of window or read pointer
422 int ml; // mask for literal/length tree
423 int md; // mask for distance tree
424 int c; // bytes to copy
425 int d; // distance back to copy from
426 int r; // copy source pointer
428 int tp_index_t_3; // (tp_index+t)*3
430 // load input, output, bit values
431 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
432 q=s.write;m=q<s.read?s.read-q-1:s.end-q;
435 ml = inflate_mask[bl];
436 md = inflate_mask[bd];
438 // do until not enough input or output space for fast loop
439 do { // assume called with m >= 258 && n >= 10
440 // get literal/length code
441 while(k<(20)){ // max bits for literal/length code
443 b|=(z.next_in[p++]&0xff)<<k;k+=8;
449 tp_index_t_3=(tp_index+t)*3;
450 if ((e = tp[tp_index_t_3]) == 0){
451 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
453 s.window[q++] = (byte)tp[tp_index_t_3+2];
459 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
463 c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]);
467 // decode distance base of block to copy
468 while(k<(15)){ // max bits for distance code
470 b|=(z.next_in[p++]&0xff)<<k;k+=8;
476 tp_index_t_3=(tp_index+t)*3;
477 e = tp[tp_index_t_3];
481 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
484 // get extra bits to add to distance base
486 while(k<(e)){ // get extra bits (up to 13)
488 b|=(z.next_in[p++]&0xff)<<k;k+=8;
491 d = tp[tp_index_t_3+2] + (b&inflate_mask[e]);
497 if (q >= d){ // offset before dest
500 if(q-r>0 && 2>(q-r)){
501 s.window[q++]=s.window[r++]; // minimum count is three,
502 s.window[q++]=s.window[r++]; // so unroll loop a little
506 System.arraycopy(s.window, r, s.window, q, 2);
510 else{ // else offset after destination
513 r+=s.end; // force pointer in window
514 }while(r<0); // covers invalid distances
516 if(c>e){ // if source crosses,
517 c-=e; // wrapped copy
518 if(q-r>0 && e>(q-r)){
519 do{s.window[q++] = s.window[r++];}
523 System.arraycopy(s.window, r, s.window, q, e);
526 r = 0; // copy rest from start of window
531 // copy all or what's left
532 if(q-r>0 && c>(q-r)){
533 do{s.window[q++] = s.window[r++];}
537 System.arraycopy(s.window, r, s.window, q, c);
543 t+=tp[tp_index_t_3+2];
544 t+=(b&inflate_mask[e]);
545 tp_index_t_3=(tp_index+t)*3;
549 z.msg = "invalid distance code";
551 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
554 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
565 t+=tp[tp_index_t_3+2];
566 t+=(b&inflate_mask[e]);
567 tp_index_t_3=(tp_index+t)*3;
568 if((e=tp[tp_index_t_3])==0){
570 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
572 s.window[q++]=(byte)tp[tp_index_t_3+2];
579 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
582 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
588 z.msg="invalid literal/length code";
590 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
593 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
601 while(m>=258 && n>= 10);
603 // not enough input or output--restore pointers and return
604 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
607 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;