react / wstein / node_modules / browserify / node_modules / browserify-zlib / node_modules / pako / lib / zlib / inftrees.js
80549 views'use strict';123var utils = require('../utils/common');45var MAXBITS = 15;6var ENOUGH_LENS = 852;7var ENOUGH_DISTS = 592;8//var ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS);910var CODES = 0;11var LENS = 1;12var DISTS = 2;1314var lbase = [ /* Length codes 257..285 base */153, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,1635, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 017];1819var lext = [ /* Length codes 257..285 extra */2016, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,2119, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 7822];2324var dbase = [ /* Distance codes 0..29 base */251, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,26257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,278193, 12289, 16385, 24577, 0, 028];2930var dext = [ /* Distance codes 0..29 extra */3116, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,3223, 23, 24, 24, 25, 25, 26, 26, 27, 27,3328, 28, 29, 29, 64, 6434];3536module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts)37{38var bits = opts.bits;39//here = opts.here; /* table entry for duplication */4041var len = 0; /* a code's length in bits */42var sym = 0; /* index of code symbols */43var min = 0, max = 0; /* minimum and maximum code lengths */44var root = 0; /* number of index bits for root table */45var curr = 0; /* number of index bits for current table */46var drop = 0; /* code bits to drop for sub-table */47var left = 0; /* number of prefix codes available */48var used = 0; /* code entries in table used */49var huff = 0; /* Huffman code */50var incr; /* for incrementing code, index */51var fill; /* index for replicating entries */52var low; /* low bits for current root entry */53var mask; /* mask for low root bits */54var next; /* next available space in table */55var base = null; /* base value table to use */56var base_index = 0;57// var shoextra; /* extra bits table to use */58var end; /* use base and extra for symbol > end */59var count = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* number of codes of each length */60var offs = new utils.Buf16(MAXBITS+1); //[MAXBITS+1]; /* offsets in table for each length */61var extra = null;62var extra_index = 0;6364var here_bits, here_op, here_val;6566/*67Process a set of code lengths to create a canonical Huffman code. The68code lengths are lens[0..codes-1]. Each length corresponds to the69symbols 0..codes-1. The Huffman code is generated by first sorting the70symbols by length from short to long, and retaining the symbol order71for codes with equal lengths. Then the code starts with all zero bits72for the first code of the shortest length, and the codes are integer73increments for the same length, and zeros are appended as the length74increases. For the deflate format, these bits are stored backwards75from their more natural integer increment ordering, and so when the76decoding tables are built in the large loop below, the integer codes77are incremented backwards.7879This routine assumes, but does not check, that all of the entries in80lens[] are in the range 0..MAXBITS. The caller must assure this.811..MAXBITS is interpreted as that code length. zero means that that82symbol does not occur in this code.8384The codes are sorted by computing a count of codes for each length,85creating from that a table of starting indices for each length in the86sorted table, and then entering the symbols in order in the sorted87table. The sorted table is work[], with that space being provided by88the caller.8990The length counts are used for other purposes as well, i.e. finding91the minimum and maximum length codes, determining if there are any92codes at all, checking for a valid set of lengths, and looking ahead93at length counts to determine sub-table sizes when building the94decoding tables.95*/9697/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */98for (len = 0; len <= MAXBITS; len++) {99count[len] = 0;100}101for (sym = 0; sym < codes; sym++) {102count[lens[lens_index + sym]]++;103}104105/* bound code lengths, force root to be within code lengths */106root = bits;107for (max = MAXBITS; max >= 1; max--) {108if (count[max] !== 0) { break; }109}110if (root > max) {111root = max;112}113if (max === 0) { /* no symbols to code at all */114//table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */115//table.bits[opts.table_index] = 1; //here.bits = (var char)1;116//table.val[opts.table_index++] = 0; //here.val = (var short)0;117table[table_index++] = (1 << 24) | (64 << 16) | 0;118119120//table.op[opts.table_index] = 64;121//table.bits[opts.table_index] = 1;122//table.val[opts.table_index++] = 0;123table[table_index++] = (1 << 24) | (64 << 16) | 0;124125opts.bits = 1;126return 0; /* no symbols, but wait for decoding to report error */127}128for (min = 1; min < max; min++) {129if (count[min] !== 0) { break; }130}131if (root < min) {132root = min;133}134135/* check for an over-subscribed or incomplete set of lengths */136left = 1;137for (len = 1; len <= MAXBITS; len++) {138left <<= 1;139left -= count[len];140if (left < 0) {141return -1;142} /* over-subscribed */143}144if (left > 0 && (type === CODES || max !== 1)) {145return -1; /* incomplete set */146}147148/* generate offsets into symbol table for each length for sorting */149offs[1] = 0;150for (len = 1; len < MAXBITS; len++) {151offs[len + 1] = offs[len] + count[len];152}153154/* sort symbols by length, by symbol order within each length */155for (sym = 0; sym < codes; sym++) {156if (lens[lens_index + sym] !== 0) {157work[offs[lens[lens_index + sym]]++] = sym;158}159}160161/*162Create and fill in decoding tables. In this loop, the table being163filled is at next and has curr index bits. The code being used is huff164with length len. That code is converted to an index by dropping drop165bits off of the bottom. For codes where len is less than drop + curr,166those top drop + curr - len bits are incremented through all values to167fill the table with replicated entries.168169root is the number of index bits for the root table. When len exceeds170root, sub-tables are created pointed to by the root entry with an index171of the low root bits of huff. This is saved in low to check for when a172new sub-table should be started. drop is zero when the root table is173being filled, and drop is root when sub-tables are being filled.174175When a new sub-table is needed, it is necessary to look ahead in the176code lengths to determine what size sub-table is needed. The length177counts are used for this, and so count[] is decremented as codes are178entered in the tables.179180used keeps track of how many table entries have been allocated from the181provided *table space. It is checked for LENS and DIST tables against182the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in183the initial root table size constants. See the comments in inftrees.h184for more information.185186sym increments through all symbols, and the loop terminates when187all codes of length max, i.e. all codes, have been processed. This188routine permits incomplete codes, so another loop after this one fills189in the rest of the decoding tables with invalid code markers.190*/191192/* set up for code type */193// poor man optimization - use if-else instead of switch,194// to avoid deopts in old v8195if (type === CODES) {196base = extra = work; /* dummy value--not used */197end = 19;198} else if (type === LENS) {199base = lbase;200base_index -= 257;201extra = lext;202extra_index -= 257;203end = 256;204} else { /* DISTS */205base = dbase;206extra = dext;207end = -1;208}209210/* initialize opts for loop */211huff = 0; /* starting code */212sym = 0; /* starting code symbol */213len = min; /* starting code length */214next = table_index; /* current table to fill in */215curr = root; /* current table index bits */216drop = 0; /* current bits to drop from code for index */217low = -1; /* trigger new sub-table when len > root */218used = 1 << root; /* use root table entries */219mask = used - 1; /* mask for comparing low */220221/* check available table space */222if ((type === LENS && used > ENOUGH_LENS) ||223(type === DISTS && used > ENOUGH_DISTS)) {224return 1;225}226227var i=0;228/* process all codes and make table entries */229for (;;) {230i++;231/* create table entry */232here_bits = len - drop;233if (work[sym] < end) {234here_op = 0;235here_val = work[sym];236}237else if (work[sym] > end) {238here_op = extra[extra_index + work[sym]];239here_val = base[base_index + work[sym]];240}241else {242here_op = 32 + 64; /* end of block */243here_val = 0;244}245246/* replicate for those indices with low len bits equal to huff */247incr = 1 << (len - drop);248fill = 1 << curr;249min = fill; /* save offset to next table */250do {251fill -= incr;252table[next + (huff >> drop) + fill] = (here_bits << 24) | (here_op << 16) | here_val |0;253} while (fill !== 0);254255/* backwards increment the len-bit code huff */256incr = 1 << (len - 1);257while (huff & incr) {258incr >>= 1;259}260if (incr !== 0) {261huff &= incr - 1;262huff += incr;263} else {264huff = 0;265}266267/* go to next symbol, update count, len */268sym++;269if (--count[len] === 0) {270if (len === max) { break; }271len = lens[lens_index + work[sym]];272}273274/* create new sub-table if needed */275if (len > root && (huff & mask) !== low) {276/* if first time, transition to sub-tables */277if (drop === 0) {278drop = root;279}280281/* increment past last table */282next += min; /* here min is 1 << curr */283284/* determine length of next table */285curr = len - drop;286left = 1 << curr;287while (curr + drop < max) {288left -= count[curr + drop];289if (left <= 0) { break; }290curr++;291left <<= 1;292}293294/* check for enough space */295used += 1 << curr;296if ((type === LENS && used > ENOUGH_LENS) ||297(type === DISTS && used > ENOUGH_DISTS)) {298return 1;299}300301/* point entry in root table to sub-table */302low = huff & mask;303/*table.op[low] = curr;304table.bits[low] = root;305table.val[low] = next - opts.table_index;*/306table[low] = (root << 24) | (curr << 16) | (next - table_index) |0;307}308}309310/* fill in remaining table entry if code is incomplete (guaranteed to have311at most one remaining entry, since if the code is incomplete, the312maximum code length that was allowed to get this far is one bit) */313if (huff !== 0) {314//table.op[next + huff] = 64; /* invalid code marker */315//table.bits[next + huff] = len - drop;316//table.val[next + huff] = 0;317table[next + huff] = ((len - drop) << 24) | (64 << 16) |0;318}319320/* set return parameters */321//opts.table_index += used;322opts.bits = root;323return 0;324};325326327