brotli 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +11 -3
  4. data/Gemfile +2 -0
  5. data/ext/brotli/brotli.c +279 -0
  6. data/ext/brotli/brotli.h +2 -0
  7. data/ext/brotli/buffer.c +95 -0
  8. data/ext/brotli/buffer.h +19 -0
  9. data/ext/brotli/extconf.rb +21 -81
  10. data/lib/brotli/version.rb +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +5 -5
  12. data/vendor/brotli/dec/bit_reader.h +15 -15
  13. data/vendor/brotli/dec/context.h +1 -1
  14. data/vendor/brotli/dec/decode.c +433 -348
  15. data/vendor/brotli/dec/decode.h +74 -48
  16. data/vendor/brotli/dec/huffman.c +5 -4
  17. data/vendor/brotli/dec/huffman.h +4 -4
  18. data/vendor/brotli/dec/port.h +2 -95
  19. data/vendor/brotli/dec/prefix.h +5 -3
  20. data/vendor/brotli/dec/state.c +15 -27
  21. data/vendor/brotli/dec/state.h +21 -17
  22. data/vendor/brotli/dec/transform.h +1 -1
  23. data/vendor/brotli/enc/backward_references.c +892 -0
  24. data/vendor/brotli/enc/backward_references.h +85 -102
  25. data/vendor/brotli/enc/backward_references_inc.h +147 -0
  26. data/vendor/brotli/enc/bit_cost.c +35 -0
  27. data/vendor/brotli/enc/bit_cost.h +23 -121
  28. data/vendor/brotli/enc/bit_cost_inc.h +127 -0
  29. data/vendor/brotli/enc/block_encoder_inc.h +33 -0
  30. data/vendor/brotli/enc/block_splitter.c +197 -0
  31. data/vendor/brotli/enc/block_splitter.h +40 -50
  32. data/vendor/brotli/enc/block_splitter_inc.h +432 -0
  33. data/vendor/brotli/enc/brotli_bit_stream.c +1334 -0
  34. data/vendor/brotli/enc/brotli_bit_stream.h +95 -167
  35. data/vendor/brotli/enc/cluster.c +56 -0
  36. data/vendor/brotli/enc/cluster.h +23 -305
  37. data/vendor/brotli/enc/cluster_inc.h +315 -0
  38. data/vendor/brotli/enc/command.h +83 -76
  39. data/vendor/brotli/enc/compress_fragment.c +747 -0
  40. data/vendor/brotli/enc/compress_fragment.h +48 -37
  41. data/vendor/brotli/enc/compress_fragment_two_pass.c +557 -0
  42. data/vendor/brotli/enc/compress_fragment_two_pass.h +37 -26
  43. data/vendor/brotli/enc/compressor.cc +139 -0
  44. data/vendor/brotli/enc/compressor.h +146 -0
  45. data/vendor/brotli/enc/context.h +102 -96
  46. data/vendor/brotli/enc/dictionary_hash.h +9 -5
  47. data/vendor/brotli/enc/encode.c +1562 -0
  48. data/vendor/brotli/enc/encode.h +211 -199
  49. data/vendor/brotli/enc/encode_parallel.cc +161 -151
  50. data/vendor/brotli/enc/encode_parallel.h +7 -8
  51. data/vendor/brotli/enc/entropy_encode.c +501 -0
  52. data/vendor/brotli/enc/entropy_encode.h +107 -89
  53. data/vendor/brotli/enc/entropy_encode_static.h +29 -62
  54. data/vendor/brotli/enc/fast_log.h +26 -20
  55. data/vendor/brotli/enc/find_match_length.h +23 -20
  56. data/vendor/brotli/enc/hash.h +614 -871
  57. data/vendor/brotli/enc/hash_forgetful_chain_inc.h +249 -0
  58. data/vendor/brotli/enc/hash_longest_match_inc.h +241 -0
  59. data/vendor/brotli/enc/hash_longest_match_quickly_inc.h +230 -0
  60. data/vendor/brotli/enc/histogram.c +95 -0
  61. data/vendor/brotli/enc/histogram.h +49 -83
  62. data/vendor/brotli/enc/histogram_inc.h +51 -0
  63. data/vendor/brotli/enc/literal_cost.c +178 -0
  64. data/vendor/brotli/enc/literal_cost.h +16 -10
  65. data/vendor/brotli/enc/memory.c +181 -0
  66. data/vendor/brotli/enc/memory.h +62 -0
  67. data/vendor/brotli/enc/metablock.c +515 -0
  68. data/vendor/brotli/enc/metablock.h +87 -57
  69. data/vendor/brotli/enc/metablock_inc.h +183 -0
  70. data/vendor/brotli/enc/port.h +73 -47
  71. data/vendor/brotli/enc/prefix.h +34 -61
  72. data/vendor/brotli/enc/quality.h +130 -0
  73. data/vendor/brotli/enc/ringbuffer.h +137 -122
  74. data/vendor/brotli/enc/{static_dict.cc → static_dict.c} +162 -139
  75. data/vendor/brotli/enc/static_dict.h +23 -18
  76. data/vendor/brotli/enc/static_dict_lut.h +11223 -12037
  77. data/vendor/brotli/enc/streams.cc +7 -7
  78. data/vendor/brotli/enc/streams.h +32 -32
  79. data/vendor/brotli/enc/{utf8_util.cc → utf8_util.c} +22 -20
  80. data/vendor/brotli/enc/utf8_util.h +16 -9
  81. data/vendor/brotli/enc/write_bits.h +49 -43
  82. metadata +34 -25
  83. data/ext/brotli/brotli.cc +0 -181
  84. data/vendor/brotli/dec/Makefile +0 -12
  85. data/vendor/brotli/dec/dictionary.c +0 -9466
  86. data/vendor/brotli/dec/dictionary.h +0 -38
  87. data/vendor/brotli/dec/types.h +0 -38
  88. data/vendor/brotli/enc/Makefile +0 -14
  89. data/vendor/brotli/enc/backward_references.cc +0 -858
  90. data/vendor/brotli/enc/block_splitter.cc +0 -505
  91. data/vendor/brotli/enc/brotli_bit_stream.cc +0 -1181
  92. data/vendor/brotli/enc/compress_fragment.cc +0 -701
  93. data/vendor/brotli/enc/compress_fragment_two_pass.cc +0 -524
  94. data/vendor/brotli/enc/dictionary.cc +0 -9466
  95. data/vendor/brotli/enc/dictionary.h +0 -41
  96. data/vendor/brotli/enc/encode.cc +0 -1180
  97. data/vendor/brotli/enc/entropy_encode.cc +0 -480
  98. data/vendor/brotli/enc/histogram.cc +0 -67
  99. data/vendor/brotli/enc/literal_cost.cc +0 -165
  100. data/vendor/brotli/enc/metablock.cc +0 -539
  101. data/vendor/brotli/enc/transform.h +0 -248
  102. data/vendor/brotli/enc/types.h +0 -29
@@ -0,0 +1,249 @@
1
+ /* NOLINT(build/header_guard) */
2
+ /* Copyright 2016 Google Inc. All Rights Reserved.
3
+
4
+ Distributed under MIT license.
5
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6
+ */
7
+
8
+ /* template parameters: FN, BUCKET_BITS, NUM_BANKS, BANK_BITS,
9
+ NUM_LAST_DISTANCES_TO_CHECK */
10
+
11
+ /* A (forgetful) hash table to the data seen by the compressor, to
12
+ help create backward references to previous data.
13
+
14
+ Hashes are stored in chains which are bucketed to groups. Group of chains
15
+ share a storage "bank". When more than "bank size" chain nodes are added,
16
+ oldest nodes are replaced; this way several chains may share a tail. */
17
+
18
+ #define HashForgetfulChain HASHER()
19
+
20
+ #define BANK_SIZE (1 << BANK_BITS)
21
+
22
+ /* Number of hash buckets. */
23
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
24
+
25
+ #define CAPPED_CHAINS 0
26
+
27
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
28
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
29
+
30
+ /* HashBytes is the function that chooses the bucket to place the address in.*/
31
+ static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t *data) {
32
+ const uint32_t h = BROTLI_UNALIGNED_LOAD32(data) * kHashMul32;
33
+ /* The higher bits contain more mixture from the multiplication,
34
+ so we take our results from there. */
35
+ return h >> (32 - BUCKET_BITS);
36
+ }
37
+
38
+ typedef struct FN(Slot) {
39
+ uint16_t delta;
40
+ uint16_t next;
41
+ } FN(Slot);
42
+
43
+ typedef struct FN(Bank) {
44
+ FN(Slot) slots[BANK_SIZE];
45
+ } FN(Bank);
46
+
47
+ typedef struct HashForgetfulChain {
48
+ uint32_t addr[BUCKET_SIZE];
49
+ uint16_t head[BUCKET_SIZE];
50
+ /* Truncated hash used for quick rejection of "distance cache" candidates. */
51
+ uint8_t tiny_hash[65536];
52
+ FN(Bank) banks[NUM_BANKS];
53
+ uint16_t free_slot_idx[NUM_BANKS];
54
+ BROTLI_BOOL is_dirty_;
55
+ DictionarySearchStatictics dict_search_stats_;
56
+ size_t max_hops;
57
+ } HashForgetfulChain;
58
+
59
+ static void FN(Reset)(HashForgetfulChain* self) {
60
+ self->is_dirty_ = BROTLI_TRUE;
61
+ DictionarySearchStaticticsReset(&self->dict_search_stats_);
62
+ }
63
+
64
+ static void FN(InitEmpty)(HashForgetfulChain* self) {
65
+ if (self->is_dirty_) {
66
+ /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
67
+ processed by hasher never reaches 3GB + 64M; this makes all new chains
68
+ to be terminated after the first node. */
69
+ memset(self->addr, 0xCC, sizeof(self->addr));
70
+ memset(self->head, 0, sizeof(self->head));
71
+ memset(self->tiny_hash, 0, sizeof(self->tiny_hash));
72
+ memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
73
+ self->is_dirty_ = BROTLI_FALSE;
74
+ }
75
+ }
76
+
77
+ static void FN(InitForData)(HashForgetfulChain* self, const uint8_t* data,
78
+ size_t num) {
79
+ size_t i;
80
+ for (i = 0; i < num; ++i) {
81
+ size_t bucket = FN(HashBytes)(&data[i]);
82
+ /* See InitEmpty comment. */
83
+ self->addr[bucket] = 0xCCCCCCCC;
84
+ self->head[bucket] = 0xCCCC;
85
+ }
86
+ memset(self->tiny_hash, 0, sizeof(self->tiny_hash));
87
+ memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
88
+ if (num != 0) {
89
+ self->is_dirty_ = BROTLI_FALSE;
90
+ }
91
+ }
92
+
93
+ static void FN(Init)(
94
+ MemoryManager* m, HashForgetfulChain* self, const uint8_t* data,
95
+ const BrotliEncoderParams* params, size_t position, size_t bytes,
96
+ BROTLI_BOOL is_last) {
97
+ /* Choose which init method is faster.
98
+ Init() is about 100 times faster than InitForData(). */
99
+ const size_t kMaxBytesForPartialHashInit = BUCKET_SIZE >> 6;
100
+ BROTLI_UNUSED(m);
101
+ self->max_hops = (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
102
+ if (position == 0 && is_last && bytes <= kMaxBytesForPartialHashInit) {
103
+ FN(InitForData)(self, data, bytes);
104
+ } else {
105
+ FN(InitEmpty)(self);
106
+ }
107
+ }
108
+
109
+ /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
110
+ node to corresponding chain; also update tiny_hash for current position. */
111
+ static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
112
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
113
+ const size_t key = FN(HashBytes)(&data[ix & mask]);
114
+ const size_t bank = key & (NUM_BANKS - 1);
115
+ const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
116
+ size_t delta = ix - self->addr[key];
117
+ self->tiny_hash[(uint16_t)ix] = (uint8_t)key;
118
+ if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;
119
+ self->banks[bank].slots[idx].delta = (uint16_t)delta;
120
+ self->banks[bank].slots[idx].next = self->head[key];
121
+ self->addr[key] = (uint32_t)ix;
122
+ self->head[key] = (uint16_t)idx;
123
+ }
124
+
125
+ static BROTLI_INLINE void FN(StoreRange)(HashForgetfulChain* self,
126
+ const uint8_t *data, const size_t mask, const size_t ix_start,
127
+ const size_t ix_end) {
128
+ size_t i;
129
+ for (i = ix_start; i < ix_end; ++i) {
130
+ FN(Store)(self, data, mask, i);
131
+ }
132
+ }
133
+
134
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(HashForgetfulChain* self,
135
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
136
+ size_t ring_buffer_mask) {
137
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
138
+ /* Prepare the hashes for three last bytes of the last write.
139
+ These could not be calculated before, since they require knowledge
140
+ of both the previous and the current block. */
141
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 3);
142
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 2);
143
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 1);
144
+ }
145
+ }
146
+
147
+ /* Find a longest backward match of &data[cur_ix] up to the length of
148
+ max_length and stores the position cur_ix in the hash table.
149
+
150
+ Does not look for matches longer than max_length.
151
+ Does not look for matches further away than max_backward.
152
+ Writes the best match into |out|.
153
+ Returns 1 when match is found, otherwise 0. */
154
+ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
155
+ HashForgetfulChain* self, const uint8_t* BROTLI_RESTRICT data,
156
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
157
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
158
+ HasherSearchResult* BROTLI_RESTRICT out) {
159
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
160
+ BROTLI_BOOL is_match_found = BROTLI_FALSE;
161
+ /* Don't accept a short copy from far away. */
162
+ score_t best_score = out->score;
163
+ size_t best_len = out->len;
164
+ size_t i;
165
+ const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
166
+ const uint8_t tiny_hash = (uint8_t)(key);
167
+ out->len = 0;
168
+ out->len_x_code = 0;
169
+ /* Try last distance first. */
170
+ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
171
+ const size_t idx = kDistanceCacheIndex[i];
172
+ const size_t backward =
173
+ (size_t)(distance_cache[idx] + kDistanceCacheOffset[i]);
174
+ size_t prev_ix = (cur_ix - backward);
175
+ if (i > 0 && self->tiny_hash[(uint16_t)prev_ix] != tiny_hash) continue;
176
+ if (prev_ix >= cur_ix || backward > max_backward) {
177
+ continue;
178
+ }
179
+ prev_ix &= ring_buffer_mask;
180
+ {
181
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
182
+ &data[cur_ix_masked],
183
+ max_length);
184
+ if (len >= 2) {
185
+ score_t score = BackwardReferenceScoreUsingLastDistance(len, i);
186
+ if (best_score < score) {
187
+ best_score = score;
188
+ best_len = len;
189
+ out->len = best_len;
190
+ out->distance = backward;
191
+ out->score = best_score;
192
+ is_match_found = BROTLI_TRUE;
193
+ }
194
+ }
195
+ }
196
+ }
197
+ {
198
+ const size_t bank = key & (NUM_BANKS - 1);
199
+ size_t backward = 0;
200
+ size_t hops = self->max_hops;
201
+ size_t delta = cur_ix - self->addr[key];
202
+ size_t slot = self->head[key];
203
+ while (hops--) {
204
+ size_t prev_ix;
205
+ size_t last = slot;
206
+ backward += delta;
207
+ if (backward > max_backward || (CAPPED_CHAINS && !delta)) break;
208
+ prev_ix = (cur_ix - backward) & ring_buffer_mask;
209
+ slot = self->banks[bank].slots[last].next;
210
+ delta = self->banks[bank].slots[last].delta;
211
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
212
+ prev_ix + best_len > ring_buffer_mask ||
213
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
214
+ continue;
215
+ }
216
+ {
217
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
218
+ &data[cur_ix_masked],
219
+ max_length);
220
+ if (len >= 4) {
221
+ /* Comparing for >= 3 does not change the semantics, but just saves
222
+ for a few unnecessary binary logarithms in backward reference
223
+ score, since we are not interested in such short matches. */
224
+ score_t score = BackwardReferenceScore(len, backward);
225
+ if (best_score < score) {
226
+ best_score = score;
227
+ best_len = len;
228
+ out->len = best_len;
229
+ out->distance = backward;
230
+ out->score = best_score;
231
+ is_match_found = BROTLI_TRUE;
232
+ }
233
+ }
234
+ }
235
+ }
236
+ FN(Store)(self, data, ring_buffer_mask, cur_ix);
237
+ }
238
+ if (!is_match_found) {
239
+ is_match_found = SearchInStaticDictionary(&self->dict_search_stats_,
240
+ &data[cur_ix_masked], max_length, max_backward, out, BROTLI_FALSE);
241
+ }
242
+ return is_match_found;
243
+ }
244
+
245
+ #undef BANK_SIZE
246
+ #undef BUCKET_SIZE
247
+ #undef CAPPED_CHAINS
248
+
249
+ #undef HashForgetfulChain
@@ -0,0 +1,241 @@
1
+ /* NOLINT(build/header_guard) */
2
+ /* Copyright 2010 Google Inc. All Rights Reserved.
3
+
4
+ Distributed under MIT license.
5
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6
+ */
7
+
8
+ /* template parameters: FN, BUCKET_BITS, BLOCK_BITS,
9
+ NUM_LAST_DISTANCES_TO_CHECK */
10
+
11
+ /* A (forgetful) hash table to the data seen by the compressor, to
12
+ help create backward references to previous data.
13
+
14
+ This is a hash map of fixed size (BUCKET_SIZE) to a ring buffer of
15
+ fixed size (BLOCK_SIZE). The ring buffer contains the last BLOCK_SIZE
16
+ index positions of the given hash key in the compressed data. */
17
+
18
+ #define HashLongestMatch HASHER()
19
+
20
+ /* Number of hash buckets. */
21
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
22
+
23
+ /* Only BLOCK_SIZE newest backward references are kept,
24
+ and the older are forgotten. */
25
+ #define BLOCK_SIZE (1u << BLOCK_BITS)
26
+
27
+ /* Mask for accessing entries in a block (in a ringbuffer manner). */
28
+ #define BLOCK_MASK ((1 << BLOCK_BITS) - 1)
29
+
30
+ #define HASH_MAP_SIZE (2 << BUCKET_BITS)
31
+
32
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
33
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
34
+
35
+ /* HashBytes is the function that chooses the bucket to place
36
+ the address in. The HashLongestMatch and HashLongestMatchQuickly
37
+ classes have separate, different implementations of hashing. */
38
+ static uint32_t FN(HashBytes)(const uint8_t *data) {
39
+ uint32_t h = BROTLI_UNALIGNED_LOAD32(data) * kHashMul32;
40
+ /* The higher bits contain more mixture from the multiplication,
41
+ so we take our results from there. */
42
+ return h >> (32 - BUCKET_BITS);
43
+ }
44
+
45
+ typedef struct HashLongestMatch {
46
+ /* Number of entries in a particular bucket. */
47
+ uint16_t num_[BUCKET_SIZE];
48
+
49
+ /* Buckets containing BLOCK_SIZE of backward references. */
50
+ uint32_t buckets_[BLOCK_SIZE << BUCKET_BITS];
51
+
52
+ /* True if num_ array needs to be initialized. */
53
+ BROTLI_BOOL is_dirty_;
54
+
55
+ DictionarySearchStatictics dict_search_stats_;
56
+ } HashLongestMatch;
57
+
58
+ static void FN(Reset)(HashLongestMatch* self) {
59
+ self->is_dirty_ = BROTLI_TRUE;
60
+ DictionarySearchStaticticsReset(&self->dict_search_stats_);
61
+ }
62
+
63
+ static void FN(InitEmpty)(HashLongestMatch* self) {
64
+ if (self->is_dirty_) {
65
+ memset(self->num_, 0, sizeof(self->num_));
66
+ self->is_dirty_ = BROTLI_FALSE;
67
+ }
68
+ }
69
+
70
+ static void FN(InitForData)(HashLongestMatch* self, const uint8_t* data,
71
+ size_t num) {
72
+ size_t i;
73
+ for (i = 0; i < num; ++i) {
74
+ const uint32_t key = FN(HashBytes)(&data[i]);
75
+ self->num_[key] = 0;
76
+ }
77
+ if (num != 0) {
78
+ self->is_dirty_ = BROTLI_FALSE;
79
+ }
80
+ }
81
+
82
+ static void FN(Init)(
83
+ MemoryManager* m, HashLongestMatch* self, const uint8_t* data,
84
+ const BrotliEncoderParams* params, size_t position, size_t bytes,
85
+ BROTLI_BOOL is_last) {
86
+ /* Choose which init method is faster.
87
+ Init() is about 100 times faster than InitForData(). */
88
+ const size_t kMaxBytesForPartialHashInit = HASH_MAP_SIZE >> 7;
89
+ BROTLI_UNUSED(m);
90
+ BROTLI_UNUSED(params);
91
+ if (position == 0 && is_last && bytes <= kMaxBytesForPartialHashInit) {
92
+ FN(InitForData)(self, data, bytes);
93
+ } else {
94
+ FN(InitEmpty)(self);
95
+ }
96
+ }
97
+
98
+ /* Look at 4 bytes at &data[ix & mask].
99
+ Compute a hash from these, and store the value of ix at that position. */
100
+ static BROTLI_INLINE void FN(Store)(HashLongestMatch* self, const uint8_t *data,
101
+ const size_t mask, const size_t ix) {
102
+ const uint32_t key = FN(HashBytes)(&data[ix & mask]);
103
+ const size_t minor_ix = self->num_[key] & BLOCK_MASK;
104
+ self->buckets_[minor_ix + (key << BLOCK_BITS)] = (uint32_t)ix;
105
+ ++self->num_[key];
106
+ }
107
+
108
+ static BROTLI_INLINE void FN(StoreRange)(HashLongestMatch* self,
109
+ const uint8_t *data, const size_t mask, const size_t ix_start,
110
+ const size_t ix_end) {
111
+ size_t i;
112
+ for (i = ix_start; i < ix_end; ++i) {
113
+ FN(Store)(self, data, mask, i);
114
+ }
115
+ }
116
+
117
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(HashLongestMatch* self,
118
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
119
+ size_t ringbuffer_mask) {
120
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
121
+ /* Prepare the hashes for three last bytes of the last write.
122
+ These could not be calculated before, since they require knowledge
123
+ of both the previous and the current block. */
124
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
125
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
126
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
127
+ }
128
+ }
129
+
130
+ /* Find a longest backward match of &data[cur_ix] up to the length of
131
+ max_length and stores the position cur_ix in the hash table.
132
+
133
+ Does not look for matches longer than max_length.
134
+ Does not look for matches further away than max_backward.
135
+ Writes the best match into |out|.
136
+ Returns true when match is found, otherwise false. */
137
+ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(HashLongestMatch* self,
138
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
139
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
140
+ const size_t max_length, const size_t max_backward,
141
+ HasherSearchResult* BROTLI_RESTRICT out) {
142
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
143
+ BROTLI_BOOL is_match_found = BROTLI_FALSE;
144
+ /* Don't accept a short copy from far away. */
145
+ score_t best_score = out->score;
146
+ size_t best_len = out->len;
147
+ size_t i;
148
+ out->len = 0;
149
+ out->len_x_code = 0;
150
+ /* Try last distance first. */
151
+ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
152
+ const size_t idx = kDistanceCacheIndex[i];
153
+ const size_t backward =
154
+ (size_t)(distance_cache[idx] + kDistanceCacheOffset[i]);
155
+ size_t prev_ix = (size_t)(cur_ix - backward);
156
+ if (prev_ix >= cur_ix) {
157
+ continue;
158
+ }
159
+ if (PREDICT_FALSE(backward > max_backward)) {
160
+ continue;
161
+ }
162
+ prev_ix &= ring_buffer_mask;
163
+
164
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
165
+ prev_ix + best_len > ring_buffer_mask ||
166
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
167
+ continue;
168
+ }
169
+ {
170
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
171
+ &data[cur_ix_masked],
172
+ max_length);
173
+ if (len >= 3 || (len == 2 && i < 2)) {
174
+ /* Comparing for >= 2 does not change the semantics, but just saves for
175
+ a few unnecessary binary logarithms in backward reference score,
176
+ since we are not interested in such short matches. */
177
+ score_t score = BackwardReferenceScoreUsingLastDistance(len, i);
178
+ if (best_score < score) {
179
+ best_score = score;
180
+ best_len = len;
181
+ out->len = best_len;
182
+ out->distance = backward;
183
+ out->score = best_score;
184
+ is_match_found = BROTLI_TRUE;
185
+ }
186
+ }
187
+ }
188
+ }
189
+ {
190
+ const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);
191
+ uint32_t* BROTLI_RESTRICT bucket = &self->buckets_[key << BLOCK_BITS];
192
+ const size_t down =
193
+ (self->num_[key] > BLOCK_SIZE) ? (self->num_[key] - BLOCK_SIZE) : 0u;
194
+ for (i = self->num_[key]; i > down;) {
195
+ size_t prev_ix = bucket[--i & BLOCK_MASK];
196
+ const size_t backward = cur_ix - prev_ix;
197
+ if (PREDICT_FALSE(backward > max_backward)) {
198
+ break;
199
+ }
200
+ prev_ix &= ring_buffer_mask;
201
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
202
+ prev_ix + best_len > ring_buffer_mask ||
203
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
204
+ continue;
205
+ }
206
+ {
207
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
208
+ &data[cur_ix_masked],
209
+ max_length);
210
+ if (len >= 4) {
211
+ /* Comparing for >= 3 does not change the semantics, but just saves
212
+ for a few unnecessary binary logarithms in backward reference
213
+ score, since we are not interested in such short matches. */
214
+ score_t score = BackwardReferenceScore(len, backward);
215
+ if (best_score < score) {
216
+ best_score = score;
217
+ best_len = len;
218
+ out->len = best_len;
219
+ out->distance = backward;
220
+ out->score = best_score;
221
+ is_match_found = BROTLI_TRUE;
222
+ }
223
+ }
224
+ }
225
+ }
226
+ bucket[self->num_[key] & BLOCK_MASK] = (uint32_t)cur_ix;
227
+ ++self->num_[key];
228
+ }
229
+ if (!is_match_found) {
230
+ is_match_found = SearchInStaticDictionary(&self->dict_search_stats_,
231
+ &data[cur_ix_masked], max_length, max_backward, out, BROTLI_FALSE);
232
+ }
233
+ return is_match_found;
234
+ }
235
+
236
+ #undef HASH_MAP_SIZE
237
+ #undef BLOCK_MASK
238
+ #undef BLOCK_SIZE
239
+ #undef BUCKET_SIZE
240
+
241
+ #undef HashLongestMatch