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