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.

719 lines
19 KiB

  1. // Copyright (c) 2006, ComponentAce
  2. // http://www.componentace.com
  3. // All rights reserved.
  4. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  7. // Neither the name of ComponentAce nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  8. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9. /*
  10. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in
  17. the documentation and/or other materials provided with the distribution.
  18. 3. The names of the authors may not be used to endorse or promote products
  19. derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  23. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  26. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * This program is based on zlib-1.1.3, so all credit should go authors
  33. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  34. * and contributors of zlib.
  35. */
  36. using System;
  37. namespace zlib
  38. {
  39. sealed class InfBlocks
  40. {
  41. private const int MANY = 1440;
  42. // And'ing with mask[n] masks the lower n bits
  43. private static readonly int[] inflate_mask = new int[]{0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff};
  44. // Table for deflate from PKZIP's appnote.txt.
  45. internal static readonly int[] border = new int[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  46. private const int Z_OK = 0;
  47. private const int Z_STREAM_END = 1;
  48. private const int Z_NEED_DICT = 2;
  49. private const int Z_ERRNO = - 1;
  50. private const int Z_STREAM_ERROR = - 2;
  51. private const int Z_DATA_ERROR = - 3;
  52. private const int Z_MEM_ERROR = - 4;
  53. private const int Z_BUF_ERROR = - 5;
  54. private const int Z_VERSION_ERROR = - 6;
  55. private const int TYPE = 0; // get type bits (3, including end bit)
  56. private const int LENS = 1; // get lengths for stored
  57. private const int STORED = 2; // processing stored block
  58. private const int TABLE = 3; // get table lengths
  59. private const int BTREE = 4; // get bit lengths tree for a dynamic block
  60. private const int DTREE = 5; // get length, distance trees for a dynamic block
  61. private const int CODES = 6; // processing fixed or dynamic block
  62. private const int DRY = 7; // output remaining window bytes
  63. private const int DONE = 8; // finished last block, done
  64. private const int BAD = 9; // ot a data error--stuck here
  65. internal int mode; // current inflate_block mode
  66. internal int left; // if STORED, bytes left to copy
  67. internal int table; // table lengths (14 bits)
  68. internal int index; // index into blens (or border)
  69. internal int[] blens; // bit lengths of codes
  70. internal int[] bb = new int[1]; // bit length tree depth
  71. internal int[] tb = new int[1]; // bit length decoding tree
  72. internal InfCodes codes; // if CODES, current state
  73. internal int last; // true if this block is the last block
  74. // mode independent information
  75. internal int bitk; // bits in bit buffer
  76. internal int bitb; // bit buffer
  77. internal int[] hufts; // single malloc for tree space
  78. internal byte[] window; // sliding window
  79. internal int end; // one byte after sliding window
  80. internal int read; // window read pointer
  81. internal int write; // window write pointer
  82. internal System.Object checkfn; // check function
  83. internal long check; // check on output
  84. internal InfBlocks(ZStream z, System.Object checkfn, int w)
  85. {
  86. hufts = new int[MANY * 3];
  87. window = new byte[w];
  88. end = w;
  89. this.checkfn = checkfn;
  90. mode = TYPE;
  91. reset(z, null);
  92. }
  93. internal void reset(ZStream z, long[] c)
  94. {
  95. if (c != null)
  96. c[0] = check;
  97. if (mode == BTREE || mode == DTREE)
  98. {
  99. blens = null;
  100. }
  101. if (mode == CODES)
  102. {
  103. codes.free(z);
  104. }
  105. mode = TYPE;
  106. bitk = 0;
  107. bitb = 0;
  108. read = write = 0;
  109. if (checkfn != null)
  110. z.adler = check = z._adler.adler32(0L, null, 0, 0);
  111. }
  112. internal int proc(ZStream z, int r)
  113. {
  114. int t; // temporary storage
  115. int b; // bit buffer
  116. int k; // bits in bit buffer
  117. int p; // input data pointer
  118. int n; // bytes available there
  119. int q; // output window write pointer
  120. int m; // bytes to end of window or read pointer
  121. // copy input/output information to locals (UPDATE macro restores)
  122. {
  123. p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;
  124. }
  125. {
  126. q = write; m = (int) (q < read?read - q - 1:end - q);
  127. }
  128. // process input based on current state
  129. while (true)
  130. {
  131. switch (mode)
  132. {
  133. case TYPE:
  134. while (k < (3))
  135. {
  136. if (n != 0)
  137. {
  138. r = Z_OK;
  139. }
  140. else
  141. {
  142. bitb = b; bitk = k;
  143. z.avail_in = n;
  144. z.total_in += p - z.next_in_index; z.next_in_index = p;
  145. write = q;
  146. return inflate_flush(z, r);
  147. }
  148. ;
  149. n--;
  150. b |= (z.next_in[p++] & 0xff) << k;
  151. k += 8;
  152. }
  153. t = (int) (b & 7);
  154. last = t & 1;
  155. switch (SupportClass.URShift(t, 1))
  156. {
  157. case 0: // stored
  158. {
  159. b = SupportClass.URShift(b, (3)); k -= (3);
  160. }
  161. t = k & 7; // go to byte boundary
  162. {
  163. b = SupportClass.URShift(b, (t)); k -= (t);
  164. }
  165. mode = LENS; // get length of stored block
  166. break;
  167. case 1: // fixed
  168. {
  169. int[] bl = new int[1];
  170. int[] bd = new int[1];
  171. int[][] tl = new int[1][];
  172. int[][] td = new int[1][];
  173. InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
  174. codes = new InfCodes(bl[0], bd[0], tl[0], td[0], z);
  175. }
  176. {
  177. b = SupportClass.URShift(b, (3)); k -= (3);
  178. }
  179. mode = CODES;
  180. break;
  181. case 2: // dynamic
  182. {
  183. b = SupportClass.URShift(b, (3)); k -= (3);
  184. }
  185. mode = TABLE;
  186. break;
  187. case 3: // illegal
  188. {
  189. b = SupportClass.URShift(b, (3)); k -= (3);
  190. }
  191. mode = BAD;
  192. z.msg = "invalid block type";
  193. r = Z_DATA_ERROR;
  194. bitb = b; bitk = k;
  195. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  196. write = q;
  197. return inflate_flush(z, r);
  198. }
  199. break;
  200. case LENS:
  201. while (k < (32))
  202. {
  203. if (n != 0)
  204. {
  205. r = Z_OK;
  206. }
  207. else
  208. {
  209. bitb = b; bitk = k;
  210. z.avail_in = n;
  211. z.total_in += p - z.next_in_index; z.next_in_index = p;
  212. write = q;
  213. return inflate_flush(z, r);
  214. }
  215. ;
  216. n--;
  217. b |= (z.next_in[p++] & 0xff) << k;
  218. k += 8;
  219. }
  220. if (((SupportClass.URShift((~ b), 16)) & 0xffff) != (b & 0xffff))
  221. {
  222. mode = BAD;
  223. z.msg = "invalid stored block lengths";
  224. r = Z_DATA_ERROR;
  225. bitb = b; bitk = k;
  226. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  227. write = q;
  228. return inflate_flush(z, r);
  229. }
  230. left = (b & 0xffff);
  231. b = k = 0; // dump bits
  232. mode = left != 0?STORED:(last != 0?DRY:TYPE);
  233. break;
  234. case STORED:
  235. if (n == 0)
  236. {
  237. bitb = b; bitk = k;
  238. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  239. write = q;
  240. return inflate_flush(z, r);
  241. }
  242. if (m == 0)
  243. {
  244. if (q == end && read != 0)
  245. {
  246. q = 0; m = (int) (q < read?read - q - 1:end - q);
  247. }
  248. if (m == 0)
  249. {
  250. write = q;
  251. r = inflate_flush(z, r);
  252. q = write; m = (int) (q < read?read - q - 1:end - q);
  253. if (q == end && read != 0)
  254. {
  255. q = 0; m = (int) (q < read?read - q - 1:end - q);
  256. }
  257. if (m == 0)
  258. {
  259. bitb = b; bitk = k;
  260. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  261. write = q;
  262. return inflate_flush(z, r);
  263. }
  264. }
  265. }
  266. r = Z_OK;
  267. t = left;
  268. if (t > n)
  269. t = n;
  270. if (t > m)
  271. t = m;
  272. Array.Copy(z.next_in, p, window, q, t);
  273. p += t; n -= t;
  274. q += t; m -= t;
  275. if ((left -= t) != 0)
  276. break;
  277. mode = last != 0?DRY:TYPE;
  278. break;
  279. case TABLE:
  280. while (k < (14))
  281. {
  282. if (n != 0)
  283. {
  284. r = Z_OK;
  285. }
  286. else
  287. {
  288. bitb = b; bitk = k;
  289. z.avail_in = n;
  290. z.total_in += p - z.next_in_index; z.next_in_index = p;
  291. write = q;
  292. return inflate_flush(z, r);
  293. }
  294. ;
  295. n--;
  296. b |= (z.next_in[p++] & 0xff) << k;
  297. k += 8;
  298. }
  299. table = t = (b & 0x3fff);
  300. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  301. {
  302. mode = BAD;
  303. z.msg = "too many length or distance symbols";
  304. r = Z_DATA_ERROR;
  305. bitb = b; bitk = k;
  306. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  307. write = q;
  308. return inflate_flush(z, r);
  309. }
  310. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  311. blens = new int[t];
  312. {
  313. b = SupportClass.URShift(b, (14)); k -= (14);
  314. }
  315. index = 0;
  316. mode = BTREE;
  317. goto case BTREE;
  318. case BTREE:
  319. while (index < 4 + (SupportClass.URShift(table, 10)))
  320. {
  321. while (k < (3))
  322. {
  323. if (n != 0)
  324. {
  325. r = Z_OK;
  326. }
  327. else
  328. {
  329. bitb = b; bitk = k;
  330. z.avail_in = n;
  331. z.total_in += p - z.next_in_index; z.next_in_index = p;
  332. write = q;
  333. return inflate_flush(z, r);
  334. }
  335. ;
  336. n--;
  337. b |= (z.next_in[p++] & 0xff) << k;
  338. k += 8;
  339. }
  340. blens[border[index++]] = b & 7;
  341. {
  342. b = SupportClass.URShift(b, (3)); k -= (3);
  343. }
  344. }
  345. while (index < 19)
  346. {
  347. blens[border[index++]] = 0;
  348. }
  349. bb[0] = 7;
  350. t = InfTree.inflate_trees_bits(blens, bb, tb, hufts, z);
  351. if (t != Z_OK)
  352. {
  353. r = t;
  354. if (r == Z_DATA_ERROR)
  355. {
  356. blens = null;
  357. mode = BAD;
  358. }
  359. bitb = b; bitk = k;
  360. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  361. write = q;
  362. return inflate_flush(z, r);
  363. }
  364. index = 0;
  365. mode = DTREE;
  366. goto case DTREE;
  367. case DTREE:
  368. while (true)
  369. {
  370. t = table;
  371. if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)))
  372. {
  373. break;
  374. }
  375. int i, j, c;
  376. t = bb[0];
  377. while (k < (t))
  378. {
  379. if (n != 0)
  380. {
  381. r = Z_OK;
  382. }
  383. else
  384. {
  385. bitb = b; bitk = k;
  386. z.avail_in = n;
  387. z.total_in += p - z.next_in_index; z.next_in_index = p;
  388. write = q;
  389. return inflate_flush(z, r);
  390. }
  391. ;
  392. n--;
  393. b |= (z.next_in[p++] & 0xff) << k;
  394. k += 8;
  395. }
  396. if (tb[0] == - 1)
  397. {
  398. //System.err.println("null...");
  399. }
  400. t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
  401. c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
  402. if (c < 16)
  403. {
  404. b = SupportClass.URShift(b, (t)); k -= (t);
  405. blens[index++] = c;
  406. }
  407. else
  408. {
  409. // c == 16..18
  410. i = c == 18?7:c - 14;
  411. j = c == 18?11:3;
  412. while (k < (t + i))
  413. {
  414. if (n != 0)
  415. {
  416. r = Z_OK;
  417. }
  418. else
  419. {
  420. bitb = b; bitk = k;
  421. z.avail_in = n;
  422. z.total_in += p - z.next_in_index; z.next_in_index = p;
  423. write = q;
  424. return inflate_flush(z, r);
  425. }
  426. ;
  427. n--;
  428. b |= (z.next_in[p++] & 0xff) << k;
  429. k += 8;
  430. }
  431. b = SupportClass.URShift(b, (t)); k -= (t);
  432. j += (b & inflate_mask[i]);
  433. b = SupportClass.URShift(b, (i)); k -= (i);
  434. i = index;
  435. t = table;
  436. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1))
  437. {
  438. blens = null;
  439. mode = BAD;
  440. z.msg = "invalid bit length repeat";
  441. r = Z_DATA_ERROR;
  442. bitb = b; bitk = k;
  443. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  444. write = q;
  445. return inflate_flush(z, r);
  446. }
  447. c = c == 16?blens[i - 1]:0;
  448. do
  449. {
  450. blens[i++] = c;
  451. }
  452. while (--j != 0);
  453. index = i;
  454. }
  455. }
  456. tb[0] = - 1;
  457. {
  458. int[] bl = new int[1];
  459. int[] bd = new int[1];
  460. int[] tl = new int[1];
  461. int[] td = new int[1];
  462. bl[0] = 9; // must be <= 9 for lookahead assumptions
  463. bd[0] = 6; // must be <= 9 for lookahead assumptions
  464. t = table;
  465. t = InfTree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl, bd, tl, td, hufts, z);
  466. if (t != Z_OK)
  467. {
  468. if (t == Z_DATA_ERROR)
  469. {
  470. blens = null;
  471. mode = BAD;
  472. }
  473. r = t;
  474. bitb = b; bitk = k;
  475. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  476. write = q;
  477. return inflate_flush(z, r);
  478. }
  479. codes = new InfCodes(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
  480. }
  481. blens = null;
  482. mode = CODES;
  483. goto case CODES;
  484. case CODES:
  485. bitb = b; bitk = k;
  486. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  487. write = q;
  488. if ((r = codes.proc(this, z, r)) != Z_STREAM_END)
  489. {
  490. return inflate_flush(z, r);
  491. }
  492. r = Z_OK;
  493. codes.free(z);
  494. p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;
  495. q = write; m = (int) (q < read?read - q - 1:end - q);
  496. if (last == 0)
  497. {
  498. mode = TYPE;
  499. break;
  500. }
  501. mode = DRY;
  502. goto case DRY;
  503. case DRY:
  504. write = q;
  505. r = inflate_flush(z, r);
  506. q = write; m = (int) (q < read?read - q - 1:end - q);
  507. if (read != write)
  508. {
  509. bitb = b; bitk = k;
  510. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  511. write = q;
  512. return inflate_flush(z, r);
  513. }
  514. mode = DONE;
  515. goto case DONE;
  516. case DONE:
  517. r = Z_STREAM_END;
  518. bitb = b; bitk = k;
  519. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  520. write = q;
  521. return inflate_flush(z, r);
  522. case BAD:
  523. r = Z_DATA_ERROR;
  524. bitb = b; bitk = k;
  525. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  526. write = q;
  527. return inflate_flush(z, r);
  528. default:
  529. r = Z_STREAM_ERROR;
  530. bitb = b; bitk = k;
  531. z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
  532. write = q;
  533. return inflate_flush(z, r);
  534. }
  535. }
  536. }
  537. internal void free(ZStream z)
  538. {
  539. reset(z, null);
  540. window = null;
  541. hufts = null;
  542. //ZFREE(z, s);
  543. }
  544. internal void set_dictionary(byte[] d, int start, int n)
  545. {
  546. Array.Copy(d, start, window, 0, n);
  547. read = write = n;
  548. }
  549. // Returns true if inflate is currently at the end of a block generated
  550. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  551. internal int sync_point()
  552. {
  553. return mode == LENS?1:0;
  554. }
  555. // copy as much as possible from the sliding window to the output area
  556. internal int inflate_flush(ZStream z, int r)
  557. {
  558. int n;
  559. int p;
  560. int q;
  561. // local copies of source and destination pointers
  562. p = z.next_out_index;
  563. q = read;
  564. // compute number of bytes to copy as far as end of window
  565. n = (int) ((q <= write?write:end) - q);
  566. if (n > z.avail_out)
  567. n = z.avail_out;
  568. if (n != 0 && r == Z_BUF_ERROR)
  569. r = Z_OK;
  570. // update counters
  571. z.avail_out -= n;
  572. z.total_out += n;
  573. // update check information
  574. if (checkfn != null)
  575. z.adler = check = z._adler.adler32(check, window, q, n);
  576. // copy as far as end of window
  577. Array.Copy(window, q, z.next_out, p, n);
  578. p += n;
  579. q += n;
  580. // see if more to copy at beginning of window
  581. if (q == end)
  582. {
  583. // wrap pointers
  584. q = 0;
  585. if (write == end)
  586. write = 0;
  587. // compute bytes to copy
  588. n = write - q;
  589. if (n > z.avail_out)
  590. n = z.avail_out;
  591. if (n != 0 && r == Z_BUF_ERROR)
  592. r = Z_OK;
  593. // update counters
  594. z.avail_out -= n;
  595. z.total_out += n;
  596. // update check information
  597. if (checkfn != null)
  598. z.adler = check = z._adler.adler32(check, window, q, n);
  599. // copy
  600. Array.Copy(window, q, z.next_out, p, n);
  601. p += n;
  602. q += n;
  603. }
  604. // update pointers
  605. z.next_out_index = p;
  606. read = q;
  607. // done
  608. return r;
  609. }
  610. }
  611. }