2005-02-27 01:58:54 +03:00
|
|
|
/* $NetBSD: inflate.c,v 1.6 2005/02/26 22:58:57 perry Exp $ */
|
1997-01-16 03:21:43 +03:00
|
|
|
|
|
|
|
/* inflate.c -- zlib interface to inflate modules
|
2002-03-12 03:42:23 +03:00
|
|
|
* Copyright (C) 1995-2002 Mark Adler
|
2005-02-27 01:58:54 +03:00
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
1997-01-16 03:21:43 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zutil.h"
|
|
|
|
#include "infblock.h"
|
|
|
|
|
|
|
|
struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
typedef enum {
|
1997-01-16 03:21:43 +03:00
|
|
|
BLOCKS, /* decompressing blocks */
|
|
|
|
DONE, /* finished check, done */
|
|
|
|
BAD} /* got an error--stay here */
|
1998-11-02 00:28:51 +03:00
|
|
|
inflate_mode;
|
|
|
|
|
|
|
|
/* inflate private state */
|
|
|
|
struct internal_state {
|
|
|
|
|
|
|
|
/* mode */
|
|
|
|
inflate_mode mode; /* current inflate mode */
|
1997-01-16 03:21:43 +03:00
|
|
|
|
|
|
|
/* mode independent information */
|
|
|
|
uInt wbits; /* log2(window size) (8..15, defaults to 15) */
|
2005-02-27 01:58:54 +03:00
|
|
|
inflate_blocks_statef
|
1997-01-16 03:21:43 +03:00
|
|
|
*blocks; /* current inflate_blocks state */
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
int ZEXPORT inflateReset(z)
|
1997-01-16 03:21:43 +03:00
|
|
|
z_streamp z;
|
|
|
|
{
|
|
|
|
if (z == Z_NULL || z->state == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
z->total_in = z->total_out = 0;
|
|
|
|
z->msg = Z_NULL;
|
2003-03-26 01:48:43 +03:00
|
|
|
z->state->mode = BLOCKS;
|
1998-11-02 00:28:51 +03:00
|
|
|
inflate_blocks_reset(z->state->blocks, z, Z_NULL);
|
|
|
|
Tracev((stderr, "inflate: reset\n"));
|
1997-01-16 03:21:43 +03:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
int ZEXPORT inflateEnd(z)
|
1997-01-16 03:21:43 +03:00
|
|
|
z_streamp z;
|
|
|
|
{
|
|
|
|
if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
if (z->state->blocks != Z_NULL)
|
1998-11-02 00:28:51 +03:00
|
|
|
inflate_blocks_free(z->state->blocks, z);
|
1997-01-16 03:21:43 +03:00
|
|
|
ZFREE(z, z->state);
|
|
|
|
z->state = Z_NULL;
|
1998-11-02 00:28:51 +03:00
|
|
|
Tracev((stderr, "inflate: end\n"));
|
1997-01-16 03:21:43 +03:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
int ZEXPORT inflateInit2_(z, w, version, stream_size)
|
1997-01-16 03:21:43 +03:00
|
|
|
z_streamp z;
|
|
|
|
int w;
|
|
|
|
const char *version;
|
|
|
|
int stream_size;
|
|
|
|
{
|
|
|
|
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
|
|
|
|
stream_size != sizeof(z_stream))
|
|
|
|
return Z_VERSION_ERROR;
|
|
|
|
|
|
|
|
/* initialize state */
|
|
|
|
if (z == Z_NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
z->msg = Z_NULL;
|
|
|
|
if (z->zalloc == Z_NULL)
|
|
|
|
{
|
|
|
|
z->zalloc = zcalloc;
|
|
|
|
z->opaque = (voidpf)0;
|
|
|
|
}
|
|
|
|
if (z->zfree == Z_NULL) z->zfree = zcfree;
|
|
|
|
if ((z->state = (struct internal_state FAR *)
|
|
|
|
ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
|
|
|
|
return Z_MEM_ERROR;
|
|
|
|
z->state->blocks = Z_NULL;
|
|
|
|
|
|
|
|
/* handle undocumented nowrap option (no zlib header or check) */
|
|
|
|
if (w < 0)
|
|
|
|
w = - w;
|
2003-03-26 01:48:43 +03:00
|
|
|
else
|
|
|
|
return Z_STREAM_ERROR;
|
1997-01-16 03:21:43 +03:00
|
|
|
|
|
|
|
/* set window size */
|
|
|
|
if (w < 8 || w > 15)
|
|
|
|
{
|
|
|
|
inflateEnd(z);
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
z->state->wbits = (uInt)w;
|
|
|
|
|
|
|
|
/* create inflate_blocks state */
|
|
|
|
if ((z->state->blocks =
|
2003-03-26 01:48:43 +03:00
|
|
|
inflate_blocks_new(z, Z_NULL, (uInt)1 << w))
|
1997-01-16 03:21:43 +03:00
|
|
|
== Z_NULL)
|
|
|
|
{
|
|
|
|
inflateEnd(z);
|
|
|
|
return Z_MEM_ERROR;
|
|
|
|
}
|
1998-11-02 00:28:51 +03:00
|
|
|
Tracev((stderr, "inflate: allocated\n"));
|
1997-01-16 03:21:43 +03:00
|
|
|
|
|
|
|
/* reset state */
|
|
|
|
inflateReset(z);
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
int ZEXPORT inflateInit_(z, version, stream_size)
|
1997-01-16 03:21:43 +03:00
|
|
|
z_streamp z;
|
|
|
|
const char *version;
|
|
|
|
int stream_size;
|
|
|
|
{
|
2003-03-26 01:48:43 +03:00
|
|
|
return inflateInit2_(z, -DEF_WBITS, version, stream_size);
|
1997-01-16 03:21:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
|
1997-01-16 03:21:43 +03:00
|
|
|
#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
int ZEXPORT inflate(z, f)
|
1997-01-16 03:21:43 +03:00
|
|
|
z_streamp z;
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
1998-11-02 00:28:51 +03:00
|
|
|
if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
|
1997-01-16 03:21:43 +03:00
|
|
|
return Z_STREAM_ERROR;
|
1998-11-02 00:28:51 +03:00
|
|
|
f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
|
1997-01-16 03:21:43 +03:00
|
|
|
r = Z_BUF_ERROR;
|
|
|
|
while (1) switch (z->state->mode)
|
|
|
|
{
|
|
|
|
case BLOCKS:
|
|
|
|
r = inflate_blocks(z->state->blocks, z, r);
|
|
|
|
if (r == Z_DATA_ERROR)
|
|
|
|
{
|
|
|
|
z->state->mode = BAD;
|
|
|
|
break;
|
|
|
|
}
|
1998-11-02 00:28:51 +03:00
|
|
|
if (r == Z_OK)
|
|
|
|
r = f;
|
1997-01-16 03:21:43 +03:00
|
|
|
if (r != Z_STREAM_END)
|
|
|
|
return r;
|
1998-11-02 00:28:51 +03:00
|
|
|
r = f;
|
2003-03-26 01:48:43 +03:00
|
|
|
inflate_blocks_reset(z->state->blocks, z, Z_NULL);
|
1997-01-16 03:21:43 +03:00
|
|
|
z->state->mode = DONE;
|
|
|
|
case DONE:
|
|
|
|
return Z_STREAM_END;
|
|
|
|
case BAD:
|
|
|
|
return Z_DATA_ERROR;
|
|
|
|
default:
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
}
|
1998-11-02 00:28:51 +03:00
|
|
|
#ifdef NEED_DUMMY_RETURN
|
|
|
|
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
|
|
|
|
#endif
|
1997-01-16 03:21:43 +03:00
|
|
|
}
|