From 0350970de946950ce47c1948913827931d88fc11 Mon Sep 17 00:00:00 2001 From: Andrew Dillon Date: Wed, 19 Dec 2018 08:40:19 -0600 Subject: [PATCH] Remove circular dependency --- dec/decode.js | 128 +++++++++++++++++++------------------- dec/dictionary-browser.js | 4 +- dec/dictionary.js | 4 +- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/dec/decode.js b/dec/decode.js index 7b2aa77..0dc8160 100644 --- a/dec/decode.js +++ b/dec/decode.js @@ -61,17 +61,17 @@ function DecodeWindowBits(br) { if (br.readBits(1) === 0) { return 16; } - + n = br.readBits(3); if (n > 0) { return 17 + n; } - + n = br.readBits(3); if (n > 0) { return 8 + n; } - + return 17; } @@ -96,32 +96,32 @@ function MetaBlockLength() { } function DecodeMetaBlockLength(br) { - var out = new MetaBlockLength; + var out = new MetaBlockLength; var size_nibbles; var size_bytes; var i; - + out.input_end = br.readBits(1); if (out.input_end && br.readBits(1)) { return out; } - + size_nibbles = br.readBits(2) + 4; if (size_nibbles === 7) { out.is_metadata = true; - + if (br.readBits(1) !== 0) throw new Error('Invalid reserved bit'); - + size_bytes = br.readBits(2); if (size_bytes === 0) return out; - + for (i = 0; i < size_bytes; i++) { var next_byte = br.readBits(8); if (i + 1 === size_bytes && size_bytes > 1 && next_byte === 0) throw new Error('Invalid size byte'); - + out.meta_block_length |= next_byte << (i * 8); } } else { @@ -129,24 +129,24 @@ function DecodeMetaBlockLength(br) { var next_nibble = br.readBits(4); if (i + 1 === size_nibbles && size_nibbles > 4 && next_nibble === 0) throw new Error('Invalid size nibble'); - + out.meta_block_length |= next_nibble << (i * 4); } } - + ++out.meta_block_length; - + if (!out.input_end && !out.is_metadata) { out.is_uncompressed = br.readBits(1); } - + return out; } /* Decodes the next Huffman code from bit-stream. */ function ReadSymbol(table, index, br) { var start_index = index; - + var nbits; br.fillBitWindow(); index += (br.val_ >>> br.bit_pos_) & HUFFMAN_TABLE_MASK; @@ -166,17 +166,17 @@ function ReadHuffmanCodeLengths(code_length_code_lengths, num_symbols, code_leng var repeat = 0; var repeat_code_len = 0; var space = 32768; - + var table = []; for (var i = 0; i < 32; i++) table.push(new HuffmanCode(0, 0)); - + BrotliBuildHuffmanTable(table, 0, 5, code_length_code_lengths, CODE_LENGTH_CODES); while (symbol < num_symbols && space > 0) { var p = 0; var code_len; - + br.readMoreInput(); br.fillBitWindow(); p += (br.val_ >>> br.bit_pos_) & 31; @@ -211,12 +211,12 @@ function ReadHuffmanCodeLengths(code_length_code_lengths, num_symbols, code_leng if (symbol + repeat_delta > num_symbols) { throw new Error('[ReadHuffmanCodeLengths] symbol + repeat_delta > num_symbols'); } - + for (var x = 0; x < repeat_delta; x++) code_lengths[symbol + x] = repeat_code_len; - + symbol += repeat_delta; - + if (repeat_code_len !== 0) { space -= repeat_delta << (15 - repeat_code_len); } @@ -225,7 +225,7 @@ function ReadHuffmanCodeLengths(code_length_code_lengths, num_symbols, code_leng if (space !== 0) { throw new Error("[ReadHuffmanCodeLengths] space = " + space); } - + for (; symbol < num_symbols; symbol++) code_lengths[symbol] = 0; } @@ -234,9 +234,9 @@ function ReadHuffmanCode(alphabet_size, tables, table, br) { var table_size = 0; var simple_code_or_skip; var code_lengths = new Uint8Array(alphabet_size); - + br.readMoreInput(); - + /* simple_code_or_skip is used as follows: 1 for simple code; 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ @@ -272,7 +272,7 @@ function ReadHuffmanCode(alphabet_size, tables, table, br) { if (symbols[0] === symbols[1]) { throw new Error('[ReadHuffmanCode] invalid symbols'); } - + code_lengths[symbols[1]] = 1; break; case 4: @@ -284,7 +284,7 @@ function ReadHuffmanCode(alphabet_size, tables, table, br) { (symbols[2] === symbols[3])) { throw new Error('[ReadHuffmanCode] invalid symbols'); } - + if (br.readBits(1)) { code_lengths[symbols[2]] = 3; code_lengths[symbols[3]] = 3; @@ -300,9 +300,9 @@ function ReadHuffmanCode(alphabet_size, tables, table, br) { var num_codes = 0; /* Static Huffman code for the code length code lengths */ var huff = [ - new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(3, 2), + new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(3, 2), new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(4, 1), - new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(3, 2), + new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(3, 2), new HuffmanCode(2, 0), new HuffmanCode(2, 4), new HuffmanCode(2, 3), new HuffmanCode(4, 5) ]; for (i = simple_code_or_skip; i < CODE_LENGTH_CODES && space > 0; ++i) { @@ -319,19 +319,19 @@ function ReadHuffmanCode(alphabet_size, tables, table, br) { ++num_codes; } } - + if (!(num_codes === 1 || space === 0)) throw new Error('[ReadHuffmanCode] invalid num_codes or space'); - + ReadHuffmanCodeLengths(code_length_code_lengths, alphabet_size, code_lengths, br); } - + table_size = BrotliBuildHuffmanTable(tables, table, HUFFMAN_TABLE_BITS, code_lengths, alphabet_size); - + if (table_size === 0) { throw new Error("[ReadHuffmanCode] BuildHuffmanTable failed: "); } - + return table_size; } @@ -379,7 +379,7 @@ function InverseMoveToFrontTransform(v, v_len) { function HuffmanTreeGroup(alphabet_size, num_htrees) { this.alphabet_size = alphabet_size; this.num_htrees = num_htrees; - this.codes = new Array(num_htrees + num_htrees * kMaxHuffmanTableSize[(alphabet_size + 31) >>> 5]); + this.codes = new Array(num_htrees + num_htrees * kMaxHuffmanTableSize[(alphabet_size + 31) >>> 5]); this.htrees = new Uint32Array(num_htrees); } @@ -400,7 +400,7 @@ function DecodeContextMap(context_map_size, br) { var max_run_length_prefix = 0; var table; var i; - + br.readMoreInput(); var num_htrees = out.num_htrees = DecodeVarLenUint8(br) + 1; @@ -413,14 +413,14 @@ function DecodeContextMap(context_map_size, br) { if (use_rle_for_zeros) { max_run_length_prefix = br.readBits(4) + 1; } - + table = []; for (i = 0; i < HUFFMAN_MAX_TABLE_SIZE; i++) { table[i] = new HuffmanCode(0, 0); } - + ReadHuffmanCode(num_htrees + max_run_length_prefix, table, 0, br); - + for (i = 0; i < context_map_size;) { var code; @@ -446,7 +446,7 @@ function DecodeContextMap(context_map_size, br) { if (br.readBits(1)) { InverseMoveToFrontTransform(context_map, context_map_size); } - + return out; } @@ -507,7 +507,7 @@ function CopyUncompressedBlockToOutput(output, len, pos, ringbuffer, ringbuffer_ var tail = BrotliBitReader.IBUF_MASK + 1 - br_pos; for (var x = 0; x < tail; x++) ringbuffer[rb_pos + x] = br.buf_[br_pos + x]; - + nbytes -= tail; rb_pos += tail; len -= tail; @@ -516,7 +516,7 @@ function CopyUncompressedBlockToOutput(output, len, pos, ringbuffer, ringbuffer_ for (var x = 0; x < nbytes; x++) ringbuffer[rb_pos + x] = br.buf_[br_pos + x]; - + rb_pos += nbytes; len -= nbytes; @@ -524,7 +524,7 @@ function CopyUncompressedBlockToOutput(output, len, pos, ringbuffer, ringbuffer_ ringbuffer to its beginning and flush the ringbuffer to the output. */ if (rb_pos >= rb_size) { output.write(ringbuffer, rb_size); - rb_pos -= rb_size; + rb_pos -= rb_size; for (var x = 0; x < rb_pos; x++) ringbuffer[x] = ringbuffer[rb_size + x]; } @@ -572,20 +572,20 @@ exports.BrotliDecompressedSize = BrotliDecompressedSize; function BrotliDecompressBuffer(buffer, output_size) { var input = new BrotliInput(buffer); - + if (output_size == null) { output_size = BrotliDecompressedSize(buffer); } - + var output_buffer = new Uint8Array(output_size); var output = new BrotliOutput(output_buffer); - + BrotliDecompress(input, output); - + if (output.pos < output.buffer.length) { output.buffer = output.buffer.subarray(0, output.pos); } - + return output.buffer; } @@ -672,7 +672,7 @@ function BrotliDecompress(input, output) { } br.readMoreInput(); - + var _out = DecodeMetaBlockLength(br); meta_block_remaining_len = _out.meta_block_length; if (pos + meta_block_remaining_len > output.buffer.length) { @@ -680,26 +680,26 @@ function BrotliDecompress(input, output) { var tmp = new Uint8Array( pos + meta_block_remaining_len ); tmp.set( output.buffer ); output.buffer = tmp; - } + } input_end = _out.input_end; is_uncompressed = _out.is_uncompressed; - + if (_out.is_metadata) { JumpToByteBoundary(br); - + for (; meta_block_remaining_len > 0; --meta_block_remaining_len) { br.readMoreInput(); /* Read one byte and ignore it. */ br.readBits(8); } - + continue; } - + if (meta_block_remaining_len === 0) { continue; } - + if (is_uncompressed) { br.bit_pos_ = (br.bit_pos_ + 7) & ~7; CopyUncompressedBlockToOutput(output, meta_block_remaining_len, pos, @@ -707,7 +707,7 @@ function BrotliDecompress(input, output) { pos += meta_block_remaining_len; continue; } - + for (i = 0; i < 3; ++i) { num_block_types[i] = DecodeVarLenUint8(br) + 1; if (num_block_types[i] >= 2) { @@ -717,9 +717,9 @@ function BrotliDecompress(input, output) { block_type_rb_index[i] = 1; } } - + br.readMoreInput(); - + distance_postfix_bits = br.readBits(2); num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES + (br.readBits(4) << distance_postfix_bits); distance_postfix_mask = (1 << distance_postfix_bits) - 1; @@ -730,15 +730,15 @@ function BrotliDecompress(input, output) { br.readMoreInput(); context_modes[i] = (br.readBits(2) << 1); } - + var _o1 = DecodeContextMap(num_block_types[0] << kLiteralContextBits, br); num_literal_htrees = _o1.num_htrees; context_map = _o1.context_map; - + var _o2 = DecodeContextMap(num_block_types[2] << kDistanceContextBits, br); num_dist_htrees = _o2.num_htrees; dist_context_map = _o2.context_map; - + hgroup[0] = new HuffmanTreeGroup(kNumLiteralCodes, num_literal_htrees); hgroup[1] = new HuffmanTreeGroup(kNumInsertAndCopyCodes, num_block_types[1]); hgroup[2] = new HuffmanTreeGroup(num_distance_codes, num_dist_htrees); @@ -768,7 +768,7 @@ function BrotliDecompress(input, output) { var copy_dst; br.readMoreInput(); - + if (block_length[1] === 0) { DecodeBlockType(num_block_types[1], block_type_trees, 1, block_type, block_type_rb, @@ -824,7 +824,7 @@ function BrotliDecompress(input, output) { if (distance_code < 0) { var context; - + br.readMoreInput(); if (block_length[2] === 0) { DecodeBlockType(num_block_types[2], @@ -886,7 +886,7 @@ function BrotliDecompress(input, output) { meta_block_remaining_len -= len; if (copy_dst >= ringbuffer_end) { output.write(ringbuffer, ringbuffer_size); - + for (var _x = 0; _x < (copy_dst - ringbuffer_end); _x++) ringbuffer[_x] = ringbuffer[ringbuffer_end + _x]; } @@ -935,4 +935,4 @@ function BrotliDecompress(input, output) { exports.BrotliDecompress = BrotliDecompress; -BrotliDictionary.init(); +BrotliDictionary.init(BrotliDecompressBuffer); diff --git a/dec/dictionary-browser.js b/dec/dictionary-browser.js index c0e2e59..0cbcb33 100644 --- a/dec/dictionary-browser.js +++ b/dec/dictionary-browser.js @@ -7,8 +7,8 @@ var base64 = require('base64-js'); * the dictionary, and on initial load, Brotli decompresses * it's own dictionary. 😜 */ -exports.init = function() { - var BrotliDecompressBuffer = require('./decode').BrotliDecompressBuffer; +exports.init = function(BrotliDecompressBuffer) { + // var BrotliDecompressBuffer = require('./decode').BrotliDecompressBuffer; var compressed = base64.toByteArray(require('./dictionary.bin.js')); return BrotliDecompressBuffer(compressed); }; diff --git a/dec/dictionary.js b/dec/dictionary.js index 2d1d478..df6a021 100644 --- a/dec/dictionary.js +++ b/dec/dictionary.js @@ -16,8 +16,8 @@ */ var data = require('./dictionary-browser'); -exports.init = function() { - exports.dictionary = data.init(); +exports.init = function(BrotliDecompressBuffer) { + exports.dictionary = data.init(BrotliDecompressBuffer); }; exports.offsetsByLength = new Uint32Array([