infback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* infback.c -- inflate using a call-back interface
  2. * Copyright (C) 1995-2016 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. This code is largely copied from inflate.c. Normally either infback.o or
  7. inflate.o would be linked into an application--not both. The interface
  8. with inffast.c is retained so that optimized assembler-coded versions of
  9. inflate_fast() can be used with either inflate.c or infback.c.
  10. */
  11. #include "zutil.h"
  12. #include "inftrees.h"
  13. #include "inflate.h"
  14. #include "inffast.h"
  15. /* function prototypes */
  16. local void fixedtables OF((struct inflate_state FAR *state));
  17. /*
  18. strm provides memory allocation functions in zalloc and zfree, or
  19. Z_NULL to use the library memory allocation functions.
  20. windowBits is in the range 8..15, and window is a user-supplied
  21. window and output buffer that is 2**windowBits bytes.
  22. */
  23. int ZEXPORT inflateBackInit_(
  24. z_streamp strm,
  25. int windowBits,
  26. unsigned char FAR *window,
  27. const char *version,
  28. int stream_size)
  29. {
  30. struct inflate_state FAR *state;
  31. if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  32. stream_size != (int)(sizeof(z_stream)))
  33. return Z_VERSION_ERROR;
  34. if (strm == Z_NULL || window == Z_NULL ||
  35. windowBits < 8 || windowBits > 15)
  36. return Z_STREAM_ERROR;
  37. strm->msg = Z_NULL; /* in case we return an error */
  38. if (strm->zalloc == (alloc_func)0) {
  39. #ifdef Z_SOLO
  40. return Z_STREAM_ERROR;
  41. #else
  42. strm->zalloc = zcalloc;
  43. strm->opaque = (voidpf)0;
  44. #endif
  45. }
  46. if (strm->zfree == (free_func)0)
  47. #ifdef Z_SOLO
  48. return Z_STREAM_ERROR;
  49. #else
  50. strm->zfree = zcfree;
  51. #endif
  52. state = (struct inflate_state FAR *)ZALLOC(strm, 1,
  53. sizeof(struct inflate_state));
  54. if (state == Z_NULL) return Z_MEM_ERROR;
  55. Tracev((stderr, "inflate: allocated\n"));
  56. strm->state = (struct internal_state FAR *)state;
  57. state->dmax = 32768U;
  58. state->wbits = (uInt)windowBits;
  59. state->wsize = 1U << windowBits;
  60. state->window = window;
  61. state->wnext = 0;
  62. state->whave = 0;
  63. return Z_OK;
  64. }
  65. /*
  66. Return state with length and distance decoding tables and index sizes set to
  67. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  68. If BUILDFIXED is defined, then instead this routine builds the tables the
  69. first time it's called, and returns those tables the first time and
  70. thereafter. This reduces the size of the code by about 2K bytes, in
  71. exchange for a little execution time. However, BUILDFIXED should not be
  72. used for threaded applications, since the rewriting of the tables and virgin
  73. may not be thread-safe.
  74. */
  75. local void fixedtables(
  76. struct inflate_state FAR *state)
  77. {
  78. #ifdef BUILDFIXED
  79. static int virgin = 1;
  80. static code *lenfix, *distfix;
  81. static code fixed[544];
  82. /* build fixed huffman tables if first call (may not be thread safe) */
  83. if (virgin) {
  84. unsigned sym, bits;
  85. static code *next;
  86. /* literal/length table */
  87. sym = 0;
  88. while (sym < 144) state->lens[sym++] = 8;
  89. while (sym < 256) state->lens[sym++] = 9;
  90. while (sym < 280) state->lens[sym++] = 7;
  91. while (sym < 288) state->lens[sym++] = 8;
  92. next = fixed;
  93. lenfix = next;
  94. bits = 9;
  95. inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
  96. /* distance table */
  97. sym = 0;
  98. while (sym < 32) state->lens[sym++] = 5;
  99. distfix = next;
  100. bits = 5;
  101. inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
  102. /* do this just once */
  103. virgin = 0;
  104. }
  105. #else /* !BUILDFIXED */
  106. # include "inffixed.h"
  107. #endif /* BUILDFIXED */
  108. state->lencode = lenfix;
  109. state->lenbits = 9;
  110. state->distcode = distfix;
  111. state->distbits = 5;
  112. }
  113. /* Macros for inflateBack(): */
  114. /* Load returned state from inflate_fast() */
  115. #define LOAD() \
  116. do { \
  117. put = strm->next_out; \
  118. left = strm->avail_out; \
  119. next = strm->next_in; \
  120. have = strm->avail_in; \
  121. hold = state->hold; \
  122. bits = state->bits; \
  123. } while (0)
  124. /* Set state from registers for inflate_fast() */
  125. #define RESTORE() \
  126. do { \
  127. strm->next_out = put; \
  128. strm->avail_out = left; \
  129. strm->next_in = next; \
  130. strm->avail_in = have; \
  131. state->hold = hold; \
  132. state->bits = bits; \
  133. } while (0)
  134. /* Clear the input bit accumulator */
  135. #define INITBITS() \
  136. do { \
  137. hold = 0; \
  138. bits = 0; \
  139. } while (0)
  140. /* Assure that some input is available. If input is requested, but denied,
  141. then return a Z_BUF_ERROR from inflateBack(). */
  142. #define PULL() \
  143. do { \
  144. if (have == 0) { \
  145. have = in(in_desc, &next); \
  146. if (have == 0) { \
  147. next = Z_NULL; \
  148. ret = Z_BUF_ERROR; \
  149. goto inf_leave; \
  150. } \
  151. } \
  152. } while (0)
  153. /* Get a byte of input into the bit accumulator, or return from inflateBack()
  154. with an error if there is no input available. */
  155. #define PULLBYTE() \
  156. do { \
  157. PULL(); \
  158. have--; \
  159. hold += (unsigned long)(*next++) << bits; \
  160. bits += 8; \
  161. } while (0)
  162. /* Assure that there are at least n bits in the bit accumulator. If there is
  163. not enough available input to do that, then return from inflateBack() with
  164. an error. */
  165. #define NEEDBITS(n) \
  166. do { \
  167. while (bits < (unsigned)(n)) \
  168. PULLBYTE(); \
  169. } while (0)
  170. /* Return the low n bits of the bit accumulator (n < 16) */
  171. #define BITS(n) \
  172. ((unsigned)hold & ((1U << (n)) - 1))
  173. /* Remove n bits from the bit accumulator */
  174. #define DROPBITS(n) \
  175. do { \
  176. hold >>= (n); \
  177. bits -= (unsigned)(n); \
  178. } while (0)
  179. /* Remove zero to seven bits as needed to go to a byte boundary */
  180. #define BYTEBITS() \
  181. do { \
  182. hold >>= bits & 7; \
  183. bits -= bits & 7; \
  184. } while (0)
  185. /* Assure that some output space is available, by writing out the window
  186. if it's full. If the write fails, return from inflateBack() with a
  187. Z_BUF_ERROR. */
  188. #define ROOM() \
  189. do { \
  190. if (left == 0) { \
  191. put = state->window; \
  192. left = state->wsize; \
  193. state->whave = left; \
  194. if (out(out_desc, put, left)) { \
  195. ret = Z_BUF_ERROR; \
  196. goto inf_leave; \
  197. } \
  198. } \
  199. } while (0)
  200. /*
  201. strm provides the memory allocation functions and window buffer on input,
  202. and provides information on the unused input on return. For Z_DATA_ERROR
  203. returns, strm will also provide an error message.
  204. in() and out() are the call-back input and output functions. When
  205. inflateBack() needs more input, it calls in(). When inflateBack() has
  206. filled the window with output, or when it completes with data in the
  207. window, it calls out() to write out the data. The application must not
  208. change the provided input until in() is called again or inflateBack()
  209. returns. The application must not change the window/output buffer until
  210. inflateBack() returns.
  211. in() and out() are called with a descriptor parameter provided in the
  212. inflateBack() call. This parameter can be a structure that provides the
  213. information required to do the read or write, as well as accumulated
  214. information on the input and output such as totals and check values.
  215. in() should return zero on failure. out() should return non-zero on
  216. failure. If either in() or out() fails, than inflateBack() returns a
  217. Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
  218. was in() or out() that caused in the error. Otherwise, inflateBack()
  219. returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
  220. error, or Z_MEM_ERROR if it could not allocate memory for the state.
  221. inflateBack() can also return Z_STREAM_ERROR if the input parameters
  222. are not correct, i.e. strm is Z_NULL or the state was not initialized.
  223. */
  224. int ZEXPORT inflateBack(
  225. z_streamp strm,
  226. in_func in,
  227. void FAR *in_desc,
  228. out_func out,
  229. void FAR *out_desc)
  230. {
  231. struct inflate_state FAR *state;
  232. z_const unsigned char FAR *next; /* next input */
  233. unsigned char FAR *put; /* next output */
  234. unsigned have, left; /* available input and output */
  235. unsigned long hold; /* bit buffer */
  236. unsigned bits; /* bits in bit buffer */
  237. unsigned copy; /* number of stored or match bytes to copy */
  238. unsigned char FAR *from; /* where to copy match bytes from */
  239. code here; /* current decoding table entry */
  240. code last; /* parent table entry */
  241. unsigned len; /* length to copy for repeats, bits to drop */
  242. int ret; /* return code */
  243. static const unsigned short order[19] = /* permutation of code lengths */
  244. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  245. /* Check that the strm exists and that the state was initialized */
  246. if (strm == Z_NULL || strm->state == Z_NULL)
  247. return Z_STREAM_ERROR;
  248. state = (struct inflate_state FAR *)strm->state;
  249. /* Reset the state */
  250. strm->msg = Z_NULL;
  251. state->mode = TYPE;
  252. state->last = 0;
  253. state->whave = 0;
  254. next = strm->next_in;
  255. have = next != Z_NULL ? strm->avail_in : 0;
  256. hold = 0;
  257. bits = 0;
  258. put = state->window;
  259. left = state->wsize;
  260. /* Inflate until end of block marked as last */
  261. for (;;)
  262. switch (state->mode) {
  263. case TYPE:
  264. /* determine and dispatch block type */
  265. if (state->last) {
  266. BYTEBITS();
  267. state->mode = DONE;
  268. break;
  269. }
  270. NEEDBITS(3);
  271. state->last = BITS(1);
  272. DROPBITS(1);
  273. switch (BITS(2)) {
  274. case 0: /* stored block */
  275. Tracev((stderr, "inflate: stored block%s\n",
  276. state->last ? " (last)" : ""));
  277. state->mode = STORED;
  278. break;
  279. case 1: /* fixed block */
  280. fixedtables(state);
  281. Tracev((stderr, "inflate: fixed codes block%s\n",
  282. state->last ? " (last)" : ""));
  283. state->mode = LEN; /* decode codes */
  284. break;
  285. case 2: /* dynamic block */
  286. Tracev((stderr, "inflate: dynamic codes block%s\n",
  287. state->last ? " (last)" : ""));
  288. state->mode = TABLE;
  289. break;
  290. case 3:
  291. strm->msg = (char *)"invalid block type";
  292. state->mode = BAD;
  293. }
  294. DROPBITS(2);
  295. break;
  296. case STORED:
  297. /* get and verify stored block length */
  298. BYTEBITS(); /* go to byte boundary */
  299. NEEDBITS(32);
  300. if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  301. strm->msg = (char *)"invalid stored block lengths";
  302. state->mode = BAD;
  303. break;
  304. }
  305. state->length = (unsigned)hold & 0xffff;
  306. Tracev((stderr, "inflate: stored length %u\n",
  307. state->length));
  308. INITBITS();
  309. /* copy stored block from input to output */
  310. while (state->length != 0) {
  311. copy = state->length;
  312. PULL();
  313. ROOM();
  314. if (copy > have) copy = have;
  315. if (copy > left) copy = left;
  316. zmemcpy(put, next, copy);
  317. have -= copy;
  318. next += copy;
  319. left -= copy;
  320. put += copy;
  321. state->length -= copy;
  322. }
  323. Tracev((stderr, "inflate: stored end\n"));
  324. state->mode = TYPE;
  325. break;
  326. case TABLE:
  327. /* get dynamic table entries descriptor */
  328. NEEDBITS(14);
  329. state->nlen = BITS(5) + 257;
  330. DROPBITS(5);
  331. state->ndist = BITS(5) + 1;
  332. DROPBITS(5);
  333. state->ncode = BITS(4) + 4;
  334. DROPBITS(4);
  335. #ifndef PKZIP_BUG_WORKAROUND
  336. if (state->nlen > 286 || state->ndist > 30) {
  337. strm->msg = (char *)"too many length or distance symbols";
  338. state->mode = BAD;
  339. break;
  340. }
  341. #endif
  342. Tracev((stderr, "inflate: table sizes ok\n"));
  343. /* get code length code lengths (not a typo) */
  344. state->have = 0;
  345. while (state->have < state->ncode) {
  346. NEEDBITS(3);
  347. state->lens[order[state->have++]] = (unsigned short)BITS(3);
  348. DROPBITS(3);
  349. }
  350. while (state->have < 19)
  351. state->lens[order[state->have++]] = 0;
  352. state->next = state->codes;
  353. state->lencode = (code const FAR *)(state->next);
  354. state->lenbits = 7;
  355. ret = inflate_table(CODES, state->lens, 19, &(state->next),
  356. &(state->lenbits), state->work);
  357. if (ret) {
  358. strm->msg = (char *)"invalid code lengths set";
  359. state->mode = BAD;
  360. break;
  361. }
  362. Tracev((stderr, "inflate: code lengths ok\n"));
  363. /* get length and distance code code lengths */
  364. state->have = 0;
  365. while (state->have < state->nlen + state->ndist) {
  366. for (;;) {
  367. here = state->lencode[BITS(state->lenbits)];
  368. if ((unsigned)(here.bits) <= bits) break;
  369. PULLBYTE();
  370. }
  371. if (here.val < 16) {
  372. DROPBITS(here.bits);
  373. state->lens[state->have++] = here.val;
  374. }
  375. else {
  376. if (here.val == 16) {
  377. NEEDBITS(here.bits + 2);
  378. DROPBITS(here.bits);
  379. if (state->have == 0) {
  380. strm->msg = (char *)"invalid bit length repeat";
  381. state->mode = BAD;
  382. break;
  383. }
  384. len = (unsigned)(state->lens[state->have - 1]);
  385. copy = 3 + BITS(2);
  386. DROPBITS(2);
  387. }
  388. else if (here.val == 17) {
  389. NEEDBITS(here.bits + 3);
  390. DROPBITS(here.bits);
  391. len = 0;
  392. copy = 3 + BITS(3);
  393. DROPBITS(3);
  394. }
  395. else {
  396. NEEDBITS(here.bits + 7);
  397. DROPBITS(here.bits);
  398. len = 0;
  399. copy = 11 + BITS(7);
  400. DROPBITS(7);
  401. }
  402. if (state->have + copy > state->nlen + state->ndist) {
  403. strm->msg = (char *)"invalid bit length repeat";
  404. state->mode = BAD;
  405. break;
  406. }
  407. while (copy--)
  408. state->lens[state->have++] = (unsigned short)len;
  409. }
  410. }
  411. /* handle error breaks in while */
  412. if (state->mode == BAD) break;
  413. /* check for end-of-block code (better have one) */
  414. if (state->lens[256] == 0) {
  415. strm->msg = (char *)"invalid code -- missing end-of-block";
  416. state->mode = BAD;
  417. break;
  418. }
  419. /* build code tables -- note: do not change the lenbits or distbits
  420. values here (9 and 6) without reading the comments in inftrees.h
  421. concerning the ENOUGH constants, which depend on those values */
  422. state->next = state->codes;
  423. state->lencode = (code const FAR *)(state->next);
  424. state->lenbits = 9;
  425. ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
  426. &(state->lenbits), state->work);
  427. if (ret) {
  428. strm->msg = (char *)"invalid literal/lengths set";
  429. state->mode = BAD;
  430. break;
  431. }
  432. state->distcode = (code const FAR *)(state->next);
  433. state->distbits = 6;
  434. ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  435. &(state->next), &(state->distbits), state->work);
  436. if (ret) {
  437. strm->msg = (char *)"invalid distances set";
  438. state->mode = BAD;
  439. break;
  440. }
  441. Tracev((stderr, "inflate: codes ok\n"));
  442. state->mode = LEN;
  443. case LEN:
  444. /* use inflate_fast() if we have enough input and output */
  445. if (have >= 6 && left >= 258) {
  446. RESTORE();
  447. if (state->whave < state->wsize)
  448. state->whave = state->wsize - left;
  449. inflate_fast(strm, state->wsize);
  450. LOAD();
  451. break;
  452. }
  453. /* get a literal, length, or end-of-block code */
  454. for (;;) {
  455. here = state->lencode[BITS(state->lenbits)];
  456. if ((unsigned)(here.bits) <= bits) break;
  457. PULLBYTE();
  458. }
  459. if (here.op && (here.op & 0xf0) == 0) {
  460. last = here;
  461. for (;;) {
  462. here = state->lencode[last.val +
  463. (BITS(last.bits + last.op) >> last.bits)];
  464. if ((unsigned)(last.bits + here.bits) <= bits) break;
  465. PULLBYTE();
  466. }
  467. DROPBITS(last.bits);
  468. }
  469. DROPBITS(here.bits);
  470. state->length = (unsigned)here.val;
  471. /* process literal */
  472. if (here.op == 0) {
  473. Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  474. "inflate: literal '%c'\n" :
  475. "inflate: literal 0x%02x\n", here.val));
  476. ROOM();
  477. *put++ = (unsigned char)(state->length);
  478. left--;
  479. state->mode = LEN;
  480. break;
  481. }
  482. /* process end of block */
  483. if (here.op & 32) {
  484. Tracevv((stderr, "inflate: end of block\n"));
  485. state->mode = TYPE;
  486. break;
  487. }
  488. /* invalid code */
  489. if (here.op & 64) {
  490. strm->msg = (char *)"invalid literal/length code";
  491. state->mode = BAD;
  492. break;
  493. }
  494. /* length code -- get extra bits, if any */
  495. state->extra = (unsigned)(here.op) & 15;
  496. if (state->extra != 0) {
  497. NEEDBITS(state->extra);
  498. state->length += BITS(state->extra);
  499. DROPBITS(state->extra);
  500. }
  501. Tracevv((stderr, "inflate: length %u\n", state->length));
  502. /* get distance code */
  503. for (;;) {
  504. here = state->distcode[BITS(state->distbits)];
  505. if ((unsigned)(here.bits) <= bits) break;
  506. PULLBYTE();
  507. }
  508. if ((here.op & 0xf0) == 0) {
  509. last = here;
  510. for (;;) {
  511. here = state->distcode[last.val +
  512. (BITS(last.bits + last.op) >> last.bits)];
  513. if ((unsigned)(last.bits + here.bits) <= bits) break;
  514. PULLBYTE();
  515. }
  516. DROPBITS(last.bits);
  517. }
  518. DROPBITS(here.bits);
  519. if (here.op & 64) {
  520. strm->msg = (char *)"invalid distance code";
  521. state->mode = BAD;
  522. break;
  523. }
  524. state->offset = (unsigned)here.val;
  525. /* get distance extra bits, if any */
  526. state->extra = (unsigned)(here.op) & 15;
  527. if (state->extra != 0) {
  528. NEEDBITS(state->extra);
  529. state->offset += BITS(state->extra);
  530. DROPBITS(state->extra);
  531. }
  532. if (state->offset > state->wsize - (state->whave < state->wsize ?
  533. left : 0)) {
  534. strm->msg = (char *)"invalid distance too far back";
  535. state->mode = BAD;
  536. break;
  537. }
  538. Tracevv((stderr, "inflate: distance %u\n", state->offset));
  539. /* copy match from window to output */
  540. do {
  541. ROOM();
  542. copy = state->wsize - state->offset;
  543. if (copy < left) {
  544. from = put + copy;
  545. copy = left - copy;
  546. }
  547. else {
  548. from = put - state->offset;
  549. copy = left;
  550. }
  551. if (copy > state->length) copy = state->length;
  552. state->length -= copy;
  553. left -= copy;
  554. do {
  555. *put++ = *from++;
  556. } while (--copy);
  557. } while (state->length != 0);
  558. break;
  559. case DONE:
  560. /* inflate stream terminated properly -- write leftover output */
  561. ret = Z_STREAM_END;
  562. if (left < state->wsize) {
  563. if (out(out_desc, state->window, state->wsize - left))
  564. ret = Z_BUF_ERROR;
  565. }
  566. goto inf_leave;
  567. case BAD:
  568. ret = Z_DATA_ERROR;
  569. goto inf_leave;
  570. default: /* can't happen, but makes compilers happy */
  571. ret = Z_STREAM_ERROR;
  572. goto inf_leave;
  573. }
  574. /* Return unused input */
  575. inf_leave:
  576. strm->next_in = next;
  577. strm->avail_in = have;
  578. return ret;
  579. }
  580. int ZEXPORT inflateBackEnd(
  581. z_streamp strm)
  582. {
  583. if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
  584. return Z_STREAM_ERROR;
  585. ZFREE(strm, strm->state);
  586. strm->state = Z_NULL;
  587. Tracev((stderr, "inflate: end\n"));
  588. return Z_OK;
  589. }