1
0

inflate.c 55 KB

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