Debian Codegraph @

ext/zlib-ng/inflate.c

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