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