Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: third_party/zlib/contrib/arm/inflate.c

Issue 2722063002: zlib: inflate using wider loads and stores
Patch Set: zlib: inflate using wider loads and stores Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/zlib/contrib/arm/inffast.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 /*
7 * Change history:
8 *
9 * 1.2.beta0 24 Nov 2002
10 * - First version -- complete rewrite of inflate to simplify code, avoid
11 * creation of window when not needed, minimize use of window when it is
12 * needed, make inffast.c even faster, implement gzip decoding, and to
13 * improve code readability and style over the previous zlib inflate code
14 *
15 * 1.2.beta1 25 Nov 2002
16 * - Use pointers for available input and output checking in inffast.c
17 * - Remove input and output counters in inffast.c
18 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19 * - Remove unnecessary second byte pull from length extra in inffast.c
20 * - Unroll direct copy to three copies per loop in inffast.c
21 *
22 * 1.2.beta2 4 Dec 2002
23 * - Change external routine names to reduce potential conflicts
24 * - Correct filename to inffixed.h for fixed tables in inflate.c
25 * - Make hbuf[] unsigned char to match parameter type in inflate.c
26 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27 * to avoid negation problem on Alphas (64 bit) in inflate.c
28 *
29 * 1.2.beta3 22 Dec 2002
30 * - Add comments on state->bits assertion in inffast.c
31 * - Add comments on op field in inftrees.h
32 * - Fix bug in reuse of allocated window after inflateReset()
33 * - Remove bit fields--back to byte structure for speed
34 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38 * - Use local copies of stream next and avail values, as well as local bit
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
40 *
41 * 1.2.beta4 1 Jan 2003
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
44 * - Add comments in inffast.c to introduce the inflate_fast() routine
45 * - Rearrange window copies in inflate_fast() for speed and simplification
46 * - Unroll last copy for window match in inflate_fast()
47 * - Use local copies of window variables in inflate_fast() for speed
48 * - Pull out common wnext == 0 case for speed in inflate_fast()
49 * - Make op and len in inflate_fast() unsigned for consistency
50 * - Add FAR to lcode and dcode declarations in inflate_fast()
51 * - Simplified bad distance check in inflate_fast()
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53 * source file infback.c to provide a call-back interface to inflate for
54 * programs like gzip and unzip -- uses window as output buffer to avoid
55 * window copying
56 *
57 * 1.2.beta5 1 Jan 2003
58 * - Improved inflateBack() interface to allow the caller to provide initial
59 * input in strm.
60 * - Fixed stored blocks bug in inflateBack()
61 *
62 * 1.2.beta6 4 Jan 2003
63 * - Added comments in inffast.c on effectiveness of POSTINC
64 * - Typecasting all around to reduce compiler warnings
65 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66 * make compilers happy
67 * - Changed type of window in inflateBackInit() to unsigned char *
68 *
69 * 1.2.beta7 27 Jan 2003
70 * - Changed many types to unsigned or unsigned short to avoid warnings
71 * - Added inflateCopy() function
72 *
73 * 1.2.0 9 Mar 2003
74 * - Changed inflateBack() interface to provide separate opaque descriptors
75 * for the in() and out() functions
76 * - Changed inflateBack() argument and in_func typedef to swap the length
77 * and buffer address return values for the input function
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
79 *
80 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81 */
82
83 #include "zutil.h"
84 #include "inftrees.h"
85 #include "inflate.h"
86 #include "inffast.h"
87 #include "contrib/arm/chunkcopy.h"
88
89 #ifdef MAKEFIXED
90 # ifndef BUILDFIXED
91 # define BUILDFIXED
92 # endif
93 #endif
94
95 /* function prototypes */
96 local int inflateStateCheck OF((z_streamp strm));
97 local void fixedtables OF((struct inflate_state FAR *state));
98 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
99 unsigned copy));
100 #ifdef BUILDFIXED
101 void makefixed OF((void));
102 #endif
103 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
104 unsigned len));
105
106 local int inflateStateCheck(strm)
107 z_streamp strm;
108 {
109 struct inflate_state FAR *state;
110 if (strm == Z_NULL ||
111 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
112 return 1;
113 state = (struct inflate_state FAR *)strm->state;
114 if (state == Z_NULL || state->strm != strm ||
115 state->mode < HEAD || state->mode > SYNC)
116 return 1;
117 return 0;
118 }
119
120 int ZEXPORT inflateResetKeep(strm)
121 z_streamp strm;
122 {
123 struct inflate_state FAR *state;
124
125 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
126 state = (struct inflate_state FAR *)strm->state;
127 strm->total_in = strm->total_out = state->total = 0;
128 strm->msg = Z_NULL;
129 if (state->wrap) /* to support ill-conceived Java test suite */
130 strm->adler = state->wrap & 1;
131 state->mode = HEAD;
132 state->last = 0;
133 state->havedict = 0;
134 state->dmax = 32768U;
135 state->head = Z_NULL;
136 state->hold = 0;
137 state->bits = 0;
138 state->lencode = state->distcode = state->next = state->codes;
139 state->sane = 1;
140 state->back = -1;
141 Tracev((stderr, "inflate: reset\n"));
142 return Z_OK;
143 }
144
145 int ZEXPORT inflateReset(strm)
146 z_streamp strm;
147 {
148 struct inflate_state FAR *state;
149
150 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
151 state = (struct inflate_state FAR *)strm->state;
152 state->wsize = 0;
153 state->whave = 0;
154 state->wnext = 0;
155 return inflateResetKeep(strm);
156 }
157
158 int ZEXPORT inflateReset2(strm, windowBits)
159 z_streamp strm;
160 int windowBits;
161 {
162 int wrap;
163 struct inflate_state FAR *state;
164
165 /* get the state */
166 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
167 state = (struct inflate_state FAR *)strm->state;
168
169 /* extract wrap request from windowBits parameter */
170 if (windowBits < 0) {
171 wrap = 0;
172 windowBits = -windowBits;
173 }
174 else {
175 wrap = (windowBits >> 4) + 5;
176 #ifdef GUNZIP
177 if (windowBits < 48)
178 windowBits &= 15;
179 #endif
180 }
181
182 /* set number of window bits, free window if different */
183 if (windowBits && (windowBits < 8 || windowBits > 15))
184 return Z_STREAM_ERROR;
185 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
186 ZFREE(strm, state->window);
187 state->window = Z_NULL;
188 }
189
190 /* update state and reset the rest of it */
191 state->wrap = wrap;
192 state->wbits = (unsigned)windowBits;
193 return inflateReset(strm);
194 }
195
196 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
197 z_streamp strm;
198 int windowBits;
199 const char *version;
200 int stream_size;
201 {
202 int ret;
203 struct inflate_state FAR *state;
204
205 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
206 stream_size != (int)(sizeof(z_stream)))
207 return Z_VERSION_ERROR;
208 if (strm == Z_NULL) return Z_STREAM_ERROR;
209 strm->msg = Z_NULL; /* in case we return an error */
210 if (strm->zalloc == (alloc_func)0) {
211 #ifdef Z_SOLO
212 return Z_STREAM_ERROR;
213 #else
214 strm->zalloc = zcalloc;
215 strm->opaque = (voidpf)0;
216 #endif
217 }
218 if (strm->zfree == (free_func)0)
219 #ifdef Z_SOLO
220 return Z_STREAM_ERROR;
221 #else
222 strm->zfree = zcfree;
223 #endif
224 state = (struct inflate_state FAR *)
225 ZALLOC(strm, 1, sizeof(struct inflate_state));
226 if (state == Z_NULL) return Z_MEM_ERROR;
227 Tracev((stderr, "inflate: allocated\n"));
228 strm->state = (struct internal_state FAR *)state;
229 state->strm = strm;
230 state->window = Z_NULL;
231 state->mode = HEAD; /* to pass state test in inflateReset2() */
232 ret = inflateReset2(strm, windowBits);
233 if (ret != Z_OK) {
234 ZFREE(strm, state);
235 strm->state = Z_NULL;
236 }
237 return ret;
238 }
239
240 int ZEXPORT inflateInit_(strm, version, stream_size)
241 z_streamp strm;
242 const char *version;
243 int stream_size;
244 {
245 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
246 }
247
248 int ZEXPORT inflatePrime(strm, bits, value)
249 z_streamp strm;
250 int bits;
251 int value;
252 {
253 struct inflate_state FAR *state;
254
255 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
256 state = (struct inflate_state FAR *)strm->state;
257 if (bits < 0) {
258 state->hold = 0;
259 state->bits = 0;
260 return Z_OK;
261 }
262 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
263 value &= (1L << bits) - 1;
264 state->hold += (unsigned)value << state->bits;
265 state->bits += (uInt)bits;
266 return Z_OK;
267 }
268
269 /*
270 Return state with length and distance decoding tables and index sizes set to
271 fixed code decoding. Normally this returns fixed tables from inffixed.h.
272 If BUILDFIXED is defined, then instead this routine builds the tables the
273 first time it's called, and returns those tables the first time and
274 thereafter. This reduces the size of the code by about 2K bytes, in
275 exchange for a little execution time. However, BUILDFIXED should not be
276 used for threaded applications, since the rewriting of the tables and virgin
277 may not be thread-safe.
278 */
279 local void fixedtables(state)
280 struct inflate_state FAR *state;
281 {
282 #ifdef BUILDFIXED
283 static int virgin = 1;
284 static code *lenfix, *distfix;
285 static code fixed[544];
286
287 /* build fixed huffman tables if first call (may not be thread safe) */
288 if (virgin) {
289 unsigned sym, bits;
290 static code *next;
291
292 /* literal/length table */
293 sym = 0;
294 while (sym < 144) state->lens[sym++] = 8;
295 while (sym < 256) state->lens[sym++] = 9;
296 while (sym < 280) state->lens[sym++] = 7;
297 while (sym < 288) state->lens[sym++] = 8;
298 next = fixed;
299 lenfix = next;
300 bits = 9;
301 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
302
303 /* distance table */
304 sym = 0;
305 while (sym < 32) state->lens[sym++] = 5;
306 distfix = next;
307 bits = 5;
308 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
309
310 /* do this just once */
311 virgin = 0;
312 }
313 #else /* !BUILDFIXED */
314 # include "inffixed.h"
315 #endif /* BUILDFIXED */
316 state->lencode = lenfix;
317 state->lenbits = 9;
318 state->distcode = distfix;
319 state->distbits = 5;
320 }
321
322 #ifdef MAKEFIXED
323 #include <stdio.h>
324
325 /*
326 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
327 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
328 those tables to stdout, which would be piped to inffixed.h. A small program
329 can simply call makefixed to do this:
330
331 void makefixed(void);
332
333 int main(void)
334 {
335 makefixed();
336 return 0;
337 }
338
339 Then that can be linked with zlib built with MAKEFIXED defined and run:
340
341 a.out > inffixed.h
342 */
343 void makefixed()
344 {
345 unsigned low, size;
346 struct inflate_state state;
347
348 fixedtables(&state);
349 puts(" /* inffixed.h -- table for decoding fixed codes");
350 puts(" * Generated automatically by makefixed().");
351 puts(" */");
352 puts("");
353 puts(" /* WARNING: this file should *not* be used by applications.");
354 puts(" It is part of the implementation of this library and is");
355 puts(" subject to change. Applications should only use zlib.h.");
356 puts(" */");
357 puts("");
358 size = 1U << 9;
359 printf(" static const code lenfix[%u] = {", size);
360 low = 0;
361 for (;;) {
362 if ((low % 7) == 0) printf("\n ");
363 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
364 state.lencode[low].bits, state.lencode[low].val);
365 if (++low == size) break;
366 putchar(',');
367 }
368 puts("\n };");
369 size = 1U << 5;
370 printf("\n static const code distfix[%u] = {", size);
371 low = 0;
372 for (;;) {
373 if ((low % 6) == 0) printf("\n ");
374 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
375 state.distcode[low].val);
376 if (++low == size) break;
377 putchar(',');
378 }
379 puts("\n };");
380 }
381 #endif /* MAKEFIXED */
382
383 /*
384 Update the window with the last wsize (normally 32K) bytes written before
385 returning. If window does not exist yet, create it. This is only called
386 when a window is already in use, or when output has been written during this
387 inflate call, but the end of the deflate stream has not been reached yet.
388 It is also called to create a window for dictionary data when a dictionary
389 is loaded.
390
391 Providing output buffers larger than 32K to inflate() should provide a speed
392 advantage, since only the last 32K of output is copied to the sliding window
393 upon return from inflate(), and since all distances after the first 32K of
394 output will fall in the output data, making match copies simpler and faster.
395 The advantage may be dependent on the size of the processor's data caches.
396 */
397 local int updatewindow(strm, end, copy)
398 z_streamp strm;
399 const Bytef *end;
400 unsigned copy;
401 {
402 struct inflate_state FAR *state;
403 unsigned dist;
404
405 state = (struct inflate_state FAR *)strm->state;
406
407 /* if it hasn't been done already, allocate space for the window */
408 if (state->window == Z_NULL) {
409 unsigned wsize = 1U << state->wbits;
410 state->window = (unsigned char FAR *)
411 ZALLOC(strm, wsize + CHUNKCOPY_CHUNK_SIZE,
412 sizeof(unsigned char));
413 if (state->window == Z_NULL) return 1;
414 #ifdef INFLATE_CLEAR_UNUSED_UNDEFINED
415 /* Copies from the overflow portion of this buffer are undefined and
416 may cause analysis tools to raise a warning if we don't initialize
417 it. However, this undefined data overwrites other undefined data
418 and is subsequently either overwritten or left deliberately
419 undefined at the end of decode; so there's really no point.
420 */
421 memset(state->window + wsize, 0, CHUNKCOPY_CHUNK_SIZE);
422 #endif
423 }
424
425 /* if window not in use yet, initialize */
426 if (state->wsize == 0) {
427 state->wsize = 1U << state->wbits;
428 state->wnext = 0;
429 state->whave = 0;
430 }
431
432 /* copy state->wsize or less output bytes into the circular window */
433 if (copy >= state->wsize) {
434 zmemcpy(state->window, end - state->wsize, state->wsize);
435 state->wnext = 0;
436 state->whave = state->wsize;
437 }
438 else {
439 dist = state->wsize - state->wnext;
440 if (dist > copy) dist = copy;
441 zmemcpy(state->window + state->wnext, end - copy, dist);
442 copy -= dist;
443 if (copy) {
444 zmemcpy(state->window, end - copy, copy);
445 state->wnext = copy;
446 state->whave = state->wsize;
447 }
448 else {
449 state->wnext += dist;
450 if (state->wnext == state->wsize) state->wnext = 0;
451 if (state->whave < state->wsize) state->whave += dist;
452 }
453 }
454 return 0;
455 }
456
457 /* Macros for inflate(): */
458
459 /* check function to use adler32() for zlib or crc32() for gzip */
460 #ifdef GUNZIP
461 # define UPDATE(check, buf, len) \
462 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
463 #else
464 # define UPDATE(check, buf, len) adler32(check, buf, len)
465 #endif
466
467 /* check macros for header crc */
468 #ifdef GUNZIP
469 # define CRC2(check, word) \
470 do { \
471 hbuf[0] = (unsigned char)(word); \
472 hbuf[1] = (unsigned char)((word) >> 8); \
473 check = crc32(check, hbuf, 2); \
474 } while (0)
475
476 # define CRC4(check, word) \
477 do { \
478 hbuf[0] = (unsigned char)(word); \
479 hbuf[1] = (unsigned char)((word) >> 8); \
480 hbuf[2] = (unsigned char)((word) >> 16); \
481 hbuf[3] = (unsigned char)((word) >> 24); \
482 check = crc32(check, hbuf, 4); \
483 } while (0)
484 #endif
485
486 /* Load registers with state in inflate() for speed */
487 #define LOAD() \
488 do { \
489 put = strm->next_out; \
490 left = strm->avail_out; \
491 next = strm->next_in; \
492 have = strm->avail_in; \
493 hold = state->hold; \
494 bits = state->bits; \
495 } while (0)
496
497 /* Restore state from registers in inflate() */
498 #define RESTORE() \
499 do { \
500 strm->next_out = put; \
501 strm->avail_out = left; \
502 strm->next_in = next; \
503 strm->avail_in = have; \
504 state->hold = hold; \
505 state->bits = bits; \
506 } while (0)
507
508 /* Clear the input bit accumulator */
509 #define INITBITS() \
510 do { \
511 hold = 0; \
512 bits = 0; \
513 } while (0)
514
515 /* Get a byte of input into the bit accumulator, or return from inflate()
516 if there is no input available. */
517 #define PULLBYTE() \
518 do { \
519 if (have == 0) goto inf_leave; \
520 have--; \
521 hold += (unsigned long)(*next++) << bits; \
522 bits += 8; \
523 } while (0)
524
525 /* Assure that there are at least n bits in the bit accumulator. If there is
526 not enough available input to do that, then return from inflate(). */
527 #define NEEDBITS(n) \
528 do { \
529 while (bits < (unsigned)(n)) \
530 PULLBYTE(); \
531 } while (0)
532
533 /* Return the low n bits of the bit accumulator (n < 16) */
534 #define BITS(n) \
535 ((unsigned)hold & ((1U << (n)) - 1))
536
537 /* Remove n bits from the bit accumulator */
538 #define DROPBITS(n) \
539 do { \
540 hold >>= (n); \
541 bits -= (unsigned)(n); \
542 } while (0)
543
544 /* Remove zero to seven bits as needed to go to a byte boundary */
545 #define BYTEBITS() \
546 do { \
547 hold >>= bits & 7; \
548 bits -= bits & 7; \
549 } while (0)
550
551 /*
552 inflate() uses a state machine to process as much input data and generate as
553 much output data as possible before returning. The state machine is
554 structured roughly as follows:
555
556 for (;;) switch (state) {
557 ...
558 case STATEn:
559 if (not enough input data or output space to make progress)
560 return;
561 ... make progress ...
562 state = STATEm;
563 break;
564 ...
565 }
566
567 so when inflate() is called again, the same case is attempted again, and
568 if the appropriate resources are provided, the machine proceeds to the
569 next state. The NEEDBITS() macro is usually the way the state evaluates
570 whether it can proceed or should return. NEEDBITS() does the return if
571 the requested bits are not available. The typical use of the BITS macros
572 is:
573
574 NEEDBITS(n);
575 ... do something with BITS(n) ...
576 DROPBITS(n);
577
578 where NEEDBITS(n) either returns from inflate() if there isn't enough
579 input left to load n bits into the accumulator, or it continues. BITS(n)
580 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
581 the low n bits off the accumulator. INITBITS() clears the accumulator
582 and sets the number of available bits to zero. BYTEBITS() discards just
583 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
584 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
585
586 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
587 if there is no input available. The decoding of variable length codes uses
588 PULLBYTE() directly in order to pull just enough bytes to decode the next
589 code, and no more.
590
591 Some states loop until they get enough input, making sure that enough
592 state information is maintained to continue the loop where it left off
593 if NEEDBITS() returns in the loop. For example, want, need, and keep
594 would all have to actually be part of the saved state in case NEEDBITS()
595 returns:
596
597 case STATEw:
598 while (want < need) {
599 NEEDBITS(n);
600 keep[want++] = BITS(n);
601 DROPBITS(n);
602 }
603 state = STATEx;
604 case STATEx:
605
606 As shown above, if the next state is also the next case, then the break
607 is omitted.
608
609 A state may also return if there is not enough output space available to
610 complete that state. Those states are copying stored data, writing a
611 literal byte, and copying a matching string.
612
613 When returning, a "goto inf_leave" is used to update the total counters,
614 update the check value, and determine whether any progress has been made
615 during that inflate() call in order to return the proper return code.
616 Progress is defined as a change in either strm->avail_in or strm->avail_out.
617 When there is a window, goto inf_leave will update the window with the last
618 output written. If a goto inf_leave occurs in the middle of decompression
619 and there is no window currently, goto inf_leave will create one and copy
620 output to the window for the next call of inflate().
621
622 In this implementation, the flush parameter of inflate() only affects the
623 return code (per zlib.h). inflate() always writes as much as possible to
624 strm->next_out, given the space available and the provided input--the effect
625 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
626 the allocation of and copying into a sliding window until necessary, which
627 provides the effect documented in zlib.h for Z_FINISH when the entire input
628 stream available. So the only thing the flush parameter actually does is:
629 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
630 will return Z_BUF_ERROR if it has not reached the end of the stream.
631 */
632
633 int ZEXPORT inflate(strm, flush)
634 z_streamp strm;
635 int flush;
636 {
637 struct inflate_state FAR *state;
638 z_const unsigned char FAR *next; /* next input */
639 unsigned char FAR *put; /* next output */
640 unsigned have, left; /* available input and output */
641 unsigned long hold; /* bit buffer */
642 unsigned bits; /* bits in bit buffer */
643 unsigned in, out; /* save starting available input and output */
644 unsigned copy; /* number of stored or match bytes to copy */
645 unsigned char FAR *from; /* where to copy match bytes from */
646 code here; /* current decoding table entry */
647 code last; /* parent table entry */
648 unsigned len; /* length to copy for repeats, bits to drop */
649 int ret; /* return code */
650 #ifdef GUNZIP
651 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
652 #endif
653 static const unsigned short order[19] = /* permutation of code lengths */
654 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
655
656 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
657 (strm->next_in == Z_NULL && strm->avail_in != 0))
658 return Z_STREAM_ERROR;
659
660 state = (struct inflate_state FAR *)strm->state;
661 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
662 LOAD();
663 in = have;
664 out = left;
665 ret = Z_OK;
666 for (;;)
667 switch (state->mode) {
668 case HEAD:
669 if (state->wrap == 0) {
670 state->mode = TYPEDO;
671 break;
672 }
673 NEEDBITS(16);
674 #ifdef GUNZIP
675 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
676 if (state->wbits == 0)
677 state->wbits = 15;
678 state->check = crc32(0L, Z_NULL, 0);
679 CRC2(state->check, hold);
680 INITBITS();
681 state->mode = FLAGS;
682 break;
683 }
684 state->flags = 0; /* expect zlib header */
685 if (state->head != Z_NULL)
686 state->head->done = -1;
687 if (!(state->wrap & 1) || /* check if zlib header allowed */
688 #else
689 if (
690 #endif
691 ((BITS(8) << 8) + (hold >> 8)) % 31) {
692 strm->msg = (char *)"incorrect header check";
693 state->mode = BAD;
694 break;
695 }
696 if (BITS(4) != Z_DEFLATED) {
697 strm->msg = (char *)"unknown compression method";
698 state->mode = BAD;
699 break;
700 }
701 DROPBITS(4);
702 len = BITS(4) + 8;
703 if (state->wbits == 0)
704 state->wbits = len;
705 if (len > 15 || len > state->wbits) {
706 strm->msg = (char *)"invalid window size";
707 state->mode = BAD;
708 break;
709 }
710 state->dmax = 1U << len;
711 Tracev((stderr, "inflate: zlib header ok\n"));
712 strm->adler = state->check = adler32(0L, Z_NULL, 0);
713 state->mode = hold & 0x200 ? DICTID : TYPE;
714 INITBITS();
715 break;
716 #ifdef GUNZIP
717 case FLAGS:
718 NEEDBITS(16);
719 state->flags = (int)(hold);
720 if ((state->flags & 0xff) != Z_DEFLATED) {
721 strm->msg = (char *)"unknown compression method";
722 state->mode = BAD;
723 break;
724 }
725 if (state->flags & 0xe000) {
726 strm->msg = (char *)"unknown header flags set";
727 state->mode = BAD;
728 break;
729 }
730 if (state->head != Z_NULL)
731 state->head->text = (int)((hold >> 8) & 1);
732 if ((state->flags & 0x0200) && (state->wrap & 4))
733 CRC2(state->check, hold);
734 INITBITS();
735 state->mode = TIME;
736 case TIME:
737 NEEDBITS(32);
738 if (state->head != Z_NULL)
739 state->head->time = hold;
740 if ((state->flags & 0x0200) && (state->wrap & 4))
741 CRC4(state->check, hold);
742 INITBITS();
743 state->mode = OS;
744 case OS:
745 NEEDBITS(16);
746 if (state->head != Z_NULL) {
747 state->head->xflags = (int)(hold & 0xff);
748 state->head->os = (int)(hold >> 8);
749 }
750 if ((state->flags & 0x0200) && (state->wrap & 4))
751 CRC2(state->check, hold);
752 INITBITS();
753 state->mode = EXLEN;
754 case EXLEN:
755 if (state->flags & 0x0400) {
756 NEEDBITS(16);
757 state->length = (unsigned)(hold);
758 if (state->head != Z_NULL)
759 state->head->extra_len = (unsigned)hold;
760 if ((state->flags & 0x0200) && (state->wrap & 4))
761 CRC2(state->check, hold);
762 INITBITS();
763 }
764 else if (state->head != Z_NULL)
765 state->head->extra = Z_NULL;
766 state->mode = EXTRA;
767 case EXTRA:
768 if (state->flags & 0x0400) {
769 copy = state->length;
770 if (copy > have) copy = have;
771 if (copy) {
772 if (state->head != Z_NULL &&
773 state->head->extra != Z_NULL) {
774 len = state->head->extra_len - state->length;
775 zmemcpy(state->head->extra + len, next,
776 len + copy > state->head->extra_max ?
777 state->head->extra_max - len : copy);
778 }
779 if ((state->flags & 0x0200) && (state->wrap & 4))
780 state->check = crc32(state->check, next, copy);
781 have -= copy;
782 next += copy;
783 state->length -= copy;
784 }
785 if (state->length) goto inf_leave;
786 }
787 state->length = 0;
788 state->mode = NAME;
789 case NAME:
790 if (state->flags & 0x0800) {
791 if (have == 0) goto inf_leave;
792 copy = 0;
793 do {
794 len = (unsigned)(next[copy++]);
795 if (state->head != Z_NULL &&
796 state->head->name != Z_NULL &&
797 state->length < state->head->name_max)
798 state->head->name[state->length++] = (Bytef)len;
799 } while (len && copy < have);
800 if ((state->flags & 0x0200) && (state->wrap & 4))
801 state->check = crc32(state->check, next, copy);
802 have -= copy;
803 next += copy;
804 if (len) goto inf_leave;
805 }
806 else if (state->head != Z_NULL)
807 state->head->name = Z_NULL;
808 state->length = 0;
809 state->mode = COMMENT;
810 case COMMENT:
811 if (state->flags & 0x1000) {
812 if (have == 0) goto inf_leave;
813 copy = 0;
814 do {
815 len = (unsigned)(next[copy++]);
816 if (state->head != Z_NULL &&
817 state->head->comment != Z_NULL &&
818 state->length < state->head->comm_max)
819 state->head->comment[state->length++] = (Bytef)len;
820 } while (len && copy < have);
821 if ((state->flags & 0x0200) && (state->wrap & 4))
822 state->check = crc32(state->check, next, copy);
823 have -= copy;
824 next += copy;
825 if (len) goto inf_leave;
826 }
827 else if (state->head != Z_NULL)
828 state->head->comment = Z_NULL;
829 state->mode = HCRC;
830 case HCRC:
831 if (state->flags & 0x0200) {
832 NEEDBITS(16);
833 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
834 strm->msg = (char *)"header crc mismatch";
835 state->mode = BAD;
836 break;
837 }
838 INITBITS();
839 }
840 if (state->head != Z_NULL) {
841 state->head->hcrc = (int)((state->flags >> 9) & 1);
842 state->head->done = 1;
843 }
844 strm->adler = state->check = crc32(0L, Z_NULL, 0);
845 state->mode = TYPE;
846 break;
847 #endif
848 case DICTID:
849 NEEDBITS(32);
850 strm->adler = state->check = ZSWAP32(hold);
851 INITBITS();
852 state->mode = DICT;
853 case DICT:
854 if (state->havedict == 0) {
855 RESTORE();
856 return Z_NEED_DICT;
857 }
858 strm->adler = state->check = adler32(0L, Z_NULL, 0);
859 state->mode = TYPE;
860 case TYPE:
861 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
862 case TYPEDO:
863 if (state->last) {
864 BYTEBITS();
865 state->mode = CHECK;
866 break;
867 }
868 NEEDBITS(3);
869 state->last = BITS(1);
870 DROPBITS(1);
871 switch (BITS(2)) {
872 case 0: /* stored block */
873 Tracev((stderr, "inflate: stored block%s\n",
874 state->last ? " (last)" : ""));
875 state->mode = STORED;
876 break;
877 case 1: /* fixed block */
878 fixedtables(state);
879 Tracev((stderr, "inflate: fixed codes block%s\n",
880 state->last ? " (last)" : ""));
881 state->mode = LEN_; /* decode codes */
882 if (flush == Z_TREES) {
883 DROPBITS(2);
884 goto inf_leave;
885 }
886 break;
887 case 2: /* dynamic block */
888 Tracev((stderr, "inflate: dynamic codes block%s\n",
889 state->last ? " (last)" : ""));
890 state->mode = TABLE;
891 break;
892 case 3:
893 strm->msg = (char *)"invalid block type";
894 state->mode = BAD;
895 }
896 DROPBITS(2);
897 break;
898 case STORED:
899 BYTEBITS(); /* go to byte boundary */
900 NEEDBITS(32);
901 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
902 strm->msg = (char *)"invalid stored block lengths";
903 state->mode = BAD;
904 break;
905 }
906 state->length = (unsigned)hold & 0xffff;
907 Tracev((stderr, "inflate: stored length %u\n",
908 state->length));
909 INITBITS();
910 state->mode = COPY_;
911 if (flush == Z_TREES) goto inf_leave;
912 case COPY_:
913 state->mode = COPY;
914 case COPY:
915 copy = state->length;
916 if (copy) {
917 if (copy > have) copy = have;
918 if (copy > left) copy = left;
919 if (copy == 0) goto inf_leave;
920 zmemcpy(put, next, copy);
921 have -= copy;
922 next += copy;
923 left -= copy;
924 put += copy;
925 state->length -= copy;
926 break;
927 }
928 Tracev((stderr, "inflate: stored end\n"));
929 state->mode = TYPE;
930 break;
931 case TABLE:
932 NEEDBITS(14);
933 state->nlen = BITS(5) + 257;
934 DROPBITS(5);
935 state->ndist = BITS(5) + 1;
936 DROPBITS(5);
937 state->ncode = BITS(4) + 4;
938 DROPBITS(4);
939 #ifndef PKZIP_BUG_WORKAROUND
940 if (state->nlen > 286 || state->ndist > 30) {
941 strm->msg = (char *)"too many length or distance symbols";
942 state->mode = BAD;
943 break;
944 }
945 #endif
946 Tracev((stderr, "inflate: table sizes ok\n"));
947 state->have = 0;
948 state->mode = LENLENS;
949 case LENLENS:
950 while (state->have < state->ncode) {
951 NEEDBITS(3);
952 state->lens[order[state->have++]] = (unsigned short)BITS(3);
953 DROPBITS(3);
954 }
955 while (state->have < 19)
956 state->lens[order[state->have++]] = 0;
957 state->next = state->codes;
958 state->lencode = (const code FAR *)(state->next);
959 state->lenbits = 7;
960 ret = inflate_table(CODES, state->lens, 19, &(state->next),
961 &(state->lenbits), state->work);
962 if (ret) {
963 strm->msg = (char *)"invalid code lengths set";
964 state->mode = BAD;
965 break;
966 }
967 Tracev((stderr, "inflate: code lengths ok\n"));
968 state->have = 0;
969 state->mode = CODELENS;
970 case CODELENS:
971 while (state->have < state->nlen + state->ndist) {
972 for (;;) {
973 here = state->lencode[BITS(state->lenbits)];
974 if ((unsigned)(here.bits) <= bits) break;
975 PULLBYTE();
976 }
977 if (here.val < 16) {
978 DROPBITS(here.bits);
979 state->lens[state->have++] = here.val;
980 }
981 else {
982 if (here.val == 16) {
983 NEEDBITS(here.bits + 2);
984 DROPBITS(here.bits);
985 if (state->have == 0) {
986 strm->msg = (char *)"invalid bit length repeat";
987 state->mode = BAD;
988 break;
989 }
990 len = state->lens[state->have - 1];
991 copy = 3 + BITS(2);
992 DROPBITS(2);
993 }
994 else if (here.val == 17) {
995 NEEDBITS(here.bits + 3);
996 DROPBITS(here.bits);
997 len = 0;
998 copy = 3 + BITS(3);
999 DROPBITS(3);
1000 }
1001 else {
1002 NEEDBITS(here.bits + 7);
1003 DROPBITS(here.bits);
1004 len = 0;
1005 copy = 11 + BITS(7);
1006 DROPBITS(7);
1007 }
1008 if (state->have + copy > state->nlen + state->ndist) {
1009 strm->msg = (char *)"invalid bit length repeat";
1010 state->mode = BAD;
1011 break;
1012 }
1013 while (copy--)
1014 state->lens[state->have++] = (unsigned short)len;
1015 }
1016 }
1017
1018 /* handle error breaks in while */
1019 if (state->mode == BAD) break;
1020
1021 /* check for end-of-block code (better have one) */
1022 if (state->lens[256] == 0) {
1023 strm->msg = (char *)"invalid code -- missing end-of-block";
1024 state->mode = BAD;
1025 break;
1026 }
1027
1028 /* build code tables -- note: do not change the lenbits or distbits
1029 values here (9 and 6) without reading the comments in inftrees.h
1030 concerning the ENOUGH constants, which depend on those values */
1031 state->next = state->codes;
1032 state->lencode = (const code FAR *)(state->next);
1033 state->lenbits = 9;
1034 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1035 &(state->lenbits), state->work);
1036 if (ret) {
1037 strm->msg = (char *)"invalid literal/lengths set";
1038 state->mode = BAD;
1039 break;
1040 }
1041 state->distcode = (const code FAR *)(state->next);
1042 state->distbits = 6;
1043 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1044 &(state->next), &(state->distbits), state->work);
1045 if (ret) {
1046 strm->msg = (char *)"invalid distances set";
1047 state->mode = BAD;
1048 break;
1049 }
1050 Tracev((stderr, "inflate: codes ok\n"));
1051 state->mode = LEN_;
1052 if (flush == Z_TREES) goto inf_leave;
1053 case LEN_:
1054 state->mode = LEN;
1055 case LEN:
1056 if (have >= 6 && left >= 258) {
1057 RESTORE();
1058 inflate_fast(strm, out);
1059 LOAD();
1060 if (state->mode == TYPE)
1061 state->back = -1;
1062 break;
1063 }
1064 state->back = 0;
1065 for (;;) {
1066 here = state->lencode[BITS(state->lenbits)];
1067 if ((unsigned)(here.bits) <= bits) break;
1068 PULLBYTE();
1069 }
1070 if (here.op && (here.op & 0xf0) == 0) {
1071 last = here;
1072 for (;;) {
1073 here = state->lencode[last.val +
1074 (BITS(last.bits + last.op) >> last.bits)];
1075 if ((unsigned)(last.bits + here.bits) <= bits) break;
1076 PULLBYTE();
1077 }
1078 DROPBITS(last.bits);
1079 state->back += last.bits;
1080 }
1081 DROPBITS(here.bits);
1082 state->back += here.bits;
1083 state->length = (unsigned)here.val;
1084 if ((int)(here.op) == 0) {
1085 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1086 "inflate: literal '%c'\n" :
1087 "inflate: literal 0x%02x\n", here.val));
1088 state->mode = LIT;
1089 break;
1090 }
1091 if (here.op & 32) {
1092 Tracevv((stderr, "inflate: end of block\n"));
1093 state->back = -1;
1094 state->mode = TYPE;
1095 break;
1096 }
1097 if (here.op & 64) {
1098 strm->msg = (char *)"invalid literal/length code";
1099 state->mode = BAD;
1100 break;
1101 }
1102 state->extra = (unsigned)(here.op) & 15;
1103 state->mode = LENEXT;
1104 case LENEXT:
1105 if (state->extra) {
1106 NEEDBITS(state->extra);
1107 state->length += BITS(state->extra);
1108 DROPBITS(state->extra);
1109 state->back += state->extra;
1110 }
1111 Tracevv((stderr, "inflate: length %u\n", state->length));
1112 state->was = state->length;
1113 state->mode = DIST;
1114 case DIST:
1115 for (;;) {
1116 here = state->distcode[BITS(state->distbits)];
1117 if ((unsigned)(here.bits) <= bits) break;
1118 PULLBYTE();
1119 }
1120 if ((here.op & 0xf0) == 0) {
1121 last = here;
1122 for (;;) {
1123 here = state->distcode[last.val +
1124 (BITS(last.bits + last.op) >> last.bits)];
1125 if ((unsigned)(last.bits + here.bits) <= bits) break;
1126 PULLBYTE();
1127 }
1128 DROPBITS(last.bits);
1129 state->back += last.bits;
1130 }
1131 DROPBITS(here.bits);
1132 state->back += here.bits;
1133 if (here.op & 64) {
1134 strm->msg = (char *)"invalid distance code";
1135 state->mode = BAD;
1136 break;
1137 }
1138 state->offset = (unsigned)here.val;
1139 state->extra = (unsigned)(here.op) & 15;
1140 state->mode = DISTEXT;
1141 case DISTEXT:
1142 if (state->extra) {
1143 NEEDBITS(state->extra);
1144 state->offset += BITS(state->extra);
1145 DROPBITS(state->extra);
1146 state->back += state->extra;
1147 }
1148 #ifdef INFLATE_STRICT
1149 if (state->offset > state->dmax) {
1150 strm->msg = (char *)"invalid distance too far back";
1151 state->mode = BAD;
1152 break;
1153 }
1154 #endif
1155 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1156 state->mode = MATCH;
1157 case MATCH:
1158 if (left == 0) goto inf_leave;
1159 copy = out - left;
1160 if (state->offset > copy) { /* copy from window */
1161 copy = state->offset - copy;
1162 if (copy > state->whave) {
1163 if (state->sane) {
1164 strm->msg = (char *)"invalid distance too far back";
1165 state->mode = BAD;
1166 break;
1167 }
1168 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1169 Trace((stderr, "inflate.c too far\n"));
1170 copy -= state->whave;
1171 if (copy > state->length) copy = state->length;
1172 if (copy > left) copy = left;
1173 left -= copy;
1174 state->length -= copy;
1175 do {
1176 *put++ = 0;
1177 } while (--copy);
1178 if (state->length == 0) state->mode = LEN;
1179 break;
1180 #endif
1181 }
1182 if (copy > state->wnext) {
1183 copy -= state->wnext;
1184 from = state->window + (state->wsize - copy);
1185 }
1186 else
1187 from = state->window + (state->wnext - copy);
1188 if (copy > state->length) copy = state->length;
1189 if (copy > left) copy = left;
1190 put = chunkcopy_safe(put, from, copy, put + left);
1191 }
1192 else { /* copy from output */
1193 copy = state->length;
1194 if (copy > left) copy = left;
1195 put = chunkcopy_lapped_safe(put, state->offset, copy, put + left );
1196 }
1197 left -= copy;
1198 state->length -= copy;
1199 if (state->length == 0) state->mode = LEN;
1200 break;
1201 case LIT:
1202 if (left == 0) goto inf_leave;
1203 *put++ = (unsigned char)(state->length);
1204 left--;
1205 state->mode = LEN;
1206 break;
1207 case CHECK:
1208 if (state->wrap) {
1209 NEEDBITS(32);
1210 out -= left;
1211 strm->total_out += out;
1212 state->total += out;
1213 if ((state->wrap & 4) && out)
1214 strm->adler = state->check =
1215 UPDATE(state->check, put - out, out);
1216 out = left;
1217 if ((state->wrap & 4) && (
1218 #ifdef GUNZIP
1219 state->flags ? hold :
1220 #endif
1221 ZSWAP32(hold)) != state->check) {
1222 strm->msg = (char *)"incorrect data check";
1223 state->mode = BAD;
1224 break;
1225 }
1226 INITBITS();
1227 Tracev((stderr, "inflate: check matches trailer\n"));
1228 }
1229 #ifdef GUNZIP
1230 state->mode = LENGTH;
1231 case LENGTH:
1232 if (state->wrap && state->flags) {
1233 NEEDBITS(32);
1234 if (hold != (state->total & 0xffffffffUL)) {
1235 strm->msg = (char *)"incorrect length check";
1236 state->mode = BAD;
1237 break;
1238 }
1239 INITBITS();
1240 Tracev((stderr, "inflate: length matches trailer\n"));
1241 }
1242 #endif
1243 state->mode = DONE;
1244 case DONE:
1245 ret = Z_STREAM_END;
1246 goto inf_leave;
1247 case BAD:
1248 ret = Z_DATA_ERROR;
1249 goto inf_leave;
1250 case MEM:
1251 return Z_MEM_ERROR;
1252 case SYNC:
1253 default:
1254 return Z_STREAM_ERROR;
1255 }
1256
1257 /*
1258 Return from inflate(), updating the total counts and the check value.
1259 If there was no progress during the inflate() call, return a buffer
1260 error. Call updatewindow() to create and/or update the window state.
1261 Note: a memory error from inflate() is non-recoverable.
1262 */
1263 inf_leave:
1264 RESTORE();
1265 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1266 (state->mode < CHECK || flush != Z_FINISH)))
1267 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1268 state->mode = MEM;
1269 return Z_MEM_ERROR;
1270 }
1271 in -= strm->avail_in;
1272 out -= strm->avail_out;
1273 strm->total_in += in;
1274 strm->total_out += out;
1275 state->total += out;
1276 if ((state->wrap & 4) && out)
1277 strm->adler = state->check =
1278 UPDATE(state->check, strm->next_out - out, out);
1279 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1280 (state->mode == TYPE ? 128 : 0) +
1281 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1282 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1283 ret = Z_BUF_ERROR;
1284 return ret;
1285 }
1286
1287 int ZEXPORT inflateEnd(strm)
1288 z_streamp strm;
1289 {
1290 struct inflate_state FAR *state;
1291 if (inflateStateCheck(strm))
1292 return Z_STREAM_ERROR;
1293 state = (struct inflate_state FAR *)strm->state;
1294 if (state->window != Z_NULL) ZFREE(strm, state->window);
1295 ZFREE(strm, strm->state);
1296 strm->state = Z_NULL;
1297 Tracev((stderr, "inflate: end\n"));
1298 return Z_OK;
1299 }
1300
1301 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1302 z_streamp strm;
1303 Bytef *dictionary;
1304 uInt *dictLength;
1305 {
1306 struct inflate_state FAR *state;
1307
1308 /* check state */
1309 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1310 state = (struct inflate_state FAR *)strm->state;
1311
1312 /* copy dictionary */
1313 if (state->whave && dictionary != Z_NULL) {
1314 zmemcpy(dictionary, state->window + state->wnext,
1315 state->whave - state->wnext);
1316 zmemcpy(dictionary + state->whave - state->wnext,
1317 state->window, state->wnext);
1318 }
1319 if (dictLength != Z_NULL)
1320 *dictLength = state->whave;
1321 return Z_OK;
1322 }
1323
1324 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1325 z_streamp strm;
1326 const Bytef *dictionary;
1327 uInt dictLength;
1328 {
1329 struct inflate_state FAR *state;
1330 unsigned long dictid;
1331 int ret;
1332
1333 /* check state */
1334 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1335 state = (struct inflate_state FAR *)strm->state;
1336 if (state->wrap != 0 && state->mode != DICT)
1337 return Z_STREAM_ERROR;
1338
1339 /* check for correct dictionary identifier */
1340 if (state->mode == DICT) {
1341 dictid = adler32(0L, Z_NULL, 0);
1342 dictid = adler32(dictid, dictionary, dictLength);
1343 if (dictid != state->check)
1344 return Z_DATA_ERROR;
1345 }
1346
1347 /* copy dictionary to window using updatewindow(), which will amend the
1348 existing dictionary if appropriate */
1349 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1350 if (ret) {
1351 state->mode = MEM;
1352 return Z_MEM_ERROR;
1353 }
1354 state->havedict = 1;
1355 Tracev((stderr, "inflate: dictionary set\n"));
1356 return Z_OK;
1357 }
1358
1359 int ZEXPORT inflateGetHeader(strm, head)
1360 z_streamp strm;
1361 gz_headerp head;
1362 {
1363 struct inflate_state FAR *state;
1364
1365 /* check state */
1366 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1367 state = (struct inflate_state FAR *)strm->state;
1368 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1369
1370 /* save header structure */
1371 state->head = head;
1372 head->done = 0;
1373 return Z_OK;
1374 }
1375
1376 /*
1377 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1378 or when out of input. When called, *have is the number of pattern bytes
1379 found in order so far, in 0..3. On return *have is updated to the new
1380 state. If on return *have equals four, then the pattern was found and the
1381 return value is how many bytes were read including the last byte of the
1382 pattern. If *have is less than four, then the pattern has not been found
1383 yet and the return value is len. In the latter case, syncsearch() can be
1384 called again with more data and the *have state. *have is initialized to
1385 zero for the first call.
1386 */
1387 local unsigned syncsearch(have, buf, len)
1388 unsigned FAR *have;
1389 const unsigned char FAR *buf;
1390 unsigned len;
1391 {
1392 unsigned got;
1393 unsigned next;
1394
1395 got = *have;
1396 next = 0;
1397 while (next < len && got < 4) {
1398 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1399 got++;
1400 else if (buf[next])
1401 got = 0;
1402 else
1403 got = 4 - got;
1404 next++;
1405 }
1406 *have = got;
1407 return next;
1408 }
1409
1410 int ZEXPORT inflateSync(strm)
1411 z_streamp strm;
1412 {
1413 unsigned len; /* number of bytes to look at or looked at */
1414 unsigned long in, out; /* temporary to save total_in and total_out */
1415 unsigned char buf[4]; /* to restore bit buffer to byte string */
1416 struct inflate_state FAR *state;
1417
1418 /* check parameters */
1419 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1420 state = (struct inflate_state FAR *)strm->state;
1421 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1422
1423 /* if first time, start search in bit buffer */
1424 if (state->mode != SYNC) {
1425 state->mode = SYNC;
1426 state->hold <<= state->bits & 7;
1427 state->bits -= state->bits & 7;
1428 len = 0;
1429 while (state->bits >= 8) {
1430 buf[len++] = (unsigned char)(state->hold);
1431 state->hold >>= 8;
1432 state->bits -= 8;
1433 }
1434 state->have = 0;
1435 syncsearch(&(state->have), buf, len);
1436 }
1437
1438 /* search available input */
1439 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1440 strm->avail_in -= len;
1441 strm->next_in += len;
1442 strm->total_in += len;
1443
1444 /* return no joy or set up to restart inflate() on a new block */
1445 if (state->have != 4) return Z_DATA_ERROR;
1446 in = strm->total_in; out = strm->total_out;
1447 inflateReset(strm);
1448 strm->total_in = in; strm->total_out = out;
1449 state->mode = TYPE;
1450 return Z_OK;
1451 }
1452
1453 /*
1454 Returns true if inflate is currently at the end of a block generated by
1455 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1456 implementation to provide an additional safety check. PPP uses
1457 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1458 block. When decompressing, PPP checks that at the end of input packet,
1459 inflate is waiting for these length bytes.
1460 */
1461 int ZEXPORT inflateSyncPoint(strm)
1462 z_streamp strm;
1463 {
1464 struct inflate_state FAR *state;
1465
1466 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1467 state = (struct inflate_state FAR *)strm->state;
1468 return state->mode == STORED && state->bits == 0;
1469 }
1470
1471 int ZEXPORT inflateCopy(dest, source)
1472 z_streamp dest;
1473 z_streamp source;
1474 {
1475 struct inflate_state FAR *state;
1476 struct inflate_state FAR *copy;
1477 unsigned char FAR *window;
1478 unsigned wsize;
1479
1480 /* check input */
1481 if (inflateStateCheck(source) || dest == Z_NULL)
1482 return Z_STREAM_ERROR;
1483 state = (struct inflate_state FAR *)source->state;
1484
1485 /* allocate space */
1486 copy = (struct inflate_state FAR *)
1487 ZALLOC(source, 1, sizeof(struct inflate_state));
1488 if (copy == Z_NULL) return Z_MEM_ERROR;
1489 window = Z_NULL;
1490 if (state->window != Z_NULL) {
1491 window = (unsigned char FAR *)
1492 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1493 if (window == Z_NULL) {
1494 ZFREE(source, copy);
1495 return Z_MEM_ERROR;
1496 }
1497 }
1498
1499 /* copy state */
1500 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1501 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1502 copy->strm = dest;
1503 if (state->lencode >= state->codes &&
1504 state->lencode <= state->codes + ENOUGH - 1) {
1505 copy->lencode = copy->codes + (state->lencode - state->codes);
1506 copy->distcode = copy->codes + (state->distcode - state->codes);
1507 }
1508 copy->next = copy->codes + (state->next - state->codes);
1509 if (window != Z_NULL) {
1510 wsize = 1U << state->wbits;
1511 zmemcpy(window, state->window, wsize);
1512 }
1513 copy->window = window;
1514 dest->state = (struct internal_state FAR *)copy;
1515 return Z_OK;
1516 }
1517
1518 int ZEXPORT inflateUndermine(strm, subvert)
1519 z_streamp strm;
1520 int subvert;
1521 {
1522 struct inflate_state FAR *state;
1523
1524 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1525 state = (struct inflate_state FAR *)strm->state;
1526 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1527 state->sane = !subvert;
1528 return Z_OK;
1529 #else
1530 (void)subvert;
1531 state->sane = 1;
1532 return Z_DATA_ERROR;
1533 #endif
1534 }
1535
1536 int ZEXPORT inflateValidate(strm, check)
1537 z_streamp strm;
1538 int check;
1539 {
1540 struct inflate_state FAR *state;
1541
1542 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1543 state = (struct inflate_state FAR *)strm->state;
1544 if (check)
1545 state->wrap |= 4;
1546 else
1547 state->wrap &= ~4;
1548 return Z_OK;
1549 }
1550
1551 long ZEXPORT inflateMark(strm)
1552 z_streamp strm;
1553 {
1554 struct inflate_state FAR *state;
1555
1556 if (inflateStateCheck(strm))
1557 return -(1L << 16);
1558 state = (struct inflate_state FAR *)strm->state;
1559 return (long)(((unsigned long)((long)state->back)) << 16) +
1560 (state->mode == COPY ? state->length :
1561 (state->mode == MATCH ? state->was - state->length : 0));
1562 }
1563
1564 unsigned long ZEXPORT inflateCodesUsed(strm)
1565 z_streamp strm;
1566 {
1567 struct inflate_state FAR *state;
1568 if (inflateStateCheck(strm)) return (unsigned long)-1;
1569 state = (struct inflate_state FAR *)strm->state;
1570 return (unsigned long)(state->next - state->codes);
1571 }
OLDNEW
« no previous file with comments | « third_party/zlib/contrib/arm/inffast.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698