inflate.c 31 KB

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