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