brotli 0.1.8 → 0.2.0

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 +1 -0
  3. data/.travis.yml +7 -3
  4. data/brotli.gemspec +1 -1
  5. data/ext/brotli/brotli.c +4 -4
  6. data/ext/brotli/brotli.h +2 -2
  7. data/ext/brotli/extconf.rb +9 -16
  8. data/lib/brotli/version.rb +1 -1
  9. data/vendor/brotli/{common → c/common}/constants.h +11 -1
  10. data/vendor/brotli/c/common/dictionary.bin +432 -0
  11. data/vendor/brotli/c/common/dictionary.c +5905 -0
  12. data/vendor/brotli/c/common/dictionary.h +64 -0
  13. data/vendor/brotli/c/common/version.h +19 -0
  14. data/vendor/brotli/{dec → c/dec}/bit_reader.c +2 -2
  15. data/vendor/brotli/{dec → c/dec}/bit_reader.h +11 -34
  16. data/vendor/brotli/{dec → c/dec}/context.h +1 -1
  17. data/vendor/brotli/{dec → c/dec}/decode.c +389 -356
  18. data/vendor/brotli/{dec → c/dec}/huffman.c +24 -23
  19. data/vendor/brotli/{dec → c/dec}/huffman.h +1 -1
  20. data/vendor/brotli/{dec → c/dec}/port.h +19 -10
  21. data/vendor/brotli/{dec → c/dec}/prefix.h +1 -1
  22. data/vendor/brotli/{dec → c/dec}/state.c +23 -19
  23. data/vendor/brotli/{dec → c/dec}/state.h +18 -17
  24. data/vendor/brotli/{dec → c/dec}/transform.h +2 -2
  25. data/vendor/brotli/c/enc/backward_references.c +134 -0
  26. data/vendor/brotli/c/enc/backward_references.h +39 -0
  27. data/vendor/brotli/{enc/backward_references.c → c/enc/backward_references_hq.c} +144 -232
  28. data/vendor/brotli/{enc/backward_references.h → c/enc/backward_references_hq.h} +28 -31
  29. data/vendor/brotli/{enc → c/enc}/backward_references_inc.h +37 -31
  30. data/vendor/brotli/{enc → c/enc}/bit_cost.c +1 -1
  31. data/vendor/brotli/{enc → c/enc}/bit_cost.h +1 -1
  32. data/vendor/brotli/{enc → c/enc}/bit_cost_inc.h +0 -0
  33. data/vendor/brotli/{enc → c/enc}/block_encoder_inc.h +0 -0
  34. data/vendor/brotli/{enc → c/enc}/block_splitter.c +2 -4
  35. data/vendor/brotli/{enc → c/enc}/block_splitter.h +1 -1
  36. data/vendor/brotli/{enc → c/enc}/block_splitter_inc.h +6 -7
  37. data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.c +22 -26
  38. data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.h +1 -5
  39. data/vendor/brotli/{enc → c/enc}/cluster.c +1 -1
  40. data/vendor/brotli/{enc → c/enc}/cluster.h +1 -1
  41. data/vendor/brotli/{enc → c/enc}/cluster_inc.h +2 -0
  42. data/vendor/brotli/{enc → c/enc}/command.h +34 -17
  43. data/vendor/brotli/{enc → c/enc}/compress_fragment.c +97 -53
  44. data/vendor/brotli/{enc → c/enc}/compress_fragment.h +5 -2
  45. data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.c +106 -51
  46. data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.h +5 -2
  47. data/vendor/brotli/{enc → c/enc}/context.h +3 -3
  48. data/vendor/brotli/c/enc/dictionary_hash.c +1120 -0
  49. data/vendor/brotli/c/enc/dictionary_hash.h +24 -0
  50. data/vendor/brotli/{enc → c/enc}/encode.c +442 -240
  51. data/vendor/brotli/{enc → c/enc}/entropy_encode.c +9 -9
  52. data/vendor/brotli/{enc → c/enc}/entropy_encode.h +4 -4
  53. data/vendor/brotli/{enc → c/enc}/entropy_encode_static.h +4 -4
  54. data/vendor/brotli/{enc → c/enc}/fast_log.h +3 -3
  55. data/vendor/brotli/{enc → c/enc}/find_match_length.h +8 -8
  56. data/vendor/brotli/c/enc/hash.h +446 -0
  57. data/vendor/brotli/{enc → c/enc}/hash_forgetful_chain_inc.h +72 -68
  58. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +266 -0
  59. data/vendor/brotli/c/enc/hash_longest_match_inc.h +258 -0
  60. data/vendor/brotli/{enc → c/enc}/hash_longest_match_quickly_inc.h +81 -77
  61. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +326 -0
  62. data/vendor/brotli/{enc → c/enc}/histogram.c +4 -2
  63. data/vendor/brotli/{enc → c/enc}/histogram.h +1 -1
  64. data/vendor/brotli/{enc → c/enc}/histogram_inc.h +0 -0
  65. data/vendor/brotli/{enc → c/enc}/literal_cost.c +4 -7
  66. data/vendor/brotli/{enc → c/enc}/literal_cost.h +2 -2
  67. data/vendor/brotli/{enc → c/enc}/memory.c +1 -1
  68. data/vendor/brotli/{enc → c/enc}/memory.h +3 -2
  69. data/vendor/brotli/{enc → c/enc}/metablock.c +136 -123
  70. data/vendor/brotli/{enc → c/enc}/metablock.h +2 -12
  71. data/vendor/brotli/{enc → c/enc}/metablock_inc.h +0 -0
  72. data/vendor/brotli/{enc → c/enc}/port.h +49 -33
  73. data/vendor/brotli/{enc → c/enc}/prefix.h +4 -2
  74. data/vendor/brotli/{enc → c/enc}/quality.h +47 -17
  75. data/vendor/brotli/{enc → c/enc}/ringbuffer.h +6 -6
  76. data/vendor/brotli/{enc → c/enc}/static_dict.c +26 -22
  77. data/vendor/brotli/{enc → c/enc}/static_dict.h +3 -1
  78. data/vendor/brotli/c/enc/static_dict_lut.h +5864 -0
  79. data/vendor/brotli/{enc → c/enc}/utf8_util.c +1 -1
  80. data/vendor/brotli/{enc → c/enc}/utf8_util.h +2 -2
  81. data/vendor/brotli/{enc → c/enc}/write_bits.h +3 -3
  82. data/vendor/brotli/c/include/brotli/decode.h +339 -0
  83. data/vendor/brotli/c/include/brotli/encode.h +402 -0
  84. data/vendor/brotli/c/include/brotli/port.h +146 -0
  85. data/vendor/brotli/c/include/brotli/types.h +90 -0
  86. metadata +80 -79
  87. data/vendor/brotli/common/dictionary.c +0 -9474
  88. data/vendor/brotli/common/dictionary.h +0 -29
  89. data/vendor/brotli/common/port.h +0 -107
  90. data/vendor/brotli/common/types.h +0 -58
  91. data/vendor/brotli/dec/decode.h +0 -188
  92. data/vendor/brotli/enc/compressor.cc +0 -139
  93. data/vendor/brotli/enc/compressor.h +0 -161
  94. data/vendor/brotli/enc/dictionary_hash.h +0 -4121
  95. data/vendor/brotli/enc/encode.h +0 -221
  96. data/vendor/brotli/enc/encode_parallel.cc +0 -289
  97. data/vendor/brotli/enc/encode_parallel.h +0 -27
  98. data/vendor/brotli/enc/hash.h +0 -717
  99. data/vendor/brotli/enc/hash_longest_match_inc.h +0 -241
  100. data/vendor/brotli/enc/static_dict_lut.h +0 -11241
  101. data/vendor/brotli/enc/streams.cc +0 -114
  102. data/vendor/brotli/enc/streams.h +0 -121
@@ -51,65 +51,57 @@ typedef struct HashForgetfulChain {
51
51
  uint8_t tiny_hash[65536];
52
52
  FN(Bank) banks[NUM_BANKS];
53
53
  uint16_t free_slot_idx[NUM_BANKS];
54
- BROTLI_BOOL is_dirty_;
55
- DictionarySearchStatictics dict_search_stats_;
56
54
  size_t max_hops;
57
55
  } HashForgetfulChain;
58
56
 
59
- static void FN(Reset)(HashForgetfulChain* self) {
60
- self->is_dirty_ = BROTLI_TRUE;
61
- DictionarySearchStaticticsReset(&self->dict_search_stats_);
57
+ static BROTLI_INLINE HashForgetfulChain* FN(Self)(HasherHandle handle) {
58
+ return (HashForgetfulChain*)&(GetHasherCommon(handle)[1]);
62
59
  }
63
60
 
64
- static void FN(InitEmpty)(HashForgetfulChain* self) {
65
- if (self->is_dirty_) {
61
+ static void FN(Initialize)(
62
+ HasherHandle handle, const BrotliEncoderParams* params) {
63
+ FN(Self)(handle)->max_hops =
64
+ (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
65
+ }
66
+
67
+ static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
68
+ size_t input_size, const uint8_t* data) {
69
+ HashForgetfulChain* self = FN(Self)(handle);
70
+ /* Partial preparation is 100 times slower (per socket). */
71
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 6;
72
+ if (one_shot && input_size <= partial_prepare_threshold) {
73
+ size_t i;
74
+ for (i = 0; i < input_size; ++i) {
75
+ size_t bucket = FN(HashBytes)(&data[i]);
76
+ /* See InitEmpty comment. */
77
+ self->addr[bucket] = 0xCCCCCCCC;
78
+ self->head[bucket] = 0xCCCC;
79
+ }
80
+ } else {
66
81
  /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
67
82
  processed by hasher never reaches 3GB + 64M; this makes all new chains
68
83
  to be terminated after the first node. */
69
84
  memset(self->addr, 0xCC, sizeof(self->addr));
70
85
  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
  }
86
87
  memset(self->tiny_hash, 0, sizeof(self->tiny_hash));
87
88
  memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
88
- if (num != 0) {
89
- self->is_dirty_ = BROTLI_FALSE;
90
- }
91
89
  }
92
90
 
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
- }
91
+ static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
92
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
93
+ size_t input_size) {
94
+ BROTLI_UNUSED(params);
95
+ BROTLI_UNUSED(one_shot);
96
+ BROTLI_UNUSED(input_size);
97
+ return sizeof(HashForgetfulChain);
107
98
  }
108
99
 
109
100
  /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
110
101
  node to corresponding chain; also update tiny_hash for current position. */
111
- static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
102
+ static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle,
112
103
  const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
104
+ HashForgetfulChain* self = FN(Self)(handle);
113
105
  const size_t key = FN(HashBytes)(&data[ix & mask]);
114
106
  const size_t bank = key & (NUM_BANKS - 1);
115
107
  const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
@@ -122,56 +114,67 @@ static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
122
114
  self->head[key] = (uint16_t)idx;
123
115
  }
124
116
 
125
- static BROTLI_INLINE void FN(StoreRange)(HashForgetfulChain* self,
117
+ static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
126
118
  const uint8_t *data, const size_t mask, const size_t ix_start,
127
119
  const size_t ix_end) {
128
120
  size_t i;
129
121
  for (i = ix_start; i < ix_end; ++i) {
130
- FN(Store)(self, data, mask, i);
122
+ FN(Store)(handle, data, mask, i);
131
123
  }
132
124
  }
133
125
 
134
- static BROTLI_INLINE void FN(StitchToPreviousBlock)(HashForgetfulChain* self,
126
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,
135
127
  size_t num_bytes, size_t position, const uint8_t* ringbuffer,
136
128
  size_t ring_buffer_mask) {
137
129
  if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
138
130
  /* Prepare the hashes for three last bytes of the last write.
139
131
  These could not be calculated before, since they require knowledge
140
132
  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);
133
+ FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 3);
134
+ FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 2);
135
+ FN(Store)(handle, ringbuffer, ring_buffer_mask, position - 1);
144
136
  }
145
137
  }
146
138
 
139
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
140
+ HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
141
+ BROTLI_UNUSED(handle);
142
+ PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);
143
+ }
144
+
147
145
  /* Find a longest backward match of &data[cur_ix] up to the length of
148
146
  max_length and stores the position cur_ix in the hash table.
149
147
 
148
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
149
+ values; if this method is invoked repeatedly with the same distance
150
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
151
+
150
152
  Does not look for matches longer than max_length.
151
153
  Does not look for matches further away than max_backward.
152
154
  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,
155
+ |out|->score is updated only if a better match is found. */
156
+ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,
157
+ const BrotliDictionary* dictionary, const uint16_t* dictionary_hash,
158
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
159
+ const int* BROTLI_RESTRICT distance_cache,
157
160
  const size_t cur_ix, const size_t max_length, const size_t max_backward,
158
- HasherSearchResult* BROTLI_RESTRICT out) {
161
+ const size_t gap, HasherSearchResult* BROTLI_RESTRICT out) {
162
+ HashForgetfulChain* self = FN(Self)(handle);
159
163
  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
160
- BROTLI_BOOL is_match_found = BROTLI_FALSE;
161
164
  /* Don't accept a short copy from far away. */
165
+ score_t min_score = out->score;
162
166
  score_t best_score = out->score;
163
167
  size_t best_len = out->len;
164
168
  size_t i;
165
169
  const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
166
170
  const uint8_t tiny_hash = (uint8_t)(key);
167
171
  out->len = 0;
168
- out->len_x_code = 0;
172
+ out->len_code_delta = 0;
169
173
  /* Try last distance first. */
170
174
  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]);
175
+ const size_t backward = (size_t)distance_cache[i];
174
176
  size_t prev_ix = (cur_ix - backward);
177
+ /* For distance code 0 we want to consider 2-byte matches. */
175
178
  if (i > 0 && self->tiny_hash[(uint16_t)prev_ix] != tiny_hash) continue;
176
179
  if (prev_ix >= cur_ix || backward > max_backward) {
177
180
  continue;
@@ -182,14 +185,16 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
182
185
  &data[cur_ix_masked],
183
186
  max_length);
184
187
  if (len >= 2) {
185
- score_t score = BackwardReferenceScoreUsingLastDistance(len, i);
188
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
186
189
  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;
190
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
191
+ if (best_score < score) {
192
+ best_score = score;
193
+ best_len = len;
194
+ out->len = best_len;
195
+ out->distance = backward;
196
+ out->score = best_score;
197
+ }
193
198
  }
194
199
  }
195
200
  }
@@ -228,18 +233,17 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
228
233
  out->len = best_len;
229
234
  out->distance = backward;
230
235
  out->score = best_score;
231
- is_match_found = BROTLI_TRUE;
232
236
  }
233
237
  }
234
238
  }
235
239
  }
236
- FN(Store)(self, data, ring_buffer_mask, cur_ix);
240
+ FN(Store)(handle, data, ring_buffer_mask, cur_ix);
237
241
  }
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);
242
+ if (out->score == min_score) {
243
+ SearchInStaticDictionary(dictionary, dictionary_hash,
244
+ handle, &data[cur_ix_masked], max_length, max_backward + gap, out,
245
+ BROTLI_FALSE);
241
246
  }
242
- return is_match_found;
243
247
  }
244
248
 
245
249
  #undef BANK_SIZE
@@ -0,0 +1,266 @@
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 */
9
+
10
+ /* A (forgetful) hash table to the data seen by the compressor, to
11
+ help create backward references to previous data.
12
+
13
+ This is a hash map of fixed size (bucket_size_) to a ring buffer of
14
+ fixed size (block_size_). The ring buffer contains the last block_size_
15
+ index positions of the given hash key in the compressed data. */
16
+
17
+ #define HashLongestMatch HASHER()
18
+
19
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
20
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
21
+
22
+ /* HashBytes is the function that chooses the bucket to place the address in. */
23
+ static BROTLI_INLINE uint32_t FN(HashBytes)(const uint8_t *data,
24
+ const uint64_t mask,
25
+ const int shift) {
26
+ const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(data) & mask) * kHashMul64Long;
27
+ /* The higher bits contain more mixture from the multiplication,
28
+ so we take our results from there. */
29
+ return (uint32_t)(h >> shift);
30
+ }
31
+
32
+ typedef struct HashLongestMatch {
33
+ /* Number of hash buckets. */
34
+ size_t bucket_size_;
35
+ /* Only block_size_ newest backward references are kept,
36
+ and the older are forgotten. */
37
+ size_t block_size_;
38
+ /* Left-shift for computing hash bucket index from hash value. */
39
+ int hash_shift_;
40
+ /* Mask for selecting the next 4-8 bytes of input */
41
+ uint64_t hash_mask_;
42
+ /* Mask for accessing entries in a block (in a ring-buffer manner). */
43
+ uint32_t block_mask_;
44
+
45
+ /* --- Dynamic size members --- */
46
+
47
+ /* Number of entries in a particular bucket. */
48
+ /* uint16_t num[bucket_size]; */
49
+
50
+ /* Buckets containing block_size_ of backward references. */
51
+ /* uint32_t* buckets[bucket_size * block_size]; */
52
+ } HashLongestMatch;
53
+
54
+ static BROTLI_INLINE HashLongestMatch* FN(Self)(HasherHandle handle) {
55
+ return (HashLongestMatch*)&(GetHasherCommon(handle)[1]);
56
+ }
57
+
58
+ static BROTLI_INLINE uint16_t* FN(Num)(HashLongestMatch* self) {
59
+ return (uint16_t*)(&self[1]);
60
+ }
61
+
62
+ static BROTLI_INLINE uint32_t* FN(Buckets)(HashLongestMatch* self) {
63
+ return (uint32_t*)(&FN(Num)(self)[self->bucket_size_]);
64
+ }
65
+
66
+ static void FN(Initialize)(
67
+ HasherHandle handle, const BrotliEncoderParams* params) {
68
+ HasherCommon* common = GetHasherCommon(handle);
69
+ HashLongestMatch* self = FN(Self)(handle);
70
+ BROTLI_UNUSED(params);
71
+ self->hash_shift_ = 64 - common->params.bucket_bits;
72
+ self->hash_mask_ = (~((uint64_t)0U)) >> (64 - 8 * common->params.hash_len);
73
+ self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
74
+ self->block_size_ = (size_t)1 << common->params.block_bits;
75
+ self->block_mask_ = (uint32_t)(self->block_size_ - 1);
76
+ }
77
+
78
+ static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
79
+ size_t input_size, const uint8_t* data) {
80
+ HashLongestMatch* self = FN(Self)(handle);
81
+ uint16_t* num = FN(Num)(self);
82
+ /* Partial preparation is 100 times slower (per socket). */
83
+ size_t partial_prepare_threshold = self->bucket_size_ >> 6;
84
+ if (one_shot && input_size <= partial_prepare_threshold) {
85
+ size_t i;
86
+ for (i = 0; i < input_size; ++i) {
87
+ const uint32_t key = FN(HashBytes)(&data[i], self->hash_mask_,
88
+ self->hash_shift_);
89
+ num[key] = 0;
90
+ }
91
+ } else {
92
+ memset(num, 0, self->bucket_size_ * sizeof(num[0]));
93
+ }
94
+ }
95
+
96
+ static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
97
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
98
+ size_t input_size) {
99
+ size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
100
+ size_t block_size = (size_t)1 << params->hasher.block_bits;
101
+ BROTLI_UNUSED(one_shot);
102
+ BROTLI_UNUSED(input_size);
103
+ return sizeof(HashLongestMatch) + bucket_size * (2 + 4 * block_size);
104
+ }
105
+
106
+ /* Look at 4 bytes at &data[ix & mask].
107
+ Compute a hash from these, and store the value of ix at that position. */
108
+ static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t *data,
109
+ const size_t mask, const size_t ix) {
110
+ HashLongestMatch* self = FN(Self)(handle);
111
+ uint16_t* num = FN(Num)(self);
112
+ const uint32_t key = FN(HashBytes)(&data[ix & mask], self->hash_mask_,
113
+ self->hash_shift_);
114
+ const size_t minor_ix = num[key] & self->block_mask_;
115
+ const size_t offset =
116
+ minor_ix + (key << GetHasherCommon(handle)->params.block_bits);
117
+ FN(Buckets)(self)[offset] = (uint32_t)ix;
118
+ ++num[key];
119
+ }
120
+
121
+ static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
122
+ const uint8_t *data, const size_t mask, const size_t ix_start,
123
+ const size_t ix_end) {
124
+ size_t i;
125
+ for (i = ix_start; i < ix_end; ++i) {
126
+ FN(Store)(handle, data, mask, i);
127
+ }
128
+ }
129
+
130
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,
131
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
132
+ size_t ringbuffer_mask) {
133
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
134
+ /* Prepare the hashes for three last bytes of the last write.
135
+ These could not be calculated before, since they require knowledge
136
+ of both the previous and the current block. */
137
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);
138
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);
139
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);
140
+ }
141
+ }
142
+
143
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
144
+ HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
145
+ PrepareDistanceCache(distance_cache,
146
+ GetHasherCommon(handle)->params.num_last_distances_to_check);
147
+ }
148
+
149
+ /* Find a longest backward match of &data[cur_ix] up to the length of
150
+ max_length and stores the position cur_ix in the hash table.
151
+
152
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
153
+ values; if this method is invoked repeatedly with the same distance
154
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
155
+
156
+ Does not look for matches longer than max_length.
157
+ Does not look for matches further away than max_backward.
158
+ Writes the best match into |out|.
159
+ |out|->score is updated only if a better match is found. */
160
+ static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,
161
+ const BrotliDictionary* dictionary, const uint16_t* dictionary_hash,
162
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
163
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
164
+ const size_t max_length, const size_t max_backward, const size_t gap,
165
+ HasherSearchResult* BROTLI_RESTRICT out) {
166
+ HasherCommon* common = GetHasherCommon(handle);
167
+ HashLongestMatch* self = FN(Self)(handle);
168
+ uint16_t* num = FN(Num)(self);
169
+ uint32_t* buckets = FN(Buckets)(self);
170
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
171
+ /* Don't accept a short copy from far away. */
172
+ score_t min_score = out->score;
173
+ score_t best_score = out->score;
174
+ size_t best_len = out->len;
175
+ size_t i;
176
+ out->len = 0;
177
+ out->len_code_delta = 0;
178
+ /* Try last distance first. */
179
+ for (i = 0; i < (size_t)common->params.num_last_distances_to_check; ++i) {
180
+ const size_t backward = (size_t)distance_cache[i];
181
+ size_t prev_ix = (size_t)(cur_ix - backward);
182
+ if (prev_ix >= cur_ix) {
183
+ continue;
184
+ }
185
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
186
+ continue;
187
+ }
188
+ prev_ix &= ring_buffer_mask;
189
+
190
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
191
+ prev_ix + best_len > ring_buffer_mask ||
192
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
193
+ continue;
194
+ }
195
+ {
196
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
197
+ &data[cur_ix_masked],
198
+ max_length);
199
+ if (len >= 3 || (len == 2 && i < 2)) {
200
+ /* Comparing for >= 2 does not change the semantics, but just saves for
201
+ a few unnecessary binary logarithms in backward reference score,
202
+ since we are not interested in such short matches. */
203
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
204
+ if (best_score < score) {
205
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
206
+ if (best_score < score) {
207
+ best_score = score;
208
+ best_len = len;
209
+ out->len = best_len;
210
+ out->distance = backward;
211
+ out->score = best_score;
212
+ }
213
+ }
214
+ }
215
+ }
216
+ }
217
+ {
218
+ const uint32_t key = FN(HashBytes)(
219
+ &data[cur_ix_masked], self->hash_mask_, self->hash_shift_);
220
+ uint32_t* BROTLI_RESTRICT bucket =
221
+ &buckets[key << common->params.block_bits];
222
+ const size_t down =
223
+ (num[key] > self->block_size_) ?
224
+ (num[key] - self->block_size_) : 0u;
225
+ for (i = num[key]; i > down;) {
226
+ size_t prev_ix = bucket[--i & self->block_mask_];
227
+ const size_t backward = cur_ix - prev_ix;
228
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
229
+ break;
230
+ }
231
+ prev_ix &= ring_buffer_mask;
232
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
233
+ prev_ix + best_len > ring_buffer_mask ||
234
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
235
+ continue;
236
+ }
237
+ {
238
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
239
+ &data[cur_ix_masked],
240
+ max_length);
241
+ if (len >= 4) {
242
+ /* Comparing for >= 3 does not change the semantics, but just saves
243
+ for a few unnecessary binary logarithms in backward reference
244
+ score, since we are not interested in such short matches. */
245
+ score_t score = BackwardReferenceScore(len, backward);
246
+ if (best_score < score) {
247
+ best_score = score;
248
+ best_len = len;
249
+ out->len = best_len;
250
+ out->distance = backward;
251
+ out->score = best_score;
252
+ }
253
+ }
254
+ }
255
+ }
256
+ bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;
257
+ ++num[key];
258
+ }
259
+ if (min_score == out->score) {
260
+ SearchInStaticDictionary(dictionary, dictionary_hash,
261
+ handle, &data[cur_ix_masked], max_length, max_backward + gap, out,
262
+ BROTLI_FALSE);
263
+ }
264
+ }
265
+
266
+ #undef HashLongestMatch