inflate.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /* inflate.c -- zlib decompression
  2. * Copyright (C) 1995-2005 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. local void fixedtables OF((struct inflate_state FAR *state));
  6. local int updatewindow OF((z_streamp strm, unsigned out));
  7. int ZEXPORT inflateReset(z_streamp strm)
  8. {
  9. struct inflate_state FAR *state;
  10. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  11. state = (struct inflate_state FAR *)strm->state;
  12. strm->total_in = strm->total_out = state->total = 0;
  13. strm->msg = Z_NULL;
  14. strm->adler = 1; /* to support ill-conceived Java test suite */
  15. state->mode = HEAD;
  16. state->last = 0;
  17. state->havedict = 0;
  18. state->dmax = 32768U;
  19. state->head = Z_NULL;
  20. state->wsize = 0;
  21. state->whave = 0;
  22. state->write = 0;
  23. state->hold = 0;
  24. state->bits = 0;
  25. state->lencode = state->distcode = state->next = state->codes;
  26. schedule();
  27. Tracev((stderr, "inflate: reset\n"));
  28. return Z_OK;
  29. }
  30. int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version,
  31. int stream_size)
  32. {
  33. struct inflate_state FAR *state;
  34. /* if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  35. stream_size != (int)(sizeof(z_stream)))
  36. return Z_VERSION_ERROR;
  37. */
  38. if (strm == Z_NULL) return Z_STREAM_ERROR;
  39. strm->msg = Z_NULL; /* in case we return an error */
  40. if (strm->zalloc == (alloc_func)0) {
  41. strm->zalloc = zcalloc;
  42. strm->opaque = (voidpf)0;
  43. }
  44. if (strm->zfree == (free_func)0) strm->zfree = zcfree;
  45. state = (struct inflate_state FAR *)
  46. ZALLOC(strm, 1, sizeof(struct inflate_state));
  47. if (state == Z_NULL) return Z_MEM_ERROR;
  48. Tracev((stderr, "inflate: allocated\n"));
  49. strm->state = (struct internal_state FAR *)state;
  50. if (windowBits < 0) {
  51. state->wrap = 0;
  52. windowBits = -windowBits;
  53. }
  54. else {
  55. state->wrap = (windowBits >> 4) + 1;
  56. #ifdef GUNZIP
  57. if (windowBits < 48) windowBits &= 15;
  58. #endif
  59. }
  60. if (windowBits < 8 || windowBits > 15) {
  61. ZFREE(strm, state);
  62. strm->state = Z_NULL;
  63. return Z_STREAM_ERROR;
  64. }
  65. state->wbits = (unsigned)windowBits;
  66. state->window = Z_NULL;
  67. return inflateReset(strm);
  68. }
  69. int ZEXPORT inflateInit_(z_streamp strm, const char *version, int stream_size)
  70. {
  71. return inflateInit2_(strm, DEF_WBITS, version, stream_size);
  72. }
  73. local void fixedtables(struct inflate_state FAR *state)
  74. {
  75. state->lencode = lenfix;
  76. state->lenbits = 9;
  77. state->distcode = distfix;
  78. state->distbits = 5;
  79. }
  80. /*
  81. Update the window with the last wsize (normally 32K) bytes written before
  82. returning. If window does not exist yet, create it. This is only called
  83. when a window is already in use, or when output has been written during this
  84. inflate call, but the end of the deflate stream has not been reached yet.
  85. It is also called to create a window for dictionary data when a dictionary
  86. is loaded.
  87. Providing output buffers larger than 32K to inflate() should provide a speed
  88. advantage, since only the last 32K of output is copied to the sliding window
  89. upon return from inflate(), and since all distances after the first 32K of
  90. output will fall in the output data, making match copies simpler and faster.
  91. The advantage may be dependent on the size of the processor's data caches.
  92. */
  93. local int updatewindow(z_streamp strm, unsigned out)
  94. {
  95. struct inflate_state FAR *state;
  96. unsigned copy, dist;
  97. state = (struct inflate_state FAR *)strm->state;
  98. /* if it hasn't been done already, allocate space for the window */
  99. if (state->window == Z_NULL) {
  100. state->window = (unsigned char FAR *)
  101. ZALLOC(strm, 1U << state->wbits,
  102. sizeof(unsigned char));
  103. if (state->window == Z_NULL) return 1;
  104. }
  105. /* if window not in use yet, initialize */
  106. if (state->wsize == 0) {
  107. state->wsize = 1U << state->wbits;
  108. state->write = 0;
  109. state->whave = 0;
  110. }
  111. /* copy state->wsize or less output bytes into the circular window */
  112. copy = out - strm->avail_out;
  113. if (copy >= state->wsize) {
  114. zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
  115. state->write = 0;
  116. state->whave = state->wsize;
  117. }
  118. else {
  119. dist = state->wsize - state->write;
  120. if (dist > copy) dist = copy;
  121. zmemcpy(state->window + state->write, strm->next_out - copy, dist);
  122. copy -= dist;
  123. if (copy) {
  124. zmemcpy(state->window, strm->next_out - copy, copy);
  125. state->write = copy;
  126. state->whave = state->wsize;
  127. }
  128. else {
  129. state->write += dist;
  130. if (state->write == state->wsize) state->write = 0;
  131. if (state->whave < state->wsize) state->whave += dist;
  132. }
  133. }
  134. return 0;
  135. }
  136. /* Macros for inflate(): */
  137. /* check function to use adler32() for zlib or crc32() for gzip */
  138. #ifdef GUNZIP
  139. # define UPDATE(check, buf, len) \
  140. (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
  141. #else
  142. # define UPDATE(check, buf, len) adler32(check, buf, len)
  143. #endif
  144. /* check macros for header crc */
  145. #ifdef GUNZIP
  146. # define CRC2(check, word) \
  147. do { \
  148. hbuf[0] = (unsigned char)(word); \
  149. hbuf[1] = (unsigned char)((word) >> 8); \
  150. check = crc32(check, hbuf, 2); \
  151. } while (0)
  152. # define CRC4(check, word) \
  153. do { \
  154. hbuf[0] = (unsigned char)(word); \
  155. hbuf[1] = (unsigned char)((word) >> 8); \
  156. hbuf[2] = (unsigned char)((word) >> 16); \
  157. hbuf[3] = (unsigned char)((word) >> 24); \
  158. check = crc32(check, hbuf, 4); \
  159. } while (0)
  160. #endif
  161. /* Load registers with state in inflate() for speed */
  162. #define LOAD() \
  163. do { \
  164. put = strm->next_out; \
  165. left = strm->avail_out; \
  166. next = strm->next_in; \
  167. have = strm->avail_in; \
  168. hold = state->hold; \
  169. bits = state->bits; \
  170. } while (0)
  171. /* Restore state from registers in inflate() */
  172. #define RESTORE() \
  173. do { \
  174. strm->next_out = put; \
  175. strm->avail_out = left; \
  176. strm->next_in = next; \
  177. strm->avail_in = have; \
  178. state->hold = hold; \
  179. state->bits = bits; \
  180. } while (0)
  181. /* Clear the input bit accumulator */
  182. #define INITBITS() \
  183. do { \
  184. hold = 0; \
  185. bits = 0; \
  186. } while (0)
  187. /* Get a byte of input into the bit accumulator, or return from inflate()
  188. if there is no input available. */
  189. #define PULLBYTE() \
  190. do { \
  191. if (have == 0) goto inf_leave; \
  192. have--; \
  193. hold += (unsigned long)(*next++) << bits; \
  194. bits += 8; \
  195. } while (0)
  196. /* Assure that there are at least n bits in the bit accumulator. If there is
  197. not enough available input to do that, then return from inflate(). */
  198. #define NEEDBITS(n) \
  199. do { \
  200. while (bits < (unsigned)(n)) \
  201. PULLBYTE(); \
  202. } while (0)
  203. /* Return the low n bits of the bit accumulator (n < 16) */
  204. #define BITS(n) \
  205. ((unsigned)hold & ((1U << (n)) - 1))
  206. /* Remove n bits from the bit accumulator */
  207. #define DROPBITS(n) \
  208. do { \
  209. hold >>= (n); \
  210. bits -= (unsigned)(n); \
  211. } while (0)
  212. /* Remove zero to seven bits as needed to go to a byte boundary */
  213. #define BYTEBITS() \
  214. do { \
  215. hold >>= bits & 7; \
  216. bits -= bits & 7; \
  217. } while (0)
  218. /* Reverse the bytes in a 32-bit value */
  219. #define REVERSE(q) \
  220. ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
  221. (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
  222. /*
  223. inflate() uses a state machine to process as much input data and generate as
  224. much output data as possible before returning. The state machine is
  225. structured roughly as follows:
  226. for (;;) switch (state) {
  227. ...
  228. case STATEn:
  229. if (not enough input data or output space to make progress)
  230. return;
  231. ... make progress ...
  232. state = STATEm;
  233. break;
  234. ...
  235. }
  236. so when inflate() is called again, the same case is attempted again, and
  237. if the appropriate resources are provided, the machine proceeds to the
  238. next state. The NEEDBITS() macro is usually the way the state evaluates
  239. whether it can proceed or should return. NEEDBITS() does the return if
  240. the requested bits are not available. The typical use of the BITS macros
  241. is:
  242. NEEDBITS(n);
  243. ... do something with BITS(n) ...
  244. DROPBITS(n);
  245. where NEEDBITS(n) either returns from inflate() if there isn't enough
  246. input left to load n bits into the accumulator, or it continues. BITS(n)
  247. gives the low n bits in the accumulator. When done, DROPBITS(n) drops
  248. the low n bits off the accumulator. INITBITS() clears the accumulator
  249. and sets the number of available bits to zero. BYTEBITS() discards just
  250. enough bits to put the accumulator on a byte boundary. After BYTEBITS()
  251. and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
  252. NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
  253. if there is no input available. The decoding of variable length codes uses
  254. PULLBYTE() directly in order to pull just enough bytes to decode the next
  255. code, and no more.
  256. Some states loop until they get enough input, making sure that enough
  257. state information is maintained to continue the loop where it left off
  258. if NEEDBITS() returns in the loop. For example, want, need, and keep
  259. would all have to actually be part of the saved state in case NEEDBITS()
  260. returns:
  261. case STATEw:
  262. while (want < need) {
  263. NEEDBITS(n);
  264. keep[want++] = BITS(n);
  265. DROPBITS(n);
  266. }
  267. state = STATEx;
  268. case STATEx:
  269. As shown above, if the next state is also the next case, then the break
  270. is omitted.
  271. A state may also return if there is not enough output space available to
  272. complete that state. Those states are copying stored data, writing a
  273. literal byte, and copying a matching string.
  274. When returning, a "goto inf_leave" is used to update the total counters,
  275. update the check value, and determine whether any progress has been made
  276. during that inflate() call in order to return the proper return code.
  277. Progress is defined as a change in either strm->avail_in or strm->avail_out.
  278. When there is a window, goto inf_leave will update the window with the last
  279. output written. If a goto inf_leave occurs in the middle of decompression
  280. and there is no window currently, goto inf_leave will create one and copy
  281. output to the window for the next call of inflate().
  282. In this implementation, the flush parameter of inflate() only affects the
  283. return code (per zlib.h). inflate() always writes as much as possible to
  284. strm->next_out, given the space available and the provided input--the effect
  285. documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
  286. the allocation of and copying into a sliding window until necessary, which
  287. provides the effect documented in zlib.h for Z_FINISH when the entire input
  288. stream available. So the only thing the flush parameter actually does is:
  289. when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
  290. will return Z_BUF_ERROR if it has not reached the end of the stream.
  291. */
  292. int ZEXPORT inflate(z_streamp strm, int flush)
  293. {
  294. struct inflate_state FAR *state;
  295. unsigned char FAR *next; /* next input */
  296. unsigned char FAR *put; /* next output */
  297. unsigned have, left; /* available input and output */
  298. unsigned long hold; /* bit buffer */
  299. unsigned bits; /* bits in bit buffer */
  300. unsigned in, out; /* save starting available input and output */
  301. unsigned copy; /* number of stored or match bytes to copy */
  302. unsigned char FAR *from; /* where to copy match bytes from */
  303. code this; /* current decoding table entry */
  304. code last; /* parent table entry */
  305. unsigned len; /* length to copy for repeats, bits to drop */
  306. int ret; /* return code */
  307. #ifdef GUNZIP
  308. unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
  309. #endif
  310. static const unsigned short order[19] = /* permutation of code lengths */
  311. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  312. if (strm == Z_NULL || strm->state == Z_NULL ||
  313. (strm->next_in == Z_NULL && strm->avail_in != 0))
  314. return Z_STREAM_ERROR;
  315. state = (struct inflate_state FAR *)strm->state;
  316. if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
  317. LOAD();
  318. in = have;
  319. out = left;
  320. ret = Z_OK;
  321. for (;;)
  322. switch (state->mode) {
  323. case HEAD:
  324. if (state->wrap == 0) {
  325. state->mode = TYPEDO;
  326. break;
  327. }
  328. NEEDBITS(16);
  329. #ifdef GUNZIP
  330. if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
  331. state->check = crc32(0L, Z_NULL, 0);
  332. CRC2(state->check, hold);
  333. INITBITS();
  334. state->mode = FLAGS;
  335. break;
  336. }
  337. state->flags = 0; /* expect zlib header */
  338. if (state->head != Z_NULL)
  339. state->head->done = -1;
  340. if (!(state->wrap & 1) || /* check if zlib header allowed */
  341. #else
  342. if (
  343. #endif
  344. ((BITS(8) << 8) + (hold >> 8)) % 31) {
  345. strm->msg = (char *)"incorrect header check";
  346. state->mode = BAD;
  347. break;
  348. }
  349. if (BITS(4) != Z_DEFLATED) {
  350. strm->msg = (char *)"unknown compression method";
  351. state->mode = BAD;
  352. break;
  353. }
  354. DROPBITS(4);
  355. len = BITS(4) + 8;
  356. if (len > state->wbits) {
  357. strm->msg = (char *)"invalid window size";
  358. state->mode = BAD;
  359. break;
  360. }
  361. state->dmax = 1U << len;
  362. Tracev((stderr, "inflate: zlib header ok\n"));
  363. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  364. state->mode = hold & 0x200 ? DICTID : TYPE;
  365. INITBITS();
  366. break;
  367. #ifdef GUNZIP
  368. case FLAGS:
  369. NEEDBITS(16);
  370. state->flags = (int)(hold);
  371. if ((state->flags & 0xff) != Z_DEFLATED) {
  372. strm->msg = (char *)"unknown compression method";
  373. state->mode = BAD;
  374. break;
  375. }
  376. if (state->flags & 0xe000) {
  377. strm->msg = (char *)"unknown header flags set";
  378. state->mode = BAD;
  379. break;
  380. }
  381. if (state->head != Z_NULL)
  382. state->head->text = (int)((hold >> 8) & 1);
  383. if (state->flags & 0x0200) CRC2(state->check, hold);
  384. INITBITS();
  385. state->mode = TIME;
  386. case TIME:
  387. NEEDBITS(32);
  388. if (state->head != Z_NULL)
  389. state->head->time = hold;
  390. if (state->flags & 0x0200) CRC4(state->check, hold);
  391. INITBITS();
  392. state->mode = OS;
  393. case OS:
  394. NEEDBITS(16);
  395. if (state->head != Z_NULL) {
  396. state->head->xflags = (int)(hold & 0xff);
  397. state->head->os = (int)(hold >> 8);
  398. }
  399. if (state->flags & 0x0200) CRC2(state->check, hold);
  400. INITBITS();
  401. state->mode = EXLEN;
  402. case EXLEN:
  403. if (state->flags & 0x0400) {
  404. NEEDBITS(16);
  405. state->length = (unsigned)(hold);
  406. if (state->head != Z_NULL)
  407. state->head->extra_len = (unsigned)hold;
  408. if (state->flags & 0x0200) CRC2(state->check, hold);
  409. INITBITS();
  410. }
  411. else if (state->head != Z_NULL)
  412. state->head->extra = Z_NULL;
  413. state->mode = EXTRA;
  414. case EXTRA:
  415. if (state->flags & 0x0400) {
  416. copy = state->length;
  417. if (copy > have) copy = have;
  418. if (copy) {
  419. if (state->head != Z_NULL &&
  420. state->head->extra != Z_NULL &&
  421. (len = state->head->extra_len - state->length) <
  422. state->head->extra_max) {
  423. zmemcpy(state->head->extra + len, next,
  424. len + copy > state->head->extra_max ?
  425. state->head->extra_max - len : copy);
  426. }
  427. if (state->flags & 0x0200)
  428. state->check = crc32(state->check, next, copy);
  429. have -= copy;
  430. next += copy;
  431. state->length -= copy;
  432. }
  433. if (state->length) goto inf_leave;
  434. }
  435. state->length = 0;
  436. state->mode = NAME;
  437. case NAME:
  438. if (state->flags & 0x0800) {
  439. if (have == 0) goto inf_leave;
  440. copy = 0;
  441. do {
  442. len = (unsigned)(next[copy++]);
  443. if (state->head != Z_NULL &&
  444. state->head->name != Z_NULL &&
  445. state->length < state->head->name_max)
  446. state->head->name[state->length++] = len;
  447. } while (len && copy < have);
  448. if (state->flags & 0x0200)
  449. state->check = crc32(state->check, next, copy);
  450. have -= copy;
  451. next += copy;
  452. if (len) goto inf_leave;
  453. }
  454. else if (state->head != Z_NULL)
  455. state->head->name = Z_NULL;
  456. state->length = 0;
  457. state->mode = COMMENT;
  458. case COMMENT:
  459. if (state->flags & 0x1000) {
  460. if (have == 0) goto inf_leave;
  461. copy = 0;
  462. do {
  463. len = (unsigned)(next[copy++]);
  464. if (state->head != Z_NULL &&
  465. state->head->comment != Z_NULL &&
  466. state->length < state->head->comm_max)
  467. state->head->comment[state->length++] = len;
  468. } while (len && copy < have);
  469. if (state->flags & 0x0200)
  470. state->check = crc32(state->check, next, copy);
  471. have -= copy;
  472. next += copy;
  473. if (len) goto inf_leave;
  474. }
  475. else if (state->head != Z_NULL)
  476. state->head->comment = Z_NULL;
  477. state->mode = HCRC;
  478. case HCRC:
  479. if (state->flags & 0x0200) {
  480. NEEDBITS(16);
  481. if (hold != (state->check & 0xffff)) {
  482. strm->msg = (char *)"header crc mismatch";
  483. state->mode = BAD;
  484. break;
  485. }
  486. INITBITS();
  487. }
  488. if (state->head != Z_NULL) {
  489. state->head->hcrc = (int)((state->flags >> 9) & 1);
  490. state->head->done = 1;
  491. }
  492. strm->adler = state->check = crc32(0L, Z_NULL, 0);
  493. state->mode = TYPE;
  494. break;
  495. #endif
  496. case DICTID:
  497. NEEDBITS(32);
  498. strm->adler = state->check = REVERSE(hold);
  499. INITBITS();
  500. state->mode = DICT;
  501. case DICT:
  502. if (state->havedict == 0) {
  503. RESTORE();
  504. return Z_NEED_DICT;
  505. }
  506. strm->adler = state->check = adler32(0L, Z_NULL, 0);
  507. state->mode = TYPE;
  508. case TYPE:
  509. schedule();
  510. if (flush == Z_BLOCK) goto inf_leave;
  511. case TYPEDO:
  512. if (state->last) {
  513. BYTEBITS();
  514. state->mode = CHECK;
  515. break;
  516. }
  517. NEEDBITS(3);
  518. state->last = BITS(1);
  519. DROPBITS(1);
  520. switch (BITS(2)) {
  521. case 0: /* stored block */
  522. Tracev((stderr, "inflate: stored block%s\n",
  523. state->last ? " (last)" : ""));
  524. state->mode = STORED;
  525. break;
  526. case 1: /* fixed block */
  527. fixedtables(state);
  528. Tracev((stderr, "inflate: fixed codes block%s\n",
  529. state->last ? " (last)" : ""));
  530. state->mode = LEN; /* decode codes */
  531. break;
  532. case 2: /* dynamic block */
  533. Tracev((stderr, "inflate: dynamic codes block%s\n",
  534. state->last ? " (last)" : ""));
  535. state->mode = TABLE;
  536. break;
  537. case 3:
  538. strm->msg = (char *)"invalid block type";
  539. state->mode = BAD;
  540. }
  541. DROPBITS(2);
  542. break;
  543. case STORED:
  544. BYTEBITS(); /* go to byte boundary */
  545. NEEDBITS(32);
  546. if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  547. strm->msg = (char *)"invalid stored block lengths";
  548. state->mode = BAD;
  549. break;
  550. }
  551. state->length = (unsigned)hold & 0xffff;
  552. Tracev((stderr, "inflate: stored length %u\n",
  553. state->length));
  554. INITBITS();
  555. state->mode = COPY;
  556. case COPY:
  557. copy = state->length;
  558. if (copy) {
  559. if (copy > have) copy = have;
  560. if (copy > left) copy = left;
  561. if (copy == 0) goto inf_leave;
  562. zmemcpy(put, next, copy);
  563. have -= copy;
  564. next += copy;
  565. left -= copy;
  566. put += copy;
  567. state->length -= copy;
  568. break;
  569. }
  570. Tracev((stderr, "inflate: stored end\n"));
  571. state->mode = TYPE;
  572. break;
  573. case TABLE:
  574. NEEDBITS(14);
  575. state->nlen = BITS(5) + 257;
  576. DROPBITS(5);
  577. state->ndist = BITS(5) + 1;
  578. DROPBITS(5);
  579. state->ncode = BITS(4) + 4;
  580. DROPBITS(4);
  581. #ifndef PKZIP_BUG_WORKAROUND
  582. if (state->nlen > 286 || state->ndist > 30) {
  583. strm->msg = (char *)"too many length or distance symbols";
  584. state->mode = BAD;
  585. break;
  586. }
  587. #endif
  588. Tracev((stderr, "inflate: table sizes ok\n"));
  589. state->have = 0;
  590. state->mode = LENLENS;
  591. case LENLENS:
  592. while (state->have < state->ncode) {
  593. NEEDBITS(3);
  594. state->lens[order[state->have++]] = (unsigned short)BITS(3);
  595. DROPBITS(3);
  596. }
  597. while (state->have < 19)
  598. state->lens[order[state->have++]] = 0;
  599. state->next = state->codes;
  600. state->lencode = (code const FAR *)(state->next);
  601. state->lenbits = 7;
  602. ret = inflate_table(CODES, state->lens, 19, &(state->next),
  603. &(state->lenbits), state->work);
  604. if (ret) {
  605. strm->msg = (char *)"invalid code lengths set";
  606. state->mode = BAD;
  607. break;
  608. }
  609. Tracev((stderr, "inflate: code lengths ok\n"));
  610. state->have = 0;
  611. state->mode = CODELENS;
  612. case CODELENS:
  613. while (state->have < state->nlen + state->ndist) {
  614. for (;;) {
  615. this = state->lencode[BITS(state->lenbits)];
  616. if ((unsigned)(this.bits) <= bits) break;
  617. PULLBYTE();
  618. }
  619. if (this.val < 16) {
  620. NEEDBITS(this.bits);
  621. DROPBITS(this.bits);
  622. state->lens[state->have++] = this.val;
  623. }
  624. else {
  625. if (this.val == 16) {
  626. NEEDBITS(this.bits + 2);
  627. DROPBITS(this.bits);
  628. if (state->have == 0) {
  629. strm->msg = (char *)"invalid bit length repeat";
  630. state->mode = BAD;
  631. break;
  632. }
  633. len = state->lens[state->have - 1];
  634. copy = 3 + BITS(2);
  635. DROPBITS(2);
  636. }
  637. else if (this.val == 17) {
  638. NEEDBITS(this.bits + 3);
  639. DROPBITS(this.bits);
  640. len = 0;
  641. copy = 3 + BITS(3);
  642. DROPBITS(3);
  643. }
  644. else {
  645. NEEDBITS(this.bits + 7);
  646. DROPBITS(this.bits);
  647. len = 0;
  648. copy = 11 + BITS(7);
  649. DROPBITS(7);
  650. }
  651. if (state->have + copy > state->nlen + state->ndist) {
  652. strm->msg = (char *)"invalid bit length repeat";
  653. state->mode = BAD;
  654. break;
  655. }
  656. while (copy--)
  657. state->lens[state->have++] = (unsigned short)len;
  658. }
  659. }
  660. /* handle error breaks in while */
  661. if (state->mode == BAD) break;
  662. /* build code tables */
  663. state->next = state->codes;
  664. state->lencode = (code const FAR *)(state->next);
  665. state->lenbits = 9;
  666. ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
  667. &(state->lenbits), state->work);
  668. if (ret) {
  669. strm->msg = (char *)"invalid literal/lengths set";
  670. state->mode = BAD;
  671. break;
  672. }
  673. state->distcode = (code const FAR *)(state->next);
  674. state->distbits = 6;
  675. ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  676. &(state->next), &(state->distbits), state->work);
  677. if (ret) {
  678. strm->msg = (char *)"invalid distances set";
  679. state->mode = BAD;
  680. break;
  681. }
  682. Tracev((stderr, "inflate: codes ok\n"));
  683. state->mode = LEN;
  684. case LEN:
  685. schedule();
  686. if (have >= 6 && left >= 258) {
  687. RESTORE();
  688. inflate_fast(strm, out);
  689. LOAD();
  690. break;
  691. }
  692. for (;;) {
  693. this = state->lencode[BITS(state->lenbits)];
  694. if ((unsigned)(this.bits) <= bits) break;
  695. PULLBYTE();
  696. }
  697. if (this.op && (this.op & 0xf0) == 0) {
  698. last = this;
  699. for (;;) {
  700. this = state->lencode[last.val +
  701. (BITS(last.bits + last.op) >> last.bits)];
  702. if ((unsigned)(last.bits + this.bits) <= bits) break;
  703. PULLBYTE();
  704. }
  705. DROPBITS(last.bits);
  706. }
  707. DROPBITS(this.bits);
  708. state->length = (unsigned)this.val;
  709. if ((int)(this.op) == 0) {
  710. Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
  711. "inflate: literal '%c'\n" :
  712. "inflate: literal 0x%02x\n", this.val));
  713. state->mode = LIT;
  714. break;
  715. }
  716. if (this.op & 32) {
  717. Tracevv((stderr, "inflate: end of block\n"));
  718. state->mode = TYPE;
  719. break;
  720. }
  721. if (this.op & 64) {
  722. strm->msg = (char *)"invalid literal/length code";
  723. state->mode = BAD;
  724. break;
  725. }
  726. state->extra = (unsigned)(this.op) & 15;
  727. state->mode = LENEXT;
  728. case LENEXT:
  729. if (state->extra) {
  730. NEEDBITS(state->extra);
  731. state->length += BITS(state->extra);
  732. DROPBITS(state->extra);
  733. }
  734. Tracevv((stderr, "inflate: length %u\n", state->length));
  735. state->mode = DIST;
  736. case DIST:
  737. for (;;) {
  738. this = state->distcode[BITS(state->distbits)];
  739. if ((unsigned)(this.bits) <= bits) break;
  740. PULLBYTE();
  741. }
  742. if ((this.op & 0xf0) == 0) {
  743. last = this;
  744. for (;;) {
  745. this = state->distcode[last.val +
  746. (BITS(last.bits + last.op) >> last.bits)];
  747. if ((unsigned)(last.bits + this.bits) <= bits) break;
  748. PULLBYTE();
  749. }
  750. DROPBITS(last.bits);
  751. }
  752. DROPBITS(this.bits);
  753. if (this.op & 64) {
  754. strm->msg = (char *)"invalid distance code";
  755. state->mode = BAD;
  756. break;
  757. }
  758. state->offset = (unsigned)this.val;
  759. state->extra = (unsigned)(this.op) & 15;
  760. state->mode = DISTEXT;
  761. case DISTEXT:
  762. if (state->extra) {
  763. NEEDBITS(state->extra);
  764. state->offset += BITS(state->extra);
  765. DROPBITS(state->extra);
  766. }
  767. #ifdef INFLATE_STRICT
  768. if (state->offset > state->dmax) {
  769. strm->msg = (char *)"invalid distance too far back";
  770. state->mode = BAD;
  771. break;
  772. }
  773. #endif
  774. if (state->offset > state->whave + out - left) {
  775. strm->msg = (char *)"invalid distance too far back";
  776. state->mode = BAD;
  777. break;
  778. }
  779. Tracevv((stderr, "inflate: distance %u\n", state->offset));
  780. state->mode = MATCH;
  781. case MATCH:
  782. if (left == 0) goto inf_leave;
  783. copy = out - left;
  784. if (state->offset > copy) { /* copy from window */
  785. copy = state->offset - copy;
  786. if (copy > state->write) {
  787. copy -= state->write;
  788. from = state->window + (state->wsize - copy);
  789. }
  790. else
  791. from = state->window + (state->write - copy);
  792. if (copy > state->length) copy = state->length;
  793. }
  794. else { /* copy from output */
  795. from = put - state->offset;
  796. copy = state->length;
  797. }
  798. if (copy > left) copy = left;
  799. left -= copy;
  800. state->length -= copy;
  801. do {
  802. *put++ = *from++;
  803. } while (--copy);
  804. if (state->length == 0) state->mode = LEN;
  805. break;
  806. case LIT:
  807. if (left == 0) goto inf_leave;
  808. *put++ = (unsigned char)(state->length);
  809. left--;
  810. state->mode = LEN;
  811. break;
  812. case CHECK:
  813. if (state->wrap) {
  814. NEEDBITS(32);
  815. out -= left;
  816. strm->total_out += out;
  817. state->total += out;
  818. if (out)
  819. strm->adler = state->check =
  820. UPDATE(state->check, put - out, out);
  821. out = left;
  822. if ((
  823. #ifdef GUNZIP
  824. state->flags ? hold :
  825. #endif
  826. REVERSE(hold)) != state->check) {
  827. strm->msg = (char *)"incorrect data check";
  828. state->mode = BAD;
  829. break;
  830. }
  831. INITBITS();
  832. Tracev((stderr, "inflate: check matches trailer\n"));
  833. }
  834. #ifdef GUNZIP
  835. state->mode = LENGTH;
  836. case LENGTH:
  837. if (state->wrap && state->flags) {
  838. NEEDBITS(32);
  839. if (hold != (state->total & 0xffffffffUL)) {
  840. strm->msg = (char *)"incorrect length check";
  841. state->mode = BAD;
  842. break;
  843. }
  844. INITBITS();
  845. Tracev((stderr, "inflate: length matches trailer\n"));
  846. }
  847. #endif
  848. state->mode = DONE;
  849. case DONE:
  850. ret = Z_STREAM_END;
  851. goto inf_leave;
  852. case BAD:
  853. ret = Z_DATA_ERROR;
  854. goto inf_leave;
  855. case MEM:
  856. return Z_MEM_ERROR;
  857. case SYNC:
  858. default:
  859. return Z_STREAM_ERROR;
  860. }
  861. /*
  862. Return from inflate(), updating the total counts and the check value.
  863. If there was no progress during the inflate() call, return a buffer
  864. error. Call updatewindow() to create and/or update the window state.
  865. Note: a memory error from inflate() is non-recoverable.
  866. */
  867. inf_leave:
  868. RESTORE();
  869. if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
  870. if (updatewindow(strm, out)) {
  871. state->mode = MEM;
  872. return Z_MEM_ERROR;
  873. }
  874. in -= strm->avail_in;
  875. out -= strm->avail_out;
  876. strm->total_in += in;
  877. strm->total_out += out;
  878. state->total += out;
  879. if (state->wrap && out)
  880. strm->adler = state->check =
  881. UPDATE(state->check, strm->next_out - out, out);
  882. strm->data_type = state->bits + (state->last ? 64 : 0) +
  883. (state->mode == TYPE ? 128 : 0);
  884. if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  885. ret = Z_BUF_ERROR;
  886. return ret;
  887. }
  888. int ZEXPORT inflateEnd(z_streamp strm)
  889. {
  890. struct inflate_state FAR *state;
  891. if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
  892. return Z_STREAM_ERROR;
  893. state = (struct inflate_state FAR *)strm->state;
  894. if (state->window != Z_NULL) {
  895. schedule();
  896. ZFREE(strm, state->window);
  897. }
  898. ZFREE(strm, strm->state);
  899. strm->state = Z_NULL;
  900. Tracev((stderr, "inflate: end\n"));
  901. return Z_OK;
  902. }