inflate.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. * Based on zlib 1.2.3 but modified for the Linux Kernel by
  6. * Richard Purdie <richard@openedhand.com>
  7. *
  8. * Changes mainly for static instead of dynamic memory allocation
  9. *
  10. */
  11. #include <linux/zutil.h>
  12. #include "inftrees.h"
  13. #include "inflate.h"
  14. #include "inffast.h"
  15. #include "infutil.h"
  16. /* architecture-specific bits */
  17. #ifdef CONFIG_ZLIB_DFLTCC
  18. # include "../zlib_dfltcc/dfltcc.h"
  19. #else
  20. #define INFLATE_RESET_HOOK(strm) do {} while (0)
  21. #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
  22. #define INFLATE_NEED_UPDATEWINDOW(strm) 1
  23. #define INFLATE_NEED_CHECKSUM(strm) 1
  24. #endif
  25. int zlib_inflate_workspacesize(void)
  26. {
  27. return sizeof(struct inflate_workspace);
  28. }
  29. int zlib_inflateReset(z_streamp strm)
  30. {
  31. struct inflate_state *state;
  32. if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
  33. state = (struct inflate_state *)strm->state;
  34. strm->total_in = strm->total_out = state->total = 0;
  35. strm->msg = NULL;
  36. strm->adler = 1; /* to support ill-conceived Java test suite */
  37. state->mode = HEAD;
  38. state->last = 0;
  39. state->havedict = 0;
  40. state->dmax = 32768U;
  41. state->hold = 0;
  42. state->bits = 0;
  43. state->lencode = state->distcode = state->next = state->codes;
  44. /* Initialise Window */
  45. state->wsize = 1U << state->wbits;
  46. state->write = 0;
  47. state->whave = 0;
  48. INFLATE_RESET_HOOK(strm);
  49. return Z_OK;
  50. }
  51. int zlib_inflateInit2(z_streamp strm, int windowBits)
  52. {
  53. struct inflate_state *state;
  54. if (strm == NULL) return Z_STREAM_ERROR;
  55. strm->msg = NULL; /* in case we return an error */
  56. state = &WS(strm)->inflate_state;
  57. strm->state = (struct internal_state *)state;
  58. if (windowBits < 0) {
  59. state->wrap = 0;
  60. windowBits = -windowBits;
  61. }
  62. else {
  63. state->wrap = (windowBits >> 4) + 1;
  64. }
  65. if (windowBits < 8 || windowBits > 15) {
  66. return Z_STREAM_ERROR;
  67. }
  68. state->wbits = (unsigned)windowBits;
  69. #ifdef CONFIG_ZLIB_DFLTCC
  70. /*
  71. * DFLTCC requires the window to be page aligned.
  72. * Thus, we overallocate and take the aligned portion of the buffer.
  73. */
  74. state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
  75. #else
  76. state->window = &WS(strm)->working_window[0];
  77. #endif
  78. return zlib_inflateReset(strm);
  79. }
  80. /*
  81. Return state with length and distance decoding tables and index sizes set to
  82. fixed code decoding. This returns fixed tables from inffixed.h.
  83. */
  84. static void zlib_fixedtables(struct inflate_state *state)
  85. {
  86. # include "inffixed.h"
  87. state->lencode = lenfix;
  88. state->lenbits = 9;
  89. state->distcode = distfix;
  90. state->distbits = 5;
  91. }
  92. /*
  93. Update the window with the last wsize (normally 32K) bytes written before
  94. returning. This is only called when a window is already in use, or when
  95. output has been written during this inflate call, but the end of the deflate
  96. stream has not been reached yet. It is also called to window dictionary data
  97. when a dictionary is loaded.
  98. Providing output buffers larger than 32K to inflate() should provide a speed
  99. advantage, since only the last 32K of output is copied to the sliding window
  100. upon return from inflate(), and since all distances after the first 32K of
  101. output will fall in the output data, making match copies simpler and faster.
  102. The advantage may be dependent on the size of the processor's data caches.
  103. */
  104. static void zlib_updatewindow(z_streamp strm, unsigned out)
  105. {
  106. struct inflate_state *state;
  107. unsigned copy, dist;
  108. state = (struct inflate_state *)strm->state;
  109. /* copy state->wsize or less output bytes into the circular window */
  110. copy = out - strm->avail_out;
  111. if (copy >= state->wsize) {
  112. memcpy(state->window, strm->next_out - state->wsize, state->wsize);
  113. state->write = 0;
  114. state->whave = state->wsize;
  115. }
  116. else {
  117. dist = state->wsize - state->write;
  118. if (dist > copy) dist = copy;
  119. memcpy(state->window + state->write, strm->next_out - copy, dist);
  120. copy -= dist;
  121. if (copy) {
  122. memcpy(state->window, strm->next_out - copy, copy);
  123. state->write = copy;
  124. state->whave = state->wsize;
  125. }
  126. else {
  127. state->write += dist;
  128. if (state->write == state->wsize) state->write = 0;
  129. if (state->whave < state->wsize) state->whave += dist;
  130. }
  131. }
  132. }
  133. /*
  134. * At the end of a Deflate-compressed PPP packet, we expect to have seen
  135. * a `stored' block type value but not the (zero) length bytes.
  136. */
  137. /*
  138. Returns true if inflate is currently at the end of a block generated by
  139. Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  140. implementation to provide an additional safety check. PPP uses
  141. Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  142. block. When decompressing, PPP checks that at the end of input packet,
  143. inflate is waiting for these length bytes.
  144. */
  145. static int zlib_inflateSyncPacket(z_streamp strm)
  146. {
  147. struct inflate_state *state;
  148. if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
  149. state = (struct inflate_state *)strm->state;
  150. if (state->mode == STORED && state->bits == 0) {
  151. state->mode = TYPE;
  152. return Z_OK;
  153. }
  154. return Z_DATA_ERROR;
  155. }
  156. /* Macros for inflate(): */
  157. /* check function to use adler32() for zlib or crc32() for gzip */
  158. #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
  159. /* Load registers with state in inflate() for speed */
  160. #define LOAD() \
  161. do { \
  162. put = strm->next_out; \
  163. left = strm->avail_out; \
  164. next = strm->next_in; \
  165. have = strm->avail_in; \
  166. hold = state->hold; \
  167. bits = state->bits; \
  168. } while (0)
  169. /* Restore state from registers in inflate() */
  170. #define RESTORE() \
  171. do { \
  172. strm->next_out = put; \
  173. strm->avail_out = left; \
  174. strm->next_in = next; \
  175. strm->avail_in = have; \
  176. state->hold = hold; \
  177. state->bits = bits; \
  178. } while (0)
  179. /* Clear the input bit accumulator */
  180. #define INITBITS() \
  181. do { \
  182. hold = 0; \
  183. bits = 0; \
  184. } while (0)
  185. /* Get a byte of input into the bit accumulator, or return from inflate()
  186. if there is no input available. */
  187. #define PULLBYTE() \
  188. do { \
  189. if (have == 0) goto inf_leave; \
  190. have--; \
  191. hold += (unsigned long)(*next++) << bits; \
  192. bits += 8; \
  193. } while (0)
  194. /* Assure that there are at least n bits in the bit accumulator. If there is
  195. not enough available input to do that, then return from inflate(). */
  196. #define NEEDBITS(n) \
  197. do { \
  198. while (bits < (unsigned)(n)) \
  199. PULLBYTE(); \
  200. } while (0)
  201. /* Return the low n bits of the bit accumulator (n < 16) */
  202. #define BITS(n) \
  203. ((unsigned)hold & ((1U << (n)) - 1))
  204. /* Remove n bits from the bit accumulator */
  205. #define DROPBITS(n) \
  206. do { \
  207. hold >>= (n); \
  208. bits -= (unsigned)(n); \
  209. } while (0)
  210. /* Remove zero to seven bits as needed to go to a byte boundary */
  211. #define BYTEBITS() \
  212. do { \
  213. hold >>= bits & 7; \
  214. bits -= bits & 7; \
  215. } while (0)
  216. /*
  217. inflate() uses a state machine to process as much input data and generate as
  218. much output data as possible before returning. The state machine is
  219. structured roughly as follows:
  220. for (;;) switch (state) {
  221. ...
  222. case STATEn:
  223. if (not enough input data or output space to make progress)
  224. return;
  225. ... make progress ...
  226. state = STATEm;
  227. break;
  228. ...
  229. }
  230. so when inflate() is called again, the same case is attempted again, and
  231. if the appropriate resources are provided, the machine proceeds to the
  232. next state. The NEEDBITS() macro is usually the way the state evaluates
  233. whether it can proceed or should return. NEEDBITS() does the return if
  234. the requested bits are not available. The typical use of the BITS macros
  235. is:
  236. NEEDBITS(n);
  237. ... do something with BITS(n) ...
  238. DROPBITS(n);
  239. where NEEDBITS(n) either returns from inflate() if there isn't enough
  240. input left to load n bits into the accumulator, or it continues. BITS(n)
  241. gives the low n bits in the accumulator. When done, DROPBITS(n) drops
  242. the low n bits off the accumulator. INITBITS() clears the accumulator
  243. and sets the number of available bits to zero. BYTEBITS() discards just
  244. enough bits to put the accumulator on a byte boundary. After BYTEBITS()
  245. and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
  246. NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
  247. if there is no input available. The decoding of variable length codes uses
  248. PULLBYTE() directly in order to pull just enough bytes to decode the next
  249. code, and no more.
  250. Some states loop until they get enough input, making sure that enough
  251. state information is maintained to continue the loop where it left off
  252. if NEEDBITS() returns in the loop. For example, want, need, and keep
  253. would all have to actually be part of the saved state in case NEEDBITS()
  254. returns:
  255. case STATEw:
  256. while (want < need) {
  257. NEEDBITS(n);
  258. keep[want++] = BITS(n);
  259. DROPBITS(n);
  260. }
  261. state = STATEx;
  262. case STATEx:
  263. As shown above, if the next state is also the next case, then the break
  264. is omitted.
  265. A state may also return if there is not enough output space available to
  266. complete that state. Those states are copying stored data, writing a
  267. literal byte, and copying a matching string.
  268. When returning, a "goto inf_leave" is used to update the total counters,
  269. update the check value, and determine whether any progress has been made
  270. during that inflate() call in order to return the proper return code.
  271. Progress is defined as a change in either strm->avail_in or strm->avail_out.
  272. When there is a window, goto inf_leave will update the window with the last
  273. output written. If a goto inf_leave occurs in the middle of decompression
  274. and there is no window currently, goto inf_leave will create one and copy
  275. output to the window for the next call of inflate().
  276. In this implementation, the flush parameter of inflate() only affects the
  277. return code (per zlib.h). inflate() always writes as much as possible to
  278. strm->next_out, given the space available and the provided input--the effect
  279. documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
  280. the allocation of and copying into a sliding window until necessary, which
  281. provides the effect documented in zlib.h for Z_FINISH when the entire input
  282. stream available. So the only thing the flush parameter actually does is:
  283. when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
  284. will return Z_BUF_ERROR if it has not reached the end of the stream.
  285. */
  286. int zlib_inflate(z_streamp strm, int flush)
  287. {
  288. struct inflate_state *state;
  289. const unsigned char *next; /* next input */
  290. unsigned char *put; /* next output */
  291. unsigned have, left; /* available input and output */
  292. unsigned long hold; /* bit buffer */
  293. unsigned bits; /* bits in bit buffer */
  294. unsigned in, out; /* save starting available input and output */
  295. unsigned copy; /* number of stored or match bytes to copy */
  296. unsigned char *from; /* where to copy match bytes from */
  297. code this; /* current decoding table entry */
  298. code last; /* parent table entry */
  299. unsigned len; /* length to copy for repeats, bits to drop */
  300. int ret; /* return code */
  301. static const unsigned short order[19] = /* permutation of code lengths */
  302. {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  303. /* Do not check for strm->next_out == NULL here as ppc zImage
  304. inflates to strm->next_out = 0 */
  305. if (strm == NULL || strm->state == NULL ||
  306. (strm->next_in == NULL && strm->avail_in != 0))
  307. return Z_STREAM_ERROR;
  308. state = (struct inflate_state *)strm->state;
  309. if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
  310. LOAD();
  311. in = have;
  312. out = left;
  313. ret = Z_OK;
  314. for (;;)
  315. switch (state->mode) {
  316. case HEAD:
  317. if (state->wrap == 0) {
  318. state->mode = TYPEDO;
  319. break;
  320. }
  321. NEEDBITS(16);
  322. if (
  323. ((BITS(8) << 8) + (hold >> 8)) % 31) {
  324. strm->msg = (char *)"incorrect header check";
  325. state->mode = BAD;
  326. break;
  327. }
  328. if (BITS(4) != Z_DEFLATED) {
  329. strm->msg = (char *)"unknown compression method";
  330. state->mode = BAD;
  331. break;
  332. }
  333. DROPBITS(4);
  334. len = BITS(4) + 8;
  335. if (len > state->wbits) {
  336. strm->msg = (char *)"invalid window size";
  337. state->mode = BAD;
  338. break;
  339. }
  340. state->dmax = 1U << len;
  341. strm->adler = state->check = zlib_adler32(0L, NULL, 0);
  342. state->mode = hold & 0x200 ? DICTID : TYPE;
  343. INITBITS();
  344. break;
  345. case DICTID:
  346. NEEDBITS(32);
  347. strm->adler = state->check = REVERSE(hold);
  348. INITBITS();
  349. state->mode = DICT;
  350. /* fall through */
  351. case DICT:
  352. if (state->havedict == 0) {
  353. RESTORE();
  354. return Z_NEED_DICT;
  355. }
  356. strm->adler = state->check = zlib_adler32(0L, NULL, 0);
  357. state->mode = TYPE;
  358. /* fall through */
  359. case TYPE:
  360. if (flush == Z_BLOCK) goto inf_leave;
  361. /* fall through */
  362. case TYPEDO:
  363. INFLATE_TYPEDO_HOOK(strm, flush);
  364. if (state->last) {
  365. BYTEBITS();
  366. state->mode = CHECK;
  367. break;
  368. }
  369. NEEDBITS(3);
  370. state->last = BITS(1);
  371. DROPBITS(1);
  372. switch (BITS(2)) {
  373. case 0: /* stored block */
  374. state->mode = STORED;
  375. break;
  376. case 1: /* fixed block */
  377. zlib_fixedtables(state);
  378. state->mode = LEN; /* decode codes */
  379. break;
  380. case 2: /* dynamic block */
  381. state->mode = TABLE;
  382. break;
  383. case 3:
  384. strm->msg = (char *)"invalid block type";
  385. state->mode = BAD;
  386. }
  387. DROPBITS(2);
  388. break;
  389. case STORED:
  390. BYTEBITS(); /* go to byte boundary */
  391. NEEDBITS(32);
  392. if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  393. strm->msg = (char *)"invalid stored block lengths";
  394. state->mode = BAD;
  395. break;
  396. }
  397. state->length = (unsigned)hold & 0xffff;
  398. INITBITS();
  399. state->mode = COPY;
  400. /* fall through */
  401. case COPY:
  402. copy = state->length;
  403. if (copy) {
  404. if (copy > have) copy = have;
  405. if (copy > left) copy = left;
  406. if (copy == 0) goto inf_leave;
  407. memcpy(put, next, copy);
  408. have -= copy;
  409. next += copy;
  410. left -= copy;
  411. put += copy;
  412. state->length -= copy;
  413. break;
  414. }
  415. state->mode = TYPE;
  416. break;
  417. case TABLE:
  418. NEEDBITS(14);
  419. state->nlen = BITS(5) + 257;
  420. DROPBITS(5);
  421. state->ndist = BITS(5) + 1;
  422. DROPBITS(5);
  423. state->ncode = BITS(4) + 4;
  424. DROPBITS(4);
  425. #ifndef PKZIP_BUG_WORKAROUND
  426. if (state->nlen > 286 || state->ndist > 30) {
  427. strm->msg = (char *)"too many length or distance symbols";
  428. state->mode = BAD;
  429. break;
  430. }
  431. #endif
  432. state->have = 0;
  433. state->mode = LENLENS;
  434. /* fall through */
  435. case LENLENS:
  436. while (state->have < state->ncode) {
  437. NEEDBITS(3);
  438. state->lens[order[state->have++]] = (unsigned short)BITS(3);
  439. DROPBITS(3);
  440. }
  441. while (state->have < 19)
  442. state->lens[order[state->have++]] = 0;
  443. state->next = state->codes;
  444. state->lencode = (code const *)(state->next);
  445. state->lenbits = 7;
  446. ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
  447. &(state->lenbits), state->work);
  448. if (ret) {
  449. strm->msg = (char *)"invalid code lengths set";
  450. state->mode = BAD;
  451. break;
  452. }
  453. state->have = 0;
  454. state->mode = CODELENS;
  455. /* fall through */
  456. case CODELENS:
  457. while (state->have < state->nlen + state->ndist) {
  458. for (;;) {
  459. this = state->lencode[BITS(state->lenbits)];
  460. if ((unsigned)(this.bits) <= bits) break;
  461. PULLBYTE();
  462. }
  463. if (this.val < 16) {
  464. NEEDBITS(this.bits);
  465. DROPBITS(this.bits);
  466. state->lens[state->have++] = this.val;
  467. }
  468. else {
  469. if (this.val == 16) {
  470. NEEDBITS(this.bits + 2);
  471. DROPBITS(this.bits);
  472. if (state->have == 0) {
  473. strm->msg = (char *)"invalid bit length repeat";
  474. state->mode = BAD;
  475. break;
  476. }
  477. len = state->lens[state->have - 1];
  478. copy = 3 + BITS(2);
  479. DROPBITS(2);
  480. }
  481. else if (this.val == 17) {
  482. NEEDBITS(this.bits + 3);
  483. DROPBITS(this.bits);
  484. len = 0;
  485. copy = 3 + BITS(3);
  486. DROPBITS(3);
  487. }
  488. else {
  489. NEEDBITS(this.bits + 7);
  490. DROPBITS(this.bits);
  491. len = 0;
  492. copy = 11 + BITS(7);
  493. DROPBITS(7);
  494. }
  495. if (state->have + copy > state->nlen + state->ndist) {
  496. strm->msg = (char *)"invalid bit length repeat";
  497. state->mode = BAD;
  498. break;
  499. }
  500. while (copy--)
  501. state->lens[state->have++] = (unsigned short)len;
  502. }
  503. }
  504. /* handle error breaks in while */
  505. if (state->mode == BAD) break;
  506. /* build code tables */
  507. state->next = state->codes;
  508. state->lencode = (code const *)(state->next);
  509. state->lenbits = 9;
  510. ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
  511. &(state->lenbits), state->work);
  512. if (ret) {
  513. strm->msg = (char *)"invalid literal/lengths set";
  514. state->mode = BAD;
  515. break;
  516. }
  517. state->distcode = (code const *)(state->next);
  518. state->distbits = 6;
  519. ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  520. &(state->next), &(state->distbits), state->work);
  521. if (ret) {
  522. strm->msg = (char *)"invalid distances set";
  523. state->mode = BAD;
  524. break;
  525. }
  526. state->mode = LEN;
  527. /* fall through */
  528. case LEN:
  529. if (have >= 6 && left >= 258) {
  530. RESTORE();
  531. inflate_fast(strm, out);
  532. LOAD();
  533. break;
  534. }
  535. for (;;) {
  536. this = state->lencode[BITS(state->lenbits)];
  537. if ((unsigned)(this.bits) <= bits) break;
  538. PULLBYTE();
  539. }
  540. if (this.op && (this.op & 0xf0) == 0) {
  541. last = this;
  542. for (;;) {
  543. this = state->lencode[last.val +
  544. (BITS(last.bits + last.op) >> last.bits)];
  545. if ((unsigned)(last.bits + this.bits) <= bits) break;
  546. PULLBYTE();
  547. }
  548. DROPBITS(last.bits);
  549. }
  550. DROPBITS(this.bits);
  551. state->length = (unsigned)this.val;
  552. if ((int)(this.op) == 0) {
  553. state->mode = LIT;
  554. break;
  555. }
  556. if (this.op & 32) {
  557. state->mode = TYPE;
  558. break;
  559. }
  560. if (this.op & 64) {
  561. strm->msg = (char *)"invalid literal/length code";
  562. state->mode = BAD;
  563. break;
  564. }
  565. state->extra = (unsigned)(this.op) & 15;
  566. state->mode = LENEXT;
  567. /* fall through */
  568. case LENEXT:
  569. if (state->extra) {
  570. NEEDBITS(state->extra);
  571. state->length += BITS(state->extra);
  572. DROPBITS(state->extra);
  573. }
  574. state->mode = DIST;
  575. /* fall through */
  576. case DIST:
  577. for (;;) {
  578. this = state->distcode[BITS(state->distbits)];
  579. if ((unsigned)(this.bits) <= bits) break;
  580. PULLBYTE();
  581. }
  582. if ((this.op & 0xf0) == 0) {
  583. last = this;
  584. for (;;) {
  585. this = state->distcode[last.val +
  586. (BITS(last.bits + last.op) >> last.bits)];
  587. if ((unsigned)(last.bits + this.bits) <= bits) break;
  588. PULLBYTE();
  589. }
  590. DROPBITS(last.bits);
  591. }
  592. DROPBITS(this.bits);
  593. if (this.op & 64) {
  594. strm->msg = (char *)"invalid distance code";
  595. state->mode = BAD;
  596. break;
  597. }
  598. state->offset = (unsigned)this.val;
  599. state->extra = (unsigned)(this.op) & 15;
  600. state->mode = DISTEXT;
  601. /* fall through */
  602. case DISTEXT:
  603. if (state->extra) {
  604. NEEDBITS(state->extra);
  605. state->offset += BITS(state->extra);
  606. DROPBITS(state->extra);
  607. }
  608. #ifdef INFLATE_STRICT
  609. if (state->offset > state->dmax) {
  610. strm->msg = (char *)"invalid distance too far back";
  611. state->mode = BAD;
  612. break;
  613. }
  614. #endif
  615. if (state->offset > state->whave + out - left) {
  616. strm->msg = (char *)"invalid distance too far back";
  617. state->mode = BAD;
  618. break;
  619. }
  620. state->mode = MATCH;
  621. /* fall through */
  622. case MATCH:
  623. if (left == 0) goto inf_leave;
  624. copy = out - left;
  625. if (state->offset > copy) { /* copy from window */
  626. copy = state->offset - copy;
  627. if (copy > state->write) {
  628. copy -= state->write;
  629. from = state->window + (state->wsize - copy);
  630. }
  631. else
  632. from = state->window + (state->write - copy);
  633. if (copy > state->length) copy = state->length;
  634. }
  635. else { /* copy from output */
  636. from = put - state->offset;
  637. copy = state->length;
  638. }
  639. if (copy > left) copy = left;
  640. left -= copy;
  641. state->length -= copy;
  642. do {
  643. *put++ = *from++;
  644. } while (--copy);
  645. if (state->length == 0) state->mode = LEN;
  646. break;
  647. case LIT:
  648. if (left == 0) goto inf_leave;
  649. *put++ = (unsigned char)(state->length);
  650. left--;
  651. state->mode = LEN;
  652. break;
  653. case CHECK:
  654. if (state->wrap) {
  655. NEEDBITS(32);
  656. out -= left;
  657. strm->total_out += out;
  658. state->total += out;
  659. if (INFLATE_NEED_CHECKSUM(strm) && out)
  660. strm->adler = state->check =
  661. UPDATE(state->check, put - out, out);
  662. out = left;
  663. if ((
  664. REVERSE(hold)) != state->check) {
  665. strm->msg = (char *)"incorrect data check";
  666. state->mode = BAD;
  667. break;
  668. }
  669. INITBITS();
  670. }
  671. state->mode = DONE;
  672. /* fall through */
  673. case DONE:
  674. ret = Z_STREAM_END;
  675. goto inf_leave;
  676. case BAD:
  677. ret = Z_DATA_ERROR;
  678. goto inf_leave;
  679. case MEM:
  680. return Z_MEM_ERROR;
  681. case SYNC:
  682. default:
  683. return Z_STREAM_ERROR;
  684. }
  685. /*
  686. Return from inflate(), updating the total counts and the check value.
  687. If there was no progress during the inflate() call, return a buffer
  688. error. Call zlib_updatewindow() to create and/or update the window state.
  689. */
  690. inf_leave:
  691. RESTORE();
  692. if (INFLATE_NEED_UPDATEWINDOW(strm) &&
  693. (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
  694. zlib_updatewindow(strm, out);
  695. in -= strm->avail_in;
  696. out -= strm->avail_out;
  697. strm->total_in += in;
  698. strm->total_out += out;
  699. state->total += out;
  700. if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
  701. strm->adler = state->check =
  702. UPDATE(state->check, strm->next_out - out, out);
  703. strm->data_type = state->bits + (state->last ? 64 : 0) +
  704. (state->mode == TYPE ? 128 : 0);
  705. if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
  706. strm->avail_out != 0 && strm->avail_in == 0)
  707. return zlib_inflateSyncPacket(strm);
  708. if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  709. ret = Z_BUF_ERROR;
  710. return ret;
  711. }
  712. int zlib_inflateEnd(z_streamp strm)
  713. {
  714. if (strm == NULL || strm->state == NULL)
  715. return Z_STREAM_ERROR;
  716. return Z_OK;
  717. }
  718. /*
  719. * This subroutine adds the data at next_in/avail_in to the output history
  720. * without performing any output. The output buffer must be "caught up";
  721. * i.e. no pending output but this should always be the case. The state must
  722. * be waiting on the start of a block (i.e. mode == TYPE or HEAD). On exit,
  723. * the output will also be caught up, and the checksum will have been updated
  724. * if need be.
  725. */
  726. int zlib_inflateIncomp(z_stream *z)
  727. {
  728. struct inflate_state *state = (struct inflate_state *)z->state;
  729. Byte *saved_no = z->next_out;
  730. uInt saved_ao = z->avail_out;
  731. if (state->mode != TYPE && state->mode != HEAD)
  732. return Z_DATA_ERROR;
  733. /* Setup some variables to allow misuse of updateWindow */
  734. z->avail_out = 0;
  735. z->next_out = (unsigned char*)z->next_in + z->avail_in;
  736. zlib_updatewindow(z, z->avail_in);
  737. /* Restore saved variables */
  738. z->avail_out = saved_ao;
  739. z->next_out = saved_no;
  740. z->adler = state->check =
  741. UPDATE(state->check, z->next_in, z->avail_in);
  742. z->total_out += z->avail_in;
  743. z->total_in += z->avail_in;
  744. z->next_in += z->avail_in;
  745. state->total += z->avail_in;
  746. z->avail_in = 0;
  747. return Z_OK;
  748. }