infblock.c 12 KB

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