1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 #include "zbuild.h"
7 #include "zutil.h"
8 #include "inftrees.h"
9 #include "inflate.h"
10 #include "inffast.h"
11 #include "inflate_p.h"
12 #include "inffixed_tbl.h"
13 #include "functable.h"
14
15 /* Architecture-specific hooks. */
16 #ifdef S390_DFLTCC_INFLATE
17 # include "arch/s390/dfltcc_inflate.h"
18 #else
19 /* Memory management for the inflate state. Useful for allocating arch-specific extension blocks. */
20 # define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
21 # define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
22 # define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
23 /* Memory management for the window. Useful for allocation the aligned window. */
24 # define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
25 # define ZFREE_WINDOW(strm, addr) ZFREE(strm, addr)
26 /* Invoked at the end of inflateResetKeep(). Useful for initializing arch-specific extension blocks. */
27 # define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
28 /* Invoked at the beginning of inflatePrime(). Useful for updating arch-specific buffers. */
29 # define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0)
30 /* Invoked at the beginning of each block. Useful for plugging arch-specific inflation code. */
31 # define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
32 /* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific inflation code already does that. */
33 # define INFLATE_NEED_CHECKSUM(strm) 1
34 /* Returns whether zlib-ng should update a window. Set to 0 if arch-specific inflation code already does that. */
35 # define INFLATE_NEED_UPDATEWINDOW(strm) 1
36 /* Invoked at the beginning of inflateMark(). Useful for updating arch-specific pointers and offsets. */
37 # define INFLATE_MARK_HOOK(strm) do {} while (0)
38 #endif
39
40 /* function prototypes */
41 static int inflateStateCheck(PREFIX3(stream) *strm);
42 static int updatewindow(PREFIX3(stream) *strm, const unsigned char *end, uint32_t copy);
43 static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
44
inflateStateCheck(PREFIX3 (stream)* strm)45 static int inflateStateCheck(PREFIX3(stream) *strm) {
46 struct inflate_state *state;
47 if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
48 return 1;
49 state = (struct inflate_state *)strm->state;
50 if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
51 return 1;
52 return 0;
53 }
54
PREFIX(inflateResetKeep)55 int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
56 struct inflate_state *state;
57
58 if (inflateStateCheck(strm))
59 return Z_STREAM_ERROR;
60 state = (struct inflate_state *)strm->state;
61 strm->total_in = strm->total_out = state->total = 0;
62 strm->msg = NULL;
63 if (state->wrap) /* to support ill-conceived Java test suite */
64 strm->adler = state->wrap & 1;
65 state->mode = HEAD;
66 state->check = ADLER32_INITIAL_VALUE;
67 state->last = 0;
68 state->havedict = 0;
69 state->flags = -1;
70 state->dmax = 32768U;
71 state->head = NULL;
72 state->hold = 0;
73 state->bits = 0;
74 state->lencode = state->distcode = state->next = state->codes;
75 state->sane = 1;
76 state->back = -1;
77 INFLATE_RESET_KEEP_HOOK(strm); /* hook for IBM Z DFLTCC */
78 Tracev((stderr, "inflate: reset\n"));
79 return Z_OK;
80 }
81
PREFIX(inflateReset)82 int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) {
83 struct inflate_state *state;
84
85 if (inflateStateCheck(strm))
86 return Z_STREAM_ERROR;
87 state = (struct inflate_state *)strm->state;
88 state->wsize = 0;
89 state->whave = 0;
90 state->wnext = 0;
91 return PREFIX(inflateResetKeep)(strm);
92 }
93
PREFIX(inflateReset2)94 int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) {
95 int wrap;
96 struct inflate_state *state;
97
98 /* get the state */
99 if (inflateStateCheck(strm))
100 return Z_STREAM_ERROR;
101 state = (struct inflate_state *)strm->state;
102
103 /* extract wrap request from windowBits parameter */
104 if (windowBits < 0) {
105 wrap = 0;
106 windowBits = -windowBits;
107 } else {
108 wrap = (windowBits >> 4) + 5;
109 #ifdef GUNZIP
110 if (windowBits < 48)
111 windowBits &= 15;
112 #endif
113 }
114
115 /* set number of window bits, free window if different */
116 if (windowBits && (windowBits < 8 || windowBits > 15))
117 return Z_STREAM_ERROR;
118 if (state->window != NULL && state->wbits != (unsigned)windowBits) {
119 ZFREE_WINDOW(strm, state->window);
120 state->window = NULL;
121 }
122
123 /* update state and reset the rest of it */
124 state->wrap = wrap;
125 state->wbits = (unsigned)windowBits;
126 return PREFIX(inflateReset)(strm);
127 }
128
PREFIX(inflateInit2_)129 int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
130 int32_t ret;
131 struct inflate_state *state;
132
133 #if defined(X86_FEATURES)
134 x86_check_features();
135 #elif defined(ARM_FEATURES)
136 arm_check_features();
137 #endif
138
139 if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
140 return Z_VERSION_ERROR;
141 if (strm == NULL)
142 return Z_STREAM_ERROR;
143 strm->msg = NULL; /* in case we return an error */
144 if (strm->zalloc == NULL) {
145 strm->zalloc = zng_calloc;
146 strm->opaque = NULL;
147 }
148 if (strm->zfree == NULL)
149 strm->zfree = zng_cfree;
150 state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
151 if (state == NULL)
152 return Z_MEM_ERROR;
153 Tracev((stderr, "inflate: allocated\n"));
154 strm->state = (struct internal_state *)state;
155 state->strm = strm;
156 state->window = NULL;
157 state->mode = HEAD; /* to pass state test in inflateReset2() */
158 state->chunksize = functable.chunksize();
159 ret = PREFIX(inflateReset2)(strm, windowBits);
160 if (ret != Z_OK) {
161 ZFREE_STATE(strm, state);
162 strm->state = NULL;
163 }
164 return ret;
165 }
166
PREFIX(inflateInit_)167 int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
168 return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
169 }
170
PREFIX(inflatePrime)171 int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
172 struct inflate_state *state;
173
174 if (inflateStateCheck(strm))
175 return Z_STREAM_ERROR;
176 INFLATE_PRIME_HOOK(strm, bits, value); /* hook for IBM Z DFLTCC */
177 state = (struct inflate_state *)strm->state;
178 if (bits < 0) {
179 state->hold = 0;
180 state->bits = 0;
181 return Z_OK;
182 }
183 if (bits > 16 || state->bits + (unsigned int)bits > 32)
184 return Z_STREAM_ERROR;
185 value &= (1L << bits) - 1;
186 state->hold += (unsigned)value << state->bits;
187 state->bits += (unsigned int)bits;
188 return Z_OK;
189 }
190
191 /*
192 Return state with length and distance decoding tables and index sizes set to
193 fixed code decoding. This returns fixed tables from inffixed_tbl.h.
194 */
195
fixedtables(struct inflate_state * state)196 void Z_INTERNAL fixedtables(struct inflate_state *state) {
197 state->lencode = lenfix;
198 state->lenbits = 9;
199 state->distcode = distfix;
200 state->distbits = 5;
201 }
202
inflate_ensure_window(struct inflate_state * state)203 int Z_INTERNAL inflate_ensure_window(struct inflate_state *state) {
204 /* if it hasn't been done already, allocate space for the window */
205 if (state->window == NULL) {
206 unsigned wsize = 1U << state->wbits;
207 state->window = (unsigned char *) ZALLOC_WINDOW(state->strm, wsize + state->chunksize, sizeof(unsigned char));
208 if (state->window == Z_NULL)
209 return 1;
210 memset(state->window + wsize, 0, state->chunksize);
211 }
212
213 /* if window not in use yet, initialize */
214 if (state->wsize == 0) {
215 state->wsize = 1U << state->wbits;
216 state->wnext = 0;
217 state->whave = 0;
218 }
219
220 return 0;
221 }
222
223 /*
224 Update the window with the last wsize (normally 32K) bytes written before
225 returning. If window does not exist yet, create it. This is only called
226 when a window is already in use, or when output has been written during this
227 inflate call, but the end of the deflate stream has not been reached yet.
228 It is also called to create a window for dictionary data when a dictionary
229 is loaded.
230
231 Providing output buffers larger than 32K to inflate() should provide a speed
232 advantage, since only the last 32K of output is copied to the sliding window
233 upon return from inflate(), and since all distances after the first 32K of
234 output will fall in the output data, making match copies simpler and faster.
235 The advantage may be dependent on the size of the processor's data caches.
236 */
updatewindow(PREFIX3 (stream)* strm,const uint8_t * end,uint32_t copy)237 static int32_t updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t copy) {
238 struct inflate_state *state;
239 uint32_t dist;
240
241 state = (struct inflate_state *)strm->state;
242
243 if (inflate_ensure_window(state)) return 1;
244
245 /* copy state->wsize or less output bytes into the circular window */
246 if (copy >= state->wsize) {
247 memcpy(state->window, end - state->wsize, state->wsize);
248 state->wnext = 0;
249 state->whave = state->wsize;
250 } else {
251 dist = state->wsize - state->wnext;
252 if (dist > copy)
253 dist = copy;
254 memcpy(state->window + state->wnext, end - copy, dist);
255 copy -= dist;
256 if (copy) {
257 memcpy(state->window, end - copy, copy);
258 state->wnext = copy;
259 state->whave = state->wsize;
260 } else {
261 state->wnext += dist;
262 if (state->wnext == state->wsize)
263 state->wnext = 0;
264 if (state->whave < state->wsize)
265 state->whave += dist;
266 }
267 }
268 return 0;
269 }
270
271
272 /*
273 Private macros for inflate()
274 Look in inflate_p.h for macros shared with inflateBack()
275 */
276
277 /* Get a byte of input into the bit accumulator, or return from inflate() if there is no input available. */
278 #define PULLBYTE() \
279 do { \
280 if (have == 0) goto inf_leave; \
281 have--; \
282 hold += ((unsigned)(*next++) << bits); \
283 bits += 8; \
284 } while (0)
285
286 /*
287 inflate() uses a state machine to process as much input data and generate as
288 much output data as possible before returning. The state machine is
289 structured roughly as follows:
290
291 for (;;) switch (state) {
292 ...
293 case STATEn:
294 if (not enough input data or output space to make progress)
295 return;
296 ... make progress ...
297 state = STATEm;
298 break;
299 ...
300 }
301
302 so when inflate() is called again, the same case is attempted again, and
303 if the appropriate resources are provided, the machine proceeds to the
304 next state. The NEEDBITS() macro is usually the way the state evaluates
305 whether it can proceed or should return. NEEDBITS() does the return if
306 the requested bits are not available. The typical use of the BITS macros
307 is:
308
309 NEEDBITS(n);
310 ... do something with BITS(n) ...
311 DROPBITS(n);
312
313 where NEEDBITS(n) either returns from inflate() if there isn't enough
314 input left to load n bits into the accumulator, or it continues. BITS(n)
315 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
316 the low n bits off the accumulator. INITBITS() clears the accumulator
317 and sets the number of available bits to zero. BYTEBITS() discards just
318 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
319 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
320
321 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
322 if there is no input available. The decoding of variable length codes uses
323 PULLBYTE() directly in order to pull just enough bytes to decode the next
324 code, and no more.
325
326 Some states loop until they get enough input, making sure that enough
327 state information is maintained to continue the loop where it left off
328 if NEEDBITS() returns in the loop. For example, want, need, and keep
329 would all have to actually be part of the saved state in case NEEDBITS()
330 returns:
331
332 case STATEw:
333 while (want < need) {
334 NEEDBITS(n);
335 keep[want++] = BITS(n);
336 DROPBITS(n);
337 }
338 state = STATEx;
339 case STATEx:
340
341 As shown above, if the next state is also the next case, then the break
342 is omitted.
343
344 A state may also return if there is not enough output space available to
345 complete that state. Those states are copying stored data, writing a
346 literal byte, and copying a matching string.
347
348 When returning, a "goto inf_leave" is used to update the total counters,
349 update the check value, and determine whether any progress has been made
350 during that inflate() call in order to return the proper return code.
351 Progress is defined as a change in either strm->avail_in or strm->avail_out.
352 When there is a window, goto inf_leave will update the window with the last
353 output written. If a goto inf_leave occurs in the middle of decompression
354 and there is no window currently, goto inf_leave will create one and copy
355 output to the window for the next call of inflate().
356
357 In this implementation, the flush parameter of inflate() only affects the
358 return code (per zlib.h). inflate() always writes as much as possible to
359 strm->next_out, given the space available and the provided input--the effect
360 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
361 the allocation of and copying into a sliding window until necessary, which
362 provides the effect documented in zlib.h for Z_FINISH when the entire input
363 stream available. So the only thing the flush parameter actually does is:
364 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
365 will return Z_BUF_ERROR if it has not reached the end of the stream.
366 */
367
PREFIX(inflate)368 int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
369 struct inflate_state *state;
370 const unsigned char *next; /* next input */
371 unsigned char *put; /* next output */
372 unsigned have, left; /* available input and output */
373 uint32_t hold; /* bit buffer */
374 unsigned bits; /* bits in bit buffer */
375 uint32_t in, out; /* save starting available input and output */
376 unsigned copy; /* number of stored or match bytes to copy */
377 unsigned char *from; /* where to copy match bytes from */
378 code here; /* current decoding table entry */
379 code last; /* parent table entry */
380 unsigned len; /* length to copy for repeats, bits to drop */
381 int32_t ret; /* return code */
382 #ifdef GUNZIP
383 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
384 #endif
385 static const uint16_t order[19] = /* permutation of code lengths */
386 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
387
388 if (inflateStateCheck(strm) || strm->next_out == NULL ||
389 (strm->next_in == NULL && strm->avail_in != 0))
390 return Z_STREAM_ERROR;
391
392 state = (struct inflate_state *)strm->state;
393 if (state->mode == TYPE) /* skip check */
394 state->mode = TYPEDO;
395 LOAD();
396 in = have;
397 out = left;
398 ret = Z_OK;
399 for (;;)
400 switch (state->mode) {
401 case HEAD:
402 if (state->wrap == 0) {
403 state->mode = TYPEDO;
404 break;
405 }
406 NEEDBITS(16);
407 #ifdef GUNZIP
408 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
409 if (state->wbits == 0)
410 state->wbits = 15;
411 state->check = PREFIX(crc32)(0L, NULL, 0);
412 CRC2(state->check, hold);
413 INITBITS();
414 state->mode = FLAGS;
415 break;
416 }
417 if (state->head != NULL)
418 state->head->done = -1;
419 if (!(state->wrap & 1) || /* check if zlib header allowed */
420 #else
421 if (
422 #endif
423 ((BITS(8) << 8) + (hold >> 8)) % 31) {
424 strm->msg = (char *)"incorrect header check";
425 state->mode = BAD;
426 break;
427 }
428 if (BITS(4) != Z_DEFLATED) {
429 strm->msg = (char *)"unknown compression method";
430 state->mode = BAD;
431 break;
432 }
433 DROPBITS(4);
434 len = BITS(4) + 8;
435 if (state->wbits == 0)
436 state->wbits = len;
437 if (len > 15 || len > state->wbits) {
438 strm->msg = (char *)"invalid window size";
439 state->mode = BAD;
440 break;
441 }
442 state->dmax = 1U << len;
443 state->flags = 0; /* indicate zlib header */
444 Tracev((stderr, "inflate: zlib header ok\n"));
445 strm->adler = state->check = ADLER32_INITIAL_VALUE;
446 state->mode = hold & 0x200 ? DICTID : TYPE;
447 INITBITS();
448 break;
449 #ifdef GUNZIP
450
451 case FLAGS:
452 NEEDBITS(16);
453 state->flags = (int)(hold);
454 if ((state->flags & 0xff) != Z_DEFLATED) {
455 strm->msg = (char *)"unknown compression method";
456 state->mode = BAD;
457 break;
458 }
459 if (state->flags & 0xe000) {
460 strm->msg = (char *)"unknown header flags set";
461 state->mode = BAD;
462 break;
463 }
464 if (state->head != NULL)
465 state->head->text = (int)((hold >> 8) & 1);
466 if ((state->flags & 0x0200) && (state->wrap & 4))
467 CRC2(state->check, hold);
468 INITBITS();
469 state->mode = TIME;
470
471 case TIME:
472 NEEDBITS(32);
473 if (state->head != NULL)
474 state->head->time = hold;
475 if ((state->flags & 0x0200) && (state->wrap & 4))
476 CRC4(state->check, hold);
477 INITBITS();
478 state->mode = OS;
479
480 case OS:
481 NEEDBITS(16);
482 if (state->head != NULL) {
483 state->head->xflags = (int)(hold & 0xff);
484 state->head->os = (int)(hold >> 8);
485 }
486 if ((state->flags & 0x0200) && (state->wrap & 4))
487 CRC2(state->check, hold);
488 INITBITS();
489 state->mode = EXLEN;
490
491 case EXLEN:
492 if (state->flags & 0x0400) {
493 NEEDBITS(16);
494 state->length = (uint16_t)hold;
495 if (state->head != NULL)
496 state->head->extra_len = (uint16_t)hold;
497 if ((state->flags & 0x0200) && (state->wrap & 4))
498 CRC2(state->check, hold);
499 INITBITS();
500 } else if (state->head != NULL) {
501 state->head->extra = NULL;
502 }
503 state->mode = EXTRA;
504
505 case EXTRA:
506 if (state->flags & 0x0400) {
507 copy = state->length;
508 if (copy > have)
509 copy = have;
510 if (copy) {
511 if (state->head != NULL &&
512 state->head->extra != NULL) {
513 len = state->head->extra_len - state->length;
514 memcpy(state->head->extra + len, next,
515 len + copy > state->head->extra_max ?
516 state->head->extra_max - len : copy);
517 }
518 if ((state->flags & 0x0200) && (state->wrap & 4))
519 state->check = PREFIX(crc32)(state->check, next, copy);
520 have -= copy;
521 next += copy;
522 state->length -= copy;
523 }
524 if (state->length)
525 goto inf_leave;
526 }
527 state->length = 0;
528 state->mode = NAME;
529
530 case NAME:
531 if (state->flags & 0x0800) {
532 if (have == 0) goto inf_leave;
533 copy = 0;
534 do {
535 len = (unsigned)(next[copy++]);
536 if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)
537 state->head->name[state->length++] = (unsigned char)len;
538 } while (len && copy < have);
539 if ((state->flags & 0x0200) && (state->wrap & 4))
540 state->check = PREFIX(crc32)(state->check, next, copy);
541 have -= copy;
542 next += copy;
543 if (len)
544 goto inf_leave;
545 } else if (state->head != NULL) {
546 state->head->name = NULL;
547 }
548 state->length = 0;
549 state->mode = COMMENT;
550
551 case COMMENT:
552 if (state->flags & 0x1000) {
553 if (have == 0) goto inf_leave;
554 copy = 0;
555 do {
556 len = (unsigned)(next[copy++]);
557 if (state->head != NULL && state->head->comment != NULL
558 && state->length < state->head->comm_max)
559 state->head->comment[state->length++] = (unsigned char)len;
560 } while (len && copy < have);
561 if ((state->flags & 0x0200) && (state->wrap & 4))
562 state->check = PREFIX(crc32)(state->check, next, copy);
563 have -= copy;
564 next += copy;
565 if (len)
566 goto inf_leave;
567 } else if (state->head != NULL) {
568 state->head->comment = NULL;
569 }
570 state->mode = HCRC;
571
572 case HCRC:
573 if (state->flags & 0x0200) {
574 NEEDBITS(16);
575 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
576 strm->msg = (char *)"header crc mismatch";
577 state->mode = BAD;
578 break;
579 }
580 INITBITS();
581 }
582 if (state->head != NULL) {
583 state->head->hcrc = (int)((state->flags >> 9) & 1);
584 state->head->done = 1;
585 }
586 strm->adler = state->check = PREFIX(crc32)(0L, NULL, 0);
587 state->mode = TYPE;
588 break;
589 #endif
590 case DICTID:
591 NEEDBITS(32);
592 strm->adler = state->check = ZSWAP32(hold);
593 INITBITS();
594 state->mode = DICT;
595
596 case DICT:
597 if (state->havedict == 0) {
598 RESTORE();
599 return Z_NEED_DICT;
600 }
601 strm->adler = state->check = ADLER32_INITIAL_VALUE;
602 state->mode = TYPE;
603
604 case TYPE:
605 if (flush == Z_BLOCK || flush == Z_TREES)
606 goto inf_leave;
607
608 case TYPEDO:
609 /* determine and dispatch block type */
610 INFLATE_TYPEDO_HOOK(strm, flush); /* hook for IBM Z DFLTCC */
611 if (state->last) {
612 BYTEBITS();
613 state->mode = CHECK;
614 break;
615 }
616 NEEDBITS(3);
617 state->last = BITS(1);
618 DROPBITS(1);
619 switch (BITS(2)) {
620 case 0: /* stored block */
621 Tracev((stderr, "inflate: stored block%s\n", state->last ? " (last)" : ""));
622 state->mode = STORED;
623 break;
624 case 1: /* fixed block */
625 fixedtables(state);
626 Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : ""));
627 state->mode = LEN_; /* decode codes */
628 if (flush == Z_TREES) {
629 DROPBITS(2);
630 goto inf_leave;
631 }
632 break;
633 case 2: /* dynamic block */
634 Tracev((stderr, "inflate: dynamic codes block%s\n", state->last ? " (last)" : ""));
635 state->mode = TABLE;
636 break;
637 case 3:
638 strm->msg = (char *)"invalid block type";
639 state->mode = BAD;
640 }
641 DROPBITS(2);
642 break;
643
644 case STORED:
645 /* get and verify stored block length */
646 BYTEBITS(); /* go to byte boundary */
647 NEEDBITS(32);
648 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
649 strm->msg = (char *)"invalid stored block lengths";
650 state->mode = BAD;
651 break;
652 }
653 state->length = (uint16_t)hold;
654 Tracev((stderr, "inflate: stored length %u\n", state->length));
655 INITBITS();
656 state->mode = COPY_;
657 if (flush == Z_TREES)
658 goto inf_leave;
659
660 case COPY_:
661 state->mode = COPY;
662
663 case COPY:
664 /* copy stored block from input to output */
665 copy = state->length;
666 if (copy) {
667 if (copy > have) copy = have;
668 if (copy > left) copy = left;
669 if (copy == 0) goto inf_leave;
670 memcpy(put, next, copy);
671 have -= copy;
672 next += copy;
673 left -= copy;
674 put += copy;
675 state->length -= copy;
676 break;
677 }
678 Tracev((stderr, "inflate: stored end\n"));
679 state->mode = TYPE;
680 break;
681
682 case TABLE:
683 /* get dynamic table entries descriptor */
684 NEEDBITS(14);
685 state->nlen = BITS(5) + 257;
686 DROPBITS(5);
687 state->ndist = BITS(5) + 1;
688 DROPBITS(5);
689 state->ncode = BITS(4) + 4;
690 DROPBITS(4);
691 #ifndef PKZIP_BUG_WORKAROUND
692 if (state->nlen > 286 || state->ndist > 30) {
693 strm->msg = (char *)"too many length or distance symbols";
694 state->mode = BAD;
695 break;
696 }
697 #endif
698 Tracev((stderr, "inflate: table sizes ok\n"));
699 state->have = 0;
700 state->mode = LENLENS;
701
702 case LENLENS:
703 /* get code length code lengths (not a typo) */
704 while (state->have < state->ncode) {
705 NEEDBITS(3);
706 state->lens[order[state->have++]] = (uint16_t)BITS(3);
707 DROPBITS(3);
708 }
709 while (state->have < 19)
710 state->lens[order[state->have++]] = 0;
711 state->next = state->codes;
712 state->lencode = (const code *)(state->next);
713 state->lenbits = 7;
714 ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
715 if (ret) {
716 strm->msg = (char *)"invalid code lengths set";
717 state->mode = BAD;
718 break;
719 }
720 Tracev((stderr, "inflate: code lengths ok\n"));
721 state->have = 0;
722 state->mode = CODELENS;
723
724 case CODELENS:
725 /* get length and distance code code lengths */
726 while (state->have < state->nlen + state->ndist) {
727 for (;;) {
728 here = state->lencode[BITS(state->lenbits)];
729 if (here.bits <= bits) break;
730 PULLBYTE();
731 }
732 if (here.val < 16) {
733 DROPBITS(here.bits);
734 state->lens[state->have++] = here.val;
735 } else {
736 if (here.val == 16) {
737 NEEDBITS(here.bits + 2);
738 DROPBITS(here.bits);
739 if (state->have == 0) {
740 strm->msg = (char *)"invalid bit length repeat";
741 state->mode = BAD;
742 break;
743 }
744 len = state->lens[state->have - 1];
745 copy = 3 + BITS(2);
746 DROPBITS(2);
747 } else if (here.val == 17) {
748 NEEDBITS(here.bits + 3);
749 DROPBITS(here.bits);
750 len = 0;
751 copy = 3 + BITS(3);
752 DROPBITS(3);
753 } else {
754 NEEDBITS(here.bits + 7);
755 DROPBITS(here.bits);
756 len = 0;
757 copy = 11 + BITS(7);
758 DROPBITS(7);
759 }
760 if (state->have + copy > state->nlen + state->ndist) {
761 strm->msg = (char *)"invalid bit length repeat";
762 state->mode = BAD;
763 break;
764 }
765 while (copy) {
766 --copy;
767 state->lens[state->have++] = (uint16_t)len;
768 }
769 }
770 }
771
772 /* handle error breaks in while */
773 if (state->mode == BAD)
774 break;
775
776 /* check for end-of-block code (better have one) */
777 if (state->lens[256] == 0) {
778 strm->msg = (char *)"invalid code -- missing end-of-block";
779 state->mode = BAD;
780 break;
781 }
782
783 /* build code tables -- note: do not change the lenbits or distbits
784 values here (9 and 6) without reading the comments in inftrees.h
785 concerning the ENOUGH constants, which depend on those values */
786 state->next = state->codes;
787 state->lencode = (const code *)(state->next);
788 state->lenbits = 9;
789 ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
790 if (ret) {
791 strm->msg = (char *)"invalid literal/lengths set";
792 state->mode = BAD;
793 break;
794 }
795 state->distcode = (const code *)(state->next);
796 state->distbits = 6;
797 ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
798 &(state->next), &(state->distbits), state->work);
799 if (ret) {
800 strm->msg = (char *)"invalid distances set";
801 state->mode = BAD;
802 break;
803 }
804 Tracev((stderr, "inflate: codes ok\n"));
805 state->mode = LEN_;
806 if (flush == Z_TREES)
807 goto inf_leave;
808
809 case LEN_:
810 state->mode = LEN;
811
812 case LEN:
813 /* use inflate_fast() if we have enough input and output */
814 if (have >= INFLATE_FAST_MIN_HAVE &&
815 left >= INFLATE_FAST_MIN_LEFT) {
816 RESTORE();
817 zng_inflate_fast(strm, out);
818 LOAD();
819 if (state->mode == TYPE)
820 state->back = -1;
821 break;
822 }
823 state->back = 0;
824
825 /* get a literal, length, or end-of-block code */
826 for (;;) {
827 here = state->lencode[BITS(state->lenbits)];
828 if (here.bits <= bits)
829 break;
830 PULLBYTE();
831 }
832 if (here.op && (here.op & 0xf0) == 0) {
833 last = here;
834 for (;;) {
835 here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
836 if ((unsigned)last.bits + (unsigned)here.bits <= bits)
837 break;
838 PULLBYTE();
839 }
840 DROPBITS(last.bits);
841 state->back += last.bits;
842 }
843 DROPBITS(here.bits);
844 state->back += here.bits;
845 state->length = here.val;
846
847 /* process literal */
848 if ((int)(here.op) == 0) {
849 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
850 "inflate: literal '%c'\n" :
851 "inflate: literal 0x%02x\n", here.val));
852 state->mode = LIT;
853 break;
854 }
855
856 /* process end of block */
857 if (here.op & 32) {
858 Tracevv((stderr, "inflate: end of block\n"));
859 state->back = -1;
860 state->mode = TYPE;
861 break;
862 }
863
864 /* invalid code */
865 if (here.op & 64) {
866 strm->msg = (char *)"invalid literal/length code";
867 state->mode = BAD;
868 break;
869 }
870
871 /* length code */
872 state->extra = (here.op & 15);
873 state->mode = LENEXT;
874
875 case LENEXT:
876 /* get extra bits, if any */
877 if (state->extra) {
878 NEEDBITS(state->extra);
879 state->length += BITS(state->extra);
880 DROPBITS(state->extra);
881 state->back += state->extra;
882 }
883 Tracevv((stderr, "inflate: length %u\n", state->length));
884 state->was = state->length;
885 state->mode = DIST;
886
887 case DIST:
888 /* get distance code */
889 for (;;) {
890 here = state->distcode[BITS(state->distbits)];
891 if (here.bits <= bits)
892 break;
893 PULLBYTE();
894 }
895 if ((here.op & 0xf0) == 0) {
896 last = here;
897 for (;;) {
898 here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
899 if ((unsigned)last.bits + (unsigned)here.bits <= bits)
900 break;
901 PULLBYTE();
902 }
903 DROPBITS(last.bits);
904 state->back += last.bits;
905 }
906 DROPBITS(here.bits);
907 state->back += here.bits;
908 if (here.op & 64) {
909 strm->msg = (char *)"invalid distance code";
910 state->mode = BAD;
911 break;
912 }
913 state->offset = here.val;
914 state->extra = (here.op & 15);
915 state->mode = DISTEXT;
916
917 case DISTEXT:
918 /* get distance extra bits, if any */
919 if (state->extra) {
920 NEEDBITS(state->extra);
921 state->offset += BITS(state->extra);
922 DROPBITS(state->extra);
923 state->back += state->extra;
924 }
925 #ifdef INFLATE_STRICT
926 if (state->offset > state->dmax) {
927 strm->msg = (char *)"invalid distance too far back";
928 state->mode = BAD;
929 break;
930 }
931 #endif
932 Tracevv((stderr, "inflate: distance %u\n", state->offset));
933 state->mode = MATCH;
934
935 case MATCH:
936 /* copy match from window to output */
937 if (left == 0) goto inf_leave;
938 copy = out - left;
939 if (state->offset > copy) { /* copy from window */
940 copy = state->offset - copy;
941 if (copy > state->whave) {
942 if (state->sane) {
943 strm->msg = (char *)"invalid distance too far back";
944 state->mode = BAD;
945 break;
946 }
947 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
948 Trace((stderr, "inflate.c too far\n"));
949 copy -= state->whave;
950 if (copy > state->length)
951 copy = state->length;
952 if (copy > left)
953 copy = left;
954 left -= copy;
955 state->length -= copy;
956 do {
957 *put++ = 0;
958 } while (--copy);
959 if (state->length == 0)
960 state->mode = LEN;
961 break;
962 #endif
963 }
964 if (copy > state->wnext) {
965 copy -= state->wnext;
966 from = state->window + (state->wsize - copy);
967 } else {
968 from = state->window + (state->wnext - copy);
969 }
970 if (copy > state->length)
971 copy = state->length;
972 if (copy > left)
973 copy = left;
974
975 put = functable.chunkcopy_safe(put, from, copy, put + left);
976 } else { /* copy from output */
977 copy = state->length;
978 if (copy > left)
979 copy = left;
980
981 put = functable.chunkmemset_safe(put, state->offset, copy, left);
982 }
983 left -= copy;
984 state->length -= copy;
985 if (state->length == 0)
986 state->mode = LEN;
987 break;
988
989 case LIT:
990 if (left == 0)
991 goto inf_leave;
992 *put++ = (unsigned char)(state->length);
993 left--;
994 state->mode = LEN;
995 break;
996
997 case CHECK:
998 if (state->wrap) {
999 NEEDBITS(32);
1000 out -= left;
1001 strm->total_out += out;
1002 state->total += out;
1003 if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
1004 strm->adler = state->check = UPDATE(state->check, put - out, out);
1005 out = left;
1006 if ((state->wrap & 4) && (
1007 #ifdef GUNZIP
1008 state->flags ? hold :
1009 #endif
1010 ZSWAP32(hold)) != state->check) {
1011 strm->msg = (char *)"incorrect data check";
1012 state->mode = BAD;
1013 break;
1014 }
1015 INITBITS();
1016 Tracev((stderr, "inflate: check matches trailer\n"));
1017 }
1018 #ifdef GUNZIP
1019 state->mode = LENGTH;
1020
1021 case LENGTH:
1022 if (state->wrap && state->flags) {
1023 NEEDBITS(32);
1024 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1025 strm->msg = (char *)"incorrect length check";
1026 state->mode = BAD;
1027 break;
1028 }
1029 INITBITS();
1030 Tracev((stderr, "inflate: length matches trailer\n"));
1031 }
1032 #endif
1033 state->mode = DONE;
1034
1035 case DONE:
1036 /* inflate stream terminated properly */
1037 ret = Z_STREAM_END;
1038 goto inf_leave;
1039
1040 case BAD:
1041 ret = Z_DATA_ERROR;
1042 goto inf_leave;
1043
1044 case MEM:
1045 return Z_MEM_ERROR;
1046
1047 case SYNC:
1048
1049 default: /* can't happen, but makes compilers happy */
1050 return Z_STREAM_ERROR;
1051 }
1052
1053 /*
1054 Return from inflate(), updating the total counts and the check value.
1055 If there was no progress during the inflate() call, return a buffer
1056 error. Call updatewindow() to create and/or update the window state.
1057 Note: a memory error from inflate() is non-recoverable.
1058 */
1059 inf_leave:
1060 RESTORE();
1061 if (INFLATE_NEED_UPDATEWINDOW(strm) &&
1062 (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1063 (state->mode < CHECK || flush != Z_FINISH)))) {
1064 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1065 state->mode = MEM;
1066 return Z_MEM_ERROR;
1067 }
1068 }
1069 in -= strm->avail_in;
1070 out -= strm->avail_out;
1071 strm->total_in += in;
1072 strm->total_out += out;
1073 state->total += out;
1074 if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
1075 strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
1076 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1077 (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1078 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1079 ret = Z_BUF_ERROR;
1080 return ret;
1081 }
1082
PREFIX(inflateEnd)1083 int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
1084 struct inflate_state *state;
1085 if (inflateStateCheck(strm))
1086 return Z_STREAM_ERROR;
1087 state = (struct inflate_state *)strm->state;
1088 if (state->window != NULL)
1089 ZFREE_WINDOW(strm, state->window);
1090 ZFREE_STATE(strm, strm->state);
1091 strm->state = NULL;
1092 Tracev((stderr, "inflate: end\n"));
1093 return Z_OK;
1094 }
1095
PREFIX(inflateGetDictionary)1096 int32_t Z_EXPORT PREFIX(inflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
1097 struct inflate_state *state;
1098
1099 /* check state */
1100 if (inflateStateCheck(strm))
1101 return Z_STREAM_ERROR;
1102 state = (struct inflate_state *)strm->state;
1103
1104 /* copy dictionary */
1105 if (state->whave && dictionary != NULL) {
1106 memcpy(dictionary, state->window + state->wnext, state->whave - state->wnext);
1107 memcpy(dictionary + state->whave - state->wnext, state->window, state->wnext);
1108 }
1109 if (dictLength != NULL)
1110 *dictLength = state->whave;
1111 return Z_OK;
1112 }
1113
PREFIX(inflateSetDictionary)1114 int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
1115 struct inflate_state *state;
1116 unsigned long dictid;
1117 int32_t ret;
1118
1119 /* check state */
1120 if (inflateStateCheck(strm))
1121 return Z_STREAM_ERROR;
1122 state = (struct inflate_state *)strm->state;
1123 if (state->wrap != 0 && state->mode != DICT)
1124 return Z_STREAM_ERROR;
1125
1126 /* check for correct dictionary identifier */
1127 if (state->mode == DICT) {
1128 dictid = functable.adler32(ADLER32_INITIAL_VALUE, dictionary, dictLength);
1129 if (dictid != state->check)
1130 return Z_DATA_ERROR;
1131 }
1132
1133 /* copy dictionary to window using updatewindow(), which will amend the
1134 existing dictionary if appropriate */
1135 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1136 if (ret) {
1137 state->mode = MEM;
1138 return Z_MEM_ERROR;
1139 }
1140 state->havedict = 1;
1141 Tracev((stderr, "inflate: dictionary set\n"));
1142 return Z_OK;
1143 }
1144
PREFIX(inflateGetHeader)1145 int32_t Z_EXPORT PREFIX(inflateGetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
1146 struct inflate_state *state;
1147
1148 /* check state */
1149 if (inflateStateCheck(strm))
1150 return Z_STREAM_ERROR;
1151 state = (struct inflate_state *)strm->state;
1152 if ((state->wrap & 2) == 0)
1153 return Z_STREAM_ERROR;
1154
1155 /* save header structure */
1156 state->head = head;
1157 head->done = 0;
1158 return Z_OK;
1159 }
1160
1161 /*
1162 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1163 or when out of input. When called, *have is the number of pattern bytes
1164 found in order so far, in 0..3. On return *have is updated to the new
1165 state. If on return *have equals four, then the pattern was found and the
1166 return value is how many bytes were read including the last byte of the
1167 pattern. If *have is less than four, then the pattern has not been found
1168 yet and the return value is len. In the latter case, syncsearch() can be
1169 called again with more data and the *have state. *have is initialized to
1170 zero for the first call.
1171 */
syncsearch(uint32_t * have,const uint8_t * buf,uint32_t len)1172 static uint32_t syncsearch(uint32_t *have, const uint8_t *buf, uint32_t len) {
1173 uint32_t got;
1174 uint32_t next;
1175
1176 got = *have;
1177 next = 0;
1178 while (next < len && got < 4) {
1179 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1180 got++;
1181 else if (buf[next])
1182 got = 0;
1183 else
1184 got = 4 - got;
1185 next++;
1186 }
1187 *have = got;
1188 return next;
1189 }
1190
PREFIX(inflateSync)1191 int32_t Z_EXPORT PREFIX(inflateSync)(PREFIX3(stream) *strm) {
1192 unsigned len; /* number of bytes to look at or looked at */
1193 int flags; /* temporary to save header status */
1194 size_t in, out; /* temporary to save total_in and total_out */
1195 unsigned char buf[4]; /* to restore bit buffer to byte string */
1196 struct inflate_state *state;
1197
1198 /* check parameters */
1199 if (inflateStateCheck(strm))
1200 return Z_STREAM_ERROR;
1201 state = (struct inflate_state *)strm->state;
1202 if (strm->avail_in == 0 && state->bits < 8)
1203 return Z_BUF_ERROR;
1204
1205 /* if first time, start search in bit buffer */
1206 if (state->mode != SYNC) {
1207 state->mode = SYNC;
1208 state->hold <<= state->bits & 7;
1209 state->bits -= state->bits & 7;
1210 len = 0;
1211 while (state->bits >= 8) {
1212 buf[len++] = (unsigned char)(state->hold);
1213 state->hold >>= 8;
1214 state->bits -= 8;
1215 }
1216 state->have = 0;
1217 syncsearch(&(state->have), buf, len);
1218 }
1219
1220 /* search available input */
1221 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1222 strm->avail_in -= len;
1223 strm->next_in += len;
1224 strm->total_in += len;
1225
1226 /* return no joy or set up to restart inflate() on a new block */
1227 if (state->have != 4)
1228 return Z_DATA_ERROR;
1229 if (state->flags == -1)
1230 state->wrap = 0; /* if no header yet, treat as raw */
1231 else
1232 state->wrap &= ~4; /* no point in computing a check value now */
1233 flags = state->flags;
1234 in = strm->total_in;
1235 out = strm->total_out;
1236 PREFIX(inflateReset)(strm);
1237 strm->total_in = in;
1238 strm->total_out = out;
1239 state->flags = flags;
1240 state->mode = TYPE;
1241 return Z_OK;
1242 }
1243
1244 /*
1245 Returns true if inflate is currently at the end of a block generated by
1246 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1247 implementation to provide an additional safety check. PPP uses
1248 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1249 block. When decompressing, PPP checks that at the end of input packet,
1250 inflate is waiting for these length bytes.
1251 */
PREFIX(inflateSyncPoint)1252 int32_t Z_EXPORT PREFIX(inflateSyncPoint)(PREFIX3(stream) *strm) {
1253 struct inflate_state *state;
1254
1255 if (inflateStateCheck(strm))
1256 return Z_STREAM_ERROR;
1257 state = (struct inflate_state *)strm->state;
1258 return state->mode == STORED && state->bits == 0;
1259 }
1260
PREFIX(inflateCopy)1261 int32_t Z_EXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
1262 struct inflate_state *state;
1263 struct inflate_state *copy;
1264 unsigned char *window;
1265 unsigned wsize;
1266
1267 /* check input */
1268 if (inflateStateCheck(source) || dest == NULL)
1269 return Z_STREAM_ERROR;
1270 state = (struct inflate_state *)source->state;
1271
1272 /* allocate space */
1273 copy = (struct inflate_state *)
1274 ZALLOC_STATE(source, 1, sizeof(struct inflate_state));
1275 if (copy == NULL)
1276 return Z_MEM_ERROR;
1277 window = NULL;
1278 if (state->window != NULL) {
1279 window = (unsigned char *) ZALLOC_WINDOW(source, 1U << state->wbits, sizeof(unsigned char));
1280 if (window == NULL) {
1281 ZFREE_STATE(source, copy);
1282 return Z_MEM_ERROR;
1283 }
1284 }
1285
1286 /* copy state */
1287 memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
1288 ZCOPY_STATE((void *)copy, (void *)state, sizeof(struct inflate_state));
1289 copy->strm = dest;
1290 if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
1291 copy->lencode = copy->codes + (state->lencode - state->codes);
1292 copy->distcode = copy->codes + (state->distcode - state->codes);
1293 }
1294 copy->next = copy->codes + (state->next - state->codes);
1295 if (window != NULL) {
1296 wsize = 1U << state->wbits;
1297 memcpy(window, state->window, wsize);
1298 }
1299 copy->window = window;
1300 dest->state = (struct internal_state *)copy;
1301 return Z_OK;
1302 }
1303
PREFIX(inflateUndermine)1304 int32_t Z_EXPORT PREFIX(inflateUndermine)(PREFIX3(stream) *strm, int32_t subvert) {
1305 struct inflate_state *state;
1306
1307 if (inflateStateCheck(strm))
1308 return Z_STREAM_ERROR;
1309 state = (struct inflate_state *)strm->state;
1310 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1311 state->sane = !subvert;
1312 return Z_OK;
1313 #else
1314 (void)subvert;
1315 state->sane = 1;
1316 return Z_DATA_ERROR;
1317 #endif
1318 }
1319
PREFIX(inflateValidate)1320 int32_t Z_EXPORT PREFIX(inflateValidate)(PREFIX3(stream) *strm, int32_t check) {
1321 struct inflate_state *state;
1322
1323 if (inflateStateCheck(strm))
1324 return Z_STREAM_ERROR;
1325 state = (struct inflate_state *)strm->state;
1326 if (check && state->wrap)
1327 state->wrap |= 4;
1328 else
1329 state->wrap &= ~4;
1330 return Z_OK;
1331 }
1332
PREFIX(inflateMark)1333 long Z_EXPORT PREFIX(inflateMark)(PREFIX3(stream) *strm) {
1334 struct inflate_state *state;
1335
1336 if (inflateStateCheck(strm))
1337 return -65536;
1338 INFLATE_MARK_HOOK(strm); /* hook for IBM Z DFLTCC */
1339 state = (struct inflate_state *)strm->state;
1340 return ((long)(state->back) << 16) + (state->mode == COPY ? state->length :
1341 (state->mode == MATCH ? state->was - state->length : 0));
1342 }
1343
PREFIX(inflateCodesUsed)1344 unsigned long Z_EXPORT PREFIX(inflateCodesUsed)(PREFIX3(stream) *strm) {
1345 struct inflate_state *state;
1346 if (strm == NULL || strm->state == NULL)
1347 return (unsigned long)-1;
1348 state = (struct inflate_state *)strm->state;
1349 return (unsigned long)(state->next - state->codes);
1350 }
1351