zlib-streams 1.0.6 → 1.0.7

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