rawinflate.js 19 KB

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