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