annotate lib/zlib/infblock.c @ 193:269ca53dae41

24.12.12
author Ritor1
date Mon, 24 Dec 2012 10:50:04 +0600
parents 8b8875f5b359
children
rev   line source
0
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
1 /* infblock.c -- interpret and process block types to last block
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
2 * Copyright (C) 1995-1998 Mark Adler
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
4 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
5
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
6 #include "zutil.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
7 #include "infblock.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
8 #include "inftrees.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
9 #include "infcodes.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
10 #include "infutil.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
11
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
12 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
13
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
14 /* simplify the use of the inflate_huft type with some defines */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
15 #define exop word.what.Exop
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
16 #define bits word.what.Bits
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
17
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
18 /* Table for deflate from PKZIP's appnote.txt. */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
19 local const uInt border[] = { /* Order of the bit length code lengths */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
20 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
21
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
22 /*
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
23 Notes beyond the 1.93a appnote.txt:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
24
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
25 1. Distance pointers never point before the beginning of the output
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
26 stream.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
27 2. Distance pointers can point back across blocks, up to 32k away.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
28 3. There is an implied maximum of 7 bits for the bit length table and
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
29 15 bits for the actual data.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
30 4. If only one code exists, then it is encoded using one bit. (Zero
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
31 would be more efficient, but perhaps a little confusing.) If two
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
32 codes exist, they are coded using one bit each (0 and 1).
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
33 5. There is no way of sending zero distance codes--a dummy must be
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
34 sent if there are none. (History: a pre 2.0 version of PKZIP would
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
35 store blocks with no distance codes, but this was discovered to be
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
36 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
37 zero distance codes, which is sent as one code of zero bits in
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
38 length.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
39 6. There are up to 286 literal/length codes. Code 256 represents the
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
40 end-of-block. Note however that the static length tree defines
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
41 288 codes just to fill out the Huffman codes. Codes 286 and 287
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
42 cannot be used though, since there is no length base or extra bits
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
43 defined for them. Similarily, there are up to 30 distance codes.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
44 However, static trees define 32 codes (all 5 bits) to fill out the
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
45 Huffman codes, but the last two had better not show up in the data.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
46 7. Unzip can check dynamic Huffman blocks for complete code sets.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
47 The exception is that a single code would not be complete (see #4).
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
48 8. The five bits following the block type is really the number of
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
49 literal codes sent minus 257.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
50 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
51 (1+6+6). Therefore, to output three times the length, you output
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
52 three codes (1+1+1), whereas to output four times the same length,
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
53 you only need two codes (1+3). Hmm.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
54 10. In the tree reconstruction algorithm, Code = Code + Increment
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
55 only if BitLength(i) is not zero. (Pretty obvious.)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
56 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
57 12. Note: length code 284 can represent 227-258, but length code 285
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
58 really is 258. The last length deserves its own, short code
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
59 since it gets used a lot in very redundant files. The length
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
60 258 is special since 258 - 3 (the min match length) is 255.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
61 13. The literal/length and distance code bit lengths are read as a
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
62 single stream of lengths. It is possible (and advantageous) for
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
63 a repeat code (16, 17, or 18) to go across the boundary between
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
64 the two sets of lengths.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
65 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
66
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
67
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
68 void inflate_blocks_reset(s, z, c)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
69 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
70 z_streamp z;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
71 uLongf *c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
72 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
73 if (c != Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
74 *c = s->check;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
75 if (s->mode == BTREE || s->mode == DTREE)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
76 ZFREE(z, s->sub.trees.blens);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
77 if (s->mode == CODES)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
78 inflate_codes_free(s->sub.decode.codes, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
79 s->mode = TYPE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
80 s->bitk = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
81 s->bitb = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
82 s->read = s->write = s->window;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
83 if (s->checkfn != Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
84 z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
85 Tracev((stderr, "inflate: blocks reset\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
86 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
87
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
88
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
89 inflate_blocks_statef *inflate_blocks_new(z, c, w)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
90 z_streamp z;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
91 check_func c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
92 uInt w;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
93 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
94 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
95
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
96 if ((s = (inflate_blocks_statef *)ZALLOC
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
97 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
98 return s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
99 if ((s->hufts =
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
100 (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
101 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
102 ZFREE(z, s);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
103 return Z_NULL;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
104 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
105 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
106 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
107 ZFREE(z, s->hufts);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
108 ZFREE(z, s);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
109 return Z_NULL;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
110 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
111 s->end = s->window + w;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
112 s->checkfn = c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
113 s->mode = TYPE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
114 Tracev((stderr, "inflate: blocks allocated\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
115 inflate_blocks_reset(s, z, Z_NULL);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
116 return s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
117 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
118
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
119
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
120 int inflate_blocks(s, z, r)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
121 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
122 z_streamp z;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
123 int r;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
124 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
125 uInt t; /* temporary storage */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
126 uLong b; /* bit buffer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
127 uInt k; /* bits in bit buffer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
128 Bytef *p; /* input data pointer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
129 uInt n; /* bytes available there */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
130 Bytef *q; /* output window write pointer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
131 uInt m; /* bytes to end of window or read pointer */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
132
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
133 /* copy input/output information to locals (UPDATE macro restores) */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
134 LOAD
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
135
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
136 /* process input based on current state */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
137 while (1) switch (s->mode)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
138 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
139 case TYPE:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
140 NEEDBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
141 t = (uInt)b & 7;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
142 s->last = t & 1;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
143 switch (t >> 1)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
144 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
145 case 0: /* stored */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
146 Tracev((stderr, "inflate: stored block%s\n",
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
147 s->last ? " (last)" : ""));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
148 DUMPBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
149 t = k & 7; /* go to byte boundary */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
150 DUMPBITS(t)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
151 s->mode = LENS; /* get length of stored block */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
152 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
153 case 1: /* fixed */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
154 Tracev((stderr, "inflate: fixed codes block%s\n",
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
155 s->last ? " (last)" : ""));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
156 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
157 uInt bl, bd;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
158 inflate_huft *tl, *td;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
159
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
160 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
161 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
162 if (s->sub.decode.codes == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
163 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
164 r = Z_MEM_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
165 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
166 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
167 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
168 DUMPBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
169 s->mode = CODES;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
170 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
171 case 2: /* dynamic */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
172 Tracev((stderr, "inflate: dynamic codes block%s\n",
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
173 s->last ? " (last)" : ""));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
174 DUMPBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
175 s->mode = TABLE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
176 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
177 case 3: /* illegal */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
178 DUMPBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
179 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
180 z->msg = (char*)"invalid block type";
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
181 r = Z_DATA_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
182 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
183 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
184 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
185 case LENS:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
186 NEEDBITS(32)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
187 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
188 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
189 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
190 z->msg = (char*)"invalid stored block lengths";
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
191 r = Z_DATA_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
192 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
193 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
194 s->sub.left = (uInt)b & 0xffff;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
195 b = k = 0; /* dump bits */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
196 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
197 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
198 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
199 case STORED:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
200 if (n == 0)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
201 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
202 NEEDOUT
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
203 t = s->sub.left;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
204 if (t > n) t = n;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
205 if (t > m) t = m;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
206 zmemcpy(q, p, t);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
207 p += t; n -= t;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
208 q += t; m -= t;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
209 if ((s->sub.left -= t) != 0)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
210 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
211 Tracev((stderr, "inflate: stored end, %lu total out\n",
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
212 z->total_out + (q >= s->read ? q - s->read :
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
213 (s->end - s->read) + (q - s->window))));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
214 s->mode = s->last ? DRY : TYPE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
215 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
216 case TABLE:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
217 NEEDBITS(14)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
218 s->sub.trees.table = t = (uInt)b & 0x3fff;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
219 #ifndef PKZIP_BUG_WORKAROUND
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
220 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
221 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
222 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
223 z->msg = (char*)"too many length or distance symbols";
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
224 r = Z_DATA_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
225 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
226 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
227 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
228 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
229 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
230 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
231 r = Z_MEM_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
232 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
233 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
234 DUMPBITS(14)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
235 s->sub.trees.index = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
236 Tracev((stderr, "inflate: table sizes ok\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
237 s->mode = BTREE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
238 case BTREE:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
239 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
240 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
241 NEEDBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
242 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
243 DUMPBITS(3)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
244 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
245 while (s->sub.trees.index < 19)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
246 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
247 s->sub.trees.bb = 7;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
248 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
249 &s->sub.trees.tb, s->hufts, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
250 if (t != Z_OK)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
251 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
252 ZFREE(z, s->sub.trees.blens);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
253 r = t;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
254 if (r == Z_DATA_ERROR)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
255 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
256 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
257 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
258 s->sub.trees.index = 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
259 Tracev((stderr, "inflate: bits tree ok\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
260 s->mode = DTREE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
261 case DTREE:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
262 while (t = s->sub.trees.table,
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
263 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
264 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
265 inflate_huft *h;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
266 uInt i, j, c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
267
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
268 t = s->sub.trees.bb;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
269 NEEDBITS(t)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
270 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
271 t = h->bits;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
272 c = h->base;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
273 if (c < 16)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
274 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
275 DUMPBITS(t)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
276 s->sub.trees.blens[s->sub.trees.index++] = c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
277 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
278 else /* c == 16..18 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
279 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
280 i = c == 18 ? 7 : c - 14;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
281 j = c == 18 ? 11 : 3;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
282 NEEDBITS(t + i)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
283 DUMPBITS(t)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
284 j += (uInt)b & inflate_mask[i];
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
285 DUMPBITS(i)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
286 i = s->sub.trees.index;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
287 t = s->sub.trees.table;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
288 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
289 (c == 16 && i < 1))
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
290 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
291 ZFREE(z, s->sub.trees.blens);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
292 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
293 z->msg = (char*)"invalid bit length repeat";
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
294 r = Z_DATA_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
295 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
296 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
297 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
298 do {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
299 s->sub.trees.blens[i++] = c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
300 } while (--j);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
301 s->sub.trees.index = i;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
302 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
303 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
304 s->sub.trees.tb = Z_NULL;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
305 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
306 uInt bl, bd;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
307 inflate_huft *tl, *td;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
308 inflate_codes_statef *c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
309
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
310 bl = 9; /* must be <= 9 for lookahead assumptions */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
311 bd = 6; /* must be <= 9 for lookahead assumptions */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
312 t = s->sub.trees.table;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
313 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
314 s->sub.trees.blens, &bl, &bd, &tl, &td,
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
315 s->hufts, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
316 ZFREE(z, s->sub.trees.blens);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
317 if (t != Z_OK)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
318 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
319 if (t == (uInt)Z_DATA_ERROR)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
320 s->mode = BAD;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
321 r = t;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
322 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
323 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
324 Tracev((stderr, "inflate: trees ok\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
325 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
326 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
327 r = Z_MEM_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
328 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
329 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
330 s->sub.decode.codes = c;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
331 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
332 s->mode = CODES;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
333 case CODES:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
334 UPDATE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
335 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
336 return inflate_flush(s, z, r);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
337 r = Z_OK;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
338 inflate_codes_free(s->sub.decode.codes, z);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
339 LOAD
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
340 Tracev((stderr, "inflate: codes end, %lu total out\n",
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
341 z->total_out + (q >= s->read ? q - s->read :
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
342 (s->end - s->read) + (q - s->window))));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
343 if (!s->last)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
344 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
345 s->mode = TYPE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
346 break;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
347 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
348 s->mode = DRY;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
349 case DRY:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
350 FLUSH
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
351 if (s->read != s->write)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
352 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
353 s->mode = DONE;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
354 case DONE:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
355 r = Z_STREAM_END;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
356 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
357 case BAD:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
358 r = Z_DATA_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
359 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
360 default:
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
361 r = Z_STREAM_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
362 LEAVE
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
363 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
364 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
365
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
366
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
367 int inflate_blocks_free(s, z)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
368 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
369 z_streamp z;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
370 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
371 inflate_blocks_reset(s, z, Z_NULL);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
372 ZFREE(z, s->window);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
373 ZFREE(z, s->hufts);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
374 ZFREE(z, s);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
375 Tracev((stderr, "inflate: blocks freed\n"));
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
376 return Z_OK;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
377 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
378
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
379
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
380 void inflate_set_dictionary(s, d, n)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
381 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
382 const Bytef *d;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
383 uInt n;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
384 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
385 zmemcpy(s->window, d, n);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
386 s->read = s->write = s->window + n;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
387 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
388
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
389
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
390 /* Returns true if inflate is currently at the end of a block generated
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
391 * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
392 * IN assertion: s != Z_NULL
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
393 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
394 int inflate_blocks_sync_point(s)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
395 inflate_blocks_statef *s;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
396 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
397 return s->mode == LENS;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
398 }