zlib-streams 1.0.0 → 1.0.2

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 (120) hide show
  1. package/Makefile +4 -4
  2. package/README.md +1 -1
  3. package/build/debug/src/inffast9.d +9 -0
  4. package/build/debug/src/inffast9.o +0 -0
  5. package/build/debug/src/inflate9.d +12 -0
  6. package/build/debug/src/inflate9.o +0 -0
  7. package/build/debug/src/zlib/adler32.d +5 -0
  8. package/build/debug/src/zlib/adler32.o +0 -0
  9. package/build/debug/src/zlib/contrib/infback9/infback9.d +13 -0
  10. package/build/debug/src/zlib/contrib/infback9/infback9.o +0 -0
  11. package/build/debug/src/zlib/contrib/infback9/inftree9.d +7 -0
  12. package/build/debug/src/zlib/contrib/infback9/inftree9.o +0 -0
  13. package/build/debug/src/zlib/crc32.d +6 -0
  14. package/build/debug/src/zlib/crc32.o +0 -0
  15. package/build/debug/src/zlib/infback.d +10 -0
  16. package/build/debug/src/zlib/infback.o +0 -0
  17. package/build/debug/src/zlib/inffast.d +9 -0
  18. package/build/debug/src/zlib/inffast.o +0 -0
  19. package/build/debug/src/zlib/inflate.d +10 -0
  20. package/build/debug/src/zlib/inflate.o +0 -0
  21. package/build/debug/src/zlib/inftrees.d +6 -0
  22. package/build/debug/src/zlib/inftrees.o +0 -0
  23. package/build/debug/src/zlib/trees.d +7 -0
  24. package/build/debug/src/zlib/trees.o +0 -0
  25. package/build/debug/src/zlib/zutil.d +6 -0
  26. package/build/debug/src/zlib/zutil.o +0 -0
  27. package/build/src/inffast9.d +9 -0
  28. package/build/src/inffast9.o +0 -0
  29. package/build/src/inflate9.d +12 -0
  30. package/build/src/inflate9.o +0 -0
  31. package/build/src/zlib/adler32.d +5 -0
  32. package/build/src/zlib/adler32.o +0 -0
  33. package/build/src/zlib/contrib/infback9/infback9.d +13 -0
  34. package/build/src/zlib/contrib/infback9/infback9.o +0 -0
  35. package/build/src/zlib/contrib/infback9/inftree9.d +7 -0
  36. package/build/src/zlib/contrib/infback9/inftree9.o +0 -0
  37. package/build/src/zlib/crc32.d +6 -0
  38. package/build/src/zlib/crc32.o +0 -0
  39. package/build/src/zlib/infback.d +10 -0
  40. package/build/src/zlib/infback.o +0 -0
  41. package/build/src/zlib/inffast.d +9 -0
  42. package/build/src/zlib/inffast.o +0 -0
  43. package/build/src/zlib/inflate.d +10 -0
  44. package/build/src/zlib/inflate.o +0 -0
  45. package/build/src/zlib/inftrees.d +6 -0
  46. package/build/src/zlib/inftrees.o +0 -0
  47. package/build/src/zlib/trees.d +7 -0
  48. package/build/src/zlib/trees.o +0 -0
  49. package/build/src/zlib/zutil.d +6 -0
  50. package/build/src/zlib/zutil.o +0 -0
  51. package/dist/zlib-streams.wasm +0 -0
  52. package/package.json +1 -1
  53. package/src/inffast9.c +312 -0
  54. package/src/inffast9.h +11 -0
  55. package/src/inflate9.c +167 -34
  56. package/src/wasm/allocator.c +12 -0
  57. package/src/wasm/allocator.h +4 -0
  58. package/src/wasm/deflate_stream_wasm.c +3 -2
  59. package/src/wasm/inflate9_stream_wasm.c +3 -2
  60. package/src/wasm/inflate_stream_wasm.c +4 -3
  61. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.log +34 -0
  62. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.out +100000 -0
  63. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.log +2 -0
  64. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.out +10000 -0
  65. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.log +1 -0
  66. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.out +0 -0
  67. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.log +1 -0
  68. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.out +0 -0
  69. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.log +2 -0
  70. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.out +0 -0
  71. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.log +1 -0
  72. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.out +0 -0
  73. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.log +1 -0
  74. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.out +0 -0
  75. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.log +1 -0
  76. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.out +0 -0
  77. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.log +2 -0
  78. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.out +0 -0
  79. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.log +2 -0
  80. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.out +0 -0
  81. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.log +1 -0
  82. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.out +100000 -0
  83. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.log +1 -0
  84. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.out +10000 -0
  85. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.log +1 -0
  86. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.out +0 -0
  87. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.log +1 -0
  88. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.out +0 -0
  89. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.log +1 -0
  90. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.out +0 -0
  91. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.log +1 -0
  92. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.out +0 -0
  93. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.log +1 -0
  94. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.out +0 -0
  95. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.log +1 -0
  96. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.out +0 -0
  97. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.log +1 -0
  98. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.out +0 -0
  99. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.log +1 -0
  100. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.out +0 -0
  101. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.log +34 -0
  102. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.out +100000 -0
  103. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.log +2 -0
  104. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.out +10000 -0
  105. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.log +1 -0
  106. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.out +0 -0
  107. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.log +1 -0
  108. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.out +0 -0
  109. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.log +2 -0
  110. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.out +0 -0
  111. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.log +1 -0
  112. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.out +0 -0
  113. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.log +1 -0
  114. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.out +0 -0
  115. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.log +1 -0
  116. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.out +0 -0
  117. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.log +2 -0
  118. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.out +0 -0
  119. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.log +2 -0
  120. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.out +0 -0
package/src/inffast9.c ADDED
@@ -0,0 +1,312 @@
1
+ /* inffast9.c -- fast decoding for deflate64
2
+ * Copyright (C) 1995-2017 Mark Adler
3
+ * For conditions of distribution and use, see copyright notice in zlib.h
4
+ */
5
+
6
+ #include "zlib/zutil.h"
7
+ #include "zlib/contrib/infback9/inftree9.h"
8
+ #include "zlib/inflate.h"
9
+ #include "inffast9.h"
10
+
11
+ #ifdef ASMINF
12
+ #pragma message("Assembler code may have bugs -- use at your own risk")
13
+ #else
14
+
15
+ /*
16
+ Decode literal, length, and distance codes and write out the resulting
17
+ literal and match bytes until either not enough input or output is
18
+ available, an end-of-block is encountered, or a data error is encountered.
19
+ When large enough input and output buffers are supplied to inflate(), for
20
+ example, a 16K input buffer and a 64K output buffer, more than 95% of the
21
+ inflate execution time is spent in this routine.
22
+
23
+ Entry assumptions:
24
+
25
+ state->mode == LEN
26
+ strm->avail_in >= 6
27
+ strm->avail_out >= 65536
28
+ start >= strm->avail_out
29
+ state->bits < 8
30
+
31
+ On return, state->mode is one of:
32
+
33
+ LEN -- ran out of enough output space or enough available input
34
+ TYPE -- reached end of block code, inflate() to interpret next block
35
+ BAD -- error in block data
36
+
37
+ Notes:
38
+
39
+ - The maximum input bits used by a length/distance pair is 15 bits for the
40
+ length code, 5 bits for the length extra, 15 bits for the distance code,
41
+ and 13 bits for the distance extra. This totals 48 bits, or six bytes.
42
+ Therefore if strm->avail_in >= 6, then there is enough input to avoid
43
+ checking for available input while decoding.
44
+
45
+ - The maximum bytes that a single length/distance pair can output is 65535
46
+ bytes, which is the maximum length that can be coded in deflate64.
47
+ inflate_fast9() requires strm->avail_out >= 65536 for each loop to avoid
48
+ checking for output space.
49
+ */
50
+ void ZLIB_INTERNAL inflate_fast9(z_streamp strm, unsigned start) {
51
+ struct inflate_state FAR *state;
52
+ z_const unsigned char FAR *in; /* local strm->next_in */
53
+ z_const unsigned char FAR *last; /* have enough input while in < last */
54
+ unsigned char FAR *out; /* local strm->next_out */
55
+ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
56
+ unsigned char FAR *end; /* while out < end, enough space available */
57
+ #ifdef INFLATE_STRICT
58
+ unsigned dmax; /* maximum distance from zlib header */
59
+ #endif
60
+ unsigned wsize; /* window size or zero if not using window */
61
+ unsigned whave; /* valid bytes in the window */
62
+ unsigned wnext; /* window write index */
63
+ unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
64
+ unsigned long hold; /* local strm->hold */
65
+ unsigned bits; /* local strm->bits */
66
+ code const FAR *lcode; /* local strm->lencode */
67
+ code const FAR *dcode; /* local strm->distcode */
68
+ unsigned lmask; /* mask for first level of length codes */
69
+ unsigned dmask; /* mask for first level of distance codes */
70
+ code const *here; /* retrieved table entry */
71
+ unsigned op; /* code bits, operation, extra bits, or */
72
+ /* window position, window bytes to copy */
73
+ unsigned len; /* match length, unused bytes */
74
+ unsigned dist; /* match distance */
75
+ unsigned char FAR *from; /* where to copy match from */
76
+
77
+ /* copy state to local variables */
78
+ state = (struct inflate_state FAR *)strm->state;
79
+ in = strm->next_in;
80
+ last = in + (strm->avail_in - 5);
81
+ out = strm->next_out;
82
+ beg = out - (start - strm->avail_out);
83
+ end = out + (strm->avail_out - 65535);
84
+ #ifdef INFLATE_STRICT
85
+ dmax = state->dmax;
86
+ #endif
87
+ wsize = state->wsize;
88
+ whave = state->whave;
89
+ wnext = state->wnext;
90
+ window = state->window;
91
+ hold = state->hold;
92
+ bits = state->bits;
93
+ lcode = state->lencode;
94
+ dcode = state->distcode;
95
+ lmask = (1U << state->lenbits) - 1;
96
+ dmask = (1U << state->distbits) - 1;
97
+
98
+ /* decode literals and length/distances until end-of-block or not enough
99
+ input data or output space */
100
+ do {
101
+ if (bits < 15) {
102
+ hold += (unsigned long)(*in++) << bits;
103
+ bits += 8;
104
+ hold += (unsigned long)(*in++) << bits;
105
+ bits += 8;
106
+ }
107
+ here = lcode + (hold & lmask);
108
+ dolen:
109
+ op = (unsigned)(here->bits);
110
+ hold >>= op;
111
+ bits -= op;
112
+ op = (unsigned)(here->op);
113
+ if (op == 0) { /* literal */
114
+ Tracevv((stderr,
115
+ here->val >= 0x20 && here->val < 0x7f
116
+ ? "inflate: literal '%c'\n"
117
+ : "inflate: literal 0x%02x\n",
118
+ here->val));
119
+ *out++ = (unsigned char)(here->val);
120
+ } else if (op & 16) { /* length base */
121
+ len = (unsigned)(here->val);
122
+ op &= 15; /* number of extra bits */
123
+ if (op) {
124
+ if (bits < op) {
125
+ hold += (unsigned long)(*in++) << bits;
126
+ bits += 8;
127
+ }
128
+ len += (unsigned)hold & ((1U << op) - 1);
129
+ hold >>= op;
130
+ bits -= op;
131
+ }
132
+ Tracevv((stderr, "inflate: length %u\n", len));
133
+ if (bits < 15) {
134
+ hold += (unsigned long)(*in++) << bits;
135
+ bits += 8;
136
+ hold += (unsigned long)(*in++) << bits;
137
+ bits += 8;
138
+ }
139
+ here = dcode + (hold & dmask);
140
+ dodist:
141
+ op = (unsigned)(here->bits);
142
+ hold >>= op;
143
+ bits -= op;
144
+ op = (unsigned)(here->op);
145
+ if (op & 16) { /* distance base */
146
+ dist = (unsigned)(here->val);
147
+ op &= 15; /* number of extra bits */
148
+ if (bits < op) {
149
+ hold += (unsigned long)(*in++) << bits;
150
+ bits += 8;
151
+ if (bits < op) {
152
+ hold += (unsigned long)(*in++) << bits;
153
+ bits += 8;
154
+ }
155
+ }
156
+ dist += (unsigned)hold & ((1U << op) - 1);
157
+ #ifdef INFLATE_STRICT
158
+ if (dist > dmax) {
159
+ strm->msg = (z_const char *)"invalid distance too far back";
160
+ state->mode = BAD;
161
+ break;
162
+ }
163
+ #endif
164
+ hold >>= op;
165
+ bits -= op;
166
+ Tracevv((stderr, "inflate: distance %u\n", dist));
167
+ op = (unsigned)(out - beg); /* max distance in output */
168
+ if (dist > op) { /* see if copy from window */
169
+ op = dist - op; /* distance back in window */
170
+ if (op > whave) {
171
+ if (state->sane) {
172
+ strm->msg = (z_const char *)"invalid distance too far back";
173
+ state->mode = BAD;
174
+ break;
175
+ }
176
+ #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
177
+ if (len <= op - whave) {
178
+ do {
179
+ *out++ = 0;
180
+ } while (--len);
181
+ continue;
182
+ }
183
+ len -= op - whave;
184
+ do {
185
+ *out++ = 0;
186
+ } while (--op > whave);
187
+ if (op == 0) {
188
+ from = out - dist;
189
+ do {
190
+ *out++ = *from++;
191
+ } while (--len);
192
+ continue;
193
+ }
194
+ #endif
195
+ }
196
+ from = window;
197
+ if (wnext == 0) { /* very common case */
198
+ from += wsize - op;
199
+ if (op < len) { /* some from window */
200
+ len -= op;
201
+ do {
202
+ *out++ = *from++;
203
+ } while (--op);
204
+ from = out - dist; /* rest from output */
205
+ }
206
+ } else if (wnext < op) { /* wrap around window */
207
+ from += wsize + wnext - op;
208
+ op -= wnext;
209
+ if (op < len) { /* some from end of window */
210
+ len -= op;
211
+ do {
212
+ *out++ = *from++;
213
+ } while (--op);
214
+ from = window;
215
+ if (wnext < len) { /* some from start of window */
216
+ op = wnext;
217
+ len -= op;
218
+ do {
219
+ *out++ = *from++;
220
+ } while (--op);
221
+ from = out - dist; /* rest from output */
222
+ }
223
+ }
224
+ } else { /* contiguous in window */
225
+ from += wnext - op;
226
+ if (op < len) { /* some from window */
227
+ len -= op;
228
+ do {
229
+ *out++ = *from++;
230
+ } while (--op);
231
+ from = out - dist; /* rest from output */
232
+ }
233
+ }
234
+ while (len > 2) {
235
+ *out++ = *from++;
236
+ *out++ = *from++;
237
+ *out++ = *from++;
238
+ len -= 3;
239
+ }
240
+ if (len) {
241
+ *out++ = *from++;
242
+ if (len > 1)
243
+ *out++ = *from++;
244
+ }
245
+ } else {
246
+ from = out - dist; /* copy direct from output */
247
+ do { /* minimum length is three */
248
+ *out++ = *from++;
249
+ *out++ = *from++;
250
+ *out++ = *from++;
251
+ len -= 3;
252
+ } while (len > 2);
253
+ if (len) {
254
+ *out++ = *from++;
255
+ if (len > 1)
256
+ *out++ = *from++;
257
+ }
258
+ }
259
+ } else if ((op & 64) == 0) { /* 2nd level distance code */
260
+ here = dcode + here->val + (hold & ((1U << op) - 1));
261
+ goto dodist;
262
+ } else {
263
+ strm->msg = (z_const char *)"invalid distance code";
264
+ state->mode = BAD;
265
+ break;
266
+ }
267
+ } else if ((op & 64) == 0) { /* 2nd level length code */
268
+ here = lcode + here->val + (hold & ((1U << op) - 1));
269
+ goto dolen;
270
+ } else if (op & 32) { /* end-of-block */
271
+ Tracevv((stderr, "inflate: end of block\n"));
272
+ state->mode = TYPE;
273
+ break;
274
+ } else {
275
+ strm->msg = (z_const char *)"invalid literal/length code";
276
+ state->mode = BAD;
277
+ break;
278
+ }
279
+ } while (in < last && out < end);
280
+
281
+ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
282
+ len = bits >> 3;
283
+ in -= len;
284
+ bits -= len << 3;
285
+ hold &= (1U << bits) - 1;
286
+
287
+ /* update state and return */
288
+ strm->next_in = in;
289
+ strm->next_out = out;
290
+ strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
291
+ strm->avail_out =
292
+ (unsigned)(out < end ? 65535 + (end - out) : 65535 - (out - end));
293
+ state->hold = hold;
294
+ state->bits = bits;
295
+ return;
296
+ }
297
+
298
+ /*
299
+ inflate_fast9() speedups that turned out slower (on a PowerPC G3 750CXe):
300
+ - Using bit fields for code structure
301
+ - Different op definition to avoid & for extra bits (do & for table bits)
302
+ - Three separate decoding do-loops for direct, window, and wnext == 0
303
+ - Special case for distance > 1 copies to do overlapped load and store copy
304
+ - Explicit branch predictions (based on measured branch probabilities)
305
+ - Deferring match copy and interspersed it with decoding subsequent codes
306
+ - Swapping literal/length else
307
+ - Swapping window/direct else
308
+ - Larger unrolled copy loops (three is about right)
309
+ - Moving len -= 3 statement into middle of loop
310
+ */
311
+
312
+ #endif /* !ASMINF */
package/src/inffast9.h ADDED
@@ -0,0 +1,11 @@
1
+ /* inffast9.h -- header to use inffast.c
2
+ * Copyright (C) 1995-2003, 2010 Mark Adler
3
+ * For conditions of distribution and use, see copyright notice in zlib.h
4
+ */
5
+
6
+ /* WARNING: this file should *not* be used by applications. It is
7
+ part of the implementation of the compression library and is
8
+ subject to change. Applications should only use zlib.h.
9
+ */
10
+
11
+ void ZLIB_INTERNAL inflate_fast9(z_streamp strm, unsigned start);
package/src/inflate9.c CHANGED
@@ -6,9 +6,15 @@
6
6
  #include "zutil.h"
7
7
  #include "inftree9.h"
8
8
  #include "inflate.h"
9
-
9
+ #include "inffast9.h"
10
10
  #include "infback9.h"
11
11
 
12
+ #ifdef MAKEFIXED
13
+ #ifndef BUILDFIXED
14
+ #define BUILDFIXED
15
+ #endif
16
+ #endif
17
+
12
18
  extern int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits,
13
19
  const char *version, int stream_size);
14
20
 
@@ -16,11 +22,6 @@ int ZEXPORT inflate9Init(z_streamp strm) {
16
22
  return inflate9Init2_(strm, 16, ZLIB_VERSION, (int)sizeof(z_stream));
17
23
  }
18
24
 
19
- static unsigned in(void *in_desc, unsigned char FAR **next);
20
-
21
- static const unsigned char order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
22
- 11, 4, 12, 3, 13, 2, 14, 1, 15};
23
-
24
25
  local int inflateStateCheck(z_streamp strm) {
25
26
  struct inflate_state FAR *state;
26
27
  if (strm == Z_NULL || strm->zalloc == (alloc_func)0 ||
@@ -88,6 +89,7 @@ int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits) {
88
89
  } else {
89
90
  wrap = (windowBits >> 4) + 5;
90
91
  }
92
+
91
93
  /* set number of window bits, free window if different */
92
94
  if (windowBits && (windowBits < 8 || windowBits > 16))
93
95
  return Z_STREAM_ERROR;
@@ -95,6 +97,7 @@ int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits) {
95
97
  ZFREE(strm, state->window);
96
98
  state->window = Z_NULL;
97
99
  }
100
+
98
101
  /* update state and reset the rest of it */
99
102
  state->wrap = wrap;
100
103
  state->wbits = (unsigned)windowBits;
@@ -113,11 +116,19 @@ int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits, const char *version,
113
116
  return Z_STREAM_ERROR;
114
117
  strm->msg = Z_NULL; /* in case we return an error */
115
118
  if (strm->zalloc == (alloc_func)0) {
119
+ #ifdef Z_SOLO
120
+ return Z_STREAM_ERROR;
121
+ #else
116
122
  strm->zalloc = zcalloc;
117
123
  strm->opaque = (voidpf)0;
124
+ #endif
118
125
  }
119
126
  if (strm->zfree == (free_func)0)
127
+ #ifdef Z_SOLO
128
+ return Z_STREAM_ERROR;
129
+ #else
120
130
  strm->zfree = zcfree;
131
+ #endif
121
132
  state =
122
133
  (struct inflate_state FAR *)ZALLOC(strm, 1, sizeof(struct inflate_state));
123
134
  if (state == Z_NULL)
@@ -126,7 +137,7 @@ int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits, const char *version,
126
137
  strm->state = (struct internal_state FAR *)state;
127
138
  state->strm = strm;
128
139
  state->window = Z_NULL;
129
- state->mode = TYPE;
140
+ state->mode = TYPE; /* to pass state test in inflate9Reset2() */
130
141
  ret = inflate9Reset2(strm, windowBits);
131
142
  if (ret != Z_OK) {
132
143
  ZFREE(strm, state);
@@ -140,24 +151,28 @@ int ZEXPORT inflate9Init_(z_streamp strm, const char *version,
140
151
  return inflate9Init2_(strm, DEF_WBITS, version, stream_size);
141
152
  }
142
153
 
143
- static unsigned in(void *in_desc, unsigned char FAR **next) {
144
- z_stream *strm = (z_stream *)in_desc;
145
-
146
- if (strm == Z_NULL || strm->next_in == Z_NULL || strm->avail_in == 0) {
147
- *next = Z_NULL;
148
- return 0;
149
- }
150
- *next = strm->next_in;
151
- return strm->avail_in;
152
- }
153
-
154
- void fixedtables(struct inflate_state FAR *state) {
154
+ /*
155
+ Return state with length and distance decoding tables and index sizes set to
156
+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
157
+ If BUILDFIXED is defined, then instead this routine builds the tables the
158
+ first time it's called, and returns those tables the first time and
159
+ thereafter. This reduces the size of the code by about 2K bytes, in
160
+ exchange for a little execution time. However, BUILDFIXED should not be
161
+ used for threaded applications, since the rewriting of the tables and virgin
162
+ may not be thread-safe.
163
+ */
164
+ local void fixedtables(struct inflate_state FAR *state) {
165
+ #ifdef BUILDFIXED
155
166
  static int virgin = 1;
156
167
  static code *lenfix, *distfix;
157
168
  static code fixed[544];
169
+
170
+ /* build fixed huffman tables if first call (may not be thread safe) */
158
171
  if (virgin) {
159
172
  unsigned sym, bits;
160
173
  static code *next;
174
+
175
+ /* literal/length table */
161
176
  sym = 0;
162
177
  while (sym < 144)
163
178
  state->lens[sym++] = 8;
@@ -183,12 +198,29 @@ void fixedtables(struct inflate_state FAR *state) {
183
198
  /* do this just once */
184
199
  virgin = 0;
185
200
  }
201
+ #else /* !BUILDFIXED */
202
+ #include "inffixed.h"
203
+ #endif /* BUILDFIXED */
186
204
  state->lencode = lenfix;
187
205
  state->lenbits = 9;
188
206
  state->distcode = distfix;
189
207
  state->distbits = 5;
190
208
  }
191
209
 
210
+ /*
211
+ Update the window with the last wsize (normally 32K) bytes written before
212
+ returning. If window does not exist yet, create it. This is only called
213
+ when a window is already in use, or when output has been written during this
214
+ inflate call, but the end of the deflate stream has not been reached yet.
215
+ It is also called to create a window for dictionary data when a dictionary
216
+ is loaded.
217
+
218
+ Providing output buffers larger than 32K to inflate() should provide a speed
219
+ advantage, since only the last 32K of output is copied to the sliding window
220
+ upon return from inflate(), and since all distances after the first 32K of
221
+ output will fall in the output data, making match copies simpler and faster.
222
+ The advantage may be dependent on the size of the processor's data caches.
223
+ */
192
224
  local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
193
225
  struct inflate_state FAR *state;
194
226
  unsigned dist;
@@ -265,6 +297,8 @@ local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
265
297
  bits = 0; \
266
298
  } while (0)
267
299
 
300
+ /* Get a byte of input into the bit accumulator, or return from inflate()
301
+ if there is no input available. */
268
302
  #define PULLBYTE() \
269
303
  do { \
270
304
  if (have == 0) \
@@ -274,26 +308,113 @@ local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
274
308
  bits += 8; \
275
309
  } while (0)
276
310
 
311
+ /* Assure that there are at least n bits in the bit accumulator. If there is
312
+ not enough available input to do that, then return from inflate(). */
277
313
  #define NEEDBITS(n) \
278
314
  do { \
279
315
  while (bits < (unsigned)(n)) \
280
316
  PULLBYTE(); \
281
317
  } while (0)
282
318
 
319
+ /* Return the low n bits of the bit accumulator (n < 16) */
283
320
  #define BITS(n) ((unsigned)hold & ((1U << (n)) - 1))
284
321
 
322
+ /* Remove n bits from the bit accumulator */
285
323
  #define DROPBITS(n) \
286
324
  do { \
287
325
  hold >>= (n); \
288
326
  bits -= (unsigned)(n); \
289
327
  } while (0)
290
328
 
329
+ /* Remove zero to seven bits as needed to go to a byte boundary */
291
330
  #define BYTEBITS() \
292
331
  do { \
293
332
  hold >>= bits & 7; \
294
333
  bits -= bits & 7; \
295
334
  } while (0)
296
335
 
336
+ /*
337
+ inflate() uses a state machine to process as much input data and generate as
338
+ much output data as possible before returning. The state machine is
339
+ structured roughly as follows:
340
+
341
+ for (;;) switch (state) {
342
+ ...
343
+ case STATEn:
344
+ if (not enough input data or output space to make progress)
345
+ return;
346
+ ... make progress ...
347
+ state = STATEm;
348
+ break;
349
+ ...
350
+ }
351
+
352
+ so when inflate() is called again, the same case is attempted again, and
353
+ if the appropriate resources are provided, the machine proceeds to the
354
+ next state. The NEEDBITS() macro is usually the way the state evaluates
355
+ whether it can proceed or should return. NEEDBITS() does the return if
356
+ the requested bits are not available. The typical use of the BITS macros
357
+ is:
358
+
359
+ NEEDBITS(n);
360
+ ... do something with BITS(n) ...
361
+ DROPBITS(n);
362
+
363
+ where NEEDBITS(n) either returns from inflate() if there isn't enough
364
+ input left to load n bits into the accumulator, or it continues. BITS(n)
365
+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
366
+ the low n bits off the accumulator. INITBITS() clears the accumulator
367
+ and sets the number of available bits to zero. BYTEBITS() discards just
368
+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
369
+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
370
+
371
+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
372
+ if there is no input available. The decoding of variable length codes uses
373
+ PULLBYTE() directly in order to pull just enough bytes to decode the next
374
+ code, and no more.
375
+
376
+ Some states loop until they get enough input, making sure that enough
377
+ state information is maintained to continue the loop where it left off
378
+ if NEEDBITS() returns in the loop. For example, want, need, and keep
379
+ would all have to actually be part of the saved state in case NEEDBITS()
380
+ returns:
381
+
382
+ case STATEw:
383
+ while (want < need) {
384
+ NEEDBITS(n);
385
+ keep[want++] = BITS(n);
386
+ DROPBITS(n);
387
+ }
388
+ state = STATEx;
389
+ case STATEx:
390
+
391
+ As shown above, if the next state is also the next case, then the break
392
+ is omitted.
393
+
394
+ A state may also return if there is not enough output space available to
395
+ complete that state. Those states are copying stored data, writing a
396
+ literal byte, and copying a matching string.
397
+
398
+ When returning, a "goto inf_leave" is used to update the total counters,
399
+ update the check value, and determine whether any progress has been made
400
+ during that inflate() call in order to return the proper return code.
401
+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
402
+ When there is a window, goto inf_leave will update the window with the last
403
+ output written. If a goto inf_leave occurs in the middle of decompression
404
+ and there is no window currently, goto inf_leave will create one and copy
405
+ output to the window for the next call of inflate().
406
+
407
+ In this implementation, the flush parameter of inflate() only affects the
408
+ return code (per zlib.h). inflate() always writes as much as possible to
409
+ strm->next_out, given the space available and the provided input--the effect
410
+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
411
+ the allocation of and copying into a sliding window until necessary, which
412
+ provides the effect documented in zlib.h for Z_FINISH when the entire input
413
+ stream available. So the only thing the flush parameter actually does is:
414
+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
415
+ will return Z_BUF_ERROR if it has not reached the end of the stream.
416
+ */
417
+
297
418
  int ZEXPORT inflate9(z_streamp strm, int flush) {
298
419
  struct inflate_state FAR *state;
299
420
  z_const unsigned char FAR *next; /* next input */
@@ -363,7 +484,7 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
363
484
  DROPBITS(2);
364
485
  break;
365
486
  case STORED:
366
- BYTEBITS();
487
+ BYTEBITS(); /* go to byte boundary */
367
488
  NEEDBITS(32);
368
489
  if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
369
490
  strm->msg = (z_const char *)"invalid stored block lengths";
@@ -408,11 +529,13 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
408
529
  DROPBITS(5);
409
530
  state->ncode = BITS(4) + 4;
410
531
  DROPBITS(4);
532
+ #ifndef PKZIP_BUG_WORKAROUND
411
533
  if (state->nlen > 286) {
412
- strm->msg = (z_const char *)"too many length or distance symbols";
534
+ strm->msg = (z_const char *)"too many length symbols";
413
535
  state->mode = BAD;
414
536
  break;
415
537
  }
538
+ #endif
416
539
  Tracev((stderr, "inflate: table sizes ok\n"));
417
540
  state->have = 0;
418
541
  state->mode = LENLENS;
@@ -496,8 +619,12 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
496
619
  break;
497
620
  }
498
621
 
622
+ /* build code tables -- note: do not change the lenbits or distbits
623
+ values here (9 and 6) without reading the comments in inftrees.h
624
+ concerning the ENOUGH constants, which depend on those values */
499
625
  state->next = state->codes;
500
626
  state->lencode = (const code FAR *)(state->next);
627
+ state->lenbits = 9;
501
628
  ret = inflate_table9(LENS, state->lens, state->nlen, &(state->next),
502
629
  &(state->lenbits), state->work);
503
630
  if (ret) {
@@ -506,6 +633,7 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
506
633
  break;
507
634
  }
508
635
  state->distcode = (const code FAR *)(state->next);
636
+ state->distbits = 6;
509
637
  ret = inflate_table9(DISTS, state->lens + state->nlen, state->ndist,
510
638
  &(state->next), &(state->distbits), state->work);
511
639
  if (ret) {
@@ -522,6 +650,14 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
522
650
  state->mode = LEN;
523
651
  /* fallthrough */
524
652
  case LEN:
653
+ if (have >= 6 && left >= 65536) {
654
+ RESTORE();
655
+ inflate_fast9(strm, out);
656
+ LOAD();
657
+ if (state->mode == TYPE)
658
+ state->back = -1;
659
+ break;
660
+ }
525
661
  state->back = 0;
526
662
  for (;;) {
527
663
  here = state->lencode[BITS(state->lenbits)];
@@ -615,11 +751,13 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
615
751
  DROPBITS(state->extra);
616
752
  state->back += state->extra;
617
753
  }
754
+ #ifdef INFLATE_STRICT
618
755
  if (state->offset > state->dmax) {
619
756
  strm->msg = (z_const char *)"invalid distance too far back";
620
757
  state->mode = BAD;
621
758
  break;
622
759
  }
760
+ #endif
623
761
  Tracevv((stderr, "inflate: distance %u\n", state->offset));
624
762
  state->mode = MATCH;
625
763
  /* fallthrough */
@@ -636,6 +774,7 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
636
774
  break;
637
775
  }
638
776
  #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
777
+ Trace((stderr, "inflate.c too far\n"));
639
778
  copy -= state->whave;
640
779
  if (copy > state->length)
641
780
  copy = state->length;
@@ -689,8 +828,13 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
689
828
  return Z_STREAM_ERROR;
690
829
  }
691
830
 
831
+ /*
832
+ Return from inflate9(), updating the total counts and the check value.
833
+ If there was no progress during the inflate9() call, return a buffer
834
+ error. Call updatewindow() to create and/or update the window state.
835
+ Note: a memory error from inflate9() is non-recoverable.
836
+ */
692
837
  inf_leave:
693
- state->hold = hold;
694
838
  RESTORE();
695
839
  if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
696
840
  (state->mode < DONE || flush != Z_FINISH)))
@@ -711,15 +855,4 @@ inf_leave:
711
855
  return ret;
712
856
  }
713
857
 
714
- int ZEXPORT inflate9End(z_streamp strm) {
715
- struct inflate_state FAR *state;
716
- if (inflateStateCheck(strm))
717
- return Z_STREAM_ERROR;
718
- state = (struct inflate_state FAR *)strm->state;
719
- if (state->window != Z_NULL)
720
- ZFREE(strm, state->window);
721
- ZFREE(strm, strm->state);
722
- strm->state = Z_NULL;
723
- Tracev((stderr, "inflate: end\n"));
724
- return Z_OK;
725
- }
858
+ int ZEXPORT inflate9End(z_streamp strm) { return inflateEnd(strm); }