Coverage Report

Created: 2025-12-31 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/thirdparty/brotli/c/enc/hash.h
Line
Count
Source
1
/* Copyright 2010 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* A (forgetful) hash table to the data seen by the compressor, to
8
   help create backward references to previous data. */
9
10
#ifndef BROTLI_ENC_HASH_H_
11
#define BROTLI_ENC_HASH_H_
12
13
#include <stdlib.h>  /* exit */
14
#include <string.h>  /* memcmp, memset */
15
16
#include <brotli/types.h>
17
18
#include "../common/constants.h"
19
#include "../common/dictionary.h"
20
#include "../common/platform.h"
21
#include "compound_dictionary.h"
22
#include "encoder_dict.h"
23
#include "fast_log.h"
24
#include "find_match_length.h"
25
#include "matching_tag_mask.h"
26
#include "memory.h"
27
#include "quality.h"
28
#include "static_dict.h"
29
30
#if defined(__cplusplus) || defined(c_plusplus)
31
extern "C" {
32
#endif
33
34
typedef struct {
35
  /**
36
   * Dynamically allocated areas; regular hasher uses one or two allocations;
37
   * "composite" hasher uses up to 4 allocations.
38
   */
39
  void* extra[4];
40
41
  /**
42
   * False before the fisrt invocation of HasherSetup (where "extra" memory)
43
   * is allocated.
44
   */
45
  BROTLI_BOOL is_setup_;
46
47
  size_t dict_num_lookups;
48
  size_t dict_num_matches;
49
50
  BrotliHasherParams params;
51
52
  /**
53
   * False if hasher needs to be "prepared" before use (before the first
54
   * invocation of HasherSetup or after HasherReset). "preparation" is hasher
55
   * data initialization (using input ringbuffer).
56
   */
57
  BROTLI_BOOL is_prepared_;
58
} HasherCommon;
59
60
0
#define score_t size_t
61
62
static const uint32_t kCutoffTransformsCount = 10;
63
/*   0,  12,   27,    23,    42,    63,    56,    48,    59,    64 */
64
/* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */
65
static const uint64_t kCutoffTransforms =
66
    BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200);
67
68
typedef struct HasherSearchResult {
69
  size_t len;
70
  size_t distance;
71
  score_t score;
72
  int len_code_delta; /* == len_code - len */
73
} HasherSearchResult;
74
75
/* kHashMul32 multiplier has these properties:
76
   * The multiplier must be odd. Otherwise we may lose the highest bit.
77
   * No long streaks of ones or zeros.
78
   * There is no effort to ensure that it is a prime, the oddity is enough
79
     for this use.
80
   * The number has been tuned heuristically against compression benchmarks. */
81
static const uint32_t kHashMul32 = 0x1E35A7BD;
82
static const uint64_t kHashMul64 =
83
    BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u);
84
85
0
static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) {
86
0
  uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
87
  /* The higher bits contain more mixture from the multiplication,
88
     so we take our results from there. */
89
0
  return h >> (32 - 14);
90
0
}
Unexecuted instantiation: encode.c:Hash14
Unexecuted instantiation: encoder_dict.c:Hash14
Unexecuted instantiation: backward_references.c:Hash14
Unexecuted instantiation: backward_references_hq.c:Hash14
91
92
static BROTLI_INLINE void PrepareDistanceCache(
93
0
    int* BROTLI_RESTRICT distance_cache, const int num_distances) {
94
0
  if (num_distances > 4) {
95
0
    int last_distance = distance_cache[0];
96
0
    distance_cache[4] = last_distance - 1;
97
0
    distance_cache[5] = last_distance + 1;
98
0
    distance_cache[6] = last_distance - 2;
99
0
    distance_cache[7] = last_distance + 2;
100
0
    distance_cache[8] = last_distance - 3;
101
0
    distance_cache[9] = last_distance + 3;
102
0
    if (num_distances > 10) {
103
0
      int next_last_distance = distance_cache[1];
104
0
      distance_cache[10] = next_last_distance - 1;
105
0
      distance_cache[11] = next_last_distance + 1;
106
0
      distance_cache[12] = next_last_distance - 2;
107
0
      distance_cache[13] = next_last_distance + 2;
108
0
      distance_cache[14] = next_last_distance - 3;
109
0
      distance_cache[15] = next_last_distance + 3;
110
0
    }
111
0
  }
112
0
}
Unexecuted instantiation: encode.c:PrepareDistanceCache
Unexecuted instantiation: encoder_dict.c:PrepareDistanceCache
Unexecuted instantiation: backward_references.c:PrepareDistanceCache
Unexecuted instantiation: backward_references_hq.c:PrepareDistanceCache
113
114
0
#define BROTLI_LITERAL_BYTE_SCORE 135
115
0
#define BROTLI_DISTANCE_BIT_PENALTY 30
116
/* Score must be positive after applying maximal penalty. */
117
0
#define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t))
118
119
/* Usually, we always choose the longest backward reference. This function
120
   allows for the exception of that rule.
121
122
   If we choose a backward reference that is further away, it will
123
   usually be coded with more bits. We approximate this by assuming
124
   log2(distance). If the distance can be expressed in terms of the
125
   last four distances, we use some heuristic constants to estimate
126
   the bits cost. For the first up to four literals we use the bit
127
   cost of the literals from the literal cost model, after that we
128
   use the average bit cost of the cost model.
129
130
   This function is used to sometimes discard a longer backward reference
131
   when it is not much longer and the bit cost for encoding it is more
132
   than the saved literals.
133
134
   backward_reference_offset MUST be positive. */
135
static BROTLI_INLINE score_t BackwardReferenceScore(
136
0
    size_t copy_length, size_t backward_reference_offset) {
137
0
  return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length -
138
0
      BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset);
139
0
}
Unexecuted instantiation: encode.c:BackwardReferenceScore
Unexecuted instantiation: encoder_dict.c:BackwardReferenceScore
Unexecuted instantiation: backward_references.c:BackwardReferenceScore
Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScore
140
141
static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance(
142
0
    size_t copy_length) {
143
0
  return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length +
144
0
      BROTLI_SCORE_BASE + 15;
145
0
}
Unexecuted instantiation: encode.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: encoder_dict.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: backward_references.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScoreUsingLastDistance
146
147
static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance(
148
0
    size_t distance_short_code) {
149
0
  return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE);
150
0
}
Unexecuted instantiation: encode.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: encoder_dict.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: backward_references.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: backward_references_hq.c:BackwardReferencePenaltyUsingLastDistance
151
152
static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem(
153
    const BrotliEncoderDictionary* dictionary, size_t len, size_t word_idx,
154
    const uint8_t* data, size_t max_length, size_t max_backward,
155
0
    size_t max_distance, HasherSearchResult* out) {
156
0
  size_t offset;
157
0
  size_t matchlen;
158
0
  size_t backward;
159
0
  score_t score;
160
0
  offset = dictionary->words->offsets_by_length[len] + len * word_idx;
161
0
  if (len > max_length) {
162
0
    return BROTLI_FALSE;
163
0
  }
164
165
0
  matchlen =
166
0
      FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len);
167
0
  if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) {
168
0
    return BROTLI_FALSE;
169
0
  }
170
0
  {
171
0
    size_t cut = len - matchlen;
172
0
    size_t transform_id = (cut << 2) +
173
0
        (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F);
174
0
    backward = max_backward + 1 + word_idx +
175
0
        (transform_id << dictionary->words->size_bits_by_length[len]);
176
0
  }
177
0
  if (backward > max_distance) {
178
0
    return BROTLI_FALSE;
179
0
  }
180
0
  score = BackwardReferenceScore(matchlen, backward);
181
0
  if (score < out->score) {
182
0
    return BROTLI_FALSE;
183
0
  }
184
0
  out->len = matchlen;
185
0
  out->len_code_delta = (int)len - (int)matchlen;
186
0
  out->distance = backward;
187
0
  out->score = score;
188
0
  return BROTLI_TRUE;
189
0
}
Unexecuted instantiation: encode.c:TestStaticDictionaryItem
Unexecuted instantiation: encoder_dict.c:TestStaticDictionaryItem
Unexecuted instantiation: backward_references.c:TestStaticDictionaryItem
Unexecuted instantiation: backward_references_hq.c:TestStaticDictionaryItem
190
191
static BROTLI_INLINE void SearchInStaticDictionary(
192
    const BrotliEncoderDictionary* dictionary,
193
    HasherCommon* common, const uint8_t* data, size_t max_length,
194
    size_t max_backward, size_t max_distance,
195
0
    HasherSearchResult* out, BROTLI_BOOL shallow) {
196
0
  size_t key;
197
0
  size_t i;
198
0
  if (common->dict_num_matches < (common->dict_num_lookups >> 7)) {
199
0
    return;
200
0
  }
201
0
  key = Hash14(data) << 1;
202
0
  for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) {
203
0
    common->dict_num_lookups++;
204
0
    if (dictionary->hash_table_lengths[key] != 0) {
205
0
      BROTLI_BOOL item_matches = TestStaticDictionaryItem(
206
0
          dictionary, dictionary->hash_table_lengths[key],
207
0
          dictionary->hash_table_words[key], data,
208
0
          max_length, max_backward, max_distance, out);
209
0
      if (item_matches) {
210
0
        common->dict_num_matches++;
211
0
      }
212
0
    }
213
0
  }
214
0
}
Unexecuted instantiation: encode.c:SearchInStaticDictionary
Unexecuted instantiation: encoder_dict.c:SearchInStaticDictionary
Unexecuted instantiation: backward_references.c:SearchInStaticDictionary
Unexecuted instantiation: backward_references_hq.c:SearchInStaticDictionary
215
216
typedef struct BackwardMatch {
217
  uint32_t distance;
218
  uint32_t length_and_code;
219
} BackwardMatch;
220
221
static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self,
222
0
    size_t dist, size_t len) {
223
0
  self->distance = (uint32_t)dist;
224
0
  self->length_and_code = (uint32_t)(len << 5);
225
0
}
Unexecuted instantiation: encode.c:InitBackwardMatch
Unexecuted instantiation: encoder_dict.c:InitBackwardMatch
Unexecuted instantiation: backward_references.c:InitBackwardMatch
Unexecuted instantiation: backward_references_hq.c:InitBackwardMatch
226
227
static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self,
228
0
    size_t dist, size_t len, size_t len_code) {
229
0
  self->distance = (uint32_t)dist;
230
0
  self->length_and_code =
231
0
      (uint32_t)((len << 5) | (len == len_code ? 0 : len_code));
232
0
}
Unexecuted instantiation: encode.c:InitDictionaryBackwardMatch
Unexecuted instantiation: encoder_dict.c:InitDictionaryBackwardMatch
Unexecuted instantiation: backward_references.c:InitDictionaryBackwardMatch
Unexecuted instantiation: backward_references_hq.c:InitDictionaryBackwardMatch
233
234
0
static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) {
235
0
  return self->length_and_code >> 5;
236
0
}
Unexecuted instantiation: encode.c:BackwardMatchLength
Unexecuted instantiation: encoder_dict.c:BackwardMatchLength
Unexecuted instantiation: backward_references.c:BackwardMatchLength
Unexecuted instantiation: backward_references_hq.c:BackwardMatchLength
237
238
0
static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) {
239
0
  size_t code = self->length_and_code & 31;
240
0
  return code ? code : BackwardMatchLength(self);
241
0
}
Unexecuted instantiation: encode.c:BackwardMatchLengthCode
Unexecuted instantiation: encoder_dict.c:BackwardMatchLengthCode
Unexecuted instantiation: backward_references.c:BackwardMatchLengthCode
Unexecuted instantiation: backward_references_hq.c:BackwardMatchLengthCode
242
243
0
#define EXPAND_CAT(a, b) CAT(a, b)
244
0
#define CAT(a, b) a ## b
245
0
#define FN(X) EXPAND_CAT(X, HASHER())
246
247
#define HASHER() H10
248
0
#define BUCKET_BITS 17
249
0
#define MAX_TREE_SEARCH_DEPTH 64
250
0
#define MAX_TREE_COMP_LENGTH 128
251
#include "hash_to_binary_tree_inc.h"  /* NOLINT(build/include) */
252
#undef MAX_TREE_SEARCH_DEPTH
253
#undef MAX_TREE_COMP_LENGTH
254
#undef BUCKET_BITS
255
#undef HASHER
256
/* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */
257
0
#define MAX_NUM_MATCHES_H10 128
258
259
/* For BUCKET_SWEEP_BITS == 0, enabling the dictionary lookup makes compression
260
   a little faster (0.5% - 1%) and it compresses 0.15% better on small text
261
   and HTML inputs. */
262
263
#define HASHER() H2
264
0
#define BUCKET_BITS 16
265
0
#define BUCKET_SWEEP_BITS 0
266
0
#define HASH_LEN 5
267
0
#define USE_DICTIONARY 1
268
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
269
#undef BUCKET_SWEEP_BITS
270
#undef USE_DICTIONARY
271
#undef HASHER
272
273
#define HASHER() H3
274
0
#define BUCKET_SWEEP_BITS 1
275
0
#define USE_DICTIONARY 0
276
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
277
#undef USE_DICTIONARY
278
#undef BUCKET_SWEEP_BITS
279
#undef BUCKET_BITS
280
#undef HASHER
281
282
#define HASHER() H4
283
0
#define BUCKET_BITS 17
284
0
#define BUCKET_SWEEP_BITS 2
285
0
#define USE_DICTIONARY 1
286
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
287
#undef USE_DICTIONARY
288
#undef HASH_LEN
289
#undef BUCKET_SWEEP_BITS
290
#undef BUCKET_BITS
291
#undef HASHER
292
293
#define HASHER() H5
294
#include "hash_longest_match_inc.h"  /* NOLINT(build/include) */
295
#undef HASHER
296
297
#define HASHER() H6
298
#include "hash_longest_match64_inc.h"  /* NOLINT(build/include) */
299
#undef HASHER
300
301
#if defined(BROTLI_MAX_SIMD_QUALITY)
302
#define HASHER() H58
303
#include "hash_longest_match_simd_inc.h" /* NOLINT(build/include) */
304
#undef HASHER
305
306
#define HASHER() H68
307
#include "hash_longest_match64_simd_inc.h" /* NOLINT(build/include) */
308
#undef HASHER
309
#endif
310
311
0
#define BUCKET_BITS 15
312
313
0
#define NUM_LAST_DISTANCES_TO_CHECK 4
314
0
#define NUM_BANKS 1
315
0
#define BANK_BITS 16
316
#define HASHER() H40
317
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
318
#undef HASHER
319
#undef NUM_LAST_DISTANCES_TO_CHECK
320
321
0
#define NUM_LAST_DISTANCES_TO_CHECK 10
322
#define HASHER() H41
323
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
324
#undef HASHER
325
#undef NUM_LAST_DISTANCES_TO_CHECK
326
#undef NUM_BANKS
327
#undef BANK_BITS
328
329
0
#define NUM_LAST_DISTANCES_TO_CHECK 16
330
0
#define NUM_BANKS 512
331
0
#define BANK_BITS 9
332
#define HASHER() H42
333
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
334
#undef HASHER
335
#undef NUM_LAST_DISTANCES_TO_CHECK
336
#undef NUM_BANKS
337
#undef BANK_BITS
338
339
#undef BUCKET_BITS
340
341
#define HASHER() H54
342
0
#define BUCKET_BITS 20
343
0
#define BUCKET_SWEEP_BITS 2
344
0
#define HASH_LEN 7
345
0
#define USE_DICTIONARY 0
346
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
347
#undef USE_DICTIONARY
348
#undef HASH_LEN
349
#undef BUCKET_SWEEP_BITS
350
#undef BUCKET_BITS
351
#undef HASHER
352
353
/* fast large window hashers */
354
355
#define HASHER() HROLLING_FAST
356
0
#define CHUNKLEN 32
357
0
#define JUMP 4
358
0
#define NUMBUCKETS 16777216
359
0
#define MASK ((NUMBUCKETS * 64) - 1)
360
#include "hash_rolling_inc.h"  /* NOLINT(build/include) */
361
#undef JUMP
362
#undef HASHER
363
364
365
#define HASHER() HROLLING
366
0
#define JUMP 1
367
#include "hash_rolling_inc.h"  /* NOLINT(build/include) */
368
#undef MASK
369
#undef NUMBUCKETS
370
#undef JUMP
371
#undef CHUNKLEN
372
#undef HASHER
373
374
#define HASHER() H35
375
#define HASHER_A H3
376
#define HASHER_B HROLLING_FAST
377
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
378
#undef HASHER_A
379
#undef HASHER_B
380
#undef HASHER
381
382
#define HASHER() H55
383
#define HASHER_A H54
384
#define HASHER_B HROLLING_FAST
385
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
386
#undef HASHER_A
387
#undef HASHER_B
388
#undef HASHER
389
390
#define HASHER() H65
391
#define HASHER_A H6
392
#define HASHER_B HROLLING
393
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
394
#undef HASHER_A
395
#undef HASHER_B
396
#undef HASHER
397
398
#undef FN
399
#undef CAT
400
#undef EXPAND_CAT
401
402
#if defined(BROTLI_MAX_SIMD_QUALITY)
403
#define FOR_SIMPLE_HASHERS(H) \
404
0
  H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) H(58) H(68)
405
#else
406
#define FOR_SIMPLE_HASHERS(H) \
407
  H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)
408
#endif
409
0
#define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65)
410
0
#define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H)
411
0
#define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10)
412
413
typedef struct {
414
  HasherCommon common;
415
416
  union {
417
#define MEMBER_(N) \
418
    H ## N _H ## N;
419
    FOR_ALL_HASHERS(MEMBER_)
420
#undef MEMBER_
421
  } privat;
422
} Hasher;
423
424
/* MUST be invoked before any other method. */
425
0
static BROTLI_INLINE void HasherInit(Hasher* hasher) {
426
0
  hasher->common.is_setup_ = BROTLI_FALSE;
427
0
  hasher->common.extra[0] = NULL;
428
0
  hasher->common.extra[1] = NULL;
429
0
  hasher->common.extra[2] = NULL;
430
0
  hasher->common.extra[3] = NULL;
431
0
}
Unexecuted instantiation: encode.c:HasherInit
Unexecuted instantiation: encoder_dict.c:HasherInit
Unexecuted instantiation: backward_references.c:HasherInit
Unexecuted instantiation: backward_references_hq.c:HasherInit
432
433
0
static BROTLI_INLINE void DestroyHasher(MemoryManager* m, Hasher* hasher) {
434
0
  if (hasher->common.extra[0] != NULL) BROTLI_FREE(m, hasher->common.extra[0]);
435
0
  if (hasher->common.extra[1] != NULL) BROTLI_FREE(m, hasher->common.extra[1]);
436
0
  if (hasher->common.extra[2] != NULL) BROTLI_FREE(m, hasher->common.extra[2]);
437
0
  if (hasher->common.extra[3] != NULL) BROTLI_FREE(m, hasher->common.extra[3]);
438
0
}
Unexecuted instantiation: encode.c:DestroyHasher
Unexecuted instantiation: encoder_dict.c:DestroyHasher
Unexecuted instantiation: backward_references.c:DestroyHasher
Unexecuted instantiation: backward_references_hq.c:DestroyHasher
439
440
0
static BROTLI_INLINE void HasherReset(Hasher* hasher) {
441
0
  hasher->common.is_prepared_ = BROTLI_FALSE;
442
0
}
Unexecuted instantiation: encode.c:HasherReset
Unexecuted instantiation: encoder_dict.c:HasherReset
Unexecuted instantiation: backward_references.c:HasherReset
Unexecuted instantiation: backward_references_hq.c:HasherReset
443
444
static BROTLI_INLINE void HasherSize(const BrotliEncoderParams* params,
445
0
    BROTLI_BOOL one_shot, const size_t input_size, size_t* alloc_size) {
446
0
  switch (params->hasher.type) {
447
0
#define SIZE_(N)                                                           \
448
0
    case N:                                                                \
449
0
      HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \
450
0
      break;
451
0
    FOR_ALL_HASHERS(SIZE_)
452
0
#undef SIZE_
453
0
    default:
454
0
      break;
455
0
  }
456
0
}
Unexecuted instantiation: encode.c:HasherSize
Unexecuted instantiation: encoder_dict.c:HasherSize
Unexecuted instantiation: backward_references.c:HasherSize
Unexecuted instantiation: backward_references_hq.c:HasherSize
457
458
static BROTLI_INLINE void HasherSetup(MemoryManager* m, Hasher* hasher,
459
    BrotliEncoderParams* params, const uint8_t* data, size_t position,
460
0
    size_t input_size, BROTLI_BOOL is_last) {
461
0
  BROTLI_BOOL one_shot = (position == 0 && is_last);
462
0
  if (!hasher->common.is_setup_) {
463
0
    size_t alloc_size[4] = {0};
464
0
    size_t i;
465
0
    ChooseHasher(params, &params->hasher);
466
0
    hasher->common.params = params->hasher;
467
0
    hasher->common.dict_num_lookups = 0;
468
0
    hasher->common.dict_num_matches = 0;
469
0
    HasherSize(params, one_shot, input_size, alloc_size);
470
0
    for (i = 0; i < 4; ++i) {
471
0
      if (alloc_size[i] == 0) continue;
472
0
      hasher->common.extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]);
473
0
      if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.extra[i])) return;
474
0
    }
475
0
    switch (hasher->common.params.type) {
476
0
#define INITIALIZE_(N)                        \
477
0
      case N:                                 \
478
0
        InitializeH ## N(&hasher->common,     \
479
0
            &hasher->privat._H ## N, params); \
480
0
        break;
481
0
      FOR_ALL_HASHERS(INITIALIZE_);
482
0
#undef INITIALIZE_
483
0
      default:
484
0
        break;
485
0
    }
486
0
    HasherReset(hasher);
487
0
    hasher->common.is_setup_ = BROTLI_TRUE;
488
0
  }
489
490
0
  if (!hasher->common.is_prepared_) {
491
0
    switch (hasher->common.params.type) {
492
0
#define PREPARE_(N)                      \
493
0
      case N:                            \
494
0
        PrepareH ## N(                   \
495
0
            &hasher->privat._H ## N,     \
496
0
            one_shot, input_size, data); \
497
0
        break;
498
0
      FOR_ALL_HASHERS(PREPARE_)
499
0
#undef PREPARE_
500
0
      default: break;
501
0
    }
502
0
    hasher->common.is_prepared_ = BROTLI_TRUE;
503
0
  }
504
0
}
Unexecuted instantiation: encode.c:HasherSetup
Unexecuted instantiation: encoder_dict.c:HasherSetup
Unexecuted instantiation: backward_references.c:HasherSetup
Unexecuted instantiation: backward_references_hq.c:HasherSetup
505
506
static BROTLI_INLINE void InitOrStitchToPreviousBlock(
507
    MemoryManager* m, Hasher* hasher, const uint8_t* data, size_t mask,
508
    BrotliEncoderParams* params, size_t position, size_t input_size,
509
0
    BROTLI_BOOL is_last) {
510
0
  HasherSetup(m, hasher, params, data, position, input_size, is_last);
511
0
  if (BROTLI_IS_OOM(m)) return;
512
0
  switch (hasher->common.params.type) {
513
0
#define INIT_(N)                             \
514
0
    case N:                                  \
515
0
      StitchToPreviousBlockH ## N(           \
516
0
          &hasher->privat._H ## N,           \
517
0
          input_size, position, data, mask); \
518
0
    break;
519
0
    FOR_ALL_HASHERS(INIT_)
520
0
#undef INIT_
521
0
    default: break;
522
0
  }
523
0
}
Unexecuted instantiation: encode.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: encoder_dict.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: backward_references.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: backward_references_hq.c:InitOrStitchToPreviousBlock
524
525
/* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
526
       to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
527
static BROTLI_INLINE void FindCompoundDictionaryMatch(
528
    const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
529
    const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
530
    const size_t cur_ix, const size_t max_length, const size_t distance_offset,
531
0
    const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) {
532
0
  const uint32_t source_size = self->source_size;
533
0
  const size_t boundary = distance_offset - source_size;
534
0
  const uint32_t hash_bits = self->hash_bits;
535
0
  const uint32_t bucket_bits = self->bucket_bits;
536
0
  const uint32_t slot_bits = self->slot_bits;
537
538
0
  const uint32_t hash_shift = 64u - bucket_bits;
539
0
  const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
540
0
  const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
541
542
0
  const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
543
0
  const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]);
544
0
  const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]);
545
0
  const uint8_t* source = NULL;
546
547
0
  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
548
0
  score_t best_score = out->score;
549
0
  size_t best_len = out->len;
550
0
  size_t i;
551
0
  const uint64_t h =
552
0
      (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
553
0
      kPreparedDictionaryHashMul64Long;
554
0
  const uint32_t key = (uint32_t)(h >> hash_shift);
555
0
  const uint32_t slot = key & slot_mask;
556
0
  const uint32_t head = heads[key];
557
0
  const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
558
0
  uint32_t item = (head == 0xFFFF) ? 1 : 0;
559
560
0
  const void* tail = (void*)&items[self->num_items];
561
0
  if (self->magic == kPreparedDictionaryMagic) {
562
0
    source = (const uint8_t*)tail;
563
0
  } else {
564
    /* kLeanPreparedDictionaryMagic */
565
0
    source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
566
0
  }
567
568
0
  BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
569
570
0
  for (i = 0; i < 4; ++i) {
571
0
    const size_t distance = (size_t)distance_cache[i];
572
0
    size_t offset;
573
0
    size_t limit;
574
0
    size_t len;
575
0
    if (distance <= boundary || distance > distance_offset) continue;
576
0
    offset = distance_offset - distance;
577
0
    limit = source_size - offset;
578
0
    limit = limit > max_length ? max_length : limit;
579
0
    len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked],
580
0
                                   limit);
581
0
    if (len >= 2) {
582
0
      score_t score = BackwardReferenceScoreUsingLastDistance(len);
583
0
      if (best_score < score) {
584
0
        if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
585
0
        if (best_score < score) {
586
0
          best_score = score;
587
0
          if (len > best_len) best_len = len;
588
0
          out->len = len;
589
0
          out->len_code_delta = 0;
590
0
          out->distance = distance;
591
0
          out->score = best_score;
592
0
        }
593
0
      }
594
0
    }
595
0
  }
596
  /* we require matches of len >4, so increase best_len to 3, so we can compare
597
   * 4 bytes all the time. */
598
0
  if (best_len < 3) {
599
0
    best_len = 3;
600
0
  }
601
0
  while (item == 0) {
602
0
    size_t offset;
603
0
    size_t distance;
604
0
    size_t limit;
605
0
    item = *chain;
606
0
    chain++;
607
0
    offset = item & 0x7FFFFFFF;
608
0
    item &= 0x80000000;
609
0
    distance = distance_offset - offset;
610
0
    limit = source_size - offset;
611
0
    limit = (limit > max_length) ? max_length : limit;
612
0
    if (distance > max_distance) continue;
613
0
    if (cur_ix_masked + best_len > ring_buffer_mask || best_len >= limit ||
614
        /* compare 4 bytes ending at best_len + 1 */
615
0
        BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
616
0
            BrotliUnalignedRead32(&source[offset + best_len - 3])) {
617
0
      continue;
618
0
    }
619
0
    {
620
0
      const size_t len = FindMatchLengthWithLimit(&source[offset],
621
0
                                                  &data[cur_ix_masked],
622
0
                                                  limit);
623
0
      if (len >= 4) {
624
0
        score_t score = BackwardReferenceScore(len, distance);
625
0
        if (best_score < score) {
626
0
          best_score = score;
627
0
          best_len = len;
628
0
          out->len = best_len;
629
0
          out->len_code_delta = 0;
630
0
          out->distance = distance;
631
0
          out->score = best_score;
632
0
        }
633
0
      }
634
0
    }
635
0
  }
636
0
}
Unexecuted instantiation: encode.c:FindCompoundDictionaryMatch
Unexecuted instantiation: encoder_dict.c:FindCompoundDictionaryMatch
Unexecuted instantiation: backward_references.c:FindCompoundDictionaryMatch
Unexecuted instantiation: backward_references_hq.c:FindCompoundDictionaryMatch
637
638
/* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
639
       to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
640
static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches(
641
    const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
642
    const size_t ring_buffer_mask, const size_t cur_ix, const size_t min_length,
643
    const size_t max_length, const size_t distance_offset,
644
0
    const size_t max_distance, BackwardMatch* matches, size_t match_limit) {
645
0
  const uint32_t source_size = self->source_size;
646
0
  const uint32_t hash_bits = self->hash_bits;
647
0
  const uint32_t bucket_bits = self->bucket_bits;
648
0
  const uint32_t slot_bits = self->slot_bits;
649
650
0
  const uint32_t hash_shift = 64u - bucket_bits;
651
0
  const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
652
0
  const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
653
654
0
  const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
655
0
  const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]);
656
0
  const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]);
657
0
  const uint8_t* source = NULL;
658
659
0
  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
660
0
  size_t best_len = min_length;
661
0
  const uint64_t h =
662
0
      (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
663
0
      kPreparedDictionaryHashMul64Long;
664
0
  const uint32_t key = (uint32_t)(h >> hash_shift);
665
0
  const uint32_t slot = key & slot_mask;
666
0
  const uint32_t head = heads[key];
667
0
  const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
668
0
  uint32_t item = (head == 0xFFFF) ? 1 : 0;
669
0
  size_t found = 0;
670
671
0
  const void* tail = (void*)&items[self->num_items];
672
0
  if (self->magic == kPreparedDictionaryMagic) {
673
0
    source = (const uint8_t*)tail;
674
0
  } else {
675
    /* kLeanPreparedDictionaryMagic */
676
0
    source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
677
0
  }
678
679
0
  BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
680
681
0
  while (item == 0) {
682
0
    size_t offset;
683
0
    size_t distance;
684
0
    size_t limit;
685
0
    size_t len;
686
0
    item = *chain;
687
0
    chain++;
688
0
    offset = item & 0x7FFFFFFF;
689
0
    item &= 0x80000000;
690
0
    distance = distance_offset - offset;
691
0
    limit = source_size - offset;
692
0
    limit = (limit > max_length) ? max_length : limit;
693
0
    if (distance > max_distance) continue;
694
0
    if (cur_ix_masked + best_len > ring_buffer_mask ||
695
0
        best_len >= limit ||
696
0
        data[cur_ix_masked + best_len] != source[offset + best_len]) {
697
0
      continue;
698
0
    }
699
0
    len = FindMatchLengthWithLimit(
700
0
        &source[offset], &data[cur_ix_masked], limit);
701
0
    if (len > best_len) {
702
0
      best_len = len;
703
0
      InitBackwardMatch(matches++, distance, len);
704
0
      found++;
705
0
      if (found == match_limit) break;
706
0
    }
707
0
  }
708
0
  return found;
709
0
}
Unexecuted instantiation: encode.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: encoder_dict.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references_hq.c:FindAllCompoundDictionaryMatches
710
711
static BROTLI_INLINE void LookupCompoundDictionaryMatch(
712
    const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
713
    const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
714
    const size_t cur_ix, const size_t max_length,
715
    const size_t max_ring_buffer_distance, const size_t max_distance,
716
0
    HasherSearchResult* sr) {
717
0
  size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
718
0
  size_t d;
719
0
  for (d = 0; d < addon->num_chunks; ++d) {
720
    /* Only one prepared dictionary type is currently supported. */
721
0
    FindCompoundDictionaryMatch(
722
0
        (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
723
0
        distance_cache, cur_ix, max_length,
724
0
        base_offset - addon->chunk_offsets[d], max_distance, sr);
725
0
  }
726
0
}
Unexecuted instantiation: encode.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: encoder_dict.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: backward_references.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: backward_references_hq.c:LookupCompoundDictionaryMatch
727
728
static BROTLI_INLINE size_t LookupAllCompoundDictionaryMatches(
729
    const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
730
    const size_t ring_buffer_mask, const size_t cur_ix, size_t min_length,
731
    const size_t max_length, const size_t max_ring_buffer_distance,
732
    const size_t max_distance, BackwardMatch* matches,
733
0
    size_t match_limit) {
734
0
  size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
735
0
  size_t d;
736
0
  size_t total_found = 0;
737
0
  for (d = 0; d < addon->num_chunks; ++d) {
738
    /* Only one prepared dictionary type is currently supported. */
739
0
    total_found += FindAllCompoundDictionaryMatches(
740
0
        (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
741
0
        cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d],
742
0
        max_distance, matches + total_found, match_limit - total_found);
743
0
    if (total_found == match_limit) break;
744
0
    if (total_found > 0) {
745
0
      min_length = BackwardMatchLength(&matches[total_found - 1]);
746
0
    }
747
0
  }
748
0
  return total_found;
749
0
}
Unexecuted instantiation: encode.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: encoder_dict.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references_hq.c:LookupAllCompoundDictionaryMatches
750
751
#if defined(__cplusplus) || defined(c_plusplus)
752
}  /* extern "C" */
753
#endif
754
755
#endif  /* BROTLI_ENC_HASH_H_ */