0
|
1 /*
|
|
2 * Port of a script by Masanao Izumo.
|
|
3 *
|
|
4 * Only changes : wrap all the variables in a function and add the
|
|
5 * main function to JSZip (DEFLATE compression method).
|
|
6 * Everything else was written by M. Izumo.
|
|
7 *
|
|
8 * Original code can be found here: http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
|
|
9 */
|
|
10
|
|
11 if(!JSZip) {
|
|
12 throw "JSZip not defined";
|
|
13 }
|
|
14
|
|
15 /*
|
|
16 * Original:
|
|
17 * http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
|
|
18 */
|
|
19
|
|
20 (function(){
|
|
21 // the original implementation leaks a global variable.
|
|
22 // Defining the variable here doesn't break anything.
|
|
23 var zip_fixed_bd;
|
|
24
|
|
25 /* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
|
|
26 * Version: 1.0.0.1
|
|
27 * LastModified: Dec 25 1999
|
|
28 */
|
|
29
|
|
30 /* Interface:
|
|
31 * data = zip_inflate(src);
|
|
32 */
|
|
33
|
|
34 /* constant parameters */
|
|
35 var zip_WSIZE = 32768; // Sliding Window size
|
|
36 var zip_STORED_BLOCK = 0;
|
|
37 var zip_STATIC_TREES = 1;
|
|
38 var zip_DYN_TREES = 2;
|
|
39
|
|
40 /* for inflate */
|
|
41 var zip_lbits = 9; // bits in base literal/length lookup table
|
|
42 var zip_dbits = 6; // bits in base distance lookup table
|
|
43 var zip_INBUFSIZ = 32768; // Input buffer size
|
|
44 var zip_INBUF_EXTRA = 64; // Extra buffer
|
|
45
|
|
46 /* variables (inflate) */
|
|
47 var zip_slide;
|
|
48 var zip_wp; // current position in slide
|
|
49 var zip_fixed_tl = null; // inflate static
|
|
50 var zip_fixed_td; // inflate static
|
|
51 var zip_fixed_bl, fixed_bd; // inflate static
|
|
52 var zip_bit_buf; // bit buffer
|
|
53 var zip_bit_len; // bits in bit buffer
|
|
54 var zip_method;
|
|
55 var zip_eof;
|
|
56 var zip_copy_leng;
|
|
57 var zip_copy_dist;
|
|
58 var zip_tl, zip_td; // literal/length and distance decoder tables
|
|
59 var zip_bl, zip_bd; // number of bits decoded by tl and td
|
|
60
|
|
61 var zip_inflate_data;
|
|
62 var zip_inflate_pos;
|
|
63
|
|
64
|
|
65 /* constant tables (inflate) */
|
|
66 var zip_MASK_BITS = new Array(
|
|
67 0x0000,
|
|
68 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
|
|
69 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff);
|
|
70 // Tables for deflate from PKZIP's appnote.txt.
|
|
71 var zip_cplens = new Array( // Copy lengths for literal codes 257..285
|
|
72 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
|
73 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0);
|
|
74 /* note: see note #13 above about the 258 in this list. */
|
|
75 var zip_cplext = new Array( // Extra bits for literal codes 257..285
|
|
76 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
|
77 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99); // 99==invalid
|
|
78 var zip_cpdist = new Array( // Copy offsets for distance codes 0..29
|
|
79 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
|
80 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
|
81 8193, 12289, 16385, 24577);
|
|
82 var zip_cpdext = new Array( // Extra bits for distance codes
|
|
83 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
|
84 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
|
85 12, 12, 13, 13);
|
|
86 var zip_border = new Array( // Order of the bit length code lengths
|
|
87 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
|
|
88 /* objects (inflate) */
|
|
89
|
|
90 function zip_HuftList() {
|
|
91 this.next = null;
|
|
92 this.list = null;
|
|
93 }
|
|
94
|
|
95 function zip_HuftNode() {
|
|
96 this.e = 0; // number of extra bits or operation
|
|
97 this.b = 0; // number of bits in this code or subcode
|
|
98
|
|
99 // union
|
|
100 this.n = 0; // literal, length base, or distance base
|
|
101 this.t = null; // (zip_HuftNode) pointer to next level of table
|
|
102 }
|
|
103
|
|
104 function zip_HuftBuild(b, // code lengths in bits (all assumed <= BMAX)
|
|
105 n, // number of codes (assumed <= N_MAX)
|
|
106 s, // number of simple-valued codes (0..s-1)
|
|
107 d, // list of base values for non-simple codes
|
|
108 e, // list of extra bits for non-simple codes
|
|
109 mm // maximum lookup bits
|
|
110 ) {
|
|
111 this.BMAX = 16; // maximum bit length of any code
|
|
112 this.N_MAX = 288; // maximum number of codes in any set
|
|
113 this.status = 0; // 0: success, 1: incomplete table, 2: bad input
|
|
114 this.root = null; // (zip_HuftList) starting table
|
|
115 this.m = 0; // maximum lookup bits, returns actual
|
|
116
|
|
117 /* Given a list of code lengths and a maximum table size, make a set of
|
|
118 tables to decode that set of codes. Return zero on success, one if
|
|
119 the given code set is incomplete (the tables are still built in this
|
|
120 case), two if the input is invalid (all zero length codes or an
|
|
121 oversubscribed set of lengths), and three if not enough memory.
|
|
122 The code with value 256 is special, and the tables are constructed
|
|
123 so that no bits beyond that code are fetched when that code is
|
|
124 decoded. */
|
|
125 {
|
|
126 var a; // counter for codes of length k
|
|
127 var c = new Array(this.BMAX+1); // bit length count table
|
|
128 var el; // length of EOB code (value 256)
|
|
129 var f; // i repeats in table every f entries
|
|
130 var g; // maximum code length
|
|
131 var h; // table level
|
|
132 var i; // counter, current code
|
|
133 var j; // counter
|
|
134 var k; // number of bits in current code
|
|
135 var lx = new Array(this.BMAX+1); // stack of bits per table
|
|
136 var p; // pointer into c[], b[], or v[]
|
|
137 var pidx; // index of p
|
|
138 var q; // (zip_HuftNode) points to current table
|
|
139 var r = new zip_HuftNode(); // table entry for structure assignment
|
|
140 var u = new Array(this.BMAX); // zip_HuftNode[BMAX][] table stack
|
|
141 var v = new Array(this.N_MAX); // values in order of bit length
|
|
142 var w;
|
|
143 var x = new Array(this.BMAX+1);// bit offsets, then code stack
|
|
144 var xp; // pointer into x or c
|
|
145 var y; // number of dummy codes added
|
|
146 var z; // number of entries in current table
|
|
147 var o;
|
|
148 var tail; // (zip_HuftList)
|
|
149
|
|
150 tail = this.root = null;
|
|
151 for(i = 0; i < c.length; i++)
|
|
152 c[i] = 0;
|
|
153 for(i = 0; i < lx.length; i++)
|
|
154 lx[i] = 0;
|
|
155 for(i = 0; i < u.length; i++)
|
|
156 u[i] = null;
|
|
157 for(i = 0; i < v.length; i++)
|
|
158 v[i] = 0;
|
|
159 for(i = 0; i < x.length; i++)
|
|
160 x[i] = 0;
|
|
161
|
|
162 // Generate counts for each bit length
|
|
163 el = n > 256 ? b[256] : this.BMAX; // set length of EOB code, if any
|
|
164 p = b; pidx = 0;
|
|
165 i = n;
|
|
166 do {
|
|
167 c[p[pidx]]++; // assume all entries <= BMAX
|
|
168 pidx++;
|
|
169 } while(--i > 0);
|
|
170 if(c[0] == n) { // null input--all zero length codes
|
|
171 this.root = null;
|
|
172 this.m = 0;
|
|
173 this.status = 0;
|
|
174 return;
|
|
175 }
|
|
176
|
|
177 // Find minimum and maximum length, bound *m by those
|
|
178 for(j = 1; j <= this.BMAX; j++)
|
|
179 if(c[j] != 0)
|
|
180 break;
|
|
181 k = j; // minimum code length
|
|
182 if(mm < j)
|
|
183 mm = j;
|
|
184 for(i = this.BMAX; i != 0; i--)
|
|
185 if(c[i] != 0)
|
|
186 break;
|
|
187 g = i; // maximum code length
|
|
188 if(mm > i)
|
|
189 mm = i;
|
|
190
|
|
191 // Adjust last length count to fill out codes, if needed
|
|
192 for(y = 1 << j; j < i; j++, y <<= 1)
|
|
193 if((y -= c[j]) < 0) {
|
|
194 this.status = 2; // bad input: more codes than bits
|
|
195 this.m = mm;
|
|
196 return;
|
|
197 }
|
|
198 if((y -= c[i]) < 0) {
|
|
199 this.status = 2;
|
|
200 this.m = mm;
|
|
201 return;
|
|
202 }
|
|
203 c[i] += y;
|
|
204
|
|
205 // Generate starting offsets into the value table for each length
|
|
206 x[1] = j = 0;
|
|
207 p = c;
|
|
208 pidx = 1;
|
|
209 xp = 2;
|
|
210 while(--i > 0) // note that i == g from above
|
|
211 x[xp++] = (j += p[pidx++]);
|
|
212
|
|
213 // Make a table of values in order of bit lengths
|
|
214 p = b; pidx = 0;
|
|
215 i = 0;
|
|
216 do {
|
|
217 if((j = p[pidx++]) != 0)
|
|
218 v[x[j]++] = i;
|
|
219 } while(++i < n);
|
|
220 n = x[g]; // set n to length of v
|
|
221
|
|
222 // Generate the Huffman codes and for each, make the table entries
|
|
223 x[0] = i = 0; // first Huffman code is zero
|
|
224 p = v; pidx = 0; // grab values in bit order
|
|
225 h = -1; // no tables yet--level -1
|
|
226 w = lx[0] = 0; // no bits decoded yet
|
|
227 q = null; // ditto
|
|
228 z = 0; // ditto
|
|
229
|
|
230 // go through the bit lengths (k already is bits in shortest code)
|
|
231 for(; k <= g; k++) {
|
|
232 a = c[k];
|
|
233 while(a-- > 0) {
|
|
234 // here i is the Huffman code of length k bits for value p[pidx]
|
|
235 // make tables up to required level
|
|
236 while(k > w + lx[1 + h]) {
|
|
237 w += lx[1 + h]; // add bits already decoded
|
|
238 h++;
|
|
239
|
|
240 // compute minimum size table less than or equal to *m bits
|
|
241 z = (z = g - w) > mm ? mm : z; // upper limit
|
|
242 if((f = 1 << (j = k - w)) > a + 1) { // try a k-w bit table
|
|
243 // too few codes for k-w bit table
|
|
244 f -= a + 1; // deduct codes from patterns left
|
|
245 xp = k;
|
|
246 while(++j < z) { // try smaller tables up to z bits
|
|
247 if((f <<= 1) <= c[++xp])
|
|
248 break; // enough codes to use up j bits
|
|
249 f -= c[xp]; // else deduct codes from patterns
|
|
250 }
|
|
251 }
|
|
252 if(w + j > el && w < el)
|
|
253 j = el - w; // make EOB code end at table
|
|
254 z = 1 << j; // table entries for j-bit table
|
|
255 lx[1 + h] = j; // set table size in stack
|
|
256
|
|
257 // allocate and link in new table
|
|
258 q = new Array(z);
|
|
259 for(o = 0; o < z; o++) {
|
|
260 q[o] = new zip_HuftNode();
|
|
261 }
|
|
262
|
|
263 if(tail == null)
|
|
264 tail = this.root = new zip_HuftList();
|
|
265 else
|
|
266 tail = tail.next = new zip_HuftList();
|
|
267 tail.next = null;
|
|
268 tail.list = q;
|
|
269 u[h] = q; // table starts after link
|
|
270
|
|
271 /* connect to last table, if there is one */
|
|
272 if(h > 0) {
|
|
273 x[h] = i; // save pattern for backing up
|
|
274 r.b = lx[h]; // bits to dump before this table
|
|
275 r.e = 16 + j; // bits in this table
|
|
276 r.t = q; // pointer to this table
|
|
277 j = (i & ((1 << w) - 1)) >> (w - lx[h]);
|
|
278 u[h-1][j].e = r.e;
|
|
279 u[h-1][j].b = r.b;
|
|
280 u[h-1][j].n = r.n;
|
|
281 u[h-1][j].t = r.t;
|
|
282 }
|
|
283 }
|
|
284
|
|
285 // set up table entry in r
|
|
286 r.b = k - w;
|
|
287 if(pidx >= n)
|
|
288 r.e = 99; // out of values--invalid code
|
|
289 else if(p[pidx] < s) {
|
|
290 r.e = (p[pidx] < 256 ? 16 : 15); // 256 is end-of-block code
|
|
291 r.n = p[pidx++]; // simple code is just the value
|
|
292 } else {
|
|
293 r.e = e[p[pidx] - s]; // non-simple--look up in lists
|
|
294 r.n = d[p[pidx++] - s];
|
|
295 }
|
|
296
|
|
297 // fill code-like entries with r //
|
|
298 f = 1 << (k - w);
|
|
299 for(j = i >> w; j < z; j += f) {
|
|
300 q[j].e = r.e;
|
|
301 q[j].b = r.b;
|
|
302 q[j].n = r.n;
|
|
303 q[j].t = r.t;
|
|
304 }
|
|
305
|
|
306 // backwards increment the k-bit code i
|
|
307 for(j = 1 << (k - 1); (i & j) != 0; j >>= 1)
|
|
308 i ^= j;
|
|
309 i ^= j;
|
|
310
|
|
311 // backup over finished tables
|
|
312 while((i & ((1 << w) - 1)) != x[h]) {
|
|
313 w -= lx[h]; // don't need to update q
|
|
314 h--;
|
|
315 }
|
|
316 }
|
|
317 }
|
|
318
|
|
319 /* return actual size of base table */
|
|
320 this.m = lx[1];
|
|
321
|
|
322 /* Return true (1) if we were given an incomplete table */
|
|
323 this.status = ((y != 0 && g != 1) ? 1 : 0);
|
|
324 } /* end of constructor */
|
|
325 }
|
|
326
|
|
327
|
|
328 /* routines (inflate) */
|
|
329
|
|
330 function zip_GET_BYTE() {
|
|
331 if(zip_inflate_data.length == zip_inflate_pos)
|
|
332 return -1;
|
|
333 return zip_inflate_data.charCodeAt(zip_inflate_pos++) & 0xff;
|
|
334 }
|
|
335
|
|
336 function zip_NEEDBITS(n) {
|
|
337 while(zip_bit_len < n) {
|
|
338 zip_bit_buf |= zip_GET_BYTE() << zip_bit_len;
|
|
339 zip_bit_len += 8;
|
|
340 }
|
|
341 }
|
|
342
|
|
343 function zip_GETBITS(n) {
|
|
344 return zip_bit_buf & zip_MASK_BITS[n];
|
|
345 }
|
|
346
|
|
347 function zip_DUMPBITS(n) {
|
|
348 zip_bit_buf >>= n;
|
|
349 zip_bit_len -= n;
|
|
350 }
|
|
351
|
|
352 function zip_inflate_codes(buff, off, size) {
|
|
353 /* inflate (decompress) the codes in a deflated (compressed) block.
|
|
354 Return an error code or zero if it all goes ok. */
|
|
355 var e; // table entry flag/number of extra bits
|
|
356 var t; // (zip_HuftNode) pointer to table entry
|
|
357 var n;
|
|
358
|
|
359 if(size == 0)
|
|
360 return 0;
|
|
361
|
|
362 // inflate the coded data
|
|
363 n = 0;
|
|
364 for(;;) { // do until end of block
|
|
365 zip_NEEDBITS(zip_bl);
|
|
366 t = zip_tl.list[zip_GETBITS(zip_bl)];
|
|
367 e = t.e;
|
|
368 while(e > 16) {
|
|
369 if(e == 99)
|
|
370 return -1;
|
|
371 zip_DUMPBITS(t.b);
|
|
372 e -= 16;
|
|
373 zip_NEEDBITS(e);
|
|
374 t = t.t[zip_GETBITS(e)];
|
|
375 e = t.e;
|
|
376 }
|
|
377 zip_DUMPBITS(t.b);
|
|
378
|
|
379 if(e == 16) { // then it's a literal
|
|
380 zip_wp &= zip_WSIZE - 1;
|
|
381 buff[off + n++] = zip_slide[zip_wp++] = t.n;
|
|
382 if(n == size)
|
|
383 return size;
|
|
384 continue;
|
|
385 }
|
|
386
|
|
387 // exit if end of block
|
|
388 if(e == 15)
|
|
389 break;
|
|
390
|
|
391 // it's an EOB or a length
|
|
392
|
|
393 // get length of block to copy
|
|
394 zip_NEEDBITS(e);
|
|
395 zip_copy_leng = t.n + zip_GETBITS(e);
|
|
396 zip_DUMPBITS(e);
|
|
397
|
|
398 // decode distance of block to copy
|
|
399 zip_NEEDBITS(zip_bd);
|
|
400 t = zip_td.list[zip_GETBITS(zip_bd)];
|
|
401 e = t.e;
|
|
402
|
|
403 while(e > 16) {
|
|
404 if(e == 99)
|
|
405 return -1;
|
|
406 zip_DUMPBITS(t.b);
|
|
407 e -= 16;
|
|
408 zip_NEEDBITS(e);
|
|
409 t = t.t[zip_GETBITS(e)];
|
|
410 e = t.e;
|
|
411 }
|
|
412 zip_DUMPBITS(t.b);
|
|
413 zip_NEEDBITS(e);
|
|
414 zip_copy_dist = zip_wp - t.n - zip_GETBITS(e);
|
|
415 zip_DUMPBITS(e);
|
|
416
|
|
417 // do the copy
|
|
418 while(zip_copy_leng > 0 && n < size) {
|
|
419 zip_copy_leng--;
|
|
420 zip_copy_dist &= zip_WSIZE - 1;
|
|
421 zip_wp &= zip_WSIZE - 1;
|
|
422 buff[off + n++] = zip_slide[zip_wp++]
|
|
423 = zip_slide[zip_copy_dist++];
|
|
424 }
|
|
425
|
|
426 if(n == size)
|
|
427 return size;
|
|
428 }
|
|
429
|
|
430 zip_method = -1; // done
|
|
431 return n;
|
|
432 }
|
|
433
|
|
434 function zip_inflate_stored(buff, off, size) {
|
|
435 /* "decompress" an inflated type 0 (stored) block. */
|
|
436 var n;
|
|
437
|
|
438 // go to byte boundary
|
|
439 n = zip_bit_len & 7;
|
|
440 zip_DUMPBITS(n);
|
|
441
|
|
442 // get the length and its complement
|
|
443 zip_NEEDBITS(16);
|
|
444 n = zip_GETBITS(16);
|
|
445 zip_DUMPBITS(16);
|
|
446 zip_NEEDBITS(16);
|
|
447 if(n != ((~zip_bit_buf) & 0xffff))
|
|
448 return -1; // error in compressed data
|
|
449 zip_DUMPBITS(16);
|
|
450
|
|
451 // read and output the compressed data
|
|
452 zip_copy_leng = n;
|
|
453
|
|
454 n = 0;
|
|
455 while(zip_copy_leng > 0 && n < size) {
|
|
456 zip_copy_leng--;
|
|
457 zip_wp &= zip_WSIZE - 1;
|
|
458 zip_NEEDBITS(8);
|
|
459 buff[off + n++] = zip_slide[zip_wp++] =
|
|
460 zip_GETBITS(8);
|
|
461 zip_DUMPBITS(8);
|
|
462 }
|
|
463
|
|
464 if(zip_copy_leng == 0)
|
|
465 zip_method = -1; // done
|
|
466 return n;
|
|
467 }
|
|
468
|
|
469 function zip_inflate_fixed(buff, off, size) {
|
|
470 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
|
|
471 either replace this with a custom decoder, or at least precompute the
|
|
472 Huffman tables. */
|
|
473
|
|
474 // if first time, set up tables for fixed blocks
|
|
475 if(zip_fixed_tl == null) {
|
|
476 var i; // temporary variable
|
|
477 var l = new Array(288); // length list for huft_build
|
|
478 var h; // zip_HuftBuild
|
|
479
|
|
480 // literal table
|
|
481 for(i = 0; i < 144; i++)
|
|
482 l[i] = 8;
|
|
483 for(; i < 256; i++)
|
|
484 l[i] = 9;
|
|
485 for(; i < 280; i++)
|
|
486 l[i] = 7;
|
|
487 for(; i < 288; i++) // make a complete, but wrong code set
|
|
488 l[i] = 8;
|
|
489 zip_fixed_bl = 7;
|
|
490
|
|
491 h = new zip_HuftBuild(l, 288, 257, zip_cplens, zip_cplext,
|
|
492 zip_fixed_bl);
|
|
493 if(h.status != 0) {
|
|
494 alert("HufBuild error: "+h.status);
|
|
495 return -1;
|
|
496 }
|
|
497 zip_fixed_tl = h.root;
|
|
498 zip_fixed_bl = h.m;
|
|
499
|
|
500 // distance table
|
|
501 for(i = 0; i < 30; i++) // make an incomplete code set
|
|
502 l[i] = 5;
|
|
503 zip_fixed_bd = 5;
|
|
504
|
|
505 h = new zip_HuftBuild(l, 30, 0, zip_cpdist, zip_cpdext, zip_fixed_bd);
|
|
506 if(h.status > 1) {
|
|
507 zip_fixed_tl = null;
|
|
508 alert("HufBuild error: "+h.status);
|
|
509 return -1;
|
|
510 }
|
|
511 zip_fixed_td = h.root;
|
|
512 zip_fixed_bd = h.m;
|
|
513 }
|
|
514
|
|
515 zip_tl = zip_fixed_tl;
|
|
516 zip_td = zip_fixed_td;
|
|
517 zip_bl = zip_fixed_bl;
|
|
518 zip_bd = zip_fixed_bd;
|
|
519 return zip_inflate_codes(buff, off, size);
|
|
520 }
|
|
521
|
|
522 function zip_inflate_dynamic(buff, off, size) {
|
|
523 // decompress an inflated type 2 (dynamic Huffman codes) block.
|
|
524 var i; // temporary variables
|
|
525 var j;
|
|
526 var l; // last length
|
|
527 var n; // number of lengths to get
|
|
528 var t; // (zip_HuftNode) literal/length code table
|
|
529 var nb; // number of bit length codes
|
|
530 var nl; // number of literal/length codes
|
|
531 var nd; // number of distance codes
|
|
532 var ll = new Array(286+30); // literal/length and distance code lengths
|
|
533 var h; // (zip_HuftBuild)
|
|
534
|
|
535 for(i = 0; i < ll.length; i++)
|
|
536 ll[i] = 0;
|
|
537
|
|
538 // read in table lengths
|
|
539 zip_NEEDBITS(5);
|
|
540 nl = 257 + zip_GETBITS(5); // number of literal/length codes
|
|
541 zip_DUMPBITS(5);
|
|
542 zip_NEEDBITS(5);
|
|
543 nd = 1 + zip_GETBITS(5); // number of distance codes
|
|
544 zip_DUMPBITS(5);
|
|
545 zip_NEEDBITS(4);
|
|
546 nb = 4 + zip_GETBITS(4); // number of bit length codes
|
|
547 zip_DUMPBITS(4);
|
|
548 if(nl > 286 || nd > 30)
|
|
549 return -1; // bad lengths
|
|
550
|
|
551 // read in bit-length-code lengths
|
|
552 for(j = 0; j < nb; j++)
|
|
553 {
|
|
554 zip_NEEDBITS(3);
|
|
555 ll[zip_border[j]] = zip_GETBITS(3);
|
|
556 zip_DUMPBITS(3);
|
|
557 }
|
|
558 for(; j < 19; j++)
|
|
559 ll[zip_border[j]] = 0;
|
|
560
|
|
561 // build decoding table for trees--single level, 7 bit lookup
|
|
562 zip_bl = 7;
|
|
563 h = new zip_HuftBuild(ll, 19, 19, null, null, zip_bl);
|
|
564 if(h.status != 0)
|
|
565 return -1; // incomplete code set
|
|
566
|
|
567 zip_tl = h.root;
|
|
568 zip_bl = h.m;
|
|
569
|
|
570 // read in literal and distance code lengths
|
|
571 n = nl + nd;
|
|
572 i = l = 0;
|
|
573 while(i < n) {
|
|
574 zip_NEEDBITS(zip_bl);
|
|
575 t = zip_tl.list[zip_GETBITS(zip_bl)];
|
|
576 j = t.b;
|
|
577 zip_DUMPBITS(j);
|
|
578 j = t.n;
|
|
579 if(j < 16) // length of code in bits (0..15)
|
|
580 ll[i++] = l = j; // save last length in l
|
|
581 else if(j == 16) { // repeat last length 3 to 6 times
|
|
582 zip_NEEDBITS(2);
|
|
583 j = 3 + zip_GETBITS(2);
|
|
584 zip_DUMPBITS(2);
|
|
585 if(i + j > n)
|
|
586 return -1;
|
|
587 while(j-- > 0)
|
|
588 ll[i++] = l;
|
|
589 } else if(j == 17) { // 3 to 10 zero length codes
|
|
590 zip_NEEDBITS(3);
|
|
591 j = 3 + zip_GETBITS(3);
|
|
592 zip_DUMPBITS(3);
|
|
593 if(i + j > n)
|
|
594 return -1;
|
|
595 while(j-- > 0)
|
|
596 ll[i++] = 0;
|
|
597 l = 0;
|
|
598 } else { // j == 18: 11 to 138 zero length codes
|
|
599 zip_NEEDBITS(7);
|
|
600 j = 11 + zip_GETBITS(7);
|
|
601 zip_DUMPBITS(7);
|
|
602 if(i + j > n)
|
|
603 return -1;
|
|
604 while(j-- > 0)
|
|
605 ll[i++] = 0;
|
|
606 l = 0;
|
|
607 }
|
|
608 }
|
|
609
|
|
610 // build the decoding tables for literal/length and distance codes
|
|
611 zip_bl = zip_lbits;
|
|
612 h = new zip_HuftBuild(ll, nl, 257, zip_cplens, zip_cplext, zip_bl);
|
|
613 if(zip_bl == 0) // no literals or lengths
|
|
614 h.status = 1;
|
|
615 if(h.status != 0) {
|
|
616 if(h.status == 1)
|
|
617 ;// **incomplete literal tree**
|
|
618 return -1; // incomplete code set
|
|
619 }
|
|
620 zip_tl = h.root;
|
|
621 zip_bl = h.m;
|
|
622
|
|
623 for(i = 0; i < nd; i++)
|
|
624 ll[i] = ll[i + nl];
|
|
625 zip_bd = zip_dbits;
|
|
626 h = new zip_HuftBuild(ll, nd, 0, zip_cpdist, zip_cpdext, zip_bd);
|
|
627 zip_td = h.root;
|
|
628 zip_bd = h.m;
|
|
629
|
|
630 if(zip_bd == 0 && nl > 257) { // lengths but no distances
|
|
631 // **incomplete distance tree**
|
|
632 return -1;
|
|
633 }
|
|
634
|
|
635 if(h.status == 1) {
|
|
636 ;// **incomplete distance tree**
|
|
637 }
|
|
638 if(h.status != 0)
|
|
639 return -1;
|
|
640
|
|
641 // decompress until an end-of-block code
|
|
642 return zip_inflate_codes(buff, off, size);
|
|
643 }
|
|
644
|
|
645 function zip_inflate_start() {
|
|
646 var i;
|
|
647
|
|
648 if(zip_slide == null)
|
|
649 zip_slide = new Array(2 * zip_WSIZE);
|
|
650 zip_wp = 0;
|
|
651 zip_bit_buf = 0;
|
|
652 zip_bit_len = 0;
|
|
653 zip_method = -1;
|
|
654 zip_eof = false;
|
|
655 zip_copy_leng = zip_copy_dist = 0;
|
|
656 zip_tl = null;
|
|
657 }
|
|
658
|
|
659 function zip_inflate_internal(buff, off, size) {
|
|
660 // decompress an inflated entry
|
|
661 var n, i;
|
|
662
|
|
663 n = 0;
|
|
664 while(n < size) {
|
|
665 if(zip_eof && zip_method == -1)
|
|
666 return n;
|
|
667
|
|
668 if(zip_copy_leng > 0) {
|
|
669 if(zip_method != zip_STORED_BLOCK) {
|
|
670 // STATIC_TREES or DYN_TREES
|
|
671 while(zip_copy_leng > 0 && n < size) {
|
|
672 zip_copy_leng--;
|
|
673 zip_copy_dist &= zip_WSIZE - 1;
|
|
674 zip_wp &= zip_WSIZE - 1;
|
|
675 buff[off + n++] = zip_slide[zip_wp++] =
|
|
676 zip_slide[zip_copy_dist++];
|
|
677 }
|
|
678 } else {
|
|
679 while(zip_copy_leng > 0 && n < size) {
|
|
680 zip_copy_leng--;
|
|
681 zip_wp &= zip_WSIZE - 1;
|
|
682 zip_NEEDBITS(8);
|
|
683 buff[off + n++] = zip_slide[zip_wp++] = zip_GETBITS(8);
|
|
684 zip_DUMPBITS(8);
|
|
685 }
|
|
686 if(zip_copy_leng == 0)
|
|
687 zip_method = -1; // done
|
|
688 }
|
|
689 if(n == size)
|
|
690 return n;
|
|
691 }
|
|
692
|
|
693 if(zip_method == -1) {
|
|
694 if(zip_eof)
|
|
695 break;
|
|
696
|
|
697 // read in last block bit
|
|
698 zip_NEEDBITS(1);
|
|
699 if(zip_GETBITS(1) != 0)
|
|
700 zip_eof = true;
|
|
701 zip_DUMPBITS(1);
|
|
702
|
|
703 // read in block type
|
|
704 zip_NEEDBITS(2);
|
|
705 zip_method = zip_GETBITS(2);
|
|
706 zip_DUMPBITS(2);
|
|
707 zip_tl = null;
|
|
708 zip_copy_leng = 0;
|
|
709 }
|
|
710
|
|
711 switch(zip_method) {
|
|
712 case 0: // zip_STORED_BLOCK
|
|
713 i = zip_inflate_stored(buff, off + n, size - n);
|
|
714 break;
|
|
715
|
|
716 case 1: // zip_STATIC_TREES
|
|
717 if(zip_tl != null)
|
|
718 i = zip_inflate_codes(buff, off + n, size - n);
|
|
719 else
|
|
720 i = zip_inflate_fixed(buff, off + n, size - n);
|
|
721 break;
|
|
722
|
|
723 case 2: // zip_DYN_TREES
|
|
724 if(zip_tl != null)
|
|
725 i = zip_inflate_codes(buff, off + n, size - n);
|
|
726 else
|
|
727 i = zip_inflate_dynamic(buff, off + n, size - n);
|
|
728 break;
|
|
729
|
|
730 default: // error
|
|
731 i = -1;
|
|
732 break;
|
|
733 }
|
|
734
|
|
735 if(i == -1) {
|
|
736 if(zip_eof)
|
|
737 return 0;
|
|
738 return -1;
|
|
739 }
|
|
740 n += i;
|
|
741 }
|
|
742 return n;
|
|
743 }
|
|
744
|
|
745 function zip_inflate(str) {
|
|
746 var out, buff;
|
|
747 var i, j;
|
|
748
|
|
749 zip_inflate_start();
|
|
750 zip_inflate_data = str;
|
|
751 zip_inflate_pos = 0;
|
|
752
|
|
753 buff = new Array(1024);
|
|
754 out = "";
|
|
755 while((i = zip_inflate_internal(buff, 0, buff.length)) > 0) {
|
|
756 for(j = 0; j < i; j++)
|
|
757 out += String.fromCharCode(buff[j]);
|
|
758 }
|
|
759 zip_inflate_data = null; // G.C.
|
|
760 return out;
|
|
761 }
|
|
762
|
|
763 //
|
|
764 // end of the script of Masanao Izumo.
|
|
765 //
|
|
766
|
|
767 // we add the compression method for JSZip
|
|
768 if(!JSZip.compressions["DEFLATE"]) {
|
|
769 JSZip.compressions["DEFLATE"] = {
|
|
770 magic : "\x08\x00",
|
|
771 uncompress : zip_inflate
|
|
772 }
|
|
773 } else {
|
|
774 JSZip.compressions["DEFLATE"].uncompress = zip_inflate;
|
|
775 }
|
|
776
|
|
777 })();
|
|
778
|
|
779 // enforcing Stuk's coding style
|
|
780 // vim: set shiftwidth=3 softtabstop=3:
|