zlib-streams 1.0.5 → 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 (163) hide show
  1. package/Makefile +17 -12
  2. package/build/debug/src/inffast.d +8 -0
  3. package/build/debug/src/{zlib/inffast.o → 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.o +0 -0
  9. package/build/debug/src/zlib/contrib/infback9/infback9.o +0 -0
  10. package/build/debug/src/zlib/contrib/infback9/inftree9.o +0 -0
  11. package/build/debug/src/zlib/crc32.o +0 -0
  12. package/build/debug/src/zlib/trees.o +0 -0
  13. package/build/debug/src/zlib/zutil.o +0 -0
  14. package/dist/zlib-streams-dev.wasm +0 -0
  15. package/dist/zlib-streams.wasm +0 -0
  16. package/package.json +1 -1
  17. package/src/inffast.c +320 -0
  18. package/src/inflate.c +1537 -0
  19. package/src/inflate.h +128 -0
  20. package/src/inftrees.c +321 -0
  21. package/src/inftrees.h +67 -0
  22. package/src/trees.c +1053 -0
  23. package/src/wasm/inflate9_stream_wasm.c +6 -18
  24. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.log +34 -0
  25. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.out +100000 -0
  26. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.log +2 -0
  27. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.log +1 -0
  28. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.out +0 -0
  29. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.log +1 -0
  30. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.out +0 -0
  31. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.log +2 -0
  32. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.out +0 -0
  33. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.log +1 -0
  34. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.out +0 -0
  35. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.log +1 -0
  36. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.out +0 -0
  37. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.log +1 -0
  38. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.out +0 -0
  39. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.log +2 -0
  40. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.out +0 -0
  41. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.log +2 -0
  42. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.out +0 -0
  43. package/tmp/{trace_ref.log → all_runs/payload_decompress_ref_debug__100k_lines.deflate64.log} +0 -1
  44. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.out +100000 -0
  45. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.log +1 -0
  46. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.out +10000 -0
  47. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.log +1 -0
  48. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.out +0 -0
  49. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.log +1 -0
  50. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.out +0 -0
  51. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.log +1 -0
  52. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.out +0 -0
  53. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.log +1 -0
  54. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.out +0 -0
  55. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.log +1 -0
  56. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.out +0 -0
  57. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.log +1 -0
  58. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.out +0 -0
  59. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.log +1 -0
  60. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.out +0 -0
  61. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.log +1 -0
  62. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.out +0 -0
  63. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.log +34 -0
  64. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.out +100000 -0
  65. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.log +2 -0
  66. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.out +10000 -0
  67. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.log +1 -0
  68. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.out +0 -0
  69. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.log +1 -0
  70. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.out +0 -0
  71. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.log +2 -0
  72. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.out +0 -0
  73. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.log +1 -0
  74. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.out +0 -0
  75. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.log +1 -0
  76. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.out +0 -0
  77. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.log +1 -0
  78. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.out +0 -0
  79. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.log +2 -0
  80. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.out +0 -0
  81. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.log +2 -0
  82. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.out +0 -0
  83. package/tmp/all_runs/wasm__100k_lines.deflate64.out +100000 -0
  84. package/tmp/all_runs/wasm__10k_lines.deflate64.out +10000 -0
  85. package/tmp/all_runs/wasm__payload_63k.deflate64.out +0 -0
  86. package/tmp/all_runs/wasm__payload_64k.deflate64.out +0 -0
  87. package/tmp/all_runs/wasm__payload_65k.deflate64.out +0 -0
  88. package/tmp/all_runs/wasm__rand_block_gap.deflate64.out +0 -0
  89. package/tmp/all_runs/wasm__repeat_63k.deflate64.out +0 -0
  90. package/tmp/all_runs/wasm__repeat_64k.deflate64.out +0 -0
  91. package/tmp/all_runs/wasm__repeat_65k.deflate64.out +0 -0
  92. package/tmp/all_runs/wasm__zeros_100k.deflate64.out +0 -0
  93. package/tmp/diagnose/diagnose_stream_mem.json +4202 -0
  94. package/build/7zip/Archive/Zip/ZipAddCommon.o +0 -0
  95. package/build/7zip/Archive/Zip/ZipHandler.o +0 -0
  96. package/build/7zip/Archive/Zip/ZipHandlerOut.o +0 -0
  97. package/build/7zip/Archive/Zip/ZipIn.o +0 -0
  98. package/build/7zip/Archive/Zip/ZipItem.o +0 -0
  99. package/build/7zip/Archive/Zip/ZipOut.o +0 -0
  100. package/build/7zip/Archive/Zip/ZipRegister.o +0 -0
  101. package/build/7zip/Archive/Zip/ZipUpdate.o +0 -0
  102. package/build/7zip/Common/CWrappers.o +0 -0
  103. package/build/7zip/Common/CreateCoder.o +0 -0
  104. package/build/7zip/Common/FileStreams.o +0 -0
  105. package/build/7zip/Common/FilterCoder.o +0 -0
  106. package/build/7zip/Common/InBuffer.o +0 -0
  107. package/build/7zip/Common/InOutTempBuffer.o +0 -0
  108. package/build/7zip/Common/OffsetStream.o +0 -0
  109. package/build/7zip/Common/OutBuffer.o +0 -0
  110. package/build/7zip/Common/OutMemStream.o +0 -0
  111. package/build/7zip/Common/StreamObjects.o +0 -0
  112. package/build/7zip/Common/StreamUtils.o +0 -0
  113. package/build/7zip/Compress/CopyCoder.o +0 -0
  114. package/build/7zip/Compress/Deflate64Register.o +0 -0
  115. package/build/7zip/Compress/DeflateDecoder.o +0 -0
  116. package/build/7zip/Compress/DeflateEncoder.o +0 -0
  117. package/build/7zip/Compress/DeflateRegister.o +0 -0
  118. package/build/7zip/Compress/LzOutWindow.o +0 -0
  119. package/build/Common/IntToString.o +0 -0
  120. package/build/Common/MyString.o +0 -0
  121. package/build/Common/MyWindows.o +0 -0
  122. package/build/Common/StringConvert.o +0 -0
  123. package/build/Common/UTFConvert.o +0 -0
  124. package/build/Windows/FileFind.o +0 -0
  125. package/build/Windows/FileIO.o +0 -0
  126. package/build/Windows/FileName.o +0 -0
  127. package/build/debug/src/inflate9.d +0 -9
  128. package/build/debug/src/inflate9.o +0 -0
  129. package/build/debug/src/zlib/infback.d +0 -10
  130. package/build/debug/src/zlib/infback.o +0 -0
  131. package/build/debug/src/zlib/inffast.d +0 -9
  132. package/build/debug/src/zlib/inflate.d +0 -10
  133. package/build/debug/src/zlib/inflate.o +0 -0
  134. package/build/debug/src/zlib/inftrees.d +0 -6
  135. package/build/debug/src/zlib/inftrees.o +0 -0
  136. package/build/src/inflate9.d +0 -9
  137. package/build/src/inflate9.o +0 -0
  138. package/build/src/zlib/adler32.d +0 -5
  139. package/build/src/zlib/adler32.o +0 -0
  140. package/build/src/zlib/contrib/infback9/infback9.d +0 -13
  141. package/build/src/zlib/contrib/infback9/infback9.o +0 -0
  142. package/build/src/zlib/contrib/infback9/inftree9.d +0 -7
  143. package/build/src/zlib/contrib/infback9/inftree9.o +0 -0
  144. package/build/src/zlib/crc32.d +0 -6
  145. package/build/src/zlib/crc32.o +0 -0
  146. package/build/src/zlib/infback.d +0 -10
  147. package/build/src/zlib/infback.o +0 -0
  148. package/build/src/zlib/inffast.d +0 -9
  149. package/build/src/zlib/inffast.o +0 -0
  150. package/build/src/zlib/inflate.d +0 -10
  151. package/build/src/zlib/inflate.o +0 -0
  152. package/build/src/zlib/inftrees.d +0 -6
  153. package/build/src/zlib/inftrees.o +0 -0
  154. package/build/src/zlib/trees.d +0 -7
  155. package/build/src/zlib/trees.o +0 -0
  156. package/build/src/zlib/zutil.d +0 -6
  157. package/build/src/zlib/zutil.o +0 -0
  158. package/src/inflate9.c +0 -859
  159. package/src/inflate9.h +0 -9
  160. package/tmp/all_runs/roundtrip_input.txt +0 -3
  161. package/tmp/all_runs/roundtrip_input1.txt +0 -3
  162. package/tmp/all_runs/roundtrip_input3.bin +0 -0
  163. /package/tmp/{out_ref.bin → all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.out} +0 -0
package/src/inflate9.c DELETED
@@ -1,859 +0,0 @@
1
- /* inflate9.c -- zlib decompression
2
- * Copyright (C) 2025 Gildas Lormeau (inflate9.c) - 1995-2022 Mark Adler (zlib)
3
- * For conditions of distribution and use, see copyright notice in zlib.h
4
- */
5
-
6
- #include "zutil.h"
7
- #include "inftree9.h"
8
- #include "inflate.h"
9
-
10
- #ifdef MAKEFIXED
11
- #ifndef BUILDFIXED
12
- #define BUILDFIXED
13
- #endif
14
- #endif
15
-
16
- extern int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits,
17
- const char *version, int stream_size);
18
-
19
- int ZEXPORT inflate9Init(z_streamp strm) {
20
- return inflate9Init2_(strm, 16, ZLIB_VERSION, (int)sizeof(z_stream));
21
- }
22
-
23
- local int inflateStateCheck(z_streamp strm) {
24
- struct inflate_state FAR *state;
25
- if (strm == Z_NULL || strm->zalloc == (alloc_func)0 ||
26
- strm->zfree == (free_func)0)
27
- return 1;
28
- state = (struct inflate_state FAR *)strm->state;
29
- if (state == Z_NULL || state->strm != strm || state->mode < TYPE ||
30
- state->mode > BAD)
31
- return 1;
32
- return 0;
33
- }
34
-
35
- int ZEXPORT inflate9ResetKeep(z_streamp strm) {
36
- struct inflate_state FAR *state;
37
-
38
- if (inflateStateCheck(strm))
39
- return Z_STREAM_ERROR;
40
- state = (struct inflate_state FAR *)strm->state;
41
- strm->total_in = strm->total_out = state->total = 0;
42
- strm->msg = Z_NULL;
43
- if (state->wrap) /* to support ill-conceived Java test suite */
44
- strm->adler = state->wrap & 1;
45
- state->mode = TYPE;
46
- state->last = 0;
47
- state->havedict = 0;
48
- state->flags = -1;
49
- state->dmax = 65536U;
50
- state->head = Z_NULL;
51
- state->hold = 0;
52
- state->bits = 0;
53
- state->lencode = state->distcode = state->next = state->codes;
54
- state->sane = 1;
55
- state->back = -1;
56
- Tracev((stderr, "inflate: reset\n"));
57
- return Z_OK;
58
- }
59
-
60
- int ZEXPORT inflate9Reset(z_streamp strm) {
61
- struct inflate_state FAR *state;
62
-
63
- if (inflateStateCheck(strm))
64
- return Z_STREAM_ERROR;
65
- state = (struct inflate_state FAR *)strm->state;
66
- state->wsize = 0;
67
- state->whave = 0;
68
- state->wnext = 0;
69
- return inflate9ResetKeep(strm);
70
- }
71
-
72
- int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits) {
73
- int wrap;
74
- struct inflate_state FAR *state;
75
-
76
- /* get the state */
77
- if (inflateStateCheck(strm))
78
- return Z_STREAM_ERROR;
79
- state = (struct inflate_state FAR *)strm->state;
80
-
81
- /* extract wrap request from windowBits parameter */
82
- if (windowBits < 0) {
83
- if (windowBits < -16)
84
- return Z_STREAM_ERROR;
85
- wrap = 0;
86
- windowBits = -windowBits;
87
- } else {
88
- wrap = (windowBits >> 4) + 5;
89
- }
90
-
91
- /* set number of window bits, free window if different */
92
- if (windowBits && (windowBits < 8 || windowBits > 16))
93
- return Z_STREAM_ERROR;
94
- if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
95
- ZFREE(strm, state->window);
96
- state->window = Z_NULL;
97
- }
98
-
99
- /* update state and reset the rest of it */
100
- state->wrap = wrap;
101
- state->wbits = (unsigned)windowBits;
102
- return inflate9Reset(strm);
103
- }
104
-
105
- int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits, const char *version,
106
- int stream_size) {
107
- int ret;
108
- struct inflate_state FAR *state;
109
-
110
- if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
111
- stream_size != (int)(sizeof(z_stream)))
112
- return Z_VERSION_ERROR;
113
- if (strm == Z_NULL)
114
- return Z_STREAM_ERROR;
115
- strm->msg = Z_NULL; /* in case we return an error */
116
- if (strm->zalloc == (alloc_func)0) {
117
- #ifdef Z_SOLO
118
- return Z_STREAM_ERROR;
119
- #else
120
- strm->zalloc = zcalloc;
121
- strm->opaque = (voidpf)0;
122
- #endif
123
- }
124
- if (strm->zfree == (free_func)0)
125
- #ifdef Z_SOLO
126
- return Z_STREAM_ERROR;
127
- #else
128
- strm->zfree = zcfree;
129
- #endif
130
- state =
131
- (struct inflate_state FAR *)ZALLOC(strm, 1, sizeof(struct inflate_state));
132
- if (state == Z_NULL)
133
- return Z_MEM_ERROR;
134
- Tracev((stderr, "inflate: allocated\n"));
135
- strm->state = (struct internal_state FAR *)state;
136
- state->strm = strm;
137
- state->window = Z_NULL;
138
- state->mode = TYPE; /* to pass state test in inflate9Reset2() */
139
- ret = inflate9Reset2(strm, windowBits);
140
- if (ret != Z_OK) {
141
- ZFREE(strm, state);
142
- strm->state = Z_NULL;
143
- }
144
- return ret;
145
- }
146
-
147
- int ZEXPORT inflate9Init_(z_streamp strm, const char *version,
148
- int stream_size) {
149
- return inflate9Init2_(strm, DEF_WBITS, version, stream_size);
150
- }
151
-
152
- /*
153
- Return state with length and distance decoding tables and index sizes set to
154
- fixed code decoding. Normally this returns fixed tables from inffixed.h.
155
- If BUILDFIXED is defined, then instead this routine builds the tables the
156
- first time it's called, and returns those tables the first time and
157
- thereafter. This reduces the size of the code by about 2K bytes, in
158
- exchange for a little execution time. However, BUILDFIXED should not be
159
- used for threaded applications, since the rewriting of the tables and virgin
160
- may not be thread-safe.
161
- */
162
- local void fixedtables(struct inflate_state FAR *state) {
163
- #ifdef BUILDFIXED
164
- static int virgin = 1;
165
- static code *lenfix, *distfix;
166
- static code fixed[544];
167
-
168
- /* build fixed huffman tables if first call (may not be thread safe) */
169
- if (virgin) {
170
- unsigned sym, bits;
171
- static code *next;
172
-
173
- /* literal/length table */
174
- sym = 0;
175
- while (sym < 144)
176
- state->lens[sym++] = 8;
177
- while (sym < 256)
178
- state->lens[sym++] = 9;
179
- while (sym < 280)
180
- state->lens[sym++] = 7;
181
- while (sym < 288)
182
- state->lens[sym++] = 8;
183
- next = fixed;
184
- lenfix = next;
185
- bits = 9;
186
- inflate_table9(LENS, state->lens, 288, &(next), &(bits), state->work);
187
-
188
- /* distance table */
189
- sym = 0;
190
- while (sym < 32)
191
- state->lens[sym++] = 5;
192
- distfix = next;
193
- bits = 5;
194
- inflate_table9(DISTS, state->lens, 32, &(next), &(bits), state->work);
195
-
196
- /* do this just once */
197
- virgin = 0;
198
- }
199
- #else /* !BUILDFIXED */
200
- #include "inffixed.h"
201
- #endif /* BUILDFIXED */
202
- state->lencode = lenfix;
203
- state->lenbits = 9;
204
- state->distcode = distfix;
205
- state->distbits = 5;
206
- }
207
-
208
- /*
209
- Update the window with the last wsize (normally 32K) bytes written before
210
- returning. If window does not exist yet, create it. This is only called
211
- when a window is already in use, or when output has been written during this
212
- inflate call, but the end of the deflate stream has not been reached yet.
213
- It is also called to create a window for dictionary data when a dictionary
214
- is loaded.
215
-
216
- Providing output buffers larger than 32K to inflate() should provide a speed
217
- advantage, since only the last 32K of output is copied to the sliding window
218
- upon return from inflate(), and since all distances after the first 32K of
219
- output will fall in the output data, making match copies simpler and faster.
220
- The advantage may be dependent on the size of the processor's data caches.
221
- */
222
- local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
223
- struct inflate_state FAR *state;
224
- unsigned dist;
225
-
226
- state = (struct inflate_state FAR *)strm->state;
227
-
228
- /* if it hasn't been done already, allocate space for the window */
229
- if (state->window == Z_NULL) {
230
- state->window = (unsigned char FAR *)ZALLOC(strm, 1U << state->wbits,
231
- sizeof(unsigned char));
232
- if (state->window == Z_NULL)
233
- return 1;
234
- }
235
-
236
- /* if window not in use yet, initialize */
237
- if (state->wsize == 0) {
238
- state->wsize = 1U << state->wbits;
239
- state->wnext = 0;
240
- state->whave = 0;
241
- }
242
-
243
- /* copy state->wsize or less output bytes into the circular window */
244
- if (copy >= state->wsize) {
245
- zmemcpy(state->window, end - state->wsize, state->wsize);
246
- state->wnext = 0;
247
- state->whave = state->wsize;
248
- } else {
249
- dist = state->wsize - state->wnext;
250
- if (dist > copy)
251
- dist = copy;
252
- zmemcpy(state->window + state->wnext, end - copy, dist);
253
- copy -= dist;
254
- if (copy) {
255
- zmemcpy(state->window, end - copy, copy);
256
- state->wnext = copy;
257
- state->whave = state->wsize;
258
- } else {
259
- state->wnext += dist;
260
- if (state->wnext == state->wsize)
261
- state->wnext = 0;
262
- if (state->whave < state->wsize)
263
- state->whave += dist;
264
- }
265
- }
266
- return 0;
267
- }
268
-
269
- /* Load registers with state in inflate() for speed */
270
- #define LOAD() \
271
- do { \
272
- put = strm->next_out; \
273
- left = strm->avail_out; \
274
- next = strm->next_in; \
275
- have = strm->avail_in; \
276
- hold = state->hold; \
277
- bits = state->bits; \
278
- } while (0)
279
-
280
- /* Restore state from registers in inflate() */
281
- #define RESTORE() \
282
- do { \
283
- strm->next_out = put; \
284
- strm->avail_out = left; \
285
- strm->next_in = next; \
286
- strm->avail_in = have; \
287
- state->hold = hold; \
288
- state->bits = bits; \
289
- } while (0)
290
-
291
- /* Clear the input bit accumulator */
292
- #define INITBITS() \
293
- do { \
294
- hold = 0; \
295
- bits = 0; \
296
- } while (0)
297
-
298
- /* Get a byte of input into the bit accumulator, or return from inflate()
299
- if there is no input available. */
300
- #define PULLBYTE() \
301
- do { \
302
- if (have == 0) \
303
- goto inf_leave; \
304
- have--; \
305
- hold += (unsigned long)(*next++) << bits; \
306
- bits += 8; \
307
- } while (0)
308
-
309
- /* Assure that there are at least n bits in the bit accumulator. If there is
310
- not enough available input to do that, then return from inflate(). */
311
- #define NEEDBITS(n) \
312
- do { \
313
- while (bits < (unsigned)(n)) \
314
- PULLBYTE(); \
315
- } while (0)
316
-
317
- /* Return the low n bits of the bit accumulator (n < 16) */
318
- #define BITS(n) ((unsigned)hold & ((1U << (n)) - 1))
319
-
320
- /* Remove n bits from the bit accumulator */
321
- #define DROPBITS(n) \
322
- do { \
323
- hold >>= (n); \
324
- bits -= (unsigned)(n); \
325
- } while (0)
326
-
327
- /* Remove zero to seven bits as needed to go to a byte boundary */
328
- #define BYTEBITS() \
329
- do { \
330
- hold >>= bits & 7; \
331
- bits -= bits & 7; \
332
- } while (0)
333
-
334
- /*
335
- inflate() uses a state machine to process as much input data and generate as
336
- much output data as possible before returning. The state machine is
337
- structured roughly as follows:
338
-
339
- for (;;) switch (state) {
340
- ...
341
- case STATEn:
342
- if (not enough input data or output space to make progress)
343
- return;
344
- ... make progress ...
345
- state = STATEm;
346
- break;
347
- ...
348
- }
349
-
350
- so when inflate() is called again, the same case is attempted again, and
351
- if the appropriate resources are provided, the machine proceeds to the
352
- next state. The NEEDBITS() macro is usually the way the state evaluates
353
- whether it can proceed or should return. NEEDBITS() does the return if
354
- the requested bits are not available. The typical use of the BITS macros
355
- is:
356
-
357
- NEEDBITS(n);
358
- ... do something with BITS(n) ...
359
- DROPBITS(n);
360
-
361
- where NEEDBITS(n) either returns from inflate() if there isn't enough
362
- input left to load n bits into the accumulator, or it continues. BITS(n)
363
- gives the low n bits in the accumulator. When done, DROPBITS(n) drops
364
- the low n bits off the accumulator. INITBITS() clears the accumulator
365
- and sets the number of available bits to zero. BYTEBITS() discards just
366
- enough bits to put the accumulator on a byte boundary. After BYTEBITS()
367
- and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
368
-
369
- NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
370
- if there is no input available. The decoding of variable length codes uses
371
- PULLBYTE() directly in order to pull just enough bytes to decode the next
372
- code, and no more.
373
-
374
- Some states loop until they get enough input, making sure that enough
375
- state information is maintained to continue the loop where it left off
376
- if NEEDBITS() returns in the loop. For example, want, need, and keep
377
- would all have to actually be part of the saved state in case NEEDBITS()
378
- returns:
379
-
380
- case STATEw:
381
- while (want < need) {
382
- NEEDBITS(n);
383
- keep[want++] = BITS(n);
384
- DROPBITS(n);
385
- }
386
- state = STATEx;
387
- case STATEx:
388
-
389
- As shown above, if the next state is also the next case, then the break
390
- is omitted.
391
-
392
- A state may also return if there is not enough output space available to
393
- complete that state. Those states are copying stored data, writing a
394
- literal byte, and copying a matching string.
395
-
396
- When returning, a "goto inf_leave" is used to update the total counters,
397
- update the check value, and determine whether any progress has been made
398
- during that inflate() call in order to return the proper return code.
399
- Progress is defined as a change in either strm->avail_in or strm->avail_out.
400
- When there is a window, goto inf_leave will update the window with the last
401
- output written. If a goto inf_leave occurs in the middle of decompression
402
- and there is no window currently, goto inf_leave will create one and copy
403
- output to the window for the next call of inflate().
404
-
405
- In this implementation, the flush parameter of inflate() only affects the
406
- return code (per zlib.h). inflate() always writes as much as possible to
407
- strm->next_out, given the space available and the provided input--the effect
408
- documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
409
- the allocation of and copying into a sliding window until necessary, which
410
- provides the effect documented in zlib.h for Z_FINISH when the entire input
411
- stream available. So the only thing the flush parameter actually does is:
412
- when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
413
- will return Z_BUF_ERROR if it has not reached the end of the stream.
414
- */
415
-
416
- int ZEXPORT inflate9(z_streamp strm, int flush) {
417
- struct inflate_state FAR *state;
418
- z_const unsigned char FAR *next; /* next input */
419
- unsigned char FAR *put; /* next output */
420
- unsigned have, left; /* available input and output */
421
- unsigned long hold; /* bit buffer */
422
- unsigned bits; /* bits in bit buffer */
423
- unsigned in, out; /* save starting available input and output */
424
- unsigned copy; /* number of stored or match bytes to copy */
425
- unsigned char FAR *from; /* where to copy match bytes from */
426
- code here; /* current decoding table entry */
427
- code last; /* parent table entry */
428
- unsigned len; /* length to copy for repeats, bits to drop */
429
- int ret; /* return code */
430
- static const unsigned short order[19] = /* permutation of code lengths */
431
- {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
432
-
433
- if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
434
- (strm->next_in == Z_NULL && strm->avail_in != 0))
435
- return Z_STREAM_ERROR;
436
-
437
- state = (struct inflate_state FAR *)strm->state;
438
- LOAD();
439
- in = have;
440
- out = left;
441
- ret = Z_OK;
442
- for (;;)
443
- switch (state->mode) {
444
- case TYPE:
445
- if (flush == Z_BLOCK || flush == Z_TREES)
446
- goto inf_leave;
447
- /* fallthrough */
448
- case TYPEDO:
449
- if (state->last) {
450
- BYTEBITS();
451
- state->mode = DONE;
452
- break;
453
- }
454
- NEEDBITS(3);
455
- state->last = BITS(1);
456
- DROPBITS(1);
457
- switch (BITS(2)) {
458
- case 0: /* stored block */
459
- Tracev((stderr, "inflate: stored block%s\n",
460
- state->last ? " (last)" : ""));
461
- state->mode = STORED;
462
- break;
463
- case 1: /* fixed block */
464
- fixedtables(state);
465
- Tracev((stderr, "inflate: fixed codes block%s\n",
466
- state->last ? " (last)" : ""));
467
- state->mode = LEN_; /* decode codes */
468
- if (flush == Z_TREES) {
469
- DROPBITS(2);
470
- goto inf_leave;
471
- }
472
- break;
473
- case 2: /* dynamic block */
474
- Tracev((stderr, "inflate: dynamic codes block%s\n",
475
- state->last ? " (last)" : ""));
476
- state->mode = TABLE;
477
- break;
478
- case 3:
479
- strm->msg = (z_const char *)"invalid block type";
480
- state->mode = BAD;
481
- }
482
- DROPBITS(2);
483
- break;
484
- case STORED:
485
- BYTEBITS(); /* go to byte boundary */
486
- NEEDBITS(32);
487
- if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
488
- strm->msg = (z_const char *)"invalid stored block lengths";
489
- state->mode = BAD;
490
- break;
491
- }
492
- state->length = (unsigned)hold & 0xffff;
493
- Tracev((stderr, "inflate: stored length %u\n", state->length));
494
- INITBITS();
495
- state->mode = COPY_;
496
- if (flush == Z_TREES)
497
- goto inf_leave;
498
- /* fallthrough */
499
- case COPY_:
500
- state->mode = COPY;
501
- /* fallthrough */
502
- case COPY:
503
- copy = state->length;
504
- if (copy) {
505
- if (copy > have)
506
- copy = have;
507
- if (copy > left)
508
- copy = left;
509
- if (copy == 0)
510
- goto inf_leave;
511
- zmemcpy(put, next, copy);
512
- have -= copy;
513
- next += copy;
514
- left -= copy;
515
- put += copy;
516
- state->length -= copy;
517
- break;
518
- }
519
- Tracev((stderr, "inflate: stored end\n"));
520
- state->mode = TYPE;
521
- break;
522
- case TABLE:
523
- NEEDBITS(14);
524
- state->nlen = BITS(5) + 257;
525
- DROPBITS(5);
526
- state->ndist = BITS(5) + 1;
527
- DROPBITS(5);
528
- state->ncode = BITS(4) + 4;
529
- DROPBITS(4);
530
- #ifndef PKZIP_BUG_WORKAROUND
531
- if (state->nlen > 286) {
532
- strm->msg = (z_const char *)"too many length symbols";
533
- state->mode = BAD;
534
- break;
535
- }
536
- #endif
537
- Tracev((stderr, "inflate: table sizes ok\n"));
538
- state->have = 0;
539
- state->mode = LENLENS;
540
- /* fallthrough */
541
- case LENLENS:
542
- while (state->have < state->ncode) {
543
- NEEDBITS(3);
544
- state->lens[order[state->have++]] = (unsigned short)BITS(3);
545
- DROPBITS(3);
546
- }
547
- while (state->have < 19)
548
- state->lens[order[state->have++]] = 0;
549
- state->next = state->codes;
550
- state->lencode = state->distcode = (const code FAR *)(state->next);
551
- state->lenbits = 7;
552
- ret = inflate_table9(CODES, state->lens, 19, &(state->next),
553
- &(state->lenbits), state->work);
554
- if (ret) {
555
- strm->msg = (z_const char *)"invalid code lengths set";
556
- state->mode = BAD;
557
- break;
558
- }
559
- Tracev((stderr, "inflate: code lengths ok\n"));
560
- state->have = 0;
561
- state->mode = CODELENS;
562
- /* fallthrough */
563
- case CODELENS:
564
- while (state->have < state->nlen + state->ndist) {
565
- for (;;) {
566
- here = state->lencode[BITS(state->lenbits)];
567
- if ((unsigned)(here.bits) <= bits)
568
- break;
569
- PULLBYTE();
570
- }
571
- if (here.val < 16) {
572
- DROPBITS(here.bits);
573
- state->lens[state->have++] = here.val;
574
- } else {
575
- if (here.val == 16) {
576
- NEEDBITS(here.bits + 2);
577
- DROPBITS(here.bits);
578
- if (state->have == 0) {
579
- strm->msg = (z_const char *)"invalid bit length repeat";
580
- state->mode = BAD;
581
- break;
582
- }
583
- len = state->lens[state->have - 1];
584
- copy = 3 + BITS(2);
585
- DROPBITS(2);
586
- } else if (here.val == 17) {
587
- NEEDBITS(here.bits + 3);
588
- DROPBITS(here.bits);
589
- len = 0;
590
- copy = 3 + BITS(3);
591
- DROPBITS(3);
592
- } else {
593
- NEEDBITS(here.bits + 7);
594
- DROPBITS(here.bits);
595
- len = 0;
596
- copy = 11 + BITS(7);
597
- DROPBITS(7);
598
- }
599
- if (state->have + copy > state->nlen + state->ndist) {
600
- strm->msg = (z_const char *)"invalid bit length repeat";
601
- state->mode = BAD;
602
- break;
603
- }
604
- while (copy--)
605
- state->lens[state->have++] = (unsigned short)len;
606
- }
607
- }
608
-
609
- /* handle error breaks in while */
610
- if (state->mode == BAD)
611
- break;
612
-
613
- /* check for end-of-block code (better have one) */
614
- if (state->lens[256] == 0) {
615
- strm->msg = (z_const char *)"invalid code -- missing end-of-block";
616
- state->mode = BAD;
617
- break;
618
- }
619
-
620
- /* build code tables -- note: do not change the lenbits or distbits
621
- values here (9 and 6) without reading the comments in inftrees.h
622
- concerning the ENOUGH constants, which depend on those values */
623
- state->next = state->codes;
624
- state->lencode = (const code FAR *)(state->next);
625
- state->lenbits = 9;
626
- ret = inflate_table9(LENS, state->lens, state->nlen, &(state->next),
627
- &(state->lenbits), state->work);
628
- if (ret) {
629
- strm->msg = (z_const char *)"invalid literal/lengths set";
630
- state->mode = BAD;
631
- break;
632
- }
633
- state->distcode = (const code FAR *)(state->next);
634
- state->distbits = 6;
635
- ret = inflate_table9(DISTS, state->lens + state->nlen, state->ndist,
636
- &(state->next), &(state->distbits), state->work);
637
- if (ret) {
638
- strm->msg = (z_const char *)"invalid distances set";
639
- state->mode = BAD;
640
- break;
641
- }
642
- Tracev((stderr, "inflate: codes ok\n"));
643
- state->mode = LEN_;
644
- if (flush == Z_TREES)
645
- goto inf_leave;
646
- /* fallthrough */
647
- case LEN_:
648
- state->mode = LEN;
649
- /* fallthrough */
650
- case LEN:
651
- state->back = 0;
652
- for (;;) {
653
- here = state->lencode[BITS(state->lenbits)];
654
- if ((unsigned)(here.bits) <= bits)
655
- break;
656
- PULLBYTE();
657
- }
658
- if (here.op && (here.op & 0xf0) == 0) {
659
- last = here;
660
- for (;;) {
661
- here = state->lencode[last.val +
662
- (BITS(last.bits + last.op) >> last.bits)];
663
- if ((unsigned)(last.bits + here.bits) <= bits)
664
- break;
665
- PULLBYTE();
666
- }
667
- DROPBITS(last.bits);
668
- state->back += last.bits;
669
- }
670
- DROPBITS(here.bits);
671
- state->back += here.bits;
672
- state->length = (unsigned)here.val;
673
- if ((int)(here.op) == 0) {
674
- Tracevv((stderr,
675
- here.val >= 0x20 && here.val < 0x7f
676
- ? "inflate: literal '%c'\n"
677
- : "inflate: literal 0x%02x\n",
678
- here.val));
679
- state->mode = LIT;
680
- break;
681
- }
682
- if (here.op & 32) {
683
- Tracevv((stderr, "inflate: end of block\n"));
684
- state->back = -1;
685
- state->mode = TYPE;
686
- break;
687
- }
688
- if (here.op & 64) {
689
- strm->msg = (z_const char *)"invalid literal/length code";
690
- state->mode = BAD;
691
- break;
692
- }
693
- state->extra = (unsigned)(here.op) & 31;
694
- state->mode = LENEXT;
695
- /* fallthrough */
696
- case LENEXT:
697
- if (state->extra) {
698
- NEEDBITS(state->extra);
699
- state->length += BITS(state->extra);
700
- DROPBITS(state->extra);
701
- state->back += state->extra;
702
- }
703
- Tracevv((stderr, "inflate: length %u\n", state->length));
704
- state->was = state->length;
705
- state->mode = DIST;
706
- /* fallthrough */
707
- case DIST:
708
- for (;;) {
709
- here = state->distcode[BITS(state->distbits)];
710
- if ((unsigned)(here.bits) <= bits)
711
- break;
712
- PULLBYTE();
713
- }
714
- if ((here.op & 0xf0) == 0) {
715
- last = here;
716
- for (;;) {
717
- here = state->distcode[last.val +
718
- (BITS(last.bits + last.op) >> last.bits)];
719
- if ((unsigned)(last.bits + here.bits) <= bits)
720
- break;
721
- PULLBYTE();
722
- }
723
- DROPBITS(last.bits);
724
- state->back += last.bits;
725
- }
726
- DROPBITS(here.bits);
727
- state->back += here.bits;
728
- if (here.op & 64) {
729
- strm->msg = (z_const char *)"invalid distance code";
730
- state->mode = BAD;
731
- break;
732
- }
733
- state->offset = (unsigned)here.val;
734
- state->extra = (unsigned)(here.op) & 15;
735
- state->mode = DISTEXT;
736
- /* fallthrough */
737
- case DISTEXT:
738
- if (state->extra) {
739
- NEEDBITS(state->extra);
740
- state->offset += BITS(state->extra);
741
- DROPBITS(state->extra);
742
- state->back += state->extra;
743
- }
744
- #ifdef INFLATE_STRICT
745
- if (state->offset > state->dmax) {
746
- strm->msg = (z_const char *)"invalid distance too far back";
747
- state->mode = BAD;
748
- break;
749
- }
750
- #endif
751
- Tracevv((stderr, "inflate: distance %u\n", state->offset));
752
- state->mode = MATCH;
753
- /* fallthrough */
754
- case MATCH:
755
- if (left == 0)
756
- goto inf_leave;
757
- copy = out - left;
758
- if (state->offset > copy) { /* copy from window */
759
- copy = state->offset - copy;
760
- if (copy > state->whave) {
761
- if (state->sane) {
762
- strm->msg = (z_const char *)"invalid distance too far back";
763
- state->mode = BAD;
764
- break;
765
- }
766
- #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
767
- Trace((stderr, "inflate9.c too far\n"));
768
- copy -= state->whave;
769
- if (copy > state->length)
770
- copy = state->length;
771
- if (copy > left)
772
- copy = left;
773
- left -= copy;
774
- state->length -= copy;
775
- do {
776
- *put++ = 0;
777
- } while (--copy);
778
- if (state->length == 0)
779
- state->mode = LEN;
780
- break;
781
- #endif
782
- }
783
- if (copy > state->wnext) {
784
- copy -= state->wnext;
785
- from = state->window + (state->wsize - copy);
786
- } else
787
- from = state->window + (state->wnext - copy);
788
- if (copy > state->length)
789
- copy = state->length;
790
- } else { /* copy from output */
791
- from = put - state->offset;
792
- copy = state->length;
793
- }
794
- if (copy > left)
795
- copy = left;
796
- left -= copy;
797
- state->length -= copy;
798
- do {
799
- *put++ = *from++;
800
- } while (--copy);
801
- if (state->length == 0)
802
- state->mode = LEN;
803
- break;
804
- case LIT:
805
- if (left == 0)
806
- goto inf_leave;
807
- *put++ = (unsigned char)(state->length);
808
- left--;
809
- state->mode = LEN;
810
- break;
811
- case DONE:
812
- ret = Z_STREAM_END;
813
- goto inf_leave;
814
- case BAD:
815
- ret = Z_DATA_ERROR;
816
- goto inf_leave;
817
- default:
818
- return Z_STREAM_ERROR;
819
- }
820
-
821
- /*
822
- Return from inflate9(), updating the total counts and the check value.
823
- If there was no progress during the inflate9() call, return a buffer
824
- error. Call updatewindow() to create and/or update the window state.
825
- Note: a memory error from inflate9() is non-recoverable.
826
- */
827
- inf_leave:
828
- RESTORE();
829
- if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
830
- (state->mode < DONE || flush != Z_FINISH)))
831
- if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
832
- state->mode = MEM;
833
- return Z_MEM_ERROR;
834
- }
835
- in -= strm->avail_in;
836
- out -= strm->avail_out;
837
- strm->total_in += in;
838
- strm->total_out += out;
839
- state->total += out;
840
- strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
841
- (state->mode == TYPE ? 128 : 0) +
842
- (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
843
- if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
844
- ret = Z_BUF_ERROR;
845
- return ret;
846
- }
847
-
848
- int ZEXPORT inflate9End(z_streamp strm) {
849
- struct inflate_state FAR *state;
850
- if (inflateStateCheck(strm))
851
- return Z_STREAM_ERROR;
852
- state = (struct inflate_state FAR *)strm->state;
853
- if (state->window != Z_NULL)
854
- ZFREE(strm, state->window);
855
- ZFREE(strm, strm->state);
856
- strm->state = Z_NULL;
857
- Tracev((stderr, "inflate: end\n"));
858
- return Z_OK;
859
- }