mirror of
https://github.com/google/brotli.git
synced 2024-12-28 02:31:05 +00:00
Fix some more compiler warnings in the decoder.
This commit is contained in:
parent
498f1ecebf
commit
bacc734997
@ -36,7 +36,9 @@ extern "C" {
|
||||
|
||||
/* Masking with this expression turns to a single "Unsigned Bit Field Extract"
|
||||
UBFX instruction on ARM. */
|
||||
static BROTLI_INLINE uint32_t BitMask(int n) { return ~((0xffffffff) << n); }
|
||||
static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
|
||||
return ~((0xffffffffU) << n);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
#if (BROTLI_64_BITS)
|
||||
@ -190,7 +192,7 @@ static BROTLI_INLINE uint64_t BrotliLoad64LE(const uint8_t* in) {
|
||||
n_bits should be in the range [1..24] for regular build. For portable
|
||||
non-64-bit little endian build only 16 bits are safe to request. */
|
||||
static BROTLI_INLINE void BrotliFillBitWindow(
|
||||
BrotliBitReader* const br, int n_bits) {
|
||||
BrotliBitReader* const br, uint32_t n_bits) {
|
||||
#if (BROTLI_64_BITS)
|
||||
if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
|
||||
if (br->bit_pos_ >= 56) {
|
||||
@ -254,14 +256,14 @@ static BROTLI_INLINE void BrotliPullByte(BrotliBitReader* const br) {
|
||||
/* Like BrotliGetBits, but does not mask the result, it is only guaranteed
|
||||
that it has minimum n_bits. */
|
||||
static BROTLI_INLINE uint32_t BrotliGetBitsUnmasked(
|
||||
BrotliBitReader* const br, int n_bits) {
|
||||
BrotliBitReader* const br, uint32_t n_bits) {
|
||||
BrotliFillBitWindow(br, n_bits);
|
||||
return (uint32_t)(br->val_ >> br->bit_pos_);
|
||||
}
|
||||
|
||||
/* Returns the specified number of bits from br without advancing bit pos. */
|
||||
static BROTLI_INLINE uint32_t BrotliGetBits(
|
||||
BrotliBitReader* const br, int n_bits) {
|
||||
BrotliBitReader* const br, uint32_t n_bits) {
|
||||
BrotliFillBitWindow(br, n_bits);
|
||||
return (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
|
||||
}
|
||||
@ -275,19 +277,19 @@ static BROTLI_INLINE void BrotliDropBits(
|
||||
/* Reads the specified number of bits from br and advances the bit pos.
|
||||
Precondition: accumulator MUST contain at least n_bits. */
|
||||
static BROTLI_INLINE void BrotliTakeBits(
|
||||
BrotliBitReader* const br, int n_bits, uint32_t* val) {
|
||||
BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
|
||||
*val = (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
|
||||
#ifdef BROTLI_DECODE_DEBUG
|
||||
printf("[BrotliReadBits] %d %d %d val: %6x\n",
|
||||
(int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val);
|
||||
#endif
|
||||
br->bit_pos_ += (uint32_t)n_bits;
|
||||
br->bit_pos_ += n_bits;
|
||||
}
|
||||
|
||||
/* Reads the specified number of bits from br and advances the bit pos.
|
||||
Assumes that there is enough input to perform BrotliFillBitWindow. */
|
||||
static BROTLI_INLINE uint32_t BrotliReadBits(
|
||||
BrotliBitReader* const br, int n_bits) {
|
||||
BrotliBitReader* const br, uint32_t n_bits) {
|
||||
if (BROTLI_64_BITS || (n_bits <= 16)) {
|
||||
uint32_t val;
|
||||
BrotliFillBitWindow(br, n_bits);
|
||||
@ -307,7 +309,7 @@ static BROTLI_INLINE uint32_t BrotliReadBits(
|
||||
/* Tries to read the specified amount of bits. Returns 0, if there is not
|
||||
enough input. */
|
||||
static BROTLI_INLINE int BrotliSafeReadBits(
|
||||
BrotliBitReader* const br, int n_bits, uint32_t* val) {
|
||||
BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
|
||||
while (br->bit_pos_ + (uint32_t)n_bits > (sizeof(br->val_) << 3)) {
|
||||
if (br->avail_in == 0) {
|
||||
return 0;
|
||||
@ -321,7 +323,7 @@ static BROTLI_INLINE int BrotliSafeReadBits(
|
||||
/* Advances the bit reader position to the next byte boundary and verifies
|
||||
that any skipped bits are set to zero. */
|
||||
static BROTLI_INLINE int BrotliJumpToByteBoundary(BrotliBitReader* br) {
|
||||
int pad_bits_count = (64 - (int)br->bit_pos_) & 0x7;
|
||||
uint32_t pad_bits_count = (64 - (int)br->bit_pos_) & 0x7;
|
||||
uint32_t pad_bits = 0;
|
||||
if (pad_bits_count != 0) {
|
||||
BrotliTakeBits(br, pad_bits_count, &pad_bits);
|
||||
@ -332,8 +334,8 @@ static BROTLI_INLINE int BrotliJumpToByteBoundary(BrotliBitReader* br) {
|
||||
/* Peeks a byte at specified offset.
|
||||
Precondition: bit reader is parked to a byte boundary.
|
||||
Returns -1 if operation is not feasible. */
|
||||
static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, int offset) {
|
||||
int bytes_left = (int)(sizeof(br->val_) - (br->bit_pos_ >> 3));
|
||||
static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, size_t offset) {
|
||||
size_t bytes_left = sizeof(br->val_) - (br->bit_pos_ >> 3);
|
||||
if (br->bit_pos_ & 7) {
|
||||
return -1;
|
||||
}
|
||||
|
185
dec/decode.c
185
dec/decode.c
@ -48,13 +48,13 @@ extern "C" {
|
||||
|
||||
static const uint8_t kDefaultCodeLength = 8;
|
||||
static const uint8_t kCodeLengthRepeatCode = 16;
|
||||
static const int kNumLiteralCodes = 256;
|
||||
static const int kNumInsertAndCopyCodes = 704;
|
||||
static const int kNumBlockLengthCodes = 26;
|
||||
static const uint32_t kNumLiteralCodes = 256;
|
||||
static const uint32_t kNumInsertAndCopyCodes = 704;
|
||||
static const uint32_t kNumBlockLengthCodes = 26;
|
||||
static const int kLiteralContextBits = 6;
|
||||
static const int kDistanceContextBits = 2;
|
||||
|
||||
#define HUFFMAN_TABLE_BITS 8
|
||||
#define HUFFMAN_TABLE_BITS 8U
|
||||
#define HUFFMAN_TABLE_MASK 0xff
|
||||
|
||||
#define CODE_LENGTH_CODES 18
|
||||
@ -115,7 +115,7 @@ static BROTLI_INLINE BROTLI_NO_ASAN void memmove16(
|
||||
|
||||
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
|
||||
static BROTLI_NOINLINE BrotliResult DecodeVarLenUint8(BrotliState* s,
|
||||
BrotliBitReader* br, int* value) {
|
||||
BrotliBitReader* br, uint32_t* value) {
|
||||
uint32_t bits;
|
||||
switch (s->substate_decode_uint8) {
|
||||
case BROTLI_STATE_DECODE_UINT8_NONE:
|
||||
@ -139,7 +139,7 @@ static BROTLI_NOINLINE BrotliResult DecodeVarLenUint8(BrotliState* s,
|
||||
return BROTLI_RESULT_SUCCESS;
|
||||
}
|
||||
/* Use output value as a temporary storage. It MUST be persisted. */
|
||||
*value = (int)bits;
|
||||
*value = bits;
|
||||
/* No break, transit to the next state. */
|
||||
|
||||
case BROTLI_STATE_DECODE_UINT8_LONG:
|
||||
@ -147,7 +147,7 @@ static BROTLI_NOINLINE BrotliResult DecodeVarLenUint8(BrotliState* s,
|
||||
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG;
|
||||
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
||||
}
|
||||
*value = (1 << *value) + (int)bits;
|
||||
*value = (1U << *value) + bits;
|
||||
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
||||
return BROTLI_RESULT_SUCCESS;
|
||||
|
||||
@ -275,16 +275,16 @@ static BrotliResult BROTLI_NOINLINE DecodeMetaBlockLength(BrotliState* s,
|
||||
}
|
||||
|
||||
/* Decodes the next Huffman code from bit-stream. Reads 0 - 15 bits. */
|
||||
static BROTLI_INLINE int ReadSymbol(const HuffmanCode* table,
|
||||
BrotliBitReader* br) {
|
||||
static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table,
|
||||
BrotliBitReader* br) {
|
||||
/* Read the bits for two reads at once. */
|
||||
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
||||
table += val & HUFFMAN_TABLE_MASK;
|
||||
if (table->bits > HUFFMAN_TABLE_BITS) {
|
||||
int nbits = table->bits - HUFFMAN_TABLE_BITS;
|
||||
uint32_t nbits = table->bits - HUFFMAN_TABLE_BITS;
|
||||
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
||||
table += table->value;
|
||||
table += (int)(val >> HUFFMAN_TABLE_BITS) & (int)BitMask(nbits);
|
||||
table += (val >> HUFFMAN_TABLE_BITS) & BitMask(nbits);
|
||||
}
|
||||
BrotliDropBits(br, table->bits);
|
||||
return table->value;
|
||||
@ -293,8 +293,8 @@ static BROTLI_INLINE int ReadSymbol(const HuffmanCode* table,
|
||||
/* Makes a look-up in first level Huffman table. Peeks 8 bits. */
|
||||
static BROTLI_INLINE void PreloadSymbol(const HuffmanCode* table,
|
||||
BrotliBitReader* br,
|
||||
unsigned* bits,
|
||||
unsigned* value) {
|
||||
uint32_t* bits,
|
||||
uint32_t* value) {
|
||||
table += BrotliGetBits(br, HUFFMAN_TABLE_BITS);
|
||||
*bits = table->bits;
|
||||
*value = table->value;
|
||||
@ -302,17 +302,17 @@ static BROTLI_INLINE void PreloadSymbol(const HuffmanCode* table,
|
||||
|
||||
/* Decodes the next Huffman code using data prepared by PreloadSymbol.
|
||||
Reads 0 - 15 bits. Also peeks 8 following bits. */
|
||||
static BROTLI_INLINE unsigned ReadPreloadedSymbol(const HuffmanCode* table,
|
||||
static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table,
|
||||
BrotliBitReader* br,
|
||||
unsigned* bits,
|
||||
unsigned* value) {
|
||||
unsigned result = *value;
|
||||
uint32_t* bits,
|
||||
uint32_t* value) {
|
||||
uint32_t result = *value;
|
||||
if (PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {
|
||||
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
||||
const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
|
||||
int mask = (int)BitMask((int)(*bits - HUFFMAN_TABLE_BITS));
|
||||
uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS));
|
||||
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
||||
ext += (int)(val >> HUFFMAN_TABLE_BITS) & mask;
|
||||
ext += (val >> HUFFMAN_TABLE_BITS) & mask;
|
||||
BrotliDropBits(br, ext->bits);
|
||||
result = ext->value;
|
||||
} else {
|
||||
@ -322,8 +322,8 @@ static BROTLI_INLINE unsigned ReadPreloadedSymbol(const HuffmanCode* table,
|
||||
return result;
|
||||
}
|
||||
|
||||
static BROTLI_INLINE int Log2Floor(int x) {
|
||||
int result = 0;
|
||||
static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) {
|
||||
uint32_t result = 0;
|
||||
while (x) {
|
||||
x >>= 1;
|
||||
++result;
|
||||
@ -343,12 +343,12 @@ static BROTLI_INLINE int Log2Floor(int x) {
|
||||
B.2) Decoded table is used to decode code lengths of symbols in resulting
|
||||
Huffman table. In worst case 3520 bits are read.
|
||||
*/
|
||||
static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
static BrotliResult ReadHuffmanCode(uint32_t alphabet_size,
|
||||
HuffmanCode* table,
|
||||
int* opt_table_size,
|
||||
uint32_t* opt_table_size,
|
||||
BrotliState* s) {
|
||||
BrotliBitReader* br = &s->br;
|
||||
int i;
|
||||
uint32_t i;
|
||||
/* Unnecessary masking, but might be good for safety. */
|
||||
alphabet_size &= 0x3ff;
|
||||
/* State machine */
|
||||
@ -357,14 +357,14 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
if (!BrotliCheckInputAmount(br, 32)) {
|
||||
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
||||
}
|
||||
i = (int)BrotliReadBits(br, 2);
|
||||
i = BrotliReadBits(br, 2);
|
||||
/* The value is used as follows:
|
||||
1 for simple code;
|
||||
0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
|
||||
BROTLI_LOG_UINT((unsigned)i);
|
||||
BROTLI_LOG_UINT(i);
|
||||
if (i == 1) {
|
||||
/* Read symbols, codes & code lengths directly. */
|
||||
int max_bits = Log2Floor(alphabet_size - 1);
|
||||
uint32_t max_bits = Log2Floor(alphabet_size - 1);
|
||||
uint32_t num_symbols = BrotliReadBits(br, 2);
|
||||
for (i = 0; i < 4; ++i) {
|
||||
s->symbols_lists_array[i] = 0;
|
||||
@ -380,7 +380,7 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
BROTLI_LOG_UINT(s->symbols_lists_array[i]);
|
||||
} while (++i <= num_symbols);
|
||||
for (i = 0; i < num_symbols; ++i) {
|
||||
int k = i + 1;
|
||||
uint32_t k = i + 1;
|
||||
for (; k <= num_symbols; ++k) {
|
||||
if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
|
||||
return BROTLI_FAILURE();
|
||||
@ -432,8 +432,8 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
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) {
|
||||
s->next_symbol[i] = i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
||||
s->symbol_lists[i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF;
|
||||
s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
||||
s->symbol_lists[(int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF;
|
||||
}
|
||||
|
||||
s->symbol = 0;
|
||||
@ -477,7 +477,7 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
}
|
||||
symbol++;
|
||||
} else { /* code_len == 16..17, extra_bits == 2..3 */
|
||||
uint32_t repeat_delta = BrotliReadBits(br, code_len - 14);
|
||||
uint32_t repeat_delta = BrotliReadBits(br, code_len - 14U);
|
||||
uint32_t old_repeat;
|
||||
uint8_t new_len = 0;
|
||||
if (code_len == kCodeLengthRepeatCode) {
|
||||
@ -501,12 +501,12 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
}
|
||||
if (repeat_code_len != 0) {
|
||||
unsigned last = symbol + repeat_delta;
|
||||
i = next_symbol[repeat_code_len];
|
||||
int next = next_symbol[repeat_code_len];
|
||||
do {
|
||||
symbol_lists[i] = (uint16_t)symbol;
|
||||
i = (int)symbol;
|
||||
symbol_lists[next] = (uint16_t)symbol;
|
||||
next = (int)symbol;
|
||||
} while (++symbol != last);
|
||||
next_symbol[repeat_code_len] = i;
|
||||
next_symbol[repeat_code_len] = next;
|
||||
space -= repeat_delta << (15 - repeat_code_len);
|
||||
code_length_histo[repeat_code_len] = (uint16_t)
|
||||
(code_length_histo[repeat_code_len] + repeat_delta);
|
||||
@ -520,7 +520,7 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
{
|
||||
int table_size = BrotliBuildHuffmanTable(
|
||||
uint32_t table_size = BrotliBuildHuffmanTable(
|
||||
table, HUFFMAN_TABLE_BITS, symbol_lists,
|
||||
s->code_length_histo);
|
||||
if (opt_table_size) {
|
||||
@ -539,8 +539,8 @@ static BrotliResult ReadHuffmanCode(int alphabet_size,
|
||||
/* Decodes a block length by reading 3..39 bits. */
|
||||
static BROTLI_INLINE int ReadBlockLength(const HuffmanCode* table,
|
||||
BrotliBitReader* br) {
|
||||
int code;
|
||||
int nbits;
|
||||
uint32_t code;
|
||||
uint32_t nbits;
|
||||
code = ReadSymbol(table, br);
|
||||
nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */
|
||||
return kBlockLengthPrefixCode[code].offset + (int)BrotliReadBits(br, nbits);
|
||||
@ -561,11 +561,11 @@ static BROTLI_INLINE int ReadBlockLength(const HuffmanCode* table,
|
||||
Most of input values are 0 and 1. To reduce number of branches, we replace
|
||||
inner for loop with do-while.
|
||||
*/
|
||||
static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, int v_len,
|
||||
BrotliState* state) {
|
||||
static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v,
|
||||
uint32_t v_len, BrotliState* state) {
|
||||
/* Reinitialize elements that could have been changed. */
|
||||
int i = 4;
|
||||
int upper_bound = state->mtf_upper_bound;
|
||||
uint32_t i = 4;
|
||||
uint32_t upper_bound = state->mtf_upper_bound;
|
||||
uint8_t* mtf = state->mtf;
|
||||
/* Load endian-aware constant. */
|
||||
const uint8_t b0123[4] = {0, 1, 2, 3};
|
||||
@ -585,8 +585,8 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, int v_len,
|
||||
for (i = 0; i < v_len; ++i) {
|
||||
int index = v[i];
|
||||
uint8_t value = mtf[index];
|
||||
upper_bound |= v[i];
|
||||
v[i] = value;
|
||||
upper_bound |= index;
|
||||
do {
|
||||
index--;
|
||||
mtf[index + 1] = mtf[index];
|
||||
@ -598,7 +598,8 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, int v_len,
|
||||
}
|
||||
|
||||
/* Expose function for testing. Will be removed by linker as unused. */
|
||||
void InverseMoveToFrontTransformForTesting(uint8_t* v, int l, BrotliState* s) {
|
||||
void InverseMoveToFrontTransformForTesting(
|
||||
uint8_t* v, uint32_t l, BrotliState* s) {
|
||||
InverseMoveToFrontTransform(v, l, s);
|
||||
}
|
||||
|
||||
@ -612,7 +613,7 @@ static BrotliResult HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
|
||||
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
|
||||
}
|
||||
while (s->htree_index < group->num_htrees) {
|
||||
int table_size;
|
||||
uint32_t table_size;
|
||||
BrotliResult result =
|
||||
ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
|
||||
if (result != BROTLI_RESULT_SUCCESS) return result;
|
||||
@ -633,8 +634,8 @@ static BrotliResult HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
|
||||
3) Read context map items; "0" values could be run-length encoded.
|
||||
4) Optionally, apply InverseMoveToFront transform to the resulting map.
|
||||
*/
|
||||
static BrotliResult DecodeContextMap(int context_map_size,
|
||||
int* num_htrees,
|
||||
static BrotliResult DecodeContextMap(uint32_t context_map_size,
|
||||
uint32_t* num_htrees,
|
||||
uint8_t** context_map_arg,
|
||||
BrotliState* s) {
|
||||
BrotliBitReader* br = &s->br;
|
||||
@ -667,7 +668,7 @@ static BrotliResult DecodeContextMap(int context_map_size,
|
||||
}
|
||||
use_rle_for_zeros = (int)BrotliReadBits(br, 1);
|
||||
if (use_rle_for_zeros) {
|
||||
s->max_run_length_prefix = (int)BrotliReadBits(br, 4) + 1;
|
||||
s->max_run_length_prefix = BrotliReadBits(br, 4) + 1;
|
||||
} else {
|
||||
s->max_run_length_prefix = 0;
|
||||
}
|
||||
@ -681,10 +682,10 @@ static BrotliResult DecodeContextMap(int context_map_size,
|
||||
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
|
||||
/* No break, continue to next state. */
|
||||
case BROTLI_STATE_CONTEXT_MAP_DECODE: {
|
||||
int context_index = s->context_index;
|
||||
int max_run_length_prefix = s->max_run_length_prefix;
|
||||
uint32_t context_index = s->context_index;
|
||||
uint32_t max_run_length_prefix = s->max_run_length_prefix;
|
||||
uint8_t* context_map = *context_map_arg;
|
||||
int code;
|
||||
uint32_t code;
|
||||
while (context_index < context_map_size) {
|
||||
if (!BrotliCheckInputAmount(br, 32)) {
|
||||
s->context_index = context_index;
|
||||
@ -694,8 +695,8 @@ static BrotliResult DecodeContextMap(int context_map_size,
|
||||
BROTLI_LOG_UINT(code);
|
||||
if (code == 0) {
|
||||
context_map[context_index++] = 0;
|
||||
} else if (code - max_run_length_prefix <= 0) {
|
||||
int reps = (1 << code) + (int)BrotliReadBits(br, code);
|
||||
} else if (code <= max_run_length_prefix) {
|
||||
uint32_t reps = (1U << code) + BrotliReadBits(br, code);
|
||||
BROTLI_LOG_UINT(reps);
|
||||
if (context_index + reps > context_map_size) {
|
||||
return BROTLI_FAILURE();
|
||||
@ -721,18 +722,20 @@ static BrotliResult DecodeContextMap(int context_map_size,
|
||||
|
||||
/* Decodes a command or literal and updates block type ringbuffer.
|
||||
Reads 0..15 bits. */
|
||||
static void DecodeBlockType(const int max_block_type,
|
||||
static void DecodeBlockType(const uint32_t max_block_type,
|
||||
const HuffmanCode* trees,
|
||||
int tree_type,
|
||||
int* ringbuffers,
|
||||
uint32_t* ringbuffers,
|
||||
BrotliBitReader* br) {
|
||||
int* ringbuffer = ringbuffers + tree_type * 2;
|
||||
int block_type =
|
||||
ReadSymbol(&trees[tree_type * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br) - 2;
|
||||
if (block_type == -1) {
|
||||
uint32_t* ringbuffer = ringbuffers + tree_type * 2;
|
||||
uint32_t block_type = ReadSymbol(
|
||||
&trees[tree_type * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
||||
if (block_type == 1) {
|
||||
block_type = ringbuffer[1] + 1;
|
||||
} else if (block_type == -2) {
|
||||
} else if (block_type == 0) {
|
||||
block_type = ringbuffer[0];
|
||||
} else {
|
||||
block_type -= 2;
|
||||
}
|
||||
if (block_type >= max_block_type) {
|
||||
block_type -= max_block_type;
|
||||
@ -746,7 +749,7 @@ static void DecodeBlockType(const int max_block_type,
|
||||
static void DecodeBlockTypeWithContext(BrotliState* s,
|
||||
BrotliBitReader* br) {
|
||||
uint8_t context_mode;
|
||||
int context_offset;
|
||||
uint32_t context_offset;
|
||||
DecodeBlockType(s->num_block_types[0], s->block_type_trees, 0,
|
||||
s->block_type_rb, br); /* Reads 0..15 bits. */
|
||||
s->block_length[0] = ReadBlockLength(s->block_len_trees, br); /* 3..39 bits */
|
||||
@ -791,14 +794,14 @@ BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput(BrotliOutput output,
|
||||
for (;;) {
|
||||
switch ((int)s->substate_uncompressed) {
|
||||
case BROTLI_STATE_UNCOMPRESSED_NONE:
|
||||
nbytes = (int)BrotliGetRemainingBytes(&s->br);
|
||||
/* For short lengths copy byte-by-byte */
|
||||
if (s->meta_block_remaining_len < 8 ||
|
||||
s->meta_block_remaining_len < BrotliGetRemainingBytes(&s->br)) {
|
||||
s->meta_block_remaining_len < nbytes) {
|
||||
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_SHORT;
|
||||
break;
|
||||
}
|
||||
/* Copy remaining bytes from s->br.buf_ to ringbuffer. */
|
||||
nbytes = (int)BrotliGetRemainingBytes(&s->br);
|
||||
BrotliCopyBytes(&s->ringbuffer[pos], &s->br, (size_t)nbytes);
|
||||
pos += nbytes;
|
||||
s->meta_block_remaining_len -= nbytes;
|
||||
@ -879,7 +882,7 @@ int BrotliDecompressedSize(size_t encoded_size,
|
||||
BrotliBitReader br;
|
||||
BrotliState s;
|
||||
int next_block_header;
|
||||
int offset;
|
||||
size_t offset;
|
||||
BrotliStateInit(&s);
|
||||
BrotliInitBitReader(&br, in);
|
||||
if (!BrotliReadInput(&br, 1) || !BrotliWarmupBitReader(&br)) {
|
||||
@ -896,13 +899,13 @@ int BrotliDecompressedSize(size_t encoded_size,
|
||||
if (!s.is_uncompressed || !BrotliJumpToByteBoundary(&br)) {
|
||||
return 0;
|
||||
}
|
||||
next_block_header = BrotliPeekByte(&br, s.meta_block_remaining_len);
|
||||
next_block_header = BrotliPeekByte(&br, (size_t)s.meta_block_remaining_len);
|
||||
if (next_block_header != -1) {
|
||||
return (next_block_header & 3) == 3;
|
||||
}
|
||||
/* Currently bit reader can't peek outside of its buffer... */
|
||||
offset = BROTLI_READ_SIZE - (int)BrotliGetRemainingBytes(&br);
|
||||
offset += s.meta_block_remaining_len;
|
||||
offset = BROTLI_READ_SIZE - BrotliGetRemainingBytes(&br);
|
||||
offset += (size_t)s.meta_block_remaining_len;
|
||||
return (offset < encoded_size) && ((encoded_buffer[offset] & 3) == 3);
|
||||
}
|
||||
|
||||
@ -922,7 +925,8 @@ int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s,
|
||||
s->ringbuffer_size = 1 << s->window_bits;
|
||||
|
||||
if (s->is_uncompressed) {
|
||||
int next_block_header = BrotliPeekByte(br, s->meta_block_remaining_len);
|
||||
int next_block_header = BrotliPeekByte(br,
|
||||
(size_t)s->meta_block_remaining_len);
|
||||
if (next_block_header != -1) { /* Peek succeeded */
|
||||
if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */
|
||||
is_last = 1;
|
||||
@ -1015,6 +1019,7 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
uint8_t context;
|
||||
int pos = s->pos;
|
||||
int i = s->loop_counter;
|
||||
uint32_t j;
|
||||
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
||||
BrotliBitReader* br = &s->br;
|
||||
int initial_remaining_len;
|
||||
@ -1199,9 +1204,9 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
||||
break;
|
||||
}
|
||||
s->distance_postfix_bits = (int)BrotliReadBits(br, 2);
|
||||
s->distance_postfix_bits = BrotliReadBits(br, 2);
|
||||
s->num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
|
||||
((int)BrotliReadBits(br, 4) << s->distance_postfix_bits);
|
||||
(BrotliReadBits(br, 4) << s->distance_postfix_bits);
|
||||
BROTLI_LOG_UINT(s->num_direct_distance_codes);
|
||||
BROTLI_LOG_UINT(s->distance_postfix_bits);
|
||||
s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
|
||||
@ -1210,9 +1215,9 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
result = BROTLI_FAILURE();
|
||||
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);
|
||||
for (j = 0; j < s->num_block_types[0]; ++j) {
|
||||
s->context_modes[j] = (uint8_t)(BrotliReadBits(br, 2) << 1);
|
||||
BROTLI_LOG_ARRAY_INDEX(s->context_modes, j);
|
||||
}
|
||||
s->state = BROTLI_STATE_CONTEXT_MAP_1;
|
||||
/* No break, continue to next state */
|
||||
@ -1223,8 +1228,8 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
break;
|
||||
}
|
||||
s->trivial_literal_context = 1;
|
||||
for (i = 0; i < s->num_block_types[0] << kLiteralContextBits; i++) {
|
||||
if (s->context_map[i] != i >> kLiteralContextBits) {
|
||||
for (j = 0; j < s->num_block_types[0] << kLiteralContextBits; j++) {
|
||||
if (s->context_map[j] != j >> kLiteralContextBits) {
|
||||
s->trivial_literal_context = 0;
|
||||
break;
|
||||
}
|
||||
@ -1233,8 +1238,8 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
/* No break, continue to next state */
|
||||
case BROTLI_STATE_CONTEXT_MAP_2:
|
||||
{
|
||||
int num_distance_codes =
|
||||
s->num_direct_distance_codes + (48 << s->distance_postfix_bits);
|
||||
uint32_t num_distance_codes =
|
||||
s->num_direct_distance_codes + (48U << s->distance_postfix_bits);
|
||||
result = DecodeContextMap(
|
||||
s->num_block_types[2] << kDistanceContextBits,
|
||||
&s->num_dist_htrees, &s->dist_context_map, s);
|
||||
@ -1313,7 +1318,7 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
&s->block_len_trees[BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
||||
}
|
||||
{
|
||||
int cmd_code = ReadSymbol(s->htree_command, br);
|
||||
uint32_t cmd_code = ReadSymbol(s->htree_command, br);
|
||||
int insert_len_extra = 0;
|
||||
CmdLutElement v;
|
||||
--s->block_length[1];
|
||||
@ -1340,8 +1345,8 @@ BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
||||
case BROTLI_STATE_COMMAND_INNER:
|
||||
/* Read the literals in the command */
|
||||
if (s->trivial_literal_context) {
|
||||
unsigned bits;
|
||||
unsigned value;
|
||||
uint32_t bits;
|
||||
uint32_t value;
|
||||
PreloadSymbol(s->literal_htree, br, &bits, &value);
|
||||
do {
|
||||
if (!BrotliCheckInputAmount(br, 64)) {
|
||||
@ -1417,7 +1422,7 @@ postDecodeLiterals:
|
||||
BROTLI_DCHECK(s->distance_code < 0);
|
||||
if (s->block_length[2] == 0) {
|
||||
/* Block switch for distance codes */
|
||||
int dist_context_offset;
|
||||
uint32_t dist_context_offset;
|
||||
DecodeBlockType(s->num_block_types[2],
|
||||
s->block_type_trees, 2,
|
||||
s->block_type_rb, br); /* Reads 0..15 bits. */
|
||||
@ -1429,8 +1434,8 @@ postDecodeLiterals:
|
||||
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
||||
}
|
||||
--s->block_length[2];
|
||||
s->distance_code =
|
||||
ReadSymbol(s->distance_hgroup.htrees[s->dist_htree_index], br);
|
||||
s->distance_code = (int)ReadSymbol(
|
||||
s->distance_hgroup.htrees[s->dist_htree_index], br);
|
||||
/* Convert the distance code to the actual distance by possibly */
|
||||
/* looking up past distances from the s->ringbuffer. */
|
||||
if ((s->distance_code & ~0xf) == 0) {
|
||||
@ -1461,22 +1466,22 @@ postDecodeLiterals:
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int distval = s->distance_code - s->num_direct_distance_codes;
|
||||
int distval = s->distance_code - (int)s->num_direct_distance_codes;
|
||||
if (distval >= 0) {
|
||||
int nbits;
|
||||
uint32_t nbits;
|
||||
int postfix;
|
||||
int offset;
|
||||
if (s->distance_postfix_bits == 0) {
|
||||
nbits = (distval >> 1) + 1;
|
||||
nbits = ((uint32_t)distval >> 1) + 1;
|
||||
offset = ((2 + (distval & 1)) << nbits) - 4;
|
||||
s->distance_code = s->num_direct_distance_codes +
|
||||
s->distance_code = (int)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;
|
||||
nbits = ((uint32_t)distval >> 1) + 1;
|
||||
offset = ((2 + (distval & 1)) << nbits) - 4;
|
||||
s->distance_code = s->num_direct_distance_codes +
|
||||
s->distance_code = (int)s->num_direct_distance_codes +
|
||||
((offset + (int)BrotliReadBits(br, nbits)) <<
|
||||
s->distance_postfix_bits) + postfix;
|
||||
}
|
||||
@ -1500,7 +1505,7 @@ postReadDistance:
|
||||
i <= kBrotliMaxDictionaryWordLength) {
|
||||
int offset = kBrotliDictionaryOffsetsByLength[i];
|
||||
int word_id = s->distance_code - s->max_distance - 1;
|
||||
int shift = kBrotliDictionarySizeBitsByLength[i];
|
||||
uint32_t shift = kBrotliDictionarySizeBitsByLength[i];
|
||||
int mask = (int)BitMask(shift);
|
||||
int word_idx = word_id & mask;
|
||||
int transform_idx = word_id >> shift;
|
||||
|
@ -152,7 +152,8 @@ void BrotliSetCustomDictionary(
|
||||
|
||||
|
||||
/* Escalate internal functions visibility; for testing purposes only. */
|
||||
void InverseMoveToFrontTransformForTesting(uint8_t* v, int l, BrotliState* s);
|
||||
void InverseMoveToFrontTransformForTesting(
|
||||
uint8_t* v, uint32_t l, BrotliState* s);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} /* extern "C" */
|
||||
|
@ -32,7 +32,7 @@ static const int kBrotliDictionaryOffsetsByLength[] = {
|
||||
115968, 118528, 119872, 121280, 122016,
|
||||
};
|
||||
|
||||
static const int8_t kBrotliDictionarySizeBitsByLength[] = {
|
||||
static const uint8_t kBrotliDictionarySizeBitsByLength[] = {
|
||||
0, 0, 0, 0, 10, 10, 11, 11, 10, 10,
|
||||
10, 10, 10, 9, 9, 8, 7, 7, 8, 7,
|
||||
7, 6, 6, 5, 5,
|
||||
|
@ -151,7 +151,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
||||
if (offset[0] == 0) {
|
||||
code.bits = 0;
|
||||
code.value = (uint16_t)sorted[0];
|
||||
for (key = 0; key < table_size; ++key) {
|
||||
for (key = 0; key < (uint32_t)table_size; ++key) {
|
||||
table[key] = code;
|
||||
}
|
||||
return;
|
||||
@ -175,10 +175,10 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
||||
} while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
||||
}
|
||||
|
||||
int BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count) {
|
||||
uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count) {
|
||||
HuffmanCode code; /* current table entry */
|
||||
HuffmanCode* table; /* next available space in table */
|
||||
int len; /* current code length */
|
||||
@ -268,15 +268,15 @@ int BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
step <<= 1;
|
||||
sub_key_step >>= 1;
|
||||
}
|
||||
return total_size;
|
||||
return (uint32_t)total_size;
|
||||
}
|
||||
|
||||
int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *val,
|
||||
uint32_t num_symbols) {
|
||||
int table_size = 1;
|
||||
const int goal_size = 1 << root_bits;
|
||||
uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *val,
|
||||
uint32_t num_symbols) {
|
||||
uint32_t table_size = 1;
|
||||
const uint32_t goal_size = 1U << root_bits;
|
||||
switch (num_symbols) {
|
||||
case 0:
|
||||
table[0].bits = 0;
|
||||
@ -362,15 +362,15 @@ int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
return goal_size;
|
||||
}
|
||||
|
||||
void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size,
|
||||
int ntrees) {
|
||||
void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group, uint32_t alphabet_size,
|
||||
uint32_t ntrees) {
|
||||
/* Pack two mallocs into one */
|
||||
const size_t code_size =
|
||||
sizeof(HuffmanCode) * (size_t)(ntrees * BROTLI_HUFFMAN_MAX_TABLE_SIZE);
|
||||
const size_t htree_size = sizeof(HuffmanCode*) * (size_t)ntrees;
|
||||
char *p = (char*)malloc(code_size + htree_size);
|
||||
group->alphabet_size = (int16_t)alphabet_size;
|
||||
group->num_htrees = (int16_t)ntrees;
|
||||
group->alphabet_size = (uint16_t)alphabet_size;
|
||||
group->num_htrees = (uint16_t)ntrees;
|
||||
group->codes = (HuffmanCode*)p;
|
||||
group->htrees = (HuffmanCode**)(p + code_size);
|
||||
}
|
||||
|
@ -48,30 +48,30 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
|
||||
|
||||
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
||||
/* Returns size of resulting table. */
|
||||
int BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count_arg);
|
||||
uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count_arg);
|
||||
|
||||
/* Builds a simple Huffman table. The num_symbols parameter is to be */
|
||||
/* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
|
||||
/* symbols, 3 means 4 symbols with lengths 2,2,2,2, 4 means 4 symbols with */
|
||||
/* lengths 1,2,3,3. */
|
||||
int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *symbols,
|
||||
uint32_t num_symbols);
|
||||
uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *symbols,
|
||||
uint32_t num_symbols);
|
||||
|
||||
/* Contains a collection of Huffman trees with the same alphabet size. */
|
||||
typedef struct {
|
||||
HuffmanCode** htrees;
|
||||
HuffmanCode* codes;
|
||||
int16_t alphabet_size;
|
||||
int16_t num_htrees;
|
||||
uint16_t alphabet_size;
|
||||
uint16_t num_htrees;
|
||||
} HuffmanTreeGroup;
|
||||
|
||||
void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group,
|
||||
int alphabet_size, int ntrees);
|
||||
uint32_t alphabet_size, uint32_t ntrees);
|
||||
void BrotliHuffmanTreeGroupRelease(HuffmanTreeGroup* group);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
|
@ -185,4 +185,6 @@ static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
|
||||
X = NULL; \
|
||||
}
|
||||
|
||||
#define BROTLI_UNUSED(X) (void)(X)
|
||||
|
||||
#endif /* BROTLI_DEC_PORT_H_ */
|
||||
|
@ -24,7 +24,7 @@
|
||||
/* [offset, offset + 2^nbits) */
|
||||
struct PrefixCodeRange {
|
||||
int16_t offset;
|
||||
int8_t nbits;
|
||||
uint8_t nbits;
|
||||
};
|
||||
|
||||
static const struct PrefixCodeRange kBlockLengthPrefixCode[] = {
|
||||
|
18
dec/state.h
18
dec/state.h
@ -128,12 +128,12 @@ typedef struct {
|
||||
int distance_context;
|
||||
int meta_block_remaining_len;
|
||||
int block_length[3];
|
||||
int num_block_types[3];
|
||||
int block_type_rb[6];
|
||||
int distance_postfix_bits;
|
||||
int num_direct_distance_codes;
|
||||
uint32_t num_block_types[3];
|
||||
uint32_t block_type_rb[6];
|
||||
uint32_t distance_postfix_bits;
|
||||
uint32_t num_direct_distance_codes;
|
||||
int distance_postfix_mask;
|
||||
int num_dist_htrees;
|
||||
uint32_t num_dist_htrees;
|
||||
uint8_t* dist_context_map;
|
||||
HuffmanCode *literal_htree;
|
||||
uint8_t literal_htree_index;
|
||||
@ -171,12 +171,12 @@ typedef struct {
|
||||
HuffmanCode* next;
|
||||
|
||||
/* For DecodeContextMap */
|
||||
int context_index;
|
||||
int max_run_length_prefix;
|
||||
uint32_t context_index;
|
||||
uint32_t max_run_length_prefix;
|
||||
HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_TABLE_SIZE];
|
||||
|
||||
/* For InverseMoveToFrontTransform */
|
||||
int mtf_upper_bound;
|
||||
uint32_t mtf_upper_bound;
|
||||
uint8_t mtf[256];
|
||||
|
||||
/* For custom dictionaries */
|
||||
@ -198,7 +198,7 @@ typedef struct {
|
||||
uint8_t size_nibbles;
|
||||
uint32_t window_bits;
|
||||
|
||||
int num_literal_htrees;
|
||||
uint32_t num_literal_htrees;
|
||||
uint8_t* context_map;
|
||||
uint8_t* context_modes;
|
||||
} BrotliState;
|
||||
|
@ -93,7 +93,9 @@ BrotliOutput BrotliFileOutput(FILE* f) {
|
||||
return out;
|
||||
}
|
||||
|
||||
int BrotliNullOutputFunction(void* data, const uint8_t* buf, size_t count) {
|
||||
int BrotliNullOutputFunction(void* data , const uint8_t* buf, size_t count) {
|
||||
BROTLI_UNUSED(data);
|
||||
BROTLI_UNUSED(buf);
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user