diff options
Diffstat (limited to 'inftrees.c')
-rw-r--r-- | inftrees.c | 48 |
1 files changed, 28 insertions, 20 deletions
@@ -1,12 +1,12 @@ | |||
1 | /* inftrees.c -- generate Huffman trees for efficient decoding | 1 | /* inftrees.c -- generate Huffman trees for efficient decoding |
2 | * Copyright (C) 1995-1996 Mark Adler | 2 | * Copyright (C) 1995-1998 Mark Adler |
3 | * For conditions of distribution and use, see copyright notice in zlib.h | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include "zutil.h" | 6 | #include "zutil.h" |
7 | #include "inftrees.h" | 7 | #include "inftrees.h" |
8 | 8 | ||
9 | char inflate_copyright[] = " inflate 1.0.4 Copyright 1995-1996 Mark Adler "; | 9 | char inflate_copyright[] = " inflate 1.0.5 Copyright 1995-1998 Mark Adler "; |
10 | /* | 10 | /* |
11 | If you use the zlib library in a product, an acknowledgment is welcome | 11 | If you use the zlib library in a product, an acknowledgment is welcome |
12 | in the documentation of your product. If for some reason you cannot | 12 | in the documentation of your product. If for some reason you cannot |
@@ -26,8 +26,8 @@ local int huft_build OF(( | |||
26 | uIntf *, /* code lengths in bits */ | 26 | uIntf *, /* code lengths in bits */ |
27 | uInt, /* number of codes */ | 27 | uInt, /* number of codes */ |
28 | uInt, /* number of "simple" codes */ | 28 | uInt, /* number of "simple" codes */ |
29 | uIntf *, /* list of base values for non-simple codes */ | 29 | const uIntf *, /* list of base values for non-simple codes */ |
30 | uIntf *, /* list of extra bits for non-simple codes */ | 30 | const uIntf *, /* list of extra bits for non-simple codes */ |
31 | inflate_huft * FAR*,/* result: starting table */ | 31 | inflate_huft * FAR*,/* result: starting table */ |
32 | uIntf *, /* maximum lookup bits (returns actual) */ | 32 | uIntf *, /* maximum lookup bits (returns actual) */ |
33 | z_streamp )); /* for zalloc function */ | 33 | z_streamp )); /* for zalloc function */ |
@@ -38,18 +38,18 @@ local voidpf falloc OF(( | |||
38 | uInt)); /* size of item */ | 38 | uInt)); /* size of item */ |
39 | 39 | ||
40 | /* Tables for deflate from PKZIP's appnote.txt. */ | 40 | /* Tables for deflate from PKZIP's appnote.txt. */ |
41 | local uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */ | 41 | local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */ |
42 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | 42 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
43 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | 43 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
44 | /* actually lengths - 2; also see note #13 above about 258 */ | 44 | /* see note #13 above about 258 */ |
45 | local uInt cplext[31] = { /* Extra bits for literal codes 257..285 */ | 45 | local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */ |
46 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | 46 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
47 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */ | 47 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */ |
48 | local uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */ | 48 | local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */ |
49 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | 49 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
50 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | 50 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
51 | 8193, 12289, 16385, 24577}; | 51 | 8193, 12289, 16385, 24577}; |
52 | local uInt cpdext[30] = { /* Extra bits for distance codes */ | 52 | local const uInt cpdext[30] = { /* Extra bits for distance codes */ |
53 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | 53 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
54 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | 54 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
55 | 12, 12, 13, 13}; | 55 | 12, 12, 13, 13}; |
@@ -99,16 +99,16 @@ local int huft_build(b, n, s, d, e, t, m, zs) | |||
99 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ | 99 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ |
100 | uInt n; /* number of codes (assumed <= N_MAX) */ | 100 | uInt n; /* number of codes (assumed <= N_MAX) */ |
101 | uInt s; /* number of simple-valued codes (0..s-1) */ | 101 | uInt s; /* number of simple-valued codes (0..s-1) */ |
102 | uIntf *d; /* list of base values for non-simple codes */ | 102 | const uIntf *d; /* list of base values for non-simple codes */ |
103 | uIntf *e; /* list of extra bits for non-simple codes */ | 103 | const uIntf *e; /* list of extra bits for non-simple codes */ |
104 | inflate_huft * FAR *t; /* result: starting table */ | 104 | inflate_huft * FAR *t; /* result: starting table */ |
105 | uIntf *m; /* maximum lookup bits, returns actual */ | 105 | uIntf *m; /* maximum lookup bits, returns actual */ |
106 | z_streamp zs; /* for zalloc function */ | 106 | z_streamp zs; /* for zalloc function */ |
107 | /* Given a list of code lengths and a maximum table size, make a set of | 107 | /* Given a list of code lengths and a maximum table size, make a set of |
108 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR | 108 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR |
109 | if the given code set is incomplete (the tables are still built in this | 109 | if the given code set is incomplete (the tables are still built in this |
110 | case), Z_DATA_ERROR if the input is invalid (all zero length codes or an | 110 | case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of |
111 | over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */ | 111 | lengths), or Z_MEM_ERROR if not enough memory. */ |
112 | { | 112 | { |
113 | 113 | ||
114 | uInt a; /* counter for codes of length k */ | 114 | uInt a; /* counter for codes of length k */ |
@@ -190,6 +190,7 @@ z_streamp zs; /* for zalloc function */ | |||
190 | if ((j = *p++) != 0) | 190 | if ((j = *p++) != 0) |
191 | v[x[j]++] = i; | 191 | v[x[j]++] = i; |
192 | } while (++i < n); | 192 | } while (++i < n); |
193 | n = x[g]; /* set n to length of v */ | ||
193 | 194 | ||
194 | 195 | ||
195 | /* Generate the Huffman codes and for each, make the table entries */ | 196 | /* Generate the Huffman codes and for each, make the table entries */ |
@@ -309,7 +310,7 @@ z_streamp z; /* for zfree function */ | |||
309 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); | 310 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); |
310 | if (r == Z_DATA_ERROR) | 311 | if (r == Z_DATA_ERROR) |
311 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; | 312 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; |
312 | else if (r == Z_BUF_ERROR) | 313 | else if (r == Z_BUF_ERROR || *bb == 0) |
313 | { | 314 | { |
314 | inflate_trees_free(*tb, z); | 315 | inflate_trees_free(*tb, z); |
315 | z->msg = (char*)"incomplete dynamic bit lengths tree"; | 316 | z->msg = (char*)"incomplete dynamic bit lengths tree"; |
@@ -332,11 +333,12 @@ z_streamp z; /* for zfree function */ | |||
332 | int r; | 333 | int r; |
333 | 334 | ||
334 | /* build literal/length tree */ | 335 | /* build literal/length tree */ |
335 | if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) | 336 | r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z); |
337 | if (r != Z_OK || *bl == 0) | ||
336 | { | 338 | { |
337 | if (r == Z_DATA_ERROR) | 339 | if (r == Z_DATA_ERROR) |
338 | z->msg = (char*)"oversubscribed literal/length tree"; | 340 | z->msg = (char*)"oversubscribed literal/length tree"; |
339 | else if (r == Z_BUF_ERROR) | 341 | else if (r != Z_MEM_ERROR) |
340 | { | 342 | { |
341 | inflate_trees_free(*tl, z); | 343 | inflate_trees_free(*tl, z); |
342 | z->msg = (char*)"incomplete literal/length tree"; | 344 | z->msg = (char*)"incomplete literal/length tree"; |
@@ -346,17 +348,23 @@ z_streamp z; /* for zfree function */ | |||
346 | } | 348 | } |
347 | 349 | ||
348 | /* build distance tree */ | 350 | /* build distance tree */ |
349 | if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) | 351 | r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z); |
352 | if (r != Z_OK || (*bd == 0 && nl > 257)) | ||
350 | { | 353 | { |
351 | if (r == Z_DATA_ERROR) | 354 | if (r == Z_DATA_ERROR) |
352 | z->msg = (char*)"oversubscribed literal/length tree"; | 355 | z->msg = (char*)"oversubscribed distance tree"; |
353 | else if (r == Z_BUF_ERROR) { | 356 | else if (r == Z_BUF_ERROR) { |
354 | #ifdef PKZIP_BUG_WORKAROUND | 357 | #ifdef PKZIP_BUG_WORKAROUND |
355 | r = Z_OK; | 358 | r = Z_OK; |
356 | } | 359 | } |
357 | #else | 360 | #else |
358 | inflate_trees_free(*td, z); | 361 | inflate_trees_free(*td, z); |
359 | z->msg = (char*)"incomplete literal/length tree"; | 362 | z->msg = (char*)"incomplete distance tree"; |
363 | r = Z_DATA_ERROR; | ||
364 | } | ||
365 | else if (r != Z_MEM_ERROR) | ||
366 | { | ||
367 | z->msg = (char*)"empty distance tree with lengths"; | ||
360 | r = Z_DATA_ERROR; | 368 | r = Z_DATA_ERROR; |
361 | } | 369 | } |
362 | inflate_trees_free(*tl, z); | 370 | inflate_trees_free(*tl, z); |