brotli 0.2.0 → 0.4.0

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 (111) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +34 -0
  3. data/.github/workflows/publish.yml +34 -0
  4. data/Gemfile +6 -2
  5. data/Rakefile +18 -6
  6. data/bin/before_install.sh +9 -0
  7. data/brotli.gemspec +7 -13
  8. data/ext/brotli/brotli.c +209 -11
  9. data/ext/brotli/buffer.c +1 -7
  10. data/ext/brotli/buffer.h +1 -1
  11. data/ext/brotli/extconf.rb +45 -26
  12. data/lib/brotli/version.rb +1 -1
  13. data/smoke.sh +1 -1
  14. data/test/brotli_test.rb +104 -0
  15. data/test/brotli_writer_test.rb +36 -0
  16. data/test/test_helper.rb +8 -0
  17. data/vendor/brotli/c/common/constants.c +15 -0
  18. data/vendor/brotli/c/common/constants.h +149 -6
  19. data/vendor/brotli/c/{dec/context.h → common/context.c} +91 -186
  20. data/vendor/brotli/c/common/context.h +113 -0
  21. data/vendor/brotli/c/common/dictionary.bin +0 -0
  22. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
  23. data/vendor/brotli/c/common/dictionary.c +11 -2
  24. data/vendor/brotli/c/common/dictionary.h +4 -4
  25. data/vendor/brotli/c/common/platform.c +22 -0
  26. data/vendor/brotli/c/common/platform.h +594 -0
  27. data/vendor/brotli/c/common/transform.c +291 -0
  28. data/vendor/brotli/c/common/transform.h +85 -0
  29. data/vendor/brotli/c/common/version.h +8 -1
  30. data/vendor/brotli/c/dec/bit_reader.c +29 -1
  31. data/vendor/brotli/c/dec/bit_reader.h +91 -100
  32. data/vendor/brotli/c/dec/decode.c +665 -437
  33. data/vendor/brotli/c/dec/huffman.c +65 -84
  34. data/vendor/brotli/c/dec/huffman.h +67 -14
  35. data/vendor/brotli/c/dec/prefix.h +1 -20
  36. data/vendor/brotli/c/dec/state.c +32 -45
  37. data/vendor/brotli/c/dec/state.h +173 -55
  38. data/vendor/brotli/c/enc/backward_references.c +27 -16
  39. data/vendor/brotli/c/enc/backward_references.h +7 -7
  40. data/vendor/brotli/c/enc/backward_references_hq.c +155 -116
  41. data/vendor/brotli/c/enc/backward_references_hq.h +22 -23
  42. data/vendor/brotli/c/enc/backward_references_inc.h +32 -22
  43. data/vendor/brotli/c/enc/bit_cost.c +1 -1
  44. data/vendor/brotli/c/enc/bit_cost.h +5 -5
  45. data/vendor/brotli/c/enc/block_encoder_inc.h +7 -6
  46. data/vendor/brotli/c/enc/block_splitter.c +5 -6
  47. data/vendor/brotli/c/enc/block_splitter.h +1 -1
  48. data/vendor/brotli/c/enc/block_splitter_inc.h +26 -17
  49. data/vendor/brotli/c/enc/brotli_bit_stream.c +107 -123
  50. data/vendor/brotli/c/enc/brotli_bit_stream.h +19 -38
  51. data/vendor/brotli/c/enc/cluster.c +1 -1
  52. data/vendor/brotli/c/enc/cluster.h +1 -1
  53. data/vendor/brotli/c/enc/cluster_inc.h +6 -3
  54. data/vendor/brotli/c/enc/command.c +28 -0
  55. data/vendor/brotli/c/enc/command.h +52 -42
  56. data/vendor/brotli/c/enc/compress_fragment.c +21 -22
  57. data/vendor/brotli/c/enc/compress_fragment.h +1 -1
  58. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +102 -69
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +1 -1
  60. data/vendor/brotli/c/enc/dictionary_hash.c +1827 -1101
  61. data/vendor/brotli/c/enc/dictionary_hash.h +2 -1
  62. data/vendor/brotli/c/enc/encode.c +358 -195
  63. data/vendor/brotli/c/enc/encoder_dict.c +33 -0
  64. data/vendor/brotli/c/enc/encoder_dict.h +43 -0
  65. data/vendor/brotli/c/enc/entropy_encode.c +16 -14
  66. data/vendor/brotli/c/enc/entropy_encode.h +7 -7
  67. data/vendor/brotli/c/enc/entropy_encode_static.h +3 -3
  68. data/vendor/brotli/c/enc/fast_log.c +105 -0
  69. data/vendor/brotli/c/enc/fast_log.h +20 -99
  70. data/vendor/brotli/c/enc/find_match_length.h +5 -6
  71. data/vendor/brotli/c/enc/hash.h +145 -103
  72. data/vendor/brotli/c/enc/hash_composite_inc.h +125 -0
  73. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +93 -53
  74. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +54 -53
  75. data/vendor/brotli/c/enc/hash_longest_match_inc.h +58 -54
  76. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +95 -63
  77. data/vendor/brotli/c/enc/hash_rolling_inc.h +212 -0
  78. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +46 -43
  79. data/vendor/brotli/c/enc/histogram.c +9 -6
  80. data/vendor/brotli/c/enc/histogram.h +6 -3
  81. data/vendor/brotli/c/enc/histogram_inc.h +1 -1
  82. data/vendor/brotli/c/enc/literal_cost.c +5 -5
  83. data/vendor/brotli/c/enc/literal_cost.h +2 -2
  84. data/vendor/brotli/c/enc/memory.c +5 -16
  85. data/vendor/brotli/c/enc/memory.h +52 -1
  86. data/vendor/brotli/c/enc/metablock.c +171 -36
  87. data/vendor/brotli/c/enc/metablock.h +13 -8
  88. data/vendor/brotli/c/enc/metablock_inc.h +2 -2
  89. data/vendor/brotli/c/enc/params.h +46 -0
  90. data/vendor/brotli/c/enc/prefix.h +3 -4
  91. data/vendor/brotli/c/enc/quality.h +29 -24
  92. data/vendor/brotli/c/enc/ringbuffer.h +19 -12
  93. data/vendor/brotli/c/enc/static_dict.c +49 -45
  94. data/vendor/brotli/c/enc/static_dict.h +4 -3
  95. data/vendor/brotli/c/enc/static_dict_lut.h +1 -1
  96. data/vendor/brotli/c/enc/utf8_util.c +21 -21
  97. data/vendor/brotli/c/enc/utf8_util.h +1 -1
  98. data/vendor/brotli/c/enc/write_bits.h +35 -38
  99. data/vendor/brotli/c/include/brotli/decode.h +13 -8
  100. data/vendor/brotli/c/include/brotli/encode.h +54 -8
  101. data/vendor/brotli/c/include/brotli/port.h +225 -83
  102. data/vendor/brotli/c/include/brotli/types.h +0 -7
  103. metadata +28 -87
  104. data/.travis.yml +0 -30
  105. data/spec/brotli_spec.rb +0 -88
  106. data/spec/inflate_spec.rb +0 -75
  107. data/spec/spec_helper.rb +0 -4
  108. data/vendor/brotli/c/dec/port.h +0 -168
  109. data/vendor/brotli/c/dec/transform.h +0 -300
  110. data/vendor/brotli/c/enc/context.h +0 -184
  111. data/vendor/brotli/c/enc/port.h +0 -184
@@ -10,42 +10,42 @@
10
10
  #define BROTLI_ENC_BACKWARD_REFERENCES_HQ_H_
11
11
 
12
12
  #include "../common/constants.h"
13
+ #include "../common/context.h"
13
14
  #include "../common/dictionary.h"
15
+ #include "../common/platform.h"
14
16
  #include <brotli/types.h>
15
17
  #include "./command.h"
16
18
  #include "./hash.h"
17
19
  #include "./memory.h"
18
- #include "./port.h"
19
20
  #include "./quality.h"
20
21
 
21
22
  #if defined(__cplusplus) || defined(c_plusplus)
22
23
  extern "C" {
23
24
  #endif
24
25
 
25
- BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(
26
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
26
+ BROTLI_INTERNAL void BrotliCreateZopfliBackwardReferences(MemoryManager* m,
27
+ size_t num_bytes,
27
28
  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);
29
+ ContextLut literal_context_lut, const BrotliEncoderParams* params,
30
+ Hasher* hasher, int* dist_cache, size_t* last_insert_len,
31
+ Command* commands, size_t* num_commands, size_t* num_literals);
31
32
 
32
- BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(
33
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
33
+ BROTLI_INTERNAL void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m,
34
+ size_t num_bytes,
34
35
  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);
36
+ ContextLut literal_context_lut, const BrotliEncoderParams* params,
37
+ Hasher* hasher, int* dist_cache, size_t* last_insert_len,
38
+ Command* commands, size_t* num_commands, size_t* num_literals);
38
39
 
39
40
  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 */
41
+ /* Best length to get up to this byte (not including this byte itself)
42
+ highest 7 bit is used to reconstruct the length code. */
42
43
  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
- */
44
+ /* Distance associated with the length. */
46
45
  uint32_t distance;
47
- /* number of literal inserts before this copy */
48
- uint32_t insert_length;
46
+ /* Number of literal inserts before this copy; highest 5 bits contain
47
+ distance short code + 1 (or zero if no short code). */
48
+ uint32_t dcode_insert_length;
49
49
 
50
50
  /* This union holds information used by dynamic-programming. During forward
51
51
  pass |cost| it used to store the goal function. When node is processed its
@@ -78,14 +78,13 @@ BROTLI_INTERNAL void BrotliInitZopfliNodes(ZopfliNode* array, size_t length);
78
78
  (2) nodes[i].command_length() <= i and
79
79
  (3) nodes[i - nodes[i].command_length()].cost < kInfinity */
80
80
  BROTLI_INTERNAL size_t BrotliZopfliComputeShortestPath(
81
- MemoryManager* m, const BrotliDictionary* dictionary, size_t num_bytes,
81
+ MemoryManager* m, size_t num_bytes,
82
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);
83
+ ContextLut literal_context_lut, const BrotliEncoderParams* params,
84
+ const int* dist_cache, Hasher* hasher, ZopfliNode* nodes);
85
85
 
86
86
  BROTLI_INTERNAL void BrotliZopfliCreateCommands(
87
- const size_t num_bytes, const size_t block_start,
88
- const size_t max_backward_limit, const ZopfliNode* nodes,
87
+ const size_t num_bytes, const size_t block_start, const ZopfliNode* nodes,
89
88
  int* dist_cache, size_t* last_insert_len, const BrotliEncoderParams* params,
90
89
  Command* commands, size_t* num_literals);
91
90
 
@@ -8,14 +8,15 @@
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
- const BrotliEncoderParams* params, HasherHandle hasher, int* dist_cache,
15
- size_t* last_insert_len, Command* commands, size_t* num_commands,
16
- size_t* num_literals) {
13
+ ContextLut literal_context_lut, const BrotliEncoderParams* params,
14
+ Hasher* hasher, int* dist_cache, size_t* last_insert_len,
15
+ Command* commands, size_t* num_commands, size_t* num_literals) {
16
+ HASHER()* privat = &hasher->privat.FN(_);
17
17
  /* Set maximum distance, see section 9.1. of the spec. */
18
18
  const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
19
+ const size_t position_offset = params->stream_offset;
19
20
 
20
21
  const Command* const orig_commands = commands;
21
22
  size_t insert_length = *last_insert_len;
@@ -32,19 +33,23 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
32
33
  /* Minimum score to accept a backward reference. */
33
34
  const score_t kMinScore = BROTLI_SCORE_BASE + 100;
34
35
 
35
- FN(PrepareDistanceCache)(hasher, dist_cache);
36
+ BROTLI_UNUSED(literal_context_lut);
37
+
38
+ FN(PrepareDistanceCache)(privat, dist_cache);
36
39
 
37
40
  while (position + FN(HashTypeLength)() < pos_end) {
38
41
  size_t max_length = pos_end - position;
39
42
  size_t max_distance = BROTLI_MIN(size_t, position, max_backward_limit);
43
+ size_t dictionary_start = BROTLI_MIN(size_t,
44
+ position + position_offset, max_backward_limit);
40
45
  HasherSearchResult sr;
41
46
  sr.len = 0;
42
47
  sr.len_code_delta = 0;
43
48
  sr.distance = 0;
44
49
  sr.score = kMinScore;
45
- FN(FindLongestMatch)(hasher, dictionary, dictionary_hash, ringbuffer,
46
- ringbuffer_mask, dist_cache, position,
47
- max_length, max_distance, gap, &sr);
50
+ FN(FindLongestMatch)(privat, &params->dictionary,
51
+ ringbuffer, ringbuffer_mask, dist_cache, position, max_length,
52
+ max_distance, dictionary_start + gap, params->dist.max_distance, &sr);
48
53
  if (sr.score > kMinScore) {
49
54
  /* Found a match. Let's look for something even better ahead. */
50
55
  int delayed_backward_references_in_row = 0;
@@ -58,9 +63,13 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
58
63
  sr2.distance = 0;
59
64
  sr2.score = kMinScore;
60
65
  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);
66
+ dictionary_start = BROTLI_MIN(size_t,
67
+ position + 1 + position_offset, max_backward_limit);
68
+ FN(FindLongestMatch)(privat,
69
+ &params->dictionary,
70
+ ringbuffer, ringbuffer_mask, dist_cache, position + 1, max_length,
71
+ max_distance, dictionary_start + gap, params->dist.max_distance,
72
+ &sr2);
64
73
  if (sr2.score >= sr.score + cost_diff_lazy) {
65
74
  /* Ok, let's just write one byte for now and start a match from the
66
75
  next byte. */
@@ -76,21 +85,22 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
76
85
  }
77
86
  apply_random_heuristics =
78
87
  position + 2 * sr.len + random_heuristics_window_size;
79
- max_distance = BROTLI_MIN(size_t, position, max_backward_limit);
88
+ dictionary_start = BROTLI_MIN(size_t,
89
+ position + position_offset, max_backward_limit);
80
90
  {
81
91
  /* The first 16 codes are special short-codes,
82
92
  and the minimum offset is 1. */
83
- size_t distance_code =
84
- ComputeDistanceCode(sr.distance, max_distance + gap, dist_cache);
85
- if ((sr.distance <= (max_distance + gap)) && distance_code > 0) {
93
+ size_t distance_code = ComputeDistanceCode(
94
+ sr.distance, dictionary_start + gap, dist_cache);
95
+ if ((sr.distance <= (dictionary_start + gap)) && distance_code > 0) {
86
96
  dist_cache[3] = dist_cache[2];
87
97
  dist_cache[2] = dist_cache[1];
88
98
  dist_cache[1] = dist_cache[0];
89
99
  dist_cache[0] = (int)sr.distance;
90
- FN(PrepareDistanceCache)(hasher, dist_cache);
100
+ FN(PrepareDistanceCache)(privat, dist_cache);
91
101
  }
92
- InitCommand(commands++, insert_length, sr.len, sr.len_code_delta,
93
- distance_code);
102
+ InitCommand(commands++, &params->dist, insert_length,
103
+ sr.len, sr.len_code_delta, distance_code);
94
104
  }
95
105
  *num_literals += insert_length;
96
106
  insert_length = 0;
@@ -105,7 +115,7 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
105
115
  range_start = BROTLI_MIN(size_t, range_end, BROTLI_MAX(size_t,
106
116
  range_start, position + sr.len - (sr.distance << 2)));
107
117
  }
108
- FN(StoreRange)(hasher, ringbuffer, ringbuffer_mask, range_start,
118
+ FN(StoreRange)(privat, ringbuffer, ringbuffer_mask, range_start,
109
119
  range_end);
110
120
  }
111
121
  position += sr.len;
@@ -131,7 +141,7 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
131
141
  size_t pos_jump =
132
142
  BROTLI_MIN(size_t, position + 16, pos_end - kMargin);
133
143
  for (; position < pos_jump; position += 4) {
134
- FN(Store)(hasher, ringbuffer, ringbuffer_mask, position);
144
+ FN(Store)(privat, ringbuffer, ringbuffer_mask, position);
135
145
  insert_length += 4;
136
146
  }
137
147
  } else {
@@ -140,7 +150,7 @@ static BROTLI_NOINLINE void EXPORT_FN(CreateBackwardReferences)(
140
150
  size_t pos_jump =
141
151
  BROTLI_MIN(size_t, position + 8, pos_end - kMargin);
142
152
  for (; position < pos_jump; position += 2) {
143
- FN(Store)(hasher, ringbuffer, ringbuffer_mask, position);
153
+ FN(Store)(privat, ringbuffer, ringbuffer_mask, position);
144
154
  insert_length += 2;
145
155
  }
146
156
  }
@@ -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)
@@ -133,7 +132,7 @@ void BrotliSplitBlock(MemoryManager* m,
133
132
  {
134
133
  size_t literals_count = CountLiterals(cmds, num_commands);
135
134
  uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count);
136
- if (BROTLI_IS_OOM(m)) return;
135
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(literals)) return;
137
136
  /* Create a continuous array of literals. */
138
137
  CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals);
139
138
  /* Create the block split on the array of literals.
@@ -151,7 +150,7 @@ void BrotliSplitBlock(MemoryManager* m,
151
150
  /* Compute prefix codes for commands. */
152
151
  uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands);
153
152
  size_t i;
154
- if (BROTLI_IS_OOM(m)) return;
153
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(insert_and_copy_codes)) return;
155
154
  for (i = 0; i < num_commands; ++i) {
156
155
  insert_and_copy_codes[i] = cmds[i].cmd_prefix_;
157
156
  }
@@ -171,11 +170,11 @@ void BrotliSplitBlock(MemoryManager* m,
171
170
  uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands);
172
171
  size_t j = 0;
173
172
  size_t i;
174
- if (BROTLI_IS_OOM(m)) return;
173
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(distance_prefixes)) return;
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
 
@@ -219,20 +219,25 @@ static void FN(ClusterBlocks)(MemoryManager* m,
219
219
  uint32_t symbols[HISTOGRAMS_PER_BATCH] = { 0 };
220
220
  uint32_t remap[HISTOGRAMS_PER_BATCH] = { 0 };
221
221
 
222
- if (BROTLI_IS_OOM(m)) return;
222
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(histogram_symbols) ||
223
+ BROTLI_IS_NULL(block_lengths) || BROTLI_IS_NULL(all_histograms) ||
224
+ BROTLI_IS_NULL(cluster_size) || BROTLI_IS_NULL(histograms) ||
225
+ BROTLI_IS_NULL(pairs)) {
226
+ return;
227
+ }
223
228
 
224
229
  memset(block_lengths, 0, num_blocks * sizeof(uint32_t));
225
230
 
226
231
  {
227
232
  size_t block_idx = 0;
228
233
  for (i = 0; i < length; ++i) {
229
- assert(block_idx < num_blocks);
234
+ BROTLI_DCHECK(block_idx < num_blocks);
230
235
  ++block_lengths[block_idx];
231
236
  if (i + 1 == length || block_ids[i] != block_ids[i + 1]) {
232
237
  ++block_idx;
233
238
  }
234
239
  }
235
- assert(block_idx == num_blocks);
240
+ BROTLI_DCHECK(block_idx == num_blocks);
236
241
  }
237
242
 
238
243
  for (i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH) {
@@ -268,8 +273,8 @@ static void FN(ClusterBlocks)(MemoryManager* m,
268
273
  histogram_symbols[i + j] = (uint32_t)num_clusters + remap[symbols[j]];
269
274
  }
270
275
  num_clusters += num_new_clusters;
271
- assert(num_clusters == cluster_size_size);
272
- assert(num_clusters == all_histograms_size);
276
+ BROTLI_DCHECK(num_clusters == cluster_size_size);
277
+ BROTLI_DCHECK(num_clusters == all_histograms_size);
273
278
  }
274
279
  BROTLI_FREE(m, histograms);
275
280
 
@@ -278,11 +283,11 @@ static void FN(ClusterBlocks)(MemoryManager* m,
278
283
  if (pairs_capacity < max_num_pairs + 1) {
279
284
  BROTLI_FREE(m, pairs);
280
285
  pairs = BROTLI_ALLOC(m, HistogramPair, max_num_pairs + 1);
281
- if (BROTLI_IS_OOM(m)) return;
286
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(pairs)) return;
282
287
  }
283
288
 
284
289
  clusters = BROTLI_ALLOC(m, uint32_t, num_clusters);
285
- if (BROTLI_IS_OOM(m)) return;
290
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(clusters)) return;
286
291
  for (i = 0; i < num_clusters; ++i) {
287
292
  clusters[i] = (uint32_t)i;
288
293
  }
@@ -294,7 +299,7 @@ static void FN(ClusterBlocks)(MemoryManager* m,
294
299
  BROTLI_FREE(m, cluster_size);
295
300
 
296
301
  new_index = BROTLI_ALLOC(m, uint32_t, num_clusters);
297
- if (BROTLI_IS_OOM(m)) return;
302
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(new_index)) return;
298
303
  for (i = 0; i < num_clusters; ++i) new_index[i] = kInvalidIndex;
299
304
  pos = 0;
300
305
  {
@@ -386,7 +391,7 @@ static void FN(SplitByteVector)(MemoryManager* m,
386
391
  return;
387
392
  }
388
393
  histograms = BROTLI_ALLOC(m, HistogramType, num_histograms);
389
- if (BROTLI_IS_OOM(m)) return;
394
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(histograms)) return;
390
395
  /* Find good entropy codes. */
391
396
  FN(InitialEntropyCodes)(data, length,
392
397
  sampling_stride_length,
@@ -405,7 +410,11 @@ static void FN(SplitByteVector)(MemoryManager* m,
405
410
  uint16_t* new_id = BROTLI_ALLOC(m, uint16_t, num_histograms);
406
411
  const size_t iters = params->quality < HQ_ZOPFLIFICATION_QUALITY ? 3 : 10;
407
412
  size_t i;
408
- if (BROTLI_IS_OOM(m)) return;
413
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(block_ids) ||
414
+ BROTLI_IS_NULL(insert_cost) || BROTLI_IS_NULL(cost) ||
415
+ BROTLI_IS_NULL(switch_signal) || BROTLI_IS_NULL(new_id)) {
416
+ return;
417
+ }
409
418
  for (i = 0; i < iters; ++i) {
410
419
  num_blocks = FN(FindBlocks)(data, length,
411
420
  block_switch_cost,