PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FreeImage/Source/ZLib/inflate.c

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