diff options
Diffstat (limited to 'inftrees.c')
-rw-r--r-- | inftrees.c | 161 |
1 files changed, 66 insertions, 95 deletions
@@ -7,7 +7,7 @@ | |||
7 | #include "inftrees.h" | 7 | #include "inftrees.h" |
8 | 8 | ||
9 | const char inflate_copyright[] = | 9 | const char inflate_copyright[] = |
10 | " inflate 1.0.8 Copyright 1995-1998 Mark Adler "; | 10 | " inflate 1.0.9 Copyright 1995-1998 Mark Adler "; |
11 | /* | 11 | /* |
12 | If you use the zlib library in a product, an acknowledgment is welcome | 12 | If you use the zlib library in a product, an acknowledgment is welcome |
13 | in the documentation of your product. If for some reason you cannot | 13 | in the documentation of your product. If for some reason you cannot |
@@ -31,12 +31,9 @@ local int huft_build OF(( | |||
31 | const uIntf *, /* list of extra bits for non-simple codes */ | 31 | const uIntf *, /* list of extra bits for non-simple codes */ |
32 | inflate_huft * FAR*,/* result: starting table */ | 32 | inflate_huft * FAR*,/* result: starting table */ |
33 | uIntf *, /* maximum lookup bits (returns actual) */ | 33 | uIntf *, /* maximum lookup bits (returns actual) */ |
34 | z_streamp )); /* for zalloc function */ | 34 | inflate_huft *, /* space for trees */ |
35 | 35 | uInt *, /* hufts used in space */ | |
36 | local voidpf falloc OF(( | 36 | uIntf * )); /* space for values */ |
37 | voidpf, /* opaque pointer (not used) */ | ||
38 | uInt, /* number of items */ | ||
39 | uInt)); /* size of item */ | ||
40 | 37 | ||
41 | /* Tables for deflate from PKZIP's appnote.txt. */ | 38 | /* Tables for deflate from PKZIP's appnote.txt. */ |
42 | local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */ | 39 | local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */ |
@@ -90,21 +87,18 @@ local const uInt cpdext[30] = { /* Extra bits for distance codes */ | |||
90 | 87 | ||
91 | /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ | 88 | /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ |
92 | #define BMAX 15 /* maximum bit length of any code */ | 89 | #define BMAX 15 /* maximum bit length of any code */ |
93 | #define N_MAX 288 /* maximum number of codes in any set */ | ||
94 | |||
95 | #ifdef DEBUG | ||
96 | uInt inflate_hufts; | ||
97 | #endif | ||
98 | 90 | ||
99 | local int huft_build(b, n, s, d, e, t, m, zs) | 91 | local int huft_build(b, n, s, d, e, t, m, hp, hn, v) |
100 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ | 92 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ |
101 | uInt n; /* number of codes (assumed <= N_MAX) */ | 93 | uInt n; /* number of codes (assumed <= 288) */ |
102 | uInt s; /* number of simple-valued codes (0..s-1) */ | 94 | uInt s; /* number of simple-valued codes (0..s-1) */ |
103 | const uIntf *d; /* list of base values for non-simple codes */ | 95 | const uIntf *d; /* list of base values for non-simple codes */ |
104 | const uIntf *e; /* list of extra bits for non-simple codes */ | 96 | const uIntf *e; /* list of extra bits for non-simple codes */ |
105 | inflate_huft * FAR *t; /* result: starting table */ | 97 | inflate_huft * FAR *t; /* result: starting table */ |
106 | uIntf *m; /* maximum lookup bits, returns actual */ | 98 | uIntf *m; /* maximum lookup bits, returns actual */ |
107 | z_streamp zs; /* for zalloc function */ | 99 | inflate_huft *hp; /* space for trees */ |
100 | uInt *hn; /* hufts used in space */ | ||
101 | uIntf *v; /* working area: values in order of bit length */ | ||
108 | /* Given a list of code lengths and a maximum table size, make a set of | 102 | /* Given a list of code lengths and a maximum table size, make a set of |
109 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR | 103 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR |
110 | if the given code set is incomplete (the tables are still built in this | 104 | if the given code set is incomplete (the tables are still built in this |
@@ -121,11 +115,11 @@ z_streamp zs; /* for zalloc function */ | |||
121 | register uInt j; /* counter */ | 115 | register uInt j; /* counter */ |
122 | register int k; /* number of bits in current code */ | 116 | register int k; /* number of bits in current code */ |
123 | int l; /* bits per table (returned in m) */ | 117 | int l; /* bits per table (returned in m) */ |
118 | uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */ | ||
124 | register uIntf *p; /* pointer into c[], b[], or v[] */ | 119 | register uIntf *p; /* pointer into c[], b[], or v[] */ |
125 | inflate_huft *q; /* points to current table */ | 120 | inflate_huft *q; /* points to current table */ |
126 | struct inflate_huft_s r; /* table entry for structure assignment */ | 121 | struct inflate_huft_s r; /* table entry for structure assignment */ |
127 | inflate_huft *u[BMAX]; /* table stack */ | 122 | inflate_huft *u[BMAX]; /* table stack */ |
128 | uInt v[N_MAX]; /* values in order of bit length */ | ||
129 | register int w; /* bits before this table == (l * h) */ | 123 | register int w; /* bits before this table == (l * h) */ |
130 | uInt x[BMAX+1]; /* bit offsets, then code stack */ | 124 | uInt x[BMAX+1]; /* bit offsets, then code stack */ |
131 | uIntf *xp; /* pointer into x */ | 125 | uIntf *xp; /* pointer into x */ |
@@ -233,20 +227,16 @@ z_streamp zs; /* for zalloc function */ | |||
233 | } | 227 | } |
234 | z = 1 << j; /* table entries for j-bit table */ | 228 | z = 1 << j; /* table entries for j-bit table */ |
235 | 229 | ||
236 | /* allocate and link in new table */ | 230 | /* allocate new table */ |
237 | if ((q = (inflate_huft *)ZALLOC | 231 | if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ |
238 | (zs,z + 1,sizeof(inflate_huft))) == Z_NULL) | ||
239 | { | ||
240 | if (h) | ||
241 | inflate_trees_free(u[0], zs); | ||
242 | return Z_MEM_ERROR; /* not enough memory */ | 232 | return Z_MEM_ERROR; /* not enough memory */ |
233 | u[h] = q = hp + *hn; | ||
234 | *hn += z; | ||
235 | if (t != Z_NULL) /* first table is returned result */ | ||
236 | { | ||
237 | *t = q; | ||
238 | t = Z_NULL; | ||
243 | } | 239 | } |
244 | #ifdef DEBUG | ||
245 | inflate_hufts += z + 1; | ||
246 | #endif | ||
247 | *t = q + 1; /* link to list for huft_free() */ | ||
248 | *(t = &(q->next)) = Z_NULL; | ||
249 | u[h] = ++q; /* table starts after link */ | ||
250 | 240 | ||
251 | /* connect to last table, if there is one */ | 241 | /* connect to last table, if there is one */ |
252 | if (h) | 242 | if (h) |
@@ -286,10 +276,12 @@ z_streamp zs; /* for zalloc function */ | |||
286 | i ^= j; | 276 | i ^= j; |
287 | 277 | ||
288 | /* backup over finished tables */ | 278 | /* backup over finished tables */ |
289 | while ((i & ((1 << w) - 1)) != x[h]) | 279 | mask = (1 << w) - 1; /* needed on HP, cc -O bug */ |
280 | while ((i & mask) != x[h]) | ||
290 | { | 281 | { |
291 | h--; /* don't need to update q */ | 282 | h--; /* don't need to update q */ |
292 | w -= l; | 283 | w -= l; |
284 | mask = (1 << w) - 1; | ||
293 | } | 285 | } |
294 | } | 286 | } |
295 | } | 287 | } |
@@ -300,28 +292,34 @@ z_streamp zs; /* for zalloc function */ | |||
300 | } | 292 | } |
301 | 293 | ||
302 | 294 | ||
303 | int inflate_trees_bits(c, bb, tb, z) | 295 | int inflate_trees_bits(c, bb, tb, hp, z) |
304 | uIntf *c; /* 19 code lengths */ | 296 | uIntf *c; /* 19 code lengths */ |
305 | uIntf *bb; /* bits tree desired/actual depth */ | 297 | uIntf *bb; /* bits tree desired/actual depth */ |
306 | inflate_huft * FAR *tb; /* bits tree result */ | 298 | inflate_huft * FAR *tb; /* bits tree result */ |
307 | z_streamp z; /* for zfree function */ | 299 | inflate_huft *hp; /* space for trees */ |
300 | z_streamp z; /* for messages */ | ||
308 | { | 301 | { |
309 | int r; | 302 | int r; |
303 | uInt hn = 0; /* hufts used in space */ | ||
304 | uIntf *v; /* work area for huft_build */ | ||
310 | 305 | ||
311 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); | 306 | if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL) |
307 | return Z_MEM_ERROR; | ||
308 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, | ||
309 | tb, bb, hp, &hn, v); | ||
312 | if (r == Z_DATA_ERROR) | 310 | if (r == Z_DATA_ERROR) |
313 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; | 311 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; |
314 | else if (r == Z_BUF_ERROR || *bb == 0) | 312 | else if (r == Z_BUF_ERROR || *bb == 0) |
315 | { | 313 | { |
316 | inflate_trees_free(*tb, z); | ||
317 | z->msg = (char*)"incomplete dynamic bit lengths tree"; | 314 | z->msg = (char*)"incomplete dynamic bit lengths tree"; |
318 | r = Z_DATA_ERROR; | 315 | r = Z_DATA_ERROR; |
319 | } | 316 | } |
317 | ZFREE(z, v); | ||
320 | return r; | 318 | return r; |
321 | } | 319 | } |
322 | 320 | ||
323 | 321 | ||
324 | int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z) | 322 | int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z) |
325 | uInt nl; /* number of literal/length codes */ | 323 | uInt nl; /* number of literal/length codes */ |
326 | uInt nd; /* number of distance codes */ | 324 | uInt nd; /* number of distance codes */ |
327 | uIntf *c; /* that many (total) code lengths */ | 325 | uIntf *c; /* that many (total) code lengths */ |
@@ -329,27 +327,34 @@ uIntf *bl; /* literal desired/actual bit depth */ | |||
329 | uIntf *bd; /* distance desired/actual bit depth */ | 327 | uIntf *bd; /* distance desired/actual bit depth */ |
330 | inflate_huft * FAR *tl; /* literal/length tree result */ | 328 | inflate_huft * FAR *tl; /* literal/length tree result */ |
331 | inflate_huft * FAR *td; /* distance tree result */ | 329 | inflate_huft * FAR *td; /* distance tree result */ |
332 | z_streamp z; /* for zfree function */ | 330 | inflate_huft *hp; /* space for trees */ |
331 | z_streamp z; /* for messages */ | ||
333 | { | 332 | { |
334 | int r; | 333 | int r; |
334 | uInt hn = 0; /* hufts used in space */ | ||
335 | uIntf *v; /* work area for huft_build */ | ||
336 | |||
337 | /* allocate work area */ | ||
338 | if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) | ||
339 | return Z_MEM_ERROR; | ||
335 | 340 | ||
336 | /* build literal/length tree */ | 341 | /* build literal/length tree */ |
337 | r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z); | 342 | r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v); |
338 | if (r != Z_OK || *bl == 0) | 343 | if (r != Z_OK || *bl == 0) |
339 | { | 344 | { |
340 | if (r == Z_DATA_ERROR) | 345 | if (r == Z_DATA_ERROR) |
341 | z->msg = (char*)"oversubscribed literal/length tree"; | 346 | z->msg = (char*)"oversubscribed literal/length tree"; |
342 | else if (r != Z_MEM_ERROR) | 347 | else if (r != Z_MEM_ERROR) |
343 | { | 348 | { |
344 | inflate_trees_free(*tl, z); | ||
345 | z->msg = (char*)"incomplete literal/length tree"; | 349 | z->msg = (char*)"incomplete literal/length tree"; |
346 | r = Z_DATA_ERROR; | 350 | r = Z_DATA_ERROR; |
347 | } | 351 | } |
352 | ZFREE(z, v); | ||
348 | return r; | 353 | return r; |
349 | } | 354 | } |
350 | 355 | ||
351 | /* build distance tree */ | 356 | /* build distance tree */ |
352 | r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z); | 357 | r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v); |
353 | if (r != Z_OK || (*bd == 0 && nl > 257)) | 358 | if (r != Z_OK || (*bd == 0 && nl > 257)) |
354 | { | 359 | { |
355 | if (r == Z_DATA_ERROR) | 360 | if (r == Z_DATA_ERROR) |
@@ -359,7 +364,6 @@ z_streamp z; /* for zfree function */ | |||
359 | r = Z_OK; | 364 | r = Z_OK; |
360 | } | 365 | } |
361 | #else | 366 | #else |
362 | inflate_trees_free(*td, z); | ||
363 | z->msg = (char*)"incomplete distance tree"; | 367 | z->msg = (char*)"incomplete distance tree"; |
364 | r = Z_DATA_ERROR; | 368 | r = Z_DATA_ERROR; |
365 | } | 369 | } |
@@ -368,19 +372,20 @@ z_streamp z; /* for zfree function */ | |||
368 | z->msg = (char*)"empty distance tree with lengths"; | 372 | z->msg = (char*)"empty distance tree with lengths"; |
369 | r = Z_DATA_ERROR; | 373 | r = Z_DATA_ERROR; |
370 | } | 374 | } |
371 | inflate_trees_free(*tl, z); | 375 | ZFREE(z, v); |
372 | return r; | 376 | return r; |
373 | #endif | 377 | #endif |
374 | } | 378 | } |
375 | 379 | ||
376 | /* done */ | 380 | /* done */ |
381 | ZFREE(z, v); | ||
377 | return Z_OK; | 382 | return Z_OK; |
378 | } | 383 | } |
379 | 384 | ||
380 | 385 | ||
381 | /* build fixed tables only once--keep them here */ | 386 | /* build fixed tables only once--keep them here */ |
382 | local int fixed_built = 0; | 387 | local int fixed_built = 0; |
383 | #define FIXEDH 530 /* number of hufts used by fixed tables */ | 388 | #define FIXEDH 424 /* number of hufts used by fixed tables */ |
384 | local inflate_huft fixed_mem[FIXEDH]; | 389 | local inflate_huft fixed_mem[FIXEDH]; |
385 | local uInt fixed_bl; | 390 | local uInt fixed_bl; |
386 | local uInt fixed_bd; | 391 | local uInt fixed_bd; |
@@ -388,36 +393,29 @@ local inflate_huft *fixed_tl; | |||
388 | local inflate_huft *fixed_td; | 393 | local inflate_huft *fixed_td; |
389 | 394 | ||
390 | 395 | ||
391 | local voidpf falloc(q, n, s) | 396 | int inflate_trees_fixed(bl, bd, tl, td, z) |
392 | voidpf q; /* opaque pointer */ | ||
393 | uInt n; /* number of items */ | ||
394 | uInt s; /* size of item */ | ||
395 | { | ||
396 | Assert(s == sizeof(inflate_huft) && n <= *(intf *)q, | ||
397 | "inflate_trees falloc overflow"); | ||
398 | *(intf *)q -= n+s-s; /* s-s to avoid warning */ | ||
399 | return (voidpf)(fixed_mem + *(intf *)q); | ||
400 | } | ||
401 | |||
402 | |||
403 | int inflate_trees_fixed(bl, bd, tl, td) | ||
404 | uIntf *bl; /* literal desired/actual bit depth */ | 397 | uIntf *bl; /* literal desired/actual bit depth */ |
405 | uIntf *bd; /* distance desired/actual bit depth */ | 398 | uIntf *bd; /* distance desired/actual bit depth */ |
406 | inflate_huft * FAR *tl; /* literal/length tree result */ | 399 | inflate_huft * FAR *tl; /* literal/length tree result */ |
407 | inflate_huft * FAR *td; /* distance tree result */ | 400 | inflate_huft * FAR *td; /* distance tree result */ |
401 | z_streamp z; /* for memory allocation */ | ||
408 | { | 402 | { |
409 | /* build fixed tables if not already (multiple overlapped executions ok) */ | 403 | /* build fixed tables if not already (multiple overlapped executions ok) */ |
410 | if (!fixed_built) | 404 | if (!fixed_built) |
411 | { | 405 | { |
412 | int k; /* temporary variable */ | 406 | int k; /* temporary variable */ |
413 | unsigned c[288]; /* length list for huft_build */ | 407 | uInt f = 0; /* number of hufts used in fixed_mem */ |
414 | z_stream z; /* for falloc function */ | 408 | uIntf *c; /* length list for huft_build */ |
415 | int f = FIXEDH; /* number of hufts left in fixed_mem */ | 409 | uIntf *v; /* work area for huft_build */ |
416 | 410 | ||
417 | /* set up fake z_stream for memory routines */ | 411 | /* allocate memory */ |
418 | z.zalloc = falloc; | 412 | if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) |
419 | z.zfree = Z_NULL; | 413 | return Z_MEM_ERROR; |
420 | z.opaque = (voidpf)&f; | 414 | if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL) |
415 | { | ||
416 | ZFREE(z, c); | ||
417 | return Z_MEM_ERROR; | ||
418 | } | ||
421 | 419 | ||
422 | /* literal table */ | 420 | /* literal table */ |
423 | for (k = 0; k < 144; k++) | 421 | for (k = 0; k < 144; k++) |
@@ -429,16 +427,19 @@ inflate_huft * FAR *td; /* distance tree result */ | |||
429 | for (; k < 288; k++) | 427 | for (; k < 288; k++) |
430 | c[k] = 8; | 428 | c[k] = 8; |
431 | fixed_bl = 7; | 429 | fixed_bl = 7; |
432 | huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z); | 430 | huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, |
431 | fixed_mem, &f, v); | ||
433 | 432 | ||
434 | /* distance table */ | 433 | /* distance table */ |
435 | for (k = 0; k < 30; k++) | 434 | for (k = 0; k < 30; k++) |
436 | c[k] = 5; | 435 | c[k] = 5; |
437 | fixed_bd = 5; | 436 | fixed_bd = 5; |
438 | huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z); | 437 | huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, |
438 | fixed_mem, &f, v); | ||
439 | 439 | ||
440 | /* done */ | 440 | /* done */ |
441 | Assert(f == 0, "invalid build of fixed tables"); | 441 | ZFREE(z, v); |
442 | ZFREE(z, c); | ||
442 | fixed_built = 1; | 443 | fixed_built = 1; |
443 | } | 444 | } |
444 | *bl = fixed_bl; | 445 | *bl = fixed_bl; |
@@ -447,33 +448,3 @@ inflate_huft * FAR *td; /* distance tree result */ | |||
447 | *td = fixed_td; | 448 | *td = fixed_td; |
448 | return Z_OK; | 449 | return Z_OK; |
449 | } | 450 | } |
450 | |||
451 | |||
452 | int inflate_trees_free(t, z) | ||
453 | inflate_huft *t; /* table to free */ | ||
454 | z_streamp z; /* for zfree function */ | ||
455 | /* Free the malloc'ed tables built by huft_build(), which makes a linked | ||
456 | list of the tables it made, with the links in a dummy first entry of | ||
457 | each table. */ | ||
458 | { | ||
459 | register inflate_huft *p, *q, *r; | ||
460 | |||
461 | /* Reverse linked list */ | ||
462 | p = Z_NULL; | ||
463 | q = t; | ||
464 | while (q != Z_NULL) | ||
465 | { | ||
466 | r = (q - 1)->next; | ||
467 | (q - 1)->next = p; | ||
468 | p = q; | ||
469 | q = r; | ||
470 | } | ||
471 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ | ||
472 | while (p != Z_NULL) | ||
473 | { | ||
474 | q = (--p)->next; | ||
475 | ZFREE(z,p); | ||
476 | p = q; | ||
477 | } | ||
478 | return Z_OK; | ||
479 | } | ||