zlib-streams 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/inflate.c ADDED
@@ -0,0 +1,1573 @@
1
+ /* inflate.c -- zlib decompression
2
+ * Copyright (C) 1995-2022 Mark Adler
3
+ * Copyright (C) 2026 Gildas Lormeau (deflate64 support)
4
+ * For conditions of distribution and use, see copyright notice in zlib.h
5
+ *
6
+ * This copy of zlib's inflate.c also decodes raw deflate64 streams, selected
7
+ * with inflateInit2(strm, -16). The deflate64 mode uses a 64K sliding
8
+ * window, the extended length code 285 (16 extra bits, base 3) and the two
9
+ * extra distance codes 30 and 31, as decoded by the tables that
10
+ * inflate_table() builds when its deflate64 argument is set. BUILDFIXED is
11
+ * forced below because both fixed-code table sets are built at run time.
12
+ */
13
+
14
+ /*
15
+ * Change history:
16
+ *
17
+ * 1.2.beta0 24 Nov 2002
18
+ * - First version -- complete rewrite of inflate to simplify code, avoid
19
+ * creation of window when not needed, minimize use of window when it is
20
+ * needed, make inffast.c even faster, implement gzip decoding, and to
21
+ * improve code readability and style over the previous zlib inflate code
22
+ *
23
+ * 1.2.beta1 25 Nov 2002
24
+ * - Use pointers for available input and output checking in inffast.c
25
+ * - Remove input and output counters in inffast.c
26
+ * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
27
+ * - Remove unnecessary second byte pull from length extra in inffast.c
28
+ * - Unroll direct copy to three copies per loop in inffast.c
29
+ *
30
+ * 1.2.beta2 4 Dec 2002
31
+ * - Change external routine names to reduce potential conflicts
32
+ * - Correct filename to inffixed.h for fixed tables in inflate.c
33
+ * - Make hbuf[] unsigned char to match parameter type in inflate.c
34
+ * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
35
+ * to avoid negation problem on Alphas (64 bit) in inflate.c
36
+ *
37
+ * 1.2.beta3 22 Dec 2002
38
+ * - Add comments on state->bits assertion in inffast.c
39
+ * - Add comments on op field in inftrees.h
40
+ * - Fix bug in reuse of allocated window after inflateReset()
41
+ * - Remove bit fields--back to byte structure for speed
42
+ * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
43
+ * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
44
+ * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
45
+ * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
46
+ * - Use local copies of stream next and avail values, as well as local bit
47
+ * buffer and bit count in inflate()--for speed when inflate_fast() not used
48
+ *
49
+ * 1.2.beta4 1 Jan 2003
50
+ * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
51
+ * - Move a comment on output buffer sizes from inffast.c to inflate.c
52
+ * - Add comments in inffast.c to introduce the inflate_fast() routine
53
+ * - Rearrange window copies in inflate_fast() for speed and simplification
54
+ * - Unroll last copy for window match in inflate_fast()
55
+ * - Use local copies of window variables in inflate_fast() for speed
56
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
57
+ * - Make op and len in inflate_fast() unsigned for consistency
58
+ * - Add FAR to lcode and dcode declarations in inflate_fast()
59
+ * - Simplified bad distance check in inflate_fast()
60
+ * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
61
+ * source file infback.c to provide a call-back interface to inflate for
62
+ * programs like gzip and unzip -- uses window as output buffer to avoid
63
+ * window copying
64
+ *
65
+ * 1.2.beta5 1 Jan 2003
66
+ * - Improved inflateBack() interface to allow the caller to provide initial
67
+ * input in strm.
68
+ * - Fixed stored blocks bug in inflateBack()
69
+ *
70
+ * 1.2.beta6 4 Jan 2003
71
+ * - Added comments in inffast.c on effectiveness of POSTINC
72
+ * - Typecasting all around to reduce compiler warnings
73
+ * - Changed loops from while (1) or do {} while (1) to for (;;), again to
74
+ * make compilers happy
75
+ * - Changed type of window in inflateBackInit() to unsigned char *
76
+ *
77
+ * 1.2.beta7 27 Jan 2003
78
+ * - Changed many types to unsigned or unsigned short to avoid warnings
79
+ * - Added inflateCopy() function
80
+ *
81
+ * 1.2.0 9 Mar 2003
82
+ * - Changed inflateBack() interface to provide separate opaque descriptors
83
+ * for the in() and out() functions
84
+ * - Changed inflateBack() argument and in_func typedef to swap the length
85
+ * and buffer address return values for the input function
86
+ * - Check next_in and next_out for Z_NULL on entry to inflate()
87
+ *
88
+ * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
89
+ */
90
+
91
+ #include "zutil.h"
92
+ #include "inftrees.h"
93
+ #include "inflate.h"
94
+ #include "inffast_chunk.h"
95
+ #include "chunkcopy.h"
96
+
97
+ #ifndef BUILDFIXED
98
+ # define BUILDFIXED
99
+ #endif
100
+
101
+ local int inflateStateCheck(z_streamp strm) {
102
+ struct inflate_state FAR *state;
103
+ if (strm == Z_NULL ||
104
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
105
+ return 1;
106
+ state = (struct inflate_state FAR *)strm->state;
107
+ if (state == Z_NULL || state->strm != strm ||
108
+ state->mode < HEAD || state->mode > SYNC)
109
+ return 1;
110
+ return 0;
111
+ }
112
+
113
+ int ZEXPORT inflateResetKeep(z_streamp strm) {
114
+ struct inflate_state FAR *state;
115
+
116
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
117
+ state = (struct inflate_state FAR *)strm->state;
118
+ strm->total_in = strm->total_out = state->total = 0;
119
+ strm->msg = Z_NULL;
120
+ if (state->wrap) /* to support ill-conceived Java test suite */
121
+ strm->adler = state->wrap & 1;
122
+ state->mode = HEAD;
123
+ state->last = 0;
124
+ state->havedict = 0;
125
+ state->flags = -1;
126
+ state->dmax = state->deflate64 ? 65536U : 32768U;
127
+ state->head = Z_NULL;
128
+ state->hold = 0;
129
+ state->bits = 0;
130
+ state->lencode = state->distcode = state->next = state->codes;
131
+ state->sane = 1;
132
+ state->back = -1;
133
+ Tracev((stderr, "inflate: reset\n"));
134
+ return Z_OK;
135
+ }
136
+
137
+ int ZEXPORT inflateReset(z_streamp strm) {
138
+ struct inflate_state FAR *state;
139
+
140
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
141
+ state = (struct inflate_state FAR *)strm->state;
142
+ state->wsize = 0;
143
+ state->whave = 0;
144
+ state->wnext = 0;
145
+ return inflateResetKeep(strm);
146
+ }
147
+
148
+ int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
149
+ int wrap;
150
+ struct inflate_state FAR *state;
151
+
152
+ /* get the state */
153
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
154
+ state = (struct inflate_state FAR *)strm->state;
155
+
156
+ /* extract wrap request from windowBits parameter */
157
+ if (windowBits < 0) {
158
+ if (windowBits < -16)
159
+ return Z_STREAM_ERROR;
160
+ wrap = 0;
161
+ state->deflate64 = windowBits == -16;
162
+ windowBits = -windowBits;
163
+ }
164
+ else {
165
+ wrap = (windowBits >> 4) + 5;
166
+ state->deflate64 = 0;
167
+ #ifdef GUNZIP
168
+ if (windowBits < 48)
169
+ windowBits &= 15;
170
+ #endif
171
+ }
172
+
173
+ /* set number of window bits, free window if different */
174
+ if (windowBits && (windowBits < 8 || windowBits > (state->deflate64 ? 16 : 15)))
175
+ return Z_STREAM_ERROR;
176
+ if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
177
+ ZFREE(strm, state->window);
178
+ state->window = Z_NULL;
179
+ }
180
+
181
+ /* update state and reset the rest of it */
182
+ state->wrap = wrap;
183
+ state->wbits = (unsigned)windowBits;
184
+ return inflateReset(strm);
185
+ }
186
+
187
+ int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
188
+ const char *version, int stream_size) {
189
+ int ret;
190
+ struct inflate_state FAR *state;
191
+
192
+ if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
193
+ stream_size != (int)(sizeof(z_stream)))
194
+ return Z_VERSION_ERROR;
195
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
196
+ strm->msg = Z_NULL; /* in case we return an error */
197
+ if (strm->zalloc == (alloc_func)0) {
198
+ #ifdef Z_SOLO
199
+ return Z_STREAM_ERROR;
200
+ #else
201
+ strm->zalloc = zcalloc;
202
+ strm->opaque = (voidpf)0;
203
+ #endif
204
+ }
205
+ if (strm->zfree == (free_func)0)
206
+ #ifdef Z_SOLO
207
+ return Z_STREAM_ERROR;
208
+ #else
209
+ strm->zfree = zcfree;
210
+ #endif
211
+ state = (struct inflate_state FAR *)
212
+ ZALLOC(strm, 1, sizeof(struct inflate_state));
213
+ if (state == Z_NULL) return Z_MEM_ERROR;
214
+ Tracev((stderr, "inflate: allocated\n"));
215
+ strm->state = (struct internal_state FAR *)state;
216
+ state->strm = strm;
217
+ state->window = Z_NULL;
218
+ state->mode = HEAD; /* to pass state test in inflateReset2() */
219
+ state->check = 1L; /* 1L is the result of adler32() zero length data */
220
+ ret = inflateReset2(strm, windowBits);
221
+ if (ret != Z_OK) {
222
+ ZFREE(strm, state);
223
+ strm->state = Z_NULL;
224
+ }
225
+ return ret;
226
+ }
227
+
228
+ int ZEXPORT inflateInit_(z_streamp strm, const char *version,
229
+ int stream_size) {
230
+ return inflateInit2_(strm, DEF_WBITS, version, stream_size);
231
+ }
232
+
233
+ int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
234
+ struct inflate_state FAR *state;
235
+
236
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
237
+ if (bits == 0)
238
+ return Z_OK;
239
+ state = (struct inflate_state FAR *)strm->state;
240
+ if (bits < 0) {
241
+ state->hold = 0;
242
+ state->bits = 0;
243
+ return Z_OK;
244
+ }
245
+ if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
246
+ value &= (1L << bits) - 1;
247
+ state->hold += (unsigned)value << state->bits;
248
+ state->bits += (uInt)bits;
249
+ return Z_OK;
250
+ }
251
+
252
+ /*
253
+ Return state with length and distance decoding tables and index sizes set to
254
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
255
+ If BUILDFIXED is defined, then instead this routine builds the tables the
256
+ first time it's called, and returns those tables the first time and
257
+ thereafter. This reduces the size of the code by about 2K bytes, in
258
+ exchange for a little execution time. However, BUILDFIXED should not be
259
+ used for threaded applications, since the rewriting of the tables and virgin
260
+ may not be thread-safe.
261
+ */
262
+ local void fixedtables(struct inflate_state FAR *state) {
263
+ static int virgin[2] = {1, 1};
264
+ static code *lenfix[2], *distfix[2];
265
+ static code fixed[2][544];
266
+ int deflate64 = state->deflate64;
267
+
268
+ /* build fixed huffman tables if first call (may not be thread safe) */
269
+ if (virgin[deflate64]) {
270
+ unsigned sym, bits;
271
+ code *next;
272
+
273
+ /* literal/length table */
274
+ sym = 0;
275
+ while (sym < 144) state->lens[sym++] = 8;
276
+ while (sym < 256) state->lens[sym++] = 9;
277
+ while (sym < 280) state->lens[sym++] = 7;
278
+ while (sym < 288) state->lens[sym++] = 8;
279
+ next = fixed[deflate64];
280
+ lenfix[deflate64] = next;
281
+ bits = 9;
282
+ inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work,
283
+ deflate64);
284
+
285
+ /* distance table */
286
+ sym = 0;
287
+ while (sym < 32) state->lens[sym++] = 5;
288
+ distfix[deflate64] = next;
289
+ bits = 5;
290
+ inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work,
291
+ deflate64);
292
+
293
+ /* do this just once */
294
+ virgin[deflate64] = 0;
295
+ }
296
+ state->lencode = lenfix[deflate64];
297
+ state->lenbits = 9;
298
+ state->distcode = distfix[deflate64];
299
+ state->distbits = 5;
300
+ }
301
+
302
+ #ifdef MAKEFIXED
303
+ #include <stdio.h>
304
+
305
+ /*
306
+ Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
307
+ defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
308
+ those tables to stdout, which would be piped to inffixed.h. A small program
309
+ can simply call makefixed to do this:
310
+
311
+ void makefixed(void);
312
+
313
+ int main(void)
314
+ {
315
+ makefixed();
316
+ return 0;
317
+ }
318
+
319
+ Then that can be linked with zlib built with MAKEFIXED defined and run:
320
+
321
+ a.out > inffixed.h
322
+ */
323
+ void makefixed(void)
324
+ {
325
+ unsigned low, size;
326
+ struct inflate_state state;
327
+
328
+ fixedtables(&state);
329
+ puts(" /* inffixed.h -- table for decoding fixed codes");
330
+ puts(" * Generated automatically by makefixed().");
331
+ puts(" */");
332
+ puts("");
333
+ puts(" /* WARNING: this file should *not* be used by applications.");
334
+ puts(" It is part of the implementation of this library and is");
335
+ puts(" subject to change. Applications should only use zlib.h.");
336
+ puts(" */");
337
+ puts("");
338
+ size = 1U << 9;
339
+ printf(" static const code lenfix[%u] = {", size);
340
+ low = 0;
341
+ for (;;) {
342
+ if ((low % 7) == 0) printf("\n ");
343
+ printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
344
+ state.lencode[low].bits, state.lencode[low].val);
345
+ if (++low == size) break;
346
+ putchar(',');
347
+ }
348
+ puts("\n };");
349
+ size = 1U << 5;
350
+ printf("\n static const code distfix[%u] = {", size);
351
+ low = 0;
352
+ for (;;) {
353
+ if ((low % 6) == 0) printf("\n ");
354
+ printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
355
+ state.distcode[low].val);
356
+ if (++low == size) break;
357
+ putchar(',');
358
+ }
359
+ puts("\n };");
360
+ }
361
+ #endif /* MAKEFIXED */
362
+
363
+ /*
364
+ Update the window with the last wsize (normally 32K) bytes written before
365
+ returning. If window does not exist yet, create it. This is only called
366
+ when a window is already in use, or when output has been written during this
367
+ inflate call, but the end of the deflate stream has not been reached yet.
368
+ It is also called to create a window for dictionary data when a dictionary
369
+ is loaded.
370
+
371
+ Providing output buffers larger than 32K to inflate() should provide a speed
372
+ advantage, since only the last 32K of output is copied to the sliding window
373
+ upon return from inflate(), and since all distances after the first 32K of
374
+ output will fall in the output data, making match copies simpler and faster.
375
+ The advantage may be dependent on the size of the processor's data caches.
376
+ */
377
+ local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
378
+ struct inflate_state FAR *state;
379
+ unsigned dist;
380
+
381
+ state = (struct inflate_state FAR *)strm->state;
382
+
383
+ /* if it hasn't been done already, allocate space for the window */
384
+ if (state->window == Z_NULL) {
385
+ unsigned wsize = 1U << state->wbits;
386
+ state->window = (unsigned char FAR *)
387
+ ZALLOC(strm, wsize + CHUNKCOPY_CHUNK_SIZE,
388
+ sizeof(unsigned char));
389
+ if (state->window == Z_NULL) return 1;
390
+ #ifdef INFLATE_CLEAR_UNUSED_UNDEFINED
391
+ /* Copies from the overflow portion of this buffer are undefined and
392
+ may cause analysis tools to raise a warning if we don't initialize
393
+ it. However, this undefined data overwrites other undefined data
394
+ and is subsequently either overwritten or left deliberately
395
+ undefined at the end of decode; so there's really no point.
396
+ */
397
+ zmemzero(state->window + wsize, CHUNKCOPY_CHUNK_SIZE);
398
+ #endif
399
+ }
400
+
401
+ /* if window not in use yet, initialize */
402
+ if (state->wsize == 0) {
403
+ state->wsize = 1U << state->wbits;
404
+ state->wnext = 0;
405
+ state->whave = 0;
406
+ }
407
+
408
+ /* copy state->wsize or less output bytes into the circular window */
409
+ if (copy >= state->wsize) {
410
+ zmemcpy(state->window, end - state->wsize, state->wsize);
411
+ state->wnext = 0;
412
+ state->whave = state->wsize;
413
+ }
414
+ else {
415
+ dist = state->wsize - state->wnext;
416
+ if (dist > copy) dist = copy;
417
+ zmemcpy(state->window + state->wnext, end - copy, dist);
418
+ copy -= dist;
419
+ if (copy) {
420
+ zmemcpy(state->window, end - copy, copy);
421
+ state->wnext = copy;
422
+ state->whave = state->wsize;
423
+ }
424
+ else {
425
+ state->wnext += dist;
426
+ if (state->wnext == state->wsize) state->wnext = 0;
427
+ if (state->whave < state->wsize) state->whave += dist;
428
+ }
429
+ }
430
+ return 0;
431
+ }
432
+
433
+ /* Macros for inflate(): */
434
+
435
+ /* check function to use adler32() for zlib or crc32() for gzip */
436
+ #ifdef GUNZIP
437
+ # define UPDATE_CHECK(check, buf, len) \
438
+ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
439
+ #else
440
+ # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
441
+ #endif
442
+
443
+ /* check macros for header crc */
444
+ #ifdef GUNZIP
445
+ # define CRC2(check, word) \
446
+ do { \
447
+ hbuf[0] = (unsigned char)(word); \
448
+ hbuf[1] = (unsigned char)((word) >> 8); \
449
+ check = crc32(check, hbuf, 2); \
450
+ } while (0)
451
+
452
+ # define CRC4(check, word) \
453
+ do { \
454
+ hbuf[0] = (unsigned char)(word); \
455
+ hbuf[1] = (unsigned char)((word) >> 8); \
456
+ hbuf[2] = (unsigned char)((word) >> 16); \
457
+ hbuf[3] = (unsigned char)((word) >> 24); \
458
+ check = crc32(check, hbuf, 4); \
459
+ } while (0)
460
+ #endif
461
+
462
+ /* Load registers with state in inflate() for speed */
463
+ #define LOAD() \
464
+ do { \
465
+ put = strm->next_out; \
466
+ left = strm->avail_out; \
467
+ next = strm->next_in; \
468
+ have = strm->avail_in; \
469
+ hold = state->hold; \
470
+ bits = state->bits; \
471
+ } while (0)
472
+
473
+ /* Restore state from registers in inflate() */
474
+ #define RESTORE() \
475
+ do { \
476
+ strm->next_out = put; \
477
+ strm->avail_out = left; \
478
+ strm->next_in = next; \
479
+ strm->avail_in = have; \
480
+ state->hold = hold; \
481
+ state->bits = bits; \
482
+ } while (0)
483
+
484
+ /* Clear the input bit accumulator */
485
+ #define INITBITS() \
486
+ do { \
487
+ hold = 0; \
488
+ bits = 0; \
489
+ } while (0)
490
+
491
+ /* Get a byte of input into the bit accumulator, or return from inflate()
492
+ if there is no input available. */
493
+ #define PULLBYTE() \
494
+ do { \
495
+ if (have == 0) goto inf_leave; \
496
+ have--; \
497
+ hold += (unsigned long)(*next++) << bits; \
498
+ bits += 8; \
499
+ } while (0)
500
+
501
+ /* Assure that there are at least n bits in the bit accumulator. If there is
502
+ not enough available input to do that, then return from inflate(). */
503
+ #define NEEDBITS(n) \
504
+ do { \
505
+ while (bits < (unsigned)(n)) \
506
+ PULLBYTE(); \
507
+ } while (0)
508
+
509
+ /* Return the low n bits of the bit accumulator (n < 16) */
510
+ #define BITS(n) \
511
+ ((unsigned)hold & ((1U << (n)) - 1))
512
+
513
+ /* Remove n bits from the bit accumulator */
514
+ #define DROPBITS(n) \
515
+ do { \
516
+ hold >>= (n); \
517
+ bits -= (unsigned)(n); \
518
+ } while (0)
519
+
520
+ /* Remove zero to seven bits as needed to go to a byte boundary */
521
+ #define BYTEBITS() \
522
+ do { \
523
+ hold >>= bits & 7; \
524
+ bits -= bits & 7; \
525
+ } while (0)
526
+
527
+ /*
528
+ inflate() uses a state machine to process as much input data and generate as
529
+ much output data as possible before returning. The state machine is
530
+ structured roughly as follows:
531
+
532
+ for (;;) switch (state) {
533
+ ...
534
+ case STATEn:
535
+ if (not enough input data or output space to make progress)
536
+ return;
537
+ ... make progress ...
538
+ state = STATEm;
539
+ break;
540
+ ...
541
+ }
542
+
543
+ so when inflate() is called again, the same case is attempted again, and
544
+ if the appropriate resources are provided, the machine proceeds to the
545
+ next state. The NEEDBITS() macro is usually the way the state evaluates
546
+ whether it can proceed or should return. NEEDBITS() does the return if
547
+ the requested bits are not available. The typical use of the BITS macros
548
+ is:
549
+
550
+ NEEDBITS(n);
551
+ ... do something with BITS(n) ...
552
+ DROPBITS(n);
553
+
554
+ where NEEDBITS(n) either returns from inflate() if there isn't enough
555
+ input left to load n bits into the accumulator, or it continues. BITS(n)
556
+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
557
+ the low n bits off the accumulator. INITBITS() clears the accumulator
558
+ and sets the number of available bits to zero. BYTEBITS() discards just
559
+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
560
+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
561
+
562
+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
563
+ if there is no input available. The decoding of variable length codes uses
564
+ PULLBYTE() directly in order to pull just enough bytes to decode the next
565
+ code, and no more.
566
+
567
+ Some states loop until they get enough input, making sure that enough
568
+ state information is maintained to continue the loop where it left off
569
+ if NEEDBITS() returns in the loop. For example, want, need, and keep
570
+ would all have to actually be part of the saved state in case NEEDBITS()
571
+ returns:
572
+
573
+ case STATEw:
574
+ while (want < need) {
575
+ NEEDBITS(n);
576
+ keep[want++] = BITS(n);
577
+ DROPBITS(n);
578
+ }
579
+ state = STATEx;
580
+ case STATEx:
581
+
582
+ As shown above, if the next state is also the next case, then the break
583
+ is omitted.
584
+
585
+ A state may also return if there is not enough output space available to
586
+ complete that state. Those states are copying stored data, writing a
587
+ literal byte, and copying a matching string.
588
+
589
+ When returning, a "goto inf_leave" is used to update the total counters,
590
+ update the check value, and determine whether any progress has been made
591
+ during that inflate() call in order to return the proper return code.
592
+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
593
+ When there is a window, goto inf_leave will update the window with the last
594
+ output written. If a goto inf_leave occurs in the middle of decompression
595
+ and there is no window currently, goto inf_leave will create one and copy
596
+ output to the window for the next call of inflate().
597
+
598
+ In this implementation, the flush parameter of inflate() only affects the
599
+ return code (per zlib.h). inflate() always writes as much as possible to
600
+ strm->next_out, given the space available and the provided input--the effect
601
+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
602
+ the allocation of and copying into a sliding window until necessary, which
603
+ provides the effect documented in zlib.h for Z_FINISH when the entire input
604
+ stream available. So the only thing the flush parameter actually does is:
605
+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
606
+ will return Z_BUF_ERROR if it has not reached the end of the stream.
607
+ */
608
+
609
+ int ZEXPORT inflate(z_streamp strm, int flush) {
610
+ struct inflate_state FAR *state;
611
+ z_const unsigned char FAR *next; /* next input */
612
+ unsigned char FAR *put; /* next output */
613
+ unsigned have, left; /* available input and output */
614
+ unsigned long hold; /* bit buffer */
615
+ unsigned bits; /* bits in bit buffer */
616
+ unsigned in, out; /* save starting available input and output */
617
+ unsigned copy; /* number of stored or match bytes to copy */
618
+ unsigned char FAR *from; /* where to copy match bytes from */
619
+ code here; /* current decoding table entry */
620
+ code last; /* parent table entry */
621
+ unsigned len; /* length to copy for repeats, bits to drop */
622
+ int ret; /* return code */
623
+ #ifdef GUNZIP
624
+ unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
625
+ #endif
626
+ static const unsigned short order[19] = /* permutation of code lengths */
627
+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
628
+
629
+ if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
630
+ (strm->next_in == Z_NULL && strm->avail_in != 0))
631
+ return Z_STREAM_ERROR;
632
+
633
+ state = (struct inflate_state FAR *)strm->state;
634
+ if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
635
+ LOAD();
636
+ in = have;
637
+ out = left;
638
+ ret = Z_OK;
639
+ for (;;)
640
+ switch (state->mode) {
641
+ case HEAD:
642
+ if (state->wrap == 0) {
643
+ state->mode = TYPEDO;
644
+ break;
645
+ }
646
+ NEEDBITS(16);
647
+ #ifdef GUNZIP
648
+ if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
649
+ if (state->wbits == 0)
650
+ state->wbits = 15;
651
+ state->check = crc32(0L, Z_NULL, 0);
652
+ CRC2(state->check, hold);
653
+ INITBITS();
654
+ state->mode = FLAGS;
655
+ break;
656
+ }
657
+ if (state->head != Z_NULL)
658
+ state->head->done = -1;
659
+ if (!(state->wrap & 1) || /* check if zlib header allowed */
660
+ #else
661
+ if (
662
+ #endif
663
+ ((BITS(8) << 8) + (hold >> 8)) % 31) {
664
+ strm->msg = (char *)"incorrect header check";
665
+ state->mode = BAD;
666
+ break;
667
+ }
668
+ if (BITS(4) != Z_DEFLATED) {
669
+ strm->msg = (char *)"unknown compression method";
670
+ state->mode = BAD;
671
+ break;
672
+ }
673
+ DROPBITS(4);
674
+ len = BITS(4) + 8;
675
+ if (state->wbits == 0)
676
+ state->wbits = len;
677
+ if (len > 15 || len > state->wbits) {
678
+ strm->msg = (char *)"invalid window size";
679
+ state->mode = BAD;
680
+ break;
681
+ }
682
+ state->dmax = 1U << len;
683
+ state->flags = 0; /* indicate zlib header */
684
+ Tracev((stderr, "inflate: zlib header ok\n"));
685
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
686
+ state->mode = hold & 0x200 ? DICTID : TYPE;
687
+ INITBITS();
688
+ break;
689
+ #ifdef GUNZIP
690
+ case FLAGS:
691
+ NEEDBITS(16);
692
+ state->flags = (int)(hold);
693
+ if ((state->flags & 0xff) != Z_DEFLATED) {
694
+ strm->msg = (char *)"unknown compression method";
695
+ state->mode = BAD;
696
+ break;
697
+ }
698
+ if (state->flags & 0xe000) {
699
+ strm->msg = (char *)"unknown header flags set";
700
+ state->mode = BAD;
701
+ break;
702
+ }
703
+ if (state->head != Z_NULL)
704
+ state->head->text = (int)((hold >> 8) & 1);
705
+ if ((state->flags & 0x0200) && (state->wrap & 4))
706
+ CRC2(state->check, hold);
707
+ INITBITS();
708
+ state->mode = TIME;
709
+ /* fallthrough */
710
+ case TIME:
711
+ NEEDBITS(32);
712
+ if (state->head != Z_NULL)
713
+ state->head->time = hold;
714
+ if ((state->flags & 0x0200) && (state->wrap & 4))
715
+ CRC4(state->check, hold);
716
+ INITBITS();
717
+ state->mode = OS;
718
+ /* fallthrough */
719
+ case OS:
720
+ NEEDBITS(16);
721
+ if (state->head != Z_NULL) {
722
+ state->head->xflags = (int)(hold & 0xff);
723
+ state->head->os = (int)(hold >> 8);
724
+ }
725
+ if ((state->flags & 0x0200) && (state->wrap & 4))
726
+ CRC2(state->check, hold);
727
+ INITBITS();
728
+ state->mode = EXLEN;
729
+ /* fallthrough */
730
+ case EXLEN:
731
+ if (state->flags & 0x0400) {
732
+ NEEDBITS(16);
733
+ state->length = (unsigned)(hold);
734
+ if (state->head != Z_NULL)
735
+ state->head->extra_len = (unsigned)hold;
736
+ if ((state->flags & 0x0200) && (state->wrap & 4))
737
+ CRC2(state->check, hold);
738
+ INITBITS();
739
+ }
740
+ else if (state->head != Z_NULL)
741
+ state->head->extra = Z_NULL;
742
+ state->mode = EXTRA;
743
+ /* fallthrough */
744
+ case EXTRA:
745
+ if (state->flags & 0x0400) {
746
+ copy = state->length;
747
+ if (copy > have) copy = have;
748
+ if (copy) {
749
+ if (state->head != Z_NULL &&
750
+ state->head->extra != Z_NULL &&
751
+ (len = state->head->extra_len - state->length) <
752
+ state->head->extra_max) {
753
+ zmemcpy(state->head->extra + len, next,
754
+ len + copy > state->head->extra_max ?
755
+ state->head->extra_max - len : copy);
756
+ }
757
+ if ((state->flags & 0x0200) && (state->wrap & 4))
758
+ state->check = crc32(state->check, next, copy);
759
+ have -= copy;
760
+ next += copy;
761
+ state->length -= copy;
762
+ }
763
+ if (state->length) goto inf_leave;
764
+ }
765
+ state->length = 0;
766
+ state->mode = NAME;
767
+ /* fallthrough */
768
+ case NAME:
769
+ if (state->flags & 0x0800) {
770
+ if (have == 0) goto inf_leave;
771
+ copy = 0;
772
+ do {
773
+ len = (unsigned)(next[copy++]);
774
+ if (state->head != Z_NULL &&
775
+ state->head->name != Z_NULL &&
776
+ state->length < state->head->name_max)
777
+ state->head->name[state->length++] = (Bytef)len;
778
+ } while (len && copy < have);
779
+ if ((state->flags & 0x0200) && (state->wrap & 4))
780
+ state->check = crc32(state->check, next, copy);
781
+ have -= copy;
782
+ next += copy;
783
+ if (len) goto inf_leave;
784
+ }
785
+ else if (state->head != Z_NULL)
786
+ state->head->name = Z_NULL;
787
+ state->length = 0;
788
+ state->mode = COMMENT;
789
+ /* fallthrough */
790
+ case COMMENT:
791
+ if (state->flags & 0x1000) {
792
+ if (have == 0) goto inf_leave;
793
+ copy = 0;
794
+ do {
795
+ len = (unsigned)(next[copy++]);
796
+ if (state->head != Z_NULL &&
797
+ state->head->comment != Z_NULL &&
798
+ state->length < state->head->comm_max)
799
+ state->head->comment[state->length++] = (Bytef)len;
800
+ } while (len && copy < have);
801
+ if ((state->flags & 0x0200) && (state->wrap & 4))
802
+ state->check = crc32(state->check, next, copy);
803
+ have -= copy;
804
+ next += copy;
805
+ if (len) goto inf_leave;
806
+ }
807
+ else if (state->head != Z_NULL)
808
+ state->head->comment = Z_NULL;
809
+ state->mode = HCRC;
810
+ /* fallthrough */
811
+ case HCRC:
812
+ if (state->flags & 0x0200) {
813
+ NEEDBITS(16);
814
+ if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
815
+ strm->msg = (char *)"header crc mismatch";
816
+ state->mode = BAD;
817
+ break;
818
+ }
819
+ INITBITS();
820
+ }
821
+ if (state->head != Z_NULL) {
822
+ state->head->hcrc = (int)((state->flags >> 9) & 1);
823
+ state->head->done = 1;
824
+ }
825
+ strm->adler = state->check = crc32(0L, Z_NULL, 0);
826
+ state->mode = TYPE;
827
+ break;
828
+ #endif
829
+ case DICTID:
830
+ NEEDBITS(32);
831
+ strm->adler = state->check = ZSWAP32(hold);
832
+ INITBITS();
833
+ state->mode = DICT;
834
+ /* fallthrough */
835
+ case DICT:
836
+ if (state->havedict == 0) {
837
+ RESTORE();
838
+ return Z_NEED_DICT;
839
+ }
840
+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
841
+ state->mode = TYPE;
842
+ /* fallthrough */
843
+ case TYPE:
844
+ if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
845
+ /* fallthrough */
846
+ case TYPEDO:
847
+ if (state->last) {
848
+ BYTEBITS();
849
+ state->mode = CHECK;
850
+ break;
851
+ }
852
+ NEEDBITS(3);
853
+ state->last = BITS(1);
854
+ DROPBITS(1);
855
+ switch (BITS(2)) {
856
+ case 0: /* stored block */
857
+ Tracev((stderr, "inflate: stored block%s\n",
858
+ state->last ? " (last)" : ""));
859
+ state->mode = STORED;
860
+ break;
861
+ case 1: /* fixed block */
862
+ fixedtables(state);
863
+ Tracev((stderr, "inflate: fixed codes block%s\n",
864
+ state->last ? " (last)" : ""));
865
+ state->mode = LEN_; /* decode codes */
866
+ if (flush == Z_TREES) {
867
+ DROPBITS(2);
868
+ goto inf_leave;
869
+ }
870
+ break;
871
+ case 2: /* dynamic block */
872
+ Tracev((stderr, "inflate: dynamic codes block%s\n",
873
+ state->last ? " (last)" : ""));
874
+ state->mode = TABLE;
875
+ break;
876
+ case 3:
877
+ strm->msg = (char *)"invalid block type";
878
+ state->mode = BAD;
879
+ }
880
+ DROPBITS(2);
881
+ break;
882
+ case STORED:
883
+ BYTEBITS(); /* go to byte boundary */
884
+ NEEDBITS(32);
885
+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
886
+ strm->msg = (char *)"invalid stored block lengths";
887
+ state->mode = BAD;
888
+ break;
889
+ }
890
+ state->length = (unsigned)hold & 0xffff;
891
+ Tracev((stderr, "inflate: stored length %u\n",
892
+ state->length));
893
+ INITBITS();
894
+ state->mode = COPY_;
895
+ if (flush == Z_TREES) goto inf_leave;
896
+ /* fallthrough */
897
+ case COPY_:
898
+ state->mode = COPY;
899
+ /* fallthrough */
900
+ case COPY:
901
+ copy = state->length;
902
+ if (copy) {
903
+ if (copy > have) copy = have;
904
+ if (copy > left) copy = left;
905
+ if (copy == 0) goto inf_leave;
906
+ zmemcpy(put, next, copy);
907
+ have -= copy;
908
+ next += copy;
909
+ left -= copy;
910
+ put += copy;
911
+ state->length -= copy;
912
+ break;
913
+ }
914
+ Tracev((stderr, "inflate: stored end\n"));
915
+ state->mode = TYPE;
916
+ break;
917
+ case TABLE:
918
+ NEEDBITS(14);
919
+ state->nlen = BITS(5) + 257;
920
+ DROPBITS(5);
921
+ state->ndist = BITS(5) + 1;
922
+ DROPBITS(5);
923
+ state->ncode = BITS(4) + 4;
924
+ DROPBITS(4);
925
+ #ifndef PKZIP_BUG_WORKAROUND
926
+ if (state->nlen > 286 ||
927
+ (!state->deflate64 && state->ndist > 30)) {
928
+ strm->msg = (char *)"too many length or distance symbols";
929
+ state->mode = BAD;
930
+ break;
931
+ }
932
+ #endif
933
+ Tracev((stderr, "inflate: table sizes ok\n"));
934
+ state->have = 0;
935
+ state->mode = LENLENS;
936
+ /* fallthrough */
937
+ case LENLENS:
938
+ while (state->have < state->ncode) {
939
+ NEEDBITS(3);
940
+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
941
+ DROPBITS(3);
942
+ }
943
+ while (state->have < 19)
944
+ state->lens[order[state->have++]] = 0;
945
+ state->next = state->codes;
946
+ state->lencode = state->distcode = (const code FAR *)(state->next);
947
+ state->lenbits = 7;
948
+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
949
+ &(state->lenbits), state->work,
950
+ state->deflate64);
951
+ if (ret) {
952
+ strm->msg = (char *)"invalid code lengths set";
953
+ state->mode = BAD;
954
+ break;
955
+ }
956
+ Tracev((stderr, "inflate: code lengths ok\n"));
957
+ state->have = 0;
958
+ state->mode = CODELENS;
959
+ /* fallthrough */
960
+ case CODELENS:
961
+ while (state->have < state->nlen + state->ndist) {
962
+ for (;;) {
963
+ here = state->lencode[BITS(state->lenbits)];
964
+ if ((unsigned)(here.bits) <= bits) break;
965
+ PULLBYTE();
966
+ }
967
+ if (here.val < 16) {
968
+ DROPBITS(here.bits);
969
+ state->lens[state->have++] = here.val;
970
+ }
971
+ else {
972
+ if (here.val == 16) {
973
+ NEEDBITS(here.bits + 2);
974
+ DROPBITS(here.bits);
975
+ if (state->have == 0) {
976
+ strm->msg = (char *)"invalid bit length repeat";
977
+ state->mode = BAD;
978
+ break;
979
+ }
980
+ len = state->lens[state->have - 1];
981
+ copy = 3 + BITS(2);
982
+ DROPBITS(2);
983
+ }
984
+ else if (here.val == 17) {
985
+ NEEDBITS(here.bits + 3);
986
+ DROPBITS(here.bits);
987
+ len = 0;
988
+ copy = 3 + BITS(3);
989
+ DROPBITS(3);
990
+ }
991
+ else {
992
+ NEEDBITS(here.bits + 7);
993
+ DROPBITS(here.bits);
994
+ len = 0;
995
+ copy = 11 + BITS(7);
996
+ DROPBITS(7);
997
+ }
998
+ if (state->have + copy > state->nlen + state->ndist) {
999
+ strm->msg = (char *)"invalid bit length repeat";
1000
+ state->mode = BAD;
1001
+ break;
1002
+ }
1003
+ while (copy--)
1004
+ state->lens[state->have++] = (unsigned short)len;
1005
+ }
1006
+ }
1007
+
1008
+ /* handle error breaks in while */
1009
+ if (state->mode == BAD) break;
1010
+
1011
+ /* check for end-of-block code (better have one) */
1012
+ if (state->lens[256] == 0) {
1013
+ strm->msg = (char *)"invalid code -- missing end-of-block";
1014
+ state->mode = BAD;
1015
+ break;
1016
+ }
1017
+
1018
+ /* build code tables -- note: do not change the lenbits or distbits
1019
+ values here (10 and 9) without reading the comments in inftrees.h
1020
+ concerning the ENOUGH constants, which depend on those values */
1021
+ state->next = state->codes;
1022
+ state->lencode = (const code FAR *)(state->next);
1023
+ state->lenbits = 9;
1024
+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1025
+ &(state->lenbits), state->work,
1026
+ state->deflate64);
1027
+ if (ret) {
1028
+ strm->msg = (char *)"invalid literal/lengths set";
1029
+ state->mode = BAD;
1030
+ break;
1031
+ }
1032
+ state->distcode = (const code FAR *)(state->next);
1033
+ state->distbits = 6;
1034
+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1035
+ &(state->next), &(state->distbits), state->work,
1036
+ state->deflate64);
1037
+ if (ret) {
1038
+ strm->msg = (char *)"invalid distances set";
1039
+ state->mode = BAD;
1040
+ break;
1041
+ }
1042
+ Tracev((stderr, "inflate: codes ok\n"));
1043
+ state->mode = LEN_;
1044
+ if (flush == Z_TREES) goto inf_leave;
1045
+ /* fallthrough */
1046
+ case LEN_:
1047
+ state->mode = LEN;
1048
+ /* fallthrough */
1049
+ case LEN:
1050
+ if (!state->deflate64 && have >= INFLATE_FAST_MIN_INPUT &&
1051
+ left >= INFLATE_FAST_MIN_OUTPUT) {
1052
+ RESTORE();
1053
+ inflate_fast_chunk_(strm, out);
1054
+ LOAD();
1055
+ if (state->mode == TYPE)
1056
+ state->back = -1;
1057
+ break;
1058
+ }
1059
+ state->back = 0;
1060
+ for (;;) {
1061
+ here = state->lencode[BITS(state->lenbits)];
1062
+ if ((unsigned)(here.bits) <= bits) break;
1063
+ PULLBYTE();
1064
+ }
1065
+ if (here.op && (here.op & 0xf0) == 0) {
1066
+ last = here;
1067
+ for (;;) {
1068
+ here = state->lencode[last.val +
1069
+ (BITS(last.bits + last.op) >> last.bits)];
1070
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
1071
+ PULLBYTE();
1072
+ }
1073
+ DROPBITS(last.bits);
1074
+ state->back += last.bits;
1075
+ }
1076
+ DROPBITS(here.bits);
1077
+ state->back += here.bits;
1078
+ state->length = (unsigned)here.val;
1079
+ if ((int)(here.op) == 0) {
1080
+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1081
+ "inflate: literal '%c'\n" :
1082
+ "inflate: literal 0x%02x\n", here.val));
1083
+ state->mode = LIT;
1084
+ break;
1085
+ }
1086
+ if (here.op & 32) {
1087
+ Tracevv((stderr, "inflate: end of block\n"));
1088
+ state->back = -1;
1089
+ state->mode = TYPE;
1090
+ break;
1091
+ }
1092
+ if (here.op & 64) {
1093
+ strm->msg = (char *)"invalid literal/length code";
1094
+ state->mode = BAD;
1095
+ break;
1096
+ }
1097
+ state->extra = (unsigned)(here.op) & (state->deflate64 ? 31 : 15);
1098
+ state->mode = LENEXT;
1099
+ /* fallthrough */
1100
+ case LENEXT:
1101
+ if (state->extra) {
1102
+ NEEDBITS(state->extra);
1103
+ state->length += BITS(state->extra);
1104
+ DROPBITS(state->extra);
1105
+ state->back += state->extra;
1106
+ }
1107
+ Tracevv((stderr, "inflate: length %u\n", state->length));
1108
+ state->was = state->length;
1109
+ state->mode = DIST;
1110
+ /* fallthrough */
1111
+ case DIST:
1112
+ for (;;) {
1113
+ here = state->distcode[BITS(state->distbits)];
1114
+ if ((unsigned)(here.bits) <= bits) break;
1115
+ PULLBYTE();
1116
+ }
1117
+ if ((here.op & 0xf0) == 0) {
1118
+ last = here;
1119
+ for (;;) {
1120
+ here = state->distcode[last.val +
1121
+ (BITS(last.bits + last.op) >> last.bits)];
1122
+ if ((unsigned)(last.bits + here.bits) <= bits) break;
1123
+ PULLBYTE();
1124
+ }
1125
+ DROPBITS(last.bits);
1126
+ state->back += last.bits;
1127
+ }
1128
+ DROPBITS(here.bits);
1129
+ state->back += here.bits;
1130
+ if (here.op & 64) {
1131
+ strm->msg = (char *)"invalid distance code";
1132
+ state->mode = BAD;
1133
+ break;
1134
+ }
1135
+ state->offset = (unsigned)here.val;
1136
+ state->extra = (unsigned)(here.op) & 15;
1137
+ state->mode = DISTEXT;
1138
+ /* fallthrough */
1139
+ case DISTEXT:
1140
+ if (state->extra) {
1141
+ NEEDBITS(state->extra);
1142
+ state->offset += BITS(state->extra);
1143
+ DROPBITS(state->extra);
1144
+ state->back += state->extra;
1145
+ }
1146
+ #ifdef INFLATE_STRICT
1147
+ if (state->offset > state->dmax) {
1148
+ strm->msg = (char *)"invalid distance too far back";
1149
+ state->mode = BAD;
1150
+ break;
1151
+ }
1152
+ #endif
1153
+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
1154
+ state->mode = MATCH;
1155
+ /* fallthrough */
1156
+ case MATCH:
1157
+ if (left == 0) goto inf_leave;
1158
+ copy = out - left;
1159
+ if (state->offset > copy) { /* copy from window */
1160
+ copy = state->offset - copy;
1161
+ if (copy > state->whave) {
1162
+ if (state->sane) {
1163
+ strm->msg = (char *)"invalid distance too far back";
1164
+ state->mode = BAD;
1165
+ break;
1166
+ }
1167
+ #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1168
+ Trace((stderr, "inflate.c too far\n"));
1169
+ copy -= state->whave;
1170
+ if (copy > state->length) copy = state->length;
1171
+ if (copy > left) copy = left;
1172
+ left -= copy;
1173
+ state->length -= copy;
1174
+ do {
1175
+ *put++ = 0;
1176
+ } while (--copy);
1177
+ if (state->length == 0) state->mode = LEN;
1178
+ break;
1179
+ #endif
1180
+ }
1181
+ if (copy > state->wnext) {
1182
+ copy -= state->wnext;
1183
+ from = state->window + (state->wsize - copy);
1184
+ }
1185
+ else
1186
+ from = state->window + (state->wnext - copy);
1187
+ if (copy > state->length) copy = state->length;
1188
+ if (copy > left) copy = left;
1189
+ put = chunkcopy_safe(put, from, copy, put + left);
1190
+ }
1191
+ else { /* copy from output */
1192
+ copy = state->length;
1193
+ if (copy > left) copy = left;
1194
+ put = chunkcopy_lapped_safe(put, state->offset, copy, put + left);
1195
+ }
1196
+ left -= copy;
1197
+ state->length -= copy;
1198
+ if (state->length == 0) state->mode = LEN;
1199
+ break;
1200
+ case LIT:
1201
+ if (left == 0) goto inf_leave;
1202
+ *put++ = (unsigned char)(state->length);
1203
+ left--;
1204
+ state->mode = LEN;
1205
+ break;
1206
+ case CHECK:
1207
+ if (state->wrap) {
1208
+ NEEDBITS(32);
1209
+ out -= left;
1210
+ strm->total_out += out;
1211
+ state->total += out;
1212
+ if ((state->wrap & 4) && out)
1213
+ strm->adler = state->check =
1214
+ UPDATE_CHECK(state->check, put - out, out);
1215
+ out = left;
1216
+ if ((state->wrap & 4) && (
1217
+ #ifdef GUNZIP
1218
+ state->flags ? hold :
1219
+ #endif
1220
+ ZSWAP32(hold)) != state->check) {
1221
+ strm->msg = (char *)"incorrect data check";
1222
+ state->mode = BAD;
1223
+ break;
1224
+ }
1225
+ INITBITS();
1226
+ Tracev((stderr, "inflate: check matches trailer\n"));
1227
+ }
1228
+ #ifdef GUNZIP
1229
+ state->mode = LENGTH;
1230
+ /* fallthrough */
1231
+ case LENGTH:
1232
+ if (state->wrap && state->flags) {
1233
+ NEEDBITS(32);
1234
+ if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1235
+ strm->msg = (char *)"incorrect length check";
1236
+ state->mode = BAD;
1237
+ break;
1238
+ }
1239
+ INITBITS();
1240
+ Tracev((stderr, "inflate: length matches trailer\n"));
1241
+ }
1242
+ #endif
1243
+ state->mode = DONE;
1244
+ /* fallthrough */
1245
+ case DONE:
1246
+ ret = Z_STREAM_END;
1247
+ goto inf_leave;
1248
+ case BAD:
1249
+ ret = Z_DATA_ERROR;
1250
+ goto inf_leave;
1251
+ case MEM:
1252
+ return Z_MEM_ERROR;
1253
+ case SYNC:
1254
+ /* fallthrough */
1255
+ default:
1256
+ return Z_STREAM_ERROR;
1257
+ }
1258
+
1259
+ /*
1260
+ Return from inflate(), updating the total counts and the check value.
1261
+ If there was no progress during the inflate() call, return a buffer
1262
+ error. Call updatewindow() to create and/or update the window state.
1263
+ Note: a memory error from inflate() is non-recoverable.
1264
+ */
1265
+ inf_leave:
1266
+ #if defined(ZLIB_DEBUG)
1267
+ /* XXX(cavalcantii): I put this in place back in 2017 to help debug faulty
1268
+ * client code relying on undefined behavior when chunk_copy first landed.
1269
+ *
1270
+ * It is save to say after all these years that Chromium code is well
1271
+ * behaved and works fine with the optimization, therefore we can enable
1272
+ * this only for DEBUG builds.
1273
+ *
1274
+ * We write a defined value in the unused space to help mark
1275
+ * where the stream has ended. We don't use zeros as that can
1276
+ * mislead clients relying on undefined behavior (i.e. assuming
1277
+ * that the data is over when the buffer has a zero/null value).
1278
+ *
1279
+ * The basic idea is that if client code is not relying on the zlib context
1280
+ * to inform the amount of decompressed data, but instead reads the output
1281
+ * buffer until a zero/null is found, it will fail faster and harder
1282
+ * when the remaining of the buffer is marked with a symbol (e.g. 0x55).
1283
+ */
1284
+ if (left >= CHUNKCOPY_CHUNK_SIZE)
1285
+ memset(put, 0x55, CHUNKCOPY_CHUNK_SIZE);
1286
+ else
1287
+ memset(put, 0x55, left);
1288
+ #endif
1289
+ RESTORE();
1290
+ if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1291
+ (state->mode < CHECK || flush != Z_FINISH)))
1292
+ if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1293
+ state->mode = MEM;
1294
+ return Z_MEM_ERROR;
1295
+ }
1296
+ in -= strm->avail_in;
1297
+ out -= strm->avail_out;
1298
+ strm->total_in += in;
1299
+ strm->total_out += out;
1300
+ state->total += out;
1301
+ if ((state->wrap & 4) && out)
1302
+ strm->adler = state->check =
1303
+ UPDATE_CHECK(state->check, strm->next_out - out, out);
1304
+ strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1305
+ (state->mode == TYPE ? 128 : 0) +
1306
+ (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1307
+ if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1308
+ ret = Z_BUF_ERROR;
1309
+ return ret;
1310
+ }
1311
+
1312
+ int ZEXPORT inflateEnd(z_streamp strm) {
1313
+ struct inflate_state FAR *state;
1314
+ if (inflateStateCheck(strm))
1315
+ return Z_STREAM_ERROR;
1316
+ state = (struct inflate_state FAR *)strm->state;
1317
+ if (state->window != Z_NULL) ZFREE(strm, state->window);
1318
+ ZFREE(strm, strm->state);
1319
+ strm->state = Z_NULL;
1320
+ Tracev((stderr, "inflate: end\n"));
1321
+ return Z_OK;
1322
+ }
1323
+
1324
+ int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1325
+ uInt *dictLength) {
1326
+ struct inflate_state FAR *state;
1327
+
1328
+ /* check state */
1329
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1330
+ state = (struct inflate_state FAR *)strm->state;
1331
+
1332
+ /* copy dictionary */
1333
+ if (state->whave && dictionary != Z_NULL) {
1334
+ zmemcpy(dictionary, state->window + state->wnext,
1335
+ state->whave - state->wnext);
1336
+ zmemcpy(dictionary + state->whave - state->wnext,
1337
+ state->window, state->wnext);
1338
+ }
1339
+ if (dictLength != Z_NULL)
1340
+ *dictLength = state->whave;
1341
+ return Z_OK;
1342
+ }
1343
+
1344
+ int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1345
+ uInt dictLength) {
1346
+ struct inflate_state FAR *state;
1347
+ unsigned long dictid;
1348
+ int ret;
1349
+
1350
+ /* check state */
1351
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1352
+ state = (struct inflate_state FAR *)strm->state;
1353
+ if (state->wrap != 0 && state->mode != DICT)
1354
+ return Z_STREAM_ERROR;
1355
+
1356
+ /* check for correct dictionary identifier */
1357
+ if (state->mode == DICT) {
1358
+ dictid = adler32(0L, Z_NULL, 0);
1359
+ dictid = adler32(dictid, dictionary, dictLength);
1360
+ if (dictid != state->check)
1361
+ return Z_DATA_ERROR;
1362
+ }
1363
+
1364
+ /* copy dictionary to window using updatewindow(), which will amend the
1365
+ existing dictionary if appropriate */
1366
+ ret = updatewindow(strm, dictionary + dictLength, dictLength);
1367
+ if (ret) {
1368
+ state->mode = MEM;
1369
+ return Z_MEM_ERROR;
1370
+ }
1371
+ state->havedict = 1;
1372
+ Tracev((stderr, "inflate: dictionary set\n"));
1373
+ return Z_OK;
1374
+ }
1375
+
1376
+ int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1377
+ struct inflate_state FAR *state;
1378
+
1379
+ /* check state */
1380
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1381
+ state = (struct inflate_state FAR *)strm->state;
1382
+ if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1383
+
1384
+ /* save header structure */
1385
+ state->head = head;
1386
+ head->done = 0;
1387
+ return Z_OK;
1388
+ }
1389
+
1390
+ /*
1391
+ Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1392
+ or when out of input. When called, *have is the number of pattern bytes
1393
+ found in order so far, in 0..3. On return *have is updated to the new
1394
+ state. If on return *have equals four, then the pattern was found and the
1395
+ return value is how many bytes were read including the last byte of the
1396
+ pattern. If *have is less than four, then the pattern has not been found
1397
+ yet and the return value is len. In the latter case, syncsearch() can be
1398
+ called again with more data and the *have state. *have is initialized to
1399
+ zero for the first call.
1400
+ */
1401
+ local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1402
+ unsigned len) {
1403
+ unsigned got;
1404
+ unsigned next;
1405
+
1406
+ got = *have;
1407
+ next = 0;
1408
+ while (next < len && got < 4) {
1409
+ if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1410
+ got++;
1411
+ else if (buf[next])
1412
+ got = 0;
1413
+ else
1414
+ got = 4 - got;
1415
+ next++;
1416
+ }
1417
+ *have = got;
1418
+ return next;
1419
+ }
1420
+
1421
+ int ZEXPORT inflateSync(z_streamp strm) {
1422
+ unsigned len; /* number of bytes to look at or looked at */
1423
+ int flags; /* temporary to save header status */
1424
+ unsigned long in, out; /* temporary to save total_in and total_out */
1425
+ unsigned char buf[4]; /* to restore bit buffer to byte string */
1426
+ struct inflate_state FAR *state;
1427
+
1428
+ /* check parameters */
1429
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1430
+ state = (struct inflate_state FAR *)strm->state;
1431
+ if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1432
+
1433
+ /* if first time, start search in bit buffer */
1434
+ if (state->mode != SYNC) {
1435
+ state->mode = SYNC;
1436
+ state->hold >>= state->bits & 7;
1437
+ state->bits -= state->bits & 7;
1438
+ len = 0;
1439
+ while (state->bits >= 8) {
1440
+ buf[len++] = (unsigned char)(state->hold);
1441
+ state->hold >>= 8;
1442
+ state->bits -= 8;
1443
+ }
1444
+ state->have = 0;
1445
+ syncsearch(&(state->have), buf, len);
1446
+ }
1447
+
1448
+ /* search available input */
1449
+ len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1450
+ strm->avail_in -= len;
1451
+ strm->next_in += len;
1452
+ strm->total_in += len;
1453
+
1454
+ /* return no joy or set up to restart inflate() on a new block */
1455
+ if (state->have != 4) return Z_DATA_ERROR;
1456
+ if (state->flags == -1)
1457
+ state->wrap = 0; /* if no header yet, treat as raw */
1458
+ else
1459
+ state->wrap &= ~4; /* no point in computing a check value now */
1460
+ flags = state->flags;
1461
+ in = strm->total_in; out = strm->total_out;
1462
+ inflateReset(strm);
1463
+ strm->total_in = in; strm->total_out = out;
1464
+ state->flags = flags;
1465
+ state->mode = TYPE;
1466
+ return Z_OK;
1467
+ }
1468
+
1469
+ /*
1470
+ Returns true if inflate is currently at the end of a block generated by
1471
+ Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1472
+ implementation to provide an additional safety check. PPP uses
1473
+ Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1474
+ block. When decompressing, PPP checks that at the end of input packet,
1475
+ inflate is waiting for these length bytes.
1476
+ */
1477
+ int ZEXPORT inflateSyncPoint(z_streamp strm) {
1478
+ struct inflate_state FAR *state;
1479
+
1480
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1481
+ state = (struct inflate_state FAR *)strm->state;
1482
+ return state->mode == STORED && state->bits == 0;
1483
+ }
1484
+
1485
+ int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1486
+ struct inflate_state FAR *state;
1487
+ struct inflate_state FAR *copy;
1488
+ unsigned char FAR *window;
1489
+ unsigned wsize;
1490
+
1491
+ /* check input */
1492
+ if (inflateStateCheck(source) || dest == Z_NULL)
1493
+ return Z_STREAM_ERROR;
1494
+ state = (struct inflate_state FAR *)source->state;
1495
+
1496
+ /* allocate space */
1497
+ copy = (struct inflate_state FAR *)
1498
+ ZALLOC(source, 1, sizeof(struct inflate_state));
1499
+ if (copy == Z_NULL) return Z_MEM_ERROR;
1500
+ window = Z_NULL;
1501
+ if (state->window != Z_NULL) {
1502
+ window = (unsigned char FAR *)ZALLOC(
1503
+ source, (1U << state->wbits) + CHUNKCOPY_CHUNK_SIZE,
1504
+ sizeof(unsigned char));
1505
+ if (window == Z_NULL) {
1506
+ ZFREE(source, copy);
1507
+ return Z_MEM_ERROR;
1508
+ }
1509
+ }
1510
+
1511
+ /* copy state */
1512
+ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1513
+ zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1514
+ copy->strm = dest;
1515
+ if (state->lencode >= state->codes &&
1516
+ state->lencode <= state->codes + ENOUGH - 1) {
1517
+ copy->lencode = copy->codes + (state->lencode - state->codes);
1518
+ copy->distcode = copy->codes + (state->distcode - state->codes);
1519
+ }
1520
+ copy->next = copy->codes + (state->next - state->codes);
1521
+ if (window != Z_NULL) {
1522
+ wsize = 1U << state->wbits;
1523
+ zmemcpy(window, state->window, wsize);
1524
+ }
1525
+ copy->window = window;
1526
+ dest->state = (struct internal_state FAR *)copy;
1527
+ return Z_OK;
1528
+ }
1529
+
1530
+ int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1531
+ struct inflate_state FAR *state;
1532
+
1533
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1534
+ state = (struct inflate_state FAR *)strm->state;
1535
+ #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1536
+ state->sane = !subvert;
1537
+ return Z_OK;
1538
+ #else
1539
+ (void)subvert;
1540
+ state->sane = 1;
1541
+ return Z_DATA_ERROR;
1542
+ #endif
1543
+ }
1544
+
1545
+ int ZEXPORT inflateValidate(z_streamp strm, int check) {
1546
+ struct inflate_state FAR *state;
1547
+
1548
+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1549
+ state = (struct inflate_state FAR *)strm->state;
1550
+ if (check && state->wrap)
1551
+ state->wrap |= 4;
1552
+ else
1553
+ state->wrap &= ~4;
1554
+ return Z_OK;
1555
+ }
1556
+
1557
+ long ZEXPORT inflateMark(z_streamp strm) {
1558
+ struct inflate_state FAR *state;
1559
+
1560
+ if (inflateStateCheck(strm))
1561
+ return -(1L << 16);
1562
+ state = (struct inflate_state FAR *)strm->state;
1563
+ return (long)(((unsigned long)((long)state->back)) << 16) +
1564
+ (state->mode == COPY ? state->length :
1565
+ (state->mode == MATCH ? state->was - state->length : 0));
1566
+ }
1567
+
1568
+ unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1569
+ struct inflate_state FAR *state;
1570
+ if (inflateStateCheck(strm)) return (unsigned long)-1;
1571
+ state = (struct inflate_state FAR *)strm->state;
1572
+ return (unsigned long)(state->next - state->codes);
1573
+ }