2
0

inflate.c 53 KB

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