diff options
author | Matt A. Tobin <email@mattatobin.com> | 2019-11-14 21:08:43 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2019-11-14 21:08:43 -0500 |
commit | 1d30f6fa8413746ddc408f93710d701493af273d (patch) | |
tree | abe84e83d704e13c60c90db7ac4b9e363d8a81fc /modules/brotli/dec/huffman.c | |
parent | 9308ec68e863e4c6e650680370a5d7baa9f0d1f3 (diff) | |
parent | 00573571a226a0c59dd744da67483864a22911aa (diff) | |
download | UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.gz UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.lz UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.xz UXP-1d30f6fa8413746ddc408f93710d701493af273d.zip |
Merge branch 'master' into mailnews-work
Diffstat (limited to 'modules/brotli/dec/huffman.c')
-rw-r--r-- | modules/brotli/dec/huffman.c | 143 |
1 files changed, 63 insertions, 80 deletions
diff --git a/modules/brotli/dec/huffman.c b/modules/brotli/dec/huffman.c index 3775ffe7e..30c40d33f 100644 --- a/modules/brotli/dec/huffman.c +++ b/modules/brotli/dec/huffman.c @@ -10,8 +10,9 @@ #include <string.h> /* memcpy, memset */ -#include "./port.h" -#include "./types.h" +#include "../common/constants.h" +#include "../common/platform.h" +#include <brotli/types.h> #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -19,8 +20,9 @@ extern "C" { #define BROTLI_REVERSE_BITS_MAX 8 -#ifdef BROTLI_RBIT -#define BROTLI_REVERSE_BITS_BASE (32 - BROTLI_REVERSE_BITS_MAX) +#if defined(BROTLI_RBIT) +#define BROTLI_REVERSE_BITS_BASE \ + ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX) #else #define BROTLI_REVERSE_BITS_BASE 0 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { @@ -60,13 +62,13 @@ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { #endif /* BROTLI_RBIT */ #define BROTLI_REVERSE_BITS_LOWEST \ - (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) + ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), where reverse(value, len) is the bit-wise reversal of the len least significant bits of value. */ -static BROTLI_INLINE uint32_t BrotliReverseBits(uint32_t num) { -#ifdef BROTLI_RBIT +static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) { +#if defined(BROTLI_RBIT) return BROTLI_RBIT(num); #else return kReverseBits[num]; @@ -84,9 +86,9 @@ static BROTLI_INLINE void ReplicateValue(HuffmanCode* table, } while (end > 0); } -/* Returns the table width of the next 2nd level table. count is the histogram - of bit lengths for the remaining symbols, len is the code length of the next - processed symbol */ +/* Returns the table width of the next 2nd level table. |count| is the histogram + of bit lengths for the remaining symbols, |len| is the code length of the + next processed symbol. */ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count, int len, int root_bits) { int left = 1 << (len - root_bits); @@ -102,13 +104,13 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count, void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, const uint8_t* const code_lengths, uint16_t* count) { - HuffmanCode code; /* current table entry */ - int symbol; /* symbol index in original or sorted table */ - uint32_t key; /* prefix code */ - uint32_t key_step; /* prefix code addend */ - int step; /* step size to replicate values in current table */ - int table_size; /* size of current table */ - int sorted[18]; /* symbols sorted by code length */ + HuffmanCode code; /* current table entry */ + int symbol; /* symbol index in original or sorted table */ + brotli_reg_t key; /* prefix code */ + brotli_reg_t key_step; /* prefix code addend */ + int step; /* step size to replicate values in current table */ + int table_size; /* size of current table */ + int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */ /* offsets in sorted table for each length */ int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]; int bits; @@ -116,7 +118,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX); - /* generate offsets into sorted symbol table by code length */ + /* Generate offsets into sorted symbol table by code length. */ symbol = -1; bits = 1; BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, { @@ -125,10 +127,10 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, bits++; }); /* Symbols with code length 0 are placed after all other symbols. */ - offset[0] = 17; + offset[0] = BROTLI_CODE_LENGTH_CODES - 1; - /* sort symbols by length, by symbol order within each length */ - symbol = 18; + /* Sort symbols by length, by symbol order within each length. */ + symbol = BROTLI_CODE_LENGTH_CODES; do { BROTLI_REPEAT(6, { symbol--; @@ -140,24 +142,22 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, /* Special case: all symbols but one have 0 code length. */ if (offset[0] == 0) { - code.bits = 0; - code.value = (uint16_t)sorted[0]; - for (key = 0; key < (uint32_t)table_size; ++key) { + code = ConstructHuffmanCode(0, (uint16_t)sorted[0]); + for (key = 0; key < (brotli_reg_t)table_size; ++key) { table[key] = code; } return; } - /* fill in table */ + /* Fill in table. */ key = 0; key_step = BROTLI_REVERSE_BITS_LOWEST; symbol = 0; bits = 1; step = 2; do { - code.bits = (uint8_t)bits; for (bits_count = count[bits]; bits_count != 0; --bits_count) { - code.value = (uint16_t)sorted[symbol++]; + code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]); ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); key += key_step; } @@ -174,10 +174,10 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, HuffmanCode* table; /* next available space in table */ int len; /* current code length */ int symbol; /* symbol index in original or sorted table */ - uint32_t key; /* prefix code */ - uint32_t key_step; /* prefix code addend */ - uint32_t sub_key; /* 2nd level table prefix code */ - uint32_t sub_key_step; /* 2nd level table prefix code addend */ + brotli_reg_t key; /* prefix code */ + brotli_reg_t key_step; /* prefix code addend */ + brotli_reg_t sub_key; /* 2nd level table prefix code */ + brotli_reg_t sub_key_step; /* 2nd level table prefix code addend */ int step; /* step size to replicate values in current table */ int table_bits; /* key length of current table */ int table_size; /* size of current table */ @@ -198,9 +198,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, table_size = 1 << table_bits; total_size = table_size; - /* fill in root table */ - /* let's reduce the table size to a smaller size if possible, and */ - /* create the repetitions by memcpy if possible in the coming loop */ + /* Fill in the root table. Reduce the table size to if possible, + and create the repetitions by memcpy. */ if (table_bits > max_length) { table_bits = max_length; table_size = 1 << table_bits; @@ -210,11 +209,10 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, bits = 1; step = 2; do { - code.bits = (uint8_t)bits; symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); for (bits_count = count[bits]; bits_count != 0; --bits_count) { symbol = symbol_lists[symbol]; - code.value = (uint16_t)symbol; + code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol); ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code); key += key_step; } @@ -222,15 +220,14 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, key_step >>= 1; } while (++bits <= table_bits); - /* if root_bits != table_bits we only created one fraction of the */ - /* table, and we need to replicate it now. */ + /* If root_bits != table_bits then replicate to fill the remaining slots. */ while (total_size != table_size) { memcpy(&table[table_size], &table[0], (size_t)table_size * sizeof(table[0])); table_size <<= 1; } - /* fill in 2nd level tables and add pointers to root table */ + /* Fill in 2nd level tables and add pointers to root table. */ key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1); sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1); sub_key_step = BROTLI_REVERSE_BITS_LOWEST; @@ -244,14 +241,13 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, total_size += table_size; sub_key = BrotliReverseBits(key); key += key_step; - root_table[sub_key].bits = (uint8_t)(table_bits + root_bits); - root_table[sub_key].value = - (uint16_t)(((size_t)(table - root_table)) - sub_key); + root_table[sub_key] = ConstructHuffmanCode( + (uint8_t)(table_bits + root_bits), + (uint16_t)(((size_t)(table - root_table)) - sub_key)); sub_key = 0; } - code.bits = (uint8_t)(len - root_bits); symbol = symbol_lists[symbol]; - code.value = (uint16_t)symbol; + code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol); ReplicateValue( &table[BrotliReverseBits(sub_key)], step, table_size, code); sub_key += sub_key_step; @@ -270,35 +266,28 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, const uint32_t goal_size = 1U << root_bits; switch (num_symbols) { case 0: - table[0].bits = 0; - table[0].value = val[0]; + table[0] = ConstructHuffmanCode(0, val[0]); break; case 1: - table[0].bits = 1; - table[1].bits = 1; if (val[1] > val[0]) { - table[0].value = val[0]; - table[1].value = val[1]; + table[0] = ConstructHuffmanCode(1, val[0]); + table[1] = ConstructHuffmanCode(1, val[1]); } else { - table[0].value = val[1]; - table[1].value = val[0]; + table[0] = ConstructHuffmanCode(1, val[1]); + table[1] = ConstructHuffmanCode(1, val[0]); } table_size = 2; break; case 2: - table[0].bits = 1; - table[0].value = val[0]; - table[2].bits = 1; - table[2].value = val[0]; + table[0] = ConstructHuffmanCode(1, val[0]); + table[2] = ConstructHuffmanCode(1, val[0]); if (val[2] > val[1]) { - table[1].value = val[1]; - table[3].value = val[2]; + table[1] = ConstructHuffmanCode(2, val[1]); + table[3] = ConstructHuffmanCode(2, val[2]); } else { - table[1].value = val[2]; - table[3].value = val[1]; + table[1] = ConstructHuffmanCode(2, val[2]); + table[3] = ConstructHuffmanCode(2, val[1]); } - table[1].bits = 2; - table[3].bits = 2; table_size = 4; break; case 3: { @@ -312,33 +301,27 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, } } } - for (i = 0; i < 4; ++i) { - table[i].bits = 2; - } - table[0].value = val[0]; - table[2].value = val[1]; - table[1].value = val[2]; - table[3].value = val[3]; + table[0] = ConstructHuffmanCode(2, val[0]); + table[2] = ConstructHuffmanCode(2, val[1]); + table[1] = ConstructHuffmanCode(2, val[2]); + table[3] = ConstructHuffmanCode(2, val[3]); table_size = 4; break; } case 4: { - int i; if (val[3] < val[2]) { uint16_t t = val[3]; val[3] = val[2]; val[2] = t; } - for (i = 0; i < 7; ++i) { - table[i].value = val[0]; - table[i].bits = (uint8_t)(1 + (i & 1)); - } - table[1].value = val[1]; - table[3].value = val[2]; - table[5].value = val[1]; - table[7].value = val[3]; - table[3].bits = 3; - table[7].bits = 3; + table[0] = ConstructHuffmanCode(1, val[0]); + table[1] = ConstructHuffmanCode(2, val[1]); + table[2] = ConstructHuffmanCode(1, val[0]); + table[3] = ConstructHuffmanCode(3, val[2]); + table[4] = ConstructHuffmanCode(1, val[0]); + table[5] = ConstructHuffmanCode(2, val[1]); + table[6] = ConstructHuffmanCode(1, val[0]); + table[7] = ConstructHuffmanCode(3, val[3]); table_size = 8; break; } |