Advertisement
rg443

javascript inflate

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