1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3  *
  4  * This code is free software; you can redistribute it and/or modify it
  5  * under the terms of the GNU General Public License version 2 only, as
  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* inffast.c -- fast decoding
 26  * Copyright (C) 1995-2017 Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 #include "zutil.h"
 31 #include "inftrees.h"
 32 #include "inflate.h"
 33 #include "inffast.h"
 34 
 35 #ifdef ASMINF
 36 #  pragma message("Assembler code may have bugs -- use at your own risk")
 37 #else
 38 
 39 /*
 40    Decode literal, length, and distance codes and write out the resulting
 41    literal and match bytes until either not enough input or output is
 42    available, an end-of-block is encountered, or a data error is encountered.
 43    When large enough input and output buffers are supplied to inflate(), for
 44    example, a 16K input buffer and a 64K output buffer, more than 95% of the
 45    inflate execution time is spent in this routine.
 46 
 47    Entry assumptions:
 48 
 49         state->mode == LEN
 50         strm->avail_in >= 6
 51         strm->avail_out >= 258
 52         start >= strm->avail_out
 53         state->bits < 8
 54 
 55    On return, state->mode is one of:
 56 
 57         LEN -- ran out of enough output space or enough available input
 58         TYPE -- reached end of block code, inflate() to interpret next block
 59         BAD -- error in block data
 60 
 61    Notes:
 62 
 63     - The maximum input bits used by a length/distance pair is 15 bits for the
 64       length code, 5 bits for the length extra, 15 bits for the distance code,
 65       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
 66       Therefore if strm->avail_in >= 6, then there is enough input to avoid
 67       checking for available input while decoding.
 68 
 69     - The maximum bytes that a single length/distance pair can output is 258
 70       bytes, which is the maximum length that can be coded.  inflate_fast()
 71       requires strm->avail_out >= 258 for each loop to avoid checking for
 72       output space.
 73  */
 74 void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
 75     struct inflate_state FAR *state;
 76     z_const unsigned char FAR *in;      /* local strm->next_in */
 77     z_const unsigned char FAR *last;    /* have enough input while in < last */
 78     unsigned char FAR *out;     /* local strm->next_out */
 79     unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
 80     unsigned char FAR *end;     /* while out < end, enough space available */
 81 #ifdef INFLATE_STRICT
 82     unsigned dmax;              /* maximum distance from zlib header */
 83 #endif
 84     unsigned wsize;             /* window size or zero if not using window */
 85     unsigned whave;             /* valid bytes in the window */
 86     unsigned wnext;             /* window write index */
 87     unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
 88     unsigned long hold;         /* local strm->hold */
 89     unsigned bits;              /* local strm->bits */
 90     code const FAR *lcode;      /* local strm->lencode */
 91     code const FAR *dcode;      /* local strm->distcode */
 92     unsigned lmask;             /* mask for first level of length codes */
 93     unsigned dmask;             /* mask for first level of distance codes */
 94     code const *here;           /* retrieved table entry */
 95     unsigned op;                /* code bits, operation, extra bits, or */
 96                                 /*  window position, window bytes to copy */
 97     unsigned len;               /* match length, unused bytes */
 98     unsigned dist;              /* match distance */
 99     unsigned char FAR *from;    /* where to copy match from */
100 
101     /* copy state to local variables */
102     state = (struct inflate_state FAR *)strm->state;
103     in = strm->next_in;
104     last = in + (strm->avail_in - 5);
105     out = strm->next_out;
106     beg = out - (start - strm->avail_out);
107     end = out + (strm->avail_out - 257);
108 #ifdef INFLATE_STRICT
109     dmax = state->dmax;
110 #endif
111     wsize = state->wsize;
112     whave = state->whave;
113     wnext = state->wnext;
114     window = state->window;
115     hold = state->hold;
116     bits = state->bits;
117     lcode = state->lencode;
118     dcode = state->distcode;
119     lmask = (1U << state->lenbits) - 1;
120     dmask = (1U << state->distbits) - 1;
121 
122     /* decode literals and length/distances until end-of-block or not enough
123        input data or output space */
124     do {
125         if (bits < 15) {
126             hold += (unsigned long)(*in++) << bits;
127             bits += 8;
128             hold += (unsigned long)(*in++) << bits;
129             bits += 8;
130         }
131         here = lcode + (hold & lmask);
132       dolen:
133         op = (unsigned)(here->bits);
134         hold >>= op;
135         bits -= op;
136         op = (unsigned)(here->op);
137         if (op == 0) {                          /* literal */
138             Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
139                     "inflate:         literal '%c'\n" :
140                     "inflate:         literal 0x%02x\n", here->val));
141             *out++ = (unsigned char)(here->val);
142         }
143         else if (op & 16) {                     /* length base */
144             len = (unsigned)(here->val);
145             op &= 15;                           /* number of extra bits */
146             if (op) {
147                 if (bits < op) {
148                     hold += (unsigned long)(*in++) << bits;
149                     bits += 8;
150                 }
151                 len += (unsigned)hold & ((1U << op) - 1);
152                 hold >>= op;
153                 bits -= op;
154             }
155             Tracevv((stderr, "inflate:         length %u\n", len));
156             if (bits < 15) {
157                 hold += (unsigned long)(*in++) << bits;
158                 bits += 8;
159                 hold += (unsigned long)(*in++) << bits;
160                 bits += 8;
161             }
162             here = dcode + (hold & dmask);
163           dodist:
164             op = (unsigned)(here->bits);
165             hold >>= op;
166             bits -= op;
167             op = (unsigned)(here->op);
168             if (op & 16) {                      /* distance base */
169                 dist = (unsigned)(here->val);
170                 op &= 15;                       /* number of extra bits */
171                 if (bits < op) {
172                     hold += (unsigned long)(*in++) << bits;
173                     bits += 8;
174                     if (bits < op) {
175                         hold += (unsigned long)(*in++) << bits;
176                         bits += 8;
177                     }
178                 }
179                 dist += (unsigned)hold & ((1U << op) - 1);
180 #ifdef INFLATE_STRICT
181                 if (dist > dmax) {
182                     strm->msg = (char *)"invalid distance too far back";
183                     state->mode = BAD;
184                     break;
185                 }
186 #endif
187                 hold >>= op;
188                 bits -= op;
189                 Tracevv((stderr, "inflate:         distance %u\n", dist));
190                 op = (unsigned)(out - beg);     /* max distance in output */
191                 if (dist > op) {                /* see if copy from window */
192                     op = dist - op;             /* distance back in window */
193                     if (op > whave) {
194                         if (state->sane) {
195                             strm->msg =
196                                 (char *)"invalid distance too far back";
197                             state->mode = BAD;
198                             break;
199                         }
200 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
201                         if (len <= op - whave) {
202                             do {
203                                 *out++ = 0;
204                             } while (--len);
205                             continue;
206                         }
207                         len -= op - whave;
208                         do {
209                             *out++ = 0;
210                         } while (--op > whave);
211                         if (op == 0) {
212                             from = out - dist;
213                             do {
214                                 *out++ = *from++;
215                             } while (--len);
216                             continue;
217                         }
218 #endif
219                     }
220                     from = window;
221                     if (wnext == 0) {           /* very common case */
222                         from += wsize - op;
223                         if (op < len) {         /* some from window */
224                             len -= op;
225                             do {
226                                 *out++ = *from++;
227                             } while (--op);
228                             from = out - dist;  /* rest from output */
229                         }
230                     }
231                     else if (wnext < op) {      /* wrap around window */
232                         from += wsize + wnext - op;
233                         op -= wnext;
234                         if (op < len) {         /* some from end of window */
235                             len -= op;
236                             do {
237                                 *out++ = *from++;
238                             } while (--op);
239                             from = window;
240                             if (wnext < len) {  /* some from start of window */
241                                 op = wnext;
242                                 len -= op;
243                                 do {
244                                     *out++ = *from++;
245                                 } while (--op);
246                                 from = out - dist;      /* rest from output */
247                             }
248                         }
249                     }
250                     else {                      /* contiguous in window */
251                         from += wnext - op;
252                         if (op < len) {         /* some from window */
253                             len -= op;
254                             do {
255                                 *out++ = *from++;
256                             } while (--op);
257                             from = out - dist;  /* rest from output */
258                         }
259                     }
260                     while (len > 2) {
261                         *out++ = *from++;
262                         *out++ = *from++;
263                         *out++ = *from++;
264                         len -= 3;
265                     }
266                     if (len) {
267                         *out++ = *from++;
268                         if (len > 1)
269                             *out++ = *from++;
270                     }
271                 }
272                 else {
273                     from = out - dist;          /* copy direct from output */
274                     do {                        /* minimum length is three */
275                         *out++ = *from++;
276                         *out++ = *from++;
277                         *out++ = *from++;
278                         len -= 3;
279                     } while (len > 2);
280                     if (len) {
281                         *out++ = *from++;
282                         if (len > 1)
283                             *out++ = *from++;
284                     }
285                 }
286             }
287             else if ((op & 64) == 0) {          /* 2nd level distance code */
288                 here = dcode + here->val + (hold & ((1U << op) - 1));
289                 goto dodist;
290             }
291             else {
292                 strm->msg = (char *)"invalid distance code";
293                 state->mode = BAD;
294                 break;
295             }
296         }
297         else if ((op & 64) == 0) {              /* 2nd level length code */
298             here = lcode + here->val + (hold & ((1U << op) - 1));
299             goto dolen;
300         }
301         else if (op & 32) {                     /* end-of-block */
302             Tracevv((stderr, "inflate:         end of block\n"));
303             state->mode = TYPE;
304             break;
305         }
306         else {
307             strm->msg = (char *)"invalid literal/length code";
308             state->mode = BAD;
309             break;
310         }
311     } while (in < last && out < end);
312 
313     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
314     len = bits >> 3;
315     in -= len;
316     bits -= len << 3;
317     hold &= (1U << bits) - 1;
318 
319     /* update state and return */
320     strm->next_in = in;
321     strm->next_out = out;
322     strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
323     strm->avail_out = (unsigned)(out < end ?
324                                  257 + (end - out) : 257 - (out - end));
325     state->hold = hold;
326     state->bits = bits;
327     return;
328 }
329 
330 /*
331    inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
332    - Using bit fields for code structure
333    - Different op definition to avoid & for extra bits (do & for table bits)
334    - Three separate decoding do-loops for direct, window, and wnext == 0
335    - Special case for distance > 1 copies to do overlapped load and store copy
336    - Explicit branch predictions (based on measured branch probabilities)
337    - Deferring match copy and interspersed it with decoding subsequent codes
338    - Swapping literal/length else
339    - Swapping window/direct else
340    - Larger unrolled copy loops (three is about right)
341    - Moving len -= 3 statement into middle of loop
342  */
343 
344 #endif /* !ASMINF */