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,41 +11,38 @@
11
11
 
12
12
  #include "../common/constants.h"
13
13
  #include "../common/dictionary.h"
14
+ #include "../common/platform.h"
14
15
  #include <brotli/types.h>
15
16
  #include "./command.h"
16
17
  #include "./hash.h"
17
18
  #include "./memory.h"
18
- #include "./port.h"
19
19
  #include "./quality.h"
20
20
 
21
21
  #if defined(__cplusplus) || defined(c_plusplus)
22
22
  extern "C" {
23
23
  #endif
24
24
 
25
- BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(
26
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
27
- size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
28
- const BrotliEncoderParams* params, HasherHandle hasher, int* dist_cache,
29
- size_t* last_insert_len, Command* commands, size_t* num_commands,
30
- size_t* num_literals);
25
+ BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m,
26
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
27
+ size_t ringbuffer_mask, const BrotliEncoderParams* params,
28
+ HasherHandle hasher, int* dist_cache, size_t* last_insert_len,
29
+ Command* commands, size_t* num_commands, size_t* num_literals);
31
30
 
32
- BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(
33
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
34
- size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
35
- const BrotliEncoderParams* params, HasherHandle hasher, int* dist_cache,
36
- size_t* last_insert_len, Command* commands, size_t* num_commands,
37
- size_t* num_literals);
31
+ BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m,
32
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
33
+ size_t ringbuffer_mask, const BrotliEncoderParams* params,
34
+ HasherHandle hasher, int* dist_cache, size_t* last_insert_len,
35
+ Command* commands, size_t* num_commands, size_t* num_literals);
38
36
 
39
37
  typedef struct ZopfliNode {
40
- /* best length to get up to this byte (not including this byte itself)
41
- highest 8 bit is used to reconstruct the length code */
38
+ /* Best length to get up to this byte (not including this byte itself)
39
+ highest 7 bit is used to reconstruct the length code. */
42
40
  uint32_t length;
43
- /* distance associated with the length
44
- highest 7 bit contains distance short code + 1 (or zero if no short code)
45
- */
41
+ /* Distance associated with the length. */
46
42
  uint32_t distance;
47
- /* number of literal inserts before this copy */
48
- uint32_t insert_length;
43
+ /* Number of literal inserts before this copy; highest 5 bits contain
44
+ distance short code + 1 (or zero if no short code). */
45
+ uint32_t dcode_insert_length;
49
46
 
50
47
  /* This union holds information used by dynamic-programming. During forward
51
48
  pass |cost| it used to store the goal function. When node is processed its
@@ -77,11 +74,11 @@ BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length);
77
74
  (1) nodes[i].copy_length() >= 2
78
75
  (2) nodes[i].command_length() <= i and
79
76
  (3) nodes[i - nodes[i].command_length()].cost < kInfinity */
80
- BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(
81
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
82
- size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
83
- const BrotliEncoderParams* params, const size_t max_backward_limit,
84
- const int* dist_cache, HasherHandle hasher, ZopfliNode* nodes);
77
+ BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(MemoryManager* m,
78
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
79
+ size_t ringbuffer_mask, const BrotliEncoderParams* params,
80
+ const size_t max_backward_limit, const int* dist_cache, HasherHandle hasher,
81
+ ZopfliNode* nodes);
85
82
 
86
83
  BROTLI_INTERNAL void BrotliZopfliCreateCommands(
87
84
  const size_t num_bytes, const size_t block_start,
@@ -8,8 +8,7 @@
8
8
  /* template parameters: EXPORT_FN, FN */
9
9
 
10
10
  static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
11
- const BrotliDictionary* dictionary,
12
- const uint16_t* dictionary_hash, size_t num_bytes, size_t position,
11
+ size_t num_bytes, size_t position,
13
12
  const uint8_t* ringbuffer, size_t ringbuffer_mask,
14
13
  const BrotliEncoderParams* params, HasherHandle hasher, int* dist_cache,
15
14
  size_t* last_insert_len, Command* commands, size_t* num_commands,
@@ -42,9 +41,10 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
42
41
  sr.len_code_delta = 0;
43
42
  sr.distance = 0;
44
43
  sr.score = kMinScore;
45
- FN(FindLongestMatch)(hasher, dictionary, dictionary_hash, ringbuffer,
46
- ringbuffer_mask, dist_cache, position,
47
- max_length, max_distance, gap, &sr);
44
+ FN(FindLongestMatch)(hasher, &params->dictionary,
45
+ ringbuffer, ringbuffer_mask, dist_cache, position,
46
+ max_length, max_distance, gap,
47
+ params->dist.max_distance, &sr);
48
48
  if (sr.score > kMinScore) {
49
49
  /* Found a match. Let's look for something even better ahead. */
50
50
  int delayed_backward_references_in_row = 0;
@@ -58,9 +58,9 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
58
58
  sr2.distance = 0;
59
59
  sr2.score = kMinScore;
60
60
  max_distance = BROTLI_MIN(size_t, position + 1, max_backward_limit);
61
- FN(FindLongestMatch)(hasher, dictionary, dictionary_hash, ringbuffer,
62
- ringbuffer_mask, dist_cache, position + 1,
63
- max_length, max_distance, gap, &sr2);
61
+ FN(FindLongestMatch)(hasher, &params->dictionary,
62
+ ringbuffer, ringbuffer_mask, dist_cache, position + 1, max_length,
63
+ max_distance, gap, params->dist.max_distance, &sr2);
64
64
  if (sr2.score >= sr.score + cost_diff_lazy) {
65
65
  /* Ok, let's just write one byte for now and start a match from the
66
66
  next byte. */
@@ -89,8 +89,8 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
89
89
  dist_cache[0] = (int)sr.distance;
90
90
  FN(PrepareDistanceCache)(hasher, dist_cache);
91
91
  }
92
- InitCommand(commands++, insert_length, sr.len, sr.len_code_delta,
93
- distance_code);
92
+ InitCommand(commands++, &params->dist, insert_length,
93
+ sr.len, sr.len_code_delta, distance_code);
94
94
  }
95
95
  *num_literals += insert_length;
96
96
  insert_length = 0;
@@ -9,10 +9,10 @@
9
9
  #include "./bit_cost.h"
10
10
 
11
11
  #include "../common/constants.h"
12
+ #include "../common/platform.h"
12
13
  #include <brotli/types.h>
13
14
  #include "./fast_log.h"
14
15
  #include "./histogram.h"
15
- #include "./port.h"
16
16
 
17
17
  #if defined(__cplusplus) || defined(c_plusplus)
18
18
  extern "C" {
@@ -9,20 +9,20 @@
9
9
  #ifndef BROTLI_ENC_BIT_COST_H_
10
10
  #define BROTLI_ENC_BIT_COST_H_
11
11
 
12
+ #include "../common/platform.h"
12
13
  #include <brotli/types.h>
13
14
  #include "./fast_log.h"
14
15
  #include "./histogram.h"
15
- #include "./port.h"
16
16
 
17
17
  #if defined(__cplusplus) || defined(c_plusplus)
18
18
  extern "C" {
19
19
  #endif
20
20
 
21
- static BROTLI_INLINE double ShannonEntropy(const uint32_t *population,
22
- size_t size, size_t *total) {
21
+ static BROTLI_INLINE double ShannonEntropy(
22
+ const uint32_t* population, size_t size, size_t* total) {
23
23
  size_t sum = 0;
24
24
  double retval = 0;
25
- const uint32_t *population_end = population + size;
25
+ const uint32_t* population_end = population + size;
26
26
  size_t p;
27
27
  if (size & 1) {
28
28
  goto odd_number_of_elements_left;
@@ -42,7 +42,7 @@ static BROTLI_INLINE double ShannonEntropy(const uint32_t *population,
42
42
  }
43
43
 
44
44
  static BROTLI_INLINE double BitsEntropy(
45
- const uint32_t *population, size_t size) {
45
+ const uint32_t* population, size_t size) {
46
46
  size_t sum;
47
47
  double retval = ShannonEntropy(population, size, &sum);
48
48
  if (retval < sum) {
@@ -13,9 +13,9 @@
13
13
  stream. */
14
14
  static void FN(BuildAndStoreEntropyCodes)(MemoryManager* m, BlockEncoder* self,
15
15
  const HistogramType* histograms, const size_t histograms_size,
16
- HuffmanTree* tree, size_t* storage_ix, uint8_t* storage) {
17
- const size_t alphabet_size = self->alphabet_size_;
18
- const size_t table_size = histograms_size * alphabet_size;
16
+ const size_t alphabet_size, HuffmanTree* tree,
17
+ size_t* storage_ix, uint8_t* storage) {
18
+ const size_t table_size = histograms_size * self->histogram_length_;
19
19
  self->depths_ = BROTLI_ALLOC(m, uint8_t, table_size);
20
20
  self->bits_ = BROTLI_ALLOC(m, uint16_t, table_size);
21
21
  if (BROTLI_IS_OOM(m)) return;
@@ -23,9 +23,10 @@ static void FN(BuildAndStoreEntropyCodes)(MemoryManager* m, BlockEncoder* self,
23
23
  {
24
24
  size_t i;
25
25
  for (i = 0; i < histograms_size; ++i) {
26
- size_t ix = i * alphabet_size;
27
- BuildAndStoreHuffmanTree(&histograms[i].data_[0], alphabet_size, tree,
28
- &self->depths_[ix], &self->bits_[ix], storage_ix, storage);
26
+ size_t ix = i * self->histogram_length_;
27
+ BuildAndStoreHuffmanTree(&histograms[i].data_[0], self->histogram_length_,
28
+ alphabet_size, tree, &self->depths_[ix], &self->bits_[ix],
29
+ storage_ix, storage);
29
30
  }
30
31
  }
31
32
  }
@@ -8,16 +8,15 @@
8
8
 
9
9
  #include "./block_splitter.h"
10
10
 
11
- #include <assert.h>
12
11
  #include <string.h> /* memcpy, memset */
13
12
 
13
+ #include "../common/platform.h"
14
14
  #include "./bit_cost.h"
15
15
  #include "./cluster.h"
16
16
  #include "./command.h"
17
17
  #include "./fast_log.h"
18
18
  #include "./histogram.h"
19
19
  #include "./memory.h"
20
- #include "./port.h"
21
20
  #include "./quality.h"
22
21
 
23
22
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -175,7 +174,7 @@ void BrotliSplitBlock(MemoryManager* m,
175
174
  for (i = 0; i < num_commands; ++i) {
176
175
  const Command* cmd = &cmds[i];
177
176
  if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
178
- distance_prefixes[j++] = cmd->dist_prefix_;
177
+ distance_prefixes[j++] = cmd->dist_prefix_ & 0x3FF;
179
178
  }
180
179
  }
181
180
  /* Create the block split on the array of distance prefixes. */
@@ -9,10 +9,10 @@
9
9
  #ifndef BROTLI_ENC_BLOCK_SPLITTER_H_
10
10
  #define BROTLI_ENC_BLOCK_SPLITTER_H_
11
11
 
12
+ #include "../common/platform.h"
12
13
  #include <brotli/types.h>
13
14
  #include "./command.h"
14
15
  #include "./memory.h"
15
- #include "./port.h"
16
16
  #include "./quality.h"
17
17
 
18
18
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -70,13 +70,13 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length,
70
70
  double* insert_cost,
71
71
  double* cost,
72
72
  uint8_t* switch_signal,
73
- uint8_t *block_id) {
73
+ uint8_t* block_id) {
74
74
  const size_t data_size = FN(HistogramDataSize)();
75
75
  const size_t bitmaplen = (num_histograms + 7) >> 3;
76
76
  size_t num_blocks = 1;
77
77
  size_t i;
78
78
  size_t j;
79
- assert(num_histograms <= 256);
79
+ BROTLI_DCHECK(num_histograms <= 256);
80
80
  if (num_histograms <= 1) {
81
81
  for (i = 0; i < length; ++i) {
82
82
  block_id[i] = 0;
@@ -126,7 +126,7 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length,
126
126
  if (cost[k] >= block_switch_cost) {
127
127
  const uint8_t mask = (uint8_t)(1u << (k & 7));
128
128
  cost[k] = block_switch_cost;
129
- assert((k >> 3) < bitmaplen);
129
+ BROTLI_DCHECK((k >> 3) < bitmaplen);
130
130
  switch_signal[ix + (k >> 3)] |= mask;
131
131
  }
132
132
  }
@@ -137,7 +137,7 @@ static size_t FN(FindBlocks)(const DataType* data, const size_t length,
137
137
  uint8_t cur_id = block_id[byte_ix];
138
138
  while (byte_ix > 0) {
139
139
  const uint8_t mask = (uint8_t)(1u << (cur_id & 7));
140
- assert(((size_t)cur_id >> 3) < bitmaplen);
140
+ BROTLI_DCHECK(((size_t)cur_id >> 3) < bitmaplen);
141
141
  --byte_ix;
142
142
  ix -= bitmaplen;
143
143
  if (switch_signal[ix + (cur_id >> 3)] & mask) {
@@ -161,16 +161,16 @@ static size_t FN(RemapBlockIds)(uint8_t* block_ids, const size_t length,
161
161
  new_id[i] = kInvalidId;
162
162
  }
163
163
  for (i = 0; i < length; ++i) {
164
- assert(block_ids[i] < num_histograms);
164
+ BROTLI_DCHECK(block_ids[i] < num_histograms);
165
165
  if (new_id[block_ids[i]] == kInvalidId) {
166
166
  new_id[block_ids[i]] = next_id++;
167
167
  }
168
168
  }
169
169
  for (i = 0; i < length; ++i) {
170
170
  block_ids[i] = (uint8_t)new_id[block_ids[i]];
171
- assert(block_ids[i] < num_histograms);
171
+ BROTLI_DCHECK(block_ids[i] < num_histograms);
172
172
  }
173
- assert(next_id <= num_histograms);
173
+ BROTLI_DCHECK(next_id <= num_histograms);
174
174
  return next_id;
175
175
  }
176
176
 
@@ -226,13 +226,13 @@ static void FN(ClusterBlocks)(MemoryManager* m,
226
226
  {
227
227
  size_t block_idx = 0;
228
228
  for (i = 0; i < length; ++i) {
229
- assert(block_idx < num_blocks);
229
+ BROTLI_DCHECK(block_idx < num_blocks);
230
230
  ++block_lengths[block_idx];
231
231
  if (i + 1 == length || block_ids[i] != block_ids[i + 1]) {
232
232
  ++block_idx;
233
233
  }
234
234
  }
235
- assert(block_idx == num_blocks);
235
+ BROTLI_DCHECK(block_idx == num_blocks);
236
236
  }
237
237
 
238
238
  for (i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH) {
@@ -268,8 +268,8 @@ static void FN(ClusterBlocks)(MemoryManager* m,
268
268
  histogram_symbols[i + j] = (uint32_t)num_clusters + remap[symbols[j]];
269
269
  }
270
270
  num_clusters += num_new_clusters;
271
- assert(num_clusters == cluster_size_size);
272
- assert(num_clusters == all_histograms_size);
271
+ BROTLI_DCHECK(num_clusters == cluster_size_size);
272
+ BROTLI_DCHECK(num_clusters == all_histograms_size);
273
273
  }
274
274
  BROTLI_FREE(m, histograms);
275
275
 
@@ -13,13 +13,14 @@
13
13
  #include <string.h> /* memcpy, memset */
14
14
 
15
15
  #include "../common/constants.h"
16
+ #include "../common/context.h"
17
+ #include "../common/platform.h"
16
18
  #include <brotli/types.h>
17
- #include "./context.h"
18
19
  #include "./entropy_encode.h"
19
20
  #include "./entropy_encode_static.h"
20
21
  #include "./fast_log.h"
22
+ #include "./histogram.h"
21
23
  #include "./memory.h"
22
- #include "./port.h"
23
24
  #include "./write_bits.h"
24
25
 
25
26
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -27,12 +28,11 @@ extern "C" {
27
28
  #endif
28
29
 
29
30
  #define MAX_HUFFMAN_TREE_SIZE (2 * BROTLI_NUM_COMMAND_SYMBOLS + 1)
30
- /* The size of Huffman dictionary for distances assuming that NPOSTFIX = 0 and
31
- NDIRECT = 0. */
32
- #define SIMPLE_DISTANCE_ALPHABET_SIZE (BROTLI_NUM_DISTANCE_SHORT_CODES + \
33
- (2 * BROTLI_MAX_DISTANCE_BITS))
34
- /* SIMPLE_DISTANCE_ALPHABET_SIZE == 64 */
35
- #define SIMPLE_DISTANCE_ALPHABET_BITS 6
31
+ /* The maximum size of Huffman dictionary for distances assuming that
32
+ NPOSTFIX = 0 and NDIRECT = 0. */
33
+ #define MAX_SIMPLE_DISTANCE_ALPHABET_SIZE \
34
+ BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_LARGE_MAX_DISTANCE_BITS)
35
+ /* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */
36
36
 
37
37
  /* Represents the range of values belonging to a prefix code:
38
38
  [offset, offset + 2^nbits) */
@@ -89,9 +89,9 @@ static void BrotliEncodeMlen(size_t length, uint64_t* bits,
89
89
  size_t* numbits, uint64_t* nibblesbits) {
90
90
  size_t lg = (length == 1) ? 1 : Log2FloorNonZero((uint32_t)(length - 1)) + 1;
91
91
  size_t mnibbles = (lg < 16 ? 16 : (lg + 3)) / 4;
92
- assert(length > 0);
93
- assert(length <= (1 << 24));
94
- assert(lg <= 24);
92
+ BROTLI_DCHECK(length > 0);
93
+ BROTLI_DCHECK(length <= (1 << 24));
94
+ BROTLI_DCHECK(lg <= 24);
95
95
  *nibblesbits = mnibbles - 4;
96
96
  *numbits = mnibbles * 4;
97
97
  *bits = length - 1;
@@ -258,7 +258,7 @@ static void StoreSimpleHuffmanTree(const uint8_t* depths,
258
258
  size_t symbols[4],
259
259
  size_t num_symbols,
260
260
  size_t max_bits,
261
- size_t *storage_ix, uint8_t *storage) {
261
+ size_t* storage_ix, uint8_t* storage) {
262
262
  /* value of 1 indicates a simple Huffman code */
263
263
  BrotliWriteBits(2, 1, storage_ix, storage);
264
264
  BrotliWriteBits(2, num_symbols - 1, storage_ix, storage); /* NSYM - 1 */
@@ -297,7 +297,7 @@ static void StoreSimpleHuffmanTree(const uint8_t* depths,
297
297
  depths = symbol depths */
298
298
  void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
299
299
  HuffmanTree* tree,
300
- size_t *storage_ix, uint8_t *storage) {
300
+ size_t* storage_ix, uint8_t* storage) {
301
301
  /* Write the Huffman tree into the brotli-representation.
302
302
  The command alphabet is the largest, so this allocation will fit all
303
303
  alphabets. */
@@ -311,7 +311,7 @@ void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
311
311
  int num_codes = 0;
312
312
  size_t code = 0;
313
313
 
314
- assert(num <= BROTLI_NUM_COMMAND_SYMBOLS);
314
+ BROTLI_DCHECK(num <= BROTLI_NUM_COMMAND_SYMBOLS);
315
315
 
316
316
  BrotliWriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree,
317
317
  huffman_tree_extra_bits);
@@ -360,8 +360,9 @@ void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
360
360
 
361
361
  /* Builds a Huffman tree from histogram[0:length] into depth[0:length] and
362
362
  bits[0:length] and stores the encoded tree to the bit stream. */
363
- static void BuildAndStoreHuffmanTree(const uint32_t *histogram,
364
- const size_t length,
363
+ static void BuildAndStoreHuffmanTree(const uint32_t* histogram,
364
+ const size_t histogram_length,
365
+ const size_t alphabet_size,
365
366
  HuffmanTree* tree,
366
367
  uint8_t* depth,
367
368
  uint16_t* bits,
@@ -371,7 +372,7 @@ static void BuildAndStoreHuffmanTree(const uint32_t *histogram,
371
372
  size_t s4[4] = { 0 };
372
373
  size_t i;
373
374
  size_t max_bits = 0;
374
- for (i = 0; i < length; i++) {
375
+ for (i = 0; i < histogram_length; i++) {
375
376
  if (histogram[i]) {
376
377
  if (count < 4) {
377
378
  s4[count] = i;
@@ -383,7 +384,7 @@ static void BuildAndStoreHuffmanTree(const uint32_t *histogram,
383
384
  }
384
385
 
385
386
  {
386
- size_t max_bits_counter = length - 1;
387
+ size_t max_bits_counter = alphabet_size - 1;
387
388
  while (max_bits_counter) {
388
389
  max_bits_counter >>= 1;
389
390
  ++max_bits;
@@ -398,14 +399,14 @@ static void BuildAndStoreHuffmanTree(const uint32_t *histogram,
398
399
  return;
399
400
  }
400
401
 
401
- memset(depth, 0, length * sizeof(depth[0]));
402
- BrotliCreateHuffmanTree(histogram, length, 15, tree, depth);
403
- BrotliConvertBitDepthsToSymbols(depth, length, bits);
402
+ memset(depth, 0, histogram_length * sizeof(depth[0]));
403
+ BrotliCreateHuffmanTree(histogram, histogram_length, 15, tree, depth);
404
+ BrotliConvertBitDepthsToSymbols(depth, histogram_length, bits);
404
405
 
405
406
  if (count <= 4) {
406
407
  StoreSimpleHuffmanTree(depth, s4, count, max_bits, storage_ix, storage);
407
408
  } else {
408
- BrotliStoreHuffmanTree(depth, length, tree, storage_ix, storage);
409
+ BrotliStoreHuffmanTree(depth, histogram_length, tree, storage_ix, storage);
409
410
  }
410
411
  }
411
412
 
@@ -619,7 +620,7 @@ static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in,
619
620
  for (i = 1; i < v_size; ++i) {
620
621
  if (v_in[i] > max_value) max_value = v_in[i];
621
622
  }
622
- assert(max_value < 256u);
623
+ BROTLI_DCHECK(max_value < 256u);
623
624
  for (i = 0; i <= max_value; ++i) {
624
625
  mtf[i] = (uint8_t)i;
625
626
  }
@@ -627,7 +628,7 @@ static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in,
627
628
  size_t mtf_size = max_value + 1;
628
629
  for (i = 0; i < v_size; ++i) {
629
630
  size_t index = IndexOf(mtf, mtf_size, (uint8_t)v_in[i]);
630
- assert(index < mtf_size);
631
+ BROTLI_DCHECK(index < mtf_size);
631
632
  v_out[i] = (uint32_t)index;
632
633
  MoveToFront(mtf, index);
633
634
  }
@@ -659,7 +660,7 @@ static void RunLengthCodeZeros(const size_t in_size,
659
660
  *max_run_length_prefix = max_prefix;
660
661
  *out_size = 0;
661
662
  for (i = 0; i < in_size;) {
662
- assert(*out_size <= i);
663
+ BROTLI_DCHECK(*out_size <= i);
663
664
  if (v[i] != 0) {
664
665
  v[*out_size] = v[i] + *max_run_length_prefix;
665
666
  ++i;
@@ -729,6 +730,7 @@ static void EncodeContextMap(MemoryManager* m,
729
730
  }
730
731
  }
731
732
  BuildAndStoreHuffmanTree(histogram, num_clusters + max_run_length_prefix,
733
+ num_clusters + max_run_length_prefix,
732
734
  tree, depths, bits, storage_ix, storage);
733
735
  for (i = 0; i < num_rle_symbols; ++i) {
734
736
  const uint32_t rle_symbol = rle_symbols[i] & kSymbolMask;
@@ -788,10 +790,11 @@ static void BuildAndStoreBlockSplitCode(const uint8_t* types,
788
790
  }
789
791
  StoreVarLenUint8(num_types - 1, storage_ix, storage);
790
792
  if (num_types > 1) { /* TODO: else? could StoreBlockSwitch occur? */
791
- BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, tree,
793
+ BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, num_types + 2, tree,
792
794
  &code->type_depths[0], &code->type_bits[0],
793
795
  storage_ix, storage);
794
796
  BuildAndStoreHuffmanTree(&length_histo[0], BROTLI_NUM_BLOCK_LEN_SYMBOLS,
797
+ BROTLI_NUM_BLOCK_LEN_SYMBOLS,
795
798
  tree, &code->length_depths[0],
796
799
  &code->length_bits[0], storage_ix, storage);
797
800
  StoreBlockSwitch(code, lengths[0], types[0], 1, storage_ix, storage);
@@ -822,8 +825,8 @@ static void StoreTrivialContextMap(size_t num_types,
822
825
  for (i = context_bits; i < alphabet_size; ++i) {
823
826
  histogram[i] = 1;
824
827
  }
825
- BuildAndStoreHuffmanTree(histogram, alphabet_size, tree,
826
- depths, bits, storage_ix, storage);
828
+ BuildAndStoreHuffmanTree(histogram, alphabet_size, alphabet_size,
829
+ tree, depths, bits, storage_ix, storage);
827
830
  for (i = 0; i < num_types; ++i) {
828
831
  size_t code = (i == 0 ? 0 : i + context_bits - 1);
829
832
  BrotliWriteBits(depths[code], bits[code], storage_ix, storage);
@@ -838,7 +841,7 @@ static void StoreTrivialContextMap(size_t num_types,
838
841
 
839
842
  /* Manages the encoding of one block category (literal, command or distance). */
840
843
  typedef struct BlockEncoder {
841
- size_t alphabet_size_;
844
+ size_t histogram_length_;
842
845
  size_t num_block_types_;
843
846
  const uint8_t* block_types_; /* Not owned. */
844
847
  const uint32_t* block_lengths_; /* Not owned. */
@@ -851,10 +854,10 @@ typedef struct BlockEncoder {
851
854
  uint16_t* bits_;
852
855
  } BlockEncoder;
853
856
 
854
- static void InitBlockEncoder(BlockEncoder* self, size_t alphabet_size,
857
+ static void InitBlockEncoder(BlockEncoder* self, size_t histogram_length,
855
858
  size_t num_block_types, const uint8_t* block_types,
856
859
  const uint32_t* block_lengths, const size_t num_blocks) {
857
- self->alphabet_size_ = alphabet_size;
860
+ self->histogram_length_ = histogram_length;
858
861
  self->num_block_types_ = num_block_types;
859
862
  self->block_types_ = block_types;
860
863
  self->block_lengths_ = block_lengths;
@@ -890,7 +893,7 @@ static void StoreSymbol(BlockEncoder* self, size_t symbol, size_t* storage_ix,
890
893
  uint32_t block_len = self->block_lengths_[block_ix];
891
894
  uint8_t block_type = self->block_types_[block_ix];
892
895
  self->block_len_ = block_len;
893
- self->entropy_ix_ = block_type * self->alphabet_size_;
896
+ self->entropy_ix_ = block_type * self->histogram_length_;
894
897
  StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0,
895
898
  storage_ix, storage);
896
899
  }
@@ -919,7 +922,7 @@ static void StoreSymbolWithContext(BlockEncoder* self, size_t symbol,
919
922
  --self->block_len_;
920
923
  {
921
924
  size_t histo_ix = context_map[self->entropy_ix_ + context];
922
- size_t ix = histo_ix * self->alphabet_size_ + symbol;
925
+ size_t ix = histo_ix * self->histogram_length_ + symbol;
923
926
  BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage);
924
927
  }
925
928
  }
@@ -945,42 +948,38 @@ static void JumpToByteBoundary(size_t* storage_ix, uint8_t* storage) {
945
948
  }
946
949
 
947
950
  void BrotliStoreMetaBlock(MemoryManager* m,
948
- const uint8_t* input,
949
- size_t start_pos,
950
- size_t length,
951
- size_t mask,
952
- uint8_t prev_byte,
953
- uint8_t prev_byte2,
954
- BROTLI_BOOL is_last,
955
- uint32_t num_direct_distance_codes,
956
- uint32_t distance_postfix_bits,
957
- ContextType literal_context_mode,
958
- const Command *commands,
959
- size_t n_commands,
960
- const MetaBlockSplit* mb,
961
- size_t *storage_ix,
962
- uint8_t *storage) {
951
+ const uint8_t* input, size_t start_pos, size_t length, size_t mask,
952
+ uint8_t prev_byte, uint8_t prev_byte2, BROTLI_BOOL is_last,
953
+ const BrotliEncoderParams* params, ContextType literal_context_mode,
954
+ const Command* commands, size_t n_commands, const MetaBlockSplit* mb,
955
+ size_t* storage_ix, uint8_t* storage) {
956
+
963
957
  size_t pos = start_pos;
964
958
  size_t i;
965
- size_t num_distance_codes =
966
- BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_distance_codes +
967
- (48u << distance_postfix_bits);
959
+ uint32_t num_distance_symbols = params->dist.alphabet_size;
960
+ uint32_t num_effective_distance_symbols = num_distance_symbols;
968
961
  HuffmanTree* tree;
962
+ ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode);
969
963
  BlockEncoder literal_enc;
970
964
  BlockEncoder command_enc;
971
965
  BlockEncoder distance_enc;
966
+ const BrotliDistanceParams* dist = &params->dist;
967
+ if (params->large_window &&
968
+ num_effective_distance_symbols > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS) {
969
+ num_effective_distance_symbols = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS;
970
+ }
972
971
 
973
972
  StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
974
973
 
975
974
  tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE);
976
975
  if (BROTLI_IS_OOM(m)) return;
977
- InitBlockEncoder(&literal_enc, 256, mb->literal_split.num_types,
978
- mb->literal_split.types, mb->literal_split.lengths,
979
- mb->literal_split.num_blocks);
976
+ InitBlockEncoder(&literal_enc, BROTLI_NUM_LITERAL_SYMBOLS,
977
+ mb->literal_split.num_types, mb->literal_split.types,
978
+ mb->literal_split.lengths, mb->literal_split.num_blocks);
980
979
  InitBlockEncoder(&command_enc, BROTLI_NUM_COMMAND_SYMBOLS,
981
980
  mb->command_split.num_types, mb->command_split.types,
982
981
  mb->command_split.lengths, mb->command_split.num_blocks);
983
- InitBlockEncoder(&distance_enc, num_distance_codes,
982
+ InitBlockEncoder(&distance_enc, num_effective_distance_symbols,
984
983
  mb->distance_split.num_types, mb->distance_split.types,
985
984
  mb->distance_split.lengths, mb->distance_split.num_blocks);
986
985
 
@@ -989,9 +988,10 @@ void BrotliStoreMetaBlock(MemoryManager* m,
989
988
  BuildAndStoreBlockSwitchEntropyCodes(
990
989
  &distance_enc, tree, storage_ix, storage);
991
990
 
992
- BrotliWriteBits(2, distance_postfix_bits, storage_ix, storage);
993
- BrotliWriteBits(4, num_direct_distance_codes >> distance_postfix_bits,
994
- storage_ix, storage);
991
+ BrotliWriteBits(2, dist->distance_postfix_bits, storage_ix, storage);
992
+ BrotliWriteBits(
993
+ 4, dist->num_direct_distance_codes >> dist->distance_postfix_bits,
994
+ storage_ix, storage);
995
995
  for (i = 0; i < mb->literal_split.num_types; ++i) {
996
996
  BrotliWriteBits(2, literal_context_mode, storage_ix, storage);
997
997
  }
@@ -1017,13 +1017,16 @@ void BrotliStoreMetaBlock(MemoryManager* m,
1017
1017
  }
1018
1018
 
1019
1019
  BuildAndStoreEntropyCodesLiteral(m, &literal_enc, mb->literal_histograms,
1020
- mb->literal_histograms_size, tree, storage_ix, storage);
1020
+ mb->literal_histograms_size, BROTLI_NUM_LITERAL_SYMBOLS, tree,
1021
+ storage_ix, storage);
1021
1022
  if (BROTLI_IS_OOM(m)) return;
1022
1023
  BuildAndStoreEntropyCodesCommand(m, &command_enc, mb->command_histograms,
1023
- mb->command_histograms_size, tree, storage_ix, storage);
1024
+ mb->command_histograms_size, BROTLI_NUM_COMMAND_SYMBOLS, tree,
1025
+ storage_ix, storage);
1024
1026
  if (BROTLI_IS_OOM(m)) return;
1025
1027
  BuildAndStoreEntropyCodesDistance(m, &distance_enc, mb->distance_histograms,
1026
- mb->distance_histograms_size, tree, storage_ix, storage);
1028
+ mb->distance_histograms_size, num_distance_symbols, tree,
1029
+ storage_ix, storage);
1027
1030
  if (BROTLI_IS_OOM(m)) return;
1028
1031
  BROTLI_FREE(m, tree);
1029
1032
 
@@ -1041,7 +1044,8 @@ void BrotliStoreMetaBlock(MemoryManager* m,
1041
1044
  } else {
1042
1045
  size_t j;
1043
1046
  for (j = cmd.insert_len_; j != 0; --j) {
1044
- size_t context = Context(prev_byte, prev_byte2, literal_context_mode);
1047
+ size_t context =
1048
+ BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut);
1045
1049
  uint8_t literal = input[pos & mask];
1046
1050
  StoreSymbolWithContext(&literal_enc, literal, context,
1047
1051
  mb->literal_context_map, storage_ix, storage,
@@ -1056,9 +1060,9 @@ void BrotliStoreMetaBlock(MemoryManager* m,
1056
1060
  prev_byte2 = input[(pos - 2) & mask];
1057
1061
  prev_byte = input[(pos - 1) & mask];
1058
1062
  if (cmd.cmd_prefix_ >= 128) {
1059
- size_t dist_code = cmd.dist_prefix_;
1060
- uint32_t distnumextra = cmd.dist_extra_ >> 24;
1061
- uint64_t distextra = cmd.dist_extra_ & 0xffffff;
1063
+ size_t dist_code = cmd.dist_prefix_ & 0x3FF;
1064
+ uint32_t distnumextra = cmd.dist_prefix_ >> 10;
1065
+ uint64_t distextra = cmd.dist_extra_;
1062
1066
  if (mb->distance_context_map_size == 0) {
1063
1067
  StoreSymbol(&distance_enc, dist_code, storage_ix, storage);
1064
1068
  } else {
@@ -1082,7 +1086,7 @@ void BrotliStoreMetaBlock(MemoryManager* m,
1082
1086
  static void BuildHistograms(const uint8_t* input,
1083
1087
  size_t start_pos,
1084
1088
  size_t mask,
1085
- const Command *commands,
1089
+ const Command* commands,
1086
1090
  size_t n_commands,
1087
1091
  HistogramLiteral* lit_histo,
1088
1092
  HistogramCommand* cmd_histo,
@@ -1099,7 +1103,7 @@ static void BuildHistograms(const uint8_t* input,
1099
1103
  }
1100
1104
  pos += CommandCopyLen(&cmd);
1101
1105
  if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
1102
- HistogramAddDistance(dist_histo, cmd.dist_prefix_);
1106
+ HistogramAddDistance(dist_histo, cmd.dist_prefix_ & 0x3FF);
1103
1107
  }
1104
1108
  }
1105
1109
  }
@@ -1107,7 +1111,7 @@ static void BuildHistograms(const uint8_t* input,
1107
1111
  static void StoreDataWithHuffmanCodes(const uint8_t* input,
1108
1112
  size_t start_pos,
1109
1113
  size_t mask,
1110
- const Command *commands,
1114
+ const Command* commands,
1111
1115
  size_t n_commands,
1112
1116
  const uint8_t* lit_depth,
1113
1117
  const uint16_t* lit_bits,
@@ -1134,9 +1138,9 @@ static void StoreDataWithHuffmanCodes(const uint8_t* input,
1134
1138
  }
1135
1139
  pos += CommandCopyLen(&cmd);
1136
1140
  if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
1137
- const size_t dist_code = cmd.dist_prefix_;
1138
- const uint32_t distnumextra = cmd.dist_extra_ >> 24;
1139
- const uint32_t distextra = cmd.dist_extra_ & 0xffffff;
1141
+ const size_t dist_code = cmd.dist_prefix_ & 0x3FF;
1142
+ const uint32_t distnumextra = cmd.dist_prefix_ >> 10;
1143
+ const uint32_t distextra = cmd.dist_extra_;
1140
1144
  BrotliWriteBits(dist_depth[dist_code], dist_bits[dist_code],
1141
1145
  storage_ix, storage);
1142
1146
  BrotliWriteBits(distnumextra, distextra, storage_ix, storage);
@@ -1145,15 +1149,10 @@ static void StoreDataWithHuffmanCodes(const uint8_t* input,
1145
1149
  }
1146
1150
 
1147
1151
  void BrotliStoreMetaBlockTrivial(MemoryManager* m,
1148
- const uint8_t* input,
1149
- size_t start_pos,
1150
- size_t length,
1151
- size_t mask,
1152
- BROTLI_BOOL is_last,
1153
- const Command *commands,
1154
- size_t n_commands,
1155
- size_t *storage_ix,
1156
- uint8_t *storage) {
1152
+ const uint8_t* input, size_t start_pos, size_t length, size_t mask,
1153
+ BROTLI_BOOL is_last, const BrotliEncoderParams* params,
1154
+ const Command* commands, size_t n_commands,
1155
+ size_t* storage_ix, uint8_t* storage) {
1157
1156
  HistogramLiteral lit_histo;
1158
1157
  HistogramCommand cmd_histo;
1159
1158
  HistogramDistance dist_histo;
@@ -1161,9 +1160,10 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m,
1161
1160
  uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
1162
1161
  uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS];
1163
1162
  uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS];
1164
- uint8_t dist_depth[SIMPLE_DISTANCE_ALPHABET_SIZE];
1165
- uint16_t dist_bits[SIMPLE_DISTANCE_ALPHABET_SIZE];
1163
+ uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
1164
+ uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
1166
1165
  HuffmanTree* tree;
1166
+ uint32_t num_distance_symbols = params->dist.alphabet_size;
1167
1167
 
1168
1168
  StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
1169
1169
 
@@ -1178,14 +1178,16 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m,
1178
1178
 
1179
1179
  tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE);
1180
1180
  if (BROTLI_IS_OOM(m)) return;
1181
- BuildAndStoreHuffmanTree(lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS, tree,
1181
+ BuildAndStoreHuffmanTree(lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS,
1182
+ BROTLI_NUM_LITERAL_SYMBOLS, tree,
1182
1183
  lit_depth, lit_bits,
1183
1184
  storage_ix, storage);
1184
- BuildAndStoreHuffmanTree(cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS, tree,
1185
+ BuildAndStoreHuffmanTree(cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS,
1186
+ BROTLI_NUM_COMMAND_SYMBOLS, tree,
1185
1187
  cmd_depth, cmd_bits,
1186
1188
  storage_ix, storage);
1187
- BuildAndStoreHuffmanTree(dist_histo.data_, SIMPLE_DISTANCE_ALPHABET_SIZE,
1188
- tree,
1189
+ BuildAndStoreHuffmanTree(dist_histo.data_, MAX_SIMPLE_DISTANCE_ALPHABET_SIZE,
1190
+ num_distance_symbols, tree,
1189
1191
  dist_depth, dist_bits,
1190
1192
  storage_ix, storage);
1191
1193
  BROTLI_FREE(m, tree);
@@ -1200,15 +1202,14 @@ void BrotliStoreMetaBlockTrivial(MemoryManager* m,
1200
1202
  }
1201
1203
 
1202
1204
  void BrotliStoreMetaBlockFast(MemoryManager* m,
1203
- const uint8_t* input,
1204
- size_t start_pos,
1205
- size_t length,
1206
- size_t mask,
1207
- BROTLI_BOOL is_last,
1208
- const Command *commands,
1209
- size_t n_commands,
1210
- size_t *storage_ix,
1211
- uint8_t *storage) {
1205
+ const uint8_t* input, size_t start_pos, size_t length, size_t mask,
1206
+ BROTLI_BOOL is_last, const BrotliEncoderParams* params,
1207
+ const Command* commands, size_t n_commands,
1208
+ size_t* storage_ix, uint8_t* storage) {
1209
+ uint32_t num_distance_symbols = params->dist.alphabet_size;
1210
+ uint32_t distance_alphabet_bits =
1211
+ Log2FloorNonZero(num_distance_symbols - 1) + 1;
1212
+
1212
1213
  StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
1213
1214
 
1214
1215
  BrotliWriteBits(13, 0, storage_ix, storage);
@@ -1252,8 +1253,8 @@ void BrotliStoreMetaBlockFast(MemoryManager* m,
1252
1253
  uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
1253
1254
  uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS];
1254
1255
  uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS];
1255
- uint8_t dist_depth[SIMPLE_DISTANCE_ALPHABET_SIZE];
1256
- uint16_t dist_bits[SIMPLE_DISTANCE_ALPHABET_SIZE];
1256
+ uint8_t dist_depth[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
1257
+ uint16_t dist_bits[MAX_SIMPLE_DISTANCE_ALPHABET_SIZE];
1257
1258
  HistogramClearLiteral(&lit_histo);
1258
1259
  HistogramClearCommand(&cmd_histo);
1259
1260
  HistogramClearDistance(&dist_histo);
@@ -1274,7 +1275,7 @@ void BrotliStoreMetaBlockFast(MemoryManager* m,
1274
1275
  BrotliBuildAndStoreHuffmanTreeFast(m, dist_histo.data_,
1275
1276
  dist_histo.total_count_,
1276
1277
  /* max_bits = */
1277
- SIMPLE_DISTANCE_ALPHABET_BITS,
1278
+ distance_alphabet_bits,
1278
1279
  dist_depth, dist_bits,
1279
1280
  storage_ix, storage);
1280
1281
  if (BROTLI_IS_OOM(m)) return;
@@ -1293,11 +1294,11 @@ void BrotliStoreMetaBlockFast(MemoryManager* m,
1293
1294
  /* This is for storing uncompressed blocks (simple raw storage of
1294
1295
  bytes-as-bytes). */
1295
1296
  void BrotliStoreUncompressedMetaBlock(BROTLI_BOOL is_final_block,
1296
- const uint8_t * BROTLI_RESTRICT input,
1297
+ const uint8_t* BROTLI_RESTRICT input,
1297
1298
  size_t position, size_t mask,
1298
1299
  size_t len,
1299
- size_t * BROTLI_RESTRICT storage_ix,
1300
- uint8_t * BROTLI_RESTRICT storage) {
1300
+ size_t* BROTLI_RESTRICT storage_ix,
1301
+ uint8_t* BROTLI_RESTRICT storage) {
1301
1302
  size_t masked_pos = position & mask;
1302
1303
  BrotliStoreUncompressedMetaBlockHeader(len, storage_ix, storage);
1303
1304
  JumpToByteBoundary(storage_ix, storage);