1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 2008-2016, 2020 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions /* inffast.c -- fast decoding
29*27b03b36SApple OSS Distributions * Copyright (C) 1995-2004 Mark Adler
30*27b03b36SApple OSS Distributions * For conditions of distribution and use, see copyright notice in zlib.h
31*27b03b36SApple OSS Distributions */
32*27b03b36SApple OSS Distributions
33*27b03b36SApple OSS Distributions
34*27b03b36SApple OSS Distributions #include "zutil.h"
35*27b03b36SApple OSS Distributions #include "inftrees.h"
36*27b03b36SApple OSS Distributions #include "inflate.h"
37*27b03b36SApple OSS Distributions #include "inffast.h"
38*27b03b36SApple OSS Distributions
39*27b03b36SApple OSS Distributions #ifndef ASMINF
40*27b03b36SApple OSS Distributions
41*27b03b36SApple OSS Distributions /*
42*27b03b36SApple OSS Distributions Decode literal, length, and distance codes and write out the resulting
43*27b03b36SApple OSS Distributions literal and match bytes until either not enough input or output is
44*27b03b36SApple OSS Distributions available, an end-of-block is encountered, or a data error is encountered.
45*27b03b36SApple OSS Distributions When large enough input and output buffers are supplied to inflate(), for
46*27b03b36SApple OSS Distributions example, a 16K input buffer and a 64K output buffer, more than 95% of the
47*27b03b36SApple OSS Distributions inflate execution time is spent in this routine.
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions Entry assumptions:
50*27b03b36SApple OSS Distributions
51*27b03b36SApple OSS Distributions state->mode == LEN
52*27b03b36SApple OSS Distributions strm->avail_in >= 6
53*27b03b36SApple OSS Distributions strm->avail_out >= 258
54*27b03b36SApple OSS Distributions start >= strm->avail_out
55*27b03b36SApple OSS Distributions state->bits < 8
56*27b03b36SApple OSS Distributions
57*27b03b36SApple OSS Distributions On return, state->mode is one of:
58*27b03b36SApple OSS Distributions
59*27b03b36SApple OSS Distributions LEN -- ran out of enough output space or enough available input
60*27b03b36SApple OSS Distributions TYPE -- reached end of block code, inflate() to interpret next block
61*27b03b36SApple OSS Distributions BAD -- error in block data
62*27b03b36SApple OSS Distributions
63*27b03b36SApple OSS Distributions Notes:
64*27b03b36SApple OSS Distributions
65*27b03b36SApple OSS Distributions - The maximum input bits used by a length/distance pair is 15 bits for the
66*27b03b36SApple OSS Distributions length code, 5 bits for the length extra, 15 bits for the distance code,
67*27b03b36SApple OSS Distributions and 13 bits for the distance extra. This totals 48 bits, or six bytes.
68*27b03b36SApple OSS Distributions Therefore if strm->avail_in >= 6, then there is enough input to avoid
69*27b03b36SApple OSS Distributions checking for available input while decoding.
70*27b03b36SApple OSS Distributions
71*27b03b36SApple OSS Distributions - The maximum bytes that a single length/distance pair can output is 258
72*27b03b36SApple OSS Distributions bytes, which is the maximum length that can be coded. inflate_fast()
73*27b03b36SApple OSS Distributions requires strm->avail_out >= 258 for each loop to avoid checking for
74*27b03b36SApple OSS Distributions output space.
75*27b03b36SApple OSS Distributions
76*27b03b36SApple OSS Distributions @param start inflate()'s starting value for strm->avail_out
77*27b03b36SApple OSS Distributions */
78*27b03b36SApple OSS Distributions void
inflate_fast(z_streamp strm,unsigned start)79*27b03b36SApple OSS Distributions inflate_fast(z_streamp strm, unsigned start)
80*27b03b36SApple OSS Distributions {
81*27b03b36SApple OSS Distributions struct inflate_state FAR *state;
82*27b03b36SApple OSS Distributions unsigned char FAR *in; /* local strm->next_in */
83*27b03b36SApple OSS Distributions unsigned char FAR *last; /* while in < last, enough input available */
84*27b03b36SApple OSS Distributions unsigned char FAR *out; /* local strm->next_out */
85*27b03b36SApple OSS Distributions unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
86*27b03b36SApple OSS Distributions unsigned char FAR *end; /* while out < end, enough space available */
87*27b03b36SApple OSS Distributions #ifdef INFLATE_STRICT
88*27b03b36SApple OSS Distributions unsigned dmax; /* maximum distance from zlib header */
89*27b03b36SApple OSS Distributions #endif
90*27b03b36SApple OSS Distributions unsigned wsize; /* window size or zero if not using window */
91*27b03b36SApple OSS Distributions unsigned whave; /* valid bytes in the window */
92*27b03b36SApple OSS Distributions unsigned write; /* window write index */
93*27b03b36SApple OSS Distributions unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
94*27b03b36SApple OSS Distributions unsigned long hold; /* local strm->hold */
95*27b03b36SApple OSS Distributions unsigned bits; /* local strm->bits */
96*27b03b36SApple OSS Distributions code const FAR *lcode; /* local strm->lencode */
97*27b03b36SApple OSS Distributions code const FAR *dcode; /* local strm->distcode */
98*27b03b36SApple OSS Distributions unsigned lmask; /* mask for first level of length codes */
99*27b03b36SApple OSS Distributions unsigned dmask; /* mask for first level of distance codes */
100*27b03b36SApple OSS Distributions code this; /* retrieved table entry */
101*27b03b36SApple OSS Distributions unsigned op; /* code bits, operation, extra bits, or */
102*27b03b36SApple OSS Distributions /* window position, window bytes to copy */
103*27b03b36SApple OSS Distributions unsigned len; /* match length, unused bytes */
104*27b03b36SApple OSS Distributions unsigned dist; /* match distance */
105*27b03b36SApple OSS Distributions unsigned char FAR *from; /* where to copy match from */
106*27b03b36SApple OSS Distributions
107*27b03b36SApple OSS Distributions /* copy state to local variables */
108*27b03b36SApple OSS Distributions state = (struct inflate_state FAR *)strm->state;
109*27b03b36SApple OSS Distributions in = strm->next_in;
110*27b03b36SApple OSS Distributions last = in + (strm->avail_in - 5);
111*27b03b36SApple OSS Distributions out = strm->next_out;
112*27b03b36SApple OSS Distributions beg = out - (start - strm->avail_out);
113*27b03b36SApple OSS Distributions end = out + (strm->avail_out - 257);
114*27b03b36SApple OSS Distributions #ifdef INFLATE_STRICT
115*27b03b36SApple OSS Distributions dmax = state->dmax;
116*27b03b36SApple OSS Distributions #endif
117*27b03b36SApple OSS Distributions wsize = state->wsize;
118*27b03b36SApple OSS Distributions whave = state->whave;
119*27b03b36SApple OSS Distributions write = state->write;
120*27b03b36SApple OSS Distributions window = state->window;
121*27b03b36SApple OSS Distributions hold = state->hold;
122*27b03b36SApple OSS Distributions bits = state->bits;
123*27b03b36SApple OSS Distributions lcode = state->lencode;
124*27b03b36SApple OSS Distributions dcode = state->distcode;
125*27b03b36SApple OSS Distributions lmask = (1U << state->lenbits) - 1;
126*27b03b36SApple OSS Distributions dmask = (1U << state->distbits) - 1;
127*27b03b36SApple OSS Distributions
128*27b03b36SApple OSS Distributions /* decode literals and length/distances until end-of-block or not enough
129*27b03b36SApple OSS Distributions input data or output space */
130*27b03b36SApple OSS Distributions do {
131*27b03b36SApple OSS Distributions if (bits < 15) {
132*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
133*27b03b36SApple OSS Distributions bits += 8;
134*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
135*27b03b36SApple OSS Distributions bits += 8;
136*27b03b36SApple OSS Distributions }
137*27b03b36SApple OSS Distributions this = lcode[hold & lmask];
138*27b03b36SApple OSS Distributions dolen:
139*27b03b36SApple OSS Distributions op = (unsigned)(this.bits);
140*27b03b36SApple OSS Distributions hold >>= op;
141*27b03b36SApple OSS Distributions bits -= op;
142*27b03b36SApple OSS Distributions op = (unsigned)(this.op);
143*27b03b36SApple OSS Distributions if (op == 0) { /* literal */
144*27b03b36SApple OSS Distributions Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
145*27b03b36SApple OSS Distributions "inflate: literal '%c'\n" :
146*27b03b36SApple OSS Distributions "inflate: literal 0x%02x\n", this.val));
147*27b03b36SApple OSS Distributions *out++ = (unsigned char)(this.val);
148*27b03b36SApple OSS Distributions }
149*27b03b36SApple OSS Distributions else if (op & 16) { /* length base */
150*27b03b36SApple OSS Distributions len = (unsigned)(this.val);
151*27b03b36SApple OSS Distributions op &= 15; /* number of extra bits */
152*27b03b36SApple OSS Distributions if (op) {
153*27b03b36SApple OSS Distributions if (bits < op) {
154*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
155*27b03b36SApple OSS Distributions bits += 8;
156*27b03b36SApple OSS Distributions }
157*27b03b36SApple OSS Distributions len += (unsigned)hold & ((1U << op) - 1);
158*27b03b36SApple OSS Distributions hold >>= op;
159*27b03b36SApple OSS Distributions bits -= op;
160*27b03b36SApple OSS Distributions }
161*27b03b36SApple OSS Distributions Tracevv((stderr, "inflate: length %u\n", len));
162*27b03b36SApple OSS Distributions if (bits < 15) {
163*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
164*27b03b36SApple OSS Distributions bits += 8;
165*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
166*27b03b36SApple OSS Distributions bits += 8;
167*27b03b36SApple OSS Distributions }
168*27b03b36SApple OSS Distributions this = dcode[hold & dmask];
169*27b03b36SApple OSS Distributions dodist:
170*27b03b36SApple OSS Distributions op = (unsigned)(this.bits);
171*27b03b36SApple OSS Distributions hold >>= op;
172*27b03b36SApple OSS Distributions bits -= op;
173*27b03b36SApple OSS Distributions op = (unsigned)(this.op);
174*27b03b36SApple OSS Distributions if (op & 16) { /* distance base */
175*27b03b36SApple OSS Distributions dist = (unsigned)(this.val);
176*27b03b36SApple OSS Distributions op &= 15; /* number of extra bits */
177*27b03b36SApple OSS Distributions if (bits < op) {
178*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
179*27b03b36SApple OSS Distributions bits += 8;
180*27b03b36SApple OSS Distributions if (bits < op) {
181*27b03b36SApple OSS Distributions hold += (unsigned long)(*in++) << bits;
182*27b03b36SApple OSS Distributions bits += 8;
183*27b03b36SApple OSS Distributions }
184*27b03b36SApple OSS Distributions }
185*27b03b36SApple OSS Distributions dist += (unsigned)hold & ((1U << op) - 1);
186*27b03b36SApple OSS Distributions #ifdef INFLATE_STRICT
187*27b03b36SApple OSS Distributions if (dist > dmax) {
188*27b03b36SApple OSS Distributions strm->msg = (char *)"invalid distance too far back";
189*27b03b36SApple OSS Distributions state->mode = BAD;
190*27b03b36SApple OSS Distributions break;
191*27b03b36SApple OSS Distributions }
192*27b03b36SApple OSS Distributions #endif
193*27b03b36SApple OSS Distributions hold >>= op;
194*27b03b36SApple OSS Distributions bits -= op;
195*27b03b36SApple OSS Distributions Tracevv((stderr, "inflate: distance %u\n", dist));
196*27b03b36SApple OSS Distributions op = (unsigned)(out - beg); /* max distance in output */
197*27b03b36SApple OSS Distributions if (dist > op) { /* see if copy from window */
198*27b03b36SApple OSS Distributions op = dist - op; /* distance back in window */
199*27b03b36SApple OSS Distributions if (op > whave) {
200*27b03b36SApple OSS Distributions strm->msg = (char *)"invalid distance too far back";
201*27b03b36SApple OSS Distributions state->mode = BAD;
202*27b03b36SApple OSS Distributions break;
203*27b03b36SApple OSS Distributions }
204*27b03b36SApple OSS Distributions from = window;
205*27b03b36SApple OSS Distributions if (write == 0) { /* very common case */
206*27b03b36SApple OSS Distributions from += wsize - op;
207*27b03b36SApple OSS Distributions if (op < len) { /* some from window */
208*27b03b36SApple OSS Distributions len -= op;
209*27b03b36SApple OSS Distributions do {
210*27b03b36SApple OSS Distributions *out++ = *from++;
211*27b03b36SApple OSS Distributions } while (--op);
212*27b03b36SApple OSS Distributions from = out - dist; /* rest from output */
213*27b03b36SApple OSS Distributions }
214*27b03b36SApple OSS Distributions }
215*27b03b36SApple OSS Distributions else if (write < op) { /* wrap around window */
216*27b03b36SApple OSS Distributions from += wsize + write - op;
217*27b03b36SApple OSS Distributions op -= write;
218*27b03b36SApple OSS Distributions if (op < len) { /* some from end of window */
219*27b03b36SApple OSS Distributions len -= op;
220*27b03b36SApple OSS Distributions do {
221*27b03b36SApple OSS Distributions *out++ = *from++;
222*27b03b36SApple OSS Distributions } while (--op);
223*27b03b36SApple OSS Distributions from = window;
224*27b03b36SApple OSS Distributions if (write < len) { /* some from start of window */
225*27b03b36SApple OSS Distributions op = write;
226*27b03b36SApple OSS Distributions len -= op;
227*27b03b36SApple OSS Distributions do {
228*27b03b36SApple OSS Distributions *out++ = *from++;
229*27b03b36SApple OSS Distributions } while (--op);
230*27b03b36SApple OSS Distributions from = out - dist; /* rest from output */
231*27b03b36SApple OSS Distributions }
232*27b03b36SApple OSS Distributions }
233*27b03b36SApple OSS Distributions }
234*27b03b36SApple OSS Distributions else { /* contiguous in window */
235*27b03b36SApple OSS Distributions from += write - op;
236*27b03b36SApple OSS Distributions if (op < len) { /* some from window */
237*27b03b36SApple OSS Distributions len -= op;
238*27b03b36SApple OSS Distributions do {
239*27b03b36SApple OSS Distributions *out++ = *from++;
240*27b03b36SApple OSS Distributions } while (--op);
241*27b03b36SApple OSS Distributions from = out - dist; /* rest from output */
242*27b03b36SApple OSS Distributions }
243*27b03b36SApple OSS Distributions }
244*27b03b36SApple OSS Distributions while (len > 2) {
245*27b03b36SApple OSS Distributions *out++ = *from++;
246*27b03b36SApple OSS Distributions *out++ = *from++;
247*27b03b36SApple OSS Distributions *out++ = *from++;
248*27b03b36SApple OSS Distributions len -= 3;
249*27b03b36SApple OSS Distributions }
250*27b03b36SApple OSS Distributions if (len) {
251*27b03b36SApple OSS Distributions *out++ = *from++;
252*27b03b36SApple OSS Distributions if (len > 1)
253*27b03b36SApple OSS Distributions *out++ = *from++;
254*27b03b36SApple OSS Distributions }
255*27b03b36SApple OSS Distributions }
256*27b03b36SApple OSS Distributions else {
257*27b03b36SApple OSS Distributions from = out - dist; /* copy direct from output */
258*27b03b36SApple OSS Distributions do { /* minimum length is three */
259*27b03b36SApple OSS Distributions *out++ = *from++;
260*27b03b36SApple OSS Distributions *out++ = *from++;
261*27b03b36SApple OSS Distributions *out++ = *from++;
262*27b03b36SApple OSS Distributions len -= 3;
263*27b03b36SApple OSS Distributions } while (len > 2);
264*27b03b36SApple OSS Distributions if (len) {
265*27b03b36SApple OSS Distributions *out++ = *from++;
266*27b03b36SApple OSS Distributions if (len > 1)
267*27b03b36SApple OSS Distributions *out++ = *from++;
268*27b03b36SApple OSS Distributions }
269*27b03b36SApple OSS Distributions }
270*27b03b36SApple OSS Distributions }
271*27b03b36SApple OSS Distributions else if ((op & 64) == 0) { /* 2nd level distance code */
272*27b03b36SApple OSS Distributions this = dcode[this.val + (hold & ((1U << op) - 1))];
273*27b03b36SApple OSS Distributions goto dodist;
274*27b03b36SApple OSS Distributions }
275*27b03b36SApple OSS Distributions else {
276*27b03b36SApple OSS Distributions strm->msg = (char *)"invalid distance code";
277*27b03b36SApple OSS Distributions state->mode = BAD;
278*27b03b36SApple OSS Distributions break;
279*27b03b36SApple OSS Distributions }
280*27b03b36SApple OSS Distributions }
281*27b03b36SApple OSS Distributions else if ((op & 64) == 0) { /* 2nd level length code */
282*27b03b36SApple OSS Distributions this = lcode[this.val + (hold & ((1U << op) - 1))];
283*27b03b36SApple OSS Distributions goto dolen;
284*27b03b36SApple OSS Distributions }
285*27b03b36SApple OSS Distributions else if (op & 32) { /* end-of-block */
286*27b03b36SApple OSS Distributions Tracevv((stderr, "inflate: end of block\n"));
287*27b03b36SApple OSS Distributions state->mode = TYPE;
288*27b03b36SApple OSS Distributions break;
289*27b03b36SApple OSS Distributions }
290*27b03b36SApple OSS Distributions else {
291*27b03b36SApple OSS Distributions strm->msg = (char *)"invalid literal/length code";
292*27b03b36SApple OSS Distributions state->mode = BAD;
293*27b03b36SApple OSS Distributions break;
294*27b03b36SApple OSS Distributions }
295*27b03b36SApple OSS Distributions } while (in < last && out < end);
296*27b03b36SApple OSS Distributions
297*27b03b36SApple OSS Distributions /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
298*27b03b36SApple OSS Distributions len = bits >> 3;
299*27b03b36SApple OSS Distributions in -= len;
300*27b03b36SApple OSS Distributions bits -= len << 3;
301*27b03b36SApple OSS Distributions hold &= (1U << bits) - 1;
302*27b03b36SApple OSS Distributions
303*27b03b36SApple OSS Distributions /* update state and return */
304*27b03b36SApple OSS Distributions strm->next_in = in;
305*27b03b36SApple OSS Distributions strm->next_out = out;
306*27b03b36SApple OSS Distributions strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
307*27b03b36SApple OSS Distributions strm->avail_out = (unsigned)(out < end ?
308*27b03b36SApple OSS Distributions 257 + (end - out) : 257 - (out - end));
309*27b03b36SApple OSS Distributions state->hold = hold;
310*27b03b36SApple OSS Distributions state->bits = bits;
311*27b03b36SApple OSS Distributions return;
312*27b03b36SApple OSS Distributions }
313*27b03b36SApple OSS Distributions
314*27b03b36SApple OSS Distributions /*
315*27b03b36SApple OSS Distributions inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
316*27b03b36SApple OSS Distributions - Using bit fields for code structure
317*27b03b36SApple OSS Distributions - Different op definition to avoid & for extra bits (do & for table bits)
318*27b03b36SApple OSS Distributions - Three separate decoding do-loops for direct, window, and write == 0
319*27b03b36SApple OSS Distributions - Special case for distance > 1 copies to do overlapped load and store copy
320*27b03b36SApple OSS Distributions - Explicit branch predictions (based on measured branch probabilities)
321*27b03b36SApple OSS Distributions - Deferring match copy and interspersed it with decoding subsequent codes
322*27b03b36SApple OSS Distributions - Swapping literal/length else
323*27b03b36SApple OSS Distributions - Swapping window/direct else
324*27b03b36SApple OSS Distributions - Larger unrolled copy loops (three is about right)
325*27b03b36SApple OSS Distributions - Moving len -= 3 statement into middle of loop
326*27b03b36SApple OSS Distributions */
327*27b03b36SApple OSS Distributions
328*27b03b36SApple OSS Distributions #endif /* !ASMINF */
329