summaryrefslogtreecommitdiff
path: root/inftrees.c
diff options
context:
space:
mode:
Diffstat (limited to 'inftrees.c')
-rw-r--r--inftrees.c77
1 files changed, 38 insertions, 39 deletions
diff --git a/inftrees.c b/inftrees.c
index ab9d74f..11967c8 100644
--- a/inftrees.c
+++ b/inftrees.c
@@ -1,11 +1,18 @@
/* inftrees.c -- generate Huffman trees for efficient decoding
- * Copyright (C) 1995 Mark Adler
+ * Copyright (C) 1995-1996 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "inftrees.h"
+char inflate_copyright[] = " inflate 1.0 Copyright 1995-1996 Mark Adler ";
+/*
+ If you use the zlib library in a product, an acknowledgment is welcome
+ in the documentation of your product. If for some reason you cannot
+ include such an acknowledgment, I would appreciate that you keep this
+ copyright string in the executable of your product.
+ */
struct internal_state {int dummy;}; /* for buggy compilers */
/* simplify the use of the inflate_huft type with some defines */
@@ -30,23 +37,19 @@ local voidpf falloc OF((
uInt, /* number of items */
uInt)); /* size of item */
-local void ffree OF((
- voidpf q, /* opaque pointer (not used) */
- voidpf p)); /* what to free (not used) */
-
/* Tables for deflate from PKZIP's appnote.txt. */
-local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
+local uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
/* actually lengths - 2; also see note #13 above about 258 */
-local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
+local uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
-local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
+local uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577};
-local uInt cpdext[] = { /* Extra bits for distance codes */
+local uInt cpdext[30] = { /* Extra bits for distance codes */
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13};
@@ -304,11 +307,11 @@ z_stream *z; /* for zfree function */
r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
if (r == Z_DATA_ERROR)
- z->msg = "oversubscribed dynamic bit lengths tree";
+ z->msg = (char*)"oversubscribed dynamic bit lengths tree";
else if (r == Z_BUF_ERROR)
{
inflate_trees_free(*tb, z);
- z->msg = "incomplete dynamic bit lengths tree";
+ z->msg = (char*)"incomplete dynamic bit lengths tree";
r = Z_DATA_ERROR;
}
return r;
@@ -331,11 +334,11 @@ z_stream *z; /* for zfree function */
if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
{
if (r == Z_DATA_ERROR)
- z->msg = "oversubscribed literal/length tree";
+ z->msg = (char*)"oversubscribed literal/length tree";
else if (r == Z_BUF_ERROR)
{
inflate_trees_free(*tl, z);
- z->msg = "incomplete literal/length tree";
+ z->msg = (char*)"incomplete literal/length tree";
r = Z_DATA_ERROR;
}
return r;
@@ -345,14 +348,14 @@ z_stream *z; /* for zfree function */
if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
{
if (r == Z_DATA_ERROR)
- z->msg = "oversubscribed literal/length tree";
+ z->msg = (char*)"oversubscribed literal/length tree";
else if (r == Z_BUF_ERROR) {
#ifdef PKZIP_BUG_WORKAROUND
r = Z_OK;
}
#else
inflate_trees_free(*td, z);
- z->msg = "incomplete literal/length tree";
+ z->msg = (char*)"incomplete literal/length tree";
r = Z_DATA_ERROR;
}
inflate_trees_free(*tl, z);
@@ -366,10 +369,8 @@ z_stream *z; /* for zfree function */
/* build fixed tables only once--keep them here */
-local int fixed_lock = 0;
local int fixed_built = 0;
#define FIXEDH 530 /* number of hufts used by fixed tables */
-local uInt fixed_left = FIXEDH;
local inflate_huft fixed_mem[FIXEDH];
local uInt fixed_bl;
local uInt fixed_bd;
@@ -378,24 +379,14 @@ local inflate_huft *fixed_td;
local voidpf falloc(q, n, s)
-voidpf q; /* opaque pointer (not used) */
+voidpf q; /* opaque pointer */
uInt n; /* number of items */
uInt s; /* size of item */
{
- Assert(s == sizeof(inflate_huft) && n <= fixed_left,
+ Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
"inflate_trees falloc overflow");
- if (q) s++; /* to make some compilers happy */
- fixed_left -= n;
- return (voidpf)(fixed_mem + fixed_left);
-}
-
-
-local void ffree(q, p)
-voidpf q;
-voidpf p;
-{
- Assert(0, "inflate_trees ffree called!");
- if (q) q = p; /* to make some compilers happy */
+ *(intf *)q -= n+s-s; /* s-s to avoid warning */
+ return (voidpf)(fixed_mem + *(intf *)q);
}
@@ -405,19 +396,18 @@ uIntf *bd; /* distance desired/actual bit depth */
inflate_huft * FAR *tl; /* literal/length tree result */
inflate_huft * FAR *td; /* distance tree result */
{
- /* build fixed tables if not built already--lock out other instances */
- while (++fixed_lock > 1)
- fixed_lock--;
+ /* build fixed tables if not already (multiple overlapped executions ok) */
if (!fixed_built)
{
int k; /* temporary variable */
unsigned c[288]; /* length list for huft_build */
z_stream z; /* for falloc function */
+ int f = FIXEDH; /* number of hufts left in fixed_mem */
/* set up fake z_stream for memory routines */
z.zalloc = falloc;
- z.zfree = ffree;
- z.opaque = Z_NULL;
+ z.zfree = Z_NULL;
+ z.opaque = (voidpf)&f;
/* literal table */
for (k = 0; k < 144; k++)
@@ -438,9 +428,9 @@ inflate_huft * FAR *td; /* distance tree result */
huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
/* done */
+ Assert(f == 0, "invalid build of fixed tables");
fixed_built = 1;
}
- fixed_lock--;
*bl = fixed_bl;
*bd = fixed_bd;
*tl = fixed_tl;
@@ -456,10 +446,19 @@ z_stream *z; /* for zfree function */
list of the tables it made, with the links in a dummy first entry of
each table. */
{
- register inflate_huft *p, *q;
+ register inflate_huft *p, *q, *r;
+ /* Reverse linked list */
+ p = Z_NULL;
+ q = t;
+ while (q != Z_NULL)
+ {
+ r = (q - 1)->next;
+ (q - 1)->next = p;
+ p = q;
+ q = r;
+ }
/* Go through linked list, freeing from the malloced (t[-1]) address. */
- p = t;
while (p != Z_NULL)
{
q = (--p)->next;