inflate.c

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