Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/brotli/c/enc/compound_dictionary.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2017 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
#include "compound_dictionary.h"
8
9
#include <brotli/types.h>
10
11
#include "../common/platform.h"
12
#include "memory.h"
13
#include "quality.h"
14
15
static PreparedDictionary* CreatePreparedDictionaryWithParams(MemoryManager* m,
16
    const uint8_t* source, size_t source_size, uint32_t bucket_bits,
17
0
    uint32_t slot_bits, uint32_t hash_bits, uint16_t bucket_limit) {
18
  /* Step 1: create "bloated" hasher. */
19
0
  uint32_t num_slots = 1u << slot_bits;
20
0
  uint32_t num_buckets = 1u << bucket_bits;
21
0
  uint32_t hash_shift = 64u - bucket_bits;
22
0
  uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
23
0
  uint32_t slot_mask = num_slots - 1;
24
0
  size_t alloc_size = (sizeof(uint32_t) << slot_bits) +
25
0
      (sizeof(uint32_t) << slot_bits) +
26
0
      (sizeof(uint16_t) << bucket_bits) +
27
0
      (sizeof(uint32_t) << bucket_bits) +
28
0
      (sizeof(uint32_t) * source_size);
29
0
  uint8_t* flat = NULL;
30
0
  PreparedDictionary* result = NULL;
31
0
  uint16_t* num = NULL;
32
0
  uint32_t* bucket_heads = NULL;
33
0
  uint32_t* next_bucket = NULL;
34
0
  uint32_t* slot_offsets = NULL;
35
0
  uint16_t* heads = NULL;
36
0
  uint32_t* items = NULL;
37
0
  uint8_t** source_ref = NULL;
38
0
  uint32_t i;
39
0
  uint32_t* slot_size = NULL;
40
0
  uint32_t* slot_limit = NULL;
41
0
  uint32_t total_items = 0;
42
0
  if (slot_bits > 16) return NULL;
43
0
  if (slot_bits > bucket_bits) return NULL;
44
0
  if (bucket_bits - slot_bits >= 16) return NULL;
45
46
0
  flat = BROTLI_ALLOC(m, uint8_t, alloc_size);
47
0
  if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(flat)) return NULL;
48
49
0
  slot_size = (uint32_t*)flat;
50
0
  slot_limit = (uint32_t*)(&slot_size[num_slots]);
51
0
  num = (uint16_t*)(&slot_limit[num_slots]);
52
0
  bucket_heads = (uint32_t*)(&num[num_buckets]);
53
0
  next_bucket = (uint32_t*)(&bucket_heads[num_buckets]);
54
0
  memset(num, 0, num_buckets * sizeof(num[0]));
55
56
  /* TODO(eustas): apply custom "store" order. */
57
0
  for (i = 0; i + 7 < source_size; ++i) {
58
0
    const uint64_t h = (BROTLI_UNALIGNED_LOAD64LE(&source[i]) & hash_mask) *
59
0
        kPreparedDictionaryHashMul64Long;
60
0
    const uint32_t key = (uint32_t)(h >> hash_shift);
61
0
    uint16_t count = num[key];
62
0
    next_bucket[i] = (count == 0) ? ((uint32_t)(-1)) : bucket_heads[key];
63
0
    bucket_heads[key] = i;
64
0
    count++;
65
0
    if (count > bucket_limit) count = bucket_limit;
66
0
    num[key] = count;
67
0
  }
68
69
  /* Step 2: find slot limits. */
70
0
  for (i = 0; i < num_slots; ++i) {
71
0
    BROTLI_BOOL overflow = BROTLI_FALSE;
72
0
    slot_limit[i] = bucket_limit;
73
0
    while (BROTLI_TRUE) {
74
0
      uint32_t limit = slot_limit[i];
75
0
      size_t j;
76
0
      uint32_t count = 0;
77
0
      overflow = BROTLI_FALSE;
78
0
      for (j = i; j < num_buckets; j += num_slots) {
79
0
        uint32_t size = num[j];
80
        /* Last chain may span behind 64K limit; overflow happens only if
81
           we are about to use 0xFFFF+ as item offset. */
82
0
        if (count >= 0xFFFF) {
83
0
          overflow = BROTLI_TRUE;
84
0
          break;
85
0
        }
86
0
        if (size > limit) size = limit;
87
0
        count += size;
88
0
      }
89
0
      if (!overflow) {
90
0
        slot_size[i] = count;
91
0
        total_items += count;
92
0
        break;
93
0
      }
94
0
      slot_limit[i]--;
95
0
    }
96
0
  }
97
98
  /* Step 3: transfer data to "slim" hasher. */
99
0
  alloc_size = sizeof(PreparedDictionary) + (sizeof(uint32_t) << slot_bits) +
100
0
      (sizeof(uint16_t) << bucket_bits) + (sizeof(uint32_t) * total_items) +
101
0
      sizeof(uint8_t*);
102
103
0
  result = (PreparedDictionary*)BROTLI_ALLOC(m, uint8_t, alloc_size);
104
0
  if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(result)) {
105
0
    BROTLI_FREE(m, flat);
106
0
    return NULL;
107
0
  }
108
0
  slot_offsets = (uint32_t*)(&result[1]);
109
0
  heads = (uint16_t*)(&slot_offsets[num_slots]);
110
0
  items = (uint32_t*)(&heads[num_buckets]);
111
0
  source_ref = (uint8_t**)(&items[total_items]);
112
113
0
  result->magic = kLeanPreparedDictionaryMagic;
114
0
  result->num_items = total_items;
115
0
  result->source_size = (uint32_t)source_size;
116
0
  result->hash_bits = hash_bits;
117
0
  result->bucket_bits = bucket_bits;
118
0
  result->slot_bits = slot_bits;
119
0
  BROTLI_UNALIGNED_STORE_PTR(source_ref, source);
120
121
0
  total_items = 0;
122
0
  for (i = 0; i < num_slots; ++i) {
123
0
    slot_offsets[i] = total_items;
124
0
    total_items += slot_size[i];
125
0
    slot_size[i] = 0;
126
0
  }
127
0
  for (i = 0; i < num_buckets; ++i) {
128
0
    uint32_t slot = i & slot_mask;
129
0
    uint32_t count = num[i];
130
0
    uint32_t pos;
131
0
    size_t j;
132
0
    size_t cursor = slot_size[slot];
133
0
    if (count > slot_limit[slot]) count = slot_limit[slot];
134
0
    if (count == 0) {
135
0
      heads[i] = 0xFFFF;
136
0
      continue;
137
0
    }
138
0
    heads[i] = (uint16_t)cursor;
139
0
    cursor += slot_offsets[slot];
140
0
    slot_size[slot] += count;
141
0
    pos = bucket_heads[i];
142
0
    for (j = 0; j < count; j++) {
143
0
      items[cursor++] = pos;
144
0
      pos = next_bucket[pos];
145
0
    }
146
0
    items[cursor - 1] |= 0x80000000;
147
0
  }
148
149
0
  BROTLI_FREE(m, flat);
150
0
  return result;
151
0
}
152
153
PreparedDictionary* CreatePreparedDictionary(MemoryManager* m,
154
0
    const uint8_t* source, size_t source_size) {
155
0
  uint32_t bucket_bits = 17;
156
0
  uint32_t slot_bits = 7;
157
0
  uint32_t hash_bits = 40;
158
0
  uint16_t bucket_limit = 32;
159
0
  size_t volume = 16u << bucket_bits;
160
  /* Tune parameters to fit dictionary size. */
161
0
  while (volume < source_size && bucket_bits < 22) {
162
0
    bucket_bits++;
163
0
    slot_bits++;
164
0
    volume <<= 1;
165
0
  }
166
0
  return CreatePreparedDictionaryWithParams(m,
167
0
      source, source_size, bucket_bits, slot_bits, hash_bits, bucket_limit);
168
0
}
169
170
void DestroyPreparedDictionary(MemoryManager* m,
171
0
    PreparedDictionary* dictionary) {
172
0
  if (!dictionary) return;
173
0
  BROTLI_FREE(m, dictionary);
174
0
}
175
176
BROTLI_BOOL AttachPreparedDictionary(
177
0
    CompoundDictionary* compound, const PreparedDictionary* dictionary) {
178
0
  size_t length = 0;
179
0
  size_t index = 0;
180
181
0
  if (compound->num_chunks == SHARED_BROTLI_MAX_COMPOUND_DICTS) {
182
0
    return BROTLI_FALSE;
183
0
  }
184
185
0
  if (!dictionary) return BROTLI_FALSE;
186
187
0
  length = dictionary->source_size;
188
0
  index = compound->num_chunks;
189
0
  compound->total_size += length;
190
0
  compound->chunks[index] = dictionary;
191
0
  compound->chunk_offsets[index + 1] = compound->total_size;
192
0
  {
193
0
    uint32_t* slot_offsets = (uint32_t*)(&dictionary[1]);
194
0
    uint16_t* heads = (uint16_t*)(&slot_offsets[1u << dictionary->slot_bits]);
195
0
    uint32_t* items = (uint32_t*)(&heads[1u << dictionary->bucket_bits]);
196
0
    const void* tail = (void*)&items[dictionary->num_items];
197
0
    if (dictionary->magic == kPreparedDictionaryMagic) {
198
0
      compound->chunk_source[index] = (const uint8_t*)tail;
199
0
    } else {
200
      /* dictionary->magic == kLeanPreparedDictionaryMagic */
201
0
      compound->chunk_source[index] =
202
0
          (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
203
0
    }
204
0
  }
205
0
  compound->num_chunks++;
206
0
  return BROTLI_TRUE;
207
0
}