inflate.c 34 KB

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