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