Path: blob/master/src/java.base/share/native/libzip/zlib/inftrees.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* inftrees.c -- generate Huffman trees for efficient decoding25* Copyright (C) 1995-2017 Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829#include "zutil.h"30#include "inftrees.h"3132#define MAXBITS 153334const char inflate_copyright[] =35" inflate 1.2.11 Copyright 1995-2017 Mark Adler ";36/*37If you use the zlib library in a product, an acknowledgment is welcome38in the documentation of your product. If for some reason you cannot39include such an acknowledgment, I would appreciate that you keep this40copyright string in the executable of your product.41*/4243/*44Build a set of tables to decode the provided canonical Huffman code.45The code lengths are lens[0..codes-1]. The result starts at *table,46whose indices are 0..2^bits-1. work is a writable array of at least47lens shorts, which is used as a work area. type is the type of code48to be generated, CODES, LENS, or DISTS. On return, zero is success,49-1 is an invalid code, and +1 means that ENOUGH isn't enough. table50on return points to the next available entry's address. bits is the51requested root table index bits, and on return it is the actual root52table index bits. It will differ if the request is greater than the53longest code or if it is less than the shortest code.54*/55int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)56codetype type;57unsigned short FAR *lens;58unsigned codes;59code FAR * FAR *table;60unsigned FAR *bits;61unsigned short FAR *work;62{63unsigned len; /* a code's length in bits */64unsigned sym; /* index of code symbols */65unsigned min, max; /* minimum and maximum code lengths */66unsigned root; /* number of index bits for root table */67unsigned curr; /* number of index bits for current table */68unsigned drop; /* code bits to drop for sub-table */69int left; /* number of prefix codes available */70unsigned used; /* code entries in table used */71unsigned huff; /* Huffman code */72unsigned incr; /* for incrementing code, index */73unsigned fill; /* index for replicating entries */74unsigned low; /* low bits for current root entry */75unsigned mask; /* mask for low root bits */76code here; /* table entry for duplication */77code FAR *next; /* next available space in table */78const unsigned short FAR *base; /* base value table to use */79const unsigned short FAR *extra; /* extra bits table to use */80unsigned match; /* use base and extra for symbol >= match */81unsigned short count[MAXBITS+1]; /* number of codes of each length */82unsigned short offs[MAXBITS+1]; /* offsets in table for each length */83static const unsigned short lbase[31] = { /* Length codes 257..285 base */843, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,8535, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};86static const unsigned short lext[31] = { /* Length codes 257..285 extra */8716, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,8819, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};89static const unsigned short dbase[32] = { /* Distance codes 0..29 base */901, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,91257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,928193, 12289, 16385, 24577, 0, 0};93static const unsigned short dext[32] = { /* Distance codes 0..29 extra */9416, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,9523, 23, 24, 24, 25, 25, 26, 26, 27, 27,9628, 28, 29, 29, 64, 64};9798/*99Process a set of code lengths to create a canonical Huffman code. The100code lengths are lens[0..codes-1]. Each length corresponds to the101symbols 0..codes-1. The Huffman code is generated by first sorting the102symbols by length from short to long, and retaining the symbol order103for codes with equal lengths. Then the code starts with all zero bits104for the first code of the shortest length, and the codes are integer105increments for the same length, and zeros are appended as the length106increases. For the deflate format, these bits are stored backwards107from their more natural integer increment ordering, and so when the108decoding tables are built in the large loop below, the integer codes109are incremented backwards.110111This routine assumes, but does not check, that all of the entries in112lens[] are in the range 0..MAXBITS. The caller must assure this.1131..MAXBITS is interpreted as that code length. zero means that that114symbol does not occur in this code.115116The codes are sorted by computing a count of codes for each length,117creating from that a table of starting indices for each length in the118sorted table, and then entering the symbols in order in the sorted119table. The sorted table is work[], with that space being provided by120the caller.121122The length counts are used for other purposes as well, i.e. finding123the minimum and maximum length codes, determining if there are any124codes at all, checking for a valid set of lengths, and looking ahead125at length counts to determine sub-table sizes when building the126decoding tables.127*/128129/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */130for (len = 0; len <= MAXBITS; len++)131count[len] = 0;132for (sym = 0; sym < codes; sym++)133count[lens[sym]]++;134135/* bound code lengths, force root to be within code lengths */136root = *bits;137for (max = MAXBITS; max >= 1; max--)138if (count[max] != 0) break;139if (root > max) root = max;140if (max == 0) { /* no symbols to code at all */141here.op = (unsigned char)64; /* invalid code marker */142here.bits = (unsigned char)1;143here.val = (unsigned short)0;144*(*table)++ = here; /* make a table to force an error */145*(*table)++ = here;146*bits = 1;147return 0; /* no symbols, but wait for decoding to report error */148}149for (min = 1; min < max; min++)150if (count[min] != 0) break;151if (root < min) root = min;152153/* check for an over-subscribed or incomplete set of lengths */154left = 1;155for (len = 1; len <= MAXBITS; len++) {156left <<= 1;157left -= count[len];158if (left < 0) return -1; /* over-subscribed */159}160if (left > 0 && (type == CODES || max != 1))161return -1; /* incomplete set */162163/* generate offsets into symbol table for each length for sorting */164offs[1] = 0;165for (len = 1; len < MAXBITS; len++)166offs[len + 1] = offs[len] + count[len];167168/* sort symbols by length, by symbol order within each length */169for (sym = 0; sym < codes; sym++)170if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;171172/*173Create and fill in decoding tables. In this loop, the table being174filled is at next and has curr index bits. The code being used is huff175with length len. That code is converted to an index by dropping drop176bits off of the bottom. For codes where len is less than drop + curr,177those top drop + curr - len bits are incremented through all values to178fill the table with replicated entries.179180root is the number of index bits for the root table. When len exceeds181root, sub-tables are created pointed to by the root entry with an index182of the low root bits of huff. This is saved in low to check for when a183new sub-table should be started. drop is zero when the root table is184being filled, and drop is root when sub-tables are being filled.185186When a new sub-table is needed, it is necessary to look ahead in the187code lengths to determine what size sub-table is needed. The length188counts are used for this, and so count[] is decremented as codes are189entered in the tables.190191used keeps track of how many table entries have been allocated from the192provided *table space. It is checked for LENS and DIST tables against193the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in194the initial root table size constants. See the comments in inftrees.h195for more information.196197sym increments through all symbols, and the loop terminates when198all codes of length max, i.e. all codes, have been processed. This199routine permits incomplete codes, so another loop after this one fills200in the rest of the decoding tables with invalid code markers.201*/202203/* set up for code type */204switch (type) {205case CODES:206base = extra = work; /* dummy value--not used */207match = 20;208break;209case LENS:210base = lbase;211extra = lext;212match = 257;213break;214default: /* DISTS */215base = dbase;216extra = dext;217match = 0;218}219220/* initialize state for loop */221huff = 0; /* starting code */222sym = 0; /* starting code symbol */223len = min; /* starting code length */224next = *table; /* current table to fill in */225curr = root; /* current table index bits */226drop = 0; /* current bits to drop from code for index */227low = (unsigned)(-1); /* trigger new sub-table when len > root */228used = 1U << root; /* use root table entries */229mask = used - 1; /* mask for comparing low */230231/* check available table space */232if ((type == LENS && used > ENOUGH_LENS) ||233(type == DISTS && used > ENOUGH_DISTS))234return 1;235236/* process all codes and make table entries */237for (;;) {238/* create table entry */239here.bits = (unsigned char)(len - drop);240if (work[sym] + 1U < match) {241here.op = (unsigned char)0;242here.val = work[sym];243}244else if (work[sym] >= match) {245here.op = (unsigned char)(extra[work[sym] - match]);246here.val = base[work[sym] - match];247}248else {249here.op = (unsigned char)(32 + 64); /* end of block */250here.val = 0;251}252253/* replicate for those indices with low len bits equal to huff */254incr = 1U << (len - drop);255fill = 1U << curr;256min = fill; /* save offset to next table */257do {258fill -= incr;259next[(huff >> drop) + fill] = here;260} while (fill != 0);261262/* backwards increment the len-bit code huff */263incr = 1U << (len - 1);264while (huff & incr)265incr >>= 1;266if (incr != 0) {267huff &= incr - 1;268huff += incr;269}270else271huff = 0;272273/* go to next symbol, update count, len */274sym++;275if (--(count[len]) == 0) {276if (len == max) break;277len = lens[work[sym]];278}279280/* create new sub-table if needed */281if (len > root && (huff & mask) != low) {282/* if first time, transition to sub-tables */283if (drop == 0)284drop = root;285286/* increment past last table */287next += min; /* here min is 1 << curr */288289/* determine length of next table */290curr = len - drop;291left = (int)(1 << curr);292while (curr + drop < max) {293left -= count[curr + drop];294if (left <= 0) break;295curr++;296left <<= 1;297}298299/* check for enough space */300used += 1U << curr;301if ((type == LENS && used > ENOUGH_LENS) ||302(type == DISTS && used > ENOUGH_DISTS))303return 1;304305/* point entry in root table to sub-table */306low = huff & mask;307(*table)[low].op = (unsigned char)curr;308(*table)[low].bits = (unsigned char)root;309(*table)[low].val = (unsigned short)(next - *table);310}311}312313/* fill in remaining table entry if code is incomplete (guaranteed to have314at most one remaining entry, since if the code is incomplete, the315maximum code length that was allowed to get this far is one bit) */316if (huff != 0) {317here.op = (unsigned char)64; /* invalid code marker */318here.bits = (unsigned char)(len - drop);319here.val = (unsigned short)0;320next[huff] = here;321}322323/* set return parameters */324*table += used;325*bits = root;326return 0;327}328329330