4
/*****************************************************************************/
6
/* This file, inffast.c, contains the code required to compress and */
7
/* uncompress data using the GZIP_1 compression format. */
9
/* Copyright (C) 2004 Association of Universities for Research in Astronomy */
12
/* Redistribution and use in source and binary forms, with or without */
13
/* modification, are permitted provided that the following conditions are */
16
/* 1. Redistributions of source code must retain the above copyright */
17
/* notice, this list of conditions and the following disclaimer. */
19
/* 2. Redistributions in binary form must reproduce the above */
20
/* copyright notice, this list of conditions and the following */
21
/* disclaimer in the documentation and/or other materials provided */
22
/* with the distribution. */
24
/* 3. The name of AURA and its representatives may not be used to */
25
/* endorse or promote products derived from this software without */
26
/* specific prior written permission. */
28
/* THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED */
29
/* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
30
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
31
/* DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT, */
32
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, */
33
/* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS */
34
/* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND */
35
/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR */
36
/* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE */
37
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH */
40
/* This code was copied and heavily modified from the ZLIB compression */
41
/* library that was written by Jean-loup Gailly and Mark Adler. That */
42
/* software containes the following copyright and warranty notices: */
44
/* Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler */
46
/* This software is provided 'as-is', without any express or implied */
47
/* warranty. In no event will the authors be held liable for any damages */
48
/* arising from the use of this software. */
50
/* Permission is granted to anyone to use this software for any purpose, */
51
/* including commercial applications, and to alter it and redistribute it */
52
/* freely, subject to the following restrictions: */
54
/* 1. The origin of this software must not be misrepresented; you must not */
55
/* claim that you wrote the original software. If you use this software */
56
/* in a product, an acknowledgment in the product documentation would be */
57
/* appreciated but is not required. */
58
/* 2. Altered source versions must be plainly marked as such, and must not */
59
/* be misrepresented as being the original software. */
60
/* 3. This notice may not be removed or altered from any source */
63
/* Jean-loup Gailly Mark Adler */
64
/* jloup@gzip.org madler@alumni.caltech.edu */
67
/* The data format used by the zlib library is described by RFCs (Request */
68
/* for Comments) 1950 to 1952 in the files */
69
/* http://www.ietf.org/rfc/rfc1950.txt (zlib format), */
70
/* rfc1951.txt (deflate format) and rfc1952.txt (gzip format). */
72
/*****************************************************************************/
74
/* This code was copied unmodified from ZLIB inftrees.c -- generate Huffman */
75
/* trees for efficient decoding */
82
const char inflate_copyright[] =
83
" inflate 1.2.5 Copyright 1995-2010 Mark Adler ";
85
If you use the zlib library in a product, an acknowledgment is welcome
86
in the documentation of your product. If for some reason you cannot
87
include such an acknowledgment, I would appreciate that you keep this
88
copyright string in the executable of your product.
92
Build a set of tables to decode the provided canonical Huffman code.
93
The code lengths are lens[0..codes-1]. The result starts at *table,
94
whose indices are 0..2^bits-1. work is a writable array of at least
95
lens shorts, which is used as a work area. type is the type of code
96
to be generated, CODES, LENS, or DISTS. On return, zero is success,
97
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table
98
on return points to the next available entry's address. bits is the
99
requested root table index bits, and on return it is the actual root
100
table index bits. It will differ if the request is greater than the
101
longest code or if it is less than the shortest code.
103
int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
105
unsigned short FAR *lens;
107
code FAR * FAR *table;
109
unsigned short FAR *work;
111
unsigned len; /* a code's length in bits */
112
unsigned sym; /* index of code symbols */
113
unsigned min, max; /* minimum and maximum code lengths */
114
unsigned root; /* number of index bits for root table */
115
unsigned curr; /* number of index bits for current table */
116
unsigned drop; /* code bits to drop for sub-table */
117
int left; /* number of prefix codes available */
118
unsigned used; /* code entries in table used */
119
unsigned huff; /* Huffman code */
120
unsigned incr; /* for incrementing code, index */
121
unsigned fill; /* index for replicating entries */
122
unsigned low; /* low bits for current root entry */
123
unsigned mask; /* mask for low root bits */
124
code here; /* table entry for duplication */
125
code FAR *next; /* next available space in table */
126
const unsigned short FAR *base; /* base value table to use */
127
const unsigned short FAR *extra; /* extra bits table to use */
128
int end; /* use base and extra for symbol > end */
129
unsigned short count[MAXBITS+1]; /* number of codes of each length */
130
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
131
static const unsigned short lbase[31] = { /* Length codes 257..285 base */
132
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
133
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
134
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
135
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
136
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
137
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
138
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
139
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
140
8193, 12289, 16385, 24577, 0, 0};
141
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
142
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
143
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
144
28, 28, 29, 29, 64, 64};
147
Process a set of code lengths to create a canonical Huffman code. The
148
code lengths are lens[0..codes-1]. Each length corresponds to the
149
symbols 0..codes-1. The Huffman code is generated by first sorting the
150
symbols by length from short to long, and retaining the symbol order
151
for codes with equal lengths. Then the code starts with all zero bits
152
for the first code of the shortest length, and the codes are integer
153
increments for the same length, and zeros are appended as the length
154
increases. For the deflate format, these bits are stored backwards
155
from their more natural integer increment ordering, and so when the
156
decoding tables are built in the large loop below, the integer codes
157
are incremented backwards.
159
This routine assumes, but does not check, that all of the entries in
160
lens[] are in the range 0..MAXBITS. The caller must assure this.
161
1..MAXBITS is interpreted as that code length. zero means that that
162
symbol does not occur in this code.
164
The codes are sorted by computing a count of codes for each length,
165
creating from that a table of starting indices for each length in the
166
sorted table, and then entering the symbols in order in the sorted
167
table. The sorted table is work[], with that space being provided by
170
The length counts are used for other purposes as well, i.e. finding
171
the minimum and maximum length codes, determining if there are any
172
codes at all, checking for a valid set of lengths, and looking ahead
173
at length counts to determine sub-table sizes when building the
177
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
178
for (len = 0; len <= MAXBITS; len++)
180
for (sym = 0; sym < codes; sym++)
183
/* bound code lengths, force root to be within code lengths */
185
for (max = MAXBITS; max >= 1; max--)
186
if (count[max] != 0) break;
187
if (root > max) root = max;
188
if (max == 0) { /* no symbols to code at all */
189
here.op = (unsigned char)64; /* invalid code marker */
190
here.bits = (unsigned char)1;
191
here.val = (unsigned short)0;
192
*(*table)++ = here; /* make a table to force an error */
195
return 0; /* no symbols, but wait for decoding to report error */
197
for (min = 1; min < max; min++)
198
if (count[min] != 0) break;
199
if (root < min) root = min;
201
/* check for an over-subscribed or incomplete set of lengths */
203
for (len = 1; len <= MAXBITS; len++) {
206
if (left < 0) return -1; /* over-subscribed */
208
if (left > 0 && (type == CODES || max != 1))
209
return -1; /* incomplete set */
211
/* generate offsets into symbol table for each length for sorting */
213
for (len = 1; len < MAXBITS; len++)
214
offs[len + 1] = offs[len] + count[len];
216
/* sort symbols by length, by symbol order within each length */
217
for (sym = 0; sym < codes; sym++)
218
if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
221
Create and fill in decoding tables. In this loop, the table being
222
filled is at next and has curr index bits. The code being used is huff
223
with length len. That code is converted to an index by dropping drop
224
bits off of the bottom. For codes where len is less than drop + curr,
225
those top drop + curr - len bits are incremented through all values to
226
fill the table with replicated entries.
228
root is the number of index bits for the root table. When len exceeds
229
root, sub-tables are created pointed to by the root entry with an index
230
of the low root bits of huff. This is saved in low to check for when a
231
new sub-table should be started. drop is zero when the root table is
232
being filled, and drop is root when sub-tables are being filled.
234
When a new sub-table is needed, it is necessary to look ahead in the
235
code lengths to determine what size sub-table is needed. The length
236
counts are used for this, and so count[] is decremented as codes are
237
entered in the tables.
239
used keeps track of how many table entries have been allocated from the
240
provided *table space. It is checked for LENS and DIST tables against
241
the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
242
the initial root table size constants. See the comments in inftrees.h
243
for more information.
245
sym increments through all symbols, and the loop terminates when
246
all codes of length max, i.e. all codes, have been processed. This
247
routine permits incomplete codes, so another loop after this one fills
248
in the rest of the decoding tables with invalid code markers.
251
/* set up for code type */
254
base = extra = work; /* dummy value--not used */
270
/* initialize state for loop */
271
huff = 0; /* starting code */
272
sym = 0; /* starting code symbol */
273
len = min; /* starting code length */
274
next = *table; /* current table to fill in */
275
curr = root; /* current table index bits */
276
drop = 0; /* current bits to drop from code for index */
277
low = (unsigned)(-1); /* trigger new sub-table when len > root */
278
used = 1U << root; /* use root table entries */
279
mask = used - 1; /* mask for comparing low */
281
/* check available table space */
282
if ((type == LENS && used >= ENOUGH_LENS) ||
283
(type == DISTS && used >= ENOUGH_DISTS))
286
/* process all codes and make table entries */
288
/* create table entry */
289
here.bits = (unsigned char)(len - drop);
290
if ((int)(work[sym]) < end) {
291
here.op = (unsigned char)0;
292
here.val = work[sym];
294
else if ((int)(work[sym]) > end) {
295
here.op = (unsigned char)(extra[work[sym]]);
296
here.val = base[work[sym]];
299
here.op = (unsigned char)(32 + 64); /* end of block */
303
/* replicate for those indices with low len bits equal to huff */
304
incr = 1U << (len - drop);
306
min = fill; /* save offset to next table */
309
next[(huff >> drop) + fill] = here;
312
/* backwards increment the len-bit code huff */
313
incr = 1U << (len - 1);
323
/* go to next symbol, update count, len */
325
if (--(count[len]) == 0) {
326
if (len == max) break;
327
len = lens[work[sym]];
330
/* create new sub-table if needed */
331
if (len > root && (huff & mask) != low) {
332
/* if first time, transition to sub-tables */
336
/* increment past last table */
337
next += min; /* here min is 1 << curr */
339
/* determine length of next table */
341
left = (int)(1 << curr);
342
while (curr + drop < max) {
343
left -= count[curr + drop];
344
if (left <= 0) break;
349
/* check for enough space */
351
if ((type == LENS && used >= ENOUGH_LENS) ||
352
(type == DISTS && used >= ENOUGH_DISTS))
355
/* point entry in root table to sub-table */
357
(*table)[low].op = (unsigned char)curr;
358
(*table)[low].bits = (unsigned char)root;
359
(*table)[low].val = (unsigned short)(next - *table);
364
Fill in rest of table for incomplete codes. This loop is similar to the
365
loop above in incrementing huff for table indices. It is assumed that
366
len is equal to curr + drop, so there is no loop needed to increment
367
through high index bits. When the current sub-table is filled, the loop
368
drops back to the root table to fill in any remaining entries there.
370
here.op = (unsigned char)64; /* invalid code marker */
371
here.bits = (unsigned char)(len - drop);
372
here.val = (unsigned short)0;
374
/* when done with sub-table, drop back to root table */
375
if (drop != 0 && (huff & mask) != low) {
379
here.bits = (unsigned char)len;
382
/* put invalid code marker in table */
383
next[huff >> drop] = here;
385
/* backwards increment the len-bit code huff */
386
incr = 1U << (len - 1);
397
/* set return parameters */