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