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