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