Coverage Report

Created: 2025-06-10 07:27

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