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.

inflate.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* inflate.c -- zlib interface to inflate modules
  2. * Copyright (C) 1995-2002 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zutil.h"
  6. #include "infblock.h"
  7. struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  8. typedef enum {
  9. METHOD, /* waiting for method byte */
  10. FLAG, /* waiting for flag byte */
  11. DICT4, /* four dictionary check bytes to go */
  12. DICT3, /* three dictionary check bytes to go */
  13. DICT2, /* two dictionary check bytes to go */
  14. DICT1, /* one dictionary check byte to go */
  15. DICT0, /* waiting for inflateSetDictionary */
  16. BLOCKS, /* decompressing blocks */
  17. CHECK4, /* four check bytes to go */
  18. CHECK3, /* three check bytes to go */
  19. CHECK2, /* two check bytes to go */
  20. CHECK1, /* one check byte to go */
  21. DONE, /* finished check, done */
  22. BAD} /* got an error--stay here */
  23. inflate_mode;
  24. /* inflate private state */
  25. struct internal_state {
  26. /* mode */
  27. inflate_mode mode; /* current inflate mode */
  28. /* mode dependent information */
  29. union {
  30. uInt method; /* if FLAGS, method byte */
  31. struct {
  32. uLong was; /* computed check value */
  33. uLong need; /* stream check value */
  34. } check; /* if CHECK, check values to compare */
  35. uInt marker; /* if BAD, inflateSync's marker bytes count */
  36. } sub; /* submode */
  37. /* mode independent information */
  38. int nowrap; /* flag for no wrapper */
  39. uInt wbits; /* log2(window size) (8..15, defaults to 15) */
  40. inflate_blocks_statef
  41. *blocks; /* current inflate_blocks state */
  42. };
  43. int ZEXPORT inflateReset(z)
  44. z_streamp z;
  45. {
  46. if (z == Z_NULL || z->state == Z_NULL)
  47. return Z_STREAM_ERROR;
  48. z->total_in = z->total_out = 0;
  49. z->msg = Z_NULL;
  50. z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  51. inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  52. Tracev((stderr, "inflate: reset\n"));
  53. return Z_OK;
  54. }
  55. int ZEXPORT inflateEnd(z)
  56. z_streamp z;
  57. {
  58. if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  59. return Z_STREAM_ERROR;
  60. if (z->state->blocks != Z_NULL)
  61. inflate_blocks_free(z->state->blocks, z);
  62. ZFREE(z, z->state);
  63. z->state = Z_NULL;
  64. Tracev((stderr, "inflate: end\n"));
  65. return Z_OK;
  66. }
  67. int ZEXPORT inflateInit2_(z, w, version, stream_size)
  68. z_streamp z;
  69. int w;
  70. const char *version;
  71. int stream_size;
  72. {
  73. if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  74. stream_size != sizeof(z_stream))
  75. return Z_VERSION_ERROR;
  76. /* initialize state */
  77. if (z == Z_NULL)
  78. return Z_STREAM_ERROR;
  79. z->msg = Z_NULL;
  80. if (z->zalloc == Z_NULL)
  81. {
  82. z->zalloc = zcalloc;
  83. z->opaque = (voidpf)0;
  84. }
  85. if (z->zfree == Z_NULL) z->zfree = zcfree;
  86. if ((z->state = (struct internal_state FAR *)
  87. ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  88. return Z_MEM_ERROR;
  89. z->state->blocks = Z_NULL;
  90. /* handle undocumented nowrap option (no zlib header or check) */
  91. z->state->nowrap = 0;
  92. if (w < 0)
  93. {
  94. w = - w;
  95. z->state->nowrap = 1;
  96. }
  97. /* set window size */
  98. if (w < 8 || w > 15)
  99. {
  100. inflateEnd(z);
  101. return Z_STREAM_ERROR;
  102. }
  103. z->state->wbits = (uInt)w;
  104. /* create inflate_blocks state */
  105. if ((z->state->blocks =
  106. inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  107. == Z_NULL)
  108. {
  109. inflateEnd(z);
  110. return Z_MEM_ERROR;
  111. }
  112. Tracev((stderr, "inflate: allocated\n"));
  113. /* reset state */
  114. inflateReset(z);
  115. return Z_OK;
  116. }
  117. int ZEXPORT inflateInit_(z, version, stream_size)
  118. z_streamp z;
  119. const char *version;
  120. int stream_size;
  121. {
  122. return inflateInit2_(z, DEF_WBITS, version, stream_size);
  123. }
  124. #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  125. #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  126. int ZEXPORT inflate(z, f)
  127. z_streamp z;
  128. int f;
  129. {
  130. int r;
  131. uInt b;
  132. if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  133. return Z_STREAM_ERROR;
  134. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  135. r = Z_BUF_ERROR;
  136. while (1) switch (z->state->mode)
  137. {
  138. case METHOD:
  139. NEEDBYTE
  140. if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  141. {
  142. z->state->mode = BAD;
  143. z->msg = (char*)"unknown compression method";
  144. z->state->sub.marker = 5; /* can't try inflateSync */
  145. break;
  146. }
  147. if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  148. {
  149. z->state->mode = BAD;
  150. z->msg = (char*)"invalid window size";
  151. z->state->sub.marker = 5; /* can't try inflateSync */
  152. break;
  153. }
  154. z->state->mode = FLAG;
  155. case FLAG:
  156. NEEDBYTE
  157. b = NEXTBYTE;
  158. if (((z->state->sub.method << 8) + b) % 31)
  159. {
  160. z->state->mode = BAD;
  161. z->msg = (char*)"incorrect header check";
  162. z->state->sub.marker = 5; /* can't try inflateSync */
  163. break;
  164. }
  165. Tracev((stderr, "inflate: zlib header ok\n"));
  166. if (!(b & PRESET_DICT))
  167. {
  168. z->state->mode = BLOCKS;
  169. break;
  170. }
  171. z->state->mode = DICT4;
  172. case DICT4:
  173. NEEDBYTE
  174. z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  175. z->state->mode = DICT3;
  176. case DICT3:
  177. NEEDBYTE
  178. z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  179. z->state->mode = DICT2;
  180. case DICT2:
  181. NEEDBYTE
  182. z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  183. z->state->mode = DICT1;
  184. case DICT1:
  185. NEEDBYTE
  186. z->state->sub.check.need += (uLong)NEXTBYTE;
  187. z->adler = z->state->sub.check.need;
  188. z->state->mode = DICT0;
  189. return Z_NEED_DICT;
  190. case DICT0:
  191. z->state->mode = BAD;
  192. z->msg = (char*)"need dictionary";
  193. z->state->sub.marker = 0; /* can try inflateSync */
  194. return Z_STREAM_ERROR;
  195. case BLOCKS:
  196. r = inflate_blocks(z->state->blocks, z, r);
  197. if (r == Z_DATA_ERROR)
  198. {
  199. z->state->mode = BAD;
  200. z->state->sub.marker = 0; /* can try inflateSync */
  201. break;
  202. }
  203. if (r == Z_OK)
  204. r = f;
  205. if (r != Z_STREAM_END)
  206. return r;
  207. r = f;
  208. inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  209. if (z->state->nowrap)
  210. {
  211. z->state->mode = DONE;
  212. break;
  213. }
  214. z->state->mode = CHECK4;
  215. case CHECK4:
  216. NEEDBYTE
  217. z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  218. z->state->mode = CHECK3;
  219. case CHECK3:
  220. NEEDBYTE
  221. z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  222. z->state->mode = CHECK2;
  223. case CHECK2:
  224. NEEDBYTE
  225. z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  226. z->state->mode = CHECK1;
  227. case CHECK1:
  228. NEEDBYTE
  229. z->state->sub.check.need += (uLong)NEXTBYTE;
  230. if (z->state->sub.check.was != z->state->sub.check.need)
  231. {
  232. z->state->mode = BAD;
  233. z->msg = (char*)"incorrect data check";
  234. z->state->sub.marker = 5; /* can't try inflateSync */
  235. break;
  236. }
  237. Tracev((stderr, "inflate: zlib check ok\n"));
  238. z->state->mode = DONE;
  239. case DONE:
  240. return Z_STREAM_END;
  241. case BAD:
  242. return Z_DATA_ERROR;
  243. default:
  244. return Z_STREAM_ERROR;
  245. }
  246. #ifdef NEED_DUMMY_RETURN
  247. return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  248. #endif
  249. }
  250. int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
  251. z_streamp z;
  252. const Bytef *dictionary;
  253. uInt dictLength;
  254. {
  255. uInt length = dictLength;
  256. if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  257. return Z_STREAM_ERROR;
  258. if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  259. z->adler = 1L;
  260. if (length >= ((uInt)1<<z->state->wbits))
  261. {
  262. length = (1<<z->state->wbits)-1;
  263. dictionary += dictLength - length;
  264. }
  265. inflate_set_dictionary(z->state->blocks, dictionary, length);
  266. z->state->mode = BLOCKS;
  267. return Z_OK;
  268. }
  269. int ZEXPORT inflateSync(z)
  270. z_streamp z;
  271. {
  272. uInt n; /* number of bytes to look at */
  273. Bytef *p; /* pointer to bytes */
  274. uInt m; /* number of marker bytes found in a row */
  275. uLong r, w; /* temporaries to save total_in and total_out */
  276. /* set up */
  277. if (z == Z_NULL || z->state == Z_NULL)
  278. return Z_STREAM_ERROR;
  279. if (z->state->mode != BAD)
  280. {
  281. z->state->mode = BAD;
  282. z->state->sub.marker = 0;
  283. }
  284. if ((n = z->avail_in) == 0)
  285. return Z_BUF_ERROR;
  286. p = z->next_in;
  287. m = z->state->sub.marker;
  288. /* search */
  289. while (n && m < 4)
  290. {
  291. static const Byte mark[4] = {0, 0, 0xff, 0xff};
  292. if (*p == mark[m])
  293. m++;
  294. else if (*p)
  295. m = 0;
  296. else
  297. m = 4 - m;
  298. p++, n--;
  299. }
  300. /* restore */
  301. z->total_in += p - z->next_in;
  302. z->next_in = p;
  303. z->avail_in = n;
  304. z->state->sub.marker = m;
  305. /* return no joy or set up to restart on a new block */
  306. if (m != 4)
  307. return Z_DATA_ERROR;
  308. r = z->total_in; w = z->total_out;
  309. inflateReset(z);
  310. z->total_in = r; z->total_out = w;
  311. z->state->mode = BLOCKS;
  312. return Z_OK;
  313. }
  314. /* Returns true if inflate is currently at the end of a block generated
  315. * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  316. * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  317. * but removes the length bytes of the resulting empty stored block. When
  318. * decompressing, PPP checks that at the end of input packet, inflate is
  319. * waiting for these length bytes.
  320. */
  321. int ZEXPORT inflateSyncPoint(z)
  322. z_streamp z;
  323. {
  324. if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  325. return Z_STREAM_ERROR;
  326. return inflate_blocks_sync_point(z->state->blocks);
  327. }