inflate.c 54 KB

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