2013-12-16 13:45:57 +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.
|
|
|
|
*/
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
#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"
|
2014-02-17 13:25:36 +00:00
|
|
|
#include "./dictionary.h"
|
2015-05-07 14:53:43 +00:00
|
|
|
#include "./port.h"
|
2014-02-17 13:25:36 +00:00
|
|
|
#include "./transform.h"
|
2013-10-11 08:26:07 +00:00
|
|
|
#include "./huffman.h"
|
|
|
|
#include "./prefix.h"
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
#ifdef __ARM_NEON__
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#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])
|
2015-06-26 15:37:00 +00:00
|
|
|
#define BROTLI_LOG(x) printf x
|
2013-10-11 08:26:07 +00:00
|
|
|
#else
|
|
|
|
#define BROTLI_LOG_UINT(name)
|
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx)
|
2015-06-26 15:37:00 +00:00
|
|
|
#define BROTLI_LOG(x)
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-08 11:34:35 +00:00
|
|
|
static const uint8_t kDefaultCodeLength = 8;
|
|
|
|
static const uint8_t kCodeLengthRepeatCode = 16;
|
2013-10-11 08:26:07 +00:00
|
|
|
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
|
|
|
|
2014-02-14 14:04:23 +00:00
|
|
|
#define HUFFMAN_TABLE_BITS 8
|
|
|
|
#define HUFFMAN_TABLE_MASK 0xff
|
|
|
|
|
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] = {
|
2014-02-14 14:04:23 +00:00
|
|
|
1, 2, 3, 4, 0, 5, 17, 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
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
static uint32_t DecodeWindowBits(BrotliBitReader* br) {
|
|
|
|
uint32_t n;
|
2015-05-07 15:44:33 +00:00
|
|
|
if (BrotliReadBits(br, 1) == 0) {
|
2013-11-28 16:37:13 +00:00
|
|
|
return 16;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
n = BrotliReadBits(br, 3);
|
|
|
|
if (n != 0) {
|
2015-05-07 15:44:33 +00:00
|
|
|
return 17 + n;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
n = BrotliReadBits(br, 3);
|
|
|
|
if (n != 0) {
|
2015-05-07 15:44:33 +00:00
|
|
|
return 8 + n;
|
|
|
|
}
|
|
|
|
return 17;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
static BROTLI_INLINE BROTLI_NO_ASAN void memmove16(
|
|
|
|
uint8_t* dst, uint8_t* src) {
|
|
|
|
#ifdef __ARM_NEON__
|
|
|
|
vst1q_u8(dst, vld1q_u8(src));
|
|
|
|
#else
|
|
|
|
/* memcpy is unsafe for overlapping regions and ASAN detects this.
|
|
|
|
But, because of optimizations, it works exactly as memmove:
|
|
|
|
copies data to registers first, and then stores them to dst. */
|
|
|
|
memcpy(dst, src, 16);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
|
2013-12-12 12:18:04 +00:00
|
|
|
static BROTLI_INLINE int DecodeVarLenUint8(BrotliBitReader* br) {
|
|
|
|
if (BrotliReadBits(br, 1)) {
|
2014-01-08 11:34:35 +00:00
|
|
|
int nbits = (int)BrotliReadBits(br, 3);
|
2013-12-12 12:18:04 +00:00
|
|
|
if (nbits == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-01-08 11:34:35 +00:00
|
|
|
return (int)BrotliReadBits(br, nbits) + (1 << nbits);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
static BrotliResult DecodeMetaBlockLength(BrotliBitReader* br,
|
|
|
|
int* meta_block_length,
|
|
|
|
int* input_end,
|
|
|
|
int* is_metadata,
|
|
|
|
int* is_uncompressed) {
|
2013-12-13 09:39:46 +00:00
|
|
|
int size_nibbles;
|
2015-04-22 12:35:21 +00:00
|
|
|
int size_bytes;
|
2013-12-13 09:39:46 +00:00
|
|
|
int i;
|
2014-01-08 11:34:35 +00:00
|
|
|
*input_end = (int)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;
|
2015-04-22 12:25:08 +00:00
|
|
|
*is_metadata = 0;
|
2013-11-28 16:37:13 +00:00
|
|
|
if (*input_end && BrotliReadBits(br, 1)) {
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2014-01-08 11:34:35 +00:00
|
|
|
size_nibbles = (int)BrotliReadBits(br, 2) + 4;
|
2015-04-22 12:25:08 +00:00
|
|
|
if (size_nibbles == 7) {
|
|
|
|
*is_metadata = 1;
|
|
|
|
/* Verify reserved bit. */
|
|
|
|
if (BrotliReadBits(br, 1) != 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-04-22 12:25:08 +00:00
|
|
|
}
|
2015-04-22 12:35:21 +00:00
|
|
|
size_bytes = (int)BrotliReadBits(br, 2);
|
|
|
|
if (size_bytes == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2015-04-22 12:25:08 +00:00
|
|
|
}
|
2015-04-22 12:35:21 +00:00
|
|
|
for (i = 0; i < size_bytes; ++i) {
|
|
|
|
int next_byte = (int)BrotliReadBits(br, 8);
|
|
|
|
if (i + 1 == size_bytes && size_bytes > 1 && next_byte == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
}
|
|
|
|
*meta_block_length |= next_byte << (i * 8);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < size_nibbles; ++i) {
|
|
|
|
int next_nibble = (int)BrotliReadBits(br, 4);
|
|
|
|
if (i + 1 == size_nibbles && size_nibbles > 4 && next_nibble == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
}
|
|
|
|
*meta_block_length |= next_nibble << (i * 4);
|
|
|
|
}
|
2013-11-28 16:37:13 +00:00
|
|
|
}
|
|
|
|
++(*meta_block_length);
|
2015-04-22 12:25:08 +00:00
|
|
|
if (!*input_end && !*is_metadata) {
|
2014-01-08 11:34:35 +00:00
|
|
|
*is_uncompressed = (int)BrotliReadBits(br, 1);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Decodes the next Huffman code from bit-stream. */
|
2014-02-14 14:04:23 +00:00
|
|
|
static BROTLI_INLINE int ReadSymbol(const HuffmanCode* table,
|
2013-10-11 08:26:07 +00:00
|
|
|
BrotliBitReader* br) {
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Read the bits for two reads at once. */
|
2015-08-28 13:20:24 +00:00
|
|
|
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
2015-05-07 15:10:27 +00:00
|
|
|
table += val & HUFFMAN_TABLE_MASK;
|
2015-08-28 13:20:24 +00:00
|
|
|
if (table->bits > HUFFMAN_TABLE_BITS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
int nbits = table->bits - HUFFMAN_TABLE_BITS;
|
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
2014-02-14 14:04:23 +00:00
|
|
|
table += table->value;
|
2015-08-28 13:20:24 +00:00
|
|
|
table += (int)(val >> HUFFMAN_TABLE_BITS) & (int)BitMask(nbits);
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, table->bits);
|
2014-02-14 14:04:23 +00:00
|
|
|
return table->value;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
static BROTLI_INLINE void PreloadSymbol(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br,
|
|
|
|
unsigned* bits,
|
|
|
|
unsigned* value) {
|
2015-08-28 13:20:24 +00:00
|
|
|
table += BrotliGetBits(br, HUFFMAN_TABLE_BITS);
|
2015-08-10 11:35:23 +00:00
|
|
|
*bits = table->bits;
|
|
|
|
*value = table->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BROTLI_INLINE unsigned ReadPreloadedSymbol(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br,
|
|
|
|
unsigned* bits,
|
|
|
|
unsigned* value) {
|
|
|
|
unsigned result = *value;
|
|
|
|
if (PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {
|
2015-08-28 13:20:24 +00:00
|
|
|
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
2015-08-10 11:35:23 +00:00
|
|
|
const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
|
2015-08-28 13:20:24 +00:00
|
|
|
int mask = (int)BitMask((int)(*bits - HUFFMAN_TABLE_BITS));
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
|
|
|
ext += (int)(val >> HUFFMAN_TABLE_BITS) & mask;
|
|
|
|
BrotliDropBits(br, ext->bits);
|
|
|
|
result = ext->value;
|
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
BrotliDropBits(br, (int)*bits);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
PreloadSymbol(table, br, bits, value);
|
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult ReadHuffmanCode(int alphabet_size,
|
|
|
|
HuffmanCode* table,
|
|
|
|
int* opt_table_size,
|
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-08-10 11:35:23 +00:00
|
|
|
/* 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 */
|
|
|
|
int simple_code_or_skip;
|
2015-08-28 13:20:24 +00:00
|
|
|
unsigned symbol = s->symbol;
|
|
|
|
uint32_t repeat = s->repeat;
|
|
|
|
uint8_t prev_code_len = s->prev_code_len;
|
|
|
|
uint8_t repeat_code_len = s->repeat_code_len;
|
|
|
|
uint32_t space = s->space;
|
|
|
|
uint16_t* symbol_lists = s->symbol_lists;
|
|
|
|
int* next_symbol = s->next_symbol;
|
|
|
|
int i = 0;
|
2015-08-10 14:39:50 +00:00
|
|
|
/* Unnecessary masking, but might be good for safety. */
|
|
|
|
alphabet_size &= 0x3ff;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->sub1_state == BROTLI_STATE_SUB1_NONE) {
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
simple_code_or_skip = (int)BrotliReadBits(br, 2);
|
|
|
|
BROTLI_LOG_UINT(simple_code_or_skip);
|
|
|
|
if (simple_code_or_skip == 1) {
|
|
|
|
/* Read symbols, codes & code lengths directly. */
|
|
|
|
int table_size;
|
|
|
|
int max_bits_counter = alphabet_size - 1;
|
|
|
|
int max_bits = 0;
|
|
|
|
uint16_t symbols[4] = { 0 };
|
|
|
|
uint32_t num_symbols = BrotliReadBits(br, 2);
|
|
|
|
i = 0;
|
|
|
|
while (max_bits_counter) {
|
|
|
|
max_bits_counter >>= 1;
|
|
|
|
++max_bits;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
int k;
|
|
|
|
uint32_t v = BrotliReadBits(br, max_bits);
|
|
|
|
if (v >= alphabet_size) {
|
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
symbols[i] = (uint16_t)v;
|
|
|
|
for (k = 0; k < i; k++) {
|
|
|
|
if (symbols[k] == (uint16_t)v) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
} while (++i <= num_symbols);
|
|
|
|
if (num_symbols == 3) {
|
|
|
|
num_symbols += BrotliReadBits(br, 1);
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(num_symbols);
|
|
|
|
table_size = BrotliBuildSimpleHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, symbols, num_symbols);
|
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = table_size;
|
|
|
|
}
|
|
|
|
s->sub1_state = BROTLI_STATE_SUB1_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
} else { /* Decode Huffman-coded code lengths. */
|
|
|
|
int i;
|
|
|
|
int8_t num_codes = 0;
|
|
|
|
/* Static Huffman code for the code length code lengths. */
|
|
|
|
static const uint8_t huff_len[16] = {
|
|
|
|
2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4,
|
|
|
|
};
|
|
|
|
static const uint8_t huff_val[16] = {
|
|
|
|
0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
|
|
|
|
};
|
|
|
|
space = 32;
|
|
|
|
memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo));
|
|
|
|
memset(&s->code_length_code_lengths[0], 0,
|
|
|
|
sizeof(s->code_length_code_lengths));
|
|
|
|
for (i = simple_code_or_skip;
|
|
|
|
i < CODE_LENGTH_CODES; ++i) {
|
|
|
|
const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
|
|
|
|
uint8_t ix = (uint8_t)BrotliGetBits(br, 4);
|
|
|
|
uint8_t v = huff_val[ix];
|
|
|
|
BrotliDropBits(br, huff_len[ix]);
|
|
|
|
s->code_length_code_lengths[code_len_idx] = v;
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx);
|
|
|
|
if (v != 0) {
|
|
|
|
space = space - (32U >> v);
|
|
|
|
++num_codes;
|
|
|
|
++s->code_length_histo[v];
|
|
|
|
if (space - 1U >= 32U) {
|
|
|
|
/* space is 0 or wrapped around */
|
|
|
|
break;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
|
|
|
if (!(num_codes == 1 || space == 0)) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BrotliBuildCodeLengthsHuffmanTable(s->table,
|
|
|
|
s->code_length_code_lengths,
|
|
|
|
s->code_length_histo);
|
|
|
|
memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo));
|
|
|
|
for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) {
|
|
|
|
next_symbol[i] = i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
|
|
|
symbol_lists[i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol = 0;
|
|
|
|
prev_code_len = kDefaultCodeLength;
|
|
|
|
repeat = 0;
|
|
|
|
repeat_code_len = 0;
|
|
|
|
space = 32768;
|
|
|
|
s->sub1_state = BROTLI_STATE_SUB1_HUFFMAN_LENGTH_SYMBOLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (symbol < alphabet_size && space > 0) {
|
|
|
|
uint32_t milestone;
|
|
|
|
if (!BrotliCheckInputAmount(br, 128)) {
|
|
|
|
s->symbol = (uint32_t)symbol;
|
|
|
|
s->repeat = repeat;
|
|
|
|
s->prev_code_len = prev_code_len;
|
|
|
|
s->repeat_code_len = repeat_code_len;
|
|
|
|
s->space = space;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
/* We use at most 5 bits per symbol. 128 * 8 / 5 = 204.8 */
|
|
|
|
milestone = symbol + 204;
|
|
|
|
if (milestone > alphabet_size) {
|
|
|
|
milestone = (uint32_t)alphabet_size;
|
|
|
|
}
|
|
|
|
while (symbol < milestone && space > 0) {
|
|
|
|
const HuffmanCode* p = s->table;
|
|
|
|
uint8_t code_len;
|
|
|
|
p += BrotliGetBits(br, BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
|
|
|
BrotliDropBits(br, p->bits);
|
|
|
|
code_len = (uint8_t)p->value;
|
|
|
|
if (code_len < kCodeLengthRepeatCode) {
|
|
|
|
repeat = 0;
|
|
|
|
if (code_len != 0) {
|
|
|
|
symbol_lists[next_symbol[code_len]] = (uint16_t)symbol;
|
|
|
|
next_symbol[code_len] = (int)symbol;
|
|
|
|
prev_code_len = code_len;
|
|
|
|
space -= 32768U >> code_len;
|
|
|
|
s->code_length_histo[code_len]++;
|
|
|
|
}
|
|
|
|
symbol++;
|
|
|
|
} else {
|
|
|
|
const int extra_bits = code_len - 14;
|
|
|
|
uint32_t old_repeat;
|
|
|
|
uint32_t repeat_delta;
|
|
|
|
uint8_t new_len = 0;
|
|
|
|
if (code_len == kCodeLengthRepeatCode) {
|
|
|
|
new_len = prev_code_len;
|
|
|
|
}
|
|
|
|
if (repeat_code_len != new_len) {
|
|
|
|
repeat = 0;
|
|
|
|
repeat_code_len = new_len;
|
|
|
|
}
|
|
|
|
old_repeat = repeat;
|
|
|
|
if (repeat > 0) {
|
|
|
|
repeat -= 2;
|
|
|
|
repeat <<= extra_bits;
|
|
|
|
}
|
|
|
|
repeat += BrotliReadBits(br, extra_bits) + 3;
|
|
|
|
repeat_delta = repeat - old_repeat;
|
|
|
|
if (symbol + repeat_delta > alphabet_size) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (repeat_code_len != 0) {
|
|
|
|
unsigned last = symbol + repeat_delta;
|
|
|
|
i = next_symbol[repeat_code_len];
|
|
|
|
do {
|
|
|
|
symbol_lists[i] = (uint16_t)symbol;
|
|
|
|
i = (int)symbol;
|
|
|
|
} while (++symbol != last);
|
|
|
|
next_symbol[repeat_code_len] = i;
|
|
|
|
space -= repeat_delta << (15 - repeat_code_len);
|
|
|
|
s->code_length_histo[repeat_code_len] = (uint16_t)
|
|
|
|
(s->code_length_histo[repeat_code_len] + repeat_delta);
|
|
|
|
} else {
|
|
|
|
symbol += repeat_delta;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (space != 0) {
|
|
|
|
BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", space));
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int table_size = BrotliBuildHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, symbol_lists,
|
|
|
|
s->code_length_histo);
|
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = table_size;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
s->sub1_state = BROTLI_STATE_SUB1_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:04:23 +00:00
|
|
|
static BROTLI_INLINE int ReadBlockLength(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int code;
|
|
|
|
int nbits;
|
2014-02-14 14:04:23 +00:00
|
|
|
code = ReadSymbol(table, br);
|
2013-10-22 13:02:54 +00:00
|
|
|
nbits = kBlockLengthPrefixCode[code].nbits;
|
2014-01-08 11:34:35 +00:00
|
|
|
return kBlockLengthPrefixCode[code].offset + (int)BrotliReadBits(br, nbits);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Transform:
|
|
|
|
1) initialize list L with values 0, 1,... 255
|
|
|
|
2) For each input element X:
|
|
|
|
2.1) let Y = L[X]
|
|
|
|
2.2) remove X-th element from L
|
|
|
|
2.3) prepend Y to L
|
|
|
|
2.4) append Y to output
|
|
|
|
|
|
|
|
In most cases max(Y) <= 7, so most of L remains intact.
|
|
|
|
To reduce the cost of initialization, we reuse L, remember the upper bound
|
|
|
|
of Y values, and reinitialize only first elements in L.
|
|
|
|
|
|
|
|
Most of input values are 0 and 1. To reduce number of brances, we replace
|
|
|
|
inner for loop with do-while.
|
|
|
|
*/
|
|
|
|
static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, int v_len,
|
|
|
|
BrotliState* state) {
|
|
|
|
/* Reinitialize elements that could have been changed. */
|
|
|
|
int i = 4;
|
|
|
|
int upper_bound = state->mtf_upper_bound;
|
|
|
|
uint8_t* mtf = state->mtf;
|
|
|
|
/* Load endian-aware constant. */
|
|
|
|
const uint8_t b0123[4] = {0, 1, 2, 3};
|
|
|
|
uint32_t pattern;
|
|
|
|
memcpy(&pattern, &b0123, 4);
|
|
|
|
|
|
|
|
/* Initialize list using 4 consequent values pattern. */
|
|
|
|
*(uint32_t*)mtf = pattern;
|
|
|
|
do {
|
|
|
|
pattern += 0x04040404; /* Advance all 4 values by 4. */
|
|
|
|
*(uint32_t*)(mtf + i) = pattern;
|
|
|
|
i += 4;
|
|
|
|
} while (i <= upper_bound);
|
|
|
|
|
|
|
|
/* Transform the input. */
|
|
|
|
upper_bound = 0;
|
2013-10-11 08:26:07 +00:00
|
|
|
for (i = 0; i < v_len; ++i) {
|
2015-08-10 11:35:23 +00:00
|
|
|
int index = v[i];
|
2015-04-22 12:25:08 +00:00
|
|
|
uint8_t value = mtf[index];
|
|
|
|
v[i] = value;
|
2015-08-10 11:35:23 +00:00
|
|
|
upper_bound |= index;
|
|
|
|
do {
|
|
|
|
index--;
|
|
|
|
mtf[index + 1] = mtf[index];
|
|
|
|
} while (index > 0);
|
2015-04-22 12:25:08 +00:00
|
|
|
mtf[0] = value;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Remember amount of elements to be reinitialized. */
|
|
|
|
state->mtf_upper_bound = upper_bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expose function for testing. Will be removed by linker as unused. */
|
|
|
|
void InverseMoveToFrontTransformForTesting(uint8_t* v, int l, BrotliState* s) {
|
|
|
|
InverseMoveToFrontTransform(v, l, s);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
|
|
|
|
BrotliState* s) {
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->sub0_state != BROTLI_STATE_SUB0_TREE_GROUP) {
|
|
|
|
s->next = group->codes;
|
|
|
|
s->htree_index = 0;
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_TREE_GROUP;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
while (s->htree_index < group->num_htrees) {
|
|
|
|
int table_size;
|
|
|
|
BrotliResult result =
|
|
|
|
ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) return result;
|
|
|
|
group->htrees[s->htree_index] = s->next;
|
|
|
|
s->next += table_size;
|
|
|
|
if (table_size == 0) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
++s->htree_index;
|
|
|
|
}
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult DecodeContextMap(int context_map_size,
|
|
|
|
int* num_htrees,
|
2015-08-28 13:20:24 +00:00
|
|
|
uint8_t** context_map_arg,
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
2014-02-14 14:04:23 +00:00
|
|
|
int use_rle_for_zeros;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
switch((int)s->sub0_state) {
|
|
|
|
case BROTLI_STATE_SUB0_NONE:
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
*num_htrees = DecodeVarLenUint8(br) + 1;
|
|
|
|
s->context_index = 0;
|
|
|
|
BROTLI_LOG_UINT(context_map_size);
|
|
|
|
BROTLI_LOG_UINT(*num_htrees);
|
2015-08-28 13:20:24 +00:00
|
|
|
*context_map_arg = (uint8_t*)malloc((size_t)context_map_size);
|
|
|
|
if (*context_map_arg == 0) {
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
if (*num_htrees <= 1) {
|
2015-08-28 13:20:24 +00:00
|
|
|
memset(*context_map_arg, 0, (size_t)context_map_size);
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
use_rle_for_zeros = (int)BrotliReadBits(br, 1);
|
|
|
|
if (use_rle_for_zeros) {
|
2015-03-30 16:00:40 +00:00
|
|
|
s->max_run_length_prefix = (int)BrotliReadBits(br, 4) + 1;
|
|
|
|
} else {
|
|
|
|
s->max_run_length_prefix = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_CONTEXT_MAP_HUFFMAN;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, continue to next state. */
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_CONTEXT_MAP_HUFFMAN:
|
2015-03-30 16:00:40 +00:00
|
|
|
result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix,
|
|
|
|
s->context_map_table, NULL, s);
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) return result;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_CONTEXT_MAPS;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, continue to next state. */
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_CONTEXT_MAPS: {
|
|
|
|
int context_index = s->context_index;
|
2015-08-28 13:20:24 +00:00
|
|
|
int max_run_length_prefix = s->max_run_length_prefix;
|
|
|
|
uint8_t* context_map = *context_map_arg;
|
|
|
|
int code;
|
2015-08-10 11:35:23 +00:00
|
|
|
while (context_index < context_map_size) {
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
s->context_index = context_index;
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
code = ReadSymbol(s->context_map_table, br);
|
|
|
|
if (code == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
context_map[context_index++] = 0;
|
|
|
|
} else if (code - max_run_length_prefix <= 0) {
|
|
|
|
int reps = (1 << code) + (int)BrotliReadBits(br, code);
|
|
|
|
if (context_index + reps > context_map_size) {
|
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
do {
|
|
|
|
context_map[context_index++] = 0;
|
|
|
|
} while (--reps);
|
2015-03-20 15:13:15 +00:00
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
context_map[context_index++] =
|
|
|
|
(uint8_t)(code - max_run_length_prefix);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
if (BrotliReadBits(br, 1)) {
|
2015-08-28 13:20:24 +00:00
|
|
|
InverseMoveToFrontTransform(context_map, context_map_size, s);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_NONE;
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
static void DecodeBlockType(const int max_block_type,
|
|
|
|
const HuffmanCode* trees,
|
|
|
|
int tree_type,
|
|
|
|
int* ringbuffers,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int* ringbuffer = ringbuffers + tree_type * 2;
|
2015-08-10 11:35:23 +00:00
|
|
|
int block_type =
|
|
|
|
ReadSymbol(&trees[tree_type * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br) - 2;
|
|
|
|
if (block_type == -1) {
|
|
|
|
block_type = ringbuffer[1] + 1;
|
|
|
|
} else if (block_type == -2) {
|
|
|
|
block_type = ringbuffer[0];
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2014-01-06 15:01:57 +00:00
|
|
|
if (block_type >= max_block_type) {
|
|
|
|
block_type -= max_block_type;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
ringbuffer[0] = ringbuffer[1];
|
|
|
|
ringbuffer[1] = block_type;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 14:15:39 +00:00
|
|
|
/* Decodes the block type and updates the state for literal context. */
|
2015-08-10 11:35:23 +00:00
|
|
|
static void DecodeBlockTypeWithContext(BrotliState* s,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
uint8_t context_mode;
|
|
|
|
int context_offset;
|
2015-08-28 13:20:24 +00:00
|
|
|
DecodeBlockType(s->num_block_types[0], s->block_type_trees, 0,
|
2015-08-10 11:35:23 +00:00
|
|
|
s->block_type_rb, br);
|
2015-04-01 14:15:39 +00:00
|
|
|
s->block_length[0] = ReadBlockLength(s->block_len_trees, br);
|
2015-08-10 11:35:23 +00:00
|
|
|
context_offset = s->block_type_rb[1] << kLiteralContextBits;
|
|
|
|
s->context_map_slice = s->context_map + context_offset;
|
2015-04-01 14:15:39 +00:00
|
|
|
s->literal_htree_index = s->context_map_slice[0];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index];
|
2015-08-10 11:35:23 +00:00
|
|
|
context_mode = s->context_modes[s->block_type_rb[1]];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]];
|
|
|
|
s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]];
|
2015-04-01 14:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliResult WriteRingBuffer(BrotliOutput output,
|
|
|
|
BrotliState* s) {
|
|
|
|
int num_written = BrotliWrite(
|
|
|
|
output, s->ringbuffer + s->partially_written,
|
|
|
|
(size_t)(s->to_write - s->partially_written));
|
|
|
|
if (num_written < 0) {
|
|
|
|
return BROTLI_FAILURE();
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->partially_written += num_written;
|
|
|
|
if (s->partially_written < s->to_write) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_OUTPUT;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput(BrotliOutput output,
|
|
|
|
int pos,
|
|
|
|
BrotliState* s) {
|
|
|
|
BrotliResult result;
|
2015-03-20 15:13:15 +00:00
|
|
|
int num_read;
|
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2015-08-10 11:35:23 +00:00
|
|
|
switch ((int)s->sub0_state) {
|
|
|
|
case BROTLI_STATE_SUB0_NONE:
|
2015-03-20 15:13:15 +00:00
|
|
|
/* For short lengths copy byte-by-byte */
|
2015-08-10 11:35:23 +00:00
|
|
|
if (s->meta_block_remaining_len < 8 ||
|
|
|
|
s->meta_block_remaining_len < BrotliGetRemainingBytes(&s->br)) {
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_SHORT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Copy remaining bytes from s->br.buf_ to ringbuffer. */
|
2015-08-10 11:35:23 +00:00
|
|
|
s->nbytes = (int)BrotliGetRemainingBytes(&s->br);
|
|
|
|
BrotliCopyBytes(&s->ringbuffer[pos], &s->br, (size_t)s->nbytes);
|
|
|
|
pos += s->nbytes;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->meta_block_remaining_len -= s->nbytes;
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pos + s->meta_block_remaining_len >= s->ringbuffer_size) {
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_FILL;
|
|
|
|
} else {
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_COPY;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_SHORT:
|
2015-03-20 15:13:15 +00:00
|
|
|
while (s->meta_block_remaining_len > 0) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(&s->br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->ringbuffer[pos++] = (uint8_t)BrotliReadBits(&s->br, 8);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->meta_block_remaining_len--;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_2;
|
|
|
|
} else {
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_NONE;
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
/* No break, if state is updated, continue to next state */
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_1:
|
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_2:
|
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_3:
|
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
pos &= s->ringbuffer_size;
|
|
|
|
s->max_distance = s->max_backward_distance;
|
|
|
|
if (s->sub0_state == BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_2) {
|
|
|
|
s->meta_block_remaining_len--;
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_SHORT;
|
|
|
|
break;
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (s->sub0_state == BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_1) {
|
|
|
|
s->meta_block_remaining_len += s->ringbuffer_size;
|
|
|
|
/* If we wrote past the logical end of the ringbuffer, copy the tail
|
|
|
|
of the ringbuffer to its beginning and flush the ringbuffer to the
|
|
|
|
output. */
|
|
|
|
memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)pos);
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos + s->meta_block_remaining_len >= s->ringbuffer_size) {
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_FILL;
|
2015-04-22 15:33:21 +00:00
|
|
|
} else {
|
2015-08-10 11:35:23 +00:00
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_COPY;
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_FILL:
|
|
|
|
/* If we have more to copy than the remaining size of the ringbuffer,
|
|
|
|
then we first fill the ringbuffer from the input and then flush the
|
|
|
|
ringbuffer to the output */
|
|
|
|
s->nbytes = s->ringbuffer_size - pos;
|
|
|
|
num_read = BrotliRead(s->br.input_, &s->ringbuffer[pos],
|
|
|
|
(size_t)s->nbytes);
|
|
|
|
s->meta_block_remaining_len -= num_read;
|
|
|
|
if (num_read < s->nbytes) {
|
|
|
|
if (num_read < 0) return BROTLI_FAILURE();
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_WRITE_3;
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
2015-08-10 11:35:23 +00:00
|
|
|
/* No break, continue to next state */
|
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_COPY:
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Copy straight from the input onto the ringbuffer. The ringbuffer will
|
|
|
|
be flushed to the output at a later time. */
|
2015-08-10 11:35:23 +00:00
|
|
|
num_read = BrotliRead(s->br.input_, &s->ringbuffer[pos],
|
2015-03-20 15:13:15 +00:00
|
|
|
(size_t)s->meta_block_remaining_len);
|
|
|
|
s->meta_block_remaining_len -= num_read;
|
|
|
|
if (s->meta_block_remaining_len > 0) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (num_read < 0) return BROTLI_FAILURE();
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_UNCOMPRESSED_WARMUP;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-08-10 11:35:23 +00:00
|
|
|
case BROTLI_STATE_SUB0_UNCOMPRESSED_WARMUP:
|
|
|
|
if (!BrotliCheckInputAmount(&s->br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliWarmupBitReader(&s->br);
|
|
|
|
s->sub0_state = BROTLI_STATE_SUB0_NONE;
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
int BrotliDecompressedSize(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size) {
|
2014-10-15 11:41:00 +00:00
|
|
|
int i;
|
|
|
|
uint64_t val = 0;
|
|
|
|
int bit_pos = 0;
|
|
|
|
int is_last;
|
|
|
|
int is_uncompressed = 0;
|
|
|
|
int size_nibbles;
|
|
|
|
int meta_block_len = 0;
|
|
|
|
if (encoded_size == 0) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2014-10-15 11:41:00 +00:00
|
|
|
/* Look at the first 8 bytes, it is enough to decode the length of the first
|
|
|
|
meta-block. */
|
2015-02-24 08:48:26 +00:00
|
|
|
for (i = 0; (size_t)i < encoded_size && i < 8; ++i) {
|
2014-10-15 11:41:00 +00:00
|
|
|
val |= (uint64_t)encoded_buffer[i] << (8 * i);
|
|
|
|
}
|
|
|
|
/* Skip the window bits. */
|
2015-05-07 15:44:33 +00:00
|
|
|
++bit_pos;
|
|
|
|
if (val & 1) {
|
|
|
|
bit_pos += 3;
|
|
|
|
if (((val >> 1) & 7) == 0) {
|
|
|
|
bit_pos += 3;
|
|
|
|
}
|
|
|
|
}
|
2014-10-15 11:41:00 +00:00
|
|
|
/* Decode the ISLAST bit. */
|
|
|
|
is_last = (val >> bit_pos) & 1;
|
|
|
|
++bit_pos;
|
|
|
|
if (is_last) {
|
|
|
|
/* Decode the ISEMPTY bit, if it is set to 1, we are done. */
|
|
|
|
if ((val >> bit_pos) & 1) {
|
|
|
|
*decoded_size = 0;
|
2015-08-10 11:35:23 +00:00
|
|
|
return 1;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
|
|
|
++bit_pos;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2014-10-15 11:41:00 +00:00
|
|
|
/* Decode the length of the first meta-block. */
|
|
|
|
size_nibbles = (int)((val >> bit_pos) & 3) + 4;
|
2015-05-07 15:44:33 +00:00
|
|
|
if (size_nibbles == 7) {
|
|
|
|
/* First meta-block contains metadata, this case is not supported here. */
|
2015-08-10 11:35:23 +00:00
|
|
|
return 0;
|
2015-05-07 15:44:33 +00:00
|
|
|
}
|
2014-10-15 11:41:00 +00:00
|
|
|
bit_pos += 2;
|
|
|
|
for (i = 0; i < size_nibbles; ++i) {
|
|
|
|
meta_block_len |= (int)((val >> bit_pos) & 0xf) << (4 * i);
|
|
|
|
bit_pos += 4;
|
|
|
|
}
|
|
|
|
++meta_block_len;
|
|
|
|
if (is_last) {
|
|
|
|
/* If this meta-block is the only one, we are done. */
|
|
|
|
*decoded_size = (size_t)meta_block_len;
|
2015-08-10 11:35:23 +00:00
|
|
|
return 1;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
|
|
|
is_uncompressed = (val >> bit_pos) & 1;
|
|
|
|
++bit_pos;
|
|
|
|
if (is_uncompressed) {
|
|
|
|
/* If the first meta-block is uncompressed, we skip it and look at the
|
|
|
|
first two bits (ISLAST and ISEMPTY) of the next meta-block, and if
|
|
|
|
both are set to 1, we have a stream with an uncompressed meta-block
|
|
|
|
followed by an empty one, so the decompressed size is the size of the
|
|
|
|
first meta-block. */
|
2015-02-24 10:22:29 +00:00
|
|
|
size_t offset = (size_t)((bit_pos + 7) >> 3) + (size_t)meta_block_len;
|
2014-10-15 11:41:00 +00:00
|
|
|
if (offset < encoded_size && ((encoded_buffer[offset] & 3) == 3)) {
|
|
|
|
*decoded_size = (size_t)meta_block_len;
|
2015-08-10 11:35:23 +00:00
|
|
|
return 1;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Could not get the size because the file has multiple meta-blocks */
|
|
|
|
return 0;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult 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);
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult success = BrotliDecompress(in, out);
|
2013-11-15 18:02:17 +00:00
|
|
|
*decoded_size = mout.pos;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompress(BrotliInput input, BrotliOutput output) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliState s;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result;
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliStateInit(&s);
|
2015-03-27 12:54:43 +00:00
|
|
|
result = BrotliDecompressStreaming(input, output, 1, &s);
|
2015-04-22 15:33:21 +00:00
|
|
|
if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Not ok: it didn't finish even though this is a non-streaming function. */
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
BrotliStateCleanup(&s);
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompressBufferStreaming(size_t* available_in,
|
|
|
|
const uint8_t** next_in,
|
|
|
|
int finish,
|
|
|
|
size_t* available_out,
|
|
|
|
uint8_t** next_out,
|
|
|
|
size_t* total_out,
|
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput in = BrotliInitMemInput(*next_in, *available_in, &memin);
|
|
|
|
BrotliMemOutput memout;
|
|
|
|
BrotliOutput out = BrotliInitMemOutput(*next_out, *available_out, &memout);
|
2015-08-28 13:20:24 +00:00
|
|
|
BrotliResult result = BrotliDecompressStreaming(in, out, finish, s);
|
2015-03-20 15:13:15 +00:00
|
|
|
/* The current implementation reads everything, so 0 bytes are available. */
|
|
|
|
*next_in += memin.pos;
|
|
|
|
*available_in -= memin.pos;
|
|
|
|
/* Update the output position to where we write next. */
|
|
|
|
*next_out += memout.pos;
|
|
|
|
*available_out -= memout.pos;
|
|
|
|
*total_out += memout.pos;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
|
|
|
int finish, BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
uint8_t context;
|
|
|
|
int pos = s->pos;
|
|
|
|
int i = s->loop_counter;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-03-20 16:03:11 +00:00
|
|
|
int initial_remaining_len;
|
2015-04-01 14:23:18 +00:00
|
|
|
int bytes_copied;
|
2015-08-28 13:20:24 +00:00
|
|
|
int is_metadata;
|
|
|
|
int is_uncompressed;
|
2015-08-10 11:35:23 +00:00
|
|
|
uint8_t *copy_src;
|
|
|
|
uint8_t *copy_dst;
|
2014-02-14 14:04:23 +00:00
|
|
|
/* We need the slack region for the following reasons:
|
2015-08-28 13:20:24 +00:00
|
|
|
- doing up to two 16-byte copies for fast backward copying
|
2014-02-14 14:04:23 +00:00
|
|
|
- transforms
|
2015-03-20 15:13:15 +00:00
|
|
|
- flushing the input s->ringbuffer when decoding uncompressed blocks */
|
2015-08-28 13:20:24 +00:00
|
|
|
static const int kRingBufferWriteAheadSlack =
|
|
|
|
BROTLI_IMPLICIT_ZEROES + BROTLI_READ_SIZE;
|
2015-05-07 14:53:43 +00:00
|
|
|
s->br.input_ = input;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
|
|
|
if (BrotliReadInput(br, finish)) {
|
|
|
|
result = BROTLI_RESULT_SUCCESS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (finish) {
|
|
|
|
BROTLI_LOG(("Unexpected end of input. State: %d\n", s->state));
|
|
|
|
result = BROTLI_FAILURE();
|
|
|
|
}
|
2015-03-20 16:03:11 +00:00
|
|
|
}
|
|
|
|
break; /* Fail, or partial data. */
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
switch (s->state) {
|
|
|
|
case BROTLI_STATE_UNINITED:
|
|
|
|
pos = 0;
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliInitBitReader(br, input);
|
2015-03-20 15:13:15 +00:00
|
|
|
|
|
|
|
s->state = BROTLI_STATE_BITREADER_WARMUP;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_BITREADER_WARMUP:
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(&s->br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliWarmupBitReader(&s->br);
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Decode window size. */
|
|
|
|
s->window_bits = DecodeWindowBits(br);
|
2015-05-07 15:44:33 +00:00
|
|
|
if (s->window_bits == 9) {
|
|
|
|
/* Value 9 is reserved for future use. */
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-05-07 15:44:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Allocate the ringbuffer */
|
|
|
|
{
|
|
|
|
size_t known_size = 0;
|
|
|
|
s->ringbuffer_size = 1 << s->window_bits;
|
|
|
|
|
|
|
|
/* If we know the data size is small, do not allocate more ringbuffer
|
|
|
|
size than needed to reduce memory usage. Since this happens after
|
|
|
|
the first BrotliCheckInputAmount call, we can read the bitreader
|
|
|
|
buffer at position 0.
|
|
|
|
We need at least 2 bytes of ring buffer size to get the last two
|
|
|
|
bytes for context from there */
|
|
|
|
if (BrotliDecompressedSize(BROTLI_READ_SIZE, br->buf_, &known_size)) {
|
|
|
|
while (s->ringbuffer_size >= known_size * 2
|
|
|
|
&& s->ringbuffer_size > 32) {
|
|
|
|
s->ringbuffer_size >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* But make it fit the custom dictionary if there is one. */
|
|
|
|
while (s->ringbuffer_size < s->custom_dict_size) {
|
|
|
|
s->ringbuffer_size <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->ringbuffer_mask = s->ringbuffer_size - 1;
|
|
|
|
s->ringbuffer = (uint8_t*)malloc((size_t)(s->ringbuffer_size +
|
|
|
|
kRingBufferWriteAheadSlack +
|
|
|
|
kMaxDictionaryWordLength));
|
|
|
|
if (!s->ringbuffer) {
|
|
|
|
result = BROTLI_FAILURE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size;
|
|
|
|
s->ringbuffer[s->ringbuffer_size - 2] = 0;
|
|
|
|
s->ringbuffer[s->ringbuffer_size - 1] = 0;
|
|
|
|
if (s->custom_dict) {
|
|
|
|
memcpy(&s->ringbuffer[(-s->custom_dict_size) & s->ringbuffer_mask],
|
|
|
|
s->custom_dict, (size_t)s->custom_dict_size);
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->max_backward_distance = (1 << s->window_bits) - 16;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->max_backward_distance_minus_custom_dict_size =
|
|
|
|
s->max_backward_distance - s->custom_dict_size;
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-08-10 14:39:50 +00:00
|
|
|
/* Allocate memory for both block_type_trees and block_len_trees. */
|
2015-03-20 16:03:11 +00:00
|
|
|
s->block_type_trees = (HuffmanCode*)malloc(
|
2015-08-10 14:39:50 +00:00
|
|
|
6 * BROTLI_HUFFMAN_MAX_TABLE_SIZE * sizeof(HuffmanCode));
|
|
|
|
|
|
|
|
if (s->block_type_trees == NULL) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 14:39:50 +00:00
|
|
|
s->block_len_trees = s->block_type_trees +
|
|
|
|
3 * BROTLI_HUFFMAN_MAX_TABLE_SIZE;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_METABLOCK_BEGIN:
|
|
|
|
if (s->input_end) {
|
2015-08-10 11:35:23 +00:00
|
|
|
s->to_write = pos;
|
2015-04-22 15:33:21 +00:00
|
|
|
s->partially_written = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_DONE;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliStateMetablockBegin(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_HEADER_1;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_1:
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(pos);
|
2015-04-22 12:25:08 +00:00
|
|
|
if (!DecodeMetaBlockLength(br,
|
|
|
|
&s->meta_block_remaining_len,
|
|
|
|
&s->input_end,
|
2015-08-28 13:20:24 +00:00
|
|
|
&is_metadata,
|
|
|
|
&is_uncompressed)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(s->meta_block_remaining_len);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (is_metadata) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(&s->br)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-22 12:25:08 +00:00
|
|
|
s->state = BROTLI_STATE_METADATA;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->meta_block_remaining_len == 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (is_uncompressed) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(&s->br)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_UNCOMPRESSED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
|
|
|
case BROTLI_STATE_UNCOMPRESSED:
|
2015-03-20 16:03:11 +00:00
|
|
|
initial_remaining_len = s->meta_block_remaining_len;
|
|
|
|
/* pos is given as argument since s->pos is only updated at the end. */
|
2015-03-27 12:54:43 +00:00
|
|
|
result = CopyUncompressedBlockToOutput(output, pos, s);
|
2015-04-01 14:23:18 +00:00
|
|
|
bytes_copied = initial_remaining_len - s->meta_block_remaining_len;
|
2015-08-10 11:35:23 +00:00
|
|
|
pos = (pos + bytes_copied) & s->ringbuffer_mask;
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
break;
|
2015-04-01 14:23:18 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
2015-04-22 12:25:08 +00:00
|
|
|
case BROTLI_STATE_METADATA:
|
|
|
|
for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(&s->br, 32)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Read one byte and ignore it. */
|
|
|
|
BrotliReadBits(&s->br, 8);
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (result == BROTLI_RESULT_SUCCESS) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
}
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_0:
|
|
|
|
if (i >= 3) {
|
2015-08-10 11:35:23 +00:00
|
|
|
BROTLI_LOG_UINT(s->num_block_type_rb[0]);
|
|
|
|
BROTLI_LOG_UINT(s->num_block_type_rb[2]);
|
|
|
|
BROTLI_LOG_UINT(s->num_block_type_rb[4]);
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(s->block_length[0]);
|
|
|
|
BROTLI_LOG_UINT(s->block_length[1]);
|
|
|
|
BROTLI_LOG_UINT(s->block_length[2]);
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_HEADER_2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->num_block_types[i] = DecodeVarLenUint8(br) + 1;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_1;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_1:
|
|
|
|
if (s->num_block_types[i] >= 2) {
|
2015-03-27 12:54:43 +00:00
|
|
|
result = ReadHuffmanCode(s->num_block_types[i] + 2,
|
2015-03-20 15:13:15 +00:00
|
|
|
&s->block_type_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE],
|
|
|
|
NULL, s);
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_2;
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_2:
|
2015-03-27 12:54:43 +00:00
|
|
|
result = ReadHuffmanCode(kNumBlockLengthCodes,
|
|
|
|
&s->block_len_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE],
|
|
|
|
NULL, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->block_length[i] = ReadBlockLength(
|
|
|
|
&s->block_len_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
|
|
|
i++;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_2:
|
2015-06-26 15:37:00 +00:00
|
|
|
/* We need up to 256 * 2 + 6 bits, this fits in 128 bytes. */
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(br, 128)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->distance_postfix_bits = (int)BrotliReadBits(br, 2);
|
|
|
|
s->num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
|
|
|
|
((int)BrotliReadBits(br, 4) << s->distance_postfix_bits);
|
2015-08-28 13:20:24 +00:00
|
|
|
s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->context_modes = (uint8_t*)malloc((size_t)s->num_block_types[0]);
|
|
|
|
if (s->context_modes == 0) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < s->num_block_types[0]; ++i) {
|
|
|
|
s->context_modes[i] = (uint8_t)(BrotliReadBits(br, 2) << 1);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(s->num_direct_distance_codes);
|
|
|
|
BROTLI_LOG_UINT(s->distance_postfix_bits);
|
|
|
|
s->state = BROTLI_STATE_CONTEXT_MAP_1;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_1:
|
2015-03-27 12:54:43 +00:00
|
|
|
result = DecodeContextMap(s->num_block_types[0] << kLiteralContextBits,
|
|
|
|
&s->num_literal_htrees, &s->context_map, s);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-01 14:15:39 +00:00
|
|
|
s->trivial_literal_context = 1;
|
|
|
|
for (i = 0; i < s->num_block_types[0] << kLiteralContextBits; i++) {
|
|
|
|
if (s->context_map[i] != i >> kLiteralContextBits) {
|
|
|
|
s->trivial_literal_context = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_CONTEXT_MAP_2;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_2:
|
2015-08-10 11:35:23 +00:00
|
|
|
{
|
2015-08-28 13:20:24 +00:00
|
|
|
int num_dist_htrees;
|
|
|
|
int num_distance_codes =
|
|
|
|
s->num_direct_distance_codes + (48 << s->distance_postfix_bits);
|
|
|
|
result = DecodeContextMap(
|
|
|
|
s->num_block_types[2] << kDistanceContextBits,
|
|
|
|
&num_dist_htrees, &s->dist_context_map, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BrotliHuffmanTreeGroupInit(
|
|
|
|
&s->literal_hgroup, kNumLiteralCodes, s->num_literal_htrees);
|
|
|
|
BrotliHuffmanTreeGroupInit(
|
|
|
|
&s->insert_copy_hgroup, kNumInsertAndCopyCodes,
|
|
|
|
s->num_block_types[1]);
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliHuffmanTreeGroupInit(
|
2015-08-28 13:20:24 +00:00
|
|
|
&s->distance_hgroup, num_distance_codes, num_dist_htrees);
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
i = 0;
|
|
|
|
s->state = BROTLI_STATE_TREE_GROUP;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_TREE_GROUP:
|
2015-08-10 11:35:23 +00:00
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
result = HuffmanTreeGroupDecode(&s->literal_hgroup, s);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
result = HuffmanTreeGroupDecode(&s->insert_copy_hgroup, s);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
result = HuffmanTreeGroupDecode(&s->distance_hgroup, s);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 15:13:15 +00:00
|
|
|
i++;
|
|
|
|
if (i >= 3) {
|
2015-08-10 11:35:23 +00:00
|
|
|
uint8_t context_mode = s->context_modes[s->block_type_rb[1]];
|
2015-03-20 15:13:15 +00:00
|
|
|
s->context_map_slice = s->context_map;
|
|
|
|
s->dist_context_map_slice = s->dist_context_map;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->context_lookup1 =
|
|
|
|
&kContextLookup[kContextLookupOffsets[context_mode]];
|
|
|
|
s->context_lookup2 =
|
|
|
|
&kContextLookup[kContextLookupOffsets[context_mode + 1]];
|
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[0];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index];
|
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_BEGIN:
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Decoding of Brotli commands is the inner loop, jumping with goto makes it
|
|
|
|
3% faster */
|
|
|
|
CommandBegin:
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Read the insert/copy length in the command */
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->block_length[1] == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Block switch for insert/copy length */
|
2015-03-20 15:13:15 +00:00
|
|
|
DecodeBlockType(s->num_block_types[1],
|
|
|
|
s->block_type_trees, 1,
|
2015-08-10 11:35:23 +00:00
|
|
|
s->block_type_rb, br);
|
2015-08-28 13:20:24 +00:00
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
|
2015-03-20 15:13:15 +00:00
|
|
|
s->block_length[1] = ReadBlockLength(
|
|
|
|
&s->block_len_trees[BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
{
|
|
|
|
int cmd_code = ReadSymbol(s->htree_command, br);
|
2015-08-28 13:20:24 +00:00
|
|
|
CmdLutElement v;
|
|
|
|
--s->block_length[1];
|
|
|
|
v = kCmdLut[cmd_code];
|
|
|
|
s->distance_code = v.distance_code;
|
|
|
|
s->distance_context = v.context;
|
|
|
|
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
|
|
|
i = (int)BrotliReadBits(br, v.insert_len_extra_bits) +
|
|
|
|
v.insert_len_offset;
|
|
|
|
s->copy_length = (int)BrotliReadBits(br, v.copy_len_extra_bits) +
|
|
|
|
v.copy_len_offset;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BROTLI_LOG_UINT(i);
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(s->copy_length);
|
|
|
|
BROTLI_LOG_UINT(s->distance_code);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (i == 0) {
|
|
|
|
goto postDecodeLiterals;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->meta_block_remaining_len -= i;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, go to next state */
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_INNER:
|
|
|
|
/* Read the literals in the command */
|
|
|
|
if (s->trivial_literal_context) {
|
|
|
|
unsigned bits;
|
|
|
|
unsigned value;
|
|
|
|
PreloadSymbol(s->literal_htree, br, &bits, &value);
|
|
|
|
do {
|
|
|
|
if (!BrotliCheckInputAmount(br, 64)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-04-01 14:15:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
/* Block switch for literals */
|
|
|
|
DecodeBlockTypeWithContext(s, br);
|
|
|
|
PreloadSymbol(s->literal_htree, br, &bits, &value);
|
|
|
|
}
|
|
|
|
s->ringbuffer[pos] =
|
|
|
|
(uint8_t)ReadPreloadedSymbol(s->literal_htree,
|
|
|
|
br, &bits, &value);
|
|
|
|
--s->block_length[0];
|
|
|
|
BROTLI_LOG_UINT(s->literal_htree_index);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(pos == s->ringbuffer_size)) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto innerWrite;
|
|
|
|
}
|
|
|
|
} while (--i != 0);
|
|
|
|
} else {
|
|
|
|
uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask];
|
|
|
|
uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask];
|
|
|
|
do {
|
|
|
|
const HuffmanCode* hc;
|
|
|
|
if (!BrotliCheckInputAmount(br, 64)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
/* Block switch for literals */
|
|
|
|
DecodeBlockTypeWithContext(s, br);
|
|
|
|
}
|
|
|
|
context = s->context_lookup1[p1] | s->context_lookup2[p2];
|
|
|
|
BROTLI_LOG_UINT(context);
|
|
|
|
hc = s->literal_hgroup.htrees[s->context_map_slice[context]];
|
|
|
|
--s->block_length[0];
|
|
|
|
p2 = p1;
|
|
|
|
p1 = (uint8_t)ReadSymbol(hc, br);
|
|
|
|
s->ringbuffer[pos] = p1;
|
|
|
|
BROTLI_LOG_UINT(s->context_map_slice[context]);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask);
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(pos == s->ringbuffer_size)) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto innerWrite;
|
|
|
|
}
|
|
|
|
} while (--i != 0);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 16:03:11 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
postDecodeLiterals:
|
|
|
|
if (s->distance_code >= 0) {
|
|
|
|
--s->dist_rb_idx;
|
|
|
|
s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
|
|
|
|
goto postReadDistance; /* We already have the implicit distance */
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Read distance code in the command, unless it was implicitely zero. */
|
2015-05-07 14:53:43 +00:00
|
|
|
BROTLI_DCHECK(s->distance_code < 0);
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->block_length[2] == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Block switch for distance codes */
|
2015-08-10 11:35:23 +00:00
|
|
|
int dist_context_offset;
|
2015-03-20 15:13:15 +00:00
|
|
|
DecodeBlockType(s->num_block_types[2],
|
|
|
|
s->block_type_trees, 2,
|
2015-08-10 11:35:23 +00:00
|
|
|
s->block_type_rb, br);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->block_length[2] = ReadBlockLength(
|
|
|
|
&s->block_len_trees[2 * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
2015-08-10 11:35:23 +00:00
|
|
|
dist_context_offset = s->block_type_rb[5] << kDistanceContextBits;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->dist_context_map_slice =
|
2015-08-10 11:35:23 +00:00
|
|
|
s->dist_context_map + dist_context_offset;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
--s->block_length[2];
|
|
|
|
s->distance_code =
|
2015-08-10 11:35:23 +00:00
|
|
|
ReadSymbol(s->distance_hgroup.htrees[s->dist_htree_index], br);
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Convert the distance code to the actual distance by possibly */
|
2015-08-10 11:35:23 +00:00
|
|
|
/* looking up past distances from the s->ringbuffer. */
|
|
|
|
if ((s->distance_code & ~0xf) == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->distance_code == 0) {
|
|
|
|
--s->dist_rb_idx;
|
|
|
|
s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
|
|
|
|
} else {
|
|
|
|
int distance_code = s->distance_code << 1;
|
|
|
|
/* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */
|
|
|
|
/* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */
|
|
|
|
const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b;
|
|
|
|
/* kDistanceShortCodeValueOffset has 2-bit values from LSB: */
|
|
|
|
/* 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3 */
|
|
|
|
const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500;
|
2015-08-10 11:35:23 +00:00
|
|
|
int v = (s->dist_rb_idx +
|
2015-08-28 13:20:24 +00:00
|
|
|
(int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3;
|
|
|
|
s->distance_code = s->dist_rb[v];
|
|
|
|
v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3;
|
|
|
|
if ((distance_code & 0x3) != 0) {
|
|
|
|
s->distance_code += v;
|
2015-08-10 11:35:23 +00:00
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
s->distance_code -= v;
|
|
|
|
if (s->distance_code <= 0) {
|
|
|
|
/* A huge distance will cause a BROTLI_FAILURE() soon. */
|
|
|
|
/* This is a little faster than failing here. */
|
|
|
|
s->distance_code = 0x0fffffff;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
int distval = s->distance_code - s->num_direct_distance_codes;
|
|
|
|
if (distval >= 0) {
|
|
|
|
int nbits;
|
|
|
|
int postfix;
|
|
|
|
int offset;
|
|
|
|
if (s->distance_postfix_bits == 0) {
|
|
|
|
nbits = (distval >> 1) + 1;
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
|
|
|
s->distance_code = s->num_direct_distance_codes +
|
|
|
|
offset + (int)BrotliReadBits(br, nbits);
|
|
|
|
} else {
|
|
|
|
postfix = distval & s->distance_postfix_mask;
|
|
|
|
distval >>= s->distance_postfix_bits;
|
|
|
|
nbits = (distval >> 1) + 1;
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
|
|
|
s->distance_code = s->num_direct_distance_codes +
|
|
|
|
((offset + (int)BrotliReadBits(br, nbits)) <<
|
|
|
|
s->distance_postfix_bits) + postfix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->distance_code = s->distance_code - NUM_DISTANCE_SHORT_CODES + 1;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
postReadDistance:
|
|
|
|
BROTLI_LOG_UINT(s->distance_code);
|
|
|
|
if (s->max_distance != s->max_backward_distance) {
|
|
|
|
if (pos < s->max_backward_distance_minus_custom_dict_size) {
|
|
|
|
s->max_distance = pos + s->custom_dict_size;
|
|
|
|
} else {
|
|
|
|
s->max_distance = s->max_backward_distance;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
i = s->copy_length;
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Apply copy of LZ77 back-reference, or static dictionary reference if
|
|
|
|
the distance is larger than the max LZ77 distance */
|
|
|
|
if (s->distance_code > s->max_distance) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (i >= kMinDictionaryWordLength &&
|
|
|
|
i <= kMaxDictionaryWordLength) {
|
|
|
|
int offset = kBrotliDictionaryOffsetsByLength[i];
|
2015-08-28 13:20:24 +00:00
|
|
|
int word_id = s->distance_code - s->max_distance - 1;
|
2015-08-10 11:35:23 +00:00
|
|
|
int shift = kBrotliDictionarySizeBitsByLength[i];
|
2015-08-28 13:20:24 +00:00
|
|
|
int mask = (int)BitMask(shift);
|
2015-03-20 15:13:15 +00:00
|
|
|
int word_idx = word_id & mask;
|
|
|
|
int transform_idx = word_id >> shift;
|
2015-08-10 11:35:23 +00:00
|
|
|
offset += word_idx * i;
|
2015-03-20 15:13:15 +00:00
|
|
|
if (transform_idx < kNumTransforms) {
|
|
|
|
const uint8_t* word = &kBrotliDictionary[offset];
|
2015-08-10 11:35:23 +00:00
|
|
|
int len = i;
|
|
|
|
if (transform_idx == 0) {
|
|
|
|
memcpy(&s->ringbuffer[pos], word, (size_t)len);
|
|
|
|
} else {
|
|
|
|
len = TransformDictionaryWord(
|
|
|
|
&s->ringbuffer[pos], word, len, transform_idx);
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
pos += len;
|
|
|
|
s->meta_block_remaining_len -= len;
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
2015-04-22 15:33:21 +00:00
|
|
|
s->partially_written = 0;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
} else {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-03-20 15:13:15 +00:00
|
|
|
"len: %d bytes left: %d\n",
|
2015-08-28 13:20:24 +00:00
|
|
|
pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-08-28 13:20:24 +00:00
|
|
|
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-10 11:35:23 +00:00
|
|
|
const uint8_t *ringbuffer_end_minus_copy_length =
|
|
|
|
s->ringbuffer_end - i;
|
2015-08-28 13:20:24 +00:00
|
|
|
copy_src = &s->ringbuffer[(pos - s->distance_code) &
|
|
|
|
s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
copy_dst = &s->ringbuffer[pos];
|
2015-08-28 13:20:24 +00:00
|
|
|
/* update the recent distances cache */
|
|
|
|
s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
|
2015-08-10 11:35:23 +00:00
|
|
|
++s->dist_rb_idx;
|
|
|
|
s->meta_block_remaining_len -= i;
|
|
|
|
if (PREDICT_FALSE(s->meta_block_remaining_len < 0)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-08-28 13:20:24 +00:00
|
|
|
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
/* There is 128+ bytes of slack in the ringbuffer allocation.
|
|
|
|
Also, we have 16 short codes, that make these 16 bytes irrelevant
|
|
|
|
in the ringbuffer. Let's copy over them as a first guess.
|
|
|
|
*/
|
|
|
|
memmove16(copy_dst, copy_src);
|
|
|
|
/* Now check if the copy extends over the ringbuffer end,
|
|
|
|
or if the copy overlaps with itself, if yes, do wrap-copy. */
|
|
|
|
if (copy_src < copy_dst) {
|
|
|
|
if (copy_dst >= ringbuffer_end_minus_copy_length) {
|
|
|
|
goto postWrapCopy;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (copy_src + i > copy_dst) {
|
|
|
|
goto postSelfintersecting;
|
2015-05-07 15:10:27 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
} else {
|
|
|
|
if (copy_src >= ringbuffer_end_minus_copy_length) {
|
|
|
|
goto postWrapCopy;
|
|
|
|
}
|
|
|
|
if (copy_dst + i > copy_src) {
|
|
|
|
goto postSelfintersecting;
|
2013-11-19 22:32:56 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
pos += i;
|
|
|
|
if (i > 16) {
|
|
|
|
if (i > 32) {
|
|
|
|
memcpy(copy_dst + 16, copy_src + 16, (size_t)(i - 16));
|
|
|
|
} else {
|
|
|
|
/* This branch covers about 45% cases.
|
|
|
|
Fixed size short copy allows more compiler optimizations. */
|
|
|
|
memmove16(copy_dst + 16, copy_src + 16);
|
|
|
|
}
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
postSelfintersecting:
|
|
|
|
while (--i >= 0) {
|
|
|
|
s->ringbuffer[pos] =
|
2015-08-28 13:20:24 +00:00
|
|
|
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
++pos;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
postWrapCopy:
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
|
|
|
/* No break, go to next state */
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
|
2015-08-10 11:35:23 +00:00
|
|
|
while (--i >= 0) {
|
|
|
|
s->ringbuffer[pos] =
|
2015-08-28 13:20:24 +00:00
|
|
|
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
++pos;
|
|
|
|
if (pos == s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_2;
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
|
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_INNER_WRITE:
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRITE_1:
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRITE_2:
|
|
|
|
innerWrite:
|
2015-08-10 11:35:23 +00:00
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
pos -= s->ringbuffer_size;
|
|
|
|
s->max_distance = s->max_backward_distance;
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) {
|
2015-08-10 11:35:23 +00:00
|
|
|
memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)pos);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
|
|
|
} else { /* BROTLI_STATE_COMMAND_INNER_WRITE */
|
|
|
|
if (i == 0) {
|
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto postDecodeLiterals;
|
|
|
|
}
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_METABLOCK_DONE:
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliStateCleanupAfterMetablock(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
|
|
|
break;
|
|
|
|
case BROTLI_STATE_DONE:
|
|
|
|
if (s->ringbuffer != 0) {
|
2015-08-10 11:35:23 +00:00
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(&s->br)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (BrotliGetRemainingBytes(br) < BROTLI_IMPLICIT_ZEROES) {
|
|
|
|
/* The brotli input stream was too small, does not follow the spec. It
|
|
|
|
might have decompressed fine because of the implicit 128 zeroes added.
|
|
|
|
NOTE: larger input is allowed, smaller not. */
|
|
|
|
result = BROTLI_FAILURE();
|
|
|
|
}
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->pos = pos;
|
|
|
|
s->loop_counter = i;
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:43:54 +00:00
|
|
|
void BrotliSetCustomDictionary(
|
|
|
|
size_t size, const uint8_t* dict, BrotliState* s) {
|
|
|
|
s->custom_dict = dict;
|
|
|
|
s->custom_dict_size = (int) size;
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2013-12-16 13:45:57 +00:00
|
|
|
} /* extern "C" */
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|