cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
inffast.c
Go to the documentation of this file.
1 /* inffast.c -- fast decoding
2  * Copyright (C) 1995-2008 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 #if defined( INC_ALL )
7  #include "crypt.h" /* For ASM override of longest-match */
8  #include "zutil.h"
9  #include "inftrees.h"
10  #include "inflate.h"
11  #include "inffast.h"
12 #else
13  #include "crypt.h" /* For ASM override of longest-match */
14  #include "zlib/zutil.h"
15  #include "zlib/inftrees.h"
16  #include "zlib/inflate.h"
17  #include "zlib/inffast.h"
18 #endif /* Compiler-specific includes */
19 #ifdef ASMV /* ASMV deflate implies ASMINF inflate - pcg */
20  #define ASMINF
21 #endif /* ASMV */
22 
23 #ifndef ASMINF
24 
25 /* Allow machine dependent optimization for post-increment or pre-increment.
26  Based on testing to date,
27  Pre-increment preferred for:
28  - PowerPC G3 (Adler)
29  - MIPS R5000 (Randers-Pehrson)
30  Post-increment preferred for:
31  - none
32  No measurable difference:
33  - Pentium III (Anderson)
34  - M68060 (Nikl)
35  */
36 #ifdef POSTINC
37 # define OFF 0
38 # define PUP(a) *(a)++
39 #else
40 # define OFF 1
41 # define PUP(a) *++(a)
42 #endif
43 
44 /*
45  Decode literal, length, and distance codes and write out the resulting
46  literal and match bytes until either not enough input or output is
47  available, an end-of-block is encountered, or a data error is encountered.
48  When large enough input and output buffers are supplied to inflate(), for
49  example, a 16K input buffer and a 64K output buffer, more than 95% of the
50  inflate execution time is spent in this routine.
51 
52  Entry assumptions:
53 
54  state->mode == LEN
55  strm->avail_in >= 6
56  strm->avail_out >= 258
57  start >= strm->avail_out
58  state->bits < 8
59 
60  On return, state->mode is one of:
61 
62  LEN -- ran out of enough output space or enough available input
63  TYPE -- reached end of block code, inflate() to interpret next block
64  BAD -- error in block data
65 
66  Notes:
67 
68  - The maximum input bits used by a length/distance pair is 15 bits for the
69  length code, 5 bits for the length extra, 15 bits for the distance code,
70  and 13 bits for the distance extra. This totals 48 bits, or six bytes.
71  Therefore if strm->avail_in >= 6, then there is enough input to avoid
72  checking for available input while decoding.
73 
74  - The maximum bytes that a single length/distance pair can output is 258
75  bytes, which is the maximum length that can be coded. inflate_fast()
76  requires strm->avail_out >= 258 for each loop to avoid checking for
77  output space.
78  */
79 void inflate_fast(z_streamp strm, /* pcg */
80  unsigned start) /* inflate()'s starting value for strm->avail_out */
81 {
82  struct inflate_state FAR *state;
83  unsigned char FAR *in; /* local strm->next_in */
84  unsigned char FAR *last; /* while in < last, enough input available */
85  unsigned char FAR *out; /* local strm->next_out */
86  unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
87  unsigned char FAR *end; /* while out < end, enough space available */
88 #ifdef INFLATE_STRICT
89  unsigned dmax; /* maximum distance from zlib header */
90 #endif
91  unsigned wsize; /* window size or zero if not using window */
92  unsigned whave; /* valid bytes in the window */
93  unsigned wnext; /* window write index */
94  unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
95  unsigned long hold; /* local strm->hold */
96  unsigned bits; /* local strm->bits */
97  code const FAR *lcode; /* local strm->lencode */
98  code const FAR *dcode; /* local strm->distcode */
99  unsigned lmask; /* mask for first level of length codes */
100  unsigned dmask; /* mask for first level of distance codes */
101  code here; /* retrieved table entry */
102  unsigned op; /* code bits, operation, extra bits, or */
103  /* window position, window bytes to copy */
104  unsigned len; /* match length, unused bytes */
105  unsigned dist; /* match distance */
106  unsigned char FAR *from; /* where to copy match from */
107 
108  /* copy state to local variables */
109  state = (struct inflate_state FAR *)strm->state;
110  in = strm->next_in - OFF;
111  last = in + (strm->avail_in - 5);
112  out = strm->next_out - OFF;
113  beg = out - (start - strm->avail_out);
114  end = out + (strm->avail_out - 257);
115 #ifdef INFLATE_STRICT
116  dmax = state->dmax;
117 #endif
118  wsize = state->wsize;
119  whave = state->whave;
120  wnext = state->wnext;
121  window = state->window;
122  hold = state->hold;
123  bits = state->bits;
124  lcode = state->lencode;
125  dcode = state->distcode;
126  lmask = (1U << state->lenbits) - 1;
127  dmask = (1U << state->distbits) - 1;
128 
129  /* decode literals and length/distances until end-of-block or not enough
130  input data or output space */
131  do {
132  if (bits < 15) {
133  hold += (unsigned long)(PUP(in)) << bits;
134  bits += 8;
135  hold += (unsigned long)(PUP(in)) << bits;
136  bits += 8;
137  }
138  here = lcode[hold & lmask];
139  dolen:
140  op = (unsigned)(here.bits);
141  hold >>= op;
142  bits -= op;
143  op = (unsigned)(here.op);
144  if (op == 0) { /* literal */
145  Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
146  "inflate: literal '%c'\n" :
147  "inflate: literal 0x%02x\n", here.val));
148  PUP(out) = (unsigned char)(here.val);
149  }
150  else if (op & 16) { /* length base */
151  len = (unsigned)(here.val);
152  op &= 15; /* number of extra bits */
153  if (op) {
154  if (bits < op) {
155  hold += (unsigned long)(PUP(in)) << bits;
156  bits += 8;
157  }
158  len += (unsigned)hold & ((1U << op) - 1);
159  hold >>= op;
160  bits -= op;
161  }
162  Tracevv((stderr, "inflate: length %u\n", len));
163  if (bits < 15) {
164  hold += (unsigned long)(PUP(in)) << bits;
165  bits += 8;
166  hold += (unsigned long)(PUP(in)) << bits;
167  bits += 8;
168  }
169  here = dcode[hold & dmask];
170  dodist:
171  op = (unsigned)(here.bits);
172  hold >>= op;
173  bits -= op;
174  op = (unsigned)(here.op);
175  if (op & 16) { /* distance base */
176  dist = (unsigned)(here.val);
177  op &= 15; /* number of extra bits */
178  if (bits < op) {
179  hold += (unsigned long)(PUP(in)) << bits;
180  bits += 8;
181  if (bits < op) {
182  hold += (unsigned long)(PUP(in)) << bits;
183  bits += 8;
184  }
185  }
186  dist += (unsigned)hold & ((1U << op) - 1);
187 #ifdef INFLATE_STRICT
188  if (dist > dmax) {
189  strm->msg = (char *)"invalid distance too far back";
190  state->mode = BAD;
191  break;
192  }
193 #endif
194  hold >>= op;
195  bits -= op;
196  Tracevv((stderr, "inflate: distance %u\n", dist));
197  op = (unsigned)(out - beg); /* max distance in output */
198  if (dist > op) { /* see if copy from window */
199  op = dist - op; /* distance back in window */
200  if (op > whave) {
201  if (state->sane) {
202  strm->msg =
203  (char *)"invalid distance too far back";
204  state->mode = BAD;
205  break;
206  }
207 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
208  if (len <= op - whave) {
209  do {
210  PUP(out) = 0;
211  } while (--len);
212  continue;
213  }
214  len -= op - whave;
215  do {
216  PUP(out) = 0;
217  } while (--op > whave);
218  if (op == 0) {
219  from = out - dist;
220  do {
221  PUP(out) = PUP(from);
222  } while (--len);
223  continue;
224  }
225 #endif
226  }
227  from = window - OFF;
228  if (wnext == 0) { /* very common case */
229  from += wsize - op;
230  if (op < len) { /* some from window */
231  len -= op;
232  do {
233  PUP(out) = PUP(from);
234  } while (--op);
235  from = out - dist; /* rest from output */
236  }
237  }
238  else if (wnext < op) { /* wrap around window */
239  from += wsize + wnext - op;
240  op -= wnext;
241  if (op < len) { /* some from end of window */
242  len -= op;
243  do {
244  PUP(out) = PUP(from);
245  } while (--op);
246  from = window - OFF;
247  if (wnext < len) { /* some from start of window */
248  op = wnext;
249  len -= op;
250  do {
251  PUP(out) = PUP(from);
252  } while (--op);
253  from = out - dist; /* rest from output */
254  }
255  }
256  }
257  else { /* contiguous in window */
258  from += wnext - op;
259  if (op < len) { /* some from window */
260  len -= op;
261  do {
262  PUP(out) = PUP(from);
263  } while (--op);
264  from = out - dist; /* rest from output */
265  }
266  }
267  while (len > 2) {
268  PUP(out) = PUP(from);
269  PUP(out) = PUP(from);
270  PUP(out) = PUP(from);
271  len -= 3;
272  }
273  if (len) {
274  PUP(out) = PUP(from);
275  if (len > 1)
276  PUP(out) = PUP(from);
277  }
278  }
279  else {
280  from = out - dist; /* copy direct from output */
281  do { /* minimum length is three */
282  PUP(out) = PUP(from);
283  PUP(out) = PUP(from);
284  PUP(out) = PUP(from);
285  len -= 3;
286  } while (len > 2);
287  if (len) {
288  PUP(out) = PUP(from);
289  if (len > 1)
290  PUP(out) = PUP(from);
291  }
292  }
293  }
294  else if ((op & 64) == 0) { /* 2nd level distance code */
295  here = dcode[here.val + (hold & ((1U << op) - 1))];
296  goto dodist;
297  }
298  else {
299  strm->msg = (char *)"invalid distance code";
300  state->mode = BAD;
301  break;
302  }
303  }
304  else if ((op & 64) == 0) { /* 2nd level length code */
305  here = lcode[here.val + (hold & ((1U << op) - 1))];
306  goto dolen;
307  }
308  else if (op & 32) { /* end-of-block */
309  Tracevv((stderr, "inflate: end of block\n"));
310  state->mode = TYPE;
311  break;
312  }
313  else {
314  strm->msg = (char *)"invalid literal/length code";
315  state->mode = BAD;
316  break;
317  }
318  } while (in < last && out < end);
319 
320  /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
321  len = bits >> 3;
322  in -= len;
323  bits -= len << 3;
324  hold &= (1U << bits) - 1;
325 
326  /* update state and return */
327  strm->next_in = in + OFF;
328  strm->next_out = out + OFF;
329  strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
330  strm->avail_out = (unsigned)(out < end ?
331  257 + (end - out) : 257 - (out - end));
332  state->hold = hold;
333  state->bits = bits;
334  return;
335 }
336 
337 /*
338  inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
339  - Using bit fields for code structure
340  - Different op definition to avoid & for extra bits (do & for table bits)
341  - Three separate decoding do-loops for direct, window, and wnext == 0
342  - Special case for distance > 1 copies to do overlapped load and store copy
343  - Explicit branch predictions (based on measured branch probabilities)
344  - Deferring match copy and interspersed it with decoding subsequent codes
345  - Swapping literal/length else
346  - Swapping window/direct else
347  - Larger unrolled copy loops (three is about right)
348  - Moving len -= 3 statement into middle of loop
349  */
350 
351 #endif /* !ASMINF */