PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/zlib/inflate.c

https://bitbucket.org/cabalistic/ogredeps/
C | 1512 lines | 1160 code | 79 blank | 273 comment | 385 complexity | 46cb3a81ac3fa57789eba65cfecf754d MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /* inflate.c -- zlib decompression
  2. * Copyright (C) 1995-2012 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. * Change history:
  7. *
  8. * 1.2.beta0 24 Nov 2002
  9. * - First version -- complete rewrite of inflate to simplify code, avoid
  10. * creation of window when not needed, minimize use of window when it is
  11. * needed, make inffast.c even faster, implement gzip decoding, and to
  12. * improve code readability and style over the previous zlib inflate code
  13. *
  14. * 1.2.beta1 25 Nov 2002
  15. * - Use pointers for available input and output checking in inffast.c
  16. * - Remove input and output counters in inffast.c
  17. * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
  18. * - Remove unnecessary second byte pull from length extra in inffast.c
  19. * - Unroll direct copy to three copies per loop in inffast.c
  20. *
  21. * 1.2.beta2 4 Dec 2002
  22. * - Change external routine names to reduce potential conflicts
  23. * - Correct filename to inffixed.h for fixed tables in inflate.c
  24. * - Make hbuf[] unsigned char to match parameter type in inflate.c
  25. * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
  26. * to avoid negation problem on Alphas (64 bit) in inflate.c
  27. *
  28. * 1.2.beta3 22 Dec 2002
  29. * - Add comments on state->bits assertion in inffast.c
  30. * - Add comments on op field in inftrees.h
  31. * - Fix bug in reuse of allocated window after inflateReset()
  32. * - Remove bit fields--back to byte structure for speed
  33. * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
  34. * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
  35. * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
  36. * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
  37. * - Use local copies of stream next and avail values, as well as local bit
  38. * buffer and bit count in inflate()--for speed when inflate_fast() not used
  39. *
  40. * 1.2.beta4 1 Jan 2003
  41. * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
  42. * - Move a comment on output buffer sizes from inffast.c to inflate.c
  43. * - Add comments in inffast.c to introduce the inflate_fast() routine
  44. * - Rearrange window copies in inflate_fast() for speed and simplification
  45. * - Unroll last copy for window match in inflate_fast()
  46. * - Use local copies of window variables in inflate_fast() for speed
  47. * - Pull out common wnext == 0 case for speed in inflate_fast()
  48. * - Make op and len in inflate_fast() unsigned for consistency
  49. * - Add FAR to lcode and dcode declarations in inflate_fast()
  50. * - Simplified bad distance check in inflate_fast()
  51. * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
  52. * source file infback.c to provide a call-back interface to inflate for
  53. * programs like gzip and unzip -- uses window as output buffer to avoid
  54. * window copying
  55. *
  56. * 1.2.beta5 1 Jan 2003
  57. * - Improved inflateBack() interface to allow the caller to provide initial
  58. * input in strm.
  59. * - Fixed stored blocks bug in inflateBack()
  60. *
  61. * 1.2.beta6 4 Jan 2003
  62. * - Added comments in inffast.c on effectiveness of POSTINC
  63. * - Typecasting all around to reduce compiler warnings
  64. * - Changed loops from while (1) or do {} while (1) to for (;;), again to
  65. * make compilers happy
  66. * - Changed type of window in inflateBackInit() to unsigned char *
  67. *
  68. * 1.2.beta7 27 Jan 2003
  69. * - Changed many types to unsigned or unsigned short to avoid warnings
  70. * - Added inflateCopy() function
  71. *
  72. * 1.2.0 9 Mar 2003
  73. * - Changed inflateBack() interface to provide separate opaque descriptors
  74. * for the in() and out() functions
  75. * - Changed inflateBack() argument and in_func typedef to swap the length
  76. * and buffer address return values for the input function
  77. * - Check next_in and next_out for Z_NULL on entry to inflate()
  78. *
  79. * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
  80. */
  81. #include "zutil.h"
  82. #include "inftrees.h"
  83. #include "inflate.h"
  84. #include "inffast.h"
  85. #ifdef MAKEFIXED
  86. # ifndef BUILDFIXED
  87. # define BUILDFIXED
  88. # endif
  89. #endif
  90. /* function prototypes */
  91. local void fixedtables OF((struct inflate_state FAR *state));
  92. local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
  93. unsigned copy));
  94. #ifdef BUILDFIXED
  95. void makefixed OF((void));
  96. #endif
  97. local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
  98. unsigned len));
  99. int ZEXPORT inflateResetKeep(strm)
  100. z_streamp strm;
  101. {
  102. struct inflate_state FAR *state;
  103. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  104. state = (struct inflate_state FAR *)strm->state;
  105. strm->total_in = strm->total_out = state->total = 0;
  106. strm->msg = Z_NULL;
  107. if (state->wrap) /* to support ill-conceived Java test suite */
  108. strm->adler = state->wrap & 1;
  109. state->mode = HEAD;
  110. state->last = 0;
  111. state->havedict = 0;
  112. state->dmax = 32768U;
  113. state->head = Z_NULL;
  114. state->hold = 0;
  115. state->bits = 0;
  116. state->lencode = state->distcode = state->next = state->codes;
  117. state->sane = 1;
  118. state->back = -1;
  119. Tracev((stderr, "inflate: reset\n"));
  120. return Z_OK;
  121. }
  122. int ZEXPORT inflateReset(strm)
  123. z_streamp strm;
  124. {
  125. struct inflate_state FAR *state;
  126. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  127. state = (struct inflate_state FAR *)strm->state;
  128. state->wsize = 0;
  129. state->whave = 0;
  130. state->wnext = 0;
  131. return inflateResetKeep(strm);
  132. }
  133. int ZEXPORT inflateReset2(strm, windowBits)
  134. z_streamp strm;
  135. int windowBits;
  136. {
  137. int wrap;
  138. struct inflate_state FAR *state;
  139. /* get the state */
  140. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  141. state = (struct inflate_state FAR *)strm->state;
  142. /* extract wrap request from windowBits parameter */
  143. if (windowBits < 0) {
  144. wrap = 0;
  145. windowBits = -windowBits;
  146. }
  147. else {
  148. wrap = (windowBits >> 4) + 1;
  149. #ifdef GUNZIP
  150. if (windowBits < 48)
  151. windowBits &= 15;
  152. #endif
  153. }
  154. /* set number of window bits, free window if different */
  155. if (windowBits && (windowBits < 8 || windowBits > 15))
  156. return Z_STREAM_ERROR;
  157. if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
  158. ZFREE(strm, state->window);
  159. state->window = Z_NULL;
  160. }
  161. /* update state and reset the rest of it */
  162. state->wrap = wrap;
  163. state->wbits = (unsigned)windowBits;
  164. return inflateReset(strm);
  165. }
  166. int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
  167. z_streamp strm;
  168. int windowBits;
  169. const char *version;
  170. int stream_size;
  171. {
  172. int ret;
  173. struct inflate_state FAR *state;
  174. if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  175. stream_size != (int)(sizeof(z_stream)))
  176. return Z_VERSION_ERROR;
  177. if (strm == Z_NULL) return Z_STREAM_ERROR;
  178. strm->msg = Z_NULL; /* in case we return an error */
  179. if (strm->zalloc == (alloc_func)0) {
  180. #ifdef Z_SOLO
  181. return Z_STREAM_ERROR;
  182. #else
  183. strm->zalloc = zcalloc;
  184. strm->opaque = (voidpf)0;
  185. #endif
  186. }
  187. if (strm->zfree == (free_func)0)
  188. #ifdef Z_SOLO
  189. return Z_STREAM_ERROR;
  190. #else
  191. strm->zfree = zcfree;
  192. #endif
  193. state = (struct inflate_state FAR *)
  194. ZALLOC(strm, 1, sizeof(struct inflate_state));
  195. if (state == Z_NULL) return Z_MEM_ERROR;
  196. Tracev((stderr, "inflate: allocated\n"));
  197. strm->state = (struct internal_state FAR *)state;
  198. state->window = Z_NULL;
  199. ret = inflateReset2(strm, windowBits);
  200. if (ret != Z_OK) {
  201. ZFREE(strm, state);
  202. strm->state = Z_NULL;
  203. }
  204. return ret;
  205. }
  206. int ZEXPORT inflateInit_(strm, version, stream_size)
  207. z_streamp strm;
  208. const char *version;
  209. int stream_size;
  210. {
  211. return inflateInit2_(strm, DEF_WBITS, version, stream_size);
  212. }
  213. int ZEXPORT inflatePrime(strm, bits, value)
  214. z_streamp strm;
  215. int bits;
  216. int value;
  217. {
  218. struct inflate_state FAR *state;
  219. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  220. state = (struct inflate_state FAR *)strm->state;
  221. if (bits < 0) {
  222. state->hold = 0;
  223. state->bits = 0;
  224. return Z_OK;
  225. }
  226. if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
  227. value &= (1L << bits) - 1;
  228. state->hold += value << state->bits;
  229. state->bits += bits;
  230. return Z_OK;
  231. }
  232. /*
  233. Return state with length and distance decoding tables and index sizes set to
  234. fixed code decoding. Normally this returns fixed tables from inffixed.h.
  235. If BUILDFIXED is defined, then instead this routine builds the tables the
  236. first time it's called, and returns those tables the first time and
  237. thereafter. This reduces the size of the code by about 2K bytes, in
  238. exchange for a little execution time. However, BUILDFIXED should not be
  239. used for threaded applications, since the rewriting of the tables and virgin
  240. may not be thread-safe.
  241. */
  242. local void fixedtables(state)
  243. struct inflate_state FAR *state;
  244. {
  245. #ifdef BUILDFIXED
  246. static int virgin = 1;
  247. static code *lenfix, *distfix;
  248. static code fixed[544];
  249. /* build fixed huffman tables if first call (may not be thread safe) */
  250. if (virgin) {
  251. unsigned sym, bits;
  252. static code *next;
  253. /* literal/length table */
  254. sym = 0;
  255. while (sym < 144) state->lens[sym++] = 8;
  256. while (sym < 256) state->lens[sym++] = 9;
  257. while (sym < 280) state->lens[sym++] = 7;
  258. while (sym < 288) state->lens[sym++] = 8;
  259. next = fixed;
  260. lenfix = next;
  261. bits = 9;
  262. inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
  263. /* distance table */
  264. sym = 0;
  265. while (sym < 32) state->lens[sym++] = 5;
  266. distfix = next;
  267. bits = 5;
  268. inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
  269. /* do this just once */
  270. virgin = 0;
  271. }
  272. #else /* !BUILDFIXED */
  273. # include "inffixed.h"
  274. #endif /* BUILDFIXED */
  275. state->lencode = lenfix;
  276. state->lenbits = 9;
  277. state->distcode = distfix;
  278. state->distbits = 5;
  279. }
  280. #ifdef MAKEFIXED
  281. #include <stdio.h>
  282. /*
  283. Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
  284. defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
  285. those tables to stdout, which would be piped to inffixed.h. A small program
  286. can simply call makefixed to do this:
  287. void makefixed(void);
  288. int main(void)
  289. {
  290. makefixed();
  291. return 0;
  292. }
  293. Then that can be linked with zlib built with MAKEFIXED defined and run:
  294. a.out > inffixed.h
  295. */
  296. void makefixed()
  297. {
  298. unsigned low, size;
  299. struct inflate_state state;
  300. fixedtables(&state);
  301. puts(" /* inffixed.h -- table for decoding fixed codes");
  302. puts(" * Generated automatically by makefixed().");
  303. puts(" */");
  304. puts("");
  305. puts(" /* WARNING: this file should *not* be used by applications.");
  306. puts(" It is part of the implementation of this library and is");
  307. puts(" subject to change. Applications should only use zlib.h.");
  308. puts(" */");
  309. puts("");
  310. size = 1U << 9;
  311. printf(" static const code lenfix[%u] = {", size);
  312. low = 0;
  313. for (;;) {
  314. if ((low % 7) == 0) printf("\n ");
  315. printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
  316. state.lencode[low].bits, state.lencode[low].val);
  317. if (++low == size) break;
  318. putchar(',');
  319. }
  320. puts("\n };");
  321. size = 1U << 5;
  322. printf("\n static const code distfix[%u] = {", size);
  323. low = 0;
  324. for (;;) {
  325. if ((low % 6) == 0) printf("\n ");
  326. printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
  327. state.distcode[low].val);
  328. if (++low == size) break;
  329. putchar(',');
  330. }
  331. puts("\n };");
  332. }
  333. #endif /* MAKEFIXED */
  334. /*
  335. Update the window with the last wsize (normally 32K) bytes written before
  336. returning. If window does not exist yet, create it. This is only called
  337. when a window is already in use, or when output has been written during this
  338. inflate call, but the end of the deflate stream has not been reached yet.
  339. It is also called to create a window for dictionary data when a dictionary
  340. is loaded.
  341. Providing output buffers larger than 32K to inflate() should provide a speed
  342. advantage, since only the last 32K of output is copied to the sliding window
  343. upon return from inflate(), and since all distances after the first 32K of
  344. output will fall in the output data, making match copies simpler and faster.
  345. The advantage may be dependent on the size of the processor's data caches.
  346. */
  347. local int updatewindow(strm, end, copy)
  348. z_streamp strm;
  349. const Bytef *end;
  350. unsigned copy;
  351. {
  352. struct inflate_state FAR *state;
  353. unsigned dist;
  354. state = (struct inflate_state FAR *)strm->state;
  355. /* if it hasn't been done already, allocate space for the window */
  356. if (state->window == Z_NULL) {
  357. state->window = (unsigned char FAR *)
  358. ZALLOC(strm, 1U << state->wbits,
  359. sizeof(unsigned char));
  360. if (state->window == Z_NULL) return 1;
  361. }
  362. /* if window not in use yet, initialize */
  363. if (state->wsize == 0) {
  364. state->wsize = 1U << state->wbits;
  365. state->wnext = 0;
  366. state->whave = 0;
  367. }
  368. /* copy state->wsize or less output bytes into the circular window */
  369. if (copy >= state->wsize) {
  370. zmemcpy(state->window, end - state->wsize, state->wsize);
  371. state->wnext = 0;
  372. state->whave = state->wsize;
  373. }
  374. else {
  375. dist = state->wsize - state->wnext;
  376. if (dist > copy) dist = copy;
  377. zmemcpy(state->window + state->wnext, end - copy, dist);
  378. copy -= dist;
  379. if (copy) {
  380. zmemcpy(state->window, end - copy, copy);
  381. state->wnext = copy;
  382. state->whave = state->wsize;
  383. }
  384. else {
  385. state->wnext += dist;
  386. if (state->wnext == state->wsize) state->wnext = 0;
  387. if (state->whave < state->wsize) state->whave += dist;
  388. }
  389. }
  390. return 0;
  391. }
  392. /* Macros for inflate(): */
  393. /* check function to use adler32() for zlib or crc32() for gzip */
  394. #ifdef GUNZIP
  395. # define UPDATE(check, buf, len) \
  396. (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
  397. #else
  398. # define UPDATE(check, buf, len) adler32(check, buf, len)
  399. #endif
  400. /* check macros for header crc */
  401. #ifdef GUNZIP
  402. # define CRC2(check, word) \
  403. do { \
  404. hbuf[0] = (unsigned char)(word); \
  405. hbuf[1] = (unsigned char)((word) >> 8); \
  406. check = crc32(check, hbuf, 2); \
  407. } while (0)
  408. # define CRC4(check, word) \
  409. do { \
  410. hbuf[0] = (unsigned char)(word); \
  411. hbuf[1] = (unsigned char)((word) >> 8); \
  412. hbuf[2] = (unsigned char)((word) >> 16); \
  413. hbuf[3] = (unsigned char)((word) >> 24); \
  414. check = crc32(check, hbuf, 4); \
  415. } while (0)
  416. #endif
  417. /* Load registers with state in inflate() for speed */
  418. #define LOAD() \
  419. do { \
  420. put = strm->next_out; \
  421. left = strm->avail_out; \
  422. next = strm->next_in; \
  423. have = strm->avail_in; \
  424. hold = state->hold; \
  425. bits = state->bits; \
  426. } while (0)
  427. /* Restore state from registers in inflate() */
  428. #define RESTORE() \
  429. do { \
  430. strm->next_out = put; \
  431. strm->avail_out = left; \
  432. strm->next_in = next; \
  433. strm->avail_in = have; \
  434. state->hold = hold; \
  435. state->bits = bits; \
  436. } while (0)
  437. /* Clear the input bit accumulator */
  438. #define INITBITS() \
  439. do { \
  440. hold = 0; \
  441. bits = 0; \
  442. } while (0)
  443. /* Get a byte of input into the bit accumulator, or return from inflate()
  444. if there is no input available. */
  445. #define PULLBYTE() \
  446. do { \
  447. if (have == 0) goto inf_leave; \
  448. have--; \
  449. hold += (unsigned long)(*next++) << bits; \
  450. bits += 8; \
  451. } while (0)
  452. /* Assure that there are at least n bits in the bit accumulator. If there is
  453. not enough available input to do that, then return from inflate(). */
  454. #define NEEDBITS(n) \
  455. do { \
  456. while (bits < (unsigned)(n)) \
  457. PULLBYTE(); \
  458. } while (0)
  459. /* Return the low n bits of the bit accumulator (n < 16) */
  460. #define BITS(n) \
  461. ((unsigned)hold & ((1U << (n)) - 1))
  462. /* Remove n bits from the bit accumulator */
  463. #define DROPBITS(n) \
  464. do { \
  465. hold >>= (n); \
  466. bits -= (unsigned)(n); \
  467. } while (0)
  468. /* Remove zero to seven bits as needed to go to a byte boundary */
  469. #define BYTEBITS() \
  470. do { \
  471. hold >>= bits & 7; \
  472. bits -= bits & 7; \
  473. } while (0)
  474. /*
  475. inflate() uses a state machine to process as much input data and generate as
  476. much output data as possible before returning. The state machine is
  477. structured roughly as follows:
  478. for (;;) switch (state) {
  479. ...
  480. case STATEn:
  481. if (not enough input data or output space to make progress)
  482. return;
  483. ... make progress ...
  484. state = STATEm;
  485. break;
  486. ...
  487. }
  488. so when inflate() is called again, the same case is attempted again, and
  489. if the appropriate resources are provided, the machine proceeds to the
  490. next state. The NEEDBITS() macro is usually the way the state evaluates
  491. whether it can proceed or should return. NEEDBITS() does the return if
  492. the requested bits are not available. The typical use of the BITS macros
  493. is:
  494. NEEDBITS(n);
  495. ... do something with BITS(n) ...
  496. DROPBITS(n);
  497. where NEEDBITS(n) either returns from inflate() if there isn't enough
  498. input left to load n bits into the accumulator, or it continues. BITS(n)
  499. gives the low n bits in the accumulator. When done, DROPBITS(n) drops
  500. the low n bits off the accumulator. INITBITS() clears the accumulator
  501. and sets the number of available bits to zero. BYTEBITS() discards just
  502. enough bits to put the accumulator on a byte boundary. After BYTEBITS()
  503. and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
  504. NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
  505. if there is no input available. The decoding of variable length codes uses
  506. PULLBYTE() directly in order to pull just enough bytes to decode the next
  507. code, and no more.
  508. Some states loop until they get enough input, making sure that enough
  509. state information is maintained to continue the loop where it left off
  510. if NEEDBITS() returns in the loop. For example, want, need, and keep
  511. would all have to actually be part of the saved state in case NEEDBITS()
  512. returns:
  513. case STATEw:
  514. while (want < need) {
  515. NEEDBITS(n);
  516. keep[want++] = BITS(n);
  517. DROPBITS(n);
  518. }
  519. state = STATEx;
  520. case STATEx:
  521. As shown above, if the next state is also the next case, then the break
  522. is omitted.
  523. A state may also return if there is not enough output space available to
  524. complete that state. Those states are copying stored data, writing a
  525. literal byte, and copying a matching string.
  526. When returning, a "goto inf_leave" is used to update the total counters,
  527. update the check value, and determine whether any progress has been made
  528. during that inflate() call in order to return the proper return code.
  529. Progress is defined as a change in either strm->avail_in or strm->avail_out.
  530. When there is a window, goto inf_leave will update the window with the last
  531. output written. If a goto inf_leave occurs in the middle of decompression
  532. and there is no window currently, goto inf_leave will create one and copy
  533. output to the window for the next call of inflate().
  534. In this implementation, the flush parameter of inflate() only affects the
  535. return code (per zlib.h). inflate() always writes as much as possible to
  536. strm->next_out, given the space available and the provided input--the effect
  537. documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
  538. the allocation of and copying into a sliding window until necessary, which
  539. provides the effect documented in zlib.h for Z_FINISH when the entire input
  540. stream available. So the only thing the flush parameter actually does is:
  541. when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
  542. will return Z_BUF_ERROR if it has not reached the end of the stream.
  543. */
  544. int ZEXPORT inflate(strm, flush)
  545. z_streamp strm;
  546. int flush;
  547. {
  548. struct inflate_state FAR *state;
  549. z_const unsigned char FAR *next; /* next input */
  550. unsigned char FAR *put; /* next output */
  551. unsigned have, left; /* available input and output */
  552. unsigned long hold; /* bit buffer */
  553. unsigned bits; /* bits in bit buffer */
  554. unsigned in, out; /* save starting available input and output */
  555. unsigned copy; /* number of stored or match bytes to copy */
  556. unsigned char FAR *from; /* where to copy match bytes from */
  557. code here; /* current decoding table entry */
  558. code last; /* parent table entry */
  559. unsigned len; /* length to copy for repeats, bits to drop */
  560. int ret; /* return code */
  561. #ifdef GUNZIP
  562. unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
  563. #endif
  564. static const unsigned short order[19] = /* permutation of code lengths */
  565. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  566. if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
  567. (strm->next_in == Z_NULL && strm->avail_in != 0))
  568. return Z_STREAM_ERROR;
  569. state = (struct inflate_state FAR *)strm->state;
  570. if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
  571. LOAD();
  572. in = have;
  573. out = left;
  574. ret = Z_OK;
  575. for (;;)
  576. switch (state->mode) {
  577. case HEAD:
  578. if (state->wrap == 0) {
  579. state->mode = TYPEDO;
  580. break;
  581. }
  582. NEEDBITS(16);
  583. #ifdef GUNZIP
  584. if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
  585. state->check = crc32(0L, Z_NULL, 0);
  586. CRC2(state->check, hold);
  587. INITBITS();
  588. state->mode = FLAGS;
  589. break;
  590. }
  591. state->flags = 0; /* expect zlib header */
  592. if (state->head != Z_NULL)
  593. state->head->done = -1;
  594. if (!(state->wrap & 1) || /* check if zlib header allowed */
  595. #else
  596. if (
  597. #endif
  598. ((BITS(8) << 8) + (hold >> 8)) % 31) {
  599. strm->msg = (char *)"incorrect header check";
  600. state->mode = BAD;
  601. break;
  602. }
  603. if (BITS(4) != Z_DEFLATED) {
  604. strm->msg = (char *)"unknown compression method";
  605. state->mode = BAD;
  606. break;
  607. }
  608. DROPBITS(4);
  609. len = BITS(4) + 8;
  610. if (state->wbits == 0)
  611. state->wbits = len;
  612. else if (len > state->wbits) {
  613. strm->msg = (char *)"invalid window size";
  614. state->mode = BAD;
  615. break;
  616. }
  617. state->dmax = 1U << len;
  618. Tracev((stderr, "inflate: zlib header ok\n"));
  619. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  620. state->mode = hold & 0x200 ? DICTID : TYPE;
  621. INITBITS();
  622. break;
  623. #ifdef GUNZIP
  624. case FLAGS:
  625. NEEDBITS(16);
  626. state->flags = (int)(hold);
  627. if ((state->flags & 0xff) != Z_DEFLATED) {
  628. strm->msg = (char *)"unknown compression method";
  629. state->mode = BAD;
  630. break;
  631. }
  632. if (state->flags & 0xe000) {
  633. strm->msg = (char *)"unknown header flags set";
  634. state->mode = BAD;
  635. break;
  636. }
  637. if (state->head != Z_NULL)
  638. state->head->text = (int)((hold >> 8) & 1);
  639. if (state->flags & 0x0200) CRC2(state->check, hold);
  640. INITBITS();
  641. state->mode = TIME;
  642. case TIME:
  643. NEEDBITS(32);
  644. if (state->head != Z_NULL)
  645. state->head->time = hold;
  646. if (state->flags & 0x0200) CRC4(state->check, hold);
  647. INITBITS();
  648. state->mode = OS;
  649. case OS:
  650. NEEDBITS(16);
  651. if (state->head != Z_NULL) {
  652. state->head->xflags = (int)(hold & 0xff);
  653. state->head->os = (int)(hold >> 8);
  654. }
  655. if (state->flags & 0x0200) CRC2(state->check, hold);
  656. INITBITS();
  657. state->mode = EXLEN;
  658. case EXLEN:
  659. if (state->flags & 0x0400) {
  660. NEEDBITS(16);
  661. state->length = (unsigned)(hold);
  662. if (state->head != Z_NULL)
  663. state->head->extra_len = (unsigned)hold;
  664. if (state->flags & 0x0200) CRC2(state->check, hold);
  665. INITBITS();
  666. }
  667. else if (state->head != Z_NULL)
  668. state->head->extra = Z_NULL;
  669. state->mode = EXTRA;
  670. case EXTRA:
  671. if (state->flags & 0x0400) {
  672. copy = state->length;
  673. if (copy > have) copy = have;
  674. if (copy) {
  675. if (state->head != Z_NULL &&
  676. state->head->extra != Z_NULL) {
  677. len = state->head->extra_len - state->length;
  678. zmemcpy(state->head->extra + len, next,
  679. len + copy > state->head->extra_max ?
  680. state->head->extra_max - len : copy);
  681. }
  682. if (state->flags & 0x0200)
  683. state->check = crc32(state->check, next, copy);
  684. have -= copy;
  685. next += copy;
  686. state->length -= copy;
  687. }
  688. if (state->length) goto inf_leave;
  689. }
  690. state->length = 0;
  691. state->mode = NAME;
  692. case NAME:
  693. if (state->flags & 0x0800) {
  694. if (have == 0) goto inf_leave;
  695. copy = 0;
  696. do {
  697. len = (unsigned)(next[copy++]);
  698. if (state->head != Z_NULL &&
  699. state->head->name != Z_NULL &&
  700. state->length < state->head->name_max)
  701. state->head->name[state->length++] = len;
  702. } while (len && copy < have);
  703. if (state->flags & 0x0200)
  704. state->check = crc32(state->check, next, copy);
  705. have -= copy;
  706. next += copy;
  707. if (len) goto inf_leave;
  708. }
  709. else if (state->head != Z_NULL)
  710. state->head->name = Z_NULL;
  711. state->length = 0;
  712. state->mode = COMMENT;
  713. case COMMENT:
  714. if (state->flags & 0x1000) {
  715. if (have == 0) goto inf_leave;
  716. copy = 0;
  717. do {
  718. len = (unsigned)(next[copy++]);
  719. if (state->head != Z_NULL &&
  720. state->head->comment != Z_NULL &&
  721. state->length < state->head->comm_max)
  722. state->head->comment[state->length++] = len;
  723. } while (len && copy < have);
  724. if (state->flags & 0x0200)
  725. state->check = crc32(state->check, next, copy);
  726. have -= copy;
  727. next += copy;
  728. if (len) goto inf_leave;
  729. }
  730. else if (state->head != Z_NULL)
  731. state->head->comment = Z_NULL;
  732. state->mode = HCRC;
  733. case HCRC:
  734. if (state->flags & 0x0200) {
  735. NEEDBITS(16);
  736. if (hold != (state->check & 0xffff)) {
  737. strm->msg = (char *)"header crc mismatch";
  738. state->mode = BAD;
  739. break;
  740. }
  741. INITBITS();
  742. }
  743. if (state->head != Z_NULL) {
  744. state->head->hcrc = (int)((state->flags >> 9) & 1);
  745. state->head->done = 1;
  746. }
  747. strm->adler = state->check = crc32(0L, Z_NULL, 0);
  748. state->mode = TYPE;
  749. break;
  750. #endif
  751. case DICTID:
  752. NEEDBITS(32);
  753. strm->adler = state->check = ZSWAP32(hold);
  754. INITBITS();
  755. state->mode = DICT;
  756. case DICT:
  757. if (state->havedict == 0) {
  758. RESTORE();
  759. return Z_NEED_DICT;
  760. }
  761. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  762. state->mode = TYPE;
  763. case TYPE:
  764. if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
  765. case TYPEDO:
  766. if (state->last) {
  767. BYTEBITS();
  768. state->mode = CHECK;
  769. break;
  770. }
  771. NEEDBITS(3);
  772. state->last = BITS(1);
  773. DROPBITS(1);
  774. switch (BITS(2)) {
  775. case 0: /* stored block */
  776. Tracev((stderr, "inflate: stored block%s\n",
  777. state->last ? " (last)" : ""));
  778. state->mode = STORED;
  779. break;
  780. case 1: /* fixed block */
  781. fixedtables(state);
  782. Tracev((stderr, "inflate: fixed codes block%s\n",
  783. state->last ? " (last)" : ""));
  784. state->mode = LEN_; /* decode codes */
  785. if (flush == Z_TREES) {
  786. DROPBITS(2);
  787. goto inf_leave;
  788. }
  789. break;
  790. case 2: /* dynamic block */
  791. Tracev((stderr, "inflate: dynamic codes block%s\n",
  792. state->last ? " (last)" : ""));
  793. state->mode = TABLE;
  794. break;
  795. case 3:
  796. strm->msg = (char *)"invalid block type";
  797. state->mode = BAD;
  798. }
  799. DROPBITS(2);
  800. break;
  801. case STORED:
  802. BYTEBITS(); /* go to byte boundary */
  803. NEEDBITS(32);
  804. if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  805. strm->msg = (char *)"invalid stored block lengths";
  806. state->mode = BAD;
  807. break;
  808. }
  809. state->length = (unsigned)hold & 0xffff;
  810. Tracev((stderr, "inflate: stored length %u\n",
  811. state->length));
  812. INITBITS();
  813. state->mode = COPY_;
  814. if (flush == Z_TREES) goto inf_leave;
  815. case COPY_:
  816. state->mode = COPY;
  817. case COPY:
  818. copy = state->length;
  819. if (copy) {
  820. if (copy > have) copy = have;
  821. if (copy > left) copy = left;
  822. if (copy == 0) goto inf_leave;
  823. zmemcpy(put, next, copy);
  824. have -= copy;
  825. next += copy;
  826. left -= copy;
  827. put += copy;
  828. state->length -= copy;
  829. break;
  830. }
  831. Tracev((stderr, "inflate: stored end\n"));
  832. state->mode = TYPE;
  833. break;
  834. case TABLE:
  835. NEEDBITS(14);
  836. state->nlen = BITS(5) + 257;
  837. DROPBITS(5);
  838. state->ndist = BITS(5) + 1;
  839. DROPBITS(5);
  840. state->ncode = BITS(4) + 4;
  841. DROPBITS(4);
  842. #ifndef PKZIP_BUG_WORKAROUND
  843. if (state->nlen > 286 || state->ndist > 30) {
  844. strm->msg = (char *)"too many length or distance symbols";
  845. state->mode = BAD;
  846. break;
  847. }
  848. #endif
  849. Tracev((stderr, "inflate: table sizes ok\n"));
  850. state->have = 0;
  851. state->mode = LENLENS;
  852. case LENLENS:
  853. while (state->have < state->ncode) {
  854. NEEDBITS(3);
  855. state->lens[order[state->have++]] = (unsigned short)BITS(3);
  856. DROPBITS(3);
  857. }
  858. while (state->have < 19)
  859. state->lens[order[state->have++]] = 0;
  860. state->next = state->codes;
  861. state->lencode = (const code FAR *)(state->next);
  862. state->lenbits = 7;
  863. ret = inflate_table(CODES, state->lens, 19, &(state->next),
  864. &(state->lenbits), state->work);
  865. if (ret) {
  866. strm->msg = (char *)"invalid code lengths set";
  867. state->mode = BAD;
  868. break;
  869. }
  870. Tracev((stderr, "inflate: code lengths ok\n"));
  871. state->have = 0;
  872. state->mode = CODELENS;
  873. case CODELENS:
  874. while (state->have < state->nlen + state->ndist) {
  875. for (;;) {
  876. here = state->lencode[BITS(state->lenbits)];
  877. if ((unsigned)(here.bits) <= bits) break;
  878. PULLBYTE();
  879. }
  880. if (here.val < 16) {
  881. DROPBITS(here.bits);
  882. state->lens[state->have++] = here.val;
  883. }
  884. else {
  885. if (here.val == 16) {
  886. NEEDBITS(here.bits + 2);
  887. DROPBITS(here.bits);
  888. if (state->have == 0) {
  889. strm->msg = (char *)"invalid bit length repeat";
  890. state->mode = BAD;
  891. break;
  892. }
  893. len = state->lens[state->have - 1];
  894. copy = 3 + BITS(2);
  895. DROPBITS(2);
  896. }
  897. else if (here.val == 17) {
  898. NEEDBITS(here.bits + 3);
  899. DROPBITS(here.bits);
  900. len = 0;
  901. copy = 3 + BITS(3);
  902. DROPBITS(3);
  903. }
  904. else {
  905. NEEDBITS(here.bits + 7);
  906. DROPBITS(here.bits);
  907. len = 0;
  908. copy = 11 + BITS(7);
  909. DROPBITS(7);
  910. }
  911. if (state->have + copy > state->nlen + state->ndist) {
  912. strm->msg = (char *)"invalid bit length repeat";
  913. state->mode = BAD;
  914. break;
  915. }
  916. while (copy--)
  917. state->lens[state->have++] = (unsigned short)len;
  918. }
  919. }
  920. /* handle error breaks in while */
  921. if (state->mode == BAD) break;
  922. /* check for end-of-block code (better have one) */
  923. if (state->lens[256] == 0) {
  924. strm->msg = (char *)"invalid code -- missing end-of-block";
  925. state->mode = BAD;
  926. break;
  927. }
  928. /* build code tables -- note: do not change the lenbits or distbits
  929. values here (9 and 6) without reading the comments in inftrees.h
  930. concerning the ENOUGH constants, which depend on those values */
  931. state->next = state->codes;
  932. state->lencode = (const code FAR *)(state->next);
  933. state->lenbits = 9;
  934. ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
  935. &(state->lenbits), state->work);
  936. if (ret) {
  937. strm->msg = (char *)"invalid literal/lengths set";
  938. state->mode = BAD;
  939. break;
  940. }
  941. state->distcode = (const code FAR *)(state->next);
  942. state->distbits = 6;
  943. ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  944. &(state->next), &(state->distbits), state->work);
  945. if (ret) {
  946. strm->msg = (char *)"invalid distances set";
  947. state->mode = BAD;
  948. break;
  949. }
  950. Tracev((stderr, "inflate: codes ok\n"));
  951. state->mode = LEN_;
  952. if (flush == Z_TREES) goto inf_leave;
  953. case LEN_:
  954. state->mode = LEN;
  955. case LEN:
  956. if (have >= 6 && left >= 258) {
  957. RESTORE();
  958. inflate_fast(strm, out);
  959. LOAD();
  960. if (state->mode == TYPE)
  961. state->back = -1;
  962. break;
  963. }
  964. state->back = 0;
  965. for (;;) {
  966. here = state->lencode[BITS(state->lenbits)];
  967. if ((unsigned)(here.bits) <= bits) break;
  968. PULLBYTE();
  969. }
  970. if (here.op && (here.op & 0xf0) == 0) {
  971. last = here;
  972. for (;;) {
  973. here = state->lencode[last.val +
  974. (BITS(last.bits + last.op) >> last.bits)];
  975. if ((unsigned)(last.bits + here.bits) <= bits) break;
  976. PULLBYTE();
  977. }
  978. DROPBITS(last.bits);
  979. state->back += last.bits;
  980. }
  981. DROPBITS(here.bits);
  982. state->back += here.bits;
  983. state->length = (unsigned)here.val;
  984. if ((int)(here.op) == 0) {
  985. Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  986. "inflate: literal '%c'\n" :
  987. "inflate: literal 0x%02x\n", here.val));
  988. state->mode = LIT;
  989. break;
  990. }
  991. if (here.op & 32) {
  992. Tracevv((stderr, "inflate: end of block\n"));
  993. state->back = -1;
  994. state->mode = TYPE;
  995. break;
  996. }
  997. if (here.op & 64) {
  998. strm->msg = (char *)"invalid literal/length code";
  999. state->mode = BAD;
  1000. break;
  1001. }
  1002. state->extra = (unsigned)(here.op) & 15;
  1003. state->mode = LENEXT;
  1004. case LENEXT:
  1005. if (state->extra) {
  1006. NEEDBITS(state->extra);
  1007. state->length += BITS(state->extra);
  1008. DROPBITS(state->extra);
  1009. state->back += state->extra;
  1010. }
  1011. Tracevv((stderr, "inflate: length %u\n", state->length));
  1012. state->was = state->length;
  1013. state->mode = DIST;
  1014. case DIST:
  1015. for (;;) {
  1016. here = state->distcode[BITS(state->distbits)];
  1017. if ((unsigned)(here.bits) <= bits) break;
  1018. PULLBYTE();
  1019. }
  1020. if ((here.op & 0xf0) == 0) {
  1021. last = here;
  1022. for (;;) {
  1023. here = state->distcode[last.val +
  1024. (BITS(last.bits + last.op) >> last.bits)];
  1025. if ((unsigned)(last.bits + here.bits) <= bits) break;
  1026. PULLBYTE();
  1027. }
  1028. DROPBITS(last.bits);
  1029. state->back += last.bits;
  1030. }
  1031. DROPBITS(here.bits);
  1032. state->back += here.bits;
  1033. if (here.op & 64) {
  1034. strm->msg = (char *)"invalid distance code";
  1035. state->mode = BAD;
  1036. break;
  1037. }
  1038. state->offset = (unsigned)here.val;
  1039. state->extra = (unsigned)(here.op) & 15;
  1040. state->mode = DISTEXT;
  1041. case DISTEXT:
  1042. if (state->extra) {
  1043. NEEDBITS(state->extra);
  1044. state->offset += BITS(state->extra);
  1045. DROPBITS(state->extra);
  1046. state->back += state->extra;
  1047. }
  1048. #ifdef INFLATE_STRICT
  1049. if (state->offset > state->dmax) {
  1050. strm->msg = (char *)"invalid distance too far back";
  1051. state->mode = BAD;
  1052. break;
  1053. }
  1054. #endif
  1055. Tracevv((stderr, "inflate: distance %u\n", state->offset));
  1056. state->mode = MATCH;
  1057. case MATCH:
  1058. if (left == 0) goto inf_leave;
  1059. copy = out - left;
  1060. if (state->offset > copy) { /* copy from window */
  1061. copy = state->offset - copy;
  1062. if (copy > state->whave) {
  1063. if (state->sane) {
  1064. strm->msg = (char *)"invalid distance too far back";
  1065. state->mode = BAD;
  1066. break;
  1067. }
  1068. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1069. Trace((stderr, "inflate.c too far\n"));
  1070. copy -= state->whave;
  1071. if (copy > state->length) copy = state->length;
  1072. if (copy > left) copy = left;
  1073. left -= copy;
  1074. state->length -= copy;
  1075. do {
  1076. *put++ = 0;
  1077. } while (--copy);
  1078. if (state->length == 0) state->mode = LEN;
  1079. break;
  1080. #endif
  1081. }
  1082. if (copy > state->wnext) {
  1083. copy -= state->wnext;
  1084. from = state->window + (state->wsize - copy);
  1085. }
  1086. else
  1087. from = state->window + (state->wnext - copy);
  1088. if (copy > state->length) copy = state->length;
  1089. }
  1090. else { /* copy from output */
  1091. from = put - state->offset;
  1092. copy = state->length;
  1093. }
  1094. if (copy > left) copy = left;
  1095. left -= copy;
  1096. state->length -= copy;
  1097. do {
  1098. *put++ = *from++;
  1099. } while (--copy);
  1100. if (state->length == 0) state->mode = LEN;
  1101. break;
  1102. case LIT:
  1103. if (left == 0) goto inf_leave;
  1104. *put++ = (unsigned char)(state->length);
  1105. left--;
  1106. state->mode = LEN;
  1107. break;
  1108. case CHECK:
  1109. if (state->wrap) {
  1110. NEEDBITS(32);
  1111. out -= left;
  1112. strm->total_out += out;
  1113. state->total += out;
  1114. if (out)
  1115. strm->adler = state->check =
  1116. UPDATE(state->check, put - out, out);
  1117. out = left;
  1118. if ((
  1119. #ifdef GUNZIP
  1120. state->flags ? hold :
  1121. #endif
  1122. ZSWAP32(hold)) != state->check) {
  1123. strm->msg = (char *)"incorrect data check";
  1124. state->mode = BAD;
  1125. break;
  1126. }
  1127. INITBITS();
  1128. Tracev((stderr, "inflate: check matches trailer\n"));
  1129. }
  1130. #ifdef GUNZIP
  1131. state->mode = LENGTH;
  1132. case LENGTH:
  1133. if (state->wrap && state->flags) {
  1134. NEEDBITS(32);
  1135. if (hold != (state->total & 0xffffffffUL)) {
  1136. strm->msg = (char *)"incorrect length check";
  1137. state->mode = BAD;
  1138. break;
  1139. }
  1140. INITBITS();
  1141. Tracev((stderr, "inflate: length matches trailer\n"));
  1142. }
  1143. #endif
  1144. state->mode = DONE;
  1145. case DONE:
  1146. ret = Z_STREAM_END;
  1147. goto inf_leave;
  1148. case BAD:
  1149. ret = Z_DATA_ERROR;
  1150. goto inf_leave;
  1151. case MEM:
  1152. return Z_MEM_ERROR;
  1153. case SYNC:
  1154. default:
  1155. return Z_STREAM_ERROR;
  1156. }
  1157. /*
  1158. Return from inflate(), updating the total counts and the check value.
  1159. If there was no progress during the inflate() call, return a buffer
  1160. error. Call updatewindow() to create and/or update the window state.
  1161. Note: a memory error from inflate() is non-recoverable.
  1162. */
  1163. inf_leave:
  1164. RESTORE();
  1165. if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
  1166. (state->mode < CHECK || flush != Z_FINISH)))
  1167. if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
  1168. state->mode = MEM;
  1169. return Z_MEM_ERROR;
  1170. }
  1171. in -= strm->avail_in;
  1172. out -= strm->avail_out;
  1173. strm->total_in += in;
  1174. strm->total_out += out;
  1175. state->total += out;
  1176. if (state->wrap && out)
  1177. strm->adler = state->check =
  1178. UPDATE(state->check, strm->next_out - out, out);
  1179. strm->data_type = state->bits + (state->last ? 64 : 0) +
  1180. (state->mode == TYPE ? 128 : 0) +
  1181. (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
  1182. if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  1183. ret = Z_BUF_ERROR;
  1184. return ret;
  1185. }
  1186. int ZEXPORT inflateEnd(strm)
  1187. z_streamp strm;
  1188. {
  1189. struct inflate_state FAR *state;
  1190. if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
  1191. return Z_STREAM_ERROR;
  1192. state = (struct inflate_state FAR *)strm->state;
  1193. if (state->window != Z_NULL) ZFREE(strm, state->window);
  1194. ZFREE(strm, strm->state);
  1195. strm->state = Z_NULL;
  1196. Tracev((stderr, "inflate: end\n"));
  1197. return Z_OK;
  1198. }
  1199. int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
  1200. z_streamp strm;
  1201. Bytef *dictionary;
  1202. uInt *dictLength;
  1203. {
  1204. struct inflate_state FAR *state;
  1205. /* check state */
  1206. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1207. state = (struct inflate_state FAR *)strm->state;
  1208. /* copy dictionary */
  1209. if (state->whave && dictionary != Z_NULL) {
  1210. zmemcpy(dictionary, state->window + state->wnext,
  1211. state->whave - state->wnext);
  1212. zmemcpy(dictionary + state->whave - state->wnext,
  1213. state->window, state->wnext);
  1214. }
  1215. if (dictLength != Z_NULL)
  1216. *dictLength = state->whave;
  1217. return Z_OK;
  1218. }
  1219. int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
  1220. z_streamp strm;
  1221. const Bytef *dictionary;
  1222. uInt dictLength;
  1223. {
  1224. struct inflate_state FAR *state;
  1225. unsigned long dictid;
  1226. int ret;
  1227. /* check state */
  1228. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1229. state = (struct inflate_state FAR *)strm->state;
  1230. if (state->wrap != 0 && state->mode != DICT)
  1231. return Z_STREAM_ERROR;
  1232. /* check for correct dictionary identifier */
  1233. if (state->mode == DICT) {
  1234. dictid = adler32(0L, Z_NULL, 0);
  1235. dictid = adler32(dictid, dictionary, dictLength);
  1236. if (dictid != state->check)
  1237. return Z_DATA_ERROR;
  1238. }
  1239. /* copy dictionary to window using updatewindow(), which will amend the
  1240. existing dictionary if appropriate */
  1241. ret = updatewindow(strm, dictionary + dictLength, dictLength);
  1242. if (ret) {
  1243. state->mode = MEM;
  1244. return Z_MEM_ERROR;
  1245. }
  1246. state->havedict = 1;
  1247. Tracev((stderr, "inflate: dictionary set\n"));
  1248. return Z_OK;
  1249. }
  1250. int ZEXPORT inflateGetHeader(strm, head)
  1251. z_streamp strm;
  1252. gz_headerp head;
  1253. {
  1254. struct inflate_state FAR *state;
  1255. /* check state */
  1256. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1257. state = (struct inflate_state FAR *)strm->state;
  1258. if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
  1259. /* save header structure */
  1260. state->head = head;
  1261. head->done = 0;
  1262. return Z_OK;
  1263. }
  1264. /*
  1265. Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
  1266. or when out of input. When called, *have is the number of pattern bytes
  1267. found in order so far, in 0..3. On return *have is updated to the new
  1268. state. If on return *have equals four, then the pattern was found and the
  1269. return value is how many bytes were read including the last byte of the
  1270. pattern. If *have is less than four, then the pattern has not been found
  1271. yet and the return value is len. In the latter case, syncsearch() can be
  1272. called again with more data and the *have state. *have is initialized to
  1273. zero for the first call.
  1274. */
  1275. local unsigned syncsearch(have, buf, len)
  1276. unsigned FAR *have;
  1277. const unsigned char FAR *buf;
  1278. unsigned len;
  1279. {
  1280. unsigned got;
  1281. unsigned next;
  1282. got = *have;
  1283. next = 0;
  1284. while (next < len && got < 4) {
  1285. if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
  1286. got++;
  1287. else if (buf[next])
  1288. got = 0;
  1289. else
  1290. got = 4 - got;
  1291. next++;
  1292. }
  1293. *have = got;
  1294. return next;
  1295. }
  1296. int ZEXPORT inflateSync(strm)
  1297. z_streamp strm;
  1298. {
  1299. unsigned len; /* number of bytes to look at or looked at */
  1300. unsigned long in, out; /* temporary to save total_in and total_out */
  1301. unsigned char buf[4]; /* to restore bit buffer to byte string */
  1302. struct inflate_state FAR *state;
  1303. /* check parameters */
  1304. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1305. state = (struct inflate_state FAR *)strm->state;
  1306. if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
  1307. /* if first time, start search in bit buffer */
  1308. if (state->mode != SYNC) {
  1309. state->mode = SYNC;
  1310. state->hold <<= state->bits & 7;
  1311. state->bits -= state->bits & 7;
  1312. len = 0;
  1313. while (state->bits >= 8) {
  1314. buf[len++] = (unsigned char)(state->hold);
  1315. state->hold >>= 8;
  1316. state->bits -= 8;
  1317. }
  1318. state->have = 0;
  1319. syncsearch(&(state->have), buf, len);
  1320. }
  1321. /* search available input */
  1322. len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
  1323. strm->avail_in -= len;
  1324. strm->next_in += len;
  1325. strm->total_in += len;
  1326. /* return no joy or set up to restart inflate() on a new block */
  1327. if (state->have != 4) return Z_DATA_ERROR;
  1328. in = strm->total_in; out = strm->total_out;
  1329. inflateReset(strm);
  1330. strm->total_in = in; strm->total_out = out;
  1331. state->mode = TYPE;
  1332. return Z_OK;
  1333. }
  1334. /*
  1335. Returns true if inflate is currently at the end of a block generated by
  1336. Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1337. implementation to provide an additional safety check. PPP uses
  1338. Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  1339. block. When decompressing, PPP checks that at the end of input packet,
  1340. inflate is waiting for these length bytes.
  1341. */
  1342. int ZEXPORT inflateSyncPoint(strm)
  1343. z_streamp strm;
  1344. {
  1345. struct inflate_state FAR *state;
  1346. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1347. state = (struct inflate_state FAR *)strm->state;
  1348. return state->mode == STORED && state->bits == 0;
  1349. }
  1350. int ZEXPORT inflateCopy(dest, source)
  1351. z_streamp dest;
  1352. z_streamp source;
  1353. {
  1354. struct inflate_state FAR *state;
  1355. struct inflate_state FAR *copy;
  1356. unsigned char FAR *window;
  1357. unsigned wsize;
  1358. /* check input */
  1359. if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
  1360. source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
  1361. return Z_STREAM_ERROR;
  1362. state = (struct inflate_state FAR *)source->state;
  1363. /* allocate space */
  1364. copy = (struct inflate_state FAR *)
  1365. ZALLOC(source, 1, sizeof(struct inflate_state));
  1366. if (copy == Z_NULL) return Z_MEM_ERROR;
  1367. window = Z_NULL;
  1368. if (state->window != Z_NULL) {
  1369. window = (unsigned char FAR *)
  1370. ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
  1371. if (window == Z_NULL) {
  1372. ZFREE(source, copy);
  1373. return Z_MEM_ERROR;
  1374. }
  1375. }
  1376. /* copy state */
  1377. zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
  1378. zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
  1379. if (state->lencode >= state->codes &&
  1380. state->lencode <= state->codes + ENOUGH - 1) {
  1381. copy->lencode = copy->codes + (state->lencode - state->codes);
  1382. copy->distcode = copy->codes + (state->distcode - state->codes);
  1383. }
  1384. copy->next = copy->codes + (state->next - state->codes);
  1385. if (window != Z_NULL) {
  1386. wsize = 1U << state->wbits;
  1387. zmemcpy(window, state->window, wsize);
  1388. }
  1389. copy->window = window;
  1390. dest->state = (struct internal_state FAR *)copy;
  1391. return Z_OK;
  1392. }
  1393. int ZEXPORT inflateUndermine(strm, subvert)
  1394. z_streamp strm;
  1395. int subvert;
  1396. {
  1397. struct inflate_state FAR *state;
  1398. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1399. state = (struct inflate_state FAR *)strm->state;
  1400. state->sane = !subvert;
  1401. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1402. return Z_OK;
  1403. #else
  1404. state->sane = 1;
  1405. return Z_DATA_ERROR;
  1406. #endif
  1407. }
  1408. long ZEXPORT inflateMark(strm)
  1409. z_streamp strm;
  1410. {
  1411. struct inflate_state FAR *state;
  1412. if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
  1413. state = (struct inflate_state FAR *)strm->state;
  1414. return ((long)(state->back) << 16) +
  1415. (state->mode == COPY ? state->length :
  1416. (state->mode == MATCH ? state->was - state->length : 0));
  1417. }