OLD | NEW |
1 /* inftrees.c -- generate Huffman trees for efficient decoding | 1 /* inftrees.c -- generate Huffman trees for efficient decoding |
2 * Copyright (C) 1995-2013 Mark Adler | 2 * Copyright (C) 1995-2017 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 #define MAXBITS 15 | 9 #define MAXBITS 15 |
10 | 10 |
11 const char inflate_copyright[] = | 11 const char inflate_copyright[] = |
12 " inflate 1.2.8 Copyright 1995-2013 Mark Adler "; | 12 " inflate 1.2.11 Copyright 1995-2017 Mark Adler "; |
13 /* | 13 /* |
14 If you use the zlib library in a product, an acknowledgment is welcome | 14 If you use the zlib library in a product, an acknowledgment is welcome |
15 in the documentation of your product. If for some reason you cannot | 15 in the documentation of your product. If for some reason you cannot |
16 include such an acknowledgment, I would appreciate that you keep this | 16 include such an acknowledgment, I would appreciate that you keep this |
17 copyright string in the executable of your product. | 17 copyright string in the executable of your product. |
18 */ | 18 */ |
19 | 19 |
20 /* | 20 /* |
21 Build a set of tables to decode the provided canonical Huffman code. | 21 Build a set of tables to decode the provided canonical Huffman code. |
22 The code lengths are lens[0..codes-1]. The result starts at *table, | 22 The code lengths are lens[0..codes-1]. The result starts at *table, |
(...skipping 24 matching lines...) Expand all Loading... |
47 unsigned used; /* code entries in table used */ | 47 unsigned used; /* code entries in table used */ |
48 unsigned huff; /* Huffman code */ | 48 unsigned huff; /* Huffman code */ |
49 unsigned incr; /* for incrementing code, index */ | 49 unsigned incr; /* for incrementing code, index */ |
50 unsigned fill; /* index for replicating entries */ | 50 unsigned fill; /* index for replicating entries */ |
51 unsigned low; /* low bits for current root entry */ | 51 unsigned low; /* low bits for current root entry */ |
52 unsigned mask; /* mask for low root bits */ | 52 unsigned mask; /* mask for low root bits */ |
53 code here; /* table entry for duplication */ | 53 code here; /* table entry for duplication */ |
54 code FAR *next; /* next available space in table */ | 54 code FAR *next; /* next available space in table */ |
55 const unsigned short FAR *base; /* base value table to use */ | 55 const unsigned short FAR *base; /* base value table to use */ |
56 const unsigned short FAR *extra; /* extra bits table to use */ | 56 const unsigned short FAR *extra; /* extra bits table to use */ |
57 int end; /* use base and extra for symbol > end */ | 57 unsigned match; /* use base and extra for symbol >= match */ |
58 unsigned short count[MAXBITS+1]; /* number of codes of each length */ | 58 unsigned short count[MAXBITS+1]; /* number of codes of each length */ |
59 unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ | 59 unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ |
60 static const unsigned short lbase[31] = { /* Length codes 257..285 base */ | 60 static const unsigned short lbase[31] = { /* Length codes 257..285 base */ |
61 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | 61 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
62 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | 62 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
63 static const unsigned short lext[31] = { /* Length codes 257..285 extra */ | 63 static const unsigned short lext[31] = { /* Length codes 257..285 extra */ |
64 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, | 64 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, |
65 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78}; | 65 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202}; |
66 static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ | 66 static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ |
67 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | 67 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
68 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | 68 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
69 8193, 12289, 16385, 24577, 0, 0}; | 69 8193, 12289, 16385, 24577, 0, 0}; |
70 static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ | 70 static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ |
71 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, | 71 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, |
72 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, | 72 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, |
73 28, 28, 29, 29, 64, 64}; | 73 28, 28, 29, 29, 64, 64}; |
74 | 74 |
75 /* | 75 /* |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 sym increments through all symbols, and the loop terminates when | 174 sym increments through all symbols, and the loop terminates when |
175 all codes of length max, i.e. all codes, have been processed. This | 175 all codes of length max, i.e. all codes, have been processed. This |
176 routine permits incomplete codes, so another loop after this one fills | 176 routine permits incomplete codes, so another loop after this one fills |
177 in the rest of the decoding tables with invalid code markers. | 177 in the rest of the decoding tables with invalid code markers. |
178 */ | 178 */ |
179 | 179 |
180 /* set up for code type */ | 180 /* set up for code type */ |
181 switch (type) { | 181 switch (type) { |
182 case CODES: | 182 case CODES: |
183 base = extra = work; /* dummy value--not used */ | 183 base = extra = work; /* dummy value--not used */ |
184 end = 19; | 184 match = 20; |
185 break; | 185 break; |
186 case LENS: | 186 case LENS: |
187 base = lbase; | 187 base = lbase; |
188 base -= 257; | |
189 extra = lext; | 188 extra = lext; |
190 extra -= 257; | 189 match = 257; |
191 end = 256; | |
192 break; | 190 break; |
193 default: /* DISTS */ | 191 default: /* DISTS */ |
194 base = dbase; | 192 base = dbase; |
195 extra = dext; | 193 extra = dext; |
196 end = -1; | 194 match = 0; |
197 } | 195 } |
198 | 196 |
199 /* initialize state for loop */ | 197 /* initialize state for loop */ |
200 huff = 0; /* starting code */ | 198 huff = 0; /* starting code */ |
201 sym = 0; /* starting code symbol */ | 199 sym = 0; /* starting code symbol */ |
202 len = min; /* starting code length */ | 200 len = min; /* starting code length */ |
203 next = *table; /* current table to fill in */ | 201 next = *table; /* current table to fill in */ |
204 curr = root; /* current table index bits */ | 202 curr = root; /* current table index bits */ |
205 drop = 0; /* current bits to drop from code for index */ | 203 drop = 0; /* current bits to drop from code for index */ |
206 low = (unsigned)(-1); /* trigger new sub-table when len > root */ | 204 low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
207 used = 1U << root; /* use root table entries */ | 205 used = 1U << root; /* use root table entries */ |
208 mask = used - 1; /* mask for comparing low */ | 206 mask = used - 1; /* mask for comparing low */ |
209 | 207 |
210 /* check available table space */ | 208 /* check available table space */ |
211 if ((type == LENS && used > ENOUGH_LENS) || | 209 if ((type == LENS && used > ENOUGH_LENS) || |
212 (type == DISTS && used > ENOUGH_DISTS)) | 210 (type == DISTS && used > ENOUGH_DISTS)) |
213 return 1; | 211 return 1; |
214 | 212 |
215 /* process all codes and make table entries */ | 213 /* process all codes and make table entries */ |
216 for (;;) { | 214 for (;;) { |
217 /* create table entry */ | 215 /* create table entry */ |
218 here.bits = (unsigned char)(len - drop); | 216 here.bits = (unsigned char)(len - drop); |
219 if ((int)(work[sym]) < end) { | 217 if (work[sym] + 1U < match) { |
220 here.op = (unsigned char)0; | 218 here.op = (unsigned char)0; |
221 here.val = work[sym]; | 219 here.val = work[sym]; |
222 } | 220 } |
223 else if ((int)(work[sym]) > end) { | 221 else if (work[sym] >= match) { |
224 here.op = (unsigned char)(extra[work[sym]]); | 222 here.op = (unsigned char)(extra[work[sym] - match]); |
225 here.val = base[work[sym]]; | 223 here.val = base[work[sym] - match]; |
226 } | 224 } |
227 else { | 225 else { |
228 here.op = (unsigned char)(32 + 64); /* end of block */ | 226 here.op = (unsigned char)(32 + 64); /* end of block */ |
229 here.val = 0; | 227 here.val = 0; |
230 } | 228 } |
231 | 229 |
232 /* replicate for those indices with low len bits equal to huff */ | 230 /* replicate for those indices with low len bits equal to huff */ |
233 incr = 1U << (len - drop); | 231 incr = 1U << (len - drop); |
234 fill = 1U << curr; | 232 fill = 1U << curr; |
235 min = fill; /* save offset to next table */ | 233 min = fill; /* save offset to next table */ |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 here.bits = (unsigned char)(len - drop); | 295 here.bits = (unsigned char)(len - drop); |
298 here.val = (unsigned short)0; | 296 here.val = (unsigned short)0; |
299 next[huff] = here; | 297 next[huff] = here; |
300 } | 298 } |
301 | 299 |
302 /* set return parameters */ | 300 /* set return parameters */ |
303 *table += used; | 301 *table += used; |
304 *bits = root; | 302 *bits = root; |
305 return 0; | 303 return 0; |
306 } | 304 } |
OLD | NEW |