inflate.c 54 KB

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