inflate.c (muscle7.61) | : | inflate.c (muscle7.62) | ||
---|---|---|---|---|
/* inflate.c -- zlib decompression | /* inflate.c -- zlib decompression | |||
* Copyright (C) 1995-2012 Mark Adler | * Copyright (C) 1995-2016 Mark Adler | |||
* For conditions of distribution and use, see copyright notice in zlib.h | * For conditions of distribution and use, see copyright notice in zlib.h | |||
*/ | */ | |||
/* | /* | |||
* Change history: | * Change history: | |||
* | * | |||
* 1.2.beta0 24 Nov 2002 | * 1.2.beta0 24 Nov 2002 | |||
* - First version -- complete rewrite of inflate to simplify code, avoid | * - First version -- complete rewrite of inflate to simplify code, avoid | |||
* creation of window when not needed, minimize use of window when it is | * creation of window when not needed, minimize use of window when it is | |||
* needed, make inffast.c even faster, implement gzip decoding, and to | * needed, make inffast.c even faster, implement gzip decoding, and to | |||
skipping to change at line 95 | skipping to change at line 95 | |||
#include "inflate.h" | #include "inflate.h" | |||
#include "inffast.h" | #include "inffast.h" | |||
#ifdef MAKEFIXED | #ifdef MAKEFIXED | |||
# ifndef BUILDFIXED | # ifndef BUILDFIXED | |||
# define BUILDFIXED | # define BUILDFIXED | |||
# endif | # endif | |||
#endif | #endif | |||
/* function prototypes */ | /* function prototypes */ | |||
local int inflateStateCheck OF((z_streamp strm)); | ||||
local void fixedtables OF((struct inflate_state FAR *state)); | local void fixedtables OF((struct inflate_state FAR *state)); | |||
local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, | local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, | |||
unsigned copy)); | unsigned copy)); | |||
#ifdef BUILDFIXED | #ifdef BUILDFIXED | |||
void makefixed OF((void)); | void makefixed OF((void)); | |||
#endif | #endif | |||
local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, | local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, | |||
unsigned len)); | unsigned len)); | |||
local int inflateStateCheck(strm) | ||||
z_streamp strm; | ||||
{ | ||||
struct inflate_state FAR *state; | ||||
if (strm == Z_NULL || | ||||
strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) | ||||
return 1; | ||||
state = (struct inflate_state FAR *)strm->state; | ||||
if (state == Z_NULL || state->strm != strm || | ||||
state->mode < HEAD || state->mode > SYNC) | ||||
return 1; | ||||
return 0; | ||||
} | ||||
int ZEXPORT inflateResetKeep(strm) | int ZEXPORT inflateResetKeep(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
strm->total_in = strm->total_out = state->total = 0; | strm->total_in = strm->total_out = state->total = 0; | |||
strm->msg = Z_NULL; | strm->msg = Z_NULL; | |||
if (state->wrap) /* to support ill-conceived Java test suite */ | if (state->wrap) /* to support ill-conceived Java test suite */ | |||
strm->adler = state->wrap & 1; | strm->adler = state->wrap & 1; | |||
state->mode = HEAD; | state->mode = HEAD; | |||
state->last = 0; | state->last = 0; | |||
state->havedict = 0; | state->havedict = 0; | |||
state->dmax = 32768U; | state->dmax = 32768U; | |||
state->head = Z_NULL; | state->head = Z_NULL; | |||
skipping to change at line 134 | skipping to change at line 149 | |||
state->back = -1; | state->back = -1; | |||
Tracev((stderr, "inflate: reset\n")); | Tracev((stderr, "inflate: reset\n")); | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
int ZEXPORT inflateReset(strm) | int ZEXPORT inflateReset(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
state->wsize = 0; | state->wsize = 0; | |||
state->whave = 0; | state->whave = 0; | |||
state->wnext = 0; | state->wnext = 0; | |||
return inflateResetKeep(strm); | return inflateResetKeep(strm); | |||
} | } | |||
int ZEXPORT inflateReset2(strm, windowBits) | int ZEXPORT inflateReset2(strm, windowBits) | |||
z_streamp strm; | z_streamp strm; | |||
int windowBits; | int windowBits; | |||
{ | { | |||
int wrap; | int wrap; | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
/* get the state */ | /* get the state */ | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
/* extract wrap request from windowBits parameter */ | /* extract wrap request from windowBits parameter */ | |||
if (windowBits < 0) { | if (windowBits < 0) { | |||
wrap = 0; | wrap = 0; | |||
windowBits = -windowBits; | windowBits = -windowBits; | |||
} | } | |||
else { | else { | |||
wrap = (windowBits >> 4) + 1; | wrap = (windowBits >> 4) + 5; | |||
#ifdef GUNZIP | #ifdef GUNZIP | |||
if (windowBits < 48) | if (windowBits < 48) | |||
windowBits &= 15; | windowBits &= 15; | |||
#endif | #endif | |||
} | } | |||
/* set number of window bits, free window if different */ | /* set number of window bits, free window if different */ | |||
if (windowBits && (windowBits < 8 || windowBits > 15)) | if (windowBits && (windowBits < 8 || windowBits > 15)) | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { | if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { | |||
skipping to change at line 213 | skipping to change at line 228 | |||
#ifdef Z_SOLO | #ifdef Z_SOLO | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
#else | #else | |||
strm->zfree = zcfree; | strm->zfree = zcfree; | |||
#endif | #endif | |||
state = (struct inflate_state FAR *) | state = (struct inflate_state FAR *) | |||
ZALLOC(strm, 1, sizeof(struct inflate_state)); | ZALLOC(strm, 1, sizeof(struct inflate_state)); | |||
if (state == Z_NULL) return Z_MEM_ERROR; | if (state == Z_NULL) return Z_MEM_ERROR; | |||
Tracev((stderr, "inflate: allocated\n")); | Tracev((stderr, "inflate: allocated\n")); | |||
strm->state = (struct internal_state FAR *)state; | strm->state = (struct internal_state FAR *)state; | |||
state->strm = strm; | ||||
state->window = Z_NULL; | state->window = Z_NULL; | |||
state->mode = HEAD; /* to pass state test in inflateReset2() */ | ||||
ret = inflateReset2(strm, windowBits); | ret = inflateReset2(strm, windowBits); | |||
if (ret != Z_OK) { | if (ret != Z_OK) { | |||
ZFREE(strm, state); | ZFREE(strm, state); | |||
strm->state = Z_NULL; | strm->state = Z_NULL; | |||
} | } | |||
return ret; | return ret; | |||
} | } | |||
int ZEXPORT inflateInit_(strm, version, stream_size) | int ZEXPORT inflateInit_(strm, version, stream_size) | |||
z_streamp strm; | z_streamp strm; | |||
skipping to change at line 237 | skipping to change at line 254 | |||
return inflateInit2_(strm, DEF_WBITS, version, stream_size); | return inflateInit2_(strm, DEF_WBITS, version, stream_size); | |||
} | } | |||
int ZEXPORT inflatePrime(strm, bits, value) | int ZEXPORT inflatePrime(strm, bits, value) | |||
z_streamp strm; | z_streamp strm; | |||
int bits; | int bits; | |||
int value; | int value; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if (bits < 0) { | if (bits < 0) { | |||
state->hold = 0; | state->hold = 0; | |||
state->bits = 0; | state->bits = 0; | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; | if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; | |||
value &= (1L << bits) - 1; | value &= (1L << bits) - 1; | |||
state->hold += value << state->bits; | state->hold += (unsigned)value << state->bits; | |||
state->bits += bits; | state->bits += (uInt)bits; | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
/* | /* | |||
Return state with length and distance decoding tables and index sizes set to | Return state with length and distance decoding tables and index sizes set to | |||
fixed code decoding. Normally this returns fixed tables from inffixed.h. | fixed code decoding. Normally this returns fixed tables from inffixed.h. | |||
If BUILDFIXED is defined, then instead this routine builds the tables the | If BUILDFIXED is defined, then instead this routine builds the tables the | |||
first time it's called, and returns those tables the first time and | first time it's called, and returns those tables the first time and | |||
thereafter. This reduces the size of the code by about 2K bytes, in | thereafter. This reduces the size of the code by about 2K bytes, in | |||
exchange for a little execution time. However, BUILDFIXED should not be | exchange for a little execution time. However, BUILDFIXED should not be | |||
skipping to change at line 628 | skipping to change at line 645 | |||
code here; /* current decoding table entry */ | code here; /* current decoding table entry */ | |||
code last; /* parent table entry */ | code last; /* parent table entry */ | |||
unsigned len; /* length to copy for repeats, bits to drop */ | unsigned len; /* length to copy for repeats, bits to drop */ | |||
int ret; /* return code */ | int ret; /* return code */ | |||
#ifdef GUNZIP | #ifdef GUNZIP | |||
unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ | unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ | |||
#endif | #endif | |||
static const unsigned short order[19] = /* permutation of code lengths */ | static const unsigned short order[19] = /* permutation of code lengths */ | |||
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | |||
if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || | if (inflateStateCheck(strm) || strm->next_out == Z_NULL || | |||
(strm->next_in == Z_NULL && strm->avail_in != 0)) | (strm->next_in == Z_NULL && strm->avail_in != 0)) | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ | if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ | |||
LOAD(); | LOAD(); | |||
in = have; | in = have; | |||
out = left; | out = left; | |||
ret = Z_OK; | ret = Z_OK; | |||
for (;;) | for (;;) | |||
switch (state->mode) { | switch (state->mode) { | |||
case HEAD: | case HEAD: | |||
if (state->wrap == 0) { | if (state->wrap == 0) { | |||
state->mode = TYPEDO; | state->mode = TYPEDO; | |||
break; | break; | |||
} | } | |||
NEEDBITS(16); | NEEDBITS(16); | |||
#ifdef GUNZIP | #ifdef GUNZIP | |||
if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ | if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ | |||
if (state->wbits == 0) | ||||
state->wbits = 15; | ||||
state->check = crc32(0L, Z_NULL, 0); | state->check = crc32(0L, Z_NULL, 0); | |||
CRC2(state->check, hold); | CRC2(state->check, hold); | |||
INITBITS(); | INITBITS(); | |||
state->mode = FLAGS; | state->mode = FLAGS; | |||
break; | break; | |||
} | } | |||
state->flags = 0; /* expect zlib header */ | state->flags = 0; /* expect zlib header */ | |||
if (state->head != Z_NULL) | if (state->head != Z_NULL) | |||
state->head->done = -1; | state->head->done = -1; | |||
if (!(state->wrap & 1) || /* check if zlib header allowed */ | if (!(state->wrap & 1) || /* check if zlib header allowed */ | |||
skipping to change at line 675 | skipping to change at line 694 | |||
} | } | |||
if (BITS(4) != Z_DEFLATED) { | if (BITS(4) != Z_DEFLATED) { | |||
strm->msg = (char *)"unknown compression method"; | strm->msg = (char *)"unknown compression method"; | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
DROPBITS(4); | DROPBITS(4); | |||
len = BITS(4) + 8; | len = BITS(4) + 8; | |||
if (state->wbits == 0) | if (state->wbits == 0) | |||
state->wbits = len; | state->wbits = len; | |||
else if (len > state->wbits) { | if (len > 15 || len > state->wbits) { | |||
strm->msg = (char *)"invalid window size"; | strm->msg = (char *)"invalid window size"; | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
state->dmax = 1U << len; | state->dmax = 1U << len; | |||
Tracev((stderr, "inflate: zlib header ok\n")); | Tracev((stderr, "inflate: zlib header ok\n")); | |||
strm->adler = state->check = adler32(0L, Z_NULL, 0); | strm->adler = state->check = adler32(0L, Z_NULL, 0); | |||
state->mode = hold & 0x200 ? DICTID : TYPE; | state->mode = hold & 0x200 ? DICTID : TYPE; | |||
INITBITS(); | INITBITS(); | |||
break; | break; | |||
skipping to change at line 702 | skipping to change at line 721 | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
if (state->flags & 0xe000) { | if (state->flags & 0xe000) { | |||
strm->msg = (char *)"unknown header flags set"; | strm->msg = (char *)"unknown header flags set"; | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
if (state->head != Z_NULL) | if (state->head != Z_NULL) | |||
state->head->text = (int)((hold >> 8) & 1); | state->head->text = (int)((hold >> 8) & 1); | |||
if (state->flags & 0x0200) CRC2(state->check, hold); | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
CRC2(state->check, hold); | ||||
INITBITS(); | INITBITS(); | |||
state->mode = TIME; | state->mode = TIME; | |||
case TIME: | case TIME: | |||
NEEDBITS(32); | NEEDBITS(32); | |||
if (state->head != Z_NULL) | if (state->head != Z_NULL) | |||
state->head->time = hold; | state->head->time = hold; | |||
if (state->flags & 0x0200) CRC4(state->check, hold); | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
CRC4(state->check, hold); | ||||
INITBITS(); | INITBITS(); | |||
state->mode = OS; | state->mode = OS; | |||
case OS: | case OS: | |||
NEEDBITS(16); | NEEDBITS(16); | |||
if (state->head != Z_NULL) { | if (state->head != Z_NULL) { | |||
state->head->xflags = (int)(hold & 0xff); | state->head->xflags = (int)(hold & 0xff); | |||
state->head->os = (int)(hold >> 8); | state->head->os = (int)(hold >> 8); | |||
} | } | |||
if (state->flags & 0x0200) CRC2(state->check, hold); | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
CRC2(state->check, hold); | ||||
INITBITS(); | INITBITS(); | |||
state->mode = EXLEN; | state->mode = EXLEN; | |||
case EXLEN: | case EXLEN: | |||
if (state->flags & 0x0400) { | if (state->flags & 0x0400) { | |||
NEEDBITS(16); | NEEDBITS(16); | |||
state->length = (unsigned)(hold); | state->length = (unsigned)(hold); | |||
if (state->head != Z_NULL) | if (state->head != Z_NULL) | |||
state->head->extra_len = (unsigned)hold; | state->head->extra_len = (unsigned)hold; | |||
if (state->flags & 0x0200) CRC2(state->check, hold); | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
CRC2(state->check, hold); | ||||
INITBITS(); | INITBITS(); | |||
} | } | |||
else if (state->head != Z_NULL) | else if (state->head != Z_NULL) | |||
state->head->extra = Z_NULL; | state->head->extra = Z_NULL; | |||
state->mode = EXTRA; | state->mode = EXTRA; | |||
case EXTRA: | case EXTRA: | |||
if (state->flags & 0x0400) { | if (state->flags & 0x0400) { | |||
copy = state->length; | copy = state->length; | |||
if (copy > have) copy = have; | if (copy > have) copy = have; | |||
if (copy) { | if (copy) { | |||
if (state->head != Z_NULL && | if (state->head != Z_NULL && | |||
state->head->extra != Z_NULL) { | state->head->extra != Z_NULL) { | |||
len = state->head->extra_len - state->length; | len = state->head->extra_len - state->length; | |||
zmemcpy(state->head->extra + len, next, | zmemcpy(state->head->extra + len, next, | |||
len + copy > state->head->extra_max ? | len + copy > state->head->extra_max ? | |||
state->head->extra_max - len : copy); | state->head->extra_max - len : copy); | |||
} | } | |||
if (state->flags & 0x0200) | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
state->check = crc32(state->check, next, copy); | state->check = crc32(state->check, next, copy); | |||
have -= copy; | have -= copy; | |||
next += copy; | next += copy; | |||
state->length -= copy; | state->length -= copy; | |||
} | } | |||
if (state->length) goto inf_leave; | if (state->length) goto inf_leave; | |||
} | } | |||
state->length = 0; | state->length = 0; | |||
state->mode = NAME; | state->mode = NAME; | |||
case NAME: | case NAME: | |||
if (state->flags & 0x0800) { | if (state->flags & 0x0800) { | |||
if (have == 0) goto inf_leave; | if (have == 0) goto inf_leave; | |||
copy = 0; | copy = 0; | |||
do { | do { | |||
len = (unsigned)(next[copy++]); | len = (unsigned)(next[copy++]); | |||
if (state->head != Z_NULL && | if (state->head != Z_NULL && | |||
state->head->name != Z_NULL && | state->head->name != Z_NULL && | |||
state->length < state->head->name_max) | state->length < state->head->name_max) | |||
state->head->name[state->length++] = len; | state->head->name[state->length++] = (Bytef)len; | |||
} while (len && copy < have); | } while (len && copy < have); | |||
if (state->flags & 0x0200) | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
state->check = crc32(state->check, next, copy); | state->check = crc32(state->check, next, copy); | |||
have -= copy; | have -= copy; | |||
next += copy; | next += copy; | |||
if (len) goto inf_leave; | if (len) goto inf_leave; | |||
} | } | |||
else if (state->head != Z_NULL) | else if (state->head != Z_NULL) | |||
state->head->name = Z_NULL; | state->head->name = Z_NULL; | |||
state->length = 0; | state->length = 0; | |||
state->mode = COMMENT; | state->mode = COMMENT; | |||
case COMMENT: | case COMMENT: | |||
if (state->flags & 0x1000) { | if (state->flags & 0x1000) { | |||
if (have == 0) goto inf_leave; | if (have == 0) goto inf_leave; | |||
copy = 0; | copy = 0; | |||
do { | do { | |||
len = (unsigned)(next[copy++]); | len = (unsigned)(next[copy++]); | |||
if (state->head != Z_NULL && | if (state->head != Z_NULL && | |||
state->head->comment != Z_NULL && | state->head->comment != Z_NULL && | |||
state->length < state->head->comm_max) | state->length < state->head->comm_max) | |||
state->head->comment[state->length++] = len; | state->head->comment[state->length++] = (Bytef)len; | |||
} while (len && copy < have); | } while (len && copy < have); | |||
if (state->flags & 0x0200) | if ((state->flags & 0x0200) && (state->wrap & 4)) | |||
state->check = crc32(state->check, next, copy); | state->check = crc32(state->check, next, copy); | |||
have -= copy; | have -= copy; | |||
next += copy; | next += copy; | |||
if (len) goto inf_leave; | if (len) goto inf_leave; | |||
} | } | |||
else if (state->head != Z_NULL) | else if (state->head != Z_NULL) | |||
state->head->comment = Z_NULL; | state->head->comment = Z_NULL; | |||
state->mode = HCRC; | state->mode = HCRC; | |||
case HCRC: | case HCRC: | |||
if (state->flags & 0x0200) { | if (state->flags & 0x0200) { | |||
NEEDBITS(16); | NEEDBITS(16); | |||
if (hold != (state->check & 0xffff)) { | if ((state->wrap & 4) && hold != (state->check & 0xffff)) { | |||
strm->msg = (char *)"header crc mismatch"; | strm->msg = (char *)"header crc mismatch"; | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
INITBITS(); | INITBITS(); | |||
} | } | |||
if (state->head != Z_NULL) { | if (state->head != Z_NULL) { | |||
state->head->hcrc = (int)((state->flags >> 9) & 1); | state->head->hcrc = (int)((state->flags >> 9) & 1); | |||
state->head->done = 1; | state->head->done = 1; | |||
} | } | |||
skipping to change at line 1180 | skipping to change at line 1203 | |||
*put++ = (unsigned char)(state->length); | *put++ = (unsigned char)(state->length); | |||
left--; | left--; | |||
state->mode = LEN; | state->mode = LEN; | |||
break; | break; | |||
case CHECK: | case CHECK: | |||
if (state->wrap) { | if (state->wrap) { | |||
NEEDBITS(32); | NEEDBITS(32); | |||
out -= left; | out -= left; | |||
strm->total_out += out; | strm->total_out += out; | |||
state->total += out; | state->total += out; | |||
if (out) | if ((state->wrap & 4) && out) | |||
strm->adler = state->check = | strm->adler = state->check = | |||
UPDATE(state->check, put - out, out); | UPDATE(state->check, put - out, out); | |||
out = left; | out = left; | |||
if (( | if ((state->wrap & 4) && ( | |||
#ifdef GUNZIP | #ifdef GUNZIP | |||
state->flags ? hold : | state->flags ? hold : | |||
#endif | #endif | |||
ZSWAP32(hold)) != state->check) { | ZSWAP32(hold)) != state->check) { | |||
strm->msg = (char *)"incorrect data check"; | strm->msg = (char *)"incorrect data check"; | |||
state->mode = BAD; | state->mode = BAD; | |||
break; | break; | |||
} | } | |||
INITBITS(); | INITBITS(); | |||
Tracev((stderr, "inflate: check matches trailer\n")); | Tracev((stderr, "inflate: check matches trailer\n")); | |||
skipping to change at line 1243 | skipping to change at line 1266 | |||
(state->mode < CHECK || flush != Z_FINISH))) | (state->mode < CHECK || flush != Z_FINISH))) | |||
if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { | if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { | |||
state->mode = MEM; | state->mode = MEM; | |||
return Z_MEM_ERROR; | return Z_MEM_ERROR; | |||
} | } | |||
in -= strm->avail_in; | in -= strm->avail_in; | |||
out -= strm->avail_out; | out -= strm->avail_out; | |||
strm->total_in += in; | strm->total_in += in; | |||
strm->total_out += out; | strm->total_out += out; | |||
state->total += out; | state->total += out; | |||
if (state->wrap && out) | if ((state->wrap & 4) && out) | |||
strm->adler = state->check = | strm->adler = state->check = | |||
UPDATE(state->check, strm->next_out - out, out); | UPDATE(state->check, strm->next_out - out, out); | |||
strm->data_type = state->bits + (state->last ? 64 : 0) + | strm->data_type = (int)state->bits + (state->last ? 64 : 0) + | |||
(state->mode == TYPE ? 128 : 0) + | (state->mode == TYPE ? 128 : 0) + | |||
(state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); | (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); | |||
if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) | if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) | |||
ret = Z_BUF_ERROR; | ret = Z_BUF_ERROR; | |||
return ret; | return ret; | |||
} | } | |||
int ZEXPORT inflateEnd(strm) | int ZEXPORT inflateEnd(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) | if (inflateStateCheck(strm)) | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if (state->window != Z_NULL) ZFREE(strm, state->window); | if (state->window != Z_NULL) ZFREE(strm, state->window); | |||
ZFREE(strm, strm->state); | ZFREE(strm, strm->state); | |||
strm->state = Z_NULL; | strm->state = Z_NULL; | |||
Tracev((stderr, "inflate: end\n")); | Tracev((stderr, "inflate: end\n")); | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) | int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) | |||
z_streamp strm; | z_streamp strm; | |||
Bytef *dictionary; | Bytef *dictionary; | |||
uInt *dictLength; | uInt *dictLength; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
/* check state */ | /* check state */ | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
/* copy dictionary */ | /* copy dictionary */ | |||
if (state->whave && dictionary != Z_NULL) { | if (state->whave && dictionary != Z_NULL) { | |||
zmemcpy(dictionary, state->window + state->wnext, | zmemcpy(dictionary, state->window + state->wnext, | |||
state->whave - state->wnext); | state->whave - state->wnext); | |||
zmemcpy(dictionary + state->whave - state->wnext, | zmemcpy(dictionary + state->whave - state->wnext, | |||
state->window, state->wnext); | state->window, state->wnext); | |||
} | } | |||
if (dictLength != Z_NULL) | if (dictLength != Z_NULL) | |||
skipping to change at line 1301 | skipping to change at line 1324 | |||
int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) | int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) | |||
z_streamp strm; | z_streamp strm; | |||
const Bytef *dictionary; | const Bytef *dictionary; | |||
uInt dictLength; | uInt dictLength; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
unsigned long dictid; | unsigned long dictid; | |||
int ret; | int ret; | |||
/* check state */ | /* check state */ | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if (state->wrap != 0 && state->mode != DICT) | if (state->wrap != 0 && state->mode != DICT) | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
/* check for correct dictionary identifier */ | /* check for correct dictionary identifier */ | |||
if (state->mode == DICT) { | if (state->mode == DICT) { | |||
dictid = adler32(0L, Z_NULL, 0); | dictid = adler32(0L, Z_NULL, 0); | |||
dictid = adler32(dictid, dictionary, dictLength); | dictid = adler32(dictid, dictionary, dictLength); | |||
if (dictid != state->check) | if (dictid != state->check) | |||
return Z_DATA_ERROR; | return Z_DATA_ERROR; | |||
skipping to change at line 1333 | skipping to change at line 1356 | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
int ZEXPORT inflateGetHeader(strm, head) | int ZEXPORT inflateGetHeader(strm, head) | |||
z_streamp strm; | z_streamp strm; | |||
gz_headerp head; | gz_headerp head; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
/* check state */ | /* check state */ | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; | if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; | |||
/* save header structure */ | /* save header structure */ | |||
state->head = head; | state->head = head; | |||
head->done = 0; | head->done = 0; | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
/* | /* | |||
skipping to change at line 1386 | skipping to change at line 1409 | |||
int ZEXPORT inflateSync(strm) | int ZEXPORT inflateSync(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
unsigned len; /* number of bytes to look at or looked at */ | unsigned len; /* number of bytes to look at or looked at */ | |||
unsigned long in, out; /* temporary to save total_in and total_out */ | unsigned long in, out; /* temporary to save total_in and total_out */ | |||
unsigned char buf[4]; /* to restore bit buffer to byte string */ | unsigned char buf[4]; /* to restore bit buffer to byte string */ | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
/* check parameters */ | /* check parameters */ | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; | if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; | |||
/* if first time, start search in bit buffer */ | /* if first time, start search in bit buffer */ | |||
if (state->mode != SYNC) { | if (state->mode != SYNC) { | |||
state->mode = SYNC; | state->mode = SYNC; | |||
state->hold <<= state->bits & 7; | state->hold <<= state->bits & 7; | |||
state->bits -= state->bits & 7; | state->bits -= state->bits & 7; | |||
len = 0; | len = 0; | |||
while (state->bits >= 8) { | while (state->bits >= 8) { | |||
skipping to change at line 1433 | skipping to change at line 1456 | |||
implementation to provide an additional safety check. PPP uses | implementation to provide an additional safety check. PPP uses | |||
Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored | Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored | |||
block. When decompressing, PPP checks that at the end of input packet, | block. When decompressing, PPP checks that at the end of input packet, | |||
inflate is waiting for these length bytes. | inflate is waiting for these length bytes. | |||
*/ | */ | |||
int ZEXPORT inflateSyncPoint(strm) | int ZEXPORT inflateSyncPoint(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
return state->mode == STORED && state->bits == 0; | return state->mode == STORED && state->bits == 0; | |||
} | } | |||
int ZEXPORT inflateCopy(dest, source) | int ZEXPORT inflateCopy(dest, source) | |||
z_streamp dest; | z_streamp dest; | |||
z_streamp source; | z_streamp source; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
struct inflate_state FAR *copy; | struct inflate_state FAR *copy; | |||
unsigned char FAR *window; | unsigned char FAR *window; | |||
unsigned wsize; | unsigned wsize; | |||
/* check input */ | /* check input */ | |||
if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || | if (inflateStateCheck(source) || dest == Z_NULL) | |||
source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) | ||||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)source->state; | state = (struct inflate_state FAR *)source->state; | |||
/* allocate space */ | /* allocate space */ | |||
copy = (struct inflate_state FAR *) | copy = (struct inflate_state FAR *) | |||
ZALLOC(source, 1, sizeof(struct inflate_state)); | ZALLOC(source, 1, sizeof(struct inflate_state)); | |||
if (copy == Z_NULL) return Z_MEM_ERROR; | if (copy == Z_NULL) return Z_MEM_ERROR; | |||
window = Z_NULL; | window = Z_NULL; | |||
if (state->window != Z_NULL) { | if (state->window != Z_NULL) { | |||
window = (unsigned char FAR *) | window = (unsigned char FAR *) | |||
ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); | ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); | |||
if (window == Z_NULL) { | if (window == Z_NULL) { | |||
ZFREE(source, copy); | ZFREE(source, copy); | |||
return Z_MEM_ERROR; | return Z_MEM_ERROR; | |||
} | } | |||
} | } | |||
/* copy state */ | /* copy state */ | |||
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); | zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); | |||
zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); | zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); | |||
copy->strm = dest; | ||||
if (state->lencode >= state->codes && | if (state->lencode >= state->codes && | |||
state->lencode <= state->codes + ENOUGH - 1) { | state->lencode <= state->codes + ENOUGH - 1) { | |||
copy->lencode = copy->codes + (state->lencode - state->codes); | copy->lencode = copy->codes + (state->lencode - state->codes); | |||
copy->distcode = copy->codes + (state->distcode - state->codes); | copy->distcode = copy->codes + (state->distcode - state->codes); | |||
} | } | |||
copy->next = copy->codes + (state->next - state->codes); | copy->next = copy->codes + (state->next - state->codes); | |||
if (window != Z_NULL) { | if (window != Z_NULL) { | |||
wsize = 1U << state->wbits; | wsize = 1U << state->wbits; | |||
zmemcpy(window, state->window, wsize); | zmemcpy(window, state->window, wsize); | |||
} | } | |||
skipping to change at line 1491 | skipping to change at line 1514 | |||
dest->state = (struct internal_state FAR *)copy; | dest->state = (struct internal_state FAR *)copy; | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
int ZEXPORT inflateUndermine(strm, subvert) | int ZEXPORT inflateUndermine(strm, subvert) | |||
z_streamp strm; | z_streamp strm; | |||
int subvert; | int subvert; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | |||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
state->sane = !subvert; | ||||
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR | |||
state->sane = !subvert; | ||||
return Z_OK; | return Z_OK; | |||
#else | #else | |||
(void)subvert; | ||||
state->sane = 1; | state->sane = 1; | |||
return Z_DATA_ERROR; | return Z_DATA_ERROR; | |||
#endif | #endif | |||
} | } | |||
int ZEXPORT inflateValidate(strm, check) | ||||
z_streamp strm; | ||||
int check; | ||||
{ | ||||
struct inflate_state FAR *state; | ||||
if (inflateStateCheck(strm)) return Z_STREAM_ERROR; | ||||
state = (struct inflate_state FAR *)strm->state; | ||||
if (check) | ||||
state->wrap |= 4; | ||||
else | ||||
state->wrap &= ~4; | ||||
return Z_OK; | ||||
} | ||||
long ZEXPORT inflateMark(strm) | long ZEXPORT inflateMark(strm) | |||
z_streamp strm; | z_streamp strm; | |||
{ | { | |||
struct inflate_state FAR *state; | struct inflate_state FAR *state; | |||
if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; | if (inflateStateCheck(strm)) | |||
return -(1L << 16); | ||||
state = (struct inflate_state FAR *)strm->state; | state = (struct inflate_state FAR *)strm->state; | |||
return ((long)(state->back) << 16) + | return (long)(((unsigned long)((long)state->back)) << 16) + | |||
(state->mode == COPY ? state->length : | (state->mode == COPY ? state->length : | |||
(state->mode == MATCH ? state->was - state->length : 0)); | (state->mode == MATCH ? state->was - state->length : 0)); | |||
} | } | |||
unsigned long ZEXPORT inflateCodesUsed(strm) | ||||
z_streamp strm; | ||||
{ | ||||
struct inflate_state FAR *state; | ||||
if (inflateStateCheck(strm)) return (unsigned long)-1; | ||||
state = (struct inflate_state FAR *)strm->state; | ||||
return (unsigned long)(state->next - state->codes); | ||||
} | ||||
End of changes. 45 change blocks. | ||||
37 lines changed or deleted | 77 lines changed or added |