abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
inffast.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"

Go to the source code of this file.

Macros

#define OFF   1
 
#define PUP(a)   *++(a)
 

Functions

void ZLIB_INTERNAL inflate_fast (z_streamp strm, unsigned start)
 

Macro Definition Documentation

#define OFF   1

Definition at line 35 of file inffast.c.

#define PUP (   a)    *++(a)

Definition at line 36 of file inffast.c.

Function Documentation

void ZLIB_INTERNAL inflate_fast ( z_streamp  strm,
unsigned  start 
)

Definition at line 74 of file inffast.c.

75 {
76  struct inflate_state FAR *state;
77  unsigned char FAR *in; /* local strm->next_in */
78  unsigned char FAR *last; /* while in < last, enough input available */
79  unsigned char FAR *out; /* local strm->next_out */
80  unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
81  unsigned char FAR *end; /* while out < end, enough space available */
82 #ifdef INFLATE_STRICT
83  unsigned dmax; /* maximum distance from zlib header */
84 #endif
85  unsigned wsize; /* window size or zero if not using window */
86  unsigned whave; /* valid bytes in the window */
87  unsigned wnext; /* window write index */
88  unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
89  unsigned long hold; /* local strm->hold */
90  unsigned bits; /* local strm->bits */
91  code const FAR *lcode; /* local strm->lencode */
92  code const FAR *dcode; /* local strm->distcode */
93  unsigned lmask; /* mask for first level of length codes */
94  unsigned dmask; /* mask for first level of distance codes */
95  code here; /* retrieved table entry */
96  unsigned op; /* code bits, operation, extra bits, or */
97  /* window position, window bytes to copy */
98  unsigned len; /* match length, unused bytes */
99  unsigned dist; /* match distance */
100  unsigned char FAR *from; /* where to copy match from */
101 
102  /* copy state to local variables */
103  state = (struct inflate_state FAR *)strm->state;
104  in = strm->next_in - OFF;
105  last = in + (strm->avail_in - 5);
106  out = strm->next_out - OFF;
107  beg = out - (start - strm->avail_out);
108  end = out + (strm->avail_out - 257);
109 #ifdef INFLATE_STRICT
110  dmax = state->dmax;
111 #endif
112  wsize = state->wsize;
113  whave = state->whave;
114  wnext = state->wnext;
115  window = state->window;
116  hold = state->hold;
117  bits = state->bits;
118  lcode = state->lencode;
119  dcode = state->distcode;
120  lmask = (1U << state->lenbits) - 1;
121  dmask = (1U << state->distbits) - 1;
122 
123  /* decode literals and length/distances until end-of-block or not enough
124  input data or output space */
125  do {
126  if (bits < 15) {
127  hold += (unsigned long)(PUP(in)) << bits;
128  bits += 8;
129  hold += (unsigned long)(PUP(in)) << bits;
130  bits += 8;
131  }
132  here = lcode[hold & lmask];
133  dolen:
134  op = (unsigned)(here.bits);
135  hold >>= op;
136  bits -= op;
137  op = (unsigned)(here.op);
138  if (op == 0) { /* literal */
139  Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
140  "inflate: literal '%c'\n" :
141  "inflate: literal 0x%02x\n", here.val));
142  PUP(out) = (unsigned char)(here.val);
143  }
144  else if (op & 16) { /* length base */
145  len = (unsigned)(here.val);
146  op &= 15; /* number of extra bits */
147  if (op) {
148  if (bits < op) {
149  hold += (unsigned long)(PUP(in)) << bits;
150  bits += 8;
151  }
152  len += (unsigned)hold & ((1U << op) - 1);
153  hold >>= op;
154  bits -= op;
155  }
156  Tracevv((stderr, "inflate: length %u\n", len));
157  if (bits < 15) {
158  hold += (unsigned long)(PUP(in)) << bits;
159  bits += 8;
160  hold += (unsigned long)(PUP(in)) << bits;
161  bits += 8;
162  }
163  here = dcode[hold & dmask];
164  dodist:
165  op = (unsigned)(here.bits);
166  hold >>= op;
167  bits -= op;
168  op = (unsigned)(here.op);
169  if (op & 16) { /* distance base */
170  dist = (unsigned)(here.val);
171  op &= 15; /* number of extra bits */
172  if (bits < op) {
173  hold += (unsigned long)(PUP(in)) << bits;
174  bits += 8;
175  if (bits < op) {
176  hold += (unsigned long)(PUP(in)) << bits;
177  bits += 8;
178  }
179  }
180  dist += (unsigned)hold & ((1U << op) - 1);
181 #ifdef INFLATE_STRICT
182  if (dist > dmax) {
183  strm->msg = (char *)"invalid distance too far back";
184  state->mode = BAD;
185  break;
186  }
187 #endif
188  hold >>= op;
189  bits -= op;
190  Tracevv((stderr, "inflate: distance %u\n", dist));
191  op = (unsigned)(out - beg); /* max distance in output */
192  if (dist > op) { /* see if copy from window */
193  op = dist - op; /* distance back in window */
194  if (op > whave) {
195  if (state->sane) {
196  strm->msg =
197  (char *)"invalid distance too far back";
198  state->mode = BAD;
199  break;
200  }
201 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
202  if (len <= op - whave) {
203  do {
204  PUP(out) = 0;
205  } while (--len);
206  continue;
207  }
208  len -= op - whave;
209  do {
210  PUP(out) = 0;
211  } while (--op > whave);
212  if (op == 0) {
213  from = out - dist;
214  do {
215  PUP(out) = PUP(from);
216  } while (--len);
217  continue;
218  }
219 #endif
220  }
221  from = window - OFF;
222  if (wnext == 0) { /* very common case */
223  from += wsize - op;
224  if (op < len) { /* some from window */
225  len -= op;
226  do {
227  PUP(out) = PUP(from);
228  } while (--op);
229  from = out - dist; /* rest from output */
230  }
231  }
232  else if (wnext < op) { /* wrap around window */
233  from += wsize + wnext - op;
234  op -= wnext;
235  if (op < len) { /* some from end of window */
236  len -= op;
237  do {
238  PUP(out) = PUP(from);
239  } while (--op);
240  from = window - OFF;
241  if (wnext < len) { /* some from start of window */
242  op = wnext;
243  len -= op;
244  do {
245  PUP(out) = PUP(from);
246  } while (--op);
247  from = out - dist; /* rest from output */
248  }
249  }
250  }
251  else { /* contiguous in window */
252  from += wnext - op;
253  if (op < len) { /* some from window */
254  len -= op;
255  do {
256  PUP(out) = PUP(from);
257  } while (--op);
258  from = out - dist; /* rest from output */
259  }
260  }
261  while (len > 2) {
262  PUP(out) = PUP(from);
263  PUP(out) = PUP(from);
264  PUP(out) = PUP(from);
265  len -= 3;
266  }
267  if (len) {
268  PUP(out) = PUP(from);
269  if (len > 1)
270  PUP(out) = PUP(from);
271  }
272  }
273  else {
274  from = out - dist; /* copy direct from output */
275  do { /* minimum length is three */
276  PUP(out) = PUP(from);
277  PUP(out) = PUP(from);
278  PUP(out) = PUP(from);
279  len -= 3;
280  } while (len > 2);
281  if (len) {
282  PUP(out) = PUP(from);
283  if (len > 1)
284  PUP(out) = PUP(from);
285  }
286  }
287  }
288  else if ((op & 64) == 0) { /* 2nd level distance code */
289  here = dcode[here.val + (hold & ((1U << op) - 1))];
290  goto dodist;
291  }
292  else {
293  strm->msg = (char *)"invalid distance code";
294  state->mode = BAD;
295  break;
296  }
297  }
298  else if ((op & 64) == 0) { /* 2nd level length code */
299  here = lcode[here.val + (hold & ((1U << op) - 1))];
300  goto dolen;
301  }
302  else if (op & 32) { /* end-of-block */
303  Tracevv((stderr, "inflate: end of block\n"));
304  state->mode = TYPE;
305  break;
306  }
307  else {
308  strm->msg = (char *)"invalid literal/length code";
309  state->mode = BAD;
310  break;
311  }
312  } while (in < last && out < end);
313 
314  /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
315  len = bits >> 3;
316  in -= len;
317  bits -= len << 3;
318  hold &= (1U << bits) - 1;
319 
320  /* update state and return */
321  strm->next_in = in + OFF;
322  strm->next_out = out + OFF;
323  strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
324  strm->avail_out = (unsigned)(out < end ?
325  257 + (end - out) : 257 - (out - end));
326  state->hold = hold;
327  state->bits = bits;
328  return;
329 }
unsigned short val
Definition: inftrees.h:29
unsigned wnext
Definition: inflate.h:97
#define PUP(a)
Definition: inffast.c:36
unsigned wsize
Definition: inflate.h:95
unsigned distbits
Definition: inflate.h:111
code const FAR * distcode
Definition: inflate.h:109
unsigned char op
Definition: inftrees.h:27
Definition: inflate.h:34
#define OFF
Definition: inffast.c:35
unsigned lenbits
Definition: inflate.h:110
unsigned long hold
Definition: inflate.h:100
unsigned char bits
Definition: inftrees.h:28
Definition: inftrees.h:26
unsigned dmax
Definition: inflate.h:89
unsigned bits
Definition: inflate.h:101
unsigned char FAR * window
Definition: inflate.h:98
inflate_mode mode
Definition: inflate.h:84
Definition: inflate.h:52
unsigned whave
Definition: inflate.h:96
#define FAR
Definition: zconf.h:329
static char * bits(int n)
Definition: abcSaucy.c:201
code const FAR * lencode
Definition: inflate.h:108
#define Tracevv(x)
Definition: zutil.h:271