2011-09-10 01:36:31 -04:00
|
|
|
/* inflate.c -- zlib interface to inflate modules
|
|
|
|
* Copyright (C) 1995 Mark Adler
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
/* inflate private state */
|
|
|
|
struct internal_state {
|
|
|
|
|
|
|
|
/* mode */
|
|
|
|
enum {
|
|
|
|
METHOD, /* waiting for method byte */
|
|
|
|
FLAG, /* waiting for flag byte */
|
|
|
|
START, /* make new blocks state */
|
|
|
|
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 */
|
2011-09-10 02:03:14 -04:00
|
|
|
INF_ERROR}/* got an error--stay here */
|
2011-09-10 01:36:31 -04:00
|
|
|
mode; /* current inflate mode */
|
|
|
|
|
|
|
|
/* mode dependent information */
|
|
|
|
union {
|
|
|
|
uInt method; /* if FLAGS, method byte */
|
|
|
|
struct inflate_blocks_state
|
|
|
|
*blocks; /* if BLOCKS, current state */
|
|
|
|
struct {
|
|
|
|
uLong was; /* computed check value */
|
|
|
|
uLong need; /* stream check value */
|
|
|
|
} check; /* if CHECK, check values to compare */
|
|
|
|
} sub; /* submode */
|
2011-09-10 01:52:17 -04:00
|
|
|
|
|
|
|
/* mode independent information */
|
|
|
|
int nowrap; /* flag for no wrapper */
|
|
|
|
uInt wbits; /* log2(window size) (8..15, defaults to 15) */
|
|
|
|
|
2011-09-10 01:36:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-10 01:52:17 -04:00
|
|
|
int inflateInit(z)
|
|
|
|
z_stream *z;
|
2011-09-10 01:36:31 -04:00
|
|
|
{
|
2011-09-10 01:52:17 -04:00
|
|
|
return inflateInit2(z, WBITS);
|
2011-09-10 01:36:31 -04:00
|
|
|
}
|
|
|
|
|
2011-09-10 01:52:17 -04:00
|
|
|
|
|
|
|
int inflateInit2(z, w)
|
2011-09-10 01:36:31 -04:00
|
|
|
z_stream *z;
|
2011-09-10 01:52:17 -04:00
|
|
|
int w;
|
2011-09-10 01:36:31 -04:00
|
|
|
{
|
2011-09-10 01:52:17 -04:00
|
|
|
/* initialize state */
|
2011-09-10 01:36:31 -04:00
|
|
|
if (z == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
if (z->zalloc == Z_NULL) z->zalloc = zcalloc;
|
|
|
|
if (z->zfree == Z_NULL) z->zfree = zcfree;
|
|
|
|
z->total_in = z->total_out = 0;
|
|
|
|
z->msg = Z_NULL;
|
|
|
|
if ((z->state = (struct internal_state *)
|
|
|
|
ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
|
|
|
|
return Z_MEM_ERROR;
|
|
|
|
z->state->mode = METHOD;
|
|
|
|
|
2011-09-10 01: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 01:36:31 -04:00
|
|
|
z->state->mode = START;
|
|
|
|
}
|
2011-09-10 01:52:17 -04:00
|
|
|
|
|
|
|
/* set window size */
|
|
|
|
if (w < 8 || w > 15)
|
|
|
|
{
|
2011-09-10 01:36:31 -04:00
|
|
|
inflateEnd(z);
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
}
|
2011-09-10 01:52:17 -04:00
|
|
|
z->state->wbits = w;
|
2011-09-10 01:36:31 -04:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
|
|
|
|
|
|
|
|
int inflate(z, f)
|
|
|
|
z_stream *z;
|
|
|
|
int f;
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
int r = f; /* to avoid warning about unused f */
|
2011-09-10 01:36:31 -04:00
|
|
|
uInt b;
|
|
|
|
uLong c;
|
|
|
|
|
|
|
|
if (z == Z_NULL || z->next_in == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
r = Z_BUF_ERROR;
|
|
|
|
while (1) switch (z->state->mode)
|
|
|
|
{
|
|
|
|
case METHOD:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
if (((z->state->sub.method = NEXTBYTE) & 0xf != DEFLATED))
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = INF_ERROR;
|
2011-09-10 01:36:31 -04:00
|
|
|
z->msg = "unknown compression method";
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
2011-09-10 01:52:17 -04:00
|
|
|
if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
|
2011-09-10 01:36:31 -04:00
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = INF_ERROR;
|
2011-09-10 01:36:31 -04:00
|
|
|
z->msg = "invalid window size";
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
z->state->mode = FLAG;
|
|
|
|
case FLAG:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
if ((b = NEXTBYTE) & 0x20)
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = INF_ERROR;
|
2011-09-10 01:36:31 -04:00
|
|
|
z->msg = "invalid reserved bit";
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
if (((z->state->sub.method << 8) + b) % 31)
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = INF_ERROR;
|
2011-09-10 01:36:31 -04:00
|
|
|
z->msg = "incorrect header check";
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
z->state->mode = START;
|
|
|
|
case START:
|
2011-09-10 01:52:17 -04:00
|
|
|
if ((z->state->sub.blocks = inflate_blocks_new(z,
|
|
|
|
z->state->nowrap ? Z_NULL : adler32,
|
|
|
|
1<< z->state->wbits)) == Z_NULL)
|
2011-09-10 01:36:31 -04:00
|
|
|
return Z_MEM_ERROR;
|
|
|
|
z->state->mode = BLOCKS;
|
|
|
|
case BLOCKS:
|
|
|
|
if ((r = inflate_blocks(z->state->sub.blocks, z, r)) != Z_STREAM_END)
|
|
|
|
return r;
|
2011-09-10 02:03:14 -04:00
|
|
|
inflate_blocks_free(z->state->sub.blocks, z, &c);
|
2011-09-10 01:52:17 -04:00
|
|
|
if (z->state->nowrap)
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = DONE;
|
2011-09-10 01:52:17 -04:00
|
|
|
break;
|
2011-09-10 01:36:31 -04:00
|
|
|
}
|
|
|
|
z->state->sub.check.was = c;
|
|
|
|
z->state->mode = CHECK4;
|
|
|
|
case CHECK4:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
z->state->sub.check.need = (uLong)NEXTBYTE << 24;
|
|
|
|
z->state->mode = CHECK3;
|
|
|
|
case CHECK3:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
z->state->sub.check.need += (uLong)NEXTBYTE << 16;
|
|
|
|
z->state->mode = CHECK2;
|
|
|
|
case CHECK2:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
z->state->sub.check.need += (uLong)NEXTBYTE << 8;
|
|
|
|
z->state->mode = CHECK1;
|
|
|
|
case CHECK1:
|
|
|
|
if (z->avail_in == 0) return r; r = Z_OK;
|
|
|
|
z->state->sub.check.need += (uLong)NEXTBYTE;
|
|
|
|
if (z->state->sub.check.was != z->state->sub.check.need)
|
|
|
|
{
|
2011-09-10 02:03:14 -04:00
|
|
|
z->state->mode = INF_ERROR;
|
2011-09-10 01:36:31 -04:00
|
|
|
z->msg = "incorrect data check";
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
z->state->mode = DONE;
|
|
|
|
case DONE:
|
|
|
|
return Z_STREAM_END;
|
2011-09-10 02:03:14 -04:00
|
|
|
case INF_ERROR:
|
2011-09-10 01:36:31 -04:00
|
|
|
return Z_DATA_ERROR;
|
|
|
|
default:
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int inflateEnd(z)
|
|
|
|
z_stream *z;
|
|
|
|
{
|
|
|
|
uLong c;
|
|
|
|
|
|
|
|
if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
if (z->state->mode == BLOCKS)
|
2011-09-10 02:03:14 -04:00
|
|
|
inflate_blocks_free(z->state->sub.blocks, z, &c);
|
2011-09-10 01:36:31 -04:00
|
|
|
ZFREE(z, z->state);
|
|
|
|
z->state = Z_NULL;
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* inflateSync not implemented yet--this just consumes input */
|
|
|
|
int inflateSync(z)
|
|
|
|
z_stream *z;
|
|
|
|
{
|
|
|
|
if (z == Z_NULL) return Z_STREAM_ERROR;
|
|
|
|
if (z->avail_in == 0) return Z_BUF_ERROR;
|
|
|
|
do {
|
|
|
|
z->total_in++;
|
|
|
|
} while (--z->avail_in);
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* inflateReset not fully implemented yet--this frees and reallocates */
|
|
|
|
int inflateReset(z)
|
|
|
|
z_stream *z;
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = inflateEnd(z)) != Z_OK)
|
|
|
|
return r;
|
|
|
|
return inflateInit(z);
|
|
|
|
}
|