Leaked source code of windows server 2003
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.

403 lines
12 KiB

  1. /* infblock.c -- interpret and process block types to last block
  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. #include "inftrees.h"
  8. #include "infcodes.h"
  9. #include "infutil.h"
  10. struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  11. /* simplify the use of the inflate_huft type with some defines */
  12. #define exop word.what.Exop
  13. #define bits word.what.Bits
  14. /* Table for deflate from PKZIP's appnote.txt. */
  15. local const uInt border[] = { /* Order of the bit length code lengths */
  16. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  17. /*
  18. Notes beyond the 1.93a appnote.txt:
  19. 1. Distance pointers never point before the beginning of the output
  20. stream.
  21. 2. Distance pointers can point back across blocks, up to 32k away.
  22. 3. There is an implied maximum of 7 bits for the bit length table and
  23. 15 bits for the actual data.
  24. 4. If only one code exists, then it is encoded using one bit. (Zero
  25. would be more efficient, but perhaps a little confusing.) If two
  26. codes exist, they are coded using one bit each (0 and 1).
  27. 5. There is no way of sending zero distance codes--a dummy must be
  28. sent if there are none. (History: a pre 2.0 version of PKZIP would
  29. store blocks with no distance codes, but this was discovered to be
  30. too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  31. zero distance codes, which is sent as one code of zero bits in
  32. length.
  33. 6. There are up to 286 literal/length codes. Code 256 represents the
  34. end-of-block. Note however that the static length tree defines
  35. 288 codes just to fill out the Huffman codes. Codes 286 and 287
  36. cannot be used though, since there is no length base or extra bits
  37. defined for them. Similarily, there are up to 30 distance codes.
  38. However, static trees define 32 codes (all 5 bits) to fill out the
  39. Huffman codes, but the last two had better not show up in the data.
  40. 7. Unzip can check dynamic Huffman blocks for complete code sets.
  41. The exception is that a single code would not be complete (see #4).
  42. 8. The five bits following the block type is really the number of
  43. literal codes sent minus 257.
  44. 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  45. (1+6+6). Therefore, to output three times the length, you output
  46. three codes (1+1+1), whereas to output four times the same length,
  47. you only need two codes (1+3). Hmm.
  48. 10. In the tree reconstruction algorithm, Code = Code + Increment
  49. only if BitLength(i) is not zero. (Pretty obvious.)
  50. 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  51. 12. Note: length code 284 can represent 227-258, but length code 285
  52. really is 258. The last length deserves its own, short code
  53. since it gets used a lot in very redundant files. The length
  54. 258 is special since 258 - 3 (the min match length) is 255.
  55. 13. The literal/length and distance code bit lengths are read as a
  56. single stream of lengths. It is possible (and advantageous) for
  57. a repeat code (16, 17, or 18) to go across the boundary between
  58. the two sets of lengths.
  59. */
  60. void inflate_blocks_reset(s, z, c)
  61. inflate_blocks_statef *s;
  62. z_streamp z;
  63. uLongf *c;
  64. {
  65. if (c != Z_NULL)
  66. *c = s->check;
  67. if (s->mode == BTREE || s->mode == DTREE)
  68. ZFREE(z, s->sub.trees.blens);
  69. if (s->mode == CODES)
  70. inflate_codes_free(s->sub.decode.codes, z);
  71. s->mode = TYPE;
  72. s->bitk = 0;
  73. s->bitb = 0;
  74. s->read = s->write = s->window;
  75. if (s->checkfn != Z_NULL)
  76. z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
  77. Tracev((stderr, "inflate: blocks reset\n"));
  78. }
  79. inflate_blocks_statef *inflate_blocks_new(z, c, w)
  80. z_streamp z;
  81. check_func c;
  82. uInt w;
  83. {
  84. inflate_blocks_statef *s;
  85. if ((s = (inflate_blocks_statef *)ZALLOC
  86. (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  87. return s;
  88. if ((s->hufts =
  89. (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  90. {
  91. ZFREE(z, s);
  92. return Z_NULL;
  93. }
  94. if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
  95. {
  96. ZFREE(z, s->hufts);
  97. ZFREE(z, s);
  98. return Z_NULL;
  99. }
  100. s->end = s->window + w;
  101. s->checkfn = c;
  102. s->mode = TYPE;
  103. Tracev((stderr, "inflate: blocks allocated\n"));
  104. inflate_blocks_reset(s, z, Z_NULL);
  105. return s;
  106. }
  107. int inflate_blocks(s, z, r)
  108. inflate_blocks_statef *s;
  109. z_streamp z;
  110. int r;
  111. {
  112. uInt t; /* temporary storage */
  113. uLong b; /* bit buffer */
  114. uInt k; /* bits in bit buffer */
  115. Bytef *p; /* input data pointer */
  116. uInt n; /* bytes available there */
  117. Bytef *q; /* output window write pointer */
  118. uInt m; /* bytes to end of window or read pointer */
  119. /* copy input/output information to locals (UPDATE macro restores) */
  120. LOAD
  121. /* process input based on current state */
  122. while (1) switch (s->mode)
  123. {
  124. case TYPE:
  125. NEEDBITS(3)
  126. t = (uInt)b & 7;
  127. s->last = t & 1;
  128. switch (t >> 1)
  129. {
  130. case 0: /* stored */
  131. Tracev((stderr, "inflate: stored block%s\n",
  132. s->last ? " (last)" : ""));
  133. DUMPBITS(3)
  134. t = k & 7; /* go to byte boundary */
  135. DUMPBITS(t)
  136. s->mode = LENS; /* get length of stored block */
  137. break;
  138. case 1: /* fixed */
  139. Tracev((stderr, "inflate: fixed codes block%s\n",
  140. s->last ? " (last)" : ""));
  141. {
  142. uInt bl, bd;
  143. inflate_huft *tl, *td;
  144. inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  145. s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  146. if (s->sub.decode.codes == Z_NULL)
  147. {
  148. r = Z_MEM_ERROR;
  149. LEAVE
  150. }
  151. }
  152. DUMPBITS(3)
  153. s->mode = CODES;
  154. break;
  155. case 2: /* dynamic */
  156. Tracev((stderr, "inflate: dynamic codes block%s\n",
  157. s->last ? " (last)" : ""));
  158. DUMPBITS(3)
  159. s->mode = TABLE;
  160. break;
  161. case 3: /* illegal */
  162. DUMPBITS(3)
  163. s->mode = BAD;
  164. z->msg = (char*)"invalid block type";
  165. r = Z_DATA_ERROR;
  166. LEAVE
  167. }
  168. break;
  169. case LENS:
  170. NEEDBITS(32)
  171. if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  172. {
  173. s->mode = BAD;
  174. z->msg = (char*)"invalid stored block lengths";
  175. r = Z_DATA_ERROR;
  176. LEAVE
  177. }
  178. s->sub.left = (uInt)b & 0xffff;
  179. b = k = 0; /* dump bits */
  180. Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  181. s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
  182. break;
  183. case STORED:
  184. if (n == 0)
  185. LEAVE
  186. NEEDOUT
  187. t = s->sub.left;
  188. if (t > n) t = n;
  189. if (t > m) t = m;
  190. zmemcpy(q, p, t);
  191. p += t; n -= t;
  192. q += t; m -= t;
  193. if ((s->sub.left -= t) != 0)
  194. break;
  195. Tracev((stderr, "inflate: stored end, %lu total out\n",
  196. z->total_out + (q >= s->read ? q - s->read :
  197. (s->end - s->read) + (q - s->window))));
  198. s->mode = s->last ? DRY : TYPE;
  199. break;
  200. case TABLE:
  201. NEEDBITS(14)
  202. s->sub.trees.table = t = (uInt)b & 0x3fff;
  203. #ifndef PKZIP_BUG_WORKAROUND
  204. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  205. {
  206. s->mode = BAD;
  207. z->msg = (char*)"too many length or distance symbols";
  208. r = Z_DATA_ERROR;
  209. LEAVE
  210. }
  211. #endif
  212. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  213. if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  214. {
  215. r = Z_MEM_ERROR;
  216. LEAVE
  217. }
  218. DUMPBITS(14)
  219. s->sub.trees.index = 0;
  220. Tracev((stderr, "inflate: table sizes ok\n"));
  221. s->mode = BTREE;
  222. case BTREE:
  223. while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  224. {
  225. NEEDBITS(3)
  226. s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  227. DUMPBITS(3)
  228. }
  229. while (s->sub.trees.index < 19)
  230. s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  231. s->sub.trees.bb = 7;
  232. t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  233. &s->sub.trees.tb, s->hufts, z);
  234. if (t != Z_OK)
  235. {
  236. r = t;
  237. if (r == Z_DATA_ERROR)
  238. {
  239. ZFREE(z, s->sub.trees.blens);
  240. s->mode = BAD;
  241. }
  242. LEAVE
  243. }
  244. s->sub.trees.index = 0;
  245. Tracev((stderr, "inflate: bits tree ok\n"));
  246. s->mode = DTREE;
  247. case DTREE:
  248. while (t = s->sub.trees.table,
  249. s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  250. {
  251. inflate_huft *h;
  252. uInt i, j, c;
  253. t = s->sub.trees.bb;
  254. NEEDBITS(t)
  255. h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  256. t = h->bits;
  257. c = h->base;
  258. if (c < 16)
  259. {
  260. DUMPBITS(t)
  261. s->sub.trees.blens[s->sub.trees.index++] = c;
  262. }
  263. else /* c == 16..18 */
  264. {
  265. i = c == 18 ? 7 : c - 14;
  266. j = c == 18 ? 11 : 3;
  267. NEEDBITS(t + i)
  268. DUMPBITS(t)
  269. j += (uInt)b & inflate_mask[i];
  270. DUMPBITS(i)
  271. i = s->sub.trees.index;
  272. t = s->sub.trees.table;
  273. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  274. (c == 16 && i < 1))
  275. {
  276. ZFREE(z, s->sub.trees.blens);
  277. s->mode = BAD;
  278. z->msg = (char*)"invalid bit length repeat";
  279. r = Z_DATA_ERROR;
  280. LEAVE
  281. }
  282. c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  283. do {
  284. s->sub.trees.blens[i++] = c;
  285. } while (--j);
  286. s->sub.trees.index = i;
  287. }
  288. }
  289. s->sub.trees.tb = Z_NULL;
  290. {
  291. uInt bl, bd;
  292. inflate_huft *tl, *td;
  293. inflate_codes_statef *c;
  294. bl = 9; /* must be <= 9 for lookahead assumptions */
  295. bd = 6; /* must be <= 9 for lookahead assumptions */
  296. t = s->sub.trees.table;
  297. t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  298. s->sub.trees.blens, &bl, &bd, &tl, &td,
  299. s->hufts, z);
  300. if (t != Z_OK)
  301. {
  302. if (t == (uInt)Z_DATA_ERROR)
  303. {
  304. ZFREE(z, s->sub.trees.blens);
  305. s->mode = BAD;
  306. }
  307. r = t;
  308. LEAVE
  309. }
  310. Tracev((stderr, "inflate: trees ok\n"));
  311. if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  312. {
  313. r = Z_MEM_ERROR;
  314. LEAVE
  315. }
  316. s->sub.decode.codes = c;
  317. }
  318. ZFREE(z, s->sub.trees.blens);
  319. s->mode = CODES;
  320. case CODES:
  321. UPDATE
  322. if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  323. return inflate_flush(s, z, r);
  324. r = Z_OK;
  325. inflate_codes_free(s->sub.decode.codes, z);
  326. LOAD
  327. Tracev((stderr, "inflate: codes end, %lu total out\n",
  328. z->total_out + (q >= s->read ? q - s->read :
  329. (s->end - s->read) + (q - s->window))));
  330. if (!s->last)
  331. {
  332. s->mode = TYPE;
  333. break;
  334. }
  335. s->mode = DRY;
  336. case DRY:
  337. FLUSH
  338. if (s->read != s->write)
  339. LEAVE
  340. s->mode = DONE;
  341. case DONE:
  342. r = Z_STREAM_END;
  343. LEAVE
  344. case BAD:
  345. r = Z_DATA_ERROR;
  346. LEAVE
  347. default:
  348. r = Z_STREAM_ERROR;
  349. LEAVE
  350. }
  351. }
  352. int inflate_blocks_free(s, z)
  353. inflate_blocks_statef *s;
  354. z_streamp z;
  355. {
  356. inflate_blocks_reset(s, z, Z_NULL);
  357. ZFREE(z, s->window);
  358. ZFREE(z, s->hufts);
  359. ZFREE(z, s);
  360. Tracev((stderr, "inflate: blocks freed\n"));
  361. return Z_OK;
  362. }
  363. void inflate_set_dictionary(s, d, n)
  364. inflate_blocks_statef *s;
  365. const Bytef *d;
  366. uInt n;
  367. {
  368. zmemcpy(s->window, d, n);
  369. s->read = s->write = s->window + n;
  370. }
  371. /* Returns true if inflate is currently at the end of a block generated
  372. * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  373. * IN assertion: s != Z_NULL
  374. */
  375. int inflate_blocks_sync_point(s)
  376. inflate_blocks_statef *s;
  377. {
  378. return s->mode == LENS;
  379. }