2013-10-11 08:26:07 +00:00
|
|
|
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-11-19 22:32:56 +00:00
|
|
|
#include <string.h>
|
2013-10-11 08:26:07 +00:00
|
|
|
#include "./bit_reader.h"
|
|
|
|
#include "./context.h"
|
|
|
|
#include "./decode.h"
|
|
|
|
#include "./huffman.h"
|
|
|
|
#include "./prefix.h"
|
|
|
|
#include "./safe_malloc.h"
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BROTLI_DECODE_DEBUG
|
|
|
|
#define BROTLI_LOG_UINT(name) \
|
2013-11-15 18:02:17 +00:00
|
|
|
printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))
|
2013-10-11 08:26:07 +00:00
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \
|
2013-10-22 13:02:54 +00:00
|
|
|
printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
|
2013-11-15 18:02:17 +00:00
|
|
|
(unsigned long)(idx), (unsigned long)array_name[idx])
|
2013-10-11 08:26:07 +00:00
|
|
|
#else
|
|
|
|
#define BROTLI_LOG_UINT(name)
|
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const int kDefaultCodeLength = 8;
|
|
|
|
static const int kCodeLengthRepeatCode = 16;
|
|
|
|
static const int kNumLiteralCodes = 256;
|
|
|
|
static const int kNumInsertAndCopyCodes = 704;
|
|
|
|
static const int kNumBlockLengthCodes = 26;
|
2013-11-15 18:02:17 +00:00
|
|
|
static const int kLiteralContextBits = 6;
|
|
|
|
static const int kDistanceContextBits = 2;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-12-12 12:18:04 +00:00
|
|
|
#define CODE_LENGTH_CODES 18
|
2013-10-11 08:26:07 +00:00
|
|
|
static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = {
|
2013-12-12 12:18:04 +00:00
|
|
|
1, 2, 3, 4, 0, 17, 5, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
2013-10-11 08:26:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_DISTANCE_SHORT_CODES 16
|
|
|
|
static const int kDistanceShortCodeIndexOffset[NUM_DISTANCE_SHORT_CODES] = {
|
|
|
|
3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int kDistanceShortCodeValueOffset[NUM_DISTANCE_SHORT_CODES] = {
|
|
|
|
0, 0, 0, 0, -1, 1, -2, 2, -3, 3, -1, 1, -2, 2, -3, 3
|
|
|
|
};
|
|
|
|
|
2013-11-28 16:37:13 +00:00
|
|
|
static BROTLI_INLINE int DecodeWindowBits(BrotliBitReader* br) {
|
|
|
|
if (BrotliReadBits(br, 1)) {
|
|
|
|
return 17 + BrotliReadBits(br, 3);
|
|
|
|
} else {
|
|
|
|
return 16;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 12:18:04 +00:00
|
|
|
// Decodes a number in the range [0..255], by reading 1 - 11 bits.
|
|
|
|
static BROTLI_INLINE int DecodeVarLenUint8(BrotliBitReader* br) {
|
|
|
|
if (BrotliReadBits(br, 1)) {
|
|
|
|
int nbits = BrotliReadBits(br, 3);
|
|
|
|
if (nbits == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return BrotliReadBits(br, nbits) + (1 << nbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-28 16:37:13 +00:00
|
|
|
static void DecodeMetaBlockLength(BrotliBitReader* br,
|
2013-11-15 18:02:17 +00:00
|
|
|
size_t* meta_block_length,
|
2013-12-12 12:18:04 +00:00
|
|
|
int* input_end,
|
|
|
|
int* is_uncompressed) {
|
2013-11-15 18:02:17 +00:00
|
|
|
*input_end = BrotliReadBits(br, 1);
|
2013-11-28 16:37:13 +00:00
|
|
|
*meta_block_length = 0;
|
2013-12-12 12:18:04 +00:00
|
|
|
*is_uncompressed = 0;
|
2013-11-28 16:37:13 +00:00
|
|
|
if (*input_end && BrotliReadBits(br, 1)) {
|
|
|
|
return;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-28 16:37:13 +00:00
|
|
|
int size_nibbles = BrotliReadBits(br, 2) + 4;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size_nibbles; ++i) {
|
|
|
|
*meta_block_length |= BrotliReadBits(br, 4) << (i * 4);
|
|
|
|
}
|
|
|
|
++(*meta_block_length);
|
2013-12-12 12:18:04 +00:00
|
|
|
if (!*input_end) {
|
|
|
|
*is_uncompressed = BrotliReadBits(br, 1);
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decodes the next Huffman code from bit-stream.
|
|
|
|
static BROTLI_INLINE int ReadSymbol(const HuffmanTree* tree,
|
|
|
|
BrotliBitReader* br) {
|
2013-11-15 18:02:17 +00:00
|
|
|
const HuffmanTreeNode* node = tree->root_;
|
|
|
|
BrotliFillBitWindow(br);
|
|
|
|
uint32_t bits = BrotliPrefetchBits(br);
|
|
|
|
int bitpos = br->bit_pos_;
|
|
|
|
// Check if we find the bit combination from the Huffman lookup table.
|
|
|
|
const int lut_ix = bits & (HUFF_LUT - 1);
|
|
|
|
const int lut_bits = tree->lut_bits_[lut_ix];
|
|
|
|
if (lut_bits <= HUFF_LUT_BITS) {
|
|
|
|
BrotliSetBitPos(br, bitpos + lut_bits);
|
|
|
|
return tree->lut_symbol_[lut_ix];
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
node += tree->lut_jump_[lut_ix];
|
|
|
|
bitpos += HUFF_LUT_BITS;
|
|
|
|
bits >>= HUFF_LUT_BITS;
|
|
|
|
|
|
|
|
// Decode the value from a binary tree.
|
|
|
|
assert(node != NULL);
|
|
|
|
do {
|
|
|
|
node = HuffmanTreeNextNode(node, bits & 1);
|
|
|
|
bits >>= 1;
|
|
|
|
++bitpos;
|
|
|
|
} while (HuffmanTreeNodeIsNotLeaf(node));
|
|
|
|
BrotliSetBitPos(br, bitpos);
|
|
|
|
return node->symbol_;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
static void PrintUcharVector(const uint8_t* v, int len) {
|
2013-10-11 08:26:07 +00:00
|
|
|
while (len-- > 0) printf(" %d", *v++);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ReadHuffmanCodeLengths(
|
2013-11-15 18:02:17 +00:00
|
|
|
const uint8_t* code_length_code_lengths,
|
|
|
|
int num_symbols, uint8_t* code_lengths,
|
2013-10-11 08:26:07 +00:00
|
|
|
BrotliBitReader* br) {
|
|
|
|
int ok = 0;
|
|
|
|
int symbol;
|
|
|
|
int max_symbol;
|
2013-10-22 13:02:54 +00:00
|
|
|
int decode_number_of_code_length_codes;
|
2013-10-11 08:26:07 +00:00
|
|
|
int prev_code_len = kDefaultCodeLength;
|
2013-12-12 12:18:04 +00:00
|
|
|
int repeat = 0;
|
|
|
|
int repeat_length = 0;
|
2013-10-11 08:26:07 +00:00
|
|
|
HuffmanTree tree;
|
|
|
|
|
2013-10-17 10:41:36 +00:00
|
|
|
if (!BrotliHuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
|
|
|
|
CODE_LENGTH_CODES)) {
|
2013-10-11 08:26:07 +00:00
|
|
|
printf("[ReadHuffmanCodeLengths] Building code length tree failed: ");
|
2013-11-15 18:02:17 +00:00
|
|
|
PrintUcharVector(code_length_code_lengths, CODE_LENGTH_CODES);
|
2013-10-11 08:26:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliReadMoreInput(br)) {
|
|
|
|
printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-22 13:02:54 +00:00
|
|
|
decode_number_of_code_length_codes = BrotliReadBits(br, 1);
|
|
|
|
BROTLI_LOG_UINT(decode_number_of_code_length_codes);
|
|
|
|
if (decode_number_of_code_length_codes) {
|
2013-12-12 12:18:04 +00:00
|
|
|
if (BrotliReadBits(br, 1)) {
|
|
|
|
max_symbol = 68 + BrotliReadBits(br, 7);
|
|
|
|
} else {
|
|
|
|
max_symbol = 4 + BrotliReadBits(br, 6);
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
if (max_symbol > num_symbols) {
|
|
|
|
printf("[ReadHuffmanCodeLengths] max_symbol > num_symbols (%d vs %d)\n",
|
|
|
|
max_symbol, num_symbols);
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
max_symbol = num_symbols;
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(max_symbol);
|
|
|
|
|
|
|
|
symbol = 0;
|
2013-12-12 12:18:04 +00:00
|
|
|
while (symbol + repeat < num_symbols) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int code_len;
|
|
|
|
if (max_symbol-- == 0) break;
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliReadMoreInput(br)) {
|
|
|
|
printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n");
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
code_len = ReadSymbol(&tree, br);
|
|
|
|
BROTLI_LOG_UINT(symbol);
|
2013-12-12 12:18:04 +00:00
|
|
|
BROTLI_LOG_UINT(repeat);
|
|
|
|
BROTLI_LOG_UINT(repeat_length);
|
2013-10-11 08:26:07 +00:00
|
|
|
BROTLI_LOG_UINT(code_len);
|
2013-12-12 12:18:04 +00:00
|
|
|
if ((code_len < kCodeLengthRepeatCode) ||
|
|
|
|
(code_len == kCodeLengthRepeatCode && repeat_length == 0) ||
|
|
|
|
(code_len > kCodeLengthRepeatCode && repeat_length > 0)) {
|
|
|
|
while (repeat > 0) {
|
|
|
|
code_lengths[symbol++] = repeat_length;
|
|
|
|
--repeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (code_len < kCodeLengthRepeatCode) {
|
2013-10-11 08:26:07 +00:00
|
|
|
code_lengths[symbol++] = code_len;
|
|
|
|
if (code_len != 0) prev_code_len = code_len;
|
|
|
|
} else {
|
2013-12-12 12:18:04 +00:00
|
|
|
const int extra_bits = code_len - 14;
|
|
|
|
if (repeat > 0) {
|
|
|
|
repeat -= 2;
|
|
|
|
repeat <<= extra_bits;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-12-12 12:18:04 +00:00
|
|
|
repeat += BrotliReadBits(br, extra_bits) + 3;
|
|
|
|
repeat_length = (code_len == kCodeLengthRepeatCode ? prev_code_len : 0);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 12:18:04 +00:00
|
|
|
if (symbol + repeat > num_symbols) {
|
|
|
|
printf("[ReadHuffmanCodeLengths] symbol + repeat > num_symbols "
|
|
|
|
"(%d + %d vs %d)\n", symbol, repeat, num_symbols);
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
while (repeat-- > 0) code_lengths[symbol++] = repeat_length;
|
2013-10-11 08:26:07 +00:00
|
|
|
while (symbol < num_symbols) code_lengths[symbol++] = 0;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
End:
|
2013-10-17 10:41:36 +00:00
|
|
|
BrotliHuffmanTreeRelease(&tree);
|
2013-10-11 08:26:07 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ReadHuffmanCode(int alphabet_size,
|
|
|
|
HuffmanTree* tree,
|
|
|
|
BrotliBitReader* br) {
|
2013-11-15 18:02:17 +00:00
|
|
|
int ok = 1;
|
|
|
|
int simple_code;
|
|
|
|
uint8_t* code_lengths = NULL;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
code_lengths =
|
|
|
|
(uint8_t*)BrotliSafeMalloc((uint64_t)alphabet_size,
|
|
|
|
sizeof(*code_lengths));
|
|
|
|
if (code_lengths == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!BrotliReadMoreInput(br)) {
|
|
|
|
printf("[ReadHuffmanCode] Unexpected end of input.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
simple_code = BrotliReadBits(br, 1);
|
|
|
|
BROTLI_LOG_UINT(simple_code);
|
2013-10-11 08:26:07 +00:00
|
|
|
if (simple_code) { // Read symbols, codes & code lengths directly.
|
2013-11-15 18:02:17 +00:00
|
|
|
int i;
|
|
|
|
int max_bits_counter = alphabet_size - 1;
|
|
|
|
int max_bits = 0;
|
|
|
|
int symbols[4] = { 0 };
|
|
|
|
const int num_symbols = BrotliReadBits(br, 2) + 1;
|
|
|
|
while (max_bits_counter) {
|
|
|
|
max_bits_counter >>= 1;
|
|
|
|
++max_bits;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
memset(code_lengths, 0, alphabet_size);
|
|
|
|
for (i = 0; i < num_symbols; ++i) {
|
|
|
|
symbols[i] = BrotliReadBits(br, max_bits);
|
|
|
|
code_lengths[symbols[i]] = 2;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
code_lengths[symbols[0]] = 1;
|
|
|
|
switch (num_symbols) {
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
code_lengths[symbols[1]] = 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (BrotliReadBits(br, 1)) {
|
|
|
|
code_lengths[symbols[2]] = 3;
|
|
|
|
code_lengths[symbols[3]] = 3;
|
|
|
|
} else {
|
|
|
|
code_lengths[symbols[0]] = 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(num_symbols);
|
2013-10-11 08:26:07 +00:00
|
|
|
} else { // Decode Huffman-coded code lengths.
|
|
|
|
int i;
|
2013-11-15 18:02:17 +00:00
|
|
|
uint8_t code_length_code_lengths[CODE_LENGTH_CODES] = { 0 };
|
2013-12-12 12:18:04 +00:00
|
|
|
const int num_codes = BrotliReadBits(br, 4) + 3;
|
2013-10-11 08:26:07 +00:00
|
|
|
BROTLI_LOG_UINT(num_codes);
|
|
|
|
if (num_codes > CODE_LENGTH_CODES) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
for (i = BrotliReadBits(br, 1) * 2; i < num_codes; ++i) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int code_len_idx = kCodeLengthCodeOrder[i];
|
2013-11-15 18:02:17 +00:00
|
|
|
int v = BrotliReadBits(br, 2);
|
2013-11-28 16:37:13 +00:00
|
|
|
if (v == 1) {
|
2013-11-15 18:02:17 +00:00
|
|
|
v = BrotliReadBits(br, 1);
|
|
|
|
if (v == 0) {
|
|
|
|
v = 2;
|
|
|
|
} else {
|
|
|
|
v = BrotliReadBits(br, 1);
|
|
|
|
if (v == 0) {
|
|
|
|
v = 1;
|
|
|
|
} else {
|
|
|
|
v = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (v == 2) {
|
|
|
|
v = 4;
|
|
|
|
}
|
|
|
|
code_length_code_lengths[code_len_idx] = v;
|
2013-10-11 08:26:07 +00:00
|
|
|
BROTLI_LOG_ARRAY_INDEX(code_length_code_lengths, code_len_idx);
|
|
|
|
}
|
|
|
|
ok = ReadHuffmanCodeLengths(code_length_code_lengths, alphabet_size,
|
2013-11-15 18:02:17 +00:00
|
|
|
code_lengths, br);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
if (ok) {
|
|
|
|
ok = BrotliHuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
|
|
|
|
if (!ok) {
|
|
|
|
printf("[ReadHuffmanCode] HuffmanTreeBuildImplicit failed: ");
|
|
|
|
PrintUcharVector(code_lengths, alphabet_size);
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
free(code_lengths);
|
|
|
|
return ok;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ReadCopyDistance(const HuffmanTree* tree,
|
|
|
|
int num_direct_codes,
|
|
|
|
int postfix_bits,
|
|
|
|
uint32_t postfix_mask,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int code;
|
|
|
|
int nbits;
|
|
|
|
int postfix;
|
|
|
|
int offset;
|
|
|
|
code = ReadSymbol(tree, br);
|
2013-10-11 08:26:07 +00:00
|
|
|
if (code < num_direct_codes) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
code -= num_direct_codes;
|
2013-10-22 13:02:54 +00:00
|
|
|
postfix = code & postfix_mask;
|
2013-10-11 08:26:07 +00:00
|
|
|
code >>= postfix_bits;
|
2013-10-22 13:02:54 +00:00
|
|
|
nbits = (code >> 1) + 1;
|
|
|
|
offset = ((2 + (code & 1)) << nbits) - 4;
|
2013-10-11 08:26:07 +00:00
|
|
|
return (num_direct_codes +
|
|
|
|
((offset + BrotliReadBits(br, nbits)) << postfix_bits) +
|
|
|
|
postfix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ReadBlockLength(const HuffmanTree* tree, BrotliBitReader* br) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int code;
|
|
|
|
int nbits;
|
|
|
|
code = ReadSymbol(tree, br);
|
|
|
|
nbits = kBlockLengthPrefixCode[code].nbits;
|
2013-10-11 08:26:07 +00:00
|
|
|
return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReadInsertAndCopy(const HuffmanTree* tree,
|
|
|
|
int* insert_len,
|
|
|
|
int* copy_len,
|
|
|
|
int* copy_dist,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int code;
|
|
|
|
int range_idx;
|
|
|
|
int insert_code;
|
2013-11-15 18:02:17 +00:00
|
|
|
int insert_extra_bits;
|
2013-10-22 13:02:54 +00:00
|
|
|
int copy_code;
|
2013-11-15 18:02:17 +00:00
|
|
|
int copy_extra_bits;
|
2013-10-22 13:02:54 +00:00
|
|
|
code = ReadSymbol(tree, br);
|
|
|
|
range_idx = code >> 6;
|
2013-10-11 08:26:07 +00:00
|
|
|
if (range_idx >= 2) {
|
|
|
|
range_idx -= 2;
|
|
|
|
*copy_dist = -1;
|
|
|
|
} else {
|
|
|
|
*copy_dist = 0;
|
|
|
|
}
|
2013-11-19 22:32:56 +00:00
|
|
|
insert_code = kInsertRangeLut[range_idx] + ((code >> 3) & 7);
|
|
|
|
copy_code = kCopyRangeLut[range_idx] + (code & 7);
|
2013-11-15 18:02:17 +00:00
|
|
|
*insert_len = kInsertLengthPrefixCode[insert_code].offset;
|
|
|
|
insert_extra_bits = kInsertLengthPrefixCode[insert_code].nbits;
|
|
|
|
if (insert_extra_bits > 0) {
|
|
|
|
*insert_len += BrotliReadBits(br, insert_extra_bits);
|
|
|
|
}
|
|
|
|
*copy_len = kCopyLengthPrefixCode[copy_code].offset;
|
|
|
|
copy_extra_bits = kCopyLengthPrefixCode[copy_code].nbits;
|
|
|
|
if (copy_extra_bits > 0) {
|
|
|
|
*copy_len += BrotliReadBits(br, copy_extra_bits);
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
static int TranslateShortCodes(int code, int* ringbuffer, size_t index) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int val;
|
|
|
|
if (code < NUM_DISTANCE_SHORT_CODES) {
|
2013-11-15 18:02:17 +00:00
|
|
|
index += kDistanceShortCodeIndexOffset[code];
|
|
|
|
index &= 3;
|
|
|
|
val = ringbuffer[index] + kDistanceShortCodeValueOffset[code];
|
2013-10-11 08:26:07 +00:00
|
|
|
} else {
|
|
|
|
val = code - NUM_DISTANCE_SHORT_CODES + 1;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MoveToFront(uint8_t* v, uint8_t index) {
|
|
|
|
uint8_t value = v[index];
|
|
|
|
uint8_t i = index;
|
|
|
|
for (; i; --i) v[i] = v[i - 1];
|
|
|
|
v[0] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
|
|
|
|
uint8_t mtf[256];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
mtf[i] = i;
|
|
|
|
}
|
|
|
|
for (i = 0; i < v_len; ++i) {
|
|
|
|
uint8_t index = v[i];
|
|
|
|
v[i] = mtf[index];
|
|
|
|
if (index) MoveToFront(mtf, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains a collection of huffman trees with the same alphabet size.
|
|
|
|
typedef struct {
|
|
|
|
int alphabet_size;
|
|
|
|
int num_htrees;
|
|
|
|
HuffmanTree* htrees;
|
|
|
|
} HuffmanTreeGroup;
|
|
|
|
|
2013-10-17 10:41:36 +00:00
|
|
|
static void HuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size,
|
|
|
|
int ntrees) {
|
2013-10-11 08:26:07 +00:00
|
|
|
group->alphabet_size = alphabet_size;
|
|
|
|
group->num_htrees = ntrees;
|
|
|
|
group->htrees = (HuffmanTree*)malloc(sizeof(HuffmanTree) * ntrees);
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:41:36 +00:00
|
|
|
static void HuffmanTreeGroupRelease(HuffmanTreeGroup* group) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < group->num_htrees; ++i) {
|
2013-10-17 10:41:36 +00:00
|
|
|
BrotliHuffmanTreeRelease(&group->htrees[i]);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
free(group->htrees);
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:41:36 +00:00
|
|
|
static int HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < group->num_htrees; ++i) {
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!ReadHuffmanCode(group->alphabet_size, &group->htrees[i], br)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
static int DecodeContextMap(int context_map_size,
|
2013-10-17 10:41:36 +00:00
|
|
|
int* num_htrees,
|
|
|
|
uint8_t** context_map,
|
|
|
|
BrotliBitReader* br) {
|
2013-11-15 18:02:17 +00:00
|
|
|
int ok = 1;
|
|
|
|
if (!BrotliReadMoreInput(br)) {
|
|
|
|
printf("[DecodeContextMap] Unexpected end of input.\n");
|
|
|
|
return 0;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-12-12 12:18:04 +00:00
|
|
|
*num_htrees = DecodeVarLenUint8(br) + 1;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
BROTLI_LOG_UINT(context_map_size);
|
|
|
|
BROTLI_LOG_UINT(*num_htrees);
|
|
|
|
|
|
|
|
*context_map = (uint8_t*)malloc(context_map_size);
|
|
|
|
if (*num_htrees <= 1) {
|
|
|
|
memset(*context_map, 0, context_map_size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-22 13:02:54 +00:00
|
|
|
{
|
|
|
|
HuffmanTree tree_index_htree;
|
|
|
|
int use_rle_for_zeros = BrotliReadBits(br, 1);
|
|
|
|
int max_run_length_prefix = 0;
|
2013-11-19 22:32:56 +00:00
|
|
|
int i;
|
2013-10-22 13:02:54 +00:00
|
|
|
if (use_rle_for_zeros) {
|
|
|
|
max_run_length_prefix = BrotliReadBits(br, 4) + 1;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!ReadHuffmanCode(*num_htrees + max_run_length_prefix,
|
|
|
|
&tree_index_htree, br)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-19 22:32:56 +00:00
|
|
|
for (i = 0; i < context_map_size;) {
|
|
|
|
int code;
|
|
|
|
if (!BrotliReadMoreInput(br)) {
|
|
|
|
printf("[DecodeContextMap] Unexpected end of input.\n");
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
code = ReadSymbol(&tree_index_htree, br);
|
|
|
|
if (code == 0) {
|
|
|
|
(*context_map)[i] = 0;
|
|
|
|
++i;
|
|
|
|
} else if (code <= max_run_length_prefix) {
|
|
|
|
int reps = 1 + (1 << code) + BrotliReadBits(br, code);
|
|
|
|
while (--reps) {
|
2013-10-11 08:26:07 +00:00
|
|
|
(*context_map)[i] = 0;
|
|
|
|
++i;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-11-19 22:32:56 +00:00
|
|
|
} else {
|
|
|
|
(*context_map)[i] = code - max_run_length_prefix;
|
|
|
|
++i;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
End:
|
2013-10-22 13:02:54 +00:00
|
|
|
BrotliHuffmanTreeRelease(&tree_index_htree);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
if (BrotliReadBits(br, 1)) {
|
|
|
|
InverseMoveToFrontTransform(*context_map, context_map_size);
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
return ok;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BROTLI_INLINE void DecodeBlockType(const HuffmanTree* trees,
|
|
|
|
int tree_type,
|
|
|
|
int* block_types,
|
|
|
|
int* ringbuffers,
|
|
|
|
size_t* indexes,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
int* ringbuffer = ringbuffers + tree_type * 2;
|
|
|
|
size_t* index = indexes + tree_type;
|
|
|
|
int type_code = ReadSymbol(trees + tree_type, br);
|
|
|
|
int block_type;
|
|
|
|
if (type_code == 0) {
|
|
|
|
block_type = ringbuffer[*index & 1];
|
|
|
|
} else if (type_code == 1) {
|
|
|
|
block_type = ringbuffer[(*index - 1) & 1] + 1;
|
|
|
|
} else {
|
|
|
|
block_type = type_code - 2;
|
|
|
|
}
|
|
|
|
block_types[tree_type] = block_type;
|
|
|
|
ringbuffer[(*index) & 1] = block_type;
|
|
|
|
++(*index);
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
// Copy len bytes from src to dst. It can write up to ten extra bytes
|
|
|
|
// after the end of the copy.
|
|
|
|
//
|
|
|
|
// The main part of this loop is a simple copy of eight bytes at a time until
|
|
|
|
// we've copied (at least) the requested amount of bytes. However, if dst and
|
|
|
|
// src are less than eight bytes apart (indicating a repeating pattern of
|
|
|
|
// length < 8), we first need to expand the pattern in order to get the correct
|
|
|
|
// results. For instance, if the buffer looks like this, with the eight-byte
|
|
|
|
// <src> and <dst> patterns marked as intervals:
|
|
|
|
//
|
|
|
|
// abxxxxxxxxxxxx
|
|
|
|
// [------] src
|
|
|
|
// [------] dst
|
|
|
|
//
|
|
|
|
// a single eight-byte copy from <src> to <dst> will repeat the pattern once,
|
|
|
|
// after which we can move <dst> two bytes without moving <src>:
|
|
|
|
//
|
|
|
|
// ababxxxxxxxxxx
|
|
|
|
// [------] src
|
|
|
|
// [------] dst
|
|
|
|
//
|
|
|
|
// and repeat the exercise until the two no longer overlap.
|
|
|
|
//
|
|
|
|
// This allows us to do very well in the special case of one single byte
|
|
|
|
// repeated many times, without taking a big hit for more general cases.
|
|
|
|
//
|
|
|
|
// The worst case of extra writing past the end of the match occurs when
|
|
|
|
// dst - src == 1 and len == 1; the last copy will read from byte positions
|
|
|
|
// [0..7] and write to [4..11], whereas it was only supposed to write to
|
|
|
|
// position 1. Thus, ten excess bytes.
|
|
|
|
static BROTLI_INLINE void IncrementalCopyFastPath(
|
|
|
|
uint8_t* dst, const uint8_t* src, int len) {
|
|
|
|
if (src < dst) {
|
|
|
|
while (dst - src < 8) {
|
|
|
|
UNALIGNED_COPY64(dst, src);
|
|
|
|
len -= dst - src;
|
|
|
|
dst += dst - src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (len > 0) {
|
|
|
|
UNALIGNED_COPY64(dst, src);
|
|
|
|
src += 8;
|
|
|
|
dst += 8;
|
|
|
|
len -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
int BrotliDecompressedSize(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size) {
|
2013-11-15 18:02:17 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput input = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
|
2013-10-11 08:26:07 +00:00
|
|
|
BrotliBitReader br;
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliInitBitReader(&br, input)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-28 16:37:13 +00:00
|
|
|
DecodeWindowBits(&br);
|
|
|
|
size_t meta_block_len;
|
|
|
|
int input_end;
|
2013-12-12 12:18:04 +00:00
|
|
|
int is_uncompressed;
|
|
|
|
DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed);
|
2013-11-28 16:37:13 +00:00
|
|
|
if (!input_end) {
|
2013-11-15 18:02:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-11-28 16:37:13 +00:00
|
|
|
*decoded_size = meta_block_len;
|
2013-11-15 18:02:17 +00:00
|
|
|
return 1;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BrotliDecompressBuffer(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size,
|
|
|
|
uint8_t* decoded_buffer) {
|
2013-11-15 18:02:17 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput in = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
|
|
|
|
BrotliMemOutput mout;
|
|
|
|
BrotliOutput out = BrotliInitMemOutput(decoded_buffer, *decoded_size, &mout);
|
|
|
|
int success = BrotliDecompress(in, out);
|
|
|
|
*decoded_size = mout.pos;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BrotliDecompress(BrotliInput input, BrotliOutput output) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int ok = 1;
|
|
|
|
int i;
|
|
|
|
size_t pos = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
int input_end = 0;
|
|
|
|
int window_bits = 0;
|
2013-11-19 22:32:56 +00:00
|
|
|
size_t max_backward_distance;
|
2013-11-15 18:02:17 +00:00
|
|
|
size_t ringbuffer_size;
|
|
|
|
size_t ringbuffer_mask;
|
|
|
|
uint8_t* ringbuffer;
|
|
|
|
uint8_t* ringbuffer_end;
|
2013-10-22 13:02:54 +00:00
|
|
|
// This ring buffer holds a few past copy distances that will be used by
|
|
|
|
// some special distance codes.
|
2013-11-28 16:37:13 +00:00
|
|
|
int dist_rb[4] = { 16, 15, 11, 4 };
|
2013-10-22 13:02:54 +00:00
|
|
|
size_t dist_rb_idx = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
// The previous 2 bytes used for context.
|
|
|
|
uint8_t prev_byte1 = 0;
|
|
|
|
uint8_t prev_byte2 = 0;
|
2013-10-22 13:02:54 +00:00
|
|
|
HuffmanTreeGroup hgroup[3];
|
2013-10-11 08:26:07 +00:00
|
|
|
BrotliBitReader br;
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliInitBitReader(&br, input)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
// Decode window size.
|
2013-11-28 16:37:13 +00:00
|
|
|
window_bits = DecodeWindowBits(&br);
|
2013-11-19 22:32:56 +00:00
|
|
|
max_backward_distance = (1 << window_bits) - 16;
|
2013-11-15 18:02:17 +00:00
|
|
|
|
2013-11-28 16:37:13 +00:00
|
|
|
static const int kRingBufferWriteAheadSlack = 16;
|
|
|
|
|
|
|
|
static const int kMaxDictionaryWordLength = 0;
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
ringbuffer_size = 1 << window_bits;
|
|
|
|
ringbuffer_mask = ringbuffer_size - 1;
|
2013-11-28 16:37:13 +00:00
|
|
|
ringbuffer = (uint8_t*)malloc(ringbuffer_size +
|
|
|
|
kRingBufferWriteAheadSlack +
|
|
|
|
kMaxDictionaryWordLength);
|
2013-11-15 18:02:17 +00:00
|
|
|
ringbuffer_end = ringbuffer + ringbuffer_size;
|
|
|
|
|
|
|
|
while (!input_end && ok) {
|
2013-10-11 08:26:07 +00:00
|
|
|
size_t meta_block_len = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
size_t meta_block_end_pos;
|
2013-12-12 12:18:04 +00:00
|
|
|
int is_uncompressed;
|
|
|
|
uint32_t block_length[3] = { 1 << 28, 1 << 28, 1 << 28 };
|
2013-10-11 08:26:07 +00:00
|
|
|
int block_type[3] = { 0 };
|
2013-11-28 16:37:13 +00:00
|
|
|
int num_block_types[3] = { 1, 1, 1 };
|
2013-10-11 08:26:07 +00:00
|
|
|
int block_type_rb[6] = { 0, 1, 0, 1, 0, 1 };
|
|
|
|
size_t block_type_rb_index[3] = { 0 };
|
|
|
|
HuffmanTree block_type_trees[3];
|
|
|
|
HuffmanTree block_len_trees[3];
|
2013-10-22 13:02:54 +00:00
|
|
|
int distance_postfix_bits;
|
|
|
|
int num_direct_distance_codes;
|
|
|
|
uint32_t distance_postfix_mask;
|
|
|
|
int num_distance_codes;
|
|
|
|
uint8_t* context_map = NULL;
|
2013-11-15 18:02:17 +00:00
|
|
|
uint8_t* context_modes = NULL;
|
2013-10-22 13:02:54 +00:00
|
|
|
int num_literal_htrees;
|
|
|
|
uint8_t* dist_context_map = NULL;
|
|
|
|
int num_dist_htrees;
|
|
|
|
int context_offset = 0;
|
|
|
|
uint8_t* context_map_slice = NULL;
|
|
|
|
uint8_t literal_htree_index = 0;
|
|
|
|
int dist_context_offset = 0;
|
|
|
|
uint8_t* dist_context_map_slice = NULL;
|
|
|
|
uint8_t dist_htree_index = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
int context_lookup_offset1 = 0;
|
|
|
|
int context_lookup_offset2 = 0;
|
|
|
|
uint8_t context_mode;
|
2013-10-22 13:02:54 +00:00
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
hgroup[i].num_htrees = 0;
|
|
|
|
hgroup[i].htrees = NULL;
|
|
|
|
block_type_trees[i].root_ = NULL;
|
|
|
|
block_len_trees[i].root_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BrotliReadMoreInput(&br)) {
|
|
|
|
printf("[BrotliDecompress] Unexpected end of input.\n");
|
2013-10-22 13:02:54 +00:00
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
BROTLI_LOG_UINT(pos);
|
2013-12-12 12:18:04 +00:00
|
|
|
DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed);
|
2013-10-22 13:02:54 +00:00
|
|
|
BROTLI_LOG_UINT(meta_block_len);
|
2013-11-15 18:02:17 +00:00
|
|
|
if (meta_block_len == 0) {
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
meta_block_end_pos = pos + meta_block_len;
|
2013-12-12 12:18:04 +00:00
|
|
|
if (is_uncompressed) {
|
|
|
|
BrotliSetBitPos(&br, (br.bit_pos_ + 7) & ~7);
|
|
|
|
for (; pos < meta_block_end_pos; ++pos) {
|
|
|
|
ringbuffer[pos & ringbuffer_mask] = BrotliReadBits(&br, 8);
|
|
|
|
if ((pos & ringbuffer_mask) == ringbuffer_mask) {
|
|
|
|
if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
block_type_trees[i].root_ = NULL;
|
|
|
|
block_len_trees[i].root_ = NULL;
|
2013-12-12 12:18:04 +00:00
|
|
|
num_block_types[i] = DecodeVarLenUint8(&br) + 1;
|
|
|
|
if (num_block_types[i] >= 2) {
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!ReadHuffmanCode(
|
|
|
|
num_block_types[i] + 2, &block_type_trees[i], &br) ||
|
|
|
|
!ReadHuffmanCode(kNumBlockLengthCodes, &block_len_trees[i], &br)) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
block_length[i] = ReadBlockLength(&block_len_trees[i], &br);
|
|
|
|
block_type_rb_index[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BROTLI_LOG_UINT(num_block_types[0]);
|
|
|
|
BROTLI_LOG_UINT(num_block_types[1]);
|
|
|
|
BROTLI_LOG_UINT(num_block_types[2]);
|
|
|
|
BROTLI_LOG_UINT(block_length[0]);
|
|
|
|
BROTLI_LOG_UINT(block_length[1]);
|
|
|
|
BROTLI_LOG_UINT(block_length[2]);
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliReadMoreInput(&br)) {
|
|
|
|
printf("[BrotliDecompress] Unexpected end of input.\n");
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-22 13:02:54 +00:00
|
|
|
distance_postfix_bits = BrotliReadBits(&br, 2);
|
|
|
|
num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
|
2013-10-11 08:26:07 +00:00
|
|
|
(BrotliReadBits(&br, 4) << distance_postfix_bits);
|
2013-10-22 13:02:54 +00:00
|
|
|
distance_postfix_mask = (1 << distance_postfix_bits) - 1;
|
|
|
|
num_distance_codes = (num_direct_distance_codes +
|
|
|
|
(48 << distance_postfix_bits));
|
2013-11-15 18:02:17 +00:00
|
|
|
context_modes = (uint8_t*)malloc(num_block_types[0]);
|
|
|
|
for (i = 0; i < num_block_types[0]; ++i) {
|
|
|
|
context_modes[i] = BrotliReadBits(&br, 2) << 1;
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(context_modes, i);
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
BROTLI_LOG_UINT(num_direct_distance_codes);
|
|
|
|
BROTLI_LOG_UINT(distance_postfix_bits);
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!DecodeContextMap(num_block_types[0] << kLiteralContextBits,
|
|
|
|
&num_literal_htrees, &context_map, &br) ||
|
|
|
|
!DecodeContextMap(num_block_types[2] << kDistanceContextBits,
|
|
|
|
&num_dist_htrees, &dist_context_map, &br)) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
HuffmanTreeGroupInit(&hgroup[0], kNumLiteralCodes, num_literal_htrees);
|
|
|
|
HuffmanTreeGroupInit(&hgroup[1], kNumInsertAndCopyCodes,
|
|
|
|
num_block_types[1]);
|
|
|
|
HuffmanTreeGroupInit(&hgroup[2], num_distance_codes, num_dist_htrees);
|
|
|
|
|
|
|
|
for (i = 0; i < 3; ++i) {
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!HuffmanTreeGroupDecode(&hgroup[i], &br)) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 13:02:54 +00:00
|
|
|
context_map_slice = context_map;
|
|
|
|
dist_context_map_slice = dist_context_map;
|
2013-11-15 18:02:17 +00:00
|
|
|
context_mode = context_modes[block_type[0]];
|
|
|
|
context_lookup_offset1 = kContextLookupOffsets[context_mode];
|
|
|
|
context_lookup_offset2 = kContextLookupOffsets[context_mode + 1];
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
while (pos < meta_block_end_pos) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int insert_length;
|
|
|
|
int copy_length;
|
|
|
|
int distance_code;
|
|
|
|
int distance;
|
2013-11-19 22:32:56 +00:00
|
|
|
size_t max_distance;
|
2013-11-15 18:02:17 +00:00
|
|
|
uint8_t context;
|
2013-10-22 13:02:54 +00:00
|
|
|
int j;
|
2013-11-15 18:02:17 +00:00
|
|
|
const uint8_t* copy_src;
|
|
|
|
uint8_t* copy_dst;
|
|
|
|
if (!BrotliReadMoreInput(&br)) {
|
|
|
|
printf("[BrotliDecompress] Unexpected end of input.\n");
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
if (block_length[1] == 0) {
|
|
|
|
DecodeBlockType(block_type_trees, 1, block_type, block_type_rb,
|
|
|
|
block_type_rb_index, &br);
|
|
|
|
block_length[1] = ReadBlockLength(&block_len_trees[1], &br);
|
|
|
|
}
|
|
|
|
--block_length[1];
|
|
|
|
ReadInsertAndCopy(&hgroup[1].htrees[block_type[1]],
|
|
|
|
&insert_length, ©_length, &distance_code, &br);
|
|
|
|
BROTLI_LOG_UINT(insert_length);
|
|
|
|
BROTLI_LOG_UINT(copy_length);
|
|
|
|
BROTLI_LOG_UINT(distance_code);
|
|
|
|
for (j = 0; j < insert_length; ++j) {
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliReadMoreInput(&br)) {
|
|
|
|
printf("[BrotliDecompress] Unexpected end of input.\n");
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
if (block_length[0] == 0) {
|
|
|
|
DecodeBlockType(block_type_trees, 0, block_type, block_type_rb,
|
|
|
|
block_type_rb_index, &br);
|
|
|
|
block_length[0] = ReadBlockLength(&block_len_trees[0], &br);
|
2013-11-15 18:02:17 +00:00
|
|
|
context_offset = block_type[0] << kLiteralContextBits;
|
2013-10-11 08:26:07 +00:00
|
|
|
context_map_slice = context_map + context_offset;
|
2013-11-15 18:02:17 +00:00
|
|
|
context_mode = context_modes[block_type[0]];
|
|
|
|
context_lookup_offset1 = kContextLookupOffsets[context_mode];
|
|
|
|
context_lookup_offset2 = kContextLookupOffsets[context_mode + 1];
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
context = (kContextLookup[context_lookup_offset1 + prev_byte1] |
|
|
|
|
kContextLookup[context_lookup_offset2 + prev_byte2]);
|
|
|
|
BROTLI_LOG_UINT(context);
|
|
|
|
literal_htree_index = context_map_slice[context];
|
2013-10-11 08:26:07 +00:00
|
|
|
--block_length[0];
|
2013-11-15 18:02:17 +00:00
|
|
|
prev_byte2 = prev_byte1;
|
|
|
|
prev_byte1 = ReadSymbol(&hgroup[0].htrees[literal_htree_index], &br);
|
|
|
|
ringbuffer[pos & ringbuffer_mask] = prev_byte1;
|
2013-10-11 08:26:07 +00:00
|
|
|
BROTLI_LOG_UINT(literal_htree_index);
|
2013-11-15 18:02:17 +00:00
|
|
|
BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos & ringbuffer_mask);
|
|
|
|
if ((pos & ringbuffer_mask) == ringbuffer_mask) {
|
|
|
|
if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
++pos;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
if (pos == meta_block_end_pos) break;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
if (distance_code < 0) {
|
2013-11-15 18:02:17 +00:00
|
|
|
if (!BrotliReadMoreInput(&br)) {
|
|
|
|
printf("[BrotliDecompress] Unexpected end of input.\n");
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
if (block_length[2] == 0) {
|
|
|
|
DecodeBlockType(block_type_trees, 2, block_type, block_type_rb,
|
|
|
|
block_type_rb_index, &br);
|
|
|
|
block_length[2] = ReadBlockLength(&block_len_trees[2], &br);
|
|
|
|
dist_htree_index = block_type[2];
|
2013-11-15 18:02:17 +00:00
|
|
|
dist_context_offset = block_type[2] << kDistanceContextBits;
|
2013-10-11 08:26:07 +00:00
|
|
|
dist_context_map_slice = dist_context_map + dist_context_offset;
|
|
|
|
}
|
|
|
|
--block_length[2];
|
2013-11-15 18:02:17 +00:00
|
|
|
uint8_t context = copy_length > 4 ? 3 : copy_length - 2;
|
|
|
|
dist_htree_index = dist_context_map_slice[context];
|
2013-10-11 08:26:07 +00:00
|
|
|
distance_code = ReadCopyDistance(&hgroup[2].htrees[dist_htree_index],
|
|
|
|
num_direct_distance_codes,
|
|
|
|
distance_postfix_bits,
|
|
|
|
distance_postfix_mask,
|
|
|
|
&br);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the distance code to the actual distance by possibly looking
|
|
|
|
// up past distnaces from the ringbuffer.
|
2013-11-15 18:02:17 +00:00
|
|
|
distance = TranslateShortCodes(distance_code, dist_rb, dist_rb_idx);
|
|
|
|
if (distance_code > 0) {
|
|
|
|
dist_rb[dist_rb_idx & 3] = distance;
|
|
|
|
++dist_rb_idx;
|
|
|
|
}
|
2013-10-22 13:02:54 +00:00
|
|
|
BROTLI_LOG_UINT(distance);
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-11-19 22:32:56 +00:00
|
|
|
max_distance = max_backward_distance;
|
|
|
|
if (pos < max_distance) {
|
|
|
|
max_distance = pos;
|
|
|
|
}
|
|
|
|
|
2013-11-28 16:37:13 +00:00
|
|
|
copy_dst = &ringbuffer[pos & ringbuffer_mask];
|
|
|
|
|
2013-11-19 22:32:56 +00:00
|
|
|
if ((size_t)distance > max_distance) {
|
2013-11-28 16:37:13 +00:00
|
|
|
printf("Invalid backward reference. pos: %lu distance: %d "
|
|
|
|
"len: %d end: %lu\n", (unsigned long)pos, distance, copy_length,
|
2013-11-15 18:02:17 +00:00
|
|
|
(unsigned long)meta_block_end_pos);
|
2013-10-11 08:26:07 +00:00
|
|
|
ok = 0;
|
|
|
|
goto End;
|
2013-11-19 22:32:56 +00:00
|
|
|
} else {
|
|
|
|
if (pos + copy_length > meta_block_end_pos) {
|
2013-11-28 16:37:13 +00:00
|
|
|
printf("Invalid backward reference. pos: %lu distance: %d "
|
|
|
|
"len: %d end: %lu\n", (unsigned long)pos, distance,
|
|
|
|
copy_length, (unsigned long)meta_block_end_pos);
|
2013-11-19 22:32:56 +00:00
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
|
2013-11-19 22:32:56 +00:00
|
|
|
copy_src = &ringbuffer[(pos - distance) & ringbuffer_mask];
|
2013-11-15 18:02:17 +00:00
|
|
|
|
|
|
|
#if (defined(__x86_64__) || defined(_M_X64))
|
2013-11-19 22:32:56 +00:00
|
|
|
if (copy_src + copy_length <= ringbuffer_end &&
|
|
|
|
copy_dst + copy_length < ringbuffer_end) {
|
|
|
|
if (copy_length <= 16 && distance >= 8) {
|
|
|
|
UNALIGNED_COPY64(copy_dst, copy_src);
|
|
|
|
UNALIGNED_COPY64(copy_dst + 8, copy_src + 8);
|
|
|
|
} else {
|
|
|
|
IncrementalCopyFastPath(copy_dst, copy_src, copy_length);
|
|
|
|
}
|
|
|
|
pos += copy_length;
|
|
|
|
copy_length = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-19 22:32:56 +00:00
|
|
|
for (j = 0; j < copy_length; ++j) {
|
|
|
|
ringbuffer[pos & ringbuffer_mask] =
|
|
|
|
ringbuffer[(pos - distance) & ringbuffer_mask];
|
|
|
|
if ((pos & ringbuffer_mask) == ringbuffer_mask) {
|
|
|
|
if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
|
|
|
|
ok = 0;
|
|
|
|
goto End;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-11-19 22:32:56 +00:00
|
|
|
++pos;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we get here, we must have inserted at least one literal and made
|
|
|
|
// a copy of at least length two, therefore accessing the last 2 bytes is
|
|
|
|
// valid.
|
|
|
|
prev_byte1 = ringbuffer[(pos - 1) & ringbuffer_mask];
|
|
|
|
prev_byte2 = ringbuffer[(pos - 2) & ringbuffer_mask];
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
End:
|
2013-11-15 18:02:17 +00:00
|
|
|
free(context_modes);
|
2013-10-11 08:26:07 +00:00
|
|
|
free(context_map);
|
|
|
|
free(dist_context_map);
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
HuffmanTreeGroupRelease(&hgroup[i]);
|
2013-10-17 10:41:36 +00:00
|
|
|
BrotliHuffmanTreeRelease(&block_type_trees[i]);
|
|
|
|
BrotliHuffmanTreeRelease(&block_len_trees[i]);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
if (BrotliWrite(output, ringbuffer, pos & ringbuffer_mask) < 0) {
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
free(ringbuffer);
|
2013-10-11 08:26:07 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|