inflate.c 27 KB

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