zlib/inflate.c

367 lines
9.4 KiB
C
Raw Normal View History

2011-09-10 09:36:31 +04:00
/* inflate.c -- zlib interface to inflate modules
2011-09-10 10:17:33 +04:00
* Copyright (C) 1995-1998 Mark Adler
2011-09-10 09:36:31 +04:00
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "infblock.h"
struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
2011-09-10 10:17:33 +04:00
typedef enum {
2011-09-10 10:08:07 +04:00
METHOD, /* waiting for method byte */
FLAG, /* waiting for flag byte */
2011-09-10 10:14:39 +04:00
DICT4, /* four dictionary check bytes to go */
DICT3, /* three dictionary check bytes to go */
DICT2, /* two dictionary check bytes to go */
DICT1, /* one dictionary check byte to go */
DICT0, /* waiting for inflateSetDictionary */
2011-09-10 10:08:07 +04:00
BLOCKS, /* decompressing blocks */
CHECK4, /* four check bytes to go */
CHECK3, /* three check bytes to go */
CHECK2, /* two check bytes to go */
CHECK1, /* one check byte to go */
DONE, /* finished check, done */
BAD} /* got an error--stay here */
2011-09-10 10:17:33 +04:00
inflate_mode;
/* inflate private state */
struct internal_state {
/* mode */
inflate_mode mode; /* current inflate mode */
2011-09-10 09:36:31 +04:00
/* mode dependent information */
union {
2011-09-10 10:08:07 +04:00
uInt method; /* if FLAGS, method byte */
2011-09-10 09:36:31 +04:00
struct {
2011-09-10 10:06:52 +04:00
uLong was; /* computed check value */
uLong need; /* stream check value */
} check; /* if CHECK, check values to compare */
2011-09-10 10:08:07 +04:00
uInt marker; /* if BAD, inflateSync's marker bytes count */
} sub; /* submode */
2011-09-10 09:52:17 +04:00
/* mode independent information */
2011-09-10 10:08:07 +04:00
int nowrap; /* flag for no wrapper */
uInt wbits; /* log2(window size) (8..15, defaults to 15) */
2011-09-10 10:09:18 +04:00
inflate_blocks_statef
2011-09-10 10:08:07 +04:00
*blocks; /* current inflate_blocks state */
2011-09-10 09:52:17 +04:00
2011-09-10 09:36:31 +04:00
};
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateReset(z)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 09:36:31 +04:00
{
2011-09-10 10:06:52 +04:00
if (z == Z_NULL || z->state == Z_NULL)
return Z_STREAM_ERROR;
z->total_in = z->total_out = 0;
z->msg = Z_NULL;
z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
2011-09-10 10:17:33 +04:00
inflate_blocks_reset(z->state->blocks, z, Z_NULL);
Tracev((stderr, "inflate: reset\n"));
2011-09-10 10:06:52 +04:00
return Z_OK;
}
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateEnd(z)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 10:06:52 +04:00
{
if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
return Z_STREAM_ERROR;
if (z->state->blocks != Z_NULL)
2011-09-10 10:17:33 +04:00
inflate_blocks_free(z->state->blocks, z);
2011-09-10 10:06:52 +04:00
ZFREE(z, z->state);
z->state = Z_NULL;
2011-09-10 10:17:33 +04:00
Tracev((stderr, "inflate: end\n"));
2011-09-10 10:06:52 +04:00
return Z_OK;
2011-09-10 09:36:31 +04:00
}
2011-09-10 09:52:17 +04:00
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateInit2_(z, w, version, stream_size)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 09:52:17 +04:00
int w;
2011-09-10 10:14:39 +04:00
const char *version;
int stream_size;
2011-09-10 09:36:31 +04:00
{
2011-09-10 10:14:39 +04:00
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != sizeof(z_stream))
return Z_VERSION_ERROR;
2011-09-10 09:52:17 +04:00
/* initialize state */
2011-09-10 09:36:31 +04:00
if (z == Z_NULL)
return Z_STREAM_ERROR;
2011-09-10 10:11:37 +04:00
z->msg = Z_NULL;
if (z->zalloc == Z_NULL)
{
z->zalloc = zcalloc;
z->opaque = (voidpf)0;
}
2011-09-10 09:36:31 +04:00
if (z->zfree == Z_NULL) z->zfree = zcfree;
2011-09-10 10:09:18 +04:00
if ((z->state = (struct internal_state FAR *)
2011-09-10 09:36:31 +04:00
ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
return Z_MEM_ERROR;
2011-09-10 10:06:52 +04:00
z->state->blocks = Z_NULL;
2011-09-10 09:36:31 +04:00
2011-09-10 09:52:17 +04:00
/* handle undocumented nowrap option (no zlib header or check) */
z->state->nowrap = 0;
if (w < 0)
{
w = - w;
z->state->nowrap = 1;
2011-09-10 09:36:31 +04:00
}
2011-09-10 09:52:17 +04:00
/* set window size */
if (w < 8 || w > 15)
{
2011-09-10 09:36:31 +04:00
inflateEnd(z);
return Z_STREAM_ERROR;
}
2011-09-10 10:06:52 +04:00
z->state->wbits = (uInt)w;
/* create inflate_blocks state */
if ((z->state->blocks =
2011-09-10 10:14:39 +04:00
inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
2011-09-10 10:06:52 +04:00
== Z_NULL)
{
inflateEnd(z);
return Z_MEM_ERROR;
}
2011-09-10 10:17:33 +04:00
Tracev((stderr, "inflate: allocated\n"));
2011-09-10 10:06:52 +04:00
/* reset state */
inflateReset(z);
2011-09-10 09:36:31 +04:00
return Z_OK;
}
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateInit_(z, version, stream_size)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 10:14:39 +04:00
const char *version;
int stream_size;
2011-09-10 10:06:52 +04:00
{
2011-09-10 10:14:39 +04:00
return inflateInit2_(z, DEF_WBITS, version, stream_size);
2011-09-10 10:06:52 +04:00
}
2011-09-10 10:19:21 +04:00
#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
2011-09-10 09:36:31 +04:00
#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
2011-09-10 10:19:21 +04:00
int ZEXPORT inflate(z, f)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 09:36:31 +04:00
int f;
{
2011-09-10 10:14:39 +04:00
int r;
2011-09-10 09:36:31 +04:00
uInt b;
2011-09-10 10:19:21 +04:00
if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
2011-09-10 09:36:31 +04:00
return Z_STREAM_ERROR;
2011-09-10 10:19:21 +04:00
f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
2011-09-10 09:36:31 +04:00
r = Z_BUF_ERROR;
while (1) switch (z->state->mode)
{
case METHOD:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 10:11:37 +04:00
if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
2011-09-10 09:36:31 +04:00
{
2011-09-10 10:06:52 +04:00
z->state->mode = BAD;
2011-09-10 10:14:39 +04:00
z->msg = (char*)"unknown compression method";
2011-09-10 10:08:07 +04:00
z->state->sub.marker = 5; /* can't try inflateSync */
break;
2011-09-10 09:36:31 +04:00
}
2011-09-10 09:52:17 +04:00
if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
2011-09-10 09:36:31 +04:00
{
2011-09-10 10:06:52 +04:00
z->state->mode = BAD;
2011-09-10 10:14:39 +04:00
z->msg = (char*)"invalid window size";
2011-09-10 10:08:07 +04:00
z->state->sub.marker = 5; /* can't try inflateSync */
break;
2011-09-10 09:36:31 +04:00
}
z->state->mode = FLAG;
case FLAG:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 10:14:39 +04:00
b = NEXTBYTE;
2011-09-10 10:13:27 +04:00
if (((z->state->sub.method << 8) + b) % 31)
2011-09-10 10:11:37 +04:00
{
2011-09-10 10:13:27 +04:00
z->state->mode = BAD;
2011-09-10 10:14:39 +04:00
z->msg = (char*)"incorrect header check";
2011-09-10 10:13:27 +04:00
z->state->sub.marker = 5; /* can't try inflateSync */
break;
2011-09-10 10:11:37 +04:00
}
2011-09-10 10:17:33 +04:00
Tracev((stderr, "inflate: zlib header ok\n"));
2011-09-10 10:14:39 +04:00
if (!(b & PRESET_DICT))
{
z->state->mode = BLOCKS;
2011-09-10 10:19:21 +04:00
break;
2011-09-10 10:14:39 +04:00
}
z->state->mode = DICT4;
case DICT4:
NEEDBYTE
z->state->sub.check.need = (uLong)NEXTBYTE << 24;
z->state->mode = DICT3;
case DICT3:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 16;
z->state->mode = DICT2;
case DICT2:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 8;
z->state->mode = DICT1;
case DICT1:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE;
z->adler = z->state->sub.check.need;
z->state->mode = DICT0;
return Z_NEED_DICT;
case DICT0:
z->state->mode = BAD;
z->msg = (char*)"need dictionary";
z->state->sub.marker = 0; /* can try inflateSync */
return Z_STREAM_ERROR;
2011-09-10 09:36:31 +04:00
case BLOCKS:
2011-09-10 10:06:52 +04:00
r = inflate_blocks(z->state->blocks, z, r);
if (r == Z_DATA_ERROR)
{
z->state->mode = BAD;
2011-09-10 10:08:07 +04:00
z->state->sub.marker = 0; /* can try inflateSync */
break;
2011-09-10 10:06:52 +04:00
}
2011-09-10 10:19:21 +04:00
if (r == Z_OK)
r = f;
2011-09-10 10:06:52 +04:00
if (r != Z_STREAM_END)
2011-09-10 10:19:21 +04:00
return r;
2011-09-10 10:19:55 +04:00
r = f;
2011-09-10 10:06:52 +04:00
inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
2011-09-10 09:52:17 +04:00
if (z->state->nowrap)
{
2011-09-10 10:08:07 +04:00
z->state->mode = DONE;
break;
2011-09-10 09:36:31 +04:00
}
z->state->mode = CHECK4;
case CHECK4:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 09:36:31 +04:00
z->state->sub.check.need = (uLong)NEXTBYTE << 24;
z->state->mode = CHECK3;
case CHECK3:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 09:36:31 +04:00
z->state->sub.check.need += (uLong)NEXTBYTE << 16;
z->state->mode = CHECK2;
case CHECK2:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 09:36:31 +04:00
z->state->sub.check.need += (uLong)NEXTBYTE << 8;
z->state->mode = CHECK1;
case CHECK1:
2011-09-10 10:06:52 +04:00
NEEDBYTE
2011-09-10 09:36:31 +04:00
z->state->sub.check.need += (uLong)NEXTBYTE;
2011-09-10 10:06:52 +04:00
2011-09-10 09:36:31 +04:00
if (z->state->sub.check.was != z->state->sub.check.need)
{
2011-09-10 10:06:52 +04:00
z->state->mode = BAD;
2011-09-10 10:14:39 +04:00
z->msg = (char*)"incorrect data check";
2011-09-10 10:08:07 +04:00
z->state->sub.marker = 5; /* can't try inflateSync */
break;
2011-09-10 09:36:31 +04:00
}
2011-09-10 10:17:33 +04:00
Tracev((stderr, "inflate: zlib check ok\n"));
2011-09-10 09:36:31 +04:00
z->state->mode = DONE;
case DONE:
return Z_STREAM_END;
2011-09-10 10:06:52 +04:00
case BAD:
2011-09-10 09:36:31 +04:00
return Z_DATA_ERROR;
default:
return Z_STREAM_ERROR;
}
2011-09-10 10:17:33 +04:00
#ifdef NEED_DUMMY_RETURN
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
#endif
2011-09-10 09:36:31 +04:00
}
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 10:14:39 +04:00
const Bytef *dictionary;
uInt dictLength;
{
uInt length = dictLength;
if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
return Z_STREAM_ERROR;
2011-09-10 10:17:02 +04:00
2011-09-10 10:14:39 +04:00
if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
z->adler = 1L;
if (length >= ((uInt)1<<z->state->wbits))
{
length = (1<<z->state->wbits)-1;
dictionary += dictLength - length;
}
inflate_set_dictionary(z->state->blocks, dictionary, length);
z->state->mode = BLOCKS;
return Z_OK;
}
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateSync(z)
2011-09-10 10:17:02 +04:00
z_streamp z;
2011-09-10 09:36:31 +04:00
{
2011-09-10 10:08:07 +04:00
uInt n; /* number of bytes to look at */
2011-09-10 10:09:18 +04:00
Bytef *p; /* pointer to bytes */
2011-09-10 10:08:07 +04:00
uInt m; /* number of marker bytes found in a row */
uLong r, w; /* temporaries to save total_in and total_out */
2011-09-10 09:36:31 +04:00
2011-09-10 10:06:52 +04:00
/* set up */
if (z == Z_NULL || z->state == Z_NULL)
2011-09-10 09:36:31 +04:00
return Z_STREAM_ERROR;
2011-09-10 10:06:52 +04:00
if (z->state->mode != BAD)
2011-09-10 10:07:35 +04:00
{
z->state->mode = BAD;
2011-09-10 10:06:52 +04:00
z->state->sub.marker = 0;
2011-09-10 10:07:35 +04:00
}
2011-09-10 10:06:52 +04:00
if ((n = z->avail_in) == 0)
return Z_BUF_ERROR;
p = z->next_in;
m = z->state->sub.marker;
2011-09-10 09:36:31 +04:00
2011-09-10 10:06:52 +04:00
/* search */
while (n && m < 4)
{
2011-09-10 10:19:55 +04:00
static const Byte mark[4] = {0, 0, 0xff, 0xff};
if (*p == mark[m])
2011-09-10 10:06:52 +04:00
m++;
2011-09-10 10:07:35 +04:00
else if (*p)
2011-09-10 10:06:52 +04:00
m = 0;
2011-09-10 10:07:35 +04:00
else
m = 4 - m;
2011-09-10 10:06:52 +04:00
p++, n--;
}
2011-09-10 09:36:31 +04:00
2011-09-10 10:06:52 +04:00
/* restore */
z->total_in += p - z->next_in;
z->next_in = p;
z->avail_in = n;
z->state->sub.marker = m;
2011-09-10 09:36:31 +04:00
2011-09-10 10:06:52 +04:00
/* return no joy or set up to restart on a new block */
if (m != 4)
return Z_DATA_ERROR;
r = z->total_in; w = z->total_out;
inflateReset(z);
z->total_in = r; z->total_out = w;
z->state->mode = BLOCKS;
return Z_OK;
2011-09-10 09:36:31 +04:00
}
2011-09-10 10:17:33 +04:00
/* Returns true if inflate is currently at the end of a block generated
* by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
* implementation to provide an additional safety check. PPP uses 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, inflate is
* waiting for these length bytes.
*/
2011-09-10 10:19:21 +04:00
int ZEXPORT inflateSyncPoint(z)
2011-09-10 10:17:33 +04:00
z_streamp z;
{
if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
return Z_STREAM_ERROR;
return inflate_blocks_sync_point(z->state->blocks);
}