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