Extends the Chrome Developer Tools, adding a new Network Panel in the Developer Tools window with better searching and response previews. https://leviolson.com/posts/chrome-ext-better-network-panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
19 KiB

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