zlib-streams 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/inflate.h ADDED
@@ -0,0 +1,128 @@
1
+ /* inflate.h -- internal inflate state definition
2
+ * Copyright (C) 1995-2019 Mark Adler
3
+ * Copyright (C) 2026 Gildas Lormeau (deflate64 support)
4
+ * For conditions of distribution and use, see copyright notice in zlib.h
5
+ */
6
+
7
+ /* WARNING: this file should *not* be used by applications. It is
8
+ part of the implementation of the compression library and is
9
+ subject to change. Applications should only use zlib.h.
10
+ */
11
+
12
+ /* define NO_GZIP when compiling if you want to disable gzip header and
13
+ trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
14
+ the crc code when it is not needed. For shared libraries, gzip decoding
15
+ should be left enabled. */
16
+ #ifndef NO_GZIP
17
+ # define GUNZIP
18
+ #endif
19
+
20
+ /* Possible inflate modes between inflate() calls */
21
+ typedef enum {
22
+ HEAD = 16180, /* i: waiting for magic header */
23
+ FLAGS, /* i: waiting for method and flags (gzip) */
24
+ TIME, /* i: waiting for modification time (gzip) */
25
+ OS, /* i: waiting for extra flags and operating system (gzip) */
26
+ EXLEN, /* i: waiting for extra length (gzip) */
27
+ EXTRA, /* i: waiting for extra bytes (gzip) */
28
+ NAME, /* i: waiting for end of file name (gzip) */
29
+ COMMENT, /* i: waiting for end of comment (gzip) */
30
+ HCRC, /* i: waiting for header crc (gzip) */
31
+ DICTID, /* i: waiting for dictionary check value */
32
+ DICT, /* waiting for inflateSetDictionary() call */
33
+ TYPE, /* i: waiting for type bits, including last-flag bit */
34
+ TYPEDO, /* i: same, but skip check to exit inflate on new block */
35
+ STORED, /* i: waiting for stored size (length and complement) */
36
+ COPY_, /* i/o: same as COPY below, but only first time in */
37
+ COPY, /* i/o: waiting for input or output to copy stored block */
38
+ TABLE, /* i: waiting for dynamic block table lengths */
39
+ LENLENS, /* i: waiting for code length code lengths */
40
+ CODELENS, /* i: waiting for length/lit and distance code lengths */
41
+ LEN_, /* i: same as LEN below, but only first time in */
42
+ LEN, /* i: waiting for length/lit/eob code */
43
+ LENEXT, /* i: waiting for length extra bits */
44
+ DIST, /* i: waiting for distance code */
45
+ DISTEXT, /* i: waiting for distance extra bits */
46
+ MATCH, /* o: waiting for output space to copy string */
47
+ LIT, /* o: waiting for output space to write literal */
48
+ CHECK, /* i: waiting for 32-bit check value */
49
+ LENGTH, /* i: waiting for 32-bit length (gzip) */
50
+ DONE, /* finished check, done -- remain here until reset */
51
+ BAD, /* got a data error -- remain here until reset */
52
+ MEM, /* got an inflate() memory error -- remain here until reset */
53
+ SYNC /* looking for synchronization bytes to restart inflate() */
54
+ } inflate_mode;
55
+
56
+ /*
57
+ State transitions between above modes -
58
+
59
+ (most modes can go to BAD or MEM on error -- not shown for clarity)
60
+
61
+ Process header:
62
+ HEAD -> (gzip) or (zlib) or (raw)
63
+ (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
64
+ HCRC -> TYPE
65
+ (zlib) -> DICTID or TYPE
66
+ DICTID -> DICT -> TYPE
67
+ (raw) -> TYPEDO
68
+ Read deflate blocks:
69
+ TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
70
+ STORED -> COPY_ -> COPY -> TYPE
71
+ TABLE -> LENLENS -> CODELENS -> LEN_
72
+ LEN_ -> LEN
73
+ Read deflate codes in fixed or dynamic block:
74
+ LEN -> LENEXT or LIT or TYPE
75
+ LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
76
+ LIT -> LEN
77
+ Process trailer:
78
+ CHECK -> LENGTH -> DONE
79
+ */
80
+
81
+ /* State maintained between inflate() calls -- approximately 7K bytes, not
82
+ including the allocated sliding window, which is up to 32K bytes. */
83
+ struct inflate_state {
84
+ z_streamp strm; /* pointer back to this zlib stream */
85
+ inflate_mode mode; /* current inflate mode */
86
+ int last; /* true if processing last block */
87
+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
88
+ bit 2 true to validate check value */
89
+ int havedict; /* true if dictionary provided */
90
+ int flags; /* gzip header method and flags, 0 if zlib, or
91
+ -1 if raw or no header yet */
92
+ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
93
+ unsigned long check; /* protected copy of check value */
94
+ unsigned long total; /* protected copy of output count */
95
+ gz_headerp head; /* where to save gzip header information */
96
+ /* sliding window */
97
+ unsigned wbits; /* log base 2 of requested window size */
98
+ unsigned wsize; /* window size or zero if not using window */
99
+ unsigned whave; /* valid bytes in the window */
100
+ unsigned wnext; /* window write index */
101
+ unsigned char FAR *window; /* allocated sliding window, if needed */
102
+ /* bit accumulator */
103
+ unsigned long hold; /* input bit accumulator */
104
+ unsigned bits; /* number of bits in hold */
105
+ /* for string and stored block copying */
106
+ unsigned length; /* literal or length of data to copy */
107
+ unsigned offset; /* distance back to copy string from */
108
+ /* for table and code decoding */
109
+ unsigned extra; /* extra bits needed */
110
+ /* fixed and dynamic code tables */
111
+ code const FAR *lencode; /* starting table for length/literal codes */
112
+ code const FAR *distcode; /* starting table for distance codes */
113
+ unsigned lenbits; /* index bits for lencode */
114
+ unsigned distbits; /* index bits for distcode */
115
+ /* dynamic table building */
116
+ unsigned ncode; /* number of code length code lengths */
117
+ unsigned nlen; /* number of length code lengths */
118
+ unsigned ndist; /* number of distance code lengths */
119
+ unsigned have; /* number of code lengths in lens[] */
120
+ code FAR *next; /* next available space in codes[] */
121
+ unsigned short lens[320]; /* temporary storage for code lengths */
122
+ unsigned short work[288]; /* work area for code table building */
123
+ code codes[ENOUGH]; /* space for code tables */
124
+ int sane; /* if false, allow invalid distance too far */
125
+ int back; /* bits back of last unprocessed length/lit */
126
+ unsigned was; /* initial length of match */
127
+ int deflate64; /* true when decoding raw deflate64 streams */
128
+ };
package/src/inftrees.c ADDED
@@ -0,0 +1,321 @@
1
+ /* inftrees.c -- generate Huffman trees for efficient decoding
2
+ * Copyright (C) 1995-2024 Mark Adler
3
+ * Copyright (C) 2026 Gildas Lormeau (deflate64 support)
4
+ * For conditions of distribution and use, see copyright notice in zlib.h
5
+ */
6
+
7
+ #include "zutil.h"
8
+ #include "inftrees.h"
9
+
10
+ #define MAXBITS 15
11
+
12
+ const char inflate_copyright[] =
13
+ " inflate 1.3.1.1 Copyright 1995-2024 Mark Adler ";
14
+ /*
15
+ If you use the zlib library in a product, an acknowledgment is welcome
16
+ in the documentation of your product. If for some reason you cannot
17
+ include such an acknowledgment, I would appreciate that you keep this
18
+ copyright string in the executable of your product.
19
+ */
20
+
21
+ /*
22
+ Build a set of tables to decode the provided canonical Huffman code.
23
+ The code lengths are lens[0..codes-1]. The result starts at *table,
24
+ whose indices are 0..2^bits-1. work is a writable array of at least
25
+ lens shorts, which is used as a work area. type is the type of code
26
+ to be generated, CODES, LENS, or DISTS. On return, zero is success,
27
+ -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
28
+ on return points to the next available entry's address. bits is the
29
+ requested root table index bits, and on return it is the actual root
30
+ table index bits. It will differ if the request is greater than the
31
+ longest code or if it is less than the shortest code.
32
+ */
33
+ int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
34
+ unsigned codes, code FAR * FAR *table,
35
+ unsigned FAR *bits, unsigned short FAR *work,
36
+ int deflate64) {
37
+ unsigned len; /* a code's length in bits */
38
+ unsigned sym; /* index of code symbols */
39
+ unsigned min, max; /* minimum and maximum code lengths */
40
+ unsigned root; /* number of index bits for root table */
41
+ unsigned curr; /* number of index bits for current table */
42
+ unsigned drop; /* code bits to drop for sub-table */
43
+ int left; /* number of prefix codes available */
44
+ unsigned used; /* code entries in table used */
45
+ unsigned huff; /* Huffman code */
46
+ unsigned incr; /* for incrementing code, index */
47
+ unsigned fill; /* index for replicating entries */
48
+ unsigned low; /* low bits for current root entry */
49
+ unsigned mask; /* mask for low root bits */
50
+ code here; /* table entry for duplication */
51
+ code FAR *next; /* next available space in table */
52
+ const unsigned short FAR *base; /* base value table to use */
53
+ const unsigned short FAR *extra; /* extra bits table to use */
54
+ unsigned match; /* use base and extra for symbol >= match */
55
+ unsigned enough_lens; /* table space limit for LENS */
56
+ unsigned enough_dists; /* table space limit for DISTS */
57
+ unsigned short count[MAXBITS+1]; /* number of codes of each length */
58
+ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
59
+ static const unsigned short lbase[31] = { /* Length codes 257..285 base */
60
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
61
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
62
+ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
63
+ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
64
+ 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 200};
65
+ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
66
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
67
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
68
+ 8193, 12289, 16385, 24577, 0, 0};
69
+ static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
70
+ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
71
+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
72
+ 28, 28, 29, 29, 64, 64};
73
+ static const unsigned short lbase9[31] = { /* Length codes 257..285 base */
74
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
75
+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3, 0, 0};
76
+ static const unsigned short lext9[31] = { /* Length codes 257..285 extra */
77
+ 128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129,
78
+ 130, 130, 130, 130, 131, 131, 131, 131, 132, 132, 132, 132,
79
+ 133, 133, 133, 133, 144, 73, 200};
80
+ static const unsigned short dbase9[32] = { /* Distance codes 0..31 base */
81
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
82
+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
83
+ 8193, 12289, 16385, 24577, 32769, 49153};
84
+ static const unsigned short dext9[32] = { /* Distance codes 0..31 extra */
85
+ 128, 128, 128, 128, 129, 129, 130, 130, 131, 131, 132, 132,
86
+ 133, 133, 134, 134, 135, 135, 136, 136, 137, 137, 138, 138,
87
+ 139, 139, 140, 140, 141, 141, 142, 142};
88
+
89
+ /*
90
+ Process a set of code lengths to create a canonical Huffman code. The
91
+ code lengths are lens[0..codes-1]. Each length corresponds to the
92
+ symbols 0..codes-1. The Huffman code is generated by first sorting the
93
+ symbols by length from short to long, and retaining the symbol order
94
+ for codes with equal lengths. Then the code starts with all zero bits
95
+ for the first code of the shortest length, and the codes are integer
96
+ increments for the same length, and zeros are appended as the length
97
+ increases. For the deflate format, these bits are stored backwards
98
+ from their more natural integer increment ordering, and so when the
99
+ decoding tables are built in the large loop below, the integer codes
100
+ are incremented backwards.
101
+
102
+ This routine assumes, but does not check, that all of the entries in
103
+ lens[] are in the range 0..MAXBITS. The caller must assure this.
104
+ 1..MAXBITS is interpreted as that code length. zero means that that
105
+ symbol does not occur in this code.
106
+
107
+ The codes are sorted by computing a count of codes for each length,
108
+ creating from that a table of starting indices for each length in the
109
+ sorted table, and then entering the symbols in order in the sorted
110
+ table. The sorted table is work[], with that space being provided by
111
+ the caller.
112
+
113
+ The length counts are used for other purposes as well, i.e. finding
114
+ the minimum and maximum length codes, determining if there are any
115
+ codes at all, checking for a valid set of lengths, and looking ahead
116
+ at length counts to determine sub-table sizes when building the
117
+ decoding tables.
118
+ */
119
+
120
+ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
121
+ for (len = 0; len <= MAXBITS; len++)
122
+ count[len] = 0;
123
+ for (sym = 0; sym < codes; sym++)
124
+ count[lens[sym]]++;
125
+
126
+ /* bound code lengths, force root to be within code lengths */
127
+ root = *bits;
128
+ for (max = MAXBITS; max >= 1; max--)
129
+ if (count[max] != 0) break;
130
+ if (root > max) root = max;
131
+ if (max == 0) { /* no symbols to code at all */
132
+ if (deflate64) return -1;
133
+ here.op = (unsigned char)64; /* invalid code marker */
134
+ here.bits = (unsigned char)1;
135
+ here.val = (unsigned short)0;
136
+ *(*table)++ = here; /* make a table to force an error */
137
+ *(*table)++ = here;
138
+ *bits = 1;
139
+ return 0; /* no symbols, but wait for decoding to report error */
140
+ }
141
+ for (min = 1; min < max; min++)
142
+ if (count[min] != 0) break;
143
+ if (root < min) root = min;
144
+
145
+ /* check for an over-subscribed or incomplete set of lengths */
146
+ left = 1;
147
+ for (len = 1; len <= MAXBITS; len++) {
148
+ left <<= 1;
149
+ left -= count[len];
150
+ if (left < 0) return -1; /* over-subscribed */
151
+ }
152
+ if (left > 0 && (type == CODES || max != 1))
153
+ return -1; /* incomplete set */
154
+
155
+ /* generate offsets into symbol table for each length for sorting */
156
+ offs[1] = 0;
157
+ for (len = 1; len < MAXBITS; len++)
158
+ offs[len + 1] = offs[len] + count[len];
159
+
160
+ /* sort symbols by length, by symbol order within each length */
161
+ for (sym = 0; sym < codes; sym++)
162
+ if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
163
+
164
+ /*
165
+ Create and fill in decoding tables. In this loop, the table being
166
+ filled is at next and has curr index bits. The code being used is huff
167
+ with length len. That code is converted to an index by dropping drop
168
+ bits off of the bottom. For codes where len is less than drop + curr,
169
+ those top drop + curr - len bits are incremented through all values to
170
+ fill the table with replicated entries.
171
+
172
+ root is the number of index bits for the root table. When len exceeds
173
+ root, sub-tables are created pointed to by the root entry with an index
174
+ of the low root bits of huff. This is saved in low to check for when a
175
+ new sub-table should be started. drop is zero when the root table is
176
+ being filled, and drop is root when sub-tables are being filled.
177
+
178
+ When a new sub-table is needed, it is necessary to look ahead in the
179
+ code lengths to determine what size sub-table is needed. The length
180
+ counts are used for this, and so count[] is decremented as codes are
181
+ entered in the tables.
182
+
183
+ used keeps track of how many table entries have been allocated from the
184
+ provided *table space. It is checked for LENS and DIST tables against
185
+ the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
186
+ the initial root table size constants. See the comments in inftrees.h
187
+ for more information.
188
+
189
+ sym increments through all symbols, and the loop terminates when
190
+ all codes of length max, i.e. all codes, have been processed. This
191
+ routine permits incomplete codes, so another loop after this one fills
192
+ in the rest of the decoding tables with invalid code markers.
193
+ */
194
+
195
+ /* set up for code type */
196
+ switch (type) {
197
+ case CODES:
198
+ base = extra = work; /* dummy value--not used */
199
+ match = 20;
200
+ break;
201
+ case LENS:
202
+ base = deflate64 ? lbase9 : lbase;
203
+ extra = deflate64 ? lext9 : lext;
204
+ match = 257;
205
+ break;
206
+ default: /* DISTS */
207
+ base = deflate64 ? dbase9 : dbase;
208
+ extra = deflate64 ? dext9 : dext;
209
+ match = 0;
210
+ }
211
+ enough_lens = deflate64 ? ENOUGH_LENS - 1 : ENOUGH_LENS;
212
+ enough_dists = deflate64 ? ENOUGH_DISTS_9 - 1 : ENOUGH_DISTS;
213
+
214
+ /* initialize state for loop */
215
+ huff = 0; /* starting code */
216
+ sym = 0; /* starting code symbol */
217
+ len = min; /* starting code length */
218
+ next = *table; /* current table to fill in */
219
+ curr = root; /* current table index bits */
220
+ drop = 0; /* current bits to drop from code for index */
221
+ low = (unsigned)(-1); /* trigger new sub-table when len > root */
222
+ used = 1U << root; /* use root table entries */
223
+ mask = used - 1; /* mask for comparing low */
224
+
225
+ /* check available table space */
226
+ if ((type == LENS && used > enough_lens) ||
227
+ (type == DISTS && used > enough_dists))
228
+ return 1;
229
+
230
+ /* process all codes and make table entries */
231
+ for (;;) {
232
+ /* create table entry */
233
+ here.bits = (unsigned char)(len - drop);
234
+ if (work[sym] + 1U < match) {
235
+ here.op = (unsigned char)0;
236
+ here.val = work[sym];
237
+ }
238
+ else if (work[sym] >= match) {
239
+ here.op = (unsigned char)(extra[work[sym] - match]);
240
+ here.val = base[work[sym] - match];
241
+ }
242
+ else {
243
+ here.op = (unsigned char)(32 + 64); /* end of block */
244
+ here.val = 0;
245
+ }
246
+
247
+ /* replicate for those indices with low len bits equal to huff */
248
+ incr = 1U << (len - drop);
249
+ fill = 1U << curr;
250
+ min = fill; /* save offset to next table */
251
+ do {
252
+ fill -= incr;
253
+ next[(huff >> drop) + fill] = here;
254
+ } while (fill != 0);
255
+
256
+ /* backwards increment the len-bit code huff */
257
+ incr = 1U << (len - 1);
258
+ while (huff & incr)
259
+ incr >>= 1;
260
+ if (incr != 0) {
261
+ huff &= incr - 1;
262
+ huff += incr;
263
+ }
264
+ else
265
+ huff = 0;
266
+
267
+ /* go to next symbol, update count, len */
268
+ sym++;
269
+ if (--(count[len]) == 0) {
270
+ if (len == max) break;
271
+ len = lens[work[sym]];
272
+ }
273
+
274
+ /* create new sub-table if needed */
275
+ if (len > root && (huff & mask) != low) {
276
+ /* if first time, transition to sub-tables */
277
+ if (drop == 0)
278
+ drop = root;
279
+
280
+ /* increment past last table */
281
+ next += min; /* here min is 1 << curr */
282
+
283
+ /* determine length of next table */
284
+ curr = len - drop;
285
+ left = (int)(1 << curr);
286
+ while (curr + drop < max) {
287
+ left -= count[curr + drop];
288
+ if (left <= 0) break;
289
+ curr++;
290
+ left <<= 1;
291
+ }
292
+
293
+ /* check for enough space */
294
+ used += 1U << curr;
295
+ if ((type == LENS && used > enough_lens) ||
296
+ (type == DISTS && used > enough_dists))
297
+ return 1;
298
+
299
+ /* point entry in root table to sub-table */
300
+ low = huff & mask;
301
+ (*table)[low].op = (unsigned char)curr;
302
+ (*table)[low].bits = (unsigned char)root;
303
+ (*table)[low].val = (unsigned short)(next - *table);
304
+ }
305
+ }
306
+
307
+ /* fill in remaining table entry if code is incomplete (guaranteed to have
308
+ at most one remaining entry, since if the code is incomplete, the
309
+ maximum code length that was allowed to get this far is one bit) */
310
+ if (huff != 0) {
311
+ here.op = (unsigned char)64; /* invalid code marker */
312
+ here.bits = (unsigned char)(len - drop);
313
+ here.val = (unsigned short)0;
314
+ next[huff] = here;
315
+ }
316
+
317
+ /* set return parameters */
318
+ *table += used;
319
+ *bits = root;
320
+ return 0;
321
+ }
package/src/inftrees.h ADDED
@@ -0,0 +1,67 @@
1
+ /* inftrees.h -- header to use inftrees.c
2
+ * Copyright (C) 1995-2005, 2010 Mark Adler
3
+ * Copyright (C) 2026 Gildas Lormeau (deflate64 support)
4
+ * For conditions of distribution and use, see copyright notice in zlib.h
5
+ */
6
+
7
+ /* WARNING: this file should *not* be used by applications. It is
8
+ part of the implementation of the compression library and is
9
+ subject to change. Applications should only use zlib.h.
10
+ */
11
+
12
+ /* Structure for decoding tables. Each entry provides either the
13
+ information needed to do the operation requested by the code that
14
+ indexed that table entry, or it provides a pointer to another
15
+ table that indexes more bits of the code. op indicates whether
16
+ the entry is a pointer to another table, a literal, a length or
17
+ distance, an end-of-block, or an invalid code. For a table
18
+ pointer, the low four bits of op is the number of index bits of
19
+ that table. For a length or distance, the low four bits of op
20
+ is the number of extra bits to get after the code. bits is
21
+ the number of bits in this code or part of the code to drop off
22
+ of the bit buffer. val is the actual byte to output in the case
23
+ of a literal, the base length or distance, or the offset from
24
+ the current table to the next table. Each entry is four bytes. */
25
+ typedef struct {
26
+ unsigned char op; /* operation, extra bits, table bits */
27
+ unsigned char bits; /* bits in this part of the code */
28
+ unsigned short val; /* offset in table or code value */
29
+ } code;
30
+
31
+ /* op values as set by inflate_table():
32
+ 00000000 - literal
33
+ 0000tttt - table link, tttt != 0 is the number of table index bits
34
+ 0001eeee - length or distance, eeee is the number of extra bits (deflate)
35
+ 100eeeee - length or distance, eeeee is the number of extra bits (deflate64)
36
+ 01100000 - end of block
37
+ 01000000 - invalid code
38
+ */
39
+
40
+ /* Maximum size of the dynamic table. The maximum number of code structures is
41
+ 1446, which is the sum of 852 for literal/length codes and 594 for distance
42
+ codes. These values were found by exhaustive searches using the program
43
+ examples/enough.c found in the zlib distribution. The arguments to that
44
+ program are the number of symbols, the initial root table size, and the
45
+ maximum bit length of a code. "enough 286 9 15" for literal/length codes
46
+ returns 852, "enough 30 6 15" for deflate distance codes returns 592, and
47
+ "enough 32 6 15" for deflate64 distance codes returns 594. The initial
48
+ root table size (9 or 6) is found in the fifth argument of the
49
+ inflate_table() calls in inflate.c and infback.c. If the root table size is
50
+ changed, then these maximum sizes would be need to be recalculated and
51
+ updated. */
52
+ #define ENOUGH_LENS 852
53
+ #define ENOUGH_DISTS 592
54
+ #define ENOUGH_DISTS_9 594
55
+ #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS_9)
56
+
57
+ /* Type of code to build for inflate_table() */
58
+ typedef enum {
59
+ CODES,
60
+ LENS,
61
+ DISTS
62
+ } codetype;
63
+
64
+ int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
65
+ unsigned codes, code FAR * FAR *table,
66
+ unsigned FAR *bits, unsigned short FAR *work,
67
+ int deflate64);
@@ -0,0 +1,80 @@
1
+ /* insert_string.h
2
+ *
3
+ * Copyright 2019 The Chromium Authors
4
+ * Use of this source code is governed by a BSD-style license that can be
5
+ * found in the Chromium source repository LICENSE file.
6
+ */
7
+
8
+ #ifndef INSERT_STRING_H
9
+ #define INSERT_STRING_H
10
+
11
+ #ifndef INLINE
12
+ #if defined(_MSC_VER) && !defined(__clang__)
13
+ #define INLINE __inline
14
+ #else
15
+ #define INLINE inline
16
+ #endif
17
+ #endif
18
+
19
+ #include <stdint.h>
20
+
21
+ /**
22
+ * Some applications need to match zlib DEFLATE output exactly [3]. Use the
23
+ * canonical zlib Rabin-Karp rolling hash [1,2] in that case.
24
+ *
25
+ * [1] For a description of the Rabin and Karp algorithm, see "Algorithms"
26
+ * book by R. Sedgewick, Addison-Wesley, p252.
27
+ * [2] https://www.euccas.me/zlib/#zlib_rabin_karp and also "rolling hash"
28
+ * https://en.wikipedia.org/wiki/Rolling_hash
29
+ * [3] crbug.com/1316541 AOSP incremental client APK package OTA upgrades.
30
+ */
31
+ #ifdef CHROMIUM_ZLIB_NO_CASTAGNOLI
32
+ #define USE_ZLIB_RABIN_KARP_ROLLING_HASH
33
+ #endif
34
+
35
+ /* ===========================================================================
36
+ * Update a hash value with the given input byte (Rabin-Karp rolling hash).
37
+ * IN assertion: all calls to UPDATE_HASH are made with consecutive input
38
+ * characters, so that a running hash key can be computed from the previous
39
+ * key instead of complete recalculation each time.
40
+ */
41
+ #define UPDATE_HASH(s, h, c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
42
+
43
+ /* ===========================================================================
44
+ * Insert string str in the dictionary and set match_head to the previous head
45
+ * of the hash chain (the most recent string with same hash key). Return
46
+ * the previous length of the hash chain.
47
+ * If this file is compiled with -DFASTEST, the compression level is forced
48
+ * to 1, and no hash chains are maintained.
49
+ * IN assertion: all calls to INSERT_STRING are made with consecutive input
50
+ * characters and the first MIN_MATCH bytes of str are valid (except for
51
+ * the last MIN_MATCH-1 bytes of the input file).
52
+ */
53
+ local INLINE Pos insert_string(deflate_state* const s, const Pos str) {
54
+ Pos ret;
55
+ /* insert_string dictionary insertion: ANZAC++ hasher
56
+ * significantly improves data compression speed.
57
+ *
58
+ * Note: the generated compressed output is a valid DEFLATE stream, but will
59
+ * differ from canonical zlib output.
60
+ */
61
+ #if defined(USE_ZLIB_RABIN_KARP_ROLLING_HASH)
62
+ UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH - 1)]);
63
+ #else
64
+ uint32_t value;
65
+ // Validated for little endian archs (i.e. x86, Arm). YMMV for big endian.
66
+ zmemcpy(&value, &s->window[str], sizeof(value));
67
+ s->ins_h = ((value * 66521 + 66521) >> 16) & s->hash_mask;
68
+ #endif
69
+
70
+ #ifdef FASTEST
71
+ ret = s->head[s->ins_h];
72
+ #else
73
+ ret = s->prev[str & s->w_mask] = s->head[s->ins_h];
74
+ #endif
75
+ s->head[s->ins_h] = str;
76
+
77
+ return ret;
78
+ }
79
+
80
+ #endif /* INSERT_STRING_H */