1 /* Copyright 2013 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 /* Function to find backward reference copies. */
8
9 #include "./backward_references.h"
10
11 #include "../common/constants.h"
12 #include "../common/dictionary.h"
13 #include <brotli/types.h>
14 #include "./command.h"
15 #include "./dictionary_hash.h"
16 #include "./memory.h"
17 #include "./port.h"
18 #include "./quality.h"
19
20 #if defined(__cplusplus) || defined(c_plusplus)
21 extern "C" {
22 #endif
23
ComputeDistanceCode(size_t distance,size_t max_distance,const int * dist_cache)24 static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance,
25 size_t max_distance,
26 const int* dist_cache) {
27 if (distance <= max_distance) {
28 size_t distance_plus_3 = distance + 3;
29 size_t offset0 = distance_plus_3 - (size_t)dist_cache[0];
30 size_t offset1 = distance_plus_3 - (size_t)dist_cache[1];
31 if (distance == (size_t)dist_cache[0]) {
32 return 0;
33 } else if (distance == (size_t)dist_cache[1]) {
34 return 1;
35 } else if (offset0 < 7) {
36 return (0x9750468 >> (4 * offset0)) & 0xF;
37 } else if (offset1 < 7) {
38 return (0xFDB1ACE >> (4 * offset1)) & 0xF;
39 } else if (distance == (size_t)dist_cache[2]) {
40 return 2;
41 } else if (distance == (size_t)dist_cache[3]) {
42 return 3;
43 }
44 }
45 return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1;
46 }
47
48 #define EXPAND_CAT(a, b) CAT(a, b)
49 #define CAT(a, b) a ## b
50 #define FN(X) EXPAND_CAT(X, HASHER())
51
52 #define HASHER() H2
53 /* NOLINTNEXTLINE(build/include) */
54 #include "./backward_references_inc.h"
55 #undef HASHER
56
57 #define HASHER() H3
58 /* NOLINTNEXTLINE(build/include) */
59 #include "./backward_references_inc.h"
60 #undef HASHER
61
62 #define HASHER() H4
63 /* NOLINTNEXTLINE(build/include) */
64 #include "./backward_references_inc.h"
65 #undef HASHER
66
67 #define HASHER() H5
68 /* NOLINTNEXTLINE(build/include) */
69 #include "./backward_references_inc.h"
70 #undef HASHER
71
72 #define HASHER() H6
73 /* NOLINTNEXTLINE(build/include) */
74 #include "./backward_references_inc.h"
75 #undef HASHER
76
77 #define HASHER() H40
78 /* NOLINTNEXTLINE(build/include) */
79 #include "./backward_references_inc.h"
80 #undef HASHER
81
82 #define HASHER() H41
83 /* NOLINTNEXTLINE(build/include) */
84 #include "./backward_references_inc.h"
85 #undef HASHER
86
87 #define HASHER() H42
88 /* NOLINTNEXTLINE(build/include) */
89 #include "./backward_references_inc.h"
90 #undef HASHER
91
92 #define HASHER() H54
93 /* NOLINTNEXTLINE(build/include) */
94 #include "./backward_references_inc.h"
95 #undef HASHER
96
97 #undef FN
98 #undef CAT
99 #undef EXPAND_CAT
100
BrotliCreateBackwardReferences(const BrotliDictionary * dictionary,size_t num_bytes,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,const BrotliEncoderParams * params,HasherHandle hasher,int * dist_cache,size_t * last_insert_len,Command * commands,size_t * num_commands,size_t * num_literals)101 void BrotliCreateBackwardReferences(const BrotliDictionary* dictionary,
102 size_t num_bytes,
103 size_t position,
104 const uint8_t* ringbuffer,
105 size_t ringbuffer_mask,
106 const BrotliEncoderParams* params,
107 HasherHandle hasher,
108 int* dist_cache,
109 size_t* last_insert_len,
110 Command* commands,
111 size_t* num_commands,
112 size_t* num_literals) {
113 switch (params->hasher.type) {
114 #define CASE_(N) \
115 case N: \
116 CreateBackwardReferencesH ## N(dictionary, \
117 kStaticDictionaryHash, num_bytes, position, ringbuffer, \
118 ringbuffer_mask, params, hasher, dist_cache, \
119 last_insert_len, commands, num_commands, num_literals); \
120 break;
121 FOR_GENERIC_HASHERS(CASE_)
122 #undef CASE_
123 default:
124 break;
125 }
126 }
127
128 #if defined(__cplusplus) || defined(c_plusplus)
129 } /* extern "C" */
130 #endif
131