annotate src/zlib/inftrees.c @ 18:1424868939a8

Indenting
author darius
date Wed, 24 Dec 1997 12:38:35 +0000
parents 1040ca591f2e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
1 /* inftrees.c -- generate Huffman trees for efficient decoding
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
2 * Copyright (C) 1995 Mark Adler
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
4 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
5
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
6 #include "zutil.h"
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
7 #include "inftrees.h"
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
8
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
9 struct internal_state {int dummy;}; /* for buggy compilers */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
10
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
11 /* simplify the use of the inflate_huft type with some defines */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
12 #define base more.Base
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
13 #define next more.Next
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
14 #define exop word.what.Exop
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
15 #define bits word.what.Bits
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
16
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
17
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
18 local int huft_build __P((
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
19 uInt *, /* code lengths in bits */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
20 uInt, /* number of codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
21 uInt, /* number of "simple" codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
22 uInt *, /* list of base values for non-simple codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
23 uInt *, /* list of extra bits for non-simple codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
24 inflate_huft **, /* result: starting table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
25 uInt *, /* maximum lookup bits (returns actual) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
26 z_stream *)); /* for zalloc function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
27
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
28 local voidp falloc __P((
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
29 voidp, /* opaque pointer (not used) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
30 uInt, /* number of items */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
31 uInt)); /* size of item */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
32
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
33 local void ffree __P((
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
34 voidp q, /* opaque pointer (not used) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
35 voidp p)); /* what to free (not used) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
36
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
37 /* Tables for deflate from PKZIP's appnote.txt. */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
38 local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
39 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
40 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
41 /* actually lengths - 2; also see note #13 above about 258 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
42 local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
43 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
44 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 128, 128}; /* 128==invalid */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
45 local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
46 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
47 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
48 8193, 12289, 16385, 24577};
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
49 local uInt cpdext[] = { /* Extra bits for distance codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
50 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
51 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
52 12, 12, 13, 13};
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
53
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
54 /*
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
55 Huffman code decoding is performed using a multi-level table lookup.
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
56 The fastest way to decode is to simply build a lookup table whose
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
57 size is determined by the longest code. However, the time it takes
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
58 to build this table can also be a factor if the data being decoded
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
59 is not very long. The most common codes are necessarily the
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
60 shortest codes, so those codes dominate the decoding time, and hence
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
61 the speed. The idea is you can have a shorter table that decodes the
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
62 shorter, more probable codes, and then point to subsidiary tables for
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
63 the longer codes. The time it costs to decode the longer codes is
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
64 then traded against the time it takes to make longer tables.
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
65
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
66 This results of this trade are in the variables lbits and dbits
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
67 below. lbits is the number of bits the first level table for literal/
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
68 length codes can decode in one step, and dbits is the same thing for
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
69 the distance codes. Subsequent tables are also less than or equal to
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
70 those sizes. These values may be adjusted either when all of the
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
71 codes are shorter than that, in which case the longest code length in
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
72 bits is used, or when the shortest code is *longer* than the requested
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
73 table size, in which case the length of the shortest code in bits is
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
74 used.
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
75
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
76 There are two different values for the two tables, since they code a
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
77 different number of possibilities each. The literal/length table
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
78 codes 286 possible values, or in a flat code, a little over eight
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
79 bits. The distance table codes 30 possible values, or a little less
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
80 than five bits, flat. The optimum values for speed end up being
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
81 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
82 The optimum values may differ though from machine to machine, and
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
83 possibly even between compilers. Your mileage may vary.
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
84 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
85
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
86
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
87 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
88 #define BMAX 15 /* maximum bit length of any code */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
89 #define N_MAX 288 /* maximum number of codes in any set */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
90
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
91 #ifdef DEBUG
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
92 uInt inflate_hufts;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
93 #endif
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
94
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
95 local int huft_build(b, n, s, d, e, t, m, zs)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
96 uInt *b; /* code lengths in bits (all assumed <= BMAX) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
97 uInt n; /* number of codes (assumed <= N_MAX) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
98 uInt s; /* number of simple-valued codes (0..s-1) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
99 uInt *d; /* list of base values for non-simple codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
100 uInt *e; /* list of extra bits for non-simple codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
101 inflate_huft **t; /* result: starting table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
102 uInt *m; /* maximum lookup bits, returns actual */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
103 z_stream *zs; /* for zalloc function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
104 /* Given a list of code lengths and a maximum table size, make a set of
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
105 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
106 if the given code set is incomplete (the tables are still built in this
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
107 case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
108 over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
109 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
110 uInt a; /* counter for codes of length k */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
111 uInt c[BMAX+1]; /* bit length count table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
112 uInt f; /* i repeats in table every f entries */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
113 int g; /* maximum code length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
114 int h; /* table level */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
115 register uInt i; /* counter, current code */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
116 register uInt j; /* counter */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
117 register int k; /* number of bits in current code */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
118 int l; /* bits per table (returned in m) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
119 register uInt *p; /* pointer into c[], b[], or v[] */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
120 register inflate_huft *q; /* points to current table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
121 inflate_huft r; /* table entry for structure assignment */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
122 inflate_huft *u[BMAX]; /* table stack */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
123 uInt v[N_MAX]; /* values in order of bit length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
124 register int w; /* bits before this table == (l * h) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
125 uInt x[BMAX+1]; /* bit offsets, then code stack */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
126 uInt *xp; /* pointer into x */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
127 int y; /* number of dummy codes added */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
128 uInt z; /* number of entries in current table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
129
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
130
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
131 /* Generate counts for each bit length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
132 p = c;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
133 #define C0 *p++ = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
134 #define C2 C0 C0 C0 C0
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
135 #define C4 C2 C2 C2 C2
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
136 C4 /* clear c[]--assume BMAX+1 is 16 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
137 p = b; i = n;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
138 do {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
139 c[*p++]++; /* assume all entries <= BMAX */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
140 } while (--i);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
141 if (c[0] == n) /* null input--all zero length codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
142 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
143 *t = (inflate_huft *)Z_NULL;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
144 *m = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
145 return Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
146 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
147
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
148
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
149 /* Find minimum and maximum length, bound *m by those */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
150 l = *m;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
151 for (j = 1; j <= BMAX; j++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
152 if (c[j])
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
153 break;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
154 k = j; /* minimum code length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
155 if ((uInt)l < j)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
156 l = j;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
157 for (i = BMAX; i; i--)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
158 if (c[i])
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
159 break;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
160 g = i; /* maximum code length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
161 if ((uInt)l > i)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
162 l = i;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
163 *m = l;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
164
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
165
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
166 /* Adjust last length count to fill out codes, if needed */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
167 for (y = 1 << j; j < i; j++, y <<= 1)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
168 if ((y -= c[j]) < 0)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
169 return Z_DATA_ERROR;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
170 if ((y -= c[i]) < 0)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
171 return Z_DATA_ERROR;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
172 c[i] += y;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
173
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
174
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
175 /* Generate starting offsets into the value table for each length */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
176 x[1] = j = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
177 p = c + 1; xp = x + 2;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
178 while (--i) { /* note that i == g from above */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
179 *xp++ = (j += *p++);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
180 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
181
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
182
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
183 /* Make a table of values in order of bit lengths */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
184 p = b; i = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
185 do {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
186 if ((j = *p++) != 0)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
187 v[x[j]++] = i;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
188 } while (++i < n);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
189
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
190
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
191 /* Generate the Huffman codes and for each, make the table entries */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
192 x[0] = i = 0; /* first Huffman code is zero */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
193 p = v; /* grab values in bit order */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
194 h = -1; /* no tables yet--level -1 */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
195 w = -l; /* bits decoded == (l * h) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
196 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
197 q = (inflate_huft *)Z_NULL; /* ditto */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
198 z = 0; /* ditto */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
199
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
200 /* go through the bit lengths (k already is bits in shortest code) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
201 for (; k <= g; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
202 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
203 a = c[k];
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
204 while (a--)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
205 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
206 /* here i is the Huffman code of length k bits for value *p */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
207 /* make tables up to required level */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
208 while (k > w + l)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
209 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
210 h++;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
211 w += l; /* previous table always l bits */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
212
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
213 /* compute minimum size table less than or equal to l bits */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
214 z = (z = g - w) > (uInt)l ? l : z; /* table size upper limit */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
215 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
216 { /* too few codes for k-w bit table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
217 f -= a + 1; /* deduct codes from patterns left */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
218 xp = c + k;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
219 if (j < z)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
220 while (++j < z) /* try smaller tables up to z bits */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
221 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
222 if ((f <<= 1) <= *++xp)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
223 break; /* enough codes to use up j bits */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
224 f -= *xp; /* else deduct codes from patterns */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
225 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
226 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
227 z = 1 << j; /* table entries for j-bit table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
228
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
229 /* allocate and link in new table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
230 if ((q = (inflate_huft *)ZALLOC
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
231 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
232 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
233 if (h)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
234 inflate_trees_free(u[0], zs);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
235 return Z_MEM_ERROR; /* not enough memory */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
236 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
237 #ifdef DEBUG
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
238 inflate_hufts += z + 1;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
239 #endif
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
240 *t = q + 1; /* link to list for huft_free() */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
241 *(t = &(q->next)) = (inflate_huft *)Z_NULL;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
242 u[h] = ++q; /* table starts after link */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
243
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
244 /* connect to last table, if there is one */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
245 if (h)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
246 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
247 x[h] = i; /* save pattern for backing up */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
248 r.bits = (Byte)l; /* bits to dump before this table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
249 r.exop = -(Char)j; /* bits in this table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
250 r.next = q; /* pointer to this table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
251 j = i >> (w - l); /* (get around Turbo C bug) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
252 u[h-1][j] = r; /* connect to last table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
253 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
254 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
255
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
256 /* set up table entry in r */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
257 r.bits = (Byte)(k - w);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
258 if (p >= v + n)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
259 r.exop = (Char)(-128); /* out of values--invalid code */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
260 else if (*p < s)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
261 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
262 r.exop = (Char)(*p < 256 ? 16 : -64); /* 256 is end-of-block code */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
263 r.base = *p++; /* simple code is just the value */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
264 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
265 else
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
266 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
267 r.exop = (Char)e[*p - s]; /* non-simple--look up in lists */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
268 r.base = d[*p++ - s];
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
269 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
270
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
271 /* fill code-like entries with r */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
272 f = 1 << (k - w);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
273 for (j = i >> w; j < z; j += f)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
274 q[j] = r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
275
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
276 /* backwards increment the k-bit code i */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
277 for (j = 1 << (k - 1); i & j; j >>= 1)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
278 i ^= j;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
279 i ^= j;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
280
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
281 /* backup over finished tables */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
282 while ((i & ((1 << w) - 1)) != x[h])
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
283 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
284 h--; /* don't need to update q */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
285 w -= l;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
286 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
287 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
288 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
289
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
290
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
291 /* Return Z_BUF_ERROR if we were given an incomplete table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
292 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
293 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
294
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
295
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
296 int inflate_trees_bits(c, bb, tb, z)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
297 uInt *c; /* 19 code lengths */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
298 uInt *bb; /* bits tree desired/actual depth */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
299 inflate_huft **tb; /* bits tree result */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
300 z_stream *z; /* for zfree function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
301 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
302 int r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
303
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
304 r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL, tb, bb, z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
305 if (r == Z_DATA_ERROR)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
306 z->msg = "oversubscribed dynamic bit lengths tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
307 else if (r == Z_BUF_ERROR)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
308 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
309 inflate_trees_free(*tb, z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
310 z->msg = "incomplete dynamic bit lengths tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
311 r = Z_DATA_ERROR;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
312 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
313 return r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
314 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
315
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
316
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
317 int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
318 uInt nl; /* number of literal/length codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
319 uInt nd; /* number of distance codes */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
320 uInt *c; /* that many (total) code lengths */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
321 uInt *bl; /* literal desired/actual bit depth */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
322 uInt *bd; /* distance desired/actual bit depth */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
323 inflate_huft **tl; /* literal/length tree result */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
324 inflate_huft **td; /* distance tree result */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
325 z_stream *z; /* for zfree function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
326 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
327 int r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
328
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
329 /* build literal/length tree */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
330 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
331 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
332 if (r == Z_DATA_ERROR)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
333 z->msg = "oversubscribed literal/length tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
334 else if (r == Z_BUF_ERROR)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
335 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
336 inflate_trees_free(*tl, z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
337 z->msg = "incomplete literal/length tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
338 r = Z_DATA_ERROR;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
339 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
340 return r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
341 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
342
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
343 /* build distance tree */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
344 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
345 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
346 if (r == Z_DATA_ERROR)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
347 z->msg = "oversubscribed literal/length tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
348 else if (r == Z_BUF_ERROR) {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
349 #ifdef PKZIP_BUG_WORKAROUND
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
350 r = Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
351 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
352 #else
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
353 inflate_trees_free(*td, z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
354 z->msg = "incomplete literal/length tree";
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
355 r = Z_DATA_ERROR;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
356 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
357 inflate_trees_free(*tl, z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
358 return r;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
359 #endif
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
360 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
361
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
362 /* done */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
363 return Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
364 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
365
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
366
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
367 /* build fixed tables only once--keep them here */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
368 local int fixed_lock = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
369 local int fixed_built = 0;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
370 #define FIXEDH 530 /* number of hufts used by fixed tables */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
371 local uInt fixed_left = FIXEDH;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
372 local inflate_huft fixed_mem[FIXEDH];
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
373 local uInt fixed_bl;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
374 local uInt fixed_bd;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
375 local inflate_huft *fixed_tl;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
376 local inflate_huft *fixed_td;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
377
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
378
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
379 local voidp falloc(q, n, s)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
380 voidp q; /* opaque pointer (not used) */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
381 uInt n; /* number of items */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
382 uInt s; /* size of item */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
383 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
384 Assert(s == sizeof(inflate_huft) && n <= fixed_left,
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
385 "inflate_trees falloc overflow");
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
386 if (q) s++; /* to make some compilers happy */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
387 fixed_left -= n;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
388 return (voidp)(fixed_mem + fixed_left);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
389 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
390
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
391
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
392 local void ffree(q, p)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
393 voidp q;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
394 voidp p;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
395 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
396 Assert(0, "inflate_trees ffree called!");
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
397 if (q) q = p; /* to make some compilers happy */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
398 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
399
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
400
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
401 int inflate_trees_fixed(bl, bd, tl, td)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
402 uInt *bl; /* literal desired/actual bit depth */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
403 uInt *bd; /* distance desired/actual bit depth */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
404 inflate_huft **tl; /* literal/length tree result */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
405 inflate_huft **td; /* distance tree result */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
406 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
407 /* build fixed tables if not built already--lock out other instances */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
408 while (++fixed_lock > 1)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
409 fixed_lock--;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
410 if (!fixed_built)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
411 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
412 int k; /* temporary variable */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
413 unsigned c[288]; /* length list for huft_build */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
414 z_stream z; /* for falloc function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
415
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
416 /* set up fake z_stream for memory routines */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
417 z.zalloc = falloc;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
418 z.zfree = ffree;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
419 z.opaque = Z_NULL;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
420
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
421 /* literal table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
422 for (k = 0; k < 144; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
423 c[k] = 8;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
424 for (; k < 256; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
425 c[k] = 9;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
426 for (; k < 280; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
427 c[k] = 7;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
428 for (; k < 288; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
429 c[k] = 8;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
430 fixed_bl = 7;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
431 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
432
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
433 /* distance table */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
434 for (k = 0; k < 30; k++)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
435 c[k] = 5;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
436 fixed_bd = 5;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
437 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
438
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
439 /* done */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
440 fixed_built = 1;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
441 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
442 fixed_lock--;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
443 *bl = fixed_bl;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
444 *bd = fixed_bd;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
445 *tl = fixed_tl;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
446 *td = fixed_td;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
447 return Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
448 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
449
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
450
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
451 int inflate_trees_free(t, z)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
452 inflate_huft *t; /* table to free */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
453 z_stream *z; /* for zfree function */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
454 /* Free the malloc'ed tables built by huft_build(), which makes a linked
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
455 list of the tables it made, with the links in a dummy first entry of
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
456 each table. */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
457 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
458 register inflate_huft *p, *q;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
459
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
460 /* Don't free fixed trees */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
461 if (t >= fixed_mem && t <= fixed_mem + FIXEDH)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
462 return Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
463
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
464 /* Go through linked list, freeing from the malloced (t[-1]) address. */
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
465 p = t;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
466 while (p != Z_NULL)
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
467 {
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
468 q = (--p)->next;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
469 ZFREE(z,p);
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
470 p = q;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
471 }
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
472 return Z_OK;
1040ca591f2e First entry of Paradise Server 2.9 patch 10 Beta
darius
parents:
diff changeset
473 }