inflate.c 49 KB

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