Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/brotli/c/enc/backward_references.c
Line
Count
Source (jump to first uncovered line)
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 <brotli/types.h>
12
13
#include "../common/constants.h"
14
#include "../common/dictionary.h"
15
#include "../common/platform.h"
16
#include "command.h"
17
#include "compound_dictionary.h"
18
#include "dictionary_hash.h"
19
#include "encoder_dict.h"
20
#include "memory.h"
21
#include "quality.h"
22
23
#if defined(__cplusplus) || defined(c_plusplus)
24
extern "C" {
25
#endif
26
27
static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance,
28
                                                size_t max_distance,
29
0
                                                const int* dist_cache) {
30
0
  if (distance <= max_distance) {
31
0
    size_t distance_plus_3 = distance + 3;
32
0
    size_t offset0 = distance_plus_3 - (size_t)dist_cache[0];
33
0
    size_t offset1 = distance_plus_3 - (size_t)dist_cache[1];
34
0
    if (distance == (size_t)dist_cache[0]) {
35
0
      return 0;
36
0
    } else if (distance == (size_t)dist_cache[1]) {
37
0
      return 1;
38
0
    } else if (offset0 < 7) {
39
0
      return (0x9750468 >> (4 * offset0)) & 0xF;
40
0
    } else if (offset1 < 7) {
41
0
      return (0xFDB1ACE >> (4 * offset1)) & 0xF;
42
0
    } else if (distance == (size_t)dist_cache[2]) {
43
0
      return 2;
44
0
    } else if (distance == (size_t)dist_cache[3]) {
45
0
      return 3;
46
0
    }
47
0
  }
48
0
  return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1;
49
0
}
50
51
0
#define EXPAND_CAT(a, b) CAT(a, b)
52
0
#define CAT(a, b) a ## b
53
0
#define FN(X) EXPAND_CAT(X, HASHER())
54
#define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER()))
55
56
#define PREFIX() N
57
0
#define ENABLE_COMPOUND_DICTIONARY 0
58
59
0
#define HASHER() H2
60
/* NOLINTNEXTLINE(build/include) */
61
#include "backward_references_inc.h"
62
#undef HASHER
63
64
0
#define HASHER() H3
65
/* NOLINTNEXTLINE(build/include) */
66
#include "backward_references_inc.h"
67
#undef HASHER
68
69
0
#define HASHER() H4
70
/* NOLINTNEXTLINE(build/include) */
71
#include "backward_references_inc.h"
72
#undef HASHER
73
74
0
#define HASHER() H5
75
/* NOLINTNEXTLINE(build/include) */
76
#include "backward_references_inc.h"
77
#undef HASHER
78
79
0
#define HASHER() H6
80
/* NOLINTNEXTLINE(build/include) */
81
#include "backward_references_inc.h"
82
#undef HASHER
83
84
0
#define HASHER() H40
85
/* NOLINTNEXTLINE(build/include) */
86
#include "backward_references_inc.h"
87
#undef HASHER
88
89
0
#define HASHER() H41
90
/* NOLINTNEXTLINE(build/include) */
91
#include "backward_references_inc.h"
92
#undef HASHER
93
94
0
#define HASHER() H42
95
/* NOLINTNEXTLINE(build/include) */
96
#include "backward_references_inc.h"
97
#undef HASHER
98
99
0
#define HASHER() H54
100
/* NOLINTNEXTLINE(build/include) */
101
#include "backward_references_inc.h"
102
#undef HASHER
103
104
0
#define HASHER() H35
105
/* NOLINTNEXTLINE(build/include) */
106
#include "backward_references_inc.h"
107
#undef HASHER
108
109
0
#define HASHER() H55
110
/* NOLINTNEXTLINE(build/include) */
111
#include "backward_references_inc.h"
112
#undef HASHER
113
114
0
#define HASHER() H65
115
/* NOLINTNEXTLINE(build/include) */
116
#include "backward_references_inc.h"
117
#undef HASHER
118
119
#undef ENABLE_COMPOUND_DICTIONARY
120
#undef PREFIX
121
#define PREFIX() D
122
0
#define ENABLE_COMPOUND_DICTIONARY 1
123
124
0
#define HASHER() H5
125
/* NOLINTNEXTLINE(build/include) */
126
#include "backward_references_inc.h"
127
#undef HASHER
128
0
#define HASHER() H6
129
/* NOLINTNEXTLINE(build/include) */
130
#include "backward_references_inc.h"
131
#undef HASHER
132
0
#define HASHER() H40
133
/* NOLINTNEXTLINE(build/include) */
134
#include "backward_references_inc.h"
135
#undef HASHER
136
0
#define HASHER() H41
137
/* NOLINTNEXTLINE(build/include) */
138
#include "backward_references_inc.h"
139
#undef HASHER
140
0
#define HASHER() H42
141
/* NOLINTNEXTLINE(build/include) */
142
#include "backward_references_inc.h"
143
#undef HASHER
144
0
#define HASHER() H55
145
/* NOLINTNEXTLINE(build/include) */
146
#include "backward_references_inc.h"
147
#undef HASHER
148
0
#define HASHER() H65
149
/* NOLINTNEXTLINE(build/include) */
150
#include "backward_references_inc.h"
151
#undef HASHER
152
153
#undef ENABLE_COMPOUND_DICTIONARY
154
#undef PREFIX
155
156
#undef EXPORT_FN
157
#undef FN
158
#undef CAT
159
#undef EXPAND_CAT
160
161
void BrotliCreateBackwardReferences(size_t num_bytes,
162
    size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
163
    ContextLut literal_context_lut, const BrotliEncoderParams* params,
164
    Hasher* hasher, int* dist_cache, size_t* last_insert_len,
165
0
    Command* commands, size_t* num_commands, size_t* num_literals) {
166
0
  if (params->dictionary.compound.num_chunks != 0) {
167
0
    switch (params->hasher.type) {
168
0
#define CASE_(N)                                                    \
169
0
      case N:                                                       \
170
0
        CreateBackwardReferencesDH ## N(num_bytes,                  \
171
0
            position, ringbuffer, ringbuffer_mask,                  \
172
0
            literal_context_lut, params, hasher, dist_cache,        \
173
0
            last_insert_len, commands, num_commands, num_literals); \
174
0
        return;
175
0
      CASE_(5)
176
0
      CASE_(6)
177
0
      CASE_(40)
178
0
      CASE_(41)
179
0
      CASE_(42)
180
0
      CASE_(55)
181
0
      CASE_(65)
182
0
#undef CASE_
183
0
      default:
184
0
        BROTLI_DCHECK(false);
185
0
        break;
186
0
    }
187
0
  }
188
189
0
  switch (params->hasher.type) {
190
0
#define CASE_(N)                                                  \
191
0
    case N:                                                       \
192
0
      CreateBackwardReferencesNH ## N(num_bytes,                  \
193
0
          position, ringbuffer, ringbuffer_mask,                  \
194
0
          literal_context_lut, params, hasher, dist_cache,        \
195
0
          last_insert_len, commands, num_commands, num_literals); \
196
0
      return;
197
0
    FOR_GENERIC_HASHERS(CASE_)
198
0
#undef CASE_
199
0
    default:
200
0
      BROTLI_DCHECK(false);
201
0
      break;
202
0
  }
203
0
}
204
205
#if defined(__cplusplus) || defined(c_plusplus)
206
}  /* extern "C" */
207
#endif