| 1 | n/a | /* infback.c -- inflate using a call-back interface | 
|---|
| 2 | n/a | * Copyright (C) 1995-2016 Mark Adler | 
|---|
| 3 | n/a | * For conditions of distribution and use, see copyright notice in zlib.h | 
|---|
| 4 | n/a | */ | 
|---|
| 5 | n/a |  | 
|---|
| 6 | n/a | /* | 
|---|
| 7 | n/a | This code is largely copied from inflate.c.  Normally either infback.o or | 
|---|
| 8 | n/a | inflate.o would be linked into an application--not both.  The interface | 
|---|
| 9 | n/a | with inffast.c is retained so that optimized assembler-coded versions of | 
|---|
| 10 | n/a | inflate_fast() can be used with either inflate.c or infback.c. | 
|---|
| 11 | n/a | */ | 
|---|
| 12 | n/a |  | 
|---|
| 13 | n/a | #include "zutil.h" | 
|---|
| 14 | n/a | #include "inftrees.h" | 
|---|
| 15 | n/a | #include "inflate.h" | 
|---|
| 16 | n/a | #include "inffast.h" | 
|---|
| 17 | n/a |  | 
|---|
| 18 | n/a | /* function prototypes */ | 
|---|
| 19 | n/a | local void fixedtables OF((struct inflate_state FAR *state)); | 
|---|
| 20 | n/a |  | 
|---|
| 21 | n/a | /* | 
|---|
| 22 | n/a | strm provides memory allocation functions in zalloc and zfree, or | 
|---|
| 23 | n/a | Z_NULL to use the library memory allocation functions. | 
|---|
| 24 | n/a |  | 
|---|
| 25 | n/a | windowBits is in the range 8..15, and window is a user-supplied | 
|---|
| 26 | n/a | window and output buffer that is 2**windowBits bytes. | 
|---|
| 27 | n/a | */ | 
|---|
| 28 | n/a | int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) | 
|---|
| 29 | n/a | z_streamp strm; | 
|---|
| 30 | n/a | int windowBits; | 
|---|
| 31 | n/a | unsigned char FAR *window; | 
|---|
| 32 | n/a | const char *version; | 
|---|
| 33 | n/a | int stream_size; | 
|---|
| 34 | n/a | { | 
|---|
| 35 | n/a | struct inflate_state FAR *state; | 
|---|
| 36 | n/a |  | 
|---|
| 37 | n/a | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || | 
|---|
| 38 | n/a | stream_size != (int)(sizeof(z_stream))) | 
|---|
| 39 | n/a | return Z_VERSION_ERROR; | 
|---|
| 40 | n/a | if (strm == Z_NULL || window == Z_NULL || | 
|---|
| 41 | n/a | windowBits < 8 || windowBits > 15) | 
|---|
| 42 | n/a | return Z_STREAM_ERROR; | 
|---|
| 43 | n/a | strm->msg = Z_NULL;                 /* in case we return an error */ | 
|---|
| 44 | n/a | if (strm->zalloc == (alloc_func)0) { | 
|---|
| 45 | n/a | #ifdef Z_SOLO | 
|---|
| 46 | n/a | return Z_STREAM_ERROR; | 
|---|
| 47 | n/a | #else | 
|---|
| 48 | n/a | strm->zalloc = zcalloc; | 
|---|
| 49 | n/a | strm->opaque = (voidpf)0; | 
|---|
| 50 | n/a | #endif | 
|---|
| 51 | n/a | } | 
|---|
| 52 | n/a | if (strm->zfree == (free_func)0) | 
|---|
| 53 | n/a | #ifdef Z_SOLO | 
|---|
| 54 | n/a | return Z_STREAM_ERROR; | 
|---|
| 55 | n/a | #else | 
|---|
| 56 | n/a | strm->zfree = zcfree; | 
|---|
| 57 | n/a | #endif | 
|---|
| 58 | n/a | state = (struct inflate_state FAR *)ZALLOC(strm, 1, | 
|---|
| 59 | n/a | sizeof(struct inflate_state)); | 
|---|
| 60 | n/a | if (state == Z_NULL) return Z_MEM_ERROR; | 
|---|
| 61 | n/a | Tracev((stderr, "inflate: allocated\n")); | 
|---|
| 62 | n/a | strm->state = (struct internal_state FAR *)state; | 
|---|
| 63 | n/a | state->dmax = 32768U; | 
|---|
| 64 | n/a | state->wbits = (uInt)windowBits; | 
|---|
| 65 | n/a | state->wsize = 1U << windowBits; | 
|---|
| 66 | n/a | state->window = window; | 
|---|
| 67 | n/a | state->wnext = 0; | 
|---|
| 68 | n/a | state->whave = 0; | 
|---|
| 69 | n/a | return Z_OK; | 
|---|
| 70 | n/a | } | 
|---|
| 71 | n/a |  | 
|---|
| 72 | n/a | /* | 
|---|
| 73 | n/a | Return state with length and distance decoding tables and index sizes set to | 
|---|
| 74 | n/a | fixed code decoding.  Normally this returns fixed tables from inffixed.h. | 
|---|
| 75 | n/a | If BUILDFIXED is defined, then instead this routine builds the tables the | 
|---|
| 76 | n/a | first time it's called, and returns those tables the first time and | 
|---|
| 77 | n/a | thereafter.  This reduces the size of the code by about 2K bytes, in | 
|---|
| 78 | n/a | exchange for a little execution time.  However, BUILDFIXED should not be | 
|---|
| 79 | n/a | used for threaded applications, since the rewriting of the tables and virgin | 
|---|
| 80 | n/a | may not be thread-safe. | 
|---|
| 81 | n/a | */ | 
|---|
| 82 | n/a | local void fixedtables(state) | 
|---|
| 83 | n/a | struct inflate_state FAR *state; | 
|---|
| 84 | n/a | { | 
|---|
| 85 | n/a | #ifdef BUILDFIXED | 
|---|
| 86 | n/a | static int virgin = 1; | 
|---|
| 87 | n/a | static code *lenfix, *distfix; | 
|---|
| 88 | n/a | static code fixed[544]; | 
|---|
| 89 | n/a |  | 
|---|
| 90 | n/a | /* build fixed huffman tables if first call (may not be thread safe) */ | 
|---|
| 91 | n/a | if (virgin) { | 
|---|
| 92 | n/a | unsigned sym, bits; | 
|---|
| 93 | n/a | static code *next; | 
|---|
| 94 | n/a |  | 
|---|
| 95 | n/a | /* literal/length table */ | 
|---|
| 96 | n/a | sym = 0; | 
|---|
| 97 | n/a | while (sym < 144) state->lens[sym++] = 8; | 
|---|
| 98 | n/a | while (sym < 256) state->lens[sym++] = 9; | 
|---|
| 99 | n/a | while (sym < 280) state->lens[sym++] = 7; | 
|---|
| 100 | n/a | while (sym < 288) state->lens[sym++] = 8; | 
|---|
| 101 | n/a | next = fixed; | 
|---|
| 102 | n/a | lenfix = next; | 
|---|
| 103 | n/a | bits = 9; | 
|---|
| 104 | n/a | inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); | 
|---|
| 105 | n/a |  | 
|---|
| 106 | n/a | /* distance table */ | 
|---|
| 107 | n/a | sym = 0; | 
|---|
| 108 | n/a | while (sym < 32) state->lens[sym++] = 5; | 
|---|
| 109 | n/a | distfix = next; | 
|---|
| 110 | n/a | bits = 5; | 
|---|
| 111 | n/a | inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); | 
|---|
| 112 | n/a |  | 
|---|
| 113 | n/a | /* do this just once */ | 
|---|
| 114 | n/a | virgin = 0; | 
|---|
| 115 | n/a | } | 
|---|
| 116 | n/a | #else /* !BUILDFIXED */ | 
|---|
| 117 | n/a | #   include "inffixed.h" | 
|---|
| 118 | n/a | #endif /* BUILDFIXED */ | 
|---|
| 119 | n/a | state->lencode = lenfix; | 
|---|
| 120 | n/a | state->lenbits = 9; | 
|---|
| 121 | n/a | state->distcode = distfix; | 
|---|
| 122 | n/a | state->distbits = 5; | 
|---|
| 123 | n/a | } | 
|---|
| 124 | n/a |  | 
|---|
| 125 | n/a | /* Macros for inflateBack(): */ | 
|---|
| 126 | n/a |  | 
|---|
| 127 | n/a | /* Load returned state from inflate_fast() */ | 
|---|
| 128 | n/a | #define LOAD() \ | 
|---|
| 129 | n/a | do { \ | 
|---|
| 130 | n/a | put = strm->next_out; \ | 
|---|
| 131 | n/a | left = strm->avail_out; \ | 
|---|
| 132 | n/a | next = strm->next_in; \ | 
|---|
| 133 | n/a | have = strm->avail_in; \ | 
|---|
| 134 | n/a | hold = state->hold; \ | 
|---|
| 135 | n/a | bits = state->bits; \ | 
|---|
| 136 | n/a | } while (0) | 
|---|
| 137 | n/a |  | 
|---|
| 138 | n/a | /* Set state from registers for inflate_fast() */ | 
|---|
| 139 | n/a | #define RESTORE() \ | 
|---|
| 140 | n/a | do { \ | 
|---|
| 141 | n/a | strm->next_out = put; \ | 
|---|
| 142 | n/a | strm->avail_out = left; \ | 
|---|
| 143 | n/a | strm->next_in = next; \ | 
|---|
| 144 | n/a | strm->avail_in = have; \ | 
|---|
| 145 | n/a | state->hold = hold; \ | 
|---|
| 146 | n/a | state->bits = bits; \ | 
|---|
| 147 | n/a | } while (0) | 
|---|
| 148 | n/a |  | 
|---|
| 149 | n/a | /* Clear the input bit accumulator */ | 
|---|
| 150 | n/a | #define INITBITS() \ | 
|---|
| 151 | n/a | do { \ | 
|---|
| 152 | n/a | hold = 0; \ | 
|---|
| 153 | n/a | bits = 0; \ | 
|---|
| 154 | n/a | } while (0) | 
|---|
| 155 | n/a |  | 
|---|
| 156 | n/a | /* Assure that some input is available.  If input is requested, but denied, | 
|---|
| 157 | n/a | then return a Z_BUF_ERROR from inflateBack(). */ | 
|---|
| 158 | n/a | #define PULL() \ | 
|---|
| 159 | n/a | do { \ | 
|---|
| 160 | n/a | if (have == 0) { \ | 
|---|
| 161 | n/a | have = in(in_desc, &next); \ | 
|---|
| 162 | n/a | if (have == 0) { \ | 
|---|
| 163 | n/a | next = Z_NULL; \ | 
|---|
| 164 | n/a | ret = Z_BUF_ERROR; \ | 
|---|
| 165 | n/a | goto inf_leave; \ | 
|---|
| 166 | n/a | } \ | 
|---|
| 167 | n/a | } \ | 
|---|
| 168 | n/a | } while (0) | 
|---|
| 169 | n/a |  | 
|---|
| 170 | n/a | /* Get a byte of input into the bit accumulator, or return from inflateBack() | 
|---|
| 171 | n/a | with an error if there is no input available. */ | 
|---|
| 172 | n/a | #define PULLBYTE() \ | 
|---|
| 173 | n/a | do { \ | 
|---|
| 174 | n/a | PULL(); \ | 
|---|
| 175 | n/a | have--; \ | 
|---|
| 176 | n/a | hold += (unsigned long)(*next++) << bits; \ | 
|---|
| 177 | n/a | bits += 8; \ | 
|---|
| 178 | n/a | } while (0) | 
|---|
| 179 | n/a |  | 
|---|
| 180 | n/a | /* Assure that there are at least n bits in the bit accumulator.  If there is | 
|---|
| 181 | n/a | not enough available input to do that, then return from inflateBack() with | 
|---|
| 182 | n/a | an error. */ | 
|---|
| 183 | n/a | #define NEEDBITS(n) \ | 
|---|
| 184 | n/a | do { \ | 
|---|
| 185 | n/a | while (bits < (unsigned)(n)) \ | 
|---|
| 186 | n/a | PULLBYTE(); \ | 
|---|
| 187 | n/a | } while (0) | 
|---|
| 188 | n/a |  | 
|---|
| 189 | n/a | /* Return the low n bits of the bit accumulator (n < 16) */ | 
|---|
| 190 | n/a | #define BITS(n) \ | 
|---|
| 191 | n/a | ((unsigned)hold & ((1U << (n)) - 1)) | 
|---|
| 192 | n/a |  | 
|---|
| 193 | n/a | /* Remove n bits from the bit accumulator */ | 
|---|
| 194 | n/a | #define DROPBITS(n) \ | 
|---|
| 195 | n/a | do { \ | 
|---|
| 196 | n/a | hold >>= (n); \ | 
|---|
| 197 | n/a | bits -= (unsigned)(n); \ | 
|---|
| 198 | n/a | } while (0) | 
|---|
| 199 | n/a |  | 
|---|
| 200 | n/a | /* Remove zero to seven bits as needed to go to a byte boundary */ | 
|---|
| 201 | n/a | #define BYTEBITS() \ | 
|---|
| 202 | n/a | do { \ | 
|---|
| 203 | n/a | hold >>= bits & 7; \ | 
|---|
| 204 | n/a | bits -= bits & 7; \ | 
|---|
| 205 | n/a | } while (0) | 
|---|
| 206 | n/a |  | 
|---|
| 207 | n/a | /* Assure that some output space is available, by writing out the window | 
|---|
| 208 | n/a | if it's full.  If the write fails, return from inflateBack() with a | 
|---|
| 209 | n/a | Z_BUF_ERROR. */ | 
|---|
| 210 | n/a | #define ROOM() \ | 
|---|
| 211 | n/a | do { \ | 
|---|
| 212 | n/a | if (left == 0) { \ | 
|---|
| 213 | n/a | put = state->window; \ | 
|---|
| 214 | n/a | left = state->wsize; \ | 
|---|
| 215 | n/a | state->whave = left; \ | 
|---|
| 216 | n/a | if (out(out_desc, put, left)) { \ | 
|---|
| 217 | n/a | ret = Z_BUF_ERROR; \ | 
|---|
| 218 | n/a | goto inf_leave; \ | 
|---|
| 219 | n/a | } \ | 
|---|
| 220 | n/a | } \ | 
|---|
| 221 | n/a | } while (0) | 
|---|
| 222 | n/a |  | 
|---|
| 223 | n/a | /* | 
|---|
| 224 | n/a | strm provides the memory allocation functions and window buffer on input, | 
|---|
| 225 | n/a | and provides information on the unused input on return.  For Z_DATA_ERROR | 
|---|
| 226 | n/a | returns, strm will also provide an error message. | 
|---|
| 227 | n/a |  | 
|---|
| 228 | n/a | in() and out() are the call-back input and output functions.  When | 
|---|
| 229 | n/a | inflateBack() needs more input, it calls in().  When inflateBack() has | 
|---|
| 230 | n/a | filled the window with output, or when it completes with data in the | 
|---|
| 231 | n/a | window, it calls out() to write out the data.  The application must not | 
|---|
| 232 | n/a | change the provided input until in() is called again or inflateBack() | 
|---|
| 233 | n/a | returns.  The application must not change the window/output buffer until | 
|---|
| 234 | n/a | inflateBack() returns. | 
|---|
| 235 | n/a |  | 
|---|
| 236 | n/a | in() and out() are called with a descriptor parameter provided in the | 
|---|
| 237 | n/a | inflateBack() call.  This parameter can be a structure that provides the | 
|---|
| 238 | n/a | information required to do the read or write, as well as accumulated | 
|---|
| 239 | n/a | information on the input and output such as totals and check values. | 
|---|
| 240 | n/a |  | 
|---|
| 241 | n/a | in() should return zero on failure.  out() should return non-zero on | 
|---|
| 242 | n/a | failure.  If either in() or out() fails, than inflateBack() returns a | 
|---|
| 243 | n/a | Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it | 
|---|
| 244 | n/a | was in() or out() that caused in the error.  Otherwise,  inflateBack() | 
|---|
| 245 | n/a | returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format | 
|---|
| 246 | n/a | error, or Z_MEM_ERROR if it could not allocate memory for the state. | 
|---|
| 247 | n/a | inflateBack() can also return Z_STREAM_ERROR if the input parameters | 
|---|
| 248 | n/a | are not correct, i.e. strm is Z_NULL or the state was not initialized. | 
|---|
| 249 | n/a | */ | 
|---|
| 250 | n/a | int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) | 
|---|
| 251 | n/a | z_streamp strm; | 
|---|
| 252 | n/a | in_func in; | 
|---|
| 253 | n/a | void FAR *in_desc; | 
|---|
| 254 | n/a | out_func out; | 
|---|
| 255 | n/a | void FAR *out_desc; | 
|---|
| 256 | n/a | { | 
|---|
| 257 | n/a | struct inflate_state FAR *state; | 
|---|
| 258 | n/a | z_const unsigned char FAR *next;    /* next input */ | 
|---|
| 259 | n/a | unsigned char FAR *put;     /* next output */ | 
|---|
| 260 | n/a | unsigned have, left;        /* available input and output */ | 
|---|
| 261 | n/a | unsigned long hold;         /* bit buffer */ | 
|---|
| 262 | n/a | unsigned bits;              /* bits in bit buffer */ | 
|---|
| 263 | n/a | unsigned copy;              /* number of stored or match bytes to copy */ | 
|---|
| 264 | n/a | unsigned char FAR *from;    /* where to copy match bytes from */ | 
|---|
| 265 | n/a | code here;                  /* current decoding table entry */ | 
|---|
| 266 | n/a | code last;                  /* parent table entry */ | 
|---|
| 267 | n/a | unsigned len;               /* length to copy for repeats, bits to drop */ | 
|---|
| 268 | n/a | int ret;                    /* return code */ | 
|---|
| 269 | n/a | static const unsigned short order[19] = /* permutation of code lengths */ | 
|---|
| 270 | n/a | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | 
|---|
| 271 | n/a |  | 
|---|
| 272 | n/a | /* Check that the strm exists and that the state was initialized */ | 
|---|
| 273 | n/a | if (strm == Z_NULL || strm->state == Z_NULL) | 
|---|
| 274 | n/a | return Z_STREAM_ERROR; | 
|---|
| 275 | n/a | state = (struct inflate_state FAR *)strm->state; | 
|---|
| 276 | n/a |  | 
|---|
| 277 | n/a | /* Reset the state */ | 
|---|
| 278 | n/a | strm->msg = Z_NULL; | 
|---|
| 279 | n/a | state->mode = TYPE; | 
|---|
| 280 | n/a | state->last = 0; | 
|---|
| 281 | n/a | state->whave = 0; | 
|---|
| 282 | n/a | next = strm->next_in; | 
|---|
| 283 | n/a | have = next != Z_NULL ? strm->avail_in : 0; | 
|---|
| 284 | n/a | hold = 0; | 
|---|
| 285 | n/a | bits = 0; | 
|---|
| 286 | n/a | put = state->window; | 
|---|
| 287 | n/a | left = state->wsize; | 
|---|
| 288 | n/a |  | 
|---|
| 289 | n/a | /* Inflate until end of block marked as last */ | 
|---|
| 290 | n/a | for (;;) | 
|---|
| 291 | n/a | switch (state->mode) { | 
|---|
| 292 | n/a | case TYPE: | 
|---|
| 293 | n/a | /* determine and dispatch block type */ | 
|---|
| 294 | n/a | if (state->last) { | 
|---|
| 295 | n/a | BYTEBITS(); | 
|---|
| 296 | n/a | state->mode = DONE; | 
|---|
| 297 | n/a | break; | 
|---|
| 298 | n/a | } | 
|---|
| 299 | n/a | NEEDBITS(3); | 
|---|
| 300 | n/a | state->last = BITS(1); | 
|---|
| 301 | n/a | DROPBITS(1); | 
|---|
| 302 | n/a | switch (BITS(2)) { | 
|---|
| 303 | n/a | case 0:                             /* stored block */ | 
|---|
| 304 | n/a | Tracev((stderr, "inflate:     stored block%s\n", | 
|---|
| 305 | n/a | state->last ? " (last)" : "")); | 
|---|
| 306 | n/a | state->mode = STORED; | 
|---|
| 307 | n/a | break; | 
|---|
| 308 | n/a | case 1:                             /* fixed block */ | 
|---|
| 309 | n/a | fixedtables(state); | 
|---|
| 310 | n/a | Tracev((stderr, "inflate:     fixed codes block%s\n", | 
|---|
| 311 | n/a | state->last ? " (last)" : "")); | 
|---|
| 312 | n/a | state->mode = LEN;              /* decode codes */ | 
|---|
| 313 | n/a | break; | 
|---|
| 314 | n/a | case 2:                             /* dynamic block */ | 
|---|
| 315 | n/a | Tracev((stderr, "inflate:     dynamic codes block%s\n", | 
|---|
| 316 | n/a | state->last ? " (last)" : "")); | 
|---|
| 317 | n/a | state->mode = TABLE; | 
|---|
| 318 | n/a | break; | 
|---|
| 319 | n/a | case 3: | 
|---|
| 320 | n/a | strm->msg = (char *)"invalid block type"; | 
|---|
| 321 | n/a | state->mode = BAD; | 
|---|
| 322 | n/a | } | 
|---|
| 323 | n/a | DROPBITS(2); | 
|---|
| 324 | n/a | break; | 
|---|
| 325 | n/a |  | 
|---|
| 326 | n/a | case STORED: | 
|---|
| 327 | n/a | /* get and verify stored block length */ | 
|---|
| 328 | n/a | BYTEBITS();                         /* go to byte boundary */ | 
|---|
| 329 | n/a | NEEDBITS(32); | 
|---|
| 330 | n/a | if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { | 
|---|
| 331 | n/a | strm->msg = (char *)"invalid stored block lengths"; | 
|---|
| 332 | n/a | state->mode = BAD; | 
|---|
| 333 | n/a | break; | 
|---|
| 334 | n/a | } | 
|---|
| 335 | n/a | state->length = (unsigned)hold & 0xffff; | 
|---|
| 336 | n/a | Tracev((stderr, "inflate:       stored length %u\n", | 
|---|
| 337 | n/a | state->length)); | 
|---|
| 338 | n/a | INITBITS(); | 
|---|
| 339 | n/a |  | 
|---|
| 340 | n/a | /* copy stored block from input to output */ | 
|---|
| 341 | n/a | while (state->length != 0) { | 
|---|
| 342 | n/a | copy = state->length; | 
|---|
| 343 | n/a | PULL(); | 
|---|
| 344 | n/a | ROOM(); | 
|---|
| 345 | n/a | if (copy > have) copy = have; | 
|---|
| 346 | n/a | if (copy > left) copy = left; | 
|---|
| 347 | n/a | zmemcpy(put, next, copy); | 
|---|
| 348 | n/a | have -= copy; | 
|---|
| 349 | n/a | next += copy; | 
|---|
| 350 | n/a | left -= copy; | 
|---|
| 351 | n/a | put += copy; | 
|---|
| 352 | n/a | state->length -= copy; | 
|---|
| 353 | n/a | } | 
|---|
| 354 | n/a | Tracev((stderr, "inflate:       stored end\n")); | 
|---|
| 355 | n/a | state->mode = TYPE; | 
|---|
| 356 | n/a | break; | 
|---|
| 357 | n/a |  | 
|---|
| 358 | n/a | case TABLE: | 
|---|
| 359 | n/a | /* get dynamic table entries descriptor */ | 
|---|
| 360 | n/a | NEEDBITS(14); | 
|---|
| 361 | n/a | state->nlen = BITS(5) + 257; | 
|---|
| 362 | n/a | DROPBITS(5); | 
|---|
| 363 | n/a | state->ndist = BITS(5) + 1; | 
|---|
| 364 | n/a | DROPBITS(5); | 
|---|
| 365 | n/a | state->ncode = BITS(4) + 4; | 
|---|
| 366 | n/a | DROPBITS(4); | 
|---|
| 367 | n/a | #ifndef PKZIP_BUG_WORKAROUND | 
|---|
| 368 | n/a | if (state->nlen > 286 || state->ndist > 30) { | 
|---|
| 369 | n/a | strm->msg = (char *)"too many length or distance symbols"; | 
|---|
| 370 | n/a | state->mode = BAD; | 
|---|
| 371 | n/a | break; | 
|---|
| 372 | n/a | } | 
|---|
| 373 | n/a | #endif | 
|---|
| 374 | n/a | Tracev((stderr, "inflate:       table sizes ok\n")); | 
|---|
| 375 | n/a |  | 
|---|
| 376 | n/a | /* get code length code lengths (not a typo) */ | 
|---|
| 377 | n/a | state->have = 0; | 
|---|
| 378 | n/a | while (state->have < state->ncode) { | 
|---|
| 379 | n/a | NEEDBITS(3); | 
|---|
| 380 | n/a | state->lens[order[state->have++]] = (unsigned short)BITS(3); | 
|---|
| 381 | n/a | DROPBITS(3); | 
|---|
| 382 | n/a | } | 
|---|
| 383 | n/a | while (state->have < 19) | 
|---|
| 384 | n/a | state->lens[order[state->have++]] = 0; | 
|---|
| 385 | n/a | state->next = state->codes; | 
|---|
| 386 | n/a | state->lencode = (code const FAR *)(state->next); | 
|---|
| 387 | n/a | state->lenbits = 7; | 
|---|
| 388 | n/a | ret = inflate_table(CODES, state->lens, 19, &(state->next), | 
|---|
| 389 | n/a | &(state->lenbits), state->work); | 
|---|
| 390 | n/a | if (ret) { | 
|---|
| 391 | n/a | strm->msg = (char *)"invalid code lengths set"; | 
|---|
| 392 | n/a | state->mode = BAD; | 
|---|
| 393 | n/a | break; | 
|---|
| 394 | n/a | } | 
|---|
| 395 | n/a | Tracev((stderr, "inflate:       code lengths ok\n")); | 
|---|
| 396 | n/a |  | 
|---|
| 397 | n/a | /* get length and distance code code lengths */ | 
|---|
| 398 | n/a | state->have = 0; | 
|---|
| 399 | n/a | while (state->have < state->nlen + state->ndist) { | 
|---|
| 400 | n/a | for (;;) { | 
|---|
| 401 | n/a | here = state->lencode[BITS(state->lenbits)]; | 
|---|
| 402 | n/a | if ((unsigned)(here.bits) <= bits) break; | 
|---|
| 403 | n/a | PULLBYTE(); | 
|---|
| 404 | n/a | } | 
|---|
| 405 | n/a | if (here.val < 16) { | 
|---|
| 406 | n/a | DROPBITS(here.bits); | 
|---|
| 407 | n/a | state->lens[state->have++] = here.val; | 
|---|
| 408 | n/a | } | 
|---|
| 409 | n/a | else { | 
|---|
| 410 | n/a | if (here.val == 16) { | 
|---|
| 411 | n/a | NEEDBITS(here.bits + 2); | 
|---|
| 412 | n/a | DROPBITS(here.bits); | 
|---|
| 413 | n/a | if (state->have == 0) { | 
|---|
| 414 | n/a | strm->msg = (char *)"invalid bit length repeat"; | 
|---|
| 415 | n/a | state->mode = BAD; | 
|---|
| 416 | n/a | break; | 
|---|
| 417 | n/a | } | 
|---|
| 418 | n/a | len = (unsigned)(state->lens[state->have - 1]); | 
|---|
| 419 | n/a | copy = 3 + BITS(2); | 
|---|
| 420 | n/a | DROPBITS(2); | 
|---|
| 421 | n/a | } | 
|---|
| 422 | n/a | else if (here.val == 17) { | 
|---|
| 423 | n/a | NEEDBITS(here.bits + 3); | 
|---|
| 424 | n/a | DROPBITS(here.bits); | 
|---|
| 425 | n/a | len = 0; | 
|---|
| 426 | n/a | copy = 3 + BITS(3); | 
|---|
| 427 | n/a | DROPBITS(3); | 
|---|
| 428 | n/a | } | 
|---|
| 429 | n/a | else { | 
|---|
| 430 | n/a | NEEDBITS(here.bits + 7); | 
|---|
| 431 | n/a | DROPBITS(here.bits); | 
|---|
| 432 | n/a | len = 0; | 
|---|
| 433 | n/a | copy = 11 + BITS(7); | 
|---|
| 434 | n/a | DROPBITS(7); | 
|---|
| 435 | n/a | } | 
|---|
| 436 | n/a | if (state->have + copy > state->nlen + state->ndist) { | 
|---|
| 437 | n/a | strm->msg = (char *)"invalid bit length repeat"; | 
|---|
| 438 | n/a | state->mode = BAD; | 
|---|
| 439 | n/a | break; | 
|---|
| 440 | n/a | } | 
|---|
| 441 | n/a | while (copy--) | 
|---|
| 442 | n/a | state->lens[state->have++] = (unsigned short)len; | 
|---|
| 443 | n/a | } | 
|---|
| 444 | n/a | } | 
|---|
| 445 | n/a |  | 
|---|
| 446 | n/a | /* handle error breaks in while */ | 
|---|
| 447 | n/a | if (state->mode == BAD) break; | 
|---|
| 448 | n/a |  | 
|---|
| 449 | n/a | /* check for end-of-block code (better have one) */ | 
|---|
| 450 | n/a | if (state->lens[256] == 0) { | 
|---|
| 451 | n/a | strm->msg = (char *)"invalid code -- missing end-of-block"; | 
|---|
| 452 | n/a | state->mode = BAD; | 
|---|
| 453 | n/a | break; | 
|---|
| 454 | n/a | } | 
|---|
| 455 | n/a |  | 
|---|
| 456 | n/a | /* build code tables -- note: do not change the lenbits or distbits | 
|---|
| 457 | n/a | values here (9 and 6) without reading the comments in inftrees.h | 
|---|
| 458 | n/a | concerning the ENOUGH constants, which depend on those values */ | 
|---|
| 459 | n/a | state->next = state->codes; | 
|---|
| 460 | n/a | state->lencode = (code const FAR *)(state->next); | 
|---|
| 461 | n/a | state->lenbits = 9; | 
|---|
| 462 | n/a | ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), | 
|---|
| 463 | n/a | &(state->lenbits), state->work); | 
|---|
| 464 | n/a | if (ret) { | 
|---|
| 465 | n/a | strm->msg = (char *)"invalid literal/lengths set"; | 
|---|
| 466 | n/a | state->mode = BAD; | 
|---|
| 467 | n/a | break; | 
|---|
| 468 | n/a | } | 
|---|
| 469 | n/a | state->distcode = (code const FAR *)(state->next); | 
|---|
| 470 | n/a | state->distbits = 6; | 
|---|
| 471 | n/a | ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, | 
|---|
| 472 | n/a | &(state->next), &(state->distbits), state->work); | 
|---|
| 473 | n/a | if (ret) { | 
|---|
| 474 | n/a | strm->msg = (char *)"invalid distances set"; | 
|---|
| 475 | n/a | state->mode = BAD; | 
|---|
| 476 | n/a | break; | 
|---|
| 477 | n/a | } | 
|---|
| 478 | n/a | Tracev((stderr, "inflate:       codes ok\n")); | 
|---|
| 479 | n/a | state->mode = LEN; | 
|---|
| 480 | n/a |  | 
|---|
| 481 | n/a | case LEN: | 
|---|
| 482 | n/a | /* use inflate_fast() if we have enough input and output */ | 
|---|
| 483 | n/a | if (have >= 6 && left >= 258) { | 
|---|
| 484 | n/a | RESTORE(); | 
|---|
| 485 | n/a | if (state->whave < state->wsize) | 
|---|
| 486 | n/a | state->whave = state->wsize - left; | 
|---|
| 487 | n/a | inflate_fast(strm, state->wsize); | 
|---|
| 488 | n/a | LOAD(); | 
|---|
| 489 | n/a | break; | 
|---|
| 490 | n/a | } | 
|---|
| 491 | n/a |  | 
|---|
| 492 | n/a | /* get a literal, length, or end-of-block code */ | 
|---|
| 493 | n/a | for (;;) { | 
|---|
| 494 | n/a | here = state->lencode[BITS(state->lenbits)]; | 
|---|
| 495 | n/a | if ((unsigned)(here.bits) <= bits) break; | 
|---|
| 496 | n/a | PULLBYTE(); | 
|---|
| 497 | n/a | } | 
|---|
| 498 | n/a | if (here.op && (here.op & 0xf0) == 0) { | 
|---|
| 499 | n/a | last = here; | 
|---|
| 500 | n/a | for (;;) { | 
|---|
| 501 | n/a | here = state->lencode[last.val + | 
|---|
| 502 | n/a | (BITS(last.bits + last.op) >> last.bits)]; | 
|---|
| 503 | n/a | if ((unsigned)(last.bits + here.bits) <= bits) break; | 
|---|
| 504 | n/a | PULLBYTE(); | 
|---|
| 505 | n/a | } | 
|---|
| 506 | n/a | DROPBITS(last.bits); | 
|---|
| 507 | n/a | } | 
|---|
| 508 | n/a | DROPBITS(here.bits); | 
|---|
| 509 | n/a | state->length = (unsigned)here.val; | 
|---|
| 510 | n/a |  | 
|---|
| 511 | n/a | /* process literal */ | 
|---|
| 512 | n/a | if (here.op == 0) { | 
|---|
| 513 | n/a | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? | 
|---|
| 514 | n/a | "inflate:         literal '%c'\n" : | 
|---|
| 515 | n/a | "inflate:         literal 0x%02x\n", here.val)); | 
|---|
| 516 | n/a | ROOM(); | 
|---|
| 517 | n/a | *put++ = (unsigned char)(state->length); | 
|---|
| 518 | n/a | left--; | 
|---|
| 519 | n/a | state->mode = LEN; | 
|---|
| 520 | n/a | break; | 
|---|
| 521 | n/a | } | 
|---|
| 522 | n/a |  | 
|---|
| 523 | n/a | /* process end of block */ | 
|---|
| 524 | n/a | if (here.op & 32) { | 
|---|
| 525 | n/a | Tracevv((stderr, "inflate:         end of block\n")); | 
|---|
| 526 | n/a | state->mode = TYPE; | 
|---|
| 527 | n/a | break; | 
|---|
| 528 | n/a | } | 
|---|
| 529 | n/a |  | 
|---|
| 530 | n/a | /* invalid code */ | 
|---|
| 531 | n/a | if (here.op & 64) { | 
|---|
| 532 | n/a | strm->msg = (char *)"invalid literal/length code"; | 
|---|
| 533 | n/a | state->mode = BAD; | 
|---|
| 534 | n/a | break; | 
|---|
| 535 | n/a | } | 
|---|
| 536 | n/a |  | 
|---|
| 537 | n/a | /* length code -- get extra bits, if any */ | 
|---|
| 538 | n/a | state->extra = (unsigned)(here.op) & 15; | 
|---|
| 539 | n/a | if (state->extra != 0) { | 
|---|
| 540 | n/a | NEEDBITS(state->extra); | 
|---|
| 541 | n/a | state->length += BITS(state->extra); | 
|---|
| 542 | n/a | DROPBITS(state->extra); | 
|---|
| 543 | n/a | } | 
|---|
| 544 | n/a | Tracevv((stderr, "inflate:         length %u\n", state->length)); | 
|---|
| 545 | n/a |  | 
|---|
| 546 | n/a | /* get distance code */ | 
|---|
| 547 | n/a | for (;;) { | 
|---|
| 548 | n/a | here = state->distcode[BITS(state->distbits)]; | 
|---|
| 549 | n/a | if ((unsigned)(here.bits) <= bits) break; | 
|---|
| 550 | n/a | PULLBYTE(); | 
|---|
| 551 | n/a | } | 
|---|
| 552 | n/a | if ((here.op & 0xf0) == 0) { | 
|---|
| 553 | n/a | last = here; | 
|---|
| 554 | n/a | for (;;) { | 
|---|
| 555 | n/a | here = state->distcode[last.val + | 
|---|
| 556 | n/a | (BITS(last.bits + last.op) >> last.bits)]; | 
|---|
| 557 | n/a | if ((unsigned)(last.bits + here.bits) <= bits) break; | 
|---|
| 558 | n/a | PULLBYTE(); | 
|---|
| 559 | n/a | } | 
|---|
| 560 | n/a | DROPBITS(last.bits); | 
|---|
| 561 | n/a | } | 
|---|
| 562 | n/a | DROPBITS(here.bits); | 
|---|
| 563 | n/a | if (here.op & 64) { | 
|---|
| 564 | n/a | strm->msg = (char *)"invalid distance code"; | 
|---|
| 565 | n/a | state->mode = BAD; | 
|---|
| 566 | n/a | break; | 
|---|
| 567 | n/a | } | 
|---|
| 568 | n/a | state->offset = (unsigned)here.val; | 
|---|
| 569 | n/a |  | 
|---|
| 570 | n/a | /* get distance extra bits, if any */ | 
|---|
| 571 | n/a | state->extra = (unsigned)(here.op) & 15; | 
|---|
| 572 | n/a | if (state->extra != 0) { | 
|---|
| 573 | n/a | NEEDBITS(state->extra); | 
|---|
| 574 | n/a | state->offset += BITS(state->extra); | 
|---|
| 575 | n/a | DROPBITS(state->extra); | 
|---|
| 576 | n/a | } | 
|---|
| 577 | n/a | if (state->offset > state->wsize - (state->whave < state->wsize ? | 
|---|
| 578 | n/a | left : 0)) { | 
|---|
| 579 | n/a | strm->msg = (char *)"invalid distance too far back"; | 
|---|
| 580 | n/a | state->mode = BAD; | 
|---|
| 581 | n/a | break; | 
|---|
| 582 | n/a | } | 
|---|
| 583 | n/a | Tracevv((stderr, "inflate:         distance %u\n", state->offset)); | 
|---|
| 584 | n/a |  | 
|---|
| 585 | n/a | /* copy match from window to output */ | 
|---|
| 586 | n/a | do { | 
|---|
| 587 | n/a | ROOM(); | 
|---|
| 588 | n/a | copy = state->wsize - state->offset; | 
|---|
| 589 | n/a | if (copy < left) { | 
|---|
| 590 | n/a | from = put + copy; | 
|---|
| 591 | n/a | copy = left - copy; | 
|---|
| 592 | n/a | } | 
|---|
| 593 | n/a | else { | 
|---|
| 594 | n/a | from = put - state->offset; | 
|---|
| 595 | n/a | copy = left; | 
|---|
| 596 | n/a | } | 
|---|
| 597 | n/a | if (copy > state->length) copy = state->length; | 
|---|
| 598 | n/a | state->length -= copy; | 
|---|
| 599 | n/a | left -= copy; | 
|---|
| 600 | n/a | do { | 
|---|
| 601 | n/a | *put++ = *from++; | 
|---|
| 602 | n/a | } while (--copy); | 
|---|
| 603 | n/a | } while (state->length != 0); | 
|---|
| 604 | n/a | break; | 
|---|
| 605 | n/a |  | 
|---|
| 606 | n/a | case DONE: | 
|---|
| 607 | n/a | /* inflate stream terminated properly -- write leftover output */ | 
|---|
| 608 | n/a | ret = Z_STREAM_END; | 
|---|
| 609 | n/a | if (left < state->wsize) { | 
|---|
| 610 | n/a | if (out(out_desc, state->window, state->wsize - left)) | 
|---|
| 611 | n/a | ret = Z_BUF_ERROR; | 
|---|
| 612 | n/a | } | 
|---|
| 613 | n/a | goto inf_leave; | 
|---|
| 614 | n/a |  | 
|---|
| 615 | n/a | case BAD: | 
|---|
| 616 | n/a | ret = Z_DATA_ERROR; | 
|---|
| 617 | n/a | goto inf_leave; | 
|---|
| 618 | n/a |  | 
|---|
| 619 | n/a | default:                /* can't happen, but makes compilers happy */ | 
|---|
| 620 | n/a | ret = Z_STREAM_ERROR; | 
|---|
| 621 | n/a | goto inf_leave; | 
|---|
| 622 | n/a | } | 
|---|
| 623 | n/a |  | 
|---|
| 624 | n/a | /* Return unused input */ | 
|---|
| 625 | n/a | inf_leave: | 
|---|
| 626 | n/a | strm->next_in = next; | 
|---|
| 627 | n/a | strm->avail_in = have; | 
|---|
| 628 | n/a | return ret; | 
|---|
| 629 | n/a | } | 
|---|
| 630 | n/a |  | 
|---|
| 631 | n/a | int ZEXPORT inflateBackEnd(strm) | 
|---|
| 632 | n/a | z_streamp strm; | 
|---|
| 633 | n/a | { | 
|---|
| 634 | n/a | if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) | 
|---|
| 635 | n/a | return Z_STREAM_ERROR; | 
|---|
| 636 | n/a | ZFREE(strm, strm->state); | 
|---|
| 637 | n/a | strm->state = Z_NULL; | 
|---|
| 638 | n/a | Tracev((stderr, "inflate: end\n")); | 
|---|
| 639 | n/a | return Z_OK; | 
|---|
| 640 | n/a | } | 
|---|