inflate.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /* inflate.c -- zlib decompression
  2. * Copyright (C) 1995-2012 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. * Change history:
  7. *
  8. * 1.2.beta0 24 Nov 2002
  9. * - First version -- complete rewrite of inflate to simplify code, avoid
  10. * creation of window when not needed, minimize use of window when it is
  11. * needed, make inffast.c even faster, implement gzip decoding, and to
  12. * improve code readability and style over the previous zlib inflate code
  13. *
  14. * 1.2.beta1 25 Nov 2002
  15. * - Use pointers for available input and output checking in inffast.c
  16. * - Remove input and output counters in inffast.c
  17. * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
  18. * - Remove unnecessary second byte pull from length extra in inffast.c
  19. * - Unroll direct copy to three copies per loop in inffast.c
  20. *
  21. * 1.2.beta2 4 Dec 2002
  22. * - Change external routine names to reduce potential conflicts
  23. * - Correct filename to inffixed.h for fixed tables in inflate.c
  24. * - Make hbuf[] unsigned char to match parameter type in inflate.c
  25. * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
  26. * to avoid negation problem on Alphas (64 bit) in inflate.c
  27. *
  28. * 1.2.beta3 22 Dec 2002
  29. * - Add comments on state->bits assertion in inffast.c
  30. * - Add comments on op field in inftrees.h
  31. * - Fix bug in reuse of allocated window after inflateReset()
  32. * - Remove bit fields--back to byte structure for speed
  33. * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
  34. * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
  35. * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
  36. * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
  37. * - Use local copies of stream next and avail values, as well as local bit
  38. * buffer and bit count in inflate()--for speed when inflate_fast() not used
  39. *
  40. * 1.2.beta4 1 Jan 2003
  41. * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
  42. * - Move a comment on output buffer sizes from inffast.c to inflate.c
  43. * - Add comments in inffast.c to introduce the inflate_fast() routine
  44. * - Rearrange window copies in inflate_fast() for speed and simplification
  45. * - Unroll last copy for window match in inflate_fast()
  46. * - Use local copies of window variables in inflate_fast() for speed
  47. * - Pull out common wnext == 0 case for speed in inflate_fast()
  48. * - Make op and len in inflate_fast() unsigned for consistency
  49. * - Add FAR to lcode and dcode declarations in inflate_fast()
  50. * - Simplified bad distance check in inflate_fast()
  51. * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
  52. * source file infback.c to provide a call-back interface to inflate for
  53. * programs like gzip and unzip -- uses window as output buffer to avoid
  54. * window copying
  55. *
  56. * 1.2.beta5 1 Jan 2003
  57. * - Improved inflateBack() interface to allow the caller to provide initial
  58. * input in strm.
  59. * - Fixed stored blocks bug in inflateBack()
  60. *
  61. * 1.2.beta6 4 Jan 2003
  62. * - Added comments in inffast.c on effectiveness of POSTINC
  63. * - Typecasting all around to reduce compiler warnings
  64. * - Changed loops from while (1) or do {} while (1) to for (;;), again to
  65. * make compilers happy
  66. * - Changed type of window in inflateBackInit() to unsigned char *
  67. *
  68. * 1.2.beta7 27 Jan 2003
  69. * - Changed many types to unsigned or unsigned short to avoid warnings
  70. * - Added inflateCopy() function
  71. *
  72. * 1.2.0 9 Mar 2003
  73. * - Changed inflateBack() interface to provide separate opaque descriptors
  74. * for the in() and out() functions
  75. * - Changed inflateBack() argument and in_func typedef to swap the length
  76. * and buffer address return values for the input function
  77. * - Check next_in and next_out for Z_NULL on entry to inflate()
  78. *
  79. * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
  80. */
  81. #include "zutil.h"
  82. #include "inftrees.h"
  83. #include "inflate.h"
  84. #include "inffast.h"
  85. #ifdef MAKEFIXED
  86. # ifndef BUILDFIXED
  87. # define BUILDFIXED
  88. # endif
  89. #endif
  90. /* function prototypes */
  91. local void fixedtables OF((struct inflate_state FAR *state));
  92. local int updatewindow OF((z_streamp strm, 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. /*
  474. inflate() uses a state machine to process as much input data and generate as
  475. much output data as possible before returning. The state machine is
  476. structured roughly as follows:
  477. for (;;) switch (state) {
  478. ...
  479. case STATEn:
  480. if (not enough input data or output space to make progress)
  481. return;
  482. ... make progress ...
  483. state = STATEm;
  484. break;
  485. ...
  486. }
  487. so when inflate() is called again, the same case is attempted again, and
  488. if the appropriate resources are provided, the machine proceeds to the
  489. next state. The NEEDBITS() macro is usually the way the state evaluates
  490. whether it can proceed or should return. NEEDBITS() does the return if
  491. the requested bits are not available. The typical use of the BITS macros
  492. is:
  493. NEEDBITS(n);
  494. ... do something with BITS(n) ...
  495. DROPBITS(n);
  496. where NEEDBITS(n) either returns from inflate() if there isn't enough
  497. input left to load n bits into the accumulator, or it continues. BITS(n)
  498. gives the low n bits in the accumulator. When done, DROPBITS(n) drops
  499. the low n bits off the accumulator. INITBITS() clears the accumulator
  500. and sets the number of available bits to zero. BYTEBITS() discards just
  501. enough bits to put the accumulator on a byte boundary. After BYTEBITS()
  502. and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
  503. NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
  504. if there is no input available. The decoding of variable length codes uses
  505. PULLBYTE() directly in order to pull just enough bytes to decode the next
  506. code, and no more.
  507. Some states loop until they get enough input, making sure that enough
  508. state information is maintained to continue the loop where it left off
  509. if NEEDBITS() returns in the loop. For example, want, need, and keep
  510. would all have to actually be part of the saved state in case NEEDBITS()
  511. returns:
  512. case STATEw:
  513. while (want < need) {
  514. NEEDBITS(n);
  515. keep[want++] = BITS(n);
  516. DROPBITS(n);
  517. }
  518. state = STATEx;
  519. case STATEx:
  520. As shown above, if the next state is also the next case, then the break
  521. is omitted.
  522. A state may also return if there is not enough output space available to
  523. complete that state. Those states are copying stored data, writing a
  524. literal byte, and copying a matching string.
  525. When returning, a "goto inf_leave" is used to update the total counters,
  526. update the check value, and determine whether any progress has been made
  527. during that inflate() call in order to return the proper return code.
  528. Progress is defined as a change in either strm->avail_in or strm->avail_out.
  529. When there is a window, goto inf_leave will update the window with the last
  530. output written. If a goto inf_leave occurs in the middle of decompression
  531. and there is no window currently, goto inf_leave will create one and copy
  532. output to the window for the next call of inflate().
  533. In this implementation, the flush parameter of inflate() only affects the
  534. return code (per zlib.h). inflate() always writes as much as possible to
  535. strm->next_out, given the space available and the provided input--the effect
  536. documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
  537. the allocation of and copying into a sliding window until necessary, which
  538. provides the effect documented in zlib.h for Z_FINISH when the entire input
  539. stream available. So the only thing the flush parameter actually does is:
  540. when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
  541. will return Z_BUF_ERROR if it has not reached the end of the stream.
  542. */
  543. int ZEXPORT inflate(strm, flush)
  544. z_streamp strm;
  545. int flush;
  546. {
  547. struct inflate_state FAR *state;
  548. unsigned char FAR *next; /* next input */
  549. unsigned char FAR *put; /* next output */
  550. unsigned have, left; /* available input and output */
  551. unsigned long hold; /* bit buffer */
  552. unsigned bits; /* bits in bit buffer */
  553. unsigned in, out; /* save starting available input and output */
  554. unsigned copy; /* number of stored or match bytes to copy */
  555. unsigned char FAR *from; /* where to copy match bytes from */
  556. code here; /* current decoding table entry */
  557. code last; /* parent table entry */
  558. unsigned len; /* length to copy for repeats, bits to drop */
  559. int ret; /* return code */
  560. #ifdef GUNZIP
  561. unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
  562. #endif
  563. static const unsigned short order[19] = /* permutation of code lengths */
  564. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  565. if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
  566. (strm->next_in == Z_NULL && strm->avail_in != 0))
  567. return Z_STREAM_ERROR;
  568. state = (struct inflate_state FAR *)strm->state;
  569. if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
  570. LOAD();
  571. in = have;
  572. out = left;
  573. ret = Z_OK;
  574. for (;;)
  575. switch (state->mode) {
  576. case HEAD:
  577. if (state->wrap == 0) {
  578. state->mode = TYPEDO;
  579. break;
  580. }
  581. NEEDBITS(16);
  582. #ifdef GUNZIP
  583. if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
  584. state->check = crc32(0L, Z_NULL, 0);
  585. CRC2(state->check, hold);
  586. INITBITS();
  587. state->mode = FLAGS;
  588. break;
  589. }
  590. state->flags = 0; /* expect zlib header */
  591. if (state->head != Z_NULL)
  592. state->head->done = -1;
  593. if (!(state->wrap & 1) || /* check if zlib header allowed */
  594. #else
  595. if (
  596. #endif
  597. ((BITS(8) << 8) + (hold >> 8)) % 31) {
  598. strm->msg = (char *)"incorrect header check";
  599. state->mode = BAD;
  600. break;
  601. }
  602. if (BITS(4) != Z_DEFLATED) {
  603. strm->msg = (char *)"unknown compression method";
  604. state->mode = BAD;
  605. break;
  606. }
  607. DROPBITS(4);
  608. len = BITS(4) + 8;
  609. if (state->wbits == 0)
  610. state->wbits = len;
  611. else if (len > state->wbits) {
  612. strm->msg = (char *)"invalid window size";
  613. state->mode = BAD;
  614. break;
  615. }
  616. state->dmax = 1U << len;
  617. Tracev((stderr, "inflate: zlib header ok\n"));
  618. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  619. state->mode = hold & 0x200 ? DICTID : TYPE;
  620. INITBITS();
  621. break;
  622. #ifdef GUNZIP
  623. case FLAGS:
  624. NEEDBITS(16);
  625. state->flags = (int)(hold);
  626. if ((state->flags & 0xff) != Z_DEFLATED) {
  627. strm->msg = (char *)"unknown compression method";
  628. state->mode = BAD;
  629. break;
  630. }
  631. if (state->flags & 0xe000) {
  632. strm->msg = (char *)"unknown header flags set";
  633. state->mode = BAD;
  634. break;
  635. }
  636. if (state->head != Z_NULL)
  637. state->head->text = (int)((hold >> 8) & 1);
  638. if (state->flags & 0x0200) CRC2(state->check, hold);
  639. INITBITS();
  640. state->mode = TIME;
  641. case TIME:
  642. NEEDBITS(32);
  643. if (state->head != Z_NULL)
  644. state->head->time = hold;
  645. if (state->flags & 0x0200) CRC4(state->check, hold);
  646. INITBITS();
  647. state->mode = OS;
  648. case OS:
  649. NEEDBITS(16);
  650. if (state->head != Z_NULL) {
  651. state->head->xflags = (int)(hold & 0xff);
  652. state->head->os = (int)(hold >> 8);
  653. }
  654. if (state->flags & 0x0200) CRC2(state->check, hold);
  655. INITBITS();
  656. state->mode = EXLEN;
  657. case EXLEN:
  658. if (state->flags & 0x0400) {
  659. NEEDBITS(16);
  660. state->length = (unsigned)(hold);
  661. if (state->head != Z_NULL)
  662. state->head->extra_len = (unsigned)hold;
  663. if (state->flags & 0x0200) CRC2(state->check, hold);
  664. INITBITS();
  665. }
  666. else if (state->head != Z_NULL)
  667. state->head->extra = Z_NULL;
  668. state->mode = EXTRA;
  669. case EXTRA:
  670. if (state->flags & 0x0400) {
  671. copy = state->length;
  672. if (copy > have) copy = have;
  673. if (copy) {
  674. if (state->head != Z_NULL &&
  675. state->head->extra != Z_NULL) {
  676. len = state->head->extra_len - state->length;
  677. zmemcpy(state->head->extra + len, next,
  678. len + copy > state->head->extra_max ?
  679. state->head->extra_max - len : copy);
  680. }
  681. if (state->flags & 0x0200)
  682. state->check = crc32(state->check, next, copy);
  683. have -= copy;
  684. next += copy;
  685. state->length -= copy;
  686. }
  687. if (state->length) goto inf_leave;
  688. }
  689. state->length = 0;
  690. state->mode = NAME;
  691. case NAME:
  692. if (state->flags & 0x0800) {
  693. if (have == 0) goto inf_leave;
  694. copy = 0;
  695. do {
  696. len = (unsigned)(next[copy++]);
  697. if (state->head != Z_NULL &&
  698. state->head->name != Z_NULL &&
  699. state->length < state->head->name_max)
  700. state->head->name[state->length++] = len;
  701. } while (len && copy < have);
  702. if (state->flags & 0x0200)
  703. state->check = crc32(state->check, next, copy);
  704. have -= copy;
  705. next += copy;
  706. if (len) goto inf_leave;
  707. }
  708. else if (state->head != Z_NULL)
  709. state->head->name = Z_NULL;
  710. state->length = 0;
  711. state->mode = COMMENT;
  712. case COMMENT:
  713. if (state->flags & 0x1000) {
  714. if (have == 0) goto inf_leave;
  715. copy = 0;
  716. do {
  717. len = (unsigned)(next[copy++]);
  718. if (state->head != Z_NULL &&
  719. state->head->comment != Z_NULL &&
  720. state->length < state->head->comm_max)
  721. state->head->comment[state->length++] = len;
  722. } while (len && copy < have);
  723. if (state->flags & 0x0200)
  724. state->check = crc32(state->check, next, copy);
  725. have -= copy;
  726. next += copy;
  727. if (len) goto inf_leave;
  728. }
  729. else if (state->head != Z_NULL)
  730. state->head->comment = Z_NULL;
  731. state->mode = HCRC;
  732. case HCRC:
  733. if (state->flags & 0x0200) {
  734. NEEDBITS(16);
  735. if (hold != (state->check & 0xffff)) {
  736. strm->msg = (char *)"header crc mismatch";
  737. state->mode = BAD;
  738. break;
  739. }
  740. INITBITS();
  741. }
  742. if (state->head != Z_NULL) {
  743. state->head->hcrc = (int)((state->flags >> 9) & 1);
  744. state->head->done = 1;
  745. }
  746. strm->adler = state->check = crc32(0L, Z_NULL, 0);
  747. state->mode = TYPE;
  748. break;
  749. #endif
  750. case DICTID:
  751. NEEDBITS(32);
  752. strm->adler = state->check = ZSWAP32(hold);
  753. INITBITS();
  754. state->mode = DICT;
  755. case DICT:
  756. if (state->havedict == 0) {
  757. RESTORE();
  758. return Z_NEED_DICT;
  759. }
  760. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  761. state->mode = TYPE;
  762. case TYPE:
  763. if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
  764. case TYPEDO:
  765. if (state->last) {
  766. BYTEBITS();
  767. state->mode = CHECK;
  768. break;
  769. }
  770. NEEDBITS(3);
  771. state->last = BITS(1);
  772. DROPBITS(1);
  773. switch (BITS(2)) {
  774. case 0: /* stored block */
  775. Tracev((stderr, "inflate: stored block%s\n",
  776. state->last ? " (last)" : ""));
  777. state->mode = STORED;
  778. break;
  779. case 1: /* fixed block */
  780. fixedtables(state);
  781. Tracev((stderr, "inflate: fixed codes block%s\n",
  782. state->last ? " (last)" : ""));
  783. state->mode = LEN_; /* decode codes */
  784. if (flush == Z_TREES) {
  785. DROPBITS(2);
  786. goto inf_leave;
  787. }
  788. break;
  789. case 2: /* dynamic block */
  790. Tracev((stderr, "inflate: dynamic codes block%s\n",
  791. state->last ? " (last)" : ""));
  792. state->mode = TABLE;
  793. break;
  794. case 3:
  795. strm->msg = (char *)"invalid block type";
  796. state->mode = BAD;
  797. }
  798. DROPBITS(2);
  799. break;
  800. case STORED:
  801. BYTEBITS(); /* go to byte boundary */
  802. NEEDBITS(32);
  803. if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  804. strm->msg = (char *)"invalid stored block lengths";
  805. state->mode = BAD;
  806. break;
  807. }
  808. state->length = (unsigned)hold & 0xffff;
  809. Tracev((stderr, "inflate: stored length %u\n",
  810. state->length));
  811. INITBITS();
  812. state->mode = COPY_;
  813. if (flush == Z_TREES) goto inf_leave;
  814. case COPY_:
  815. state->mode = COPY;
  816. case COPY:
  817. copy = state->length;
  818. if (copy) {
  819. if (copy > have) copy = have;
  820. if (copy > left) copy = left;
  821. if (copy == 0) goto inf_leave;
  822. zmemcpy(put, next, copy);
  823. have -= copy;
  824. next += copy;
  825. left -= copy;
  826. put += copy;
  827. state->length -= copy;
  828. break;
  829. }
  830. Tracev((stderr, "inflate: stored end\n"));
  831. state->mode = TYPE;
  832. break;
  833. case TABLE:
  834. NEEDBITS(14);
  835. state->nlen = BITS(5) + 257;
  836. DROPBITS(5);
  837. state->ndist = BITS(5) + 1;
  838. DROPBITS(5);
  839. state->ncode = BITS(4) + 4;
  840. DROPBITS(4);
  841. #ifndef PKZIP_BUG_WORKAROUND
  842. if (state->nlen > 286 || state->ndist > 30) {
  843. strm->msg = (char *)"too many length or distance symbols";
  844. state->mode = BAD;
  845. break;
  846. }
  847. #endif
  848. Tracev((stderr, "inflate: table sizes ok\n"));
  849. state->have = 0;
  850. state->mode = LENLENS;
  851. case LENLENS:
  852. while (state->have < state->ncode) {
  853. NEEDBITS(3);
  854. state->lens[order[state->have++]] = (unsigned short)BITS(3);
  855. DROPBITS(3);
  856. }
  857. while (state->have < 19)
  858. state->lens[order[state->have++]] = 0;
  859. state->next = state->codes;
  860. state->lencode = (code const FAR *)(state->next);
  861. state->lenbits = 7;
  862. ret = inflate_table(CODES, state->lens, 19, &(state->next),
  863. &(state->lenbits), state->work);
  864. if (ret) {
  865. strm->msg = (char *)"invalid code lengths set";
  866. state->mode = BAD;
  867. break;
  868. }
  869. Tracev((stderr, "inflate: code lengths ok\n"));
  870. state->have = 0;
  871. state->mode = CODELENS;
  872. case CODELENS:
  873. while (state->have < state->nlen + state->ndist) {
  874. for (;;) {
  875. here = state->lencode[BITS(state->lenbits)];
  876. if ((unsigned)(here.bits) <= bits) break;
  877. PULLBYTE();
  878. }
  879. if (here.val < 16) {
  880. DROPBITS(here.bits);
  881. state->lens[state->have++] = here.val;
  882. }
  883. else {
  884. if (here.val == 16) {
  885. NEEDBITS(here.bits + 2);
  886. DROPBITS(here.bits);
  887. if (state->have == 0) {
  888. strm->msg = (char *)"invalid bit length repeat";
  889. state->mode = BAD;
  890. break;
  891. }
  892. len = state->lens[state->have - 1];
  893. copy = 3 + BITS(2);
  894. DROPBITS(2);
  895. }
  896. else if (here.val == 17) {
  897. NEEDBITS(here.bits + 3);
  898. DROPBITS(here.bits);
  899. len = 0;
  900. copy = 3 + BITS(3);
  901. DROPBITS(3);
  902. }
  903. else {
  904. NEEDBITS(here.bits + 7);
  905. DROPBITS(here.bits);
  906. len = 0;
  907. copy = 11 + BITS(7);
  908. DROPBITS(7);
  909. }
  910. if (state->have + copy > state->nlen + state->ndist) {
  911. strm->msg = (char *)"invalid bit length repeat";
  912. state->mode = BAD;
  913. break;
  914. }
  915. while (copy--)
  916. state->lens[state->have++] = (unsigned short)len;
  917. }
  918. }
  919. /* handle error breaks in while */
  920. if (state->mode == BAD) break;
  921. /* check for end-of-block code (better have one) */
  922. if (state->lens[256] == 0) {
  923. strm->msg = (char *)"invalid code -- missing end-of-block";
  924. state->mode = BAD;
  925. break;
  926. }
  927. /* build code tables -- note: do not change the lenbits or distbits
  928. values here (9 and 6) without reading the comments in inftrees.h
  929. concerning the ENOUGH constants, which depend on those values */
  930. state->next = state->codes;
  931. state->lencode = (code const FAR *)(state->next);
  932. state->lenbits = 9;
  933. ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
  934. &(state->lenbits), state->work);
  935. if (ret) {
  936. strm->msg = (char *)"invalid literal/lengths set";
  937. state->mode = BAD;
  938. break;
  939. }
  940. state->distcode = (code const FAR *)(state->next);
  941. state->distbits = 6;
  942. ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  943. &(state->next), &(state->distbits), state->work);
  944. if (ret) {
  945. strm->msg = (char *)"invalid distances set";
  946. state->mode = BAD;
  947. break;
  948. }
  949. Tracev((stderr, "inflate: codes ok\n"));
  950. state->mode = LEN_;
  951. if (flush == Z_TREES) goto inf_leave;
  952. case LEN_:
  953. state->mode = LEN;
  954. case LEN:
  955. if (have >= 6 && left >= 258) {
  956. RESTORE();
  957. inflate_fast(strm, out);
  958. LOAD();
  959. if (state->mode == TYPE)
  960. state->back = -1;
  961. break;
  962. }
  963. state->back = 0;
  964. for (;;) {
  965. here = state->lencode[BITS(state->lenbits)];
  966. if ((unsigned)(here.bits) <= bits) break;
  967. PULLBYTE();
  968. }
  969. if (here.op && (here.op & 0xf0) == 0) {
  970. last = here;
  971. for (;;) {
  972. here = state->lencode[last.val +
  973. (BITS(last.bits + last.op) >> last.bits)];
  974. if ((unsigned)(last.bits + here.bits) <= bits) break;
  975. PULLBYTE();
  976. }
  977. DROPBITS(last.bits);
  978. state->back += last.bits;
  979. }
  980. DROPBITS(here.bits);
  981. state->back += here.bits;
  982. state->length = (unsigned)here.val;
  983. if ((int)(here.op) == 0) {
  984. Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  985. "inflate: literal '%c'\n" :
  986. "inflate: literal 0x%02x\n", here.val));
  987. state->mode = LIT;
  988. break;
  989. }
  990. if (here.op & 32) {
  991. Tracevv((stderr, "inflate: end of block\n"));
  992. state->back = -1;
  993. state->mode = TYPE;
  994. break;
  995. }
  996. if (here.op & 64) {
  997. strm->msg = (char *)"invalid literal/length code";
  998. state->mode = BAD;
  999. break;
  1000. }
  1001. state->extra = (unsigned)(here.op) & 15;
  1002. state->mode = LENEXT;
  1003. case LENEXT:
  1004. if (state->extra) {
  1005. NEEDBITS(state->extra);
  1006. state->length += BITS(state->extra);
  1007. DROPBITS(state->extra);
  1008. state->back += state->extra;
  1009. }
  1010. Tracevv((stderr, "inflate: length %u\n", state->length));
  1011. state->was = state->length;
  1012. state->mode = DIST;
  1013. case DIST:
  1014. for (;;) {
  1015. here = state->distcode[BITS(state->distbits)];
  1016. if ((unsigned)(here.bits) <= bits) break;
  1017. PULLBYTE();
  1018. }
  1019. if ((here.op & 0xf0) == 0) {
  1020. last = here;
  1021. for (;;) {
  1022. here = state->distcode[last.val +
  1023. (BITS(last.bits + last.op) >> last.bits)];
  1024. if ((unsigned)(last.bits + here.bits) <= bits) break;
  1025. PULLBYTE();
  1026. }
  1027. DROPBITS(last.bits);
  1028. state->back += last.bits;
  1029. }
  1030. DROPBITS(here.bits);
  1031. state->back += here.bits;
  1032. if (here.op & 64) {
  1033. strm->msg = (char *)"invalid distance code";
  1034. state->mode = BAD;
  1035. break;
  1036. }
  1037. state->offset = (unsigned)here.val;
  1038. state->extra = (unsigned)(here.op) & 15;
  1039. state->mode = DISTEXT;
  1040. case DISTEXT:
  1041. if (state->extra) {
  1042. NEEDBITS(state->extra);
  1043. state->offset += BITS(state->extra);
  1044. DROPBITS(state->extra);
  1045. state->back += state->extra;
  1046. }
  1047. #ifdef INFLATE_STRICT
  1048. if (state->offset > state->dmax) {
  1049. strm->msg = (char *)"invalid distance too far back";
  1050. state->mode = BAD;
  1051. break;
  1052. }
  1053. #endif
  1054. Tracevv((stderr, "inflate: distance %u\n", state->offset));
  1055. state->mode = MATCH;
  1056. case MATCH:
  1057. if (left == 0) goto inf_leave;
  1058. copy = out - left;
  1059. if (state->offset > copy) { /* copy from window */
  1060. copy = state->offset - copy;
  1061. if (copy > state->whave) {
  1062. if (state->sane) {
  1063. strm->msg = (char *)"invalid distance too far back";
  1064. state->mode = BAD;
  1065. break;
  1066. }
  1067. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1068. Trace((stderr, "inflate.c too far\n"));
  1069. copy -= state->whave;
  1070. if (copy > state->length) copy = state->length;
  1071. if (copy > left) copy = left;
  1072. left -= copy;
  1073. state->length -= copy;
  1074. do {
  1075. *put++ = 0;
  1076. } while (--copy);
  1077. if (state->length == 0) state->mode = LEN;
  1078. break;
  1079. #endif
  1080. }
  1081. if (copy > state->wnext) {
  1082. copy -= state->wnext;
  1083. from = state->window + (state->wsize - copy);
  1084. }
  1085. else
  1086. from = state->window + (state->wnext - copy);
  1087. if (copy > state->length) copy = state->length;
  1088. }
  1089. else { /* copy from output */
  1090. from = put - state->offset;
  1091. copy = state->length;
  1092. }
  1093. if (copy > left) copy = left;
  1094. left -= copy;
  1095. state->length -= copy;
  1096. do {
  1097. *put++ = *from++;
  1098. } while (--copy);
  1099. if (state->length == 0) state->mode = LEN;
  1100. break;
  1101. case LIT:
  1102. if (left == 0) goto inf_leave;
  1103. *put++ = (unsigned char)(state->length);
  1104. left--;
  1105. state->mode = LEN;
  1106. break;
  1107. case CHECK:
  1108. if (state->wrap) {
  1109. NEEDBITS(32);
  1110. out -= left;
  1111. strm->total_out += out;
  1112. state->total += out;
  1113. if (out)
  1114. strm->adler = state->check =
  1115. UPDATE(state->check, put - out, out);
  1116. out = left;
  1117. if ((
  1118. #ifdef GUNZIP
  1119. state->flags ? hold :
  1120. #endif
  1121. ZSWAP32(hold)) != state->check) {
  1122. strm->msg = (char *)"incorrect data check";
  1123. state->mode = BAD;
  1124. break;
  1125. }
  1126. INITBITS();
  1127. Tracev((stderr, "inflate: check matches trailer\n"));
  1128. }
  1129. #ifdef GUNZIP
  1130. state->mode = LENGTH;
  1131. case LENGTH:
  1132. if (state->wrap && state->flags) {
  1133. NEEDBITS(32);
  1134. if (hold != (state->total & 0xffffffffUL)) {
  1135. strm->msg = (char *)"incorrect length check";
  1136. state->mode = BAD;
  1137. break;
  1138. }
  1139. INITBITS();
  1140. Tracev((stderr, "inflate: length matches trailer\n"));
  1141. }
  1142. #endif
  1143. state->mode = DONE;
  1144. case DONE:
  1145. ret = Z_STREAM_END;
  1146. goto inf_leave;
  1147. case BAD:
  1148. ret = Z_DATA_ERROR;
  1149. goto inf_leave;
  1150. case MEM:
  1151. return Z_MEM_ERROR;
  1152. case SYNC:
  1153. default:
  1154. return Z_STREAM_ERROR;
  1155. }
  1156. /*
  1157. Return from inflate(), updating the total counts and the check value.
  1158. If there was no progress during the inflate() call, return a buffer
  1159. error. Call updatewindow() to create and/or update the window state.
  1160. Note: a memory error from inflate() is non-recoverable.
  1161. */
  1162. inf_leave:
  1163. RESTORE();
  1164. if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
  1165. (state->mode < CHECK || flush != Z_FINISH)))
  1166. if (updatewindow(strm, out)) {
  1167. state->mode = MEM;
  1168. return Z_MEM_ERROR;
  1169. }
  1170. in -= strm->avail_in;
  1171. out -= strm->avail_out;
  1172. strm->total_in += in;
  1173. strm->total_out += out;
  1174. state->total += out;
  1175. if (state->wrap && out)
  1176. strm->adler = state->check =
  1177. UPDATE(state->check, strm->next_out - out, out);
  1178. strm->data_type = state->bits + (state->last ? 64 : 0) +
  1179. (state->mode == TYPE ? 128 : 0) +
  1180. (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
  1181. if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  1182. ret = Z_BUF_ERROR;
  1183. return ret;
  1184. }
  1185. int ZEXPORT inflateEnd(strm)
  1186. z_streamp strm;
  1187. {
  1188. struct inflate_state FAR *state;
  1189. if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
  1190. return Z_STREAM_ERROR;
  1191. state = (struct inflate_state FAR *)strm->state;
  1192. if (state->window != Z_NULL) ZFREE(strm, state->window);
  1193. ZFREE(strm, strm->state);
  1194. strm->state = Z_NULL;
  1195. Tracev((stderr, "inflate: end\n"));
  1196. return Z_OK;
  1197. }
  1198. int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
  1199. z_streamp strm;
  1200. const Bytef *dictionary;
  1201. uInt dictLength;
  1202. {
  1203. struct inflate_state FAR *state;
  1204. unsigned long dictid;
  1205. unsigned char *next;
  1206. unsigned avail;
  1207. int ret;
  1208. /* check state */
  1209. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1210. state = (struct inflate_state FAR *)strm->state;
  1211. if (state->wrap != 0 && state->mode != DICT)
  1212. return Z_STREAM_ERROR;
  1213. /* check for correct dictionary identifier */
  1214. if (state->mode == DICT) {
  1215. dictid = adler32(0L, Z_NULL, 0);
  1216. dictid = adler32(dictid, dictionary, dictLength);
  1217. if (dictid != state->check)
  1218. return Z_DATA_ERROR;
  1219. }
  1220. /* copy dictionary to window using updatewindow(), which will amend the
  1221. existing dictionary if appropriate */
  1222. next = strm->next_out;
  1223. avail = strm->avail_out;
  1224. strm->next_out = (Bytef *)dictionary + dictLength;
  1225. strm->avail_out = 0;
  1226. ret = updatewindow(strm, dictLength);
  1227. strm->avail_out = avail;
  1228. strm->next_out = next;
  1229. if (ret) {
  1230. state->mode = MEM;
  1231. return Z_MEM_ERROR;
  1232. }
  1233. state->havedict = 1;
  1234. Tracev((stderr, "inflate: dictionary set\n"));
  1235. return Z_OK;
  1236. }
  1237. int ZEXPORT inflateGetHeader(strm, head)
  1238. z_streamp strm;
  1239. gz_headerp head;
  1240. {
  1241. struct inflate_state FAR *state;
  1242. /* check state */
  1243. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1244. state = (struct inflate_state FAR *)strm->state;
  1245. if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
  1246. /* save header structure */
  1247. state->head = head;
  1248. head->done = 0;
  1249. return Z_OK;
  1250. }
  1251. /*
  1252. Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
  1253. or when out of input. When called, *have is the number of pattern bytes
  1254. found in order so far, in 0..3. On return *have is updated to the new
  1255. state. If on return *have equals four, then the pattern was found and the
  1256. return value is how many bytes were read including the last byte of the
  1257. pattern. If *have is less than four, then the pattern has not been found
  1258. yet and the return value is len. In the latter case, syncsearch() can be
  1259. called again with more data and the *have state. *have is initialized to
  1260. zero for the first call.
  1261. */
  1262. local unsigned syncsearch(have, buf, len)
  1263. unsigned FAR *have;
  1264. unsigned char FAR *buf;
  1265. unsigned len;
  1266. {
  1267. unsigned got;
  1268. unsigned next;
  1269. got = *have;
  1270. next = 0;
  1271. while (next < len && got < 4) {
  1272. if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
  1273. got++;
  1274. else if (buf[next])
  1275. got = 0;
  1276. else
  1277. got = 4 - got;
  1278. next++;
  1279. }
  1280. *have = got;
  1281. return next;
  1282. }
  1283. int ZEXPORT inflateSync(strm)
  1284. z_streamp strm;
  1285. {
  1286. unsigned len; /* number of bytes to look at or looked at */
  1287. unsigned long in, out; /* temporary to save total_in and total_out */
  1288. unsigned char buf[4]; /* to restore bit buffer to byte string */
  1289. struct inflate_state FAR *state;
  1290. /* check parameters */
  1291. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1292. state = (struct inflate_state FAR *)strm->state;
  1293. if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
  1294. /* if first time, start search in bit buffer */
  1295. if (state->mode != SYNC) {
  1296. state->mode = SYNC;
  1297. state->hold <<= state->bits & 7;
  1298. state->bits -= state->bits & 7;
  1299. len = 0;
  1300. while (state->bits >= 8) {
  1301. buf[len++] = (unsigned char)(state->hold);
  1302. state->hold >>= 8;
  1303. state->bits -= 8;
  1304. }
  1305. state->have = 0;
  1306. syncsearch(&(state->have), buf, len);
  1307. }
  1308. /* search available input */
  1309. len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
  1310. strm->avail_in -= len;
  1311. strm->next_in += len;
  1312. strm->total_in += len;
  1313. /* return no joy or set up to restart inflate() on a new block */
  1314. if (state->have != 4) return Z_DATA_ERROR;
  1315. in = strm->total_in; out = strm->total_out;
  1316. inflateReset(strm);
  1317. strm->total_in = in; strm->total_out = out;
  1318. state->mode = TYPE;
  1319. return Z_OK;
  1320. }
  1321. /*
  1322. Returns true if inflate is currently at the end of a block generated by
  1323. Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1324. implementation to provide an additional safety check. PPP uses
  1325. Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  1326. block. When decompressing, PPP checks that at the end of input packet,
  1327. inflate is waiting for these length bytes.
  1328. */
  1329. int ZEXPORT inflateSyncPoint(strm)
  1330. z_streamp strm;
  1331. {
  1332. struct inflate_state FAR *state;
  1333. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1334. state = (struct inflate_state FAR *)strm->state;
  1335. return state->mode == STORED && state->bits == 0;
  1336. }
  1337. int ZEXPORT inflateCopy(dest, source)
  1338. z_streamp dest;
  1339. z_streamp source;
  1340. {
  1341. struct inflate_state FAR *state;
  1342. struct inflate_state FAR *copy;
  1343. unsigned char FAR *window;
  1344. unsigned wsize;
  1345. /* check input */
  1346. if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
  1347. source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
  1348. return Z_STREAM_ERROR;
  1349. state = (struct inflate_state FAR *)source->state;
  1350. /* allocate space */
  1351. copy = (struct inflate_state FAR *)
  1352. ZALLOC(source, 1, sizeof(struct inflate_state));
  1353. if (copy == Z_NULL) return Z_MEM_ERROR;
  1354. window = Z_NULL;
  1355. if (state->window != Z_NULL) {
  1356. window = (unsigned char FAR *)
  1357. ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
  1358. if (window == Z_NULL) {
  1359. ZFREE(source, copy);
  1360. return Z_MEM_ERROR;
  1361. }
  1362. }
  1363. /* copy state */
  1364. zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
  1365. zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
  1366. if (state->lencode >= state->codes &&
  1367. state->lencode <= state->codes + ENOUGH - 1) {
  1368. copy->lencode = copy->codes + (state->lencode - state->codes);
  1369. copy->distcode = copy->codes + (state->distcode - state->codes);
  1370. }
  1371. copy->next = copy->codes + (state->next - state->codes);
  1372. if (window != Z_NULL) {
  1373. wsize = 1U << state->wbits;
  1374. zmemcpy(window, state->window, wsize);
  1375. }
  1376. copy->window = window;
  1377. dest->state = (struct internal_state FAR *)copy;
  1378. return Z_OK;
  1379. }
  1380. int ZEXPORT inflateUndermine(strm, subvert)
  1381. z_streamp strm;
  1382. int subvert;
  1383. {
  1384. struct inflate_state FAR *state;
  1385. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  1386. state = (struct inflate_state FAR *)strm->state;
  1387. state->sane = !subvert;
  1388. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1389. return Z_OK;
  1390. #else
  1391. state->sane = 1;
  1392. return Z_DATA_ERROR;
  1393. #endif
  1394. }
  1395. long ZEXPORT inflateMark(strm)
  1396. z_streamp strm;
  1397. {
  1398. struct inflate_state FAR *state;
  1399. if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
  1400. state = (struct inflate_state FAR *)strm->state;
  1401. return ((long)(state->back) << 16) +
  1402. (state->mode == COPY ? state->length :
  1403. (state->mode == MATCH ? state->was - state->length : 0));
  1404. }