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.

449 lines
13 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 Inflate
  40. {
  41. private const int MAX_WBITS = 15; // 32K LZ77 window
  42. // preset dictionary flag in zlib header
  43. private const int PRESET_DICT = 0x20;
  44. internal const int Z_NO_FLUSH = 0;
  45. internal const int Z_PARTIAL_FLUSH = 1;
  46. internal const int Z_SYNC_FLUSH = 2;
  47. internal const int Z_FULL_FLUSH = 3;
  48. internal const int Z_FINISH = 4;
  49. private const int Z_DEFLATED = 8;
  50. private const int Z_OK = 0;
  51. private const int Z_STREAM_END = 1;
  52. private const int Z_NEED_DICT = 2;
  53. private const int Z_ERRNO = - 1;
  54. private const int Z_STREAM_ERROR = - 2;
  55. private const int Z_DATA_ERROR = - 3;
  56. private const int Z_MEM_ERROR = - 4;
  57. private const int Z_BUF_ERROR = - 5;
  58. private const int Z_VERSION_ERROR = - 6;
  59. private const int METHOD = 0; // waiting for method byte
  60. private const int FLAG = 1; // waiting for flag byte
  61. private const int DICT4 = 2; // four dictionary check bytes to go
  62. private const int DICT3 = 3; // three dictionary check bytes to go
  63. private const int DICT2 = 4; // two dictionary check bytes to go
  64. private const int DICT1 = 5; // one dictionary check byte to go
  65. private const int DICT0 = 6; // waiting for inflateSetDictionary
  66. private const int BLOCKS = 7; // decompressing blocks
  67. private const int CHECK4 = 8; // four check bytes to go
  68. private const int CHECK3 = 9; // three check bytes to go
  69. private const int CHECK2 = 10; // two check bytes to go
  70. private const int CHECK1 = 11; // one check byte to go
  71. private const int DONE = 12; // finished check, done
  72. private const int BAD = 13; // got an error--stay here
  73. internal int mode; // current inflate mode
  74. // mode dependent information
  75. internal int method; // if FLAGS, method byte
  76. // if CHECK, check values to compare
  77. internal long[] was = new long[1]; // computed check value
  78. internal long need; // stream check value
  79. // if BAD, inflateSync's marker bytes count
  80. internal int marker;
  81. // mode independent information
  82. internal int nowrap; // flag for no wrapper
  83. internal int wbits; // log2(window size) (8..15, defaults to 15)
  84. internal InfBlocks blocks; // current inflate_blocks state
  85. internal int inflateReset(ZStream z)
  86. {
  87. if (z == null || z.istate == null)
  88. return Z_STREAM_ERROR;
  89. z.total_in = z.total_out = 0;
  90. z.msg = null;
  91. z.istate.mode = z.istate.nowrap != 0?BLOCKS:METHOD;
  92. z.istate.blocks.reset(z, null);
  93. return Z_OK;
  94. }
  95. internal int inflateEnd(ZStream z)
  96. {
  97. if (blocks != null)
  98. blocks.free(z);
  99. blocks = null;
  100. // ZFREE(z, z->state);
  101. return Z_OK;
  102. }
  103. internal int inflateInit(ZStream z, int w)
  104. {
  105. z.msg = null;
  106. blocks = null;
  107. // handle undocumented nowrap option (no zlib header or check)
  108. nowrap = 0;
  109. if (w < 0)
  110. {
  111. w = - w;
  112. nowrap = 1;
  113. }
  114. // set window size
  115. if (w < 8 || w > 15)
  116. {
  117. inflateEnd(z);
  118. return Z_STREAM_ERROR;
  119. }
  120. wbits = w;
  121. z.istate.blocks = new InfBlocks(z, z.istate.nowrap != 0?null:this, 1 << w);
  122. // reset state
  123. inflateReset(z);
  124. return Z_OK;
  125. }
  126. internal int inflate(ZStream z, int f)
  127. {
  128. int r;
  129. int b;
  130. if (z == null || z.istate == null || z.next_in == null)
  131. return Z_STREAM_ERROR;
  132. f = f == Z_FINISH?Z_BUF_ERROR:Z_OK;
  133. r = Z_BUF_ERROR;
  134. while (true)
  135. {
  136. //System.out.println("mode: "+z.istate.mode);
  137. switch (z.istate.mode)
  138. {
  139. case METHOD:
  140. if (z.avail_in == 0)
  141. return r; r = f;
  142. z.avail_in--; z.total_in++;
  143. if (((z.istate.method = z.next_in[z.next_in_index++]) & 0xf) != Z_DEFLATED)
  144. {
  145. z.istate.mode = BAD;
  146. z.msg = "unknown compression method";
  147. z.istate.marker = 5; // can't try inflateSync
  148. break;
  149. }
  150. if ((z.istate.method >> 4) + 8 > z.istate.wbits)
  151. {
  152. z.istate.mode = BAD;
  153. z.msg = "invalid window size";
  154. z.istate.marker = 5; // can't try inflateSync
  155. break;
  156. }
  157. z.istate.mode = FLAG;
  158. goto case FLAG;
  159. case FLAG:
  160. if (z.avail_in == 0)
  161. return r; r = f;
  162. z.avail_in--; z.total_in++;
  163. b = (z.next_in[z.next_in_index++]) & 0xff;
  164. if ((((z.istate.method << 8) + b) % 31) != 0)
  165. {
  166. z.istate.mode = BAD;
  167. z.msg = "incorrect header check";
  168. z.istate.marker = 5; // can't try inflateSync
  169. break;
  170. }
  171. if ((b & PRESET_DICT) == 0)
  172. {
  173. z.istate.mode = BLOCKS;
  174. break;
  175. }
  176. z.istate.mode = DICT4;
  177. goto case DICT4;
  178. case DICT4:
  179. if (z.avail_in == 0)
  180. return r; r = f;
  181. z.avail_in--; z.total_in++;
  182. z.istate.need = ((z.next_in[z.next_in_index++] & 0xff) << 24) & unchecked((int) 0xff000000L);
  183. z.istate.mode = DICT3;
  184. goto case DICT3;
  185. case DICT3:
  186. if (z.avail_in == 0)
  187. return r; r = f;
  188. z.avail_in--; z.total_in++;
  189. z.istate.need += (((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L);
  190. z.istate.mode = DICT2;
  191. goto case DICT2;
  192. case DICT2:
  193. if (z.avail_in == 0)
  194. return r; r = f;
  195. z.avail_in--; z.total_in++;
  196. z.istate.need += (((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L);
  197. z.istate.mode = DICT1;
  198. goto case DICT1;
  199. case DICT1:
  200. if (z.avail_in == 0)
  201. return r; r = f;
  202. z.avail_in--; z.total_in++;
  203. z.istate.need += (z.next_in[z.next_in_index++] & 0xffL);
  204. z.adler = z.istate.need;
  205. z.istate.mode = DICT0;
  206. return Z_NEED_DICT;
  207. case DICT0:
  208. z.istate.mode = BAD;
  209. z.msg = "need dictionary";
  210. z.istate.marker = 0; // can try inflateSync
  211. return Z_STREAM_ERROR;
  212. case BLOCKS:
  213. r = z.istate.blocks.proc(z, r);
  214. if (r == Z_DATA_ERROR)
  215. {
  216. z.istate.mode = BAD;
  217. z.istate.marker = 0; // can try inflateSync
  218. break;
  219. }
  220. if (r == Z_OK)
  221. {
  222. r = f;
  223. }
  224. if (r != Z_STREAM_END)
  225. {
  226. return r;
  227. }
  228. r = f;
  229. z.istate.blocks.reset(z, z.istate.was);
  230. if (z.istate.nowrap != 0)
  231. {
  232. z.istate.mode = DONE;
  233. break;
  234. }
  235. z.istate.mode = CHECK4;
  236. goto case CHECK4;
  237. case CHECK4:
  238. if (z.avail_in == 0)
  239. return r; r = f;
  240. z.avail_in--; z.total_in++;
  241. z.istate.need = ((z.next_in[z.next_in_index++] & 0xff) << 24) & unchecked((int) 0xff000000L);
  242. z.istate.mode = CHECK3;
  243. goto case CHECK3;
  244. case CHECK3:
  245. if (z.avail_in == 0)
  246. return r; r = f;
  247. z.avail_in--; z.total_in++;
  248. z.istate.need += (((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L);
  249. z.istate.mode = CHECK2;
  250. goto case CHECK2;
  251. case CHECK2:
  252. if (z.avail_in == 0)
  253. return r; r = f;
  254. z.avail_in--; z.total_in++;
  255. z.istate.need += (((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L);
  256. z.istate.mode = CHECK1;
  257. goto case CHECK1;
  258. case CHECK1:
  259. if (z.avail_in == 0)
  260. return r; r = f;
  261. z.avail_in--; z.total_in++;
  262. z.istate.need += (z.next_in[z.next_in_index++] & 0xffL);
  263. if (((int) (z.istate.was[0])) != ((int) (z.istate.need)))
  264. {
  265. z.istate.mode = BAD;
  266. z.msg = "incorrect data check";
  267. z.istate.marker = 5; // can't try inflateSync
  268. break;
  269. }
  270. z.istate.mode = DONE;
  271. goto case DONE;
  272. case DONE:
  273. return Z_STREAM_END;
  274. case BAD:
  275. return Z_DATA_ERROR;
  276. default:
  277. return Z_STREAM_ERROR;
  278. }
  279. }
  280. }
  281. internal int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength)
  282. {
  283. int index = 0;
  284. int length = dictLength;
  285. if (z == null || z.istate == null || z.istate.mode != DICT0)
  286. return Z_STREAM_ERROR;
  287. if (z._adler.adler32(1L, dictionary, 0, dictLength) != z.adler)
  288. {
  289. return Z_DATA_ERROR;
  290. }
  291. z.adler = z._adler.adler32(0, null, 0, 0);
  292. if (length >= (1 << z.istate.wbits))
  293. {
  294. length = (1 << z.istate.wbits) - 1;
  295. index = dictLength - length;
  296. }
  297. z.istate.blocks.set_dictionary(dictionary, index, length);
  298. z.istate.mode = BLOCKS;
  299. return Z_OK;
  300. }
  301. private static byte[] mark = new byte[]{(byte) 0, (byte) 0, (byte) SupportClass.Identity(0xff), (byte) SupportClass.Identity(0xff)};
  302. internal int inflateSync(ZStream z)
  303. {
  304. int n; // number of bytes to look at
  305. int p; // pointer to bytes
  306. int m; // number of marker bytes found in a row
  307. long r, w; // temporaries to save total_in and total_out
  308. // set up
  309. if (z == null || z.istate == null)
  310. return Z_STREAM_ERROR;
  311. if (z.istate.mode != BAD)
  312. {
  313. z.istate.mode = BAD;
  314. z.istate.marker = 0;
  315. }
  316. if ((n = z.avail_in) == 0)
  317. return Z_BUF_ERROR;
  318. p = z.next_in_index;
  319. m = z.istate.marker;
  320. // search
  321. while (n != 0 && m < 4)
  322. {
  323. if (z.next_in[p] == mark[m])
  324. {
  325. m++;
  326. }
  327. else if (z.next_in[p] != 0)
  328. {
  329. m = 0;
  330. }
  331. else
  332. {
  333. m = 4 - m;
  334. }
  335. p++; n--;
  336. }
  337. // restore
  338. z.total_in += p - z.next_in_index;
  339. z.next_in_index = p;
  340. z.avail_in = n;
  341. z.istate.marker = m;
  342. // return no joy or set up to restart on a new block
  343. if (m != 4)
  344. {
  345. return Z_DATA_ERROR;
  346. }
  347. r = z.total_in; w = z.total_out;
  348. inflateReset(z);
  349. z.total_in = r; z.total_out = w;
  350. z.istate.mode = BLOCKS;
  351. return Z_OK;
  352. }
  353. // Returns true if inflate is currently at the end of a block generated
  354. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  355. // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  356. // but removes the length bytes of the resulting empty stored block. When
  357. // decompressing, PPP checks that at the end of input packet, inflate is
  358. // waiting for these length bytes.
  359. internal int inflateSyncPoint(ZStream z)
  360. {
  361. if (z == null || z.istate == null || z.istate.blocks == null)
  362. return Z_STREAM_ERROR;
  363. return z.istate.blocks.sync_point();
  364. }
  365. }
  366. }