brotli 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +1 -0
  3. data/Rakefile +6 -1
  4. data/brotli.gemspec +1 -1
  5. data/docs/Brotli.html +485 -0
  6. data/docs/Brotli/Error.html +124 -0
  7. data/docs/_index.html +122 -0
  8. data/docs/class_list.html +51 -0
  9. data/docs/css/common.css +1 -0
  10. data/docs/css/full_list.css +58 -0
  11. data/docs/css/style.css +496 -0
  12. data/docs/file.README.html +127 -0
  13. data/docs/file_list.html +56 -0
  14. data/docs/frames.html +17 -0
  15. data/docs/index.html +127 -0
  16. data/docs/js/app.js +292 -0
  17. data/docs/js/full_list.js +216 -0
  18. data/docs/js/jquery.js +4 -0
  19. data/docs/method_list.html +67 -0
  20. data/docs/top-level-namespace.html +110 -0
  21. data/ext/brotli/brotli.c +20 -0
  22. data/lib/brotli/version.rb +1 -1
  23. data/vendor/brotli/c/common/constants.h +13 -6
  24. data/vendor/brotli/c/{dec → common}/context.h +182 -172
  25. data/vendor/brotli/c/common/dictionary.bin +0 -0
  26. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
  27. data/vendor/brotli/c/common/dictionary.c +1 -1
  28. data/vendor/brotli/c/common/dictionary.h +4 -4
  29. data/vendor/brotli/c/common/platform.h +509 -0
  30. data/vendor/brotli/c/common/transform.c +235 -0
  31. data/vendor/brotli/c/common/transform.h +80 -0
  32. data/vendor/brotli/c/common/version.h +8 -1
  33. data/vendor/brotli/c/dec/bit_reader.c +1 -1
  34. data/vendor/brotli/c/dec/bit_reader.h +35 -86
  35. data/vendor/brotli/c/dec/decode.c +322 -205
  36. data/vendor/brotli/c/dec/huffman.c +35 -37
  37. data/vendor/brotli/c/dec/huffman.h +13 -9
  38. data/vendor/brotli/c/dec/prefix.h +3 -4
  39. data/vendor/brotli/c/dec/state.c +26 -34
  40. data/vendor/brotli/c/dec/state.h +34 -23
  41. data/vendor/brotli/c/enc/backward_references.c +25 -15
  42. data/vendor/brotli/c/enc/backward_references.h +5 -6
  43. data/vendor/brotli/c/enc/backward_references_hq.c +94 -68
  44. data/vendor/brotli/c/enc/backward_references_hq.h +22 -25
  45. data/vendor/brotli/c/enc/backward_references_inc.h +10 -10
  46. data/vendor/brotli/c/enc/bit_cost.c +1 -1
  47. data/vendor/brotli/c/enc/bit_cost.h +5 -5
  48. data/vendor/brotli/c/enc/block_encoder_inc.h +7 -6
  49. data/vendor/brotli/c/enc/block_splitter.c +2 -3
  50. data/vendor/brotli/c/enc/block_splitter.h +1 -1
  51. data/vendor/brotli/c/enc/block_splitter_inc.h +11 -11
  52. data/vendor/brotli/c/enc/brotli_bit_stream.c +102 -101
  53. data/vendor/brotli/c/enc/brotli_bit_stream.h +19 -38
  54. data/vendor/brotli/c/enc/cluster.c +1 -1
  55. data/vendor/brotli/c/enc/cluster.h +1 -1
  56. data/vendor/brotli/c/enc/command.h +40 -30
  57. data/vendor/brotli/c/enc/compress_fragment.c +21 -22
  58. data/vendor/brotli/c/enc/compress_fragment.h +1 -1
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +101 -68
  60. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +1 -1
  61. data/vendor/brotli/c/enc/dictionary_hash.c +1 -1
  62. data/vendor/brotli/c/enc/encode.c +262 -162
  63. data/vendor/brotli/c/enc/encoder_dict.c +32 -0
  64. data/vendor/brotli/c/enc/encoder_dict.h +41 -0
  65. data/vendor/brotli/c/enc/entropy_encode.c +14 -14
  66. data/vendor/brotli/c/enc/entropy_encode.h +5 -5
  67. data/vendor/brotli/c/enc/entropy_encode_static.h +3 -3
  68. data/vendor/brotli/c/enc/fast_log.h +4 -2
  69. data/vendor/brotli/c/enc/find_match_length.h +3 -3
  70. data/vendor/brotli/c/enc/hash.h +75 -24
  71. data/vendor/brotli/c/enc/hash_composite_inc.h +133 -0
  72. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +9 -8
  73. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +8 -8
  74. data/vendor/brotli/c/enc/hash_longest_match_inc.h +8 -8
  75. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +10 -9
  76. data/vendor/brotli/c/enc/hash_rolling_inc.h +215 -0
  77. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +9 -8
  78. data/vendor/brotli/c/enc/histogram.c +9 -6
  79. data/vendor/brotli/c/enc/histogram.h +6 -3
  80. data/vendor/brotli/c/enc/histogram_inc.h +1 -1
  81. data/vendor/brotli/c/enc/literal_cost.c +5 -5
  82. data/vendor/brotli/c/enc/literal_cost.h +2 -2
  83. data/vendor/brotli/c/enc/memory.c +5 -16
  84. data/vendor/brotli/c/enc/memory.h +40 -1
  85. data/vendor/brotli/c/enc/metablock.c +163 -25
  86. data/vendor/brotli/c/enc/metablock.h +13 -8
  87. data/vendor/brotli/c/enc/metablock_inc.h +1 -1
  88. data/vendor/brotli/c/enc/params.h +44 -0
  89. data/vendor/brotli/c/enc/prefix.h +3 -4
  90. data/vendor/brotli/c/enc/quality.h +29 -24
  91. data/vendor/brotli/c/enc/ringbuffer.h +15 -11
  92. data/vendor/brotli/c/enc/static_dict.c +49 -45
  93. data/vendor/brotli/c/enc/static_dict.h +4 -3
  94. data/vendor/brotli/c/enc/static_dict_lut.h +1 -1
  95. data/vendor/brotli/c/enc/utf8_util.c +20 -20
  96. data/vendor/brotli/c/enc/utf8_util.h +1 -1
  97. data/vendor/brotli/c/enc/write_bits.h +16 -21
  98. data/vendor/brotli/c/include/brotli/decode.h +13 -8
  99. data/vendor/brotli/c/include/brotli/encode.h +33 -8
  100. data/vendor/brotli/c/include/brotli/port.h +211 -83
  101. data/vendor/brotli/c/include/brotli/types.h +0 -7
  102. metadata +33 -12
  103. data/vendor/brotli/c/dec/port.h +0 -168
  104. data/vendor/brotli/c/dec/transform.h +0 -300
  105. data/vendor/brotli/c/enc/context.h +0 -184
  106. data/vendor/brotli/c/enc/port.h +0 -184
@@ -11,8 +11,8 @@
11
11
  #include <string.h> /* memcpy, memset */
12
12
 
13
13
  #include "../common/constants.h"
14
+ #include "../common/platform.h"
14
15
  #include <brotli/types.h>
15
- #include "./port.h"
16
16
 
17
17
  #if defined(__cplusplus) || defined(c_plusplus)
18
18
  extern "C" {
@@ -20,9 +20,9 @@ extern "C" {
20
20
 
21
21
  #define BROTLI_REVERSE_BITS_MAX 8
22
22
 
23
- #ifdef BROTLI_RBIT
23
+ #if defined(BROTLI_RBIT)
24
24
  #define BROTLI_REVERSE_BITS_BASE \
25
- ((sizeof(reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
25
+ ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
26
26
  #else
27
27
  #define BROTLI_REVERSE_BITS_BASE 0
28
28
  static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
@@ -62,13 +62,13 @@ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
62
62
  #endif /* BROTLI_RBIT */
63
63
 
64
64
  #define BROTLI_REVERSE_BITS_LOWEST \
65
- ((reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
65
+ ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
66
66
 
67
67
  /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
68
68
  where reverse(value, len) is the bit-wise reversal of the len least
69
69
  significant bits of value. */
70
- static BROTLI_INLINE reg_t BrotliReverseBits(reg_t num) {
71
- #ifdef BROTLI_RBIT
70
+ static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) {
71
+ #if defined(BROTLI_RBIT)
72
72
  return BROTLI_RBIT(num);
73
73
  #else
74
74
  return kReverseBits[num];
@@ -86,9 +86,9 @@ static BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
86
86
  } while (end > 0);
87
87
  }
88
88
 
89
- /* Returns the table width of the next 2nd level table. count is the histogram
90
- of bit lengths for the remaining symbols, len is the code length of the next
91
- processed symbol */
89
+ /* Returns the table width of the next 2nd level table. |count| is the histogram
90
+ of bit lengths for the remaining symbols, |len| is the code length of the
91
+ next processed symbol. */
92
92
  static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
93
93
  int len, int root_bits) {
94
94
  int left = 1 << (len - root_bits);
@@ -104,12 +104,12 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
104
104
  void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
105
105
  const uint8_t* const code_lengths,
106
106
  uint16_t* count) {
107
- HuffmanCode code; /* current table entry */
108
- int symbol; /* symbol index in original or sorted table */
109
- reg_t key; /* prefix code */
110
- reg_t key_step; /* prefix code addend */
111
- int step; /* step size to replicate values in current table */
112
- int table_size; /* size of current table */
107
+ HuffmanCode code; /* current table entry */
108
+ int symbol; /* symbol index in original or sorted table */
109
+ brotli_reg_t key; /* prefix code */
110
+ brotli_reg_t key_step; /* prefix code addend */
111
+ int step; /* step size to replicate values in current table */
112
+ int table_size; /* size of current table */
113
113
  int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */
114
114
  /* offsets in sorted table for each length */
115
115
  int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
@@ -118,7 +118,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
118
118
  BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
119
119
  BROTLI_REVERSE_BITS_MAX);
120
120
 
121
- /* generate offsets into sorted symbol table by code length */
121
+ /* Generate offsets into sorted symbol table by code length. */
122
122
  symbol = -1;
123
123
  bits = 1;
124
124
  BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {
@@ -129,7 +129,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
129
129
  /* Symbols with code length 0 are placed after all other symbols. */
130
130
  offset[0] = BROTLI_CODE_LENGTH_CODES - 1;
131
131
 
132
- /* sort symbols by length, by symbol order within each length */
132
+ /* Sort symbols by length, by symbol order within each length. */
133
133
  symbol = BROTLI_CODE_LENGTH_CODES;
134
134
  do {
135
135
  BROTLI_REPEAT(6, {
@@ -144,13 +144,13 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
144
144
  if (offset[0] == 0) {
145
145
  code.bits = 0;
146
146
  code.value = (uint16_t)sorted[0];
147
- for (key = 0; key < (reg_t)table_size; ++key) {
147
+ for (key = 0; key < (brotli_reg_t)table_size; ++key) {
148
148
  table[key] = code;
149
149
  }
150
150
  return;
151
151
  }
152
152
 
153
- /* fill in table */
153
+ /* Fill in table. */
154
154
  key = 0;
155
155
  key_step = BROTLI_REVERSE_BITS_LOWEST;
156
156
  symbol = 0;
@@ -172,18 +172,18 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
172
172
  int root_bits,
173
173
  const uint16_t* const symbol_lists,
174
174
  uint16_t* count) {
175
- HuffmanCode code; /* current table entry */
176
- HuffmanCode* table; /* next available space in table */
177
- int len; /* current code length */
178
- int symbol; /* symbol index in original or sorted table */
179
- reg_t key; /* prefix code */
180
- reg_t key_step; /* prefix code addend */
181
- reg_t sub_key; /* 2nd level table prefix code */
182
- reg_t sub_key_step; /* 2nd level table prefix code addend */
183
- int step; /* step size to replicate values in current table */
184
- int table_bits; /* key length of current table */
185
- int table_size; /* size of current table */
186
- int total_size; /* sum of root table size and 2nd level table sizes */
175
+ HuffmanCode code; /* current table entry */
176
+ HuffmanCode* table; /* next available space in table */
177
+ int len; /* current code length */
178
+ int symbol; /* symbol index in original or sorted table */
179
+ brotli_reg_t key; /* prefix code */
180
+ brotli_reg_t key_step; /* prefix code addend */
181
+ brotli_reg_t sub_key; /* 2nd level table prefix code */
182
+ brotli_reg_t sub_key_step; /* 2nd level table prefix code addend */
183
+ int step; /* step size to replicate values in current table */
184
+ int table_bits; /* key length of current table */
185
+ int table_size; /* size of current table */
186
+ int total_size; /* sum of root table size and 2nd level table sizes */
187
187
  int max_length = -1;
188
188
  int bits;
189
189
  int bits_count;
@@ -200,9 +200,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
200
200
  table_size = 1 << table_bits;
201
201
  total_size = table_size;
202
202
 
203
- /* fill in root table */
204
- /* let's reduce the table size to a smaller size if possible, and */
205
- /* create the repetitions by memcpy if possible in the coming loop */
203
+ /* Fill in the root table. Reduce the table size to if possible,
204
+ and create the repetitions by memcpy. */
206
205
  if (table_bits > max_length) {
207
206
  table_bits = max_length;
208
207
  table_size = 1 << table_bits;
@@ -224,15 +223,14 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
224
223
  key_step >>= 1;
225
224
  } while (++bits <= table_bits);
226
225
 
227
- /* if root_bits != table_bits we only created one fraction of the */
228
- /* table, and we need to replicate it now. */
226
+ /* If root_bits != table_bits then replicate to fill the remaining slots. */
229
227
  while (total_size != table_size) {
230
228
  memcpy(&table[table_size], &table[0],
231
229
  (size_t)table_size * sizeof(table[0]));
232
230
  table_size <<= 1;
233
231
  }
234
232
 
235
- /* fill in 2nd level tables and add pointers to root table */
233
+ /* Fill in 2nd level tables and add pointers to root table. */
236
234
  key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);
237
235
  sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);
238
236
  sub_key_step = BROTLI_REVERSE_BITS_LOWEST;
@@ -9,8 +9,8 @@
9
9
  #ifndef BROTLI_DEC_HUFFMAN_H_
10
10
  #define BROTLI_DEC_HUFFMAN_H_
11
11
 
12
+ #include "../common/platform.h"
12
13
  #include <brotli/types.h>
13
- #include "./port.h"
14
14
 
15
15
  #if defined(__cplusplus) || defined(c_plusplus)
16
16
  extern "C" {
@@ -19,10 +19,11 @@ extern "C" {
19
19
  #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
20
20
 
21
21
  /* Maximum possible Huffman table size for an alphabet size of (index * 32),
22
- * max code length 15 and root table bits 8. */
22
+ max code length 15 and root table bits 8. */
23
23
  static const uint16_t kMaxHuffmanTableSize[] = {
24
24
  256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
25
- 854, 886, 920, 952, 984, 1016, 1048, 1080};
25
+ 854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272,
26
+ 1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528};
26
27
  /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
27
28
  #define BROTLI_HUFFMAN_MAX_SIZE_26 396
28
29
  /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
@@ -41,23 +42,26 @@ typedef struct {
41
42
  BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
42
43
  const uint8_t* const code_lengths, uint16_t* count);
43
44
 
44
- /* Builds Huffman lookup table assuming code lengths are in symbol order. */
45
- /* Returns size of resulting table. */
45
+ /* Builds Huffman lookup table assuming code lengths are in symbol order.
46
+ Returns size of resulting table. */
46
47
  BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
47
48
  int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
48
49
 
49
- /* Builds a simple Huffman table. The num_symbols parameter is to be */
50
- /* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
51
- /* symbols, 3 means 4 symbols with lengths 2,2,2,2, 4 means 4 symbols with */
52
- /* lengths 1,2,3,3. */
50
+ /* Builds a simple Huffman table. The |num_symbols| parameter is to be
51
+ interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
52
+ 2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
53
+ 4 means 4 symbols with lengths [1, 2, 3, 3]. */
53
54
  BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
54
55
  int root_bits, uint16_t* symbols, uint32_t num_symbols);
55
56
 
56
57
  /* Contains a collection of Huffman trees with the same alphabet size. */
58
+ /* max_symbol is needed due to simple codes since log2(alphabet_size) could be
59
+ greater than log2(max_symbol). */
57
60
  typedef struct {
58
61
  HuffmanCode** htrees;
59
62
  HuffmanCode* codes;
60
63
  uint16_t alphabet_size;
64
+ uint16_t max_symbol;
61
65
  uint16_t num_htrees;
62
66
  } HuffmanTreeGroup;
63
67
 
@@ -5,8 +5,7 @@
5
5
  */
6
6
 
7
7
  /* Lookup tables to map prefix codes to value ranges. This is used during
8
- decoding of the block lengths, literal insertion lengths and copy lengths.
9
- */
8
+ decoding of the block lengths, literal insertion lengths and copy lengths. */
10
9
 
11
10
  #ifndef BROTLI_DEC_PREFIX_H_
12
11
  #define BROTLI_DEC_PREFIX_H_
@@ -14,8 +13,8 @@
14
13
  #include "../common/constants.h"
15
14
  #include <brotli/types.h>
16
15
 
17
- /* Represents the range of values belonging to a prefix code: */
18
- /* [offset, offset + 2^nbits) */
16
+ /* Represents the range of values belonging to a prefix code:
17
+ [offset, offset + 2^nbits) */
19
18
  struct PrefixCodeRange {
20
19
  uint16_t offset;
21
20
  uint8_t nbits;
@@ -15,25 +15,11 @@
15
15
  extern "C" {
16
16
  #endif
17
17
 
18
- static void* DefaultAllocFunc(void* opaque, size_t size) {
19
- BROTLI_UNUSED(opaque);
20
- return malloc(size);
21
- }
22
-
23
- static void DefaultFreeFunc(void* opaque, void* address) {
24
- BROTLI_UNUSED(opaque);
25
- free(address);
26
- }
27
-
28
- void BrotliDecoderStateInit(BrotliDecoderState* s) {
29
- BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0);
30
- }
31
-
32
- void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
18
+ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
33
19
  brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
34
20
  if (!alloc_func) {
35
- s->alloc_func = DefaultAllocFunc;
36
- s->free_func = DefaultFreeFunc;
21
+ s->alloc_func = BrotliDefaultAllocFunc;
22
+ s->free_func = BrotliDefaultFreeFunc;
37
23
  s->memory_manager_opaque = 0;
38
24
  } else {
39
25
  s->alloc_func = alloc_func;
@@ -45,6 +31,7 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
45
31
 
46
32
  BrotliInitBitReader(&s->br);
47
33
  s->state = BROTLI_STATE_UNINITED;
34
+ s->large_window = 0;
48
35
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
49
36
  s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
50
37
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
@@ -53,8 +40,6 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
53
40
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
54
41
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
55
42
 
56
- s->dictionary = BrotliGetDictionary();
57
-
58
43
  s->buffer_length = 0;
59
44
  s->loop_counter = 0;
60
45
  s->pos = 0;
@@ -103,13 +88,18 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
103
88
  s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
104
89
 
105
90
  s->mtf_upper_bound = 63;
91
+
92
+ s->dictionary = BrotliGetDictionary();
93
+ s->transforms = BrotliGetTransforms();
94
+
95
+ return BROTLI_TRUE;
106
96
  }
107
97
 
108
98
  void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
109
99
  s->meta_block_remaining_len = 0;
110
- s->block_length[0] = 1U << 28;
111
- s->block_length[1] = 1U << 28;
112
- s->block_length[2] = 1U << 28;
100
+ s->block_length[0] = 1U << 24;
101
+ s->block_length[1] = 1U << 24;
102
+ s->block_length[2] = 1U << 24;
113
103
  s->num_block_types[0] = 1;
114
104
  s->num_block_types[1] = 1;
115
105
  s->num_block_types[2] = 1;
@@ -126,8 +116,7 @@ void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
126
116
  s->literal_htree = NULL;
127
117
  s->dist_context_map_slice = NULL;
128
118
  s->dist_htree_index = 0;
129
- s->context_lookup1 = NULL;
130
- s->context_lookup2 = NULL;
119
+ s->context_lookup = NULL;
131
120
  s->literal_hgroup.codes = NULL;
132
121
  s->literal_hgroup.htrees = NULL;
133
122
  s->insert_copy_hgroup.codes = NULL;
@@ -137,30 +126,33 @@ void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
137
126
  }
138
127
 
139
128
  void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
140
- BROTLI_FREE(s, s->context_modes);
141
- BROTLI_FREE(s, s->context_map);
142
- BROTLI_FREE(s, s->dist_context_map);
143
- BROTLI_FREE(s, s->literal_hgroup.htrees);
144
- BROTLI_FREE(s, s->insert_copy_hgroup.htrees);
145
- BROTLI_FREE(s, s->distance_hgroup.htrees);
129
+ BROTLI_DECODER_FREE(s, s->context_modes);
130
+ BROTLI_DECODER_FREE(s, s->context_map);
131
+ BROTLI_DECODER_FREE(s, s->dist_context_map);
132
+ BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
133
+ BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
134
+ BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
146
135
  }
147
136
 
148
137
  void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
149
138
  BrotliDecoderStateCleanupAfterMetablock(s);
150
139
 
151
- BROTLI_FREE(s, s->ringbuffer);
152
- BROTLI_FREE(s, s->block_type_trees);
140
+ BROTLI_DECODER_FREE(s, s->ringbuffer);
141
+ BROTLI_DECODER_FREE(s, s->block_type_trees);
153
142
  }
154
143
 
155
144
  BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
156
- HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
145
+ HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol,
146
+ uint32_t ntrees) {
157
147
  /* Pack two allocations into one */
158
148
  const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
159
149
  const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
160
150
  const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
161
151
  /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
162
- HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size);
152
+ HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
153
+ code_size + htree_size);
163
154
  group->alphabet_size = (uint16_t)alphabet_size;
155
+ group->max_symbol = (uint16_t)max_symbol;
164
156
  group->num_htrees = (uint16_t)ntrees;
165
157
  group->htrees = p;
166
158
  group->codes = (HuffmanCode*)(&p[ntrees]);
@@ -11,10 +11,11 @@
11
11
 
12
12
  #include "../common/constants.h"
13
13
  #include "../common/dictionary.h"
14
+ #include "../common/platform.h"
15
+ #include "../common/transform.h"
14
16
  #include <brotli/types.h>
15
17
  #include "./bit_reader.h"
16
18
  #include "./huffman.h"
17
- #include "./port.h"
18
19
 
19
20
  #if defined(__cplusplus) || defined(c_plusplus)
20
21
  extern "C" {
@@ -22,6 +23,8 @@ extern "C" {
22
23
 
23
24
  typedef enum {
24
25
  BROTLI_STATE_UNINITED,
26
+ BROTLI_STATE_LARGE_WINDOW_BITS,
27
+ BROTLI_STATE_INITIALIZE,
25
28
  BROTLI_STATE_METABLOCK_BEGIN,
26
29
  BROTLI_STATE_METABLOCK_HEADER,
27
30
  BROTLI_STATE_METABLOCK_HEADER_2,
@@ -126,23 +129,22 @@ struct BrotliDecoderStateStruct {
126
129
  uint8_t* ringbuffer;
127
130
  uint8_t* ringbuffer_end;
128
131
  HuffmanCode* htree_command;
129
- const uint8_t* context_lookup1;
130
- const uint8_t* context_lookup2;
132
+ const uint8_t* context_lookup;
131
133
  uint8_t* context_map_slice;
132
134
  uint8_t* dist_context_map_slice;
133
135
 
134
- /* This ring buffer holds a few past copy distances that will be used by */
135
- /* some special distance codes. */
136
+ /* This ring buffer holds a few past copy distances that will be used by
137
+ some special distance codes. */
136
138
  HuffmanTreeGroup literal_hgroup;
137
139
  HuffmanTreeGroup insert_copy_hgroup;
138
140
  HuffmanTreeGroup distance_hgroup;
139
141
  HuffmanCode* block_type_trees;
140
142
  HuffmanCode* block_len_trees;
141
143
  /* This is true if the literal context map histogram type always matches the
142
- block type. It is then not needed to keep the context (faster decoding). */
144
+ block type. It is then not needed to keep the context (faster decoding). */
143
145
  int trivial_literal_context;
144
- /* Distance context is actual after command is decoded and before distance
145
- is computed. After distance computation it is used as a temporary variable. */
146
+ /* Distance context is actual after command is decoded and before distance is
147
+ computed. After distance computation it is used as a temporary variable. */
146
148
  int distance_context;
147
149
  int meta_block_remaining_len;
148
150
  uint32_t block_length_index;
@@ -162,11 +164,11 @@ struct BrotliDecoderStateStruct {
162
164
  int copy_length;
163
165
  int distance_code;
164
166
 
165
- /* For partial write operations */
166
- size_t rb_roundtrips; /* How many times we went around the ring-buffer */
167
- size_t partial_pos_out; /* How much output to the user in total */
167
+ /* For partial write operations. */
168
+ size_t rb_roundtrips; /* how many times we went around the ring-buffer */
169
+ size_t partial_pos_out; /* how much output to the user in total */
168
170
 
169
- /* For ReadHuffmanCode */
171
+ /* For ReadHuffmanCode. */
170
172
  uint32_t symbol;
171
173
  uint32_t repeat;
172
174
  uint32_t space;
@@ -180,25 +182,26 @@ struct BrotliDecoderStateStruct {
180
182
  /* Tails of symbol chains. */
181
183
  int next_symbol[32];
182
184
  uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];
183
- /* Population counts for the code lengths */
185
+ /* Population counts for the code lengths. */
184
186
  uint16_t code_length_histo[16];
185
187
 
186
- /* For HuffmanTreeGroupDecode */
188
+ /* For HuffmanTreeGroupDecode. */
187
189
  int htree_index;
188
190
  HuffmanCode* next;
189
191
 
190
- /* For DecodeContextMap */
192
+ /* For DecodeContextMap. */
191
193
  uint32_t context_index;
192
194
  uint32_t max_run_length_prefix;
193
195
  uint32_t code;
194
196
  HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];
195
197
 
196
- /* For InverseMoveToFrontTransform */
198
+ /* For InverseMoveToFrontTransform. */
197
199
  uint32_t mtf_upper_bound;
198
200
  uint32_t mtf[64 + 1];
199
201
 
200
- /* less used attributes are in the end of this struct */
201
- /* States inside function calls */
202
+ /* Less used attributes are at the end of this struct. */
203
+
204
+ /* States inside function calls. */
202
205
  BrotliRunningMetablockHeaderState substate_metablock_header;
203
206
  BrotliRunningTreeGroupState substate_tree_group;
204
207
  BrotliRunningContextMapState substate_context_map;
@@ -212,6 +215,7 @@ struct BrotliDecoderStateStruct {
212
215
  unsigned int is_metadata : 1;
213
216
  unsigned int should_wrap_ringbuffer : 1;
214
217
  unsigned int canny_ringbuffer_allocation : 1;
218
+ unsigned int large_window : 1;
215
219
  unsigned int size_nibbles : 8;
216
220
  uint32_t window_bits;
217
221
 
@@ -220,7 +224,9 @@ struct BrotliDecoderStateStruct {
220
224
  uint32_t num_literal_htrees;
221
225
  uint8_t* context_map;
222
226
  uint8_t* context_modes;
227
+
223
228
  const BrotliDictionary* dictionary;
229
+ const BrotliTransforms* transforms;
224
230
 
225
231
  uint32_t trivial_literal_contexts[8]; /* 256 bits */
226
232
  };
@@ -228,17 +234,22 @@ struct BrotliDecoderStateStruct {
228
234
  typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal;
229
235
  #define BrotliDecoderState BrotliDecoderStateInternal
230
236
 
231
- BROTLI_INTERNAL void BrotliDecoderStateInit(BrotliDecoderState* s);
232
- BROTLI_INTERNAL void BrotliDecoderStateInitWithCustomAllocators(
233
- BrotliDecoderState* s, brotli_alloc_func alloc_func,
234
- brotli_free_func free_func, void* opaque);
237
+ BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
238
+ brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
235
239
  BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);
236
240
  BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
237
241
  BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
238
242
  BrotliDecoderState* s);
239
243
  BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
240
244
  BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,
241
- uint32_t ntrees);
245
+ uint32_t max_symbol, uint32_t ntrees);
246
+
247
+ #define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
248
+
249
+ #define BROTLI_DECODER_FREE(S, X) { \
250
+ S->free_func(S->memory_manager_opaque, X); \
251
+ X = NULL; \
252
+ }
242
253
 
243
254
  #if defined(__cplusplus) || defined(c_plusplus)
244
255
  } /* extern "C" */