1 | /* -*- mode: javascript; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | |
3 | // |
4 | // Javascript ZLib |
5 | // By Thomas Down 2010-2011 |
6 | // |
7 | // Based very heavily on portions of jzlib (by ymnk@jcraft.com), who in |
8 | // turn credits Jean-loup Gailly and Mark Adler for the original zlib code. |
9 | // |
10 | // inflate.js: ZLib inflate code |
11 | // |
12 | |
13 | // |
14 | // Shared constants |
15 | // |
16 | |
17 | var MAX_WBITS=15; // 32K LZ77 window |
18 | var DEF_WBITS=MAX_WBITS; |
19 | var MAX_MEM_LEVEL=9; |
20 | var MANY=1440; |
21 | var BMAX = 15; |
22 | |
23 | // preset dictionary flag in zlib header |
24 | var PRESET_DICT=0x20; |
25 | |
26 | var Z_NO_FLUSH=0; |
27 | var Z_PARTIAL_FLUSH=1; |
28 | var Z_SYNC_FLUSH=2; |
29 | var Z_FULL_FLUSH=3; |
30 | var Z_FINISH=4; |
31 | |
32 | var Z_DEFLATED=8; |
33 | |
34 | var Z_OK=0; |
35 | var Z_STREAM_END=1; |
36 | var Z_NEED_DICT=2; |
37 | var Z_ERRNO=-1; |
38 | var Z_STREAM_ERROR=-2; |
39 | var Z_DATA_ERROR=-3; |
40 | var Z_MEM_ERROR=-4; |
41 | var Z_BUF_ERROR=-5; |
42 | var Z_VERSION_ERROR=-6; |
43 | |
44 | var METHOD=0; // waiting for method byte |
45 | var FLAG=1; // waiting for flag byte |
46 | var DICT4=2; // four dictionary check bytes to go |
47 | var DICT3=3; // three dictionary check bytes to go |
48 | var DICT2=4; // two dictionary check bytes to go |
49 | var DICT1=5; // one dictionary check byte to go |
50 | var DICT0=6; // waiting for inflateSetDictionary |
51 | var BLOCKS=7; // decompressing blocks |
52 | var CHECK4=8; // four check bytes to go |
53 | var CHECK3=9; // three check bytes to go |
54 | var CHECK2=10; // two check bytes to go |
55 | var CHECK1=11; // one check byte to go |
56 | var DONE=12; // finished check, done |
57 | var BAD=13; // got an error--stay here |
58 | |
59 | var inflate_mask = [0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff]; |
60 | |
61 | var IB_TYPE=0; // get type bits (3, including end bit) |
62 | var IB_LENS=1; // get lengths for stored |
63 | var IB_STORED=2;// processing stored block |
64 | var IB_TABLE=3; // get table lengths |
65 | var IB_BTREE=4; // get bit lengths tree for a dynamic block |
66 | var IB_DTREE=5; // get length, distance trees for a dynamic block |
67 | var IB_CODES=6; // processing fixed or dynamic block |
68 | var IB_DRY=7; // output remaining window bytes |
69 | var IB_DONE=8; // finished last block, done |
70 | var IB_BAD=9; // ot a data error--stuck here |
71 | |
72 | var fixed_bl = 9; |
73 | var fixed_bd = 5; |
74 | |
75 | var fixed_tl = [ |
76 | 96,7,256, 0,8,80, 0,8,16, 84,8,115, |
77 | 82,7,31, 0,8,112, 0,8,48, 0,9,192, |
78 | 80,7,10, 0,8,96, 0,8,32, 0,9,160, |
79 | 0,8,0, 0,8,128, 0,8,64, 0,9,224, |
80 | 80,7,6, 0,8,88, 0,8,24, 0,9,144, |
81 | 83,7,59, 0,8,120, 0,8,56, 0,9,208, |
82 | 81,7,17, 0,8,104, 0,8,40, 0,9,176, |
83 | 0,8,8, 0,8,136, 0,8,72, 0,9,240, |
84 | 80,7,4, 0,8,84, 0,8,20, 85,8,227, |
85 | 83,7,43, 0,8,116, 0,8,52, 0,9,200, |
86 | 81,7,13, 0,8,100, 0,8,36, 0,9,168, |
87 | 0,8,4, 0,8,132, 0,8,68, 0,9,232, |
88 | 80,7,8, 0,8,92, 0,8,28, 0,9,152, |
89 | 84,7,83, 0,8,124, 0,8,60, 0,9,216, |
90 | 82,7,23, 0,8,108, 0,8,44, 0,9,184, |
91 | 0,8,12, 0,8,140, 0,8,76, 0,9,248, |
92 | 80,7,3, 0,8,82, 0,8,18, 85,8,163, |
93 | 83,7,35, 0,8,114, 0,8,50, 0,9,196, |
94 | 81,7,11, 0,8,98, 0,8,34, 0,9,164, |
95 | 0,8,2, 0,8,130, 0,8,66, 0,9,228, |
96 | 80,7,7, 0,8,90, 0,8,26, 0,9,148, |
97 | 84,7,67, 0,8,122, 0,8,58, 0,9,212, |
98 | 82,7,19, 0,8,106, 0,8,42, 0,9,180, |
99 | 0,8,10, 0,8,138, 0,8,74, 0,9,244, |
100 | 80,7,5, 0,8,86, 0,8,22, 192,8,0, |
101 | 83,7,51, 0,8,118, 0,8,54, 0,9,204, |
102 | 81,7,15, 0,8,102, 0,8,38, 0,9,172, |
103 | 0,8,6, 0,8,134, 0,8,70, 0,9,236, |
104 | 80,7,9, 0,8,94, 0,8,30, 0,9,156, |
105 | 84,7,99, 0,8,126, 0,8,62, 0,9,220, |
106 | 82,7,27, 0,8,110, 0,8,46, 0,9,188, |
107 | 0,8,14, 0,8,142, 0,8,78, 0,9,252, |
108 | 96,7,256, 0,8,81, 0,8,17, 85,8,131, |
109 | 82,7,31, 0,8,113, 0,8,49, 0,9,194, |
110 | 80,7,10, 0,8,97, 0,8,33, 0,9,162, |
111 | 0,8,1, 0,8,129, 0,8,65, 0,9,226, |
112 | 80,7,6, 0,8,89, 0,8,25, 0,9,146, |
113 | 83,7,59, 0,8,121, 0,8,57, 0,9,210, |
114 | 81,7,17, 0,8,105, 0,8,41, 0,9,178, |
115 | 0,8,9, 0,8,137, 0,8,73, 0,9,242, |
116 | 80,7,4, 0,8,85, 0,8,21, 80,8,258, |
117 | 83,7,43, 0,8,117, 0,8,53, 0,9,202, |
118 | 81,7,13, 0,8,101, 0,8,37, 0,9,170, |
119 | 0,8,5, 0,8,133, 0,8,69, 0,9,234, |
120 | 80,7,8, 0,8,93, 0,8,29, 0,9,154, |
121 | 84,7,83, 0,8,125, 0,8,61, 0,9,218, |
122 | 82,7,23, 0,8,109, 0,8,45, 0,9,186, |
123 | 0,8,13, 0,8,141, 0,8,77, 0,9,250, |
124 | 80,7,3, 0,8,83, 0,8,19, 85,8,195, |
125 | 83,7,35, 0,8,115, 0,8,51, 0,9,198, |
126 | 81,7,11, 0,8,99, 0,8,35, 0,9,166, |
127 | 0,8,3, 0,8,131, 0,8,67, 0,9,230, |
128 | 80,7,7, 0,8,91, 0,8,27, 0,9,150, |
129 | 84,7,67, 0,8,123, 0,8,59, 0,9,214, |
130 | 82,7,19, 0,8,107, 0,8,43, 0,9,182, |
131 | 0,8,11, 0,8,139, 0,8,75, 0,9,246, |
132 | 80,7,5, 0,8,87, 0,8,23, 192,8,0, |
133 | 83,7,51, 0,8,119, 0,8,55, 0,9,206, |
134 | 81,7,15, 0,8,103, 0,8,39, 0,9,174, |
135 | 0,8,7, 0,8,135, 0,8,71, 0,9,238, |
136 | 80,7,9, 0,8,95, 0,8,31, 0,9,158, |
137 | 84,7,99, 0,8,127, 0,8,63, 0,9,222, |
138 | 82,7,27, 0,8,111, 0,8,47, 0,9,190, |
139 | 0,8,15, 0,8,143, 0,8,79, 0,9,254, |
140 | 96,7,256, 0,8,80, 0,8,16, 84,8,115, |
141 | 82,7,31, 0,8,112, 0,8,48, 0,9,193, |
142 | |
143 | 80,7,10, 0,8,96, 0,8,32, 0,9,161, |
144 | 0,8,0, 0,8,128, 0,8,64, 0,9,225, |
145 | 80,7,6, 0,8,88, 0,8,24, 0,9,145, |
146 | 83,7,59, 0,8,120, 0,8,56, 0,9,209, |
147 | 81,7,17, 0,8,104, 0,8,40, 0,9,177, |
148 | 0,8,8, 0,8,136, 0,8,72, 0,9,241, |
149 | 80,7,4, 0,8,84, 0,8,20, 85,8,227, |
150 | 83,7,43, 0,8,116, 0,8,52, 0,9,201, |
151 | 81,7,13, 0,8,100, 0,8,36, 0,9,169, |
152 | 0,8,4, 0,8,132, 0,8,68, 0,9,233, |
153 | 80,7,8, 0,8,92, 0,8,28, 0,9,153, |
154 | 84,7,83, 0,8,124, 0,8,60, 0,9,217, |
155 | 82,7,23, 0,8,108, 0,8,44, 0,9,185, |
156 | 0,8,12, 0,8,140, 0,8,76, 0,9,249, |
157 | 80,7,3, 0,8,82, 0,8,18, 85,8,163, |
158 | 83,7,35, 0,8,114, 0,8,50, 0,9,197, |
159 | 81,7,11, 0,8,98, 0,8,34, 0,9,165, |
160 | 0,8,2, 0,8,130, 0,8,66, 0,9,229, |
161 | 80,7,7, 0,8,90, 0,8,26, 0,9,149, |
162 | 84,7,67, 0,8,122, 0,8,58, 0,9,213, |
163 | 82,7,19, 0,8,106, 0,8,42, 0,9,181, |
164 | 0,8,10, 0,8,138, 0,8,74, 0,9,245, |
165 | 80,7,5, 0,8,86, 0,8,22, 192,8,0, |
166 | 83,7,51, 0,8,118, 0,8,54, 0,9,205, |
167 | 81,7,15, 0,8,102, 0,8,38, 0,9,173, |
168 | 0,8,6, 0,8,134, 0,8,70, 0,9,237, |
169 | 80,7,9, 0,8,94, 0,8,30, 0,9,157, |
170 | 84,7,99, 0,8,126, 0,8,62, 0,9,221, |
171 | 82,7,27, 0,8,110, 0,8,46, 0,9,189, |
172 | 0,8,14, 0,8,142, 0,8,78, 0,9,253, |
173 | 96,7,256, 0,8,81, 0,8,17, 85,8,131, |
174 | 82,7,31, 0,8,113, 0,8,49, 0,9,195, |
175 | 80,7,10, 0,8,97, 0,8,33, 0,9,163, |
176 | 0,8,1, 0,8,129, 0,8,65, 0,9,227, |
177 | 80,7,6, 0,8,89, 0,8,25, 0,9,147, |
178 | 83,7,59, 0,8,121, 0,8,57, 0,9,211, |
179 | 81,7,17, 0,8,105, 0,8,41, 0,9,179, |
180 | 0,8,9, 0,8,137, 0,8,73, 0,9,243, |
181 | 80,7,4, 0,8,85, 0,8,21, 80,8,258, |
182 | 83,7,43, 0,8,117, 0,8,53, 0,9,203, |
183 | 81,7,13, 0,8,101, 0,8,37, 0,9,171, |
184 | 0,8,5, 0,8,133, 0,8,69, 0,9,235, |
185 | 80,7,8, 0,8,93, 0,8,29, 0,9,155, |
186 | 84,7,83, 0,8,125, 0,8,61, 0,9,219, |
187 | 82,7,23, 0,8,109, 0,8,45, 0,9,187, |
188 | 0,8,13, 0,8,141, 0,8,77, 0,9,251, |
189 | 80,7,3, 0,8,83, 0,8,19, 85,8,195, |
190 | 83,7,35, 0,8,115, 0,8,51, 0,9,199, |
191 | 81,7,11, 0,8,99, 0,8,35, 0,9,167, |
192 | 0,8,3, 0,8,131, 0,8,67, 0,9,231, |
193 | 80,7,7, 0,8,91, 0,8,27, 0,9,151, |
194 | 84,7,67, 0,8,123, 0,8,59, 0,9,215, |
195 | 82,7,19, 0,8,107, 0,8,43, 0,9,183, |
196 | 0,8,11, 0,8,139, 0,8,75, 0,9,247, |
197 | 80,7,5, 0,8,87, 0,8,23, 192,8,0, |
198 | 83,7,51, 0,8,119, 0,8,55, 0,9,207, |
199 | 81,7,15, 0,8,103, 0,8,39, 0,9,175, |
200 | 0,8,7, 0,8,135, 0,8,71, 0,9,239, |
201 | 80,7,9, 0,8,95, 0,8,31, 0,9,159, |
202 | 84,7,99, 0,8,127, 0,8,63, 0,9,223, |
203 | 82,7,27, 0,8,111, 0,8,47, 0,9,191, |
204 | 0,8,15, 0,8,143, 0,8,79, 0,9,255 |
205 | ]; |
206 | var fixed_td = [ |
207 | 80,5,1, 87,5,257, 83,5,17, 91,5,4097, |
208 | 81,5,5, 89,5,1025, 85,5,65, 93,5,16385, |
209 | 80,5,3, 88,5,513, 84,5,33, 92,5,8193, |
210 | 82,5,9, 90,5,2049, 86,5,129, 192,5,24577, |
211 | 80,5,2, 87,5,385, 83,5,25, 91,5,6145, |
212 | 81,5,7, 89,5,1537, 85,5,97, 93,5,24577, |
213 | 80,5,4, 88,5,769, 84,5,49, 92,5,12289, |
214 | 82,5,13, 90,5,3073, 86,5,193, 192,5,24577 |
215 | ]; |
216 | |
217 | // Tables for deflate from PKZIP's appnote.txt. |
218 | var cplens = [ // Copy lengths for literal codes 257..285 |
219 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
220 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
221 | ]; |
222 | |
223 | // see note #13 above about 258 |
224 | var cplext = [ // Extra bits for literal codes 257..285 |
225 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
226 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 // 112==invalid |
227 | ]; |
228 | |
229 | var cpdist = [ // Copy offsets for distance codes 0..29 |
230 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
231 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
232 | 8193, 12289, 16385, 24577 |
233 | ]; |
234 | |
235 | var cpdext = [ // Extra bits for distance codes |
236 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
237 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
238 | 12, 12, 13, 13]; |
239 | |
240 | // |
241 | // ZStream.java |
242 | // |
243 | |
244 | function ZStream() { |
245 | } |
246 | |
247 | |
248 | ZStream.prototype.inflateInit = function(w, nowrap) { |
249 | if (!w) { |
250 | w = DEF_WBITS; |
251 | } |
252 | if (nowrap) { |
253 | nowrap = false; |
254 | } |
255 | this.istate = new Inflate(); |
256 | return this.istate.inflateInit(this, nowrap?-w:w); |
257 | } |
258 | |
259 | ZStream.prototype.inflate = function(f) { |
260 | if(this.istate==null) return Z_STREAM_ERROR; |
261 | return this.istate.inflate(this, f); |
262 | } |
263 | |
264 | ZStream.prototype.inflateEnd = function(){ |
265 | if(this.istate==null) return Z_STREAM_ERROR; |
266 | var ret=istate.inflateEnd(this); |
267 | this.istate = null; |
268 | return ret; |
269 | } |
270 | ZStream.prototype.inflateSync = function(){ |
271 | // if(istate == null) return Z_STREAM_ERROR; |
272 | return istate.inflateSync(this); |
273 | } |
274 | ZStream.prototype.inflateSetDictionary = function(dictionary, dictLength){ |
275 | // if(istate == null) return Z_STREAM_ERROR; |
276 | return istate.inflateSetDictionary(this, dictionary, dictLength); |
277 | } |
278 | |
279 | /* |
280 | |
281 | public int deflateInit(int level){ |
282 | return deflateInit(level, MAX_WBITS); |
283 | } |
284 | public int deflateInit(int level, boolean nowrap){ |
285 | return deflateInit(level, MAX_WBITS, nowrap); |
286 | } |
287 | public int deflateInit(int level, int bits){ |
288 | return deflateInit(level, bits, false); |
289 | } |
290 | public int deflateInit(int level, int bits, boolean nowrap){ |
291 | dstate=new Deflate(); |
292 | return dstate.deflateInit(this, level, nowrap?-bits:bits); |
293 | } |
294 | public int deflate(int flush){ |
295 | if(dstate==null){ |
296 | return Z_STREAM_ERROR; |
297 | } |
298 | return dstate.deflate(this, flush); |
299 | } |
300 | public int deflateEnd(){ |
301 | if(dstate==null) return Z_STREAM_ERROR; |
302 | int ret=dstate.deflateEnd(); |
303 | dstate=null; |
304 | return ret; |
305 | } |
306 | public int deflateParams(int level, int strategy){ |
307 | if(dstate==null) return Z_STREAM_ERROR; |
308 | return dstate.deflateParams(this, level, strategy); |
309 | } |
310 | public int deflateSetDictionary (byte[] dictionary, int dictLength){ |
311 | if(dstate == null) |
312 | return Z_STREAM_ERROR; |
313 | return dstate.deflateSetDictionary(this, dictionary, dictLength); |
314 | } |
315 | |
316 | */ |
317 | |
318 | /* |
319 | // Flush as much pending output as possible. All deflate() output goes |
320 | // through this function so some applications may wish to modify it |
321 | // to avoid allocating a large strm->next_out buffer and copying into it. |
322 | // (See also read_buf()). |
323 | void flush_pending(){ |
324 | int len=dstate.pending; |
325 | |
326 | if(len>avail_out) len=avail_out; |
327 | if(len==0) return; |
328 | |
329 | if(dstate.pending_buf.length<=dstate.pending_out || |
330 | next_out.length<=next_out_index || |
331 | dstate.pending_buf.length<(dstate.pending_out+len) || |
332 | next_out.length<(next_out_index+len)){ |
333 | System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+ |
334 | ", "+next_out.length+", "+next_out_index+", "+len); |
335 | System.out.println("avail_out="+avail_out); |
336 | } |
337 | |
338 | System.arraycopy(dstate.pending_buf, dstate.pending_out, |
339 | next_out, next_out_index, len); |
340 | |
341 | next_out_index+=len; |
342 | dstate.pending_out+=len; |
343 | total_out+=len; |
344 | avail_out-=len; |
345 | dstate.pending-=len; |
346 | if(dstate.pending==0){ |
347 | dstate.pending_out=0; |
348 | } |
349 | } |
350 | |
351 | // Read a new buffer from the current input stream, update the adler32 |
352 | // and total number of bytes read. All deflate() input goes through |
353 | // this function so some applications may wish to modify it to avoid |
354 | // allocating a large strm->next_in buffer and copying from it. |
355 | // (See also flush_pending()). |
356 | int read_buf(byte[] buf, int start, int size) { |
357 | int len=avail_in; |
358 | |
359 | if(len>size) len=size; |
360 | if(len==0) return 0; |
361 | |
362 | avail_in-=len; |
363 | |
364 | if(dstate.noheader==0) { |
365 | adler=_adler.adler32(adler, next_in, next_in_index, len); |
366 | } |
367 | System.arraycopy(next_in, next_in_index, buf, start, len); |
368 | next_in_index += len; |
369 | total_in += len; |
370 | return len; |
371 | } |
372 | |
373 | public void free(){ |
374 | next_in=null; |
375 | next_out=null; |
376 | msg=null; |
377 | _adler=null; |
378 | } |
379 | } |
380 | */ |
381 | |
382 | |
383 | // |
384 | // Inflate.java |
385 | // |
386 | |
387 | function Inflate() { |
388 | this.was = [0]; |
389 | } |
390 | |
391 | Inflate.prototype.inflateReset = function(z) { |
392 | if(z == null || z.istate == null) return Z_STREAM_ERROR; |
393 | |
394 | z.total_in = z.total_out = 0; |
395 | z.msg = null; |
396 | z.istate.mode = z.istate.nowrap!=0 ? BLOCKS : METHOD; |
397 | z.istate.blocks.reset(z, null); |
398 | return Z_OK; |
399 | } |
400 | |
401 | Inflate.prototype.inflateEnd = function(z){ |
402 | if(this.blocks != null) |
403 | this.blocks.free(z); |
404 | this.blocks=null; |
405 | return Z_OK; |
406 | } |
407 | |
408 | Inflate.prototype.inflateInit = function(z, w){ |
409 | z.msg = null; |
410 | this.blocks = null; |
411 | |
412 | // handle undocumented nowrap option (no zlib header or check) |
413 | nowrap = 0; |
414 | if(w < 0){ |
415 | w = - w; |
416 | nowrap = 1; |
417 | } |
418 | |
419 | // set window size |
420 | if(w<8 ||w>15){ |
421 | this.inflateEnd(z); |
422 | return Z_STREAM_ERROR; |
423 | } |
424 | this.wbits=w; |
425 | |
426 | z.istate.blocks=new InfBlocks(z, |
427 | z.istate.nowrap!=0 ? null : this, |
428 | 1<<w); |
429 | |
430 | // reset state |
431 | this.inflateReset(z); |
432 | return Z_OK; |
433 | } |
434 | |
435 | Inflate.prototype.inflate = function(z, f){ |
436 | var r, b; |
437 | |
438 | if(z == null || z.istate == null || z.next_in == null) |
439 | return Z_STREAM_ERROR; |
440 | f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; |
441 | r = Z_BUF_ERROR; |
442 | while (true){ |
443 | switch (z.istate.mode){ |
444 | case METHOD: |
445 | |
446 | if(z.avail_in==0)return r;r=f; |
447 | |
448 | z.avail_in--; z.total_in++; |
449 | if(((z.istate.method = z.next_in[z.next_in_index++])&0xf)!=Z_DEFLATED){ |
450 | z.istate.mode = BAD; |
451 | z.msg="unknown compression method"; |
452 | z.istate.marker = 5; // can't try inflateSync |
453 | break; |
454 | } |
455 | if((z.istate.method>>4)+8>z.istate.wbits){ |
456 | z.istate.mode = BAD; |
457 | z.msg="invalid window size"; |
458 | z.istate.marker = 5; // can't try inflateSync |
459 | break; |
460 | } |
461 | z.istate.mode=FLAG; |
462 | case FLAG: |
463 | |
464 | if(z.avail_in==0)return r;r=f; |
465 | |
466 | z.avail_in--; z.total_in++; |
467 | b = (z.next_in[z.next_in_index++])&0xff; |
468 | |
469 | if((((z.istate.method << 8)+b) % 31)!=0){ |
470 | z.istate.mode = BAD; |
471 | z.msg = "incorrect header check"; |
472 | z.istate.marker = 5; // can't try inflateSync |
473 | break; |
474 | } |
475 | |
476 | if((b&PRESET_DICT)==0){ |
477 | z.istate.mode = BLOCKS; |
478 | break; |
479 | } |
480 | z.istate.mode = DICT4; |
481 | case DICT4: |
482 | |
483 | if(z.avail_in==0)return r;r=f; |
484 | |
485 | z.avail_in--; z.total_in++; |
486 | z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000; |
487 | z.istate.mode=DICT3; |
488 | case DICT3: |
489 | |
490 | if(z.avail_in==0)return r;r=f; |
491 | |
492 | z.avail_in--; z.total_in++; |
493 | z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000; |
494 | z.istate.mode=DICT2; |
495 | case DICT2: |
496 | |
497 | if(z.avail_in==0)return r;r=f; |
498 | |
499 | z.avail_in--; z.total_in++; |
500 | z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00; |
501 | z.istate.mode=DICT1; |
502 | case DICT1: |
503 | |
504 | if(z.avail_in==0)return r;r=f; |
505 | |
506 | z.avail_in--; z.total_in++; |
507 | z.istate.need += (z.next_in[z.next_in_index++]&0xff); |
508 | z.adler = z.istate.need; |
509 | z.istate.mode = DICT0; |
510 | return Z_NEED_DICT; |
511 | case DICT0: |
512 | z.istate.mode = BAD; |
513 | z.msg = "need dictionary"; |
514 | z.istate.marker = 0; // can try inflateSync |
515 | return Z_STREAM_ERROR; |
516 | case BLOCKS: |
517 | |
518 | r = z.istate.blocks.proc(z, r); |
519 | if(r == Z_DATA_ERROR){ |
520 | z.istate.mode = BAD; |
521 | z.istate.marker = 0; // can try inflateSync |
522 | break; |
523 | } |
524 | if(r == Z_OK){ |
525 | r = f; |
526 | } |
527 | if(r != Z_STREAM_END){ |
528 | return r; |
529 | } |
530 | r = f; |
531 | z.istate.blocks.reset(z, z.istate.was); |
532 | if(z.istate.nowrap!=0){ |
533 | z.istate.mode=DONE; |
534 | break; |
535 | } |
536 | z.istate.mode=CHECK4; |
537 | case CHECK4: |
538 | |
539 | if(z.avail_in==0)return r;r=f; |
540 | |
541 | z.avail_in--; z.total_in++; |
542 | z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000; |
543 | z.istate.mode=CHECK3; |
544 | case CHECK3: |
545 | |
546 | if(z.avail_in==0)return r;r=f; |
547 | |
548 | z.avail_in--; z.total_in++; |
549 | z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000; |
550 | z.istate.mode = CHECK2; |
551 | case CHECK2: |
552 | |
553 | if(z.avail_in==0)return r;r=f; |
554 | |
555 | z.avail_in--; z.total_in++; |
556 | z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00; |
557 | z.istate.mode = CHECK1; |
558 | case CHECK1: |
559 | |
560 | if(z.avail_in==0)return r;r=f; |
561 | |
562 | z.avail_in--; z.total_in++; |
563 | z.istate.need+=(z.next_in[z.next_in_index++]&0xff); |
564 | |
565 | if(((z.istate.was[0])) != ((z.istate.need))){ |
566 | z.istate.mode = BAD; |
567 | z.msg = "incorrect data check"; |
568 | z.istate.marker = 5; // can't try inflateSync |
569 | break; |
570 | } |
571 | |
572 | z.istate.mode = DONE; |
573 | case DONE: |
574 | return Z_STREAM_END; |
575 | case BAD: |
576 | return Z_DATA_ERROR; |
577 | default: |
578 | return Z_STREAM_ERROR; |
579 | } |
580 | } |
581 | } |
582 | |
583 | |
584 | Inflate.prototype.inflateSetDictionary = function(z, dictionary, dictLength) { |
585 | var index=0; |
586 | var length = dictLength; |
587 | if(z==null || z.istate == null|| z.istate.mode != DICT0) |
588 | return Z_STREAM_ERROR; |
589 | |
590 | if(z._adler.adler32(1, dictionary, 0, dictLength)!=z.adler){ |
591 | return Z_DATA_ERROR; |
592 | } |
593 | |
594 | z.adler = z._adler.adler32(0, null, 0, 0); |
595 | |
596 | if(length >= (1<<z.istate.wbits)){ |
597 | length = (1<<z.istate.wbits)-1; |
598 | index=dictLength - length; |
599 | } |
600 | z.istate.blocks.set_dictionary(dictionary, index, length); |
601 | z.istate.mode = BLOCKS; |
602 | return Z_OK; |
603 | } |
604 | |
605 | // static private byte[] mark = {(byte)0, (byte)0, (byte)0xff, (byte)0xff}; |
606 | var mark = [0, 0, 255, 255] |
607 | |
608 | Inflate.prototype.inflateSync = function(z){ |
609 | var n; // number of bytes to look at |
610 | var p; // pointer to bytes |
611 | var m; // number of marker bytes found in a row |
612 | var r, w; // temporaries to save total_in and total_out |
613 | |
614 | // set up |
615 | if(z == null || z.istate == null) |
616 | return Z_STREAM_ERROR; |
617 | if(z.istate.mode != BAD){ |
618 | z.istate.mode = BAD; |
619 | z.istate.marker = 0; |
620 | } |
621 | if((n=z.avail_in)==0) |
622 | return Z_BUF_ERROR; |
623 | p=z.next_in_index; |
624 | m=z.istate.marker; |
625 | |
626 | // search |
627 | while (n!=0 && m < 4){ |
628 | if(z.next_in[p] == mark[m]){ |
629 | m++; |
630 | } |
631 | else if(z.next_in[p]!=0){ |
632 | m = 0; |
633 | } |
634 | else{ |
635 | m = 4 - m; |
636 | } |
637 | p++; n--; |
638 | } |
639 | |
640 | // restore |
641 | z.total_in += p-z.next_in_index; |
642 | z.next_in_index = p; |
643 | z.avail_in = n; |
644 | z.istate.marker = m; |
645 | |
646 | // return no joy or set up to restart on a new block |
647 | if(m != 4){ |
648 | return Z_DATA_ERROR; |
649 | } |
650 | r=z.total_in; w=z.total_out; |
651 | this.inflateReset(z); |
652 | z.total_in=r; z.total_out = w; |
653 | z.istate.mode = BLOCKS; |
654 | return Z_OK; |
655 | } |
656 | |
657 | // Returns true if inflate is currently at the end of a block generated |
658 | // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP |
659 | // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH |
660 | // but removes the length bytes of the resulting empty stored block. When |
661 | // decompressing, PPP checks that at the end of input packet, inflate is |
662 | // waiting for these length bytes. |
663 | Inflate.prototype.inflateSyncPoint = function(z){ |
664 | if(z == null || z.istate == null || z.istate.blocks == null) |
665 | return Z_STREAM_ERROR; |
666 | return z.istate.blocks.sync_point(); |
667 | } |
668 | |
669 | |
670 | // |
671 | // InfBlocks.java |
672 | // |
673 | |
674 | var INFBLOCKS_BORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; |
675 | |
676 | function InfBlocks(z, checkfn, w) { |
677 | this.hufts=new Int32Array(MANY*3); |
678 | this.window=new Uint8Array(w); |
679 | this.end=w; |
680 | this.checkfn = checkfn; |
681 | this.mode = IB_TYPE; |
682 | this.reset(z, null); |
683 | |
684 | this.left = 0; // if STORED, bytes left to copy |
685 | |
686 | this.table = 0; // table lengths (14 bits) |
687 | this.index = 0; // index into blens (or border) |
688 | this.blens = null; // bit lengths of codes |
689 | this.bb=new Int32Array(1); // bit length tree depth |
690 | this.tb=new Int32Array(1); // bit length decoding tree |
691 | |
692 | this.codes = new InfCodes(); |
693 | |
694 | this.last = 0; // true if this block is the last block |
695 | |
696 | // mode independent information |
697 | this.bitk = 0; // bits in bit buffer |
698 | this.bitb = 0; // bit buffer |
699 | this.read = 0; // window read pointer |
700 | this.write = 0; // window write pointer |
701 | this.check = 0; // check on output |
702 | |
703 | this.inftree=new InfTree(); |
704 | } |
705 | |
706 | |
707 | |
708 | |
709 | InfBlocks.prototype.reset = function(z, c){ |
710 | if(c) c[0]=this.check; |
711 | if(this.mode==IB_CODES){ |
712 | this.codes.free(z); |
713 | } |
714 | this.mode=IB_TYPE; |
715 | this.bitk=0; |
716 | this.bitb=0; |
717 | this.read=this.write=0; |
718 | |
719 | if(this.checkfn) |
720 | z.adler=this.check=z._adler.adler32(0, null, 0, 0); |
721 | } |
722 | |
723 | InfBlocks.prototype.proc = function(z, r){ |
724 | var t; // temporary storage |
725 | var b; // bit buffer |
726 | var k; // bits in bit buffer |
727 | var p; // input data pointer |
728 | var n; // bytes available there |
729 | var q; // output window write pointer |
730 | var m; // bytes to end of window or read pointer |
731 | |
732 | // copy input/output information to locals (UPDATE macro restores) |
733 | {p=z.next_in_index;n=z.avail_in;b=this.bitb;k=this.bitk;} |
734 | {q=this.write;m=(q<this.read ? this.read-q-1 : this.end-q);} |
735 | |
736 | // process input based on current state |
737 | while(true){ |
738 | switch (this.mode){ |
739 | case IB_TYPE: |
740 | |
741 | while(k<(3)){ |
742 | if(n!=0){ |
743 | r=Z_OK; |
744 | } |
745 | else{ |
746 | this.bitb=b; this.bitk=k; |
747 | z.avail_in=n; |
748 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
749 | this.write=q; |
750 | return this.inflate_flush(z,r); |
751 | }; |
752 | n--; |
753 | b|=(z.next_in[p++]&0xff)<<k; |
754 | k+=8; |
755 | } |
756 | t = (b & 7); |
757 | this.last = t & 1; |
758 | |
759 | switch (t >>> 1){ |
760 | case 0: // stored |
761 | {b>>>=(3);k-=(3);} |
762 | t = k & 7; // go to byte boundary |
763 | |
764 | {b>>>=(t);k-=(t);} |
765 | this.mode = IB_LENS; // get length of stored block |
766 | break; |
767 | case 1: // fixed |
768 | { |
769 | var bl=new Int32Array(1); |
770 | var bd=new Int32Array(1); |
771 | var tl=[]; |
772 | var td=[]; |
773 | |
774 | inflate_trees_fixed(bl, bd, tl, td, z); |
775 | this.codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z); |
776 | } |
777 | |
778 | {b>>>=(3);k-=(3);} |
779 | |
780 | this.mode = IB_CODES; |
781 | break; |
782 | case 2: // dynamic |
783 | |
784 | {b>>>=(3);k-=(3);} |
785 | |
786 | this.mode = IB_TABLE; |
787 | break; |
788 | case 3: // illegal |
789 | |
790 | {b>>>=(3);k-=(3);} |
791 | this.mode = BAD; |
792 | z.msg = "invalid block type"; |
793 | r = Z_DATA_ERROR; |
794 | |
795 | this.bitb=b; this.bitk=k; |
796 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
797 | this.write=q; |
798 | return this.inflate_flush(z,r); |
799 | } |
800 | break; |
801 | case IB_LENS: |
802 | while(k<(32)){ |
803 | if(n!=0){ |
804 | r=Z_OK; |
805 | } |
806 | else{ |
807 | this.bitb=b; this.bitk=k; |
808 | z.avail_in=n; |
809 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
810 | this.write=q; |
811 | return this.inflate_flush(z,r); |
812 | }; |
813 | n--; |
814 | b|=(z.next_in[p++]&0xff)<<k; |
815 | k+=8; |
816 | } |
817 | |
818 | if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)){ |
819 | this.mode = BAD; |
820 | z.msg = "invalid stored block lengths"; |
821 | r = Z_DATA_ERROR; |
822 | |
823 | this.bitb=b; this.bitk=k; |
824 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
825 | this.write=q; |
826 | return this.inflate_flush(z,r); |
827 | } |
828 | this.left = (b & 0xffff); |
829 | b = k = 0; // dump bits |
830 | this.mode = this.left!=0 ? IB_STORED : (this.last!=0 ? IB_DRY : IB_TYPE); |
831 | break; |
832 | case IB_STORED: |
833 | if (n == 0){ |
834 | this.bitb=b; this.bitk=k; |
835 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
836 | write=q; |
837 | return this.inflate_flush(z,r); |
838 | } |
839 | |
840 | if(m==0){ |
841 | if(q==end&&read!=0){ |
842 | q=0; m=(q<this.read ? this.read-q-1 : this.end-q); |
843 | } |
844 | if(m==0){ |
845 | this.write=q; |
846 | r=this.inflate_flush(z,r); |
847 | q=this.write; m = (q < this.read ? this.read-q-1 : this.end-q); |
848 | if(q==this.end && this.read != 0){ |
849 | q=0; m = (q < this.read ? this.read-q-1 : this.end-q); |
850 | } |
851 | if(m==0){ |
852 | this.bitb=b; this.bitk=k; |
853 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
854 | this.write=q; |
855 | return this.inflate_flush(z,r); |
856 | } |
857 | } |
858 | } |
859 | r=Z_OK; |
860 | |
861 | t = this.left; |
862 | if(t>n) t = n; |
863 | if(t>m) t = m; |
864 | arrayCopy(z.next_in, p, this.window, q, t); |
865 | p += t; n -= t; |
866 | q += t; m -= t; |
867 | if ((this.left -= t) != 0) |
868 | break; |
869 | this.mode = (this.last != 0 ? IB_DRY : IB_TYPE); |
870 | break; |
871 | case IB_TABLE: |
872 | |
873 | while(k<(14)){ |
874 | if(n!=0){ |
875 | r=Z_OK; |
876 | } |
877 | else{ |
878 | this.bitb=b; this.bitk=k; |
879 | z.avail_in=n; |
880 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
881 | this.write=q; |
882 | return this.inflate_flush(z,r); |
883 | }; |
884 | n--; |
885 | b|=(z.next_in[p++]&0xff)<<k; |
886 | k+=8; |
887 | } |
888 | |
889 | this.table = t = (b & 0x3fff); |
890 | if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) |
891 | { |
892 | this.mode = IB_BAD; |
893 | z.msg = "too many length or distance symbols"; |
894 | r = Z_DATA_ERROR; |
895 | |
896 | this.bitb=b; this.bitk=k; |
897 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
898 | this.write=q; |
899 | return this.inflate_flush(z,r); |
900 | } |
901 | t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); |
902 | if(this.blens==null || this.blens.length<t){ |
903 | this.blens=new Int32Array(t); |
904 | } |
905 | else{ |
906 | for(var i=0; i<t; i++){ |
907 | this.blens[i]=0; |
908 | } |
909 | } |
910 | |
911 | {b>>>=(14);k-=(14);} |
912 | |
913 | this.index = 0; |
914 | mode = IB_BTREE; |
915 | case IB_BTREE: |
916 | while (this.index < 4 + (this.table >>> 10)){ |
917 | while(k<(3)){ |
918 | if(n!=0){ |
919 | r=Z_OK; |
920 | } |
921 | else{ |
922 | this.bitb=b; this.bitk=k; |
923 | z.avail_in=n; |
924 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
925 | this.write=q; |
926 | return this.inflate_flush(z,r); |
927 | }; |
928 | n--; |
929 | b|=(z.next_in[p++]&0xff)<<k; |
930 | k+=8; |
931 | } |
932 | |
933 | this.blens[INFBLOCKS_BORDER[this.index++]] = b&7; |
934 | |
935 | {b>>>=(3);k-=(3);} |
936 | } |
937 | |
938 | while(this.index < 19){ |
939 | this.blens[INFBLOCKS_BORDER[this.index++]] = 0; |
940 | } |
941 | |
942 | this.bb[0] = 7; |
943 | t = this.inftree.inflate_trees_bits(this.blens, this.bb, this.tb, this.hufts, z); |
944 | if (t != Z_OK){ |
945 | r = t; |
946 | if (r == Z_DATA_ERROR){ |
947 | this.blens=null; |
948 | this.mode = IB_BAD; |
949 | } |
950 | |
951 | this.bitb=b; this.bitk=k; |
952 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
953 | write=q; |
954 | return this.inflate_flush(z,r); |
955 | } |
956 | |
957 | this.index = 0; |
958 | this.mode = IB_DTREE; |
959 | case IB_DTREE: |
960 | while (true){ |
961 | t = this.table; |
962 | if(!(this.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){ |
963 | break; |
964 | } |
965 | |
966 | var h; //int[] |
967 | var i, j, c; |
968 | |
969 | t = this.bb[0]; |
970 | |
971 | while(k<(t)){ |
972 | if(n!=0){ |
973 | r=Z_OK; |
974 | } |
975 | else{ |
976 | this.bitb=b; this.bitk=k; |
977 | z.avail_in=n; |
978 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
979 | this.write=q; |
980 | return this.inflate_flush(z,r); |
981 | }; |
982 | n--; |
983 | b|=(z.next_in[p++]&0xff)<<k; |
984 | k+=8; |
985 | } |
986 | |
987 | // if (this.tb[0]==-1){ |
988 | // dlog("null..."); |
989 | // } |
990 | |
991 | t=this.hufts[(this.tb[0]+(b & inflate_mask[t]))*3+1]; |
992 | c=this.hufts[(this.tb[0]+(b & inflate_mask[t]))*3+2]; |
993 | |
994 | if (c < 16){ |
995 | b>>>=(t);k-=(t); |
996 | this.blens[this.index++] = c; |
997 | } |
998 | else { // c == 16..18 |
999 | i = c == 18 ? 7 : c - 14; |
1000 | j = c == 18 ? 11 : 3; |
1001 | |
1002 | while(k<(t+i)){ |
1003 | if(n!=0){ |
1004 | r=Z_OK; |
1005 | } |
1006 | else{ |
1007 | this.bitb=b; this.bitk=k; |
1008 | z.avail_in=n; |
1009 | z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1010 | this.write=q; |
1011 | return this.inflate_flush(z,r); |
1012 | }; |
1013 | n--; |
1014 | b|=(z.next_in[p++]&0xff)<<k; |
1015 | k+=8; |
1016 | } |
1017 | |
1018 | b>>>=(t);k-=(t); |
1019 | |
1020 | j += (b & inflate_mask[i]); |
1021 | |
1022 | b>>>=(i);k-=(i); |
1023 | |
1024 | i = this.index; |
1025 | t = this.table; |
1026 | if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || |
1027 | (c == 16 && i < 1)){ |
1028 | this.blens=null; |
1029 | this.mode = IB_BAD; |
1030 | z.msg = "invalid bit length repeat"; |
1031 | r = Z_DATA_ERROR; |
1032 | |
1033 | this.bitb=b; this.bitk=k; |
1034 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1035 | this.write=q; |
1036 | return this.inflate_flush(z,r); |
1037 | } |
1038 | |
1039 | c = c == 16 ? this.blens[i-1] : 0; |
1040 | do{ |
1041 | this.blens[i++] = c; |
1042 | } |
1043 | while (--j!=0); |
1044 | this.index = i; |
1045 | } |
1046 | } |
1047 | |
1048 | this.tb[0]=-1; |
1049 | { |
1050 | var bl=new Int32Array(1); |
1051 | var bd=new Int32Array(1); |
1052 | var tl=new Int32Array(1); |
1053 | var td=new Int32Array(1); |
1054 | bl[0] = 9; // must be <= 9 for lookahead assumptions |
1055 | bd[0] = 6; // must be <= 9 for lookahead assumptions |
1056 | |
1057 | t = this.table; |
1058 | t = this.inftree.inflate_trees_dynamic(257 + (t & 0x1f), |
1059 | 1 + ((t >> 5) & 0x1f), |
1060 | this.blens, bl, bd, tl, td, this.hufts, z); |
1061 | |
1062 | if (t != Z_OK){ |
1063 | if (t == Z_DATA_ERROR){ |
1064 | this.blens=null; |
1065 | this.mode = BAD; |
1066 | } |
1067 | r = t; |
1068 | |
1069 | this.bitb=b; this.bitk=k; |
1070 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1071 | this.write=q; |
1072 | return this.inflate_flush(z,r); |
1073 | } |
1074 | this.codes.init(bl[0], bd[0], this.hufts, tl[0], this.hufts, td[0], z); |
1075 | } |
1076 | this.mode = IB_CODES; |
1077 | case IB_CODES: |
1078 | this.bitb=b; this.bitk=k; |
1079 | z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1080 | this.write=q; |
1081 | |
1082 | if ((r = this.codes.proc(this, z, r)) != Z_STREAM_END){ |
1083 | return this.inflate_flush(z, r); |
1084 | } |
1085 | r = Z_OK; |
1086 | this.codes.free(z); |
1087 | |
1088 | p=z.next_in_index; n=z.avail_in;b=this.bitb;k=this.bitk; |
1089 | q=this.write;m = (q < this.read ? this.read-q-1 : this.end-q); |
1090 | |
1091 | if (this.last==0){ |
1092 | this.mode = IB_TYPE; |
1093 | break; |
1094 | } |
1095 | this.mode = IB_DRY; |
1096 | case IB_DRY: |
1097 | this.write=q; |
1098 | r = this.inflate_flush(z, r); |
1099 | q=this.write; m = (q < this.read ? this.read-q-1 : this.end-q); |
1100 | if (this.read != this.write){ |
1101 | this.bitb=b; this.bitk=k; |
1102 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1103 | this.write=q; |
1104 | return this.inflate_flush(z, r); |
1105 | } |
1106 | mode = DONE; |
1107 | case IB_DONE: |
1108 | r = Z_STREAM_END; |
1109 | |
1110 | this.bitb=b; this.bitk=k; |
1111 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1112 | this.write=q; |
1113 | return this.inflate_flush(z, r); |
1114 | case IB_BAD: |
1115 | r = Z_DATA_ERROR; |
1116 | |
1117 | this.bitb=b; this.bitk=k; |
1118 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1119 | this.write=q; |
1120 | return this.inflate_flush(z, r); |
1121 | |
1122 | default: |
1123 | r = Z_STREAM_ERROR; |
1124 | |
1125 | this.bitb=b; this.bitk=k; |
1126 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1127 | this.write=q; |
1128 | return this.inflate_flush(z, r); |
1129 | } |
1130 | } |
1131 | } |
1132 | |
1133 | InfBlocks.prototype.free = function(z){ |
1134 | this.reset(z, null); |
1135 | this.window=null; |
1136 | this.hufts=null; |
1137 | } |
1138 | |
1139 | InfBlocks.prototype.set_dictionary = function(d, start, n){ |
1140 | arrayCopy(d, start, window, 0, n); |
1141 | this.read = this.write = n; |
1142 | } |
1143 | |
1144 | // Returns true if inflate is currently at the end of a block generated |
1145 | // by Z_SYNC_FLUSH or Z_FULL_FLUSH. |
1146 | InfBlocks.prototype.sync_point = function(){ |
1147 | return this.mode == IB_LENS; |
1148 | } |
1149 | |
1150 | // copy as much as possible from the sliding window to the output area |
1151 | InfBlocks.prototype.inflate_flush = function(z, r){ |
1152 | var n; |
1153 | var p; |
1154 | var q; |
1155 | |
1156 | // local copies of source and destination pointers |
1157 | p = z.next_out_index; |
1158 | q = this.read; |
1159 | |
1160 | // compute number of bytes to copy as far as end of window |
1161 | n = ((q <= this.write ? this.write : this.end) - q); |
1162 | if (n > z.avail_out) n = z.avail_out; |
1163 | if (n!=0 && r == Z_BUF_ERROR) r = Z_OK; |
1164 | |
1165 | // update counters |
1166 | z.avail_out -= n; |
1167 | z.total_out += n; |
1168 | |
1169 | // update check information |
1170 | if(this.checkfn != null) |
1171 | z.adler=this.check=z._adler.adler32(this.check, this.window, q, n); |
1172 | |
1173 | // copy as far as end of window |
1174 | arrayCopy(this.window, q, z.next_out, p, n); |
1175 | p += n; |
1176 | q += n; |
1177 | |
1178 | // see if more to copy at beginning of window |
1179 | if (q == this.end){ |
1180 | // wrap pointers |
1181 | q = 0; |
1182 | if (this.write == this.end) |
1183 | this.write = 0; |
1184 | |
1185 | // compute bytes to copy |
1186 | n = this.write - q; |
1187 | if (n > z.avail_out) n = z.avail_out; |
1188 | if (n!=0 && r == Z_BUF_ERROR) r = Z_OK; |
1189 | |
1190 | // update counters |
1191 | z.avail_out -= n; |
1192 | z.total_out += n; |
1193 | |
1194 | // update check information |
1195 | if(this.checkfn != null) |
1196 | z.adler=this.check=z._adler.adler32(this.check, this.window, q, n); |
1197 | |
1198 | // copy |
1199 | arrayCopy(this.window, q, z.next_out, p, n); |
1200 | p += n; |
1201 | q += n; |
1202 | } |
1203 | |
1204 | // update pointers |
1205 | z.next_out_index = p; |
1206 | this.read = q; |
1207 | |
1208 | // done |
1209 | return r; |
1210 | } |
1211 | |
1212 | // |
1213 | // InfCodes.java |
1214 | // |
1215 | |
1216 | var IC_START=0; // x: set up for LEN |
1217 | var IC_LEN=1; // i: get length/literal/eob next |
1218 | var IC_LENEXT=2; // i: getting length extra (have base) |
1219 | var IC_DIST=3; // i: get distance next |
1220 | var IC_DISTEXT=4;// i: getting distance extra |
1221 | var IC_COPY=5; // o: copying bytes in window, waiting for space |
1222 | var IC_LIT=6; // o: got literal, waiting for output space |
1223 | var IC_WASH=7; // o: got eob, possibly still output waiting |
1224 | var IC_END=8; // x: got eob and all data flushed |
1225 | var IC_BADCODE=9;// x: got error |
1226 | |
1227 | function InfCodes() { |
1228 | } |
1229 | |
1230 | InfCodes.prototype.init = function(bl, bd, tl, tl_index, td, td_index, z) { |
1231 | this.mode=IC_START; |
1232 | this.lbits=bl; |
1233 | this.dbits=bd; |
1234 | this.ltree=tl; |
1235 | this.ltree_index=tl_index; |
1236 | this.dtree = td; |
1237 | this.dtree_index=td_index; |
1238 | this.tree=null; |
1239 | } |
1240 | |
1241 | InfCodes.prototype.proc = function(s, z, r){ |
1242 | var j; // temporary storage |
1243 | var t; // temporary pointer (int[]) |
1244 | var tindex; // temporary pointer |
1245 | var e; // extra bits or operation |
1246 | var b=0; // bit buffer |
1247 | var k=0; // bits in bit buffer |
1248 | var p=0; // input data pointer |
1249 | var n; // bytes available there |
1250 | var q; // output window write pointer |
1251 | var m; // bytes to end of window or read pointer |
1252 | var f; // pointer to copy strings from |
1253 | |
1254 | // copy input/output information to locals (UPDATE macro restores) |
1255 | p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; |
1256 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1257 | |
1258 | // process input and output based on current state |
1259 | while (true){ |
1260 | switch (this.mode){ |
1261 | // waiting for "i:"=input, "o:"=output, "x:"=nothing |
1262 | case IC_START: // x: set up for LEN |
1263 | if (m >= 258 && n >= 10){ |
1264 | |
1265 | s.bitb=b;s.bitk=k; |
1266 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1267 | s.write=q; |
1268 | r = this.inflate_fast(this.lbits, this.dbits, |
1269 | this.ltree, this.ltree_index, |
1270 | this.dtree, this.dtree_index, |
1271 | s, z); |
1272 | |
1273 | p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; |
1274 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1275 | |
1276 | if (r != Z_OK){ |
1277 | this.mode = r == Z_STREAM_END ? IC_WASH : IC_BADCODE; |
1278 | break; |
1279 | } |
1280 | } |
1281 | this.need = this.lbits; |
1282 | this.tree = this.ltree; |
1283 | this.tree_index=this.ltree_index; |
1284 | |
1285 | this.mode = IC_LEN; |
1286 | case IC_LEN: // i: get length/literal/eob next |
1287 | j = this.need; |
1288 | |
1289 | while(k<(j)){ |
1290 | if(n!=0)r=Z_OK; |
1291 | else{ |
1292 | |
1293 | s.bitb=b;s.bitk=k; |
1294 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1295 | s.write=q; |
1296 | return s.inflate_flush(z,r); |
1297 | } |
1298 | n--; |
1299 | b|=(z.next_in[p++]&0xff)<<k; |
1300 | k+=8; |
1301 | } |
1302 | |
1303 | tindex=(this.tree_index+(b&inflate_mask[j]))*3; |
1304 | |
1305 | b>>>=(this.tree[tindex+1]); |
1306 | k-=(this.tree[tindex+1]); |
1307 | |
1308 | e=this.tree[tindex]; |
1309 | |
1310 | if(e == 0){ // literal |
1311 | this.lit = this.tree[tindex+2]; |
1312 | this.mode = IC_LIT; |
1313 | break; |
1314 | } |
1315 | if((e & 16)!=0 ){ // length |
1316 | this.get = e & 15; |
1317 | this.len = this.tree[tindex+2]; |
1318 | this.mode = IC_LENEXT; |
1319 | break; |
1320 | } |
1321 | if ((e & 64) == 0){ // next table |
1322 | this.need = e; |
1323 | this.tree_index = tindex/3 + this.tree[tindex+2]; |
1324 | break; |
1325 | } |
1326 | if ((e & 32)!=0){ // end of block |
1327 | this.mode = IC_WASH; |
1328 | break; |
1329 | } |
1330 | this.mode = IC_BADCODE; // invalid code |
1331 | z.msg = "invalid literal/length code"; |
1332 | r = Z_DATA_ERROR; |
1333 | |
1334 | s.bitb=b;s.bitk=k; |
1335 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1336 | s.write=q; |
1337 | return s.inflate_flush(z,r); |
1338 | |
1339 | case IC_LENEXT: // i: getting length extra (have base) |
1340 | j = this.get; |
1341 | |
1342 | while(k<(j)){ |
1343 | if(n!=0)r=Z_OK; |
1344 | else{ |
1345 | |
1346 | s.bitb=b;s.bitk=k; |
1347 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1348 | s.write=q; |
1349 | return s.inflate_flush(z,r); |
1350 | } |
1351 | n--; b|=(z.next_in[p++]&0xff)<<k; |
1352 | k+=8; |
1353 | } |
1354 | |
1355 | this.len += (b & inflate_mask[j]); |
1356 | |
1357 | b>>=j; |
1358 | k-=j; |
1359 | |
1360 | this.need = this.dbits; |
1361 | this.tree = this.dtree; |
1362 | this.tree_index = this.dtree_index; |
1363 | this.mode = IC_DIST; |
1364 | case IC_DIST: // i: get distance next |
1365 | j = this.need; |
1366 | |
1367 | while(k<(j)){ |
1368 | if(n!=0)r=Z_OK; |
1369 | else{ |
1370 | |
1371 | s.bitb=b;s.bitk=k; |
1372 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1373 | s.write=q; |
1374 | return s.inflate_flush(z,r); |
1375 | } |
1376 | n--; b|=(z.next_in[p++]&0xff)<<k; |
1377 | k+=8; |
1378 | } |
1379 | |
1380 | tindex=(this.tree_index+(b & inflate_mask[j]))*3; |
1381 | |
1382 | b>>=this.tree[tindex+1]; |
1383 | k-=this.tree[tindex+1]; |
1384 | |
1385 | e = (this.tree[tindex]); |
1386 | if((e & 16)!=0){ // distance |
1387 | this.get = e & 15; |
1388 | this.dist = this.tree[tindex+2]; |
1389 | this.mode = IC_DISTEXT; |
1390 | break; |
1391 | } |
1392 | if ((e & 64) == 0){ // next table |
1393 | this.need = e; |
1394 | this.tree_index = tindex/3 + this.tree[tindex+2]; |
1395 | break; |
1396 | } |
1397 | this.mode = IC_BADCODE; // invalid code |
1398 | z.msg = "invalid distance code"; |
1399 | r = Z_DATA_ERROR; |
1400 | |
1401 | s.bitb=b;s.bitk=k; |
1402 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1403 | s.write=q; |
1404 | return s.inflate_flush(z,r); |
1405 | |
1406 | case IC_DISTEXT: // i: getting distance extra |
1407 | j = this.get; |
1408 | |
1409 | while(k<(j)){ |
1410 | if(n!=0)r=Z_OK; |
1411 | else{ |
1412 | |
1413 | s.bitb=b;s.bitk=k; |
1414 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1415 | s.write=q; |
1416 | return s.inflate_flush(z,r); |
1417 | } |
1418 | n--; b|=(z.next_in[p++]&0xff)<<k; |
1419 | k+=8; |
1420 | } |
1421 | |
1422 | this.dist += (b & inflate_mask[j]); |
1423 | |
1424 | b>>=j; |
1425 | k-=j; |
1426 | |
1427 | this.mode = IC_COPY; |
1428 | case IC_COPY: // o: copying bytes in window, waiting for space |
1429 | f = q - this.dist; |
1430 | while(f < 0){ // modulo window size-"while" instead |
1431 | f += s.end; // of "if" handles invalid distances |
1432 | } |
1433 | while (this.len!=0){ |
1434 | |
1435 | if(m==0){ |
1436 | if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} |
1437 | if(m==0){ |
1438 | s.write=q; r=s.inflate_flush(z,r); |
1439 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1440 | |
1441 | if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} |
1442 | |
1443 | if(m==0){ |
1444 | s.bitb=b;s.bitk=k; |
1445 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1446 | s.write=q; |
1447 | return s.inflate_flush(z,r); |
1448 | } |
1449 | } |
1450 | } |
1451 | |
1452 | s.window[q++]=s.window[f++]; m--; |
1453 | |
1454 | if (f == s.end) |
1455 | f = 0; |
1456 | this.len--; |
1457 | } |
1458 | this.mode = IC_START; |
1459 | break; |
1460 | case IC_LIT: // o: got literal, waiting for output space |
1461 | if(m==0){ |
1462 | if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} |
1463 | if(m==0){ |
1464 | s.write=q; r=s.inflate_flush(z,r); |
1465 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1466 | |
1467 | if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} |
1468 | if(m==0){ |
1469 | s.bitb=b;s.bitk=k; |
1470 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1471 | s.write=q; |
1472 | return s.inflate_flush(z,r); |
1473 | } |
1474 | } |
1475 | } |
1476 | r=Z_OK; |
1477 | |
1478 | s.window[q++]=this.lit; m--; |
1479 | |
1480 | this.mode = IC_START; |
1481 | break; |
1482 | case IC_WASH: // o: got eob, possibly more output |
1483 | if (k > 7){ // return unused byte, if any |
1484 | k -= 8; |
1485 | n++; |
1486 | p--; // can always return one |
1487 | } |
1488 | |
1489 | s.write=q; r=s.inflate_flush(z,r); |
1490 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1491 | |
1492 | if (s.read != s.write){ |
1493 | s.bitb=b;s.bitk=k; |
1494 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1495 | s.write=q; |
1496 | return s.inflate_flush(z,r); |
1497 | } |
1498 | this.mode = IC_END; |
1499 | case IC_END: |
1500 | r = Z_STREAM_END; |
1501 | s.bitb=b;s.bitk=k; |
1502 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1503 | s.write=q; |
1504 | return s.inflate_flush(z,r); |
1505 | |
1506 | case IC_BADCODE: // x: got error |
1507 | |
1508 | r = Z_DATA_ERROR; |
1509 | |
1510 | s.bitb=b;s.bitk=k; |
1511 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1512 | s.write=q; |
1513 | return s.inflate_flush(z,r); |
1514 | |
1515 | default: |
1516 | r = Z_STREAM_ERROR; |
1517 | |
1518 | s.bitb=b;s.bitk=k; |
1519 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1520 | s.write=q; |
1521 | return s.inflate_flush(z,r); |
1522 | } |
1523 | } |
1524 | } |
1525 | |
1526 | InfCodes.prototype.free = function(z){ |
1527 | // ZFREE(z, c); |
1528 | } |
1529 | |
1530 | // Called with number of bytes left to write in window at least 258 |
1531 | // (the maximum string length) and number of input bytes available |
1532 | // at least ten. The ten bytes are six bytes for the longest length/ |
1533 | // distance pair plus four bytes for overloading the bit buffer. |
1534 | |
1535 | InfCodes.prototype.inflate_fast = function(bl, bd, tl, tl_index, td, td_index, s, z) { |
1536 | var t; // temporary pointer |
1537 | var tp; // temporary pointer (int[]) |
1538 | var tp_index; // temporary pointer |
1539 | var e; // extra bits or operation |
1540 | var b; // bit buffer |
1541 | var k; // bits in bit buffer |
1542 | var p; // input data pointer |
1543 | var n; // bytes available there |
1544 | var q; // output window write pointer |
1545 | var m; // bytes to end of window or read pointer |
1546 | var ml; // mask for literal/length tree |
1547 | var md; // mask for distance tree |
1548 | var c; // bytes to copy |
1549 | var d; // distance back to copy from |
1550 | var r; // copy source pointer |
1551 | |
1552 | var tp_index_t_3; // (tp_index+t)*3 |
1553 | |
1554 | // load input, output, bit values |
1555 | p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; |
1556 | q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
1557 | |
1558 | // initialize masks |
1559 | ml = inflate_mask[bl]; |
1560 | md = inflate_mask[bd]; |
1561 | |
1562 | // do until not enough input or output space for fast loop |
1563 | do { // assume called with m >= 258 && n >= 10 |
1564 | // get literal/length code |
1565 | while(k<(20)){ // max bits for literal/length code |
1566 | n--; |
1567 | b|=(z.next_in[p++]&0xff)<<k;k+=8; |
1568 | } |
1569 | |
1570 | t= b&ml; |
1571 | tp=tl; |
1572 | tp_index=tl_index; |
1573 | tp_index_t_3=(tp_index+t)*3; |
1574 | if ((e = tp[tp_index_t_3]) == 0){ |
1575 | b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
1576 | |
1577 | s.window[q++] = tp[tp_index_t_3+2]; |
1578 | m--; |
1579 | continue; |
1580 | } |
1581 | do { |
1582 | |
1583 | b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
1584 | |
1585 | if((e&16)!=0){ |
1586 | e &= 15; |
1587 | c = tp[tp_index_t_3+2] + (b & inflate_mask[e]); |
1588 | |
1589 | b>>=e; k-=e; |
1590 | |
1591 | // decode distance base of block to copy |
1592 | while(k<(15)){ // max bits for distance code |
1593 | n--; |
1594 | b|=(z.next_in[p++]&0xff)<<k;k+=8; |
1595 | } |
1596 | |
1597 | t= b&md; |
1598 | tp=td; |
1599 | tp_index=td_index; |
1600 | tp_index_t_3=(tp_index+t)*3; |
1601 | e = tp[tp_index_t_3]; |
1602 | |
1603 | do { |
1604 | |
1605 | b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
1606 | |
1607 | if((e&16)!=0){ |
1608 | // get extra bits to add to distance base |
1609 | e &= 15; |
1610 | while(k<(e)){ // get extra bits (up to 13) |
1611 | n--; |
1612 | b|=(z.next_in[p++]&0xff)<<k;k+=8; |
1613 | } |
1614 | |
1615 | d = tp[tp_index_t_3+2] + (b&inflate_mask[e]); |
1616 | |
1617 | b>>=(e); k-=(e); |
1618 | |
1619 | // do the copy |
1620 | m -= c; |
1621 | if (q >= d){ // offset before dest |
1622 | // just copy |
1623 | r=q-d; |
1624 | if(q-r>0 && 2>(q-r)){ |
1625 | s.window[q++]=s.window[r++]; // minimum count is three, |
1626 | s.window[q++]=s.window[r++]; // so unroll loop a little |
1627 | c-=2; |
1628 | } |
1629 | else{ |
1630 | s.window[q++]=s.window[r++]; // minimum count is three, |
1631 | s.window[q++]=s.window[r++]; // so unroll loop a little |
1632 | c-=2; |
1633 | } |
1634 | } |
1635 | else{ // else offset after destination |
1636 | r=q-d; |
1637 | do{ |
1638 | r+=s.end; // force pointer in window |
1639 | }while(r<0); // covers invalid distances |
1640 | e=s.end-r; |
1641 | if(c>e){ // if source crosses, |
1642 | c-=e; // wrapped copy |
1643 | if(q-r>0 && e>(q-r)){ |
1644 | do{s.window[q++] = s.window[r++];} |
1645 | while(--e!=0); |
1646 | } |
1647 | else{ |
1648 | arrayCopy(s.window, r, s.window, q, e); |
1649 | q+=e; r+=e; e=0; |
1650 | } |
1651 | r = 0; // copy rest from start of window |
1652 | } |
1653 | |
1654 | } |
1655 | |
1656 | // copy all or what's left |
1657 | do{s.window[q++] = s.window[r++];} |
1658 | while(--c!=0); |
1659 | break; |
1660 | } |
1661 | else if((e&64)==0){ |
1662 | t+=tp[tp_index_t_3+2]; |
1663 | t+=(b&inflate_mask[e]); |
1664 | tp_index_t_3=(tp_index+t)*3; |
1665 | e=tp[tp_index_t_3]; |
1666 | } |
1667 | else{ |
1668 | z.msg = "invalid distance code"; |
1669 | |
1670 | c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
1671 | |
1672 | s.bitb=b;s.bitk=k; |
1673 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1674 | s.write=q; |
1675 | |
1676 | return Z_DATA_ERROR; |
1677 | } |
1678 | } |
1679 | while(true); |
1680 | break; |
1681 | } |
1682 | |
1683 | if((e&64)==0){ |
1684 | t+=tp[tp_index_t_3+2]; |
1685 | t+=(b&inflate_mask[e]); |
1686 | tp_index_t_3=(tp_index+t)*3; |
1687 | if((e=tp[tp_index_t_3])==0){ |
1688 | |
1689 | b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
1690 | |
1691 | s.window[q++]=tp[tp_index_t_3+2]; |
1692 | m--; |
1693 | break; |
1694 | } |
1695 | } |
1696 | else if((e&32)!=0){ |
1697 | |
1698 | c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
1699 | |
1700 | s.bitb=b;s.bitk=k; |
1701 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1702 | s.write=q; |
1703 | |
1704 | return Z_STREAM_END; |
1705 | } |
1706 | else{ |
1707 | z.msg="invalid literal/length code"; |
1708 | |
1709 | c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
1710 | |
1711 | s.bitb=b;s.bitk=k; |
1712 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1713 | s.write=q; |
1714 | |
1715 | return Z_DATA_ERROR; |
1716 | } |
1717 | } |
1718 | while(true); |
1719 | } |
1720 | while(m>=258 && n>= 10); |
1721 | |
1722 | // not enough input or output--restore pointers and return |
1723 | c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
1724 | |
1725 | s.bitb=b;s.bitk=k; |
1726 | z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
1727 | s.write=q; |
1728 | |
1729 | return Z_OK; |
1730 | } |
1731 | |
1732 | // |
1733 | // InfTree.java |
1734 | // |
1735 | |
1736 | function InfTree() { |
1737 | } |
1738 | |
1739 | InfTree.prototype.huft_build = function(b, bindex, n, s, d, e, t, m, hp, hn, v) { |
1740 | |
1741 | // Given a list of code lengths and a maximum table size, make a set of |
1742 | // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR |
1743 | // if the given code set is incomplete (the tables are still built in this |
1744 | // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of |
1745 | // lengths), or Z_MEM_ERROR if not enough memory. |
1746 | |
1747 | var a; // counter for codes of length k |
1748 | var f; // i repeats in table every f entries |
1749 | var g; // maximum code length |
1750 | var h; // table level |
1751 | var i; // counter, current code |
1752 | var j; // counter |
1753 | var k; // number of bits in current code |
1754 | var l; // bits per table (returned in m) |
1755 | var mask; // (1 << w) - 1, to avoid cc -O bug on HP |
1756 | var p; // pointer into c[], b[], or v[] |
1757 | var q; // points to current table |
1758 | var w; // bits before this table == (l * h) |
1759 | var xp; // pointer into x |
1760 | var y; // number of dummy codes added |
1761 | var z; // number of entries in current table |
1762 | |
1763 | // Generate counts for each bit length |
1764 | |
1765 | p = 0; i = n; |
1766 | do { |
1767 | this.c[b[bindex+p]]++; p++; i--; // assume all entries <= BMAX |
1768 | }while(i!=0); |
1769 | |
1770 | if(this.c[0] == n){ // null input--all zero length codes |
1771 | t[0] = -1; |
1772 | m[0] = 0; |
1773 | return Z_OK; |
1774 | } |
1775 | |
1776 | // Find minimum and maximum length, bound *m by those |
1777 | l = m[0]; |
1778 | for (j = 1; j <= BMAX; j++) |
1779 | if(this.c[j]!=0) break; |
1780 | k = j; // minimum code length |
1781 | if(l < j){ |
1782 | l = j; |
1783 | } |
1784 | for (i = BMAX; i!=0; i--){ |
1785 | if(this.c[i]!=0) break; |
1786 | } |
1787 | g = i; // maximum code length |
1788 | if(l > i){ |
1789 | l = i; |
1790 | } |
1791 | m[0] = l; |
1792 | |
1793 | // Adjust last length count to fill out codes, if needed |
1794 | for (y = 1 << j; j < i; j++, y <<= 1){ |
1795 | if ((y -= this.c[j]) < 0){ |
1796 | return Z_DATA_ERROR; |
1797 | } |
1798 | } |
1799 | if ((y -= this.c[i]) < 0){ |
1800 | return Z_DATA_ERROR; |
1801 | } |
1802 | this.c[i] += y; |
1803 | |
1804 | // Generate starting offsets into the value table for each length |
1805 | this.x[1] = j = 0; |
1806 | p = 1; xp = 2; |
1807 | while (--i!=0) { // note that i == g from above |
1808 | this.x[xp] = (j += this.c[p]); |
1809 | xp++; |
1810 | p++; |
1811 | } |
1812 | |
1813 | // Make a table of values in order of bit lengths |
1814 | i = 0; p = 0; |
1815 | do { |
1816 | if ((j = b[bindex+p]) != 0){ |
1817 | this.v[this.x[j]++] = i; |
1818 | } |
1819 | p++; |
1820 | } |
1821 | while (++i < n); |
1822 | n = this.x[g]; // set n to length of v |
1823 | |
1824 | // Generate the Huffman codes and for each, make the table entries |
1825 | this.x[0] = i = 0; // first Huffman code is zero |
1826 | p = 0; // grab values in bit order |
1827 | h = -1; // no tables yet--level -1 |
1828 | w = -l; // bits decoded == (l * h) |
1829 | this.u[0] = 0; // just to keep compilers happy |
1830 | q = 0; // ditto |
1831 | z = 0; // ditto |
1832 | |
1833 | // go through the bit lengths (k already is bits in shortest code) |
1834 | for (; k <= g; k++){ |
1835 | a = this.c[k]; |
1836 | while (a--!=0){ |
1837 | // here i is the Huffman code of length k bits for value *p |
1838 | // make tables up to required level |
1839 | while (k > w + l){ |
1840 | h++; |
1841 | w += l; // previous table always l bits |
1842 | // compute minimum size table less than or equal to l bits |
1843 | z = g - w; |
1844 | z = (z > l) ? l : z; // table size upper limit |
1845 | if((f=1<<(j=k-w))>a+1){ // try a k-w bit table |
1846 | // too few codes for k-w bit table |
1847 | f -= a + 1; // deduct codes from patterns left |
1848 | xp = k; |
1849 | if(j < z){ |
1850 | while (++j < z){ // try smaller tables up to z bits |
1851 | if((f <<= 1) <= this.c[++xp]) |
1852 | break; // enough codes to use up j bits |
1853 | f -= this.c[xp]; // else deduct codes from patterns |
1854 | } |
1855 | } |
1856 | } |
1857 | z = 1 << j; // table entries for j-bit table |
1858 | |
1859 | // allocate new table |
1860 | if (this.hn[0] + z > MANY){ // (note: doesn't matter for fixed) |
1861 | return Z_DATA_ERROR; // overflow of MANY |
1862 | } |
1863 | this.u[h] = q = /*hp+*/ this.hn[0]; // DEBUG |
1864 | this.hn[0] += z; |
1865 | |
1866 | // connect to last table, if there is one |
1867 | if(h!=0){ |
1868 | this.x[h]=i; // save pattern for backing up |
1869 | this.r[0]=j; // bits in this table |
1870 | this.r[1]=l; // bits to dump before this table |
1871 | j=i>>>(w - l); |
1872 | this.r[2] = (q - this.u[h-1] - j); // offset to this table |
1873 | arrayCopy(this.r, 0, hp, (this.u[h-1]+j)*3, 3); // connect to last table |
1874 | } |
1875 | else{ |
1876 | t[0] = q; // first table is returned result |
1877 | } |
1878 | } |
1879 | |
1880 | // set up table entry in r |
1881 | this.r[1] = (k - w); |
1882 | if (p >= n){ |
1883 | this.r[0] = 128 + 64; // out of values--invalid code |
1884 | } |
1885 | else if (v[p] < s){ |
1886 | this.r[0] = (this.v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block |
1887 | this.r[2] = this.v[p++]; // simple code is just the value |
1888 | } |
1889 | else{ |
1890 | this.r[0]=(e[this.v[p]-s]+16+64); // non-simple--look up in lists |
1891 | this.r[2]=d[this.v[p++] - s]; |
1892 | } |
1893 | |
1894 | // fill code-like entries with r |
1895 | f=1<<(k-w); |
1896 | for (j=i>>>w;j<z;j+=f){ |
1897 | arrayCopy(this.r, 0, hp, (q+j)*3, 3); |
1898 | } |
1899 | |
1900 | // backwards increment the k-bit code i |
1901 | for (j = 1 << (k - 1); (i & j)!=0; j >>>= 1){ |
1902 | i ^= j; |
1903 | } |
1904 | i ^= j; |
1905 | |
1906 | // backup over finished tables |
1907 | mask = (1 << w) - 1; // needed on HP, cc -O bug |
1908 | while ((i & mask) != this.x[h]){ |
1909 | h--; // don't need to update q |
1910 | w -= l; |
1911 | mask = (1 << w) - 1; |
1912 | } |
1913 | } |
1914 | } |
1915 | // Return Z_BUF_ERROR if we were given an incomplete table |
1916 | return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; |
1917 | } |
1918 | |
1919 | InfTree.prototype.inflate_trees_bits = function(c, bb, tb, hp, z) { |
1920 | var result; |
1921 | this.initWorkArea(19); |
1922 | this.hn[0]=0; |
1923 | result = this.huft_build(c, 0, 19, 19, null, null, tb, bb, hp, this.hn, this.v); |
1924 | |
1925 | if(result == Z_DATA_ERROR){ |
1926 | z.msg = "oversubscribed dynamic bit lengths tree"; |
1927 | } |
1928 | else if(result == Z_BUF_ERROR || bb[0] == 0){ |
1929 | z.msg = "incomplete dynamic bit lengths tree"; |
1930 | result = Z_DATA_ERROR; |
1931 | } |
1932 | return result; |
1933 | } |
1934 | |
1935 | InfTree.prototype.inflate_trees_dynamic = function(nl, nd, c, bl, bd, tl, td, hp, z) { |
1936 | var result; |
1937 | |
1938 | // build literal/length tree |
1939 | this.initWorkArea(288); |
1940 | this.hn[0]=0; |
1941 | result = this.huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, this.hn, this.v); |
1942 | if (result != Z_OK || bl[0] == 0){ |
1943 | if(result == Z_DATA_ERROR){ |
1944 | z.msg = "oversubscribed literal/length tree"; |
1945 | } |
1946 | else if (result != Z_MEM_ERROR){ |
1947 | z.msg = "incomplete literal/length tree"; |
1948 | result = Z_DATA_ERROR; |
1949 | } |
1950 | return result; |
1951 | } |
1952 | |
1953 | // build distance tree |
1954 | this.initWorkArea(288); |
1955 | result = this.huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, this.hn, this.v); |
1956 | |
1957 | if (result != Z_OK || (bd[0] == 0 && nl > 257)){ |
1958 | if (result == Z_DATA_ERROR){ |
1959 | z.msg = "oversubscribed distance tree"; |
1960 | } |
1961 | else if (result == Z_BUF_ERROR) { |
1962 | z.msg = "incomplete distance tree"; |
1963 | result = Z_DATA_ERROR; |
1964 | } |
1965 | else if (result != Z_MEM_ERROR){ |
1966 | z.msg = "empty distance tree with lengths"; |
1967 | result = Z_DATA_ERROR; |
1968 | } |
1969 | return result; |
1970 | } |
1971 | |
1972 | return Z_OK; |
1973 | } |
1974 | /* |
1975 | static int inflate_trees_fixed(int[] bl, //literal desired/actual bit depth |
1976 | int[] bd, //distance desired/actual bit depth |
1977 | int[][] tl,//literal/length tree result |
1978 | int[][] td,//distance tree result |
1979 | ZStream z //for memory allocation |
1980 | ){ |
1981 | |
1982 | */ |
1983 | |
1984 | function inflate_trees_fixed(bl, bd, tl, td, z) { |
1985 | bl[0]=fixed_bl; |
1986 | bd[0]=fixed_bd; |
1987 | tl[0]=fixed_tl; |
1988 | td[0]=fixed_td; |
1989 | return Z_OK; |
1990 | } |
1991 | |
1992 | InfTree.prototype.initWorkArea = function(vsize){ |
1993 | if(this.hn==null){ |
1994 | this.hn=new Int32Array(1); |
1995 | this.v=new Int32Array(vsize); |
1996 | this.c=new Int32Array(BMAX+1); |
1997 | this.r=new Int32Array(3); |
1998 | this.u=new Int32Array(BMAX); |
1999 | this.x=new Int32Array(BMAX+1); |
2000 | } |
2001 | if(this.v.length<vsize){ |
2002 | this.v=new Int32Array(vsize); |
2003 | } |
2004 | for(var i=0; i<vsize; i++){this.v[i]=0;} |
2005 | for(var i=0; i<BMAX+1; i++){this.c[i]=0;} |
2006 | for(var i=0; i<3; i++){this.r[i]=0;} |
2007 | // for(int i=0; i<BMAX; i++){u[i]=0;} |
2008 | arrayCopy(this.c, 0, this.u, 0, BMAX); |
2009 | // for(int i=0; i<BMAX+1; i++){x[i]=0;} |
2010 | arrayCopy(this.c, 0, this.x, 0, BMAX+1); |
2011 | } |
2012 | |
2013 | var testArray = new Uint8Array(1); |
2014 | var hasSubarray = (typeof testArray.subarray === 'function'); |
2015 | var hasSlice = false; /* (typeof testArray.slice === 'function'); */ // Chrome slice performance is so dire that we're currently not using it... |
2016 | |
2017 | function arrayCopy(src, srcOffset, dest, destOffset, count) { |
2018 | if (count == 0) { |
2019 | return; |
2020 | } |
2021 | if (!src) { |
2022 | throw "Undef src"; |
2023 | } else if (!dest) { |
2024 | throw "Undef dest"; |
2025 | } |
2026 | |
2027 | if (srcOffset == 0 && count == src.length) { |
2028 | arrayCopy_fast(src, dest, destOffset); |
2029 | } else if (hasSubarray) { |
2030 | arrayCopy_fast(src.subarray(srcOffset, srcOffset + count), dest, destOffset); |
2031 | } else if (src.BYTES_PER_ELEMENT == 1 && count > 100) { |
2032 | arrayCopy_fast(new Uint8Array(src.buffer, src.byteOffset + srcOffset, count), dest, destOffset); |
2033 | } else { |
2034 | arrayCopy_slow(src, srcOffset, dest, destOffset, count); |
2035 | } |
2036 | |
2037 | } |
2038 | |
2039 | function arrayCopy_slow(src, srcOffset, dest, destOffset, count) { |
2040 | |
2041 | // dlog('_slow call: srcOffset=' + srcOffset + '; destOffset=' + destOffset + '; count=' + count); |
2042 | |
2043 | for (var i = 0; i < count; ++i) { |
2044 | dest[destOffset + i] = src[srcOffset + i]; |
2045 | } |
2046 | } |
2047 | |
2048 | function arrayCopy_fast(src, dest, destOffset) { |
2049 | dest.set(src, destOffset); |
2050 | } |
2051 | |
2052 | |
2053 | // largest prime smaller than 65536 |
2054 | var ADLER_BASE=65521; |
2055 | // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 |
2056 | var ADLER_NMAX=5552; |
2057 | |
2058 | function adler32(adler, /* byte[] */ buf, index, len){ |
2059 | if(buf == null){ return 1; } |
2060 | |
2061 | var s1=adler&0xffff; |
2062 | var s2=(adler>>16)&0xffff; |
2063 | var k; |
2064 | |
2065 | while(len > 0) { |
2066 | k=len<ADLER_NMAX?len:ADLER_NMAX; |
2067 | len-=k; |
2068 | while(k>=16){ |
2069 | s1+=buf[index++]&0xff; s2+=s1; |
2070 | s1+=buf[index++]&0xff; s2+=s1; |
2071 | s1+=buf[index++]&0xff; s2+=s1; |
2072 | s1+=buf[index++]&0xff; s2+=s1; |
2073 | s1+=buf[index++]&0xff; s2+=s1; |
2074 | s1+=buf[index++]&0xff; s2+=s1; |
2075 | s1+=buf[index++]&0xff; s2+=s1; |
2076 | s1+=buf[index++]&0xff; s2+=s1; |
2077 | s1+=buf[index++]&0xff; s2+=s1; |
2078 | s1+=buf[index++]&0xff; s2+=s1; |
2079 | s1+=buf[index++]&0xff; s2+=s1; |
2080 | s1+=buf[index++]&0xff; s2+=s1; |
2081 | s1+=buf[index++]&0xff; s2+=s1; |
2082 | s1+=buf[index++]&0xff; s2+=s1; |
2083 | s1+=buf[index++]&0xff; s2+=s1; |
2084 | s1+=buf[index++]&0xff; s2+=s1; |
2085 | k-=16; |
2086 | } |
2087 | if(k!=0){ |
2088 | do{ |
2089 | s1+=buf[index++]&0xff; s2+=s1; |
2090 | } |
2091 | while(--k!=0); |
2092 | } |
2093 | s1%=ADLER_BASE; |
2094 | s2%=ADLER_BASE; |
2095 | } |
2096 | return (s2<<16)|s1; |
2097 | } |
2098 | |
2099 | |
2100 | |
2101 | function jszlib_inflate_buffer(buffer, start, length, afterUncOffset) { |
2102 | if (!start) { |
2103 | buffer = new Uint8Array(buffer); |
2104 | } else if (!length) { |
2105 | buffer = new Uint8Array(buffer, start, buffer.byteLength - start); |
2106 | } else { |
2107 | buffer = new Uint8Array(buffer, start, length); |
2108 | } |
2109 | |
2110 | var z = new ZStream(); |
2111 | z.inflateInit(DEF_WBITS, true); |
2112 | z.next_in = buffer; |
2113 | z.next_in_index = 0; |
2114 | z.avail_in = buffer.length; |
2115 | |
2116 | var oBlockList = []; |
2117 | var totalSize = 0; |
2118 | while (true) { |
2119 | var obuf = new Uint8Array(32000); |
2120 | z.next_out = obuf; |
2121 | z.next_out_index = 0; |
2122 | z.avail_out = obuf.length; |
2123 | var status = z.inflate(Z_NO_FLUSH); |
2124 | if (status != Z_OK && status != Z_STREAM_END && status != Z_BUF_ERROR) { |
2125 | throw z.msg; |
2126 | } |
2127 | if (z.avail_out != 0) { |
2128 | var newob = new Uint8Array(obuf.length - z.avail_out); |
2129 | arrayCopy(obuf, 0, newob, 0, (obuf.length - z.avail_out)); |
2130 | obuf = newob; |
2131 | } |
2132 | oBlockList.push(obuf); |
2133 | totalSize += obuf.length; |
2134 | if (status == Z_STREAM_END || status == Z_BUF_ERROR) { |
2135 | break; |
2136 | } |
2137 | } |
2138 | |
2139 | if (afterUncOffset) { |
2140 | afterUncOffset[0] = (start || 0) + z.next_in_index; |
2141 | } |
2142 | |
2143 | if (oBlockList.length == 1) { |
2144 | return oBlockList[0].buffer; |
2145 | } else { |
2146 | var out = new Uint8Array(totalSize); |
2147 | var cursor = 0; |
2148 | for (var i = 0; i < oBlockList.length; ++i) { |
2149 | var b = oBlockList[i]; |
2150 | arrayCopy(b, 0, out, cursor, b.length); |
2151 | cursor += b.length; |
2152 | } |
2153 | return out.buffer; |
2154 | } |
2155 | } |
2156 | |
2157 | if (typeof(module) !== 'undefined') { |
2158 | module.exports = { |
2159 | inflateBuffer: jszlib_inflate_buffer, |
2160 | arrayCopy: arrayCopy |
2161 | }; |
2162 | } |
Snippet is not live.
Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #2000601 |
Snippet name: | inflate.js |
Eternal ID of this version: | #2000601/1 |
Text MD5: | c7db770a5f78deac5ac8a47581b00990 |
Author: | stefan |
Category: | javascript |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-27 07:09:43 |
Source code size: | 62118 bytes / 2162 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 136 / 61 |
Referenced in: | [show references] |