COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
inffast.c File Reference
#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"
Include dependency graph for inffast.c:

Macros

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

Functions

void inflate_fast (z_streamp strm, unsigned start)
 

Macro Definition Documentation

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

Function Documentation

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