1// See state defs from inflate.js
2var BAD = 30; /* got a data error -- remain here until reset */
3var TYPE = 12; /* i: waiting for type bits, including last-flag bit */
6 Decode literal, length, and distance codes and write out the resulting
7 literal and match bytes until either not enough input or output is
8 available, an end-of-block is encountered, or a data error is encountered.
9 When large enough input and output buffers are supplied to inflate(), for
10 example, a 16K input buffer and a 64K output buffer, more than 95% of the
11 inflate execution time is spent in this routine.
18 start >= strm.avail_out
21 On return, state.mode is one of:
23 LEN -- ran out of enough output space or enough available input
24 TYPE -- reached end of block code, inflate() to interpret next block
25 BAD -- error in block data
29 - The maximum input bits used by a length/distance pair is 15 bits for the
30 length code, 5 bits for the length extra, 15 bits for the distance code,
31 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
32 Therefore if strm.avail_in >= 6, then there is enough input to avoid
33 checking for available input while decoding.
35 - The maximum bytes that a single length/distance pair can output is 258
36 bytes, which is the maximum length that can be coded. inflate_fast()
37 requires strm.avail_out >= 258 for each loop to avoid checking for
40export default function inflate_fast(strm, start) {
42 var _in; /* local strm.input */
43 var last; /* have enough input while in < last */
44 var _out; /* local strm.output */
45 var beg; /* inflate()'s initial strm.output */
46 var end; /* while out < end, enough space available */
47//#ifdef INFLATE_STRICT
48 var dmax; /* maximum distance from zlib header */
50 var wsize; /* window size or zero if not using window */
51 var whave; /* valid bytes in the window */
52 var wnext; /* window write index */
53 // Use `s_window` instead `window`, avoid conflict with instrumentation tools
54 var s_window; /* allocated sliding window, if wsize != 0 */
55 var hold; /* local strm.hold */
56 var bits; /* local strm.bits */
57 var lcode; /* local strm.lencode */
58 var dcode; /* local strm.distcode */
59 var lmask; /* mask for first level of length codes */
60 var dmask; /* mask for first level of distance codes */
61 var here; /* retrieved table entry */
62 var op; /* code bits, operation, extra bits, or */
63 /* window position, window bytes to copy */
64 var len; /* match length, unused bytes */
65 var dist; /* match distance */
66 var from; /* where to copy match from */
70 var input, output; // JS specific, because we have no pointers
72 /* copy state to local variables */
77 last = _in + (strm.avail_in - 5);
80 beg = _out - (start - strm.avail_out);
81 end = _out + (strm.avail_out - 257);
82//#ifdef INFLATE_STRICT
88 s_window = state.window;
91 lcode = state.lencode;
92 dcode = state.distcode;
93 lmask = (1 << state.lenbits) - 1;
94 dmask = (1 << state.distbits) - 1;
97 /* decode literals and length/distances until end-of-block or not enough
98 input data or output space */
103 hold += input[_in++] << bits;
105 hold += input[_in++] << bits;
109 here = lcode[hold & lmask];
112 for (;;) { // Goto emulation
113 op = here >>> 24/*here.bits*/;
116 op = (here >>> 16) & 0xff/*here.op*/;
117 if (op === 0) { /* literal */
118 //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
119 // "inflate: literal '%c'\n" :
120 // "inflate: literal 0x%02x\n", here.val));
121 output[_out++] = here & 0xffff/*here.val*/;
123 else if (op & 16) { /* length base */
124 len = here & 0xffff/*here.val*/;
125 op &= 15; /* number of extra bits */
128 hold += input[_in++] << bits;
131 len += hold & ((1 << op) - 1);
135 //Tracevv((stderr, "inflate: length %u\n", len));
137 hold += input[_in++] << bits;
139 hold += input[_in++] << bits;
142 here = dcode[hold & dmask];
145 for (;;) { // goto emulation
146 op = here >>> 24/*here.bits*/;
149 op = (here >>> 16) & 0xff/*here.op*/;
151 if (op & 16) { /* distance base */
152 dist = here & 0xffff/*here.val*/;
153 op &= 15; /* number of extra bits */
155 hold += input[_in++] << bits;
158 hold += input[_in++] << bits;
162 dist += hold & ((1 << op) - 1);
163//#ifdef INFLATE_STRICT
165 strm.msg = 'invalid distance too far back';
172 //Tracevv((stderr, "inflate: distance %u\n", dist));
173 op = _out - beg; /* max distance in output */
174 if (dist > op) { /* see if copy from window */
175 op = dist - op; /* distance back in window */
178 strm.msg = 'invalid distance too far back';
183// (!) This block is disabled in zlib defailts,
184// don't enable it for binary compatibility
185//#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
186// if (len <= op - whave) {
188// output[_out++] = 0;
194// output[_out++] = 0;
195// } while (--op > whave);
197// from = _out - dist;
199// output[_out++] = output[from++];
205 from = 0; // window index
206 from_source = s_window;
207 if (wnext === 0) { /* very common case */
209 if (op < len) { /* some from window */
212 output[_out++] = s_window[from++];
214 from = _out - dist; /* rest from output */
215 from_source = output;
218 else if (wnext < op) { /* wrap around window */
219 from += wsize + wnext - op;
221 if (op < len) { /* some from end of window */
224 output[_out++] = s_window[from++];
227 if (wnext < len) { /* some from start of window */
231 output[_out++] = s_window[from++];
233 from = _out - dist; /* rest from output */
234 from_source = output;
238 else { /* contiguous in window */
240 if (op < len) { /* some from window */
243 output[_out++] = s_window[from++];
245 from = _out - dist; /* rest from output */
246 from_source = output;
250 output[_out++] = from_source[from++];
251 output[_out++] = from_source[from++];
252 output[_out++] = from_source[from++];
256 output[_out++] = from_source[from++];
258 output[_out++] = from_source[from++];
263 from = _out - dist; /* copy direct from output */
264 do { /* minimum length is three */
265 output[_out++] = output[from++];
266 output[_out++] = output[from++];
267 output[_out++] = output[from++];
271 output[_out++] = output[from++];
273 output[_out++] = output[from++];
278 else if ((op & 64) === 0) { /* 2nd level distance code */
279 here = dcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
283 strm.msg = 'invalid distance code';
288 break; // need to emulate goto via "continue"
291 else if ((op & 64) === 0) { /* 2nd level length code */
292 here = lcode[(here & 0xffff)/*here.val*/ + (hold & ((1 << op) - 1))];
295 else if (op & 32) { /* end-of-block */
296 //Tracevv((stderr, "inflate: end of block\n"));
301 strm.msg = 'invalid literal/length code';
306 break; // need to emulate goto via "continue"
308 } while (_in < last && _out < end);
310 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
314 hold &= (1 << bits) - 1;
316 /* update state and return */
318 strm.next_out = _out;
319 strm.avail_in = (_in < last ? 5 + (last - _in) : 5 - (_in - last));
320 strm.avail_out = (_out < end ? 257 + (end - _out) : 257 - (_out - end));