Debian Codegraph @

crt/s2n/utils/s2n_map.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "utils/s2n_map.h"
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "api/s2n.h"
22 #include "crypto/s2n_hash.h"
23 #include "error/s2n_errno.h"
24 #include "utils/s2n_blob.h"
25 #include "utils/s2n_map_internal.h"
26 #include "utils/s2n_mem.h"
27 #include "utils/s2n_result.h"
28 #include "utils/s2n_safety.h"
29
30 #define S2N_INITIAL_TABLE_SIZE 1024
31
32 static S2N_RESULT s2n_map_slot(const struct s2n_map *map, struct s2n_blob *key, uint32_t *slot)
33 {
34 RESULT_ENSURE_REF(map);
35 RESULT_ENSURE(map->capacity != 0, S2N_ERR_SAFETY);
36 union {
37 uint8_t u8[32];
38 uint32_t u32[8];
39 } digest;
40
41 DEFER_CLEANUP(struct s2n_hash_state sha256 = { 0 }, s2n_hash_free);
42 RESULT_GUARD_POSIX(s2n_hash_new(&sha256));
43 RESULT_GUARD_POSIX(s2n_hash_init(&sha256, S2N_HASH_SHA256));
44 RESULT_GUARD_POSIX(s2n_hash_update(&sha256, key->data, key->size));
45 RESULT_GUARD_POSIX(s2n_hash_digest(&sha256, digest.u8, sizeof(digest)));
46
47 *slot = digest.u32[0] % map->capacity;
48 return S2N_RESULT_OK;
49 }
50
51 static S2N_RESULT s2n_map_embiggen(struct s2n_map *map, uint32_t capacity)
52 {
53 RESULT_ENSURE_REF(map);
54 struct s2n_blob mem = { 0 };
55 struct s2n_map tmp = { 0 };
56
57 RESULT_ENSURE(!map->immutable, S2N_ERR_MAP_IMMUTABLE);
58
59 RESULT_GUARD_POSIX(s2n_alloc(&mem, (capacity * sizeof(struct s2n_map_entry))));
60 RESULT_GUARD_POSIX(s2n_blob_zero(&mem));
61
62 tmp.capacity = capacity;
63 tmp.size = 0;
64 tmp.table = (void *) mem.data;
65 tmp.immutable = 0;
66
67 for (size_t i = 0; i < map->capacity; i++) {
68 if (map->table[i].key.size) {
69 RESULT_GUARD(s2n_map_add(&tmp, &map->table[i].key, &map->table[i].value));
70 RESULT_GUARD_POSIX(s2n_free(&map->table[i].key));
71 RESULT_GUARD_POSIX(s2n_free(&map->table[i].value));
72 }
73 }
74 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &map->table, map->capacity * sizeof(struct s2n_map_entry)));
75
76 /* Clone the temporary map */
77 map->capacity = tmp.capacity;
78 map->size = tmp.size;
79 map->table = tmp.table;
80 map->immutable = 0;
81
82 return S2N_RESULT_OK;
83 }
84
85 struct s2n_map *s2n_map_new()
86 {
87 return s2n_map_new_with_initial_capacity(S2N_INITIAL_TABLE_SIZE);
88 }
89
90 struct s2n_map *s2n_map_new_with_initial_capacity(uint32_t capacity)
91 {
92 PTR_ENSURE(capacity != 0, S2N_ERR_MAP_INVALID_MAP_SIZE);
93 struct s2n_blob mem = { 0 };
94 struct s2n_map *map = NULL;
95
96 PTR_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_map)));
97
98 map = (void *) mem.data;
99 map->capacity = 0;
100 map->size = 0;
101 map->immutable = 0;
102 map->table = NULL;
103
104 PTR_GUARD_RESULT(s2n_map_embiggen(map, capacity));
105
106 return map;
107 }
108
109 S2N_RESULT s2n_map_add(struct s2n_map *map, struct s2n_blob *key, struct s2n_blob *value)
110 {
111 RESULT_ENSURE_REF(map);
112 RESULT_ENSURE(!map->immutable, S2N_ERR_MAP_IMMUTABLE);
113
114 if (map->capacity < (map->size * 2)) {
115 /* Embiggen the map */
116 RESULT_GUARD(s2n_map_embiggen(map, map->capacity * 2));
117 }
118
119 uint32_t slot = 0;
120 RESULT_GUARD(s2n_map_slot(map, key, &slot));
121
122 /* Linear probing until we find an empty slot */
123 while (map->table[slot].key.size) {
124 if (key->size != map->table[slot].key.size || memcmp(key->data, map->table[slot].key.data, key->size)) {
125 slot++;
126 slot %= map->capacity;
127 continue;
128 }
129
130 /* We found a duplicate key */
131 RESULT_BAIL(S2N_ERR_MAP_DUPLICATE);
132 }
133
134 RESULT_GUARD_POSIX(s2n_dup(key, &map->table[slot].key));
135 RESULT_GUARD_POSIX(s2n_dup(value, &map->table[slot].value));
136 map->size++;
137
138 return S2N_RESULT_OK;
139 }
140
141 S2N_RESULT s2n_map_put(struct s2n_map *map, struct s2n_blob *key, struct s2n_blob *value)
142 {
143 RESULT_ENSURE_REF(map);
144 RESULT_ENSURE(!map->immutable, S2N_ERR_MAP_IMMUTABLE);
145
146 if (map->capacity < (map->size * 2)) {
147 /* Embiggen the map */
148 RESULT_GUARD(s2n_map_embiggen(map, map->capacity * 2));
149 }
150
151 uint32_t slot = 0;
152 RESULT_GUARD(s2n_map_slot(map, key, &slot));
153
154 /* Linear probing until we find an empty slot */
155 while (map->table[slot].key.size) {
156 if (key->size != map->table[slot].key.size || memcmp(key->data, map->table[slot].key.data, key->size)) {
157 slot++;
158 slot %= map->capacity;
159 continue;
160 }
161
162 /* We found a duplicate key that will be overwritten */
163 RESULT_GUARD_POSIX(s2n_free(&map->table[slot].key));
164 RESULT_GUARD_POSIX(s2n_free(&map->table[slot].value));
165 map->size--;
166 break;
167 }
168
169 RESULT_GUARD_POSIX(s2n_dup(key, &map->table[slot].key));
170 RESULT_GUARD_POSIX(s2n_dup(value, &map->table[slot].value));
171 map->size++;
172
173 return S2N_RESULT_OK;
174 }
175
176 S2N_RESULT s2n_map_complete(struct s2n_map *map)
177 {
178 RESULT_ENSURE_REF(map);
179 map->immutable = 1;
180
181 return S2N_RESULT_OK;
182 }
183
184 S2N_RESULT s2n_map_unlock(struct s2n_map *map)
185 {
186 RESULT_ENSURE_REF(map);
187 map->immutable = 0;
188
189 return S2N_RESULT_OK;
190 }
191
192 S2N_RESULT s2n_map_lookup(const struct s2n_map *map, struct s2n_blob *key, struct s2n_blob *value, bool *key_found)
193 {
194 RESULT_ENSURE_REF(map);
195 RESULT_ENSURE(map->immutable, S2N_ERR_MAP_MUTABLE);
196
197 uint32_t slot = 0;
198 RESULT_GUARD(s2n_map_slot(map, key, &slot));
199 const uint32_t initial_slot = slot;
200
201 while (map->table[slot].key.size) {
202 if (key->size != map->table[slot].key.size || memcmp(key->data, map->table[slot].key.data, key->size)) {
203 slot++;
204 slot %= map->capacity;
205 /* We went over all the slots but found no match */
206 if (slot == initial_slot) {
207 break;
208 }
209 continue;
210 }
211
212 /* We found a match */
213 struct s2n_blob entry_value = map->table[slot].value;
214 RESULT_GUARD_POSIX(s2n_blob_init(value, entry_value.data, entry_value.size));
215
216 *key_found = true;
217
218 return S2N_RESULT_OK;
219 }
220
221 *key_found = false;
222
223 return S2N_RESULT_OK;
224 }
225
226 S2N_RESULT s2n_map_free(struct s2n_map *map)
227 {
228 if (map == NULL) {
229 return S2N_RESULT_OK;
230 }
231
232 /* Free the keys and values */
233 /* cppcheck has a false positive warning for checking the pointer here */
234 /* cppcheck-suppress nullPointerRedundantCheck */
235 for (size_t i = 0; i < map->capacity; i++) {
236 if (map->table[i].key.size) {
237 RESULT_GUARD_POSIX(s2n_free(&map->table[i].key));
238 RESULT_GUARD_POSIX(s2n_free(&map->table[i].value));
239 }
240 }
241
242 /* Free the table */
243 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &map->table, map->capacity * sizeof(struct s2n_map_entry)));
244
245 /* And finally the map */
246 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &map, sizeof(struct s2n_map)));
247
248 return S2N_RESULT_OK;
249 }
250
251 S2N_RESULT s2n_map_size(struct s2n_map *map, uint32_t *size)
252 {
253 RESULT_ENSURE_REF(map);
254 *size = map->size;
255 return S2N_RESULT_OK;
256 }
257
258 /* Update the internal state so that `current_index` will point to the next value
259 * in the table or set iter->consumed equal to true if there are no more elements
260 * in the map.
261 */
262 S2N_RESULT s2n_map_iterator_advance(struct s2n_map_iterator *iter)
263 {
264 RESULT_ENSURE_REF(iter);
265 RESULT_ENSURE_REF(iter->map);
266 RESULT_ENSURE(s2n_map_iterator_has_next(iter), S2N_ERR_ARRAY_INDEX_OOB);
267
268 iter->current_index++;
269 while (iter->current_index < iter->map->capacity) {
270 /* a value was found in the map */
271 if (iter->map->table[iter->current_index].key.size) {
272 return S2N_RESULT_OK;
273 }
274 iter->current_index++;
275 }
276 /* no more values were found in the map */
277 iter->consumed = true;
278 return S2N_RESULT_OK;
279 }
280
281 S2N_RESULT s2n_map_iterator_init(struct s2n_map_iterator *iter, const struct s2n_map *map)
282 {
283 RESULT_ENSURE_REF(iter);
284 RESULT_ENSURE_REF(map);
285 RESULT_ENSURE(map->immutable, S2N_ERR_MAP_MUTABLE);
286
287 iter->map = map;
288 iter->current_index = 0;
289
290 /* Advance the index to point to the first value in the map. This isn't
291 * necessary if the slot at index 0 already contains a value.
292 */
293 if (iter->map->table[0].key.size == 0) {
294 RESULT_GUARD(s2n_map_iterator_advance(iter));
295 }
296
297 return S2N_RESULT_OK;
298 }
299
300 S2N_RESULT s2n_map_iterator_next(struct s2n_map_iterator *iter, struct s2n_blob *value)
301 {
302 RESULT_ENSURE_REF(iter);
303 RESULT_ENSURE_REF(iter->map);
304 RESULT_ENSURE(iter->map->immutable, S2N_ERR_MAP_MUTABLE);
305 RESULT_ENSURE(s2n_map_iterator_has_next(iter), S2N_ERR_ARRAY_INDEX_OOB);
306
307 RESULT_ENSURE(iter->current_index < iter->map->capacity, S2N_ERR_ARRAY_INDEX_OOB);
308 struct s2n_blob entry_value = iter->map->table[iter->current_index].value;
309 RESULT_GUARD_POSIX(s2n_blob_init(value, entry_value.data, entry_value.size));
310
311 RESULT_GUARD(s2n_map_iterator_advance(iter));
312
313 return S2N_RESULT_OK;
314 }
315
316 bool s2n_map_iterator_has_next(const struct s2n_map_iterator *iter)
317 {
318 return iter && !iter->consumed;
319 }