Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1480 lines
51 KiB

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