Line | Count | Source |
1 | | /* inftrees.c -- generate Huffman trees for efficient decoding |
2 | | * Copyright (C) 1995-2024 Mark Adler |
3 | | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | | */ |
5 | | |
6 | | #include "zbuild.h" |
7 | | #include "zutil.h" |
8 | | #include "inftrees.h" |
9 | | #include "inflate_p.h" |
10 | | #include "fallback_builtins.h" |
11 | | |
12 | | #if defined(__SSE2__) |
13 | | # include "arch/x86/x86_intrins.h" |
14 | | #elif defined(__ARM_NEON) || defined(__ARM_NEON__) |
15 | | # include "arch/arm/neon_intrins.h" |
16 | | #elif defined(__ALTIVEC__) |
17 | | # include "arch/power/power_intrins.h" |
18 | | #endif |
19 | | |
20 | | const char PREFIX(inflate_copyright)[] = " inflate 1.3.1 Copyright 1995-2024 Mark Adler "; |
21 | | /* |
22 | | If you use the zlib library in a product, an acknowledgment is welcome |
23 | | in the documentation of your product. If for some reason you cannot |
24 | | include such an acknowledgment, I would appreciate that you keep this |
25 | | copyright string in the executable of your product. |
26 | | */ |
27 | | |
28 | | /* Count number of codes for each code length. */ |
29 | 39.0k | static inline void count_lengths(uint16_t *lens, int codes, uint16_t *count) { |
30 | | /* IBM...made some weird choices for VSX/VMX. Basically vec_ld has an inherent |
31 | | * endianness but we don't want to force VSX to be needed */ |
32 | 39.0k | static const ALIGNED_(16) uint8_t one[256] = { |
33 | 39.0k | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
34 | 39.0k | 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
35 | 39.0k | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
36 | 39.0k | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
37 | 39.0k | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
38 | 39.0k | 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
39 | 39.0k | 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
40 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
41 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
42 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, |
43 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, |
44 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, |
45 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
46 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, |
47 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, |
48 | 39.0k | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
49 | 39.0k | }; |
50 | | |
51 | | #if defined(__ALTIVEC__) |
52 | | vector unsigned char s1 = vec_splat_u8(0); |
53 | | vector unsigned char s2 = vec_splat_u8(0); |
54 | | |
55 | | if (codes & 1) { |
56 | | s1 = vec_ld(16 * lens[0], one); |
57 | | --codes; |
58 | | ++lens; |
59 | | } |
60 | | |
61 | | while (codes) { |
62 | | s1 = vec_add(s1, vec_ld(16 * lens[0], one)); |
63 | | s2 = vec_add(s2, vec_ld(16 * lens[1], one)); |
64 | | codes -= 2; |
65 | | lens += 2; |
66 | | } |
67 | | |
68 | | vector unsigned short sum_lo = vec_add(vec_unpackh(s1), vec_unpackh(s2)); |
69 | | vector unsigned short sum_hi = vec_add(vec_unpackl(s1), vec_unpackl(s2)); |
70 | | |
71 | | vec_st(sum_lo, 0, &count[0]); |
72 | | vec_st(sum_hi, 0, &count[8]); |
73 | | |
74 | | #elif defined(__ARM_NEON) || defined(__ARM_NEON__) |
75 | | int sym; |
76 | | uint8x16_t s1 = vdupq_n_u8(0); |
77 | | uint8x16_t s2 = vdupq_n_u8(0); |
78 | | |
79 | | if (codes & 1) { |
80 | | s1 = vld1q_u8(&one[16 * lens[0]]); |
81 | | } |
82 | | for (sym = codes & 1; sym < codes; sym += 2) { |
83 | | s1 = vaddq_u8(s1, vld1q_u8(&one[16 * lens[sym]])); |
84 | | s2 = vaddq_u8(s2, vld1q_u8(&one[16 * lens[sym+1]])); |
85 | | } |
86 | | |
87 | | vst1q_u16(&count[0], vaddl_u8(vget_low_u8(s1), vget_low_u8(s2))); |
88 | | vst1q_u16(&count[8], vaddl_u8(vget_high_u8(s1), vget_high_u8(s2))); |
89 | | |
90 | | #elif defined(__SSE2__) |
91 | | int sym; |
92 | 39.0k | __m128i s1 = _mm_setzero_si128(); |
93 | 39.0k | __m128i s2 = _mm_setzero_si128(); |
94 | | |
95 | 39.0k | if (codes & 1) { |
96 | 22.2k | s1 = _mm_load_si128((const __m128i*)&one[16 * lens[0]]); |
97 | 22.2k | } |
98 | 1.97M | for (sym = codes & 1; sym < codes; sym += 2) { |
99 | 1.93M | s1 = _mm_add_epi8(s1, _mm_load_si128((const __m128i*)&one[16 * lens[sym]])); // vaddq_u8 |
100 | 1.93M | s2 = _mm_add_epi8(s2, _mm_load_si128((const __m128i*)&one[16 * lens[sym+1]])); |
101 | 1.93M | } |
102 | | |
103 | | # if defined(__AVX2__) |
104 | | __m256i w1 = _mm256_cvtepu8_epi16(s1); |
105 | | __m256i w2 = _mm256_cvtepu8_epi16(s2); |
106 | | __m256i sum = _mm256_add_epi16(w1, w2); |
107 | | |
108 | | _mm256_storeu_si256((__m256i*)&count[0], sum); |
109 | | # else |
110 | 39.0k | __m128i zero = _mm_setzero_si128(); |
111 | | |
112 | 39.0k | __m128i s1_lo = _mm_unpacklo_epi8(s1, zero); |
113 | 39.0k | __m128i s2_lo = _mm_unpacklo_epi8(s2, zero); |
114 | 39.0k | __m128i sum_lo = _mm_add_epi16(s1_lo, s2_lo); |
115 | 39.0k | _mm_storeu_si128((__m128i*)&count[0], sum_lo); |
116 | | |
117 | 39.0k | __m128i s1_hi = _mm_unpackhi_epi8(s1, zero); |
118 | 39.0k | __m128i s2_hi = _mm_unpackhi_epi8(s2, zero); |
119 | 39.0k | __m128i sum_hi = _mm_add_epi16(s1_hi, s2_hi); |
120 | 39.0k | _mm_storeu_si128((__m128i*)&count[8], sum_hi); |
121 | 39.0k | # endif |
122 | | #else |
123 | | int len, sym; |
124 | | for (len = 0; len <= MAX_BITS; len++) |
125 | | count[len] = 0; |
126 | | for (sym = 0; sym < codes; sym++) |
127 | | count[lens[sym]]++; |
128 | | Z_UNUSED(one); |
129 | | #endif |
130 | 39.0k | } |
131 | | |
132 | | /* |
133 | | Build a set of tables to decode the provided canonical Huffman code. |
134 | | The code lengths are lens[0..codes-1]. The result starts at *table, |
135 | | whose indices are 0..2^bits-1. work is a writable array of at least |
136 | | lens shorts, which is used as a work area. type is the type of code |
137 | | to be generated, CODES, LENS, or DISTS. On return, zero is success, |
138 | | -1 is an invalid code, and +1 means that ENOUGH isn't enough. table |
139 | | on return points to the next available entry's address. bits is the |
140 | | requested root table index bits, and on return it is the actual root |
141 | | table index bits. It will differ if the request is greater than the |
142 | | longest code or if it is less than the shortest code. |
143 | | */ |
144 | | int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes, |
145 | 39.0k | code * *table, unsigned *bits, uint16_t *work) { |
146 | 39.0k | unsigned len; /* a code's length in bits */ |
147 | 39.0k | unsigned sym; /* index of code symbols */ |
148 | 39.0k | unsigned min, max; /* minimum and maximum code lengths */ |
149 | 39.0k | unsigned root; /* number of index bits for root table */ |
150 | 39.0k | unsigned curr; /* number of index bits for current table */ |
151 | 39.0k | unsigned drop; /* code bits to drop for sub-table */ |
152 | 39.0k | int left; /* number of prefix codes available */ |
153 | 39.0k | unsigned used; /* code entries in table used */ |
154 | 39.0k | uint16_t rhuff; /* Reversed huffman code */ |
155 | 39.0k | unsigned huff; /* Huffman code */ |
156 | 39.0k | unsigned incr; /* for incrementing code, index */ |
157 | 39.0k | unsigned fill; /* index for replicating entries */ |
158 | 39.0k | unsigned low; /* low bits for current root entry */ |
159 | 39.0k | unsigned mask; /* mask for low root bits */ |
160 | 39.0k | code here; /* table entry for duplication */ |
161 | 39.0k | code *next; /* next available space in table */ |
162 | 39.0k | const uint16_t *base; /* base value table to use */ |
163 | 39.0k | const uint16_t *extra; /* extra bits table to use */ |
164 | 39.0k | unsigned match; /* use base and extra for symbol >= match */ |
165 | 39.0k | uint16_t ALIGNED_(16) count[MAX_BITS+1]; /* number of codes of each length */ |
166 | 39.0k | uint16_t offs[MAX_BITS+1]; /* offsets in table for each length */ |
167 | 39.0k | static const uint16_t lbase[31] = { /* Length codes 257..285 base */ |
168 | 39.0k | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
169 | 39.0k | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
170 | 39.0k | static const uint16_t lext[31] = { /* Length codes 257..285 extra */ |
171 | 39.0k | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, |
172 | 39.0k | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 77}; |
173 | 39.0k | static const uint16_t dbase[32] = { /* Distance codes 0..29 base */ |
174 | 39.0k | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
175 | 39.0k | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
176 | 39.0k | 8193, 12289, 16385, 24577, 0, 0}; |
177 | 39.0k | static const uint16_t dext[32] = { /* Distance codes 0..29 extra */ |
178 | 39.0k | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, |
179 | 39.0k | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, |
180 | 39.0k | 28, 28, 29, 29, 64, 64}; |
181 | | |
182 | | /* |
183 | | Process a set of code lengths to create a canonical Huffman code. The |
184 | | code lengths are lens[0..codes-1]. Each length corresponds to the |
185 | | symbols 0..codes-1. The Huffman code is generated by first sorting the |
186 | | symbols by length from short to long, and retaining the symbol order |
187 | | for codes with equal lengths. Then the code starts with all zero bits |
188 | | for the first code of the shortest length, and the codes are integer |
189 | | increments for the same length, and zeros are appended as the length |
190 | | increases. For the deflate format, these bits are stored backwards |
191 | | from their more natural integer increment ordering, and so when the |
192 | | decoding tables are built in the large loop below, the integer codes |
193 | | are incremented backwards. |
194 | | |
195 | | This routine assumes, but does not check, that all of the entries in |
196 | | lens[] are in the range 0..MAXBITS. The caller must assure this. |
197 | | 1..MAXBITS is interpreted as that code length. zero means that that |
198 | | symbol does not occur in this code. |
199 | | |
200 | | The codes are sorted by computing a count of codes for each length, |
201 | | creating from that a table of starting indices for each length in the |
202 | | sorted table, and then entering the symbols in order in the sorted |
203 | | table. The sorted table is work[], with that space being provided by |
204 | | the caller. |
205 | | |
206 | | The length counts are used for other purposes as well, i.e. finding |
207 | | the minimum and maximum length codes, determining if there are any |
208 | | codes at all, checking for a valid set of lengths, and looking ahead |
209 | | at length counts to determine sub-table sizes when building the |
210 | | decoding tables. |
211 | | */ |
212 | | |
213 | | /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ |
214 | 39.0k | count_lengths(lens, codes, count); |
215 | | |
216 | | /* bound code lengths, force root to be within code lengths */ |
217 | 39.0k | root = *bits; |
218 | 372k | for (max = MAX_BITS; max >= 1; max--) |
219 | 372k | if (count[max] != 0) break; |
220 | 39.0k | root = MIN(root, max); |
221 | 39.0k | if (UNLIKELY(max == 0)) { /* no symbols to code at all */ |
222 | 0 | here.op = (unsigned char)64; /* invalid code marker */ |
223 | 0 | here.bits = (unsigned char)1; |
224 | 0 | here.val = (uint16_t)0; |
225 | 0 | *(*table)++ = here; /* make a table to force an error */ |
226 | 0 | *(*table)++ = here; |
227 | 0 | *bits = 1; |
228 | 0 | return 0; /* no symbols, but wait for decoding to report error */ |
229 | 0 | } |
230 | 83.7k | for (min = 1; min < max; min++) |
231 | 74.1k | if (count[min] != 0) break; |
232 | 39.0k | root = MAX(root, min); |
233 | | |
234 | | /* check for an over-subscribed or incomplete set of lengths */ |
235 | 39.0k | left = 1; |
236 | 624k | for (len = 1; len <= MAX_BITS; len++) { |
237 | 585k | left <<= 1; |
238 | 585k | left -= count[len]; |
239 | 585k | if (left < 0) return -1; /* over-subscribed */ |
240 | 585k | } |
241 | 39.0k | if (left > 0 && (type == CODES || max != 1)) |
242 | 0 | return -1; /* incomplete set */ |
243 | | |
244 | | /* generate offsets into symbol table for each length for sorting */ |
245 | 39.0k | offs[1] = 0; |
246 | 585k | for (len = 1; len < MAX_BITS; len++) |
247 | 546k | offs[len + 1] = offs[len] + count[len]; |
248 | | |
249 | | /* sort symbols by length, by symbol order within each length */ |
250 | 3.93M | for (sym = 0; sym < codes; sym++) |
251 | 3.89M | if (lens[sym] != 0) work[offs[lens[sym]]++] = (uint16_t)sym; |
252 | | |
253 | | /* |
254 | | Create and fill in decoding tables. In this loop, the table being |
255 | | filled is at next and has curr index bits. The code being used is huff |
256 | | with length len. That code is converted to an index by dropping drop |
257 | | bits off of the bottom. For codes where len is less than drop + curr, |
258 | | those top drop + curr - len bits are incremented through all values to |
259 | | fill the table with replicated entries. |
260 | | |
261 | | root is the number of index bits for the root table. When len exceeds |
262 | | root, sub-tables are created pointed to by the root entry with an index |
263 | | of the low root bits of huff. This is saved in low to check for when a |
264 | | new sub-table should be started. drop is zero when the root table is |
265 | | being filled, and drop is root when sub-tables are being filled. |
266 | | |
267 | | When a new sub-table is needed, it is necessary to look ahead in the |
268 | | code lengths to determine what size sub-table is needed. The length |
269 | | counts are used for this, and so count[] is decremented as codes are |
270 | | entered in the tables. |
271 | | |
272 | | used keeps track of how many table entries have been allocated from the |
273 | | provided *table space. It is checked for LENS and DIST tables against |
274 | | the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in |
275 | | the initial root table size constants. See the comments in inftrees.h |
276 | | for more information. |
277 | | |
278 | | sym increments through all symbols, and the loop terminates when |
279 | | all codes of length max, i.e. all codes, have been processed. This |
280 | | routine permits incomplete codes, so another loop after this one fills |
281 | | in the rest of the decoding tables with invalid code markers. |
282 | | */ |
283 | | |
284 | | /* set up for code type */ |
285 | 39.0k | switch (type) { |
286 | 13.0k | case CODES: |
287 | 13.0k | base = extra = work; /* dummy value--not used */ |
288 | 13.0k | match = 20; |
289 | 13.0k | break; |
290 | 13.0k | case LENS: |
291 | 13.0k | base = lbase; |
292 | 13.0k | extra = lext; |
293 | 13.0k | match = 257; |
294 | 13.0k | break; |
295 | 13.0k | default: /* DISTS */ |
296 | 13.0k | base = dbase; |
297 | 13.0k | extra = dext; |
298 | 13.0k | match = 0; |
299 | 39.0k | } |
300 | | |
301 | | /* initialize state for loop */ |
302 | 39.0k | rhuff = 0; /* starting code, reversed */ |
303 | 39.0k | huff = 0; /* starting code */ |
304 | 39.0k | sym = 0; /* starting code symbol */ |
305 | 39.0k | len = min; /* starting code length */ |
306 | 39.0k | next = *table; /* current table to fill in */ |
307 | 39.0k | curr = root; /* current table index bits */ |
308 | 39.0k | drop = 0; /* current bits to drop from code for index */ |
309 | 39.0k | low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
310 | 39.0k | used = 1U << root; /* use root table entries */ |
311 | 39.0k | mask = used - 1; /* mask for comparing low */ |
312 | | |
313 | | /* check available table space */ |
314 | 39.0k | if ((type == LENS && used > ENOUGH_LENS) || |
315 | 39.0k | (type == DISTS && used > ENOUGH_DISTS)) |
316 | 0 | return 1; |
317 | | |
318 | | /* process all codes and make table entries */ |
319 | 2.40M | for (;;) { |
320 | | /* create table entry */ |
321 | 2.40M | here.bits = (unsigned char)(len - drop); |
322 | 2.40M | if (LIKELY(work[sym] >= match)) { |
323 | 196k | unsigned op = extra[work[sym] - match]; |
324 | 196k | here.op = COMBINE_OP(op, here.bits); |
325 | 196k | here.bits = COMBINE_BITS(here.bits, op); |
326 | 196k | here.val = base[work[sym] - match]; |
327 | 2.20M | } else if (work[sym] + 1U < match) { |
328 | 2.19M | here.op = (unsigned char)0; |
329 | 2.19M | here.val = work[sym]; |
330 | 2.19M | } else { |
331 | 13.0k | here.op = (unsigned char)(32 + 64); /* end of block */ |
332 | 13.0k | here.val = 0; |
333 | 13.0k | } |
334 | | |
335 | | /* replicate for those indices with low len bits equal to huff */ |
336 | 2.40M | incr = 1U << (len - drop); |
337 | 2.40M | fill = 1U << curr; |
338 | 2.40M | min = fill; /* save offset to next table */ |
339 | 12.5M | do { |
340 | 12.5M | fill -= incr; |
341 | 12.5M | next[(huff >> drop) + fill] = here; |
342 | 12.5M | } while (fill != 0); |
343 | | |
344 | | /* backwards increment the len-bit code huff */ |
345 | 2.40M | rhuff += (0x8000u >> (len - 1)); |
346 | 2.40M | huff = zng_bitreverse16(rhuff); |
347 | | |
348 | | /* go to next symbol, update count, len */ |
349 | 2.40M | sym++; |
350 | 2.40M | if (--(count[len]) == 0) { |
351 | 180k | if (len == max) |
352 | 39.0k | break; |
353 | 141k | len = lens[work[sym]]; |
354 | 141k | } |
355 | | |
356 | | /* create new sub-table if needed */ |
357 | 2.36M | if (len > root && (huff & mask) != low) { |
358 | | /* if first time, transition to sub-tables */ |
359 | 63.9k | if (drop == 0) |
360 | 9.15k | drop = root; |
361 | | |
362 | | /* increment past last table */ |
363 | 63.9k | next += min; /* here min is 1 << curr */ |
364 | | |
365 | | /* determine length of next table */ |
366 | 63.9k | curr = len - drop; |
367 | 63.9k | left = (int)(1 << curr); |
368 | 76.9k | while (curr + drop < max) { |
369 | 60.7k | left -= count[curr + drop]; |
370 | 60.7k | if (left <= 0) |
371 | 47.7k | break; |
372 | 12.9k | curr++; |
373 | 12.9k | left <<= 1; |
374 | 12.9k | } |
375 | | |
376 | | /* check for enough space */ |
377 | 63.9k | used += 1U << curr; |
378 | 63.9k | if ((type == LENS && used > ENOUGH_LENS) || (type == DISTS && used > ENOUGH_DISTS)) |
379 | 0 | return 1; |
380 | | |
381 | | /* point entry in root table to sub-table */ |
382 | 63.9k | low = huff & mask; |
383 | 63.9k | (*table)[low].op = (unsigned char)curr; |
384 | 63.9k | (*table)[low].bits = (unsigned char)root; |
385 | 63.9k | (*table)[low].val = (uint16_t)(next - *table); |
386 | 63.9k | } |
387 | 2.36M | } |
388 | | |
389 | | /* fill in remaining table entry if code is incomplete (guaranteed to have |
390 | | at most one remaining entry, since if the code is incomplete, the |
391 | | maximum code length that was allowed to get this far is one bit) */ |
392 | 39.0k | if (UNLIKELY(huff != 0)) { |
393 | 0 | here.op = (unsigned char)64; /* invalid code marker */ |
394 | 0 | here.bits = (unsigned char)(len - drop); |
395 | 0 | here.val = (uint16_t)0; |
396 | 0 | next[huff] = here; |
397 | 0 | } |
398 | | |
399 | | /* set return parameters */ |
400 | 39.0k | *table += used; |
401 | 39.0k | *bits = root; |
402 | 39.0k | return 0; |
403 | 39.0k | } |