1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* critbit89 - A crit-bit tree implementation for strings in C89
* Written by Jonas Gehring <jonas@jgehring.net>
* Implemented key-value storing by Marek Vavrusa <marek.vavrusa@nic.cz>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* The code makes the assumption that malloc returns pointers aligned at at
* least a two-byte boundary. Since the C standard requires that malloc return
* pointers that can store any type, there are no commonly-used toolchains for
* which this assumption is false.
*
* See https://github.com/agl/critbit/blob/master/critbit.pdf for reference.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "map.h"
#include "lib/utils.h"
/* Exports */
#if defined _WIN32 || defined __CYGWIN__
#define EXPORT __attribute__ ((dllexport))
#else
#define EXPORT __attribute__ ((visibility ("default")))
#endif
#ifdef _MSC_VER /* MSVC */
typedef unsigned __int8 uint8_t;
typedef unsigned __int32 uint32_t;
#ifdef _WIN64
typedef signed __int64 intptr_t;
#else
typedef _W64 signed int intptr_t;
#endif
#else /* Not MSVC */
#include <stdint.h>
#endif
typedef struct {
void* value;
uint8_t key[];
} cb_data_t;
typedef struct {
void *child[2];
uint32_t byte;
uint8_t otherbits;
} cb_node_t;
/* Return true if ptr is internal node. */
static inline int ref_is_internal(const uint8_t *p)
{
return 1 & (intptr_t)p;
}
/* Get internal node. */
static inline cb_node_t *ref_get_internal(uint8_t *p)
{
return (cb_node_t *)(p - 1);
}
/* Static helper functions */
static void cbt_traverse_delete(map_t *map, void *top)
{
uint8_t *p = top;
if (ref_is_internal(p)) {
cb_node_t *q = ref_get_internal(p);
cbt_traverse_delete(map, q->child[0]);
cbt_traverse_delete(map, q->child[1]);
mm_free(map->pool, q);
} else {
mm_free(map->pool, p);
}
}
static int cbt_traverse_prefixed(void *top,
int (*callback)(const char *, void *, void *), void *baton)
{
uint8_t *p = top;
cb_data_t *x = (cb_data_t *)top;
if (ref_is_internal(p)) {
cb_node_t *q = ref_get_internal(p);
int ret = 0;
ret = cbt_traverse_prefixed(q->child[0], callback, baton);
if (ret != 0) {
return ret;
}
ret = cbt_traverse_prefixed(q->child[1], callback, baton);
if (ret != 0) {
return ret;
}
return 0;
}
return (callback)((const char *)x->key, x->value, baton);
}
static cb_data_t *cbt_make_data(map_t *map, const uint8_t *str, size_t len, void *value)
{
cb_data_t *x = mm_alloc(map->pool, sizeof(cb_data_t) + len);
if (x != NULL) {
x->value = value;
memcpy(x->key, str, len);
}
return x;
}
/*! Like map_contains, but also set the value, if passed and found. */
static int cbt_get(map_t *map, const char *str, void **value)
{
const uint8_t *ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = map->root;
cb_data_t *x = NULL;
if (p == NULL) {
return 0;
}
while (ref_is_internal(p)) {
cb_node_t *q = ref_get_internal(p);
uint8_t c = 0;
int direction;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
x = (cb_data_t *)p;
if (strcmp(str, (const char *)x->key) == 0) {
if (value != NULL) {
*value = x->value;
}
return 1;
}
return 0;
}
/*! Returns non-zero if map contains str */
EXPORT int map_contains(map_t *map, const char *str)
{
return cbt_get(map, str, NULL);
}
EXPORT void *map_get(map_t *map, const char *str)
{
void *v = NULL;
cbt_get(map, str, &v);
return v;
}
EXPORT int map_set(map_t *map, const char *str, void *val)
{
const uint8_t *const ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = map->root;
uint8_t c = 0, *x = NULL;
uint32_t newbyte = 0;
uint32_t newotherbits = 0;
int direction = 0, newdirection = 0;
cb_node_t *newnode = NULL;
cb_data_t *data = NULL;
void **wherep = NULL;
if (p == NULL) {
map->root = cbt_make_data(map, (const uint8_t *)str, ulen + 1, val);
if (map->root == NULL) {
return ENOMEM;
}
return 0;
}
while (ref_is_internal(p)) {
cb_node_t *q = ref_get_internal(p);
c = 0;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
}
data = (cb_data_t *)p;
for (newbyte = 0; newbyte < ulen; ++newbyte) {
if (data->key[newbyte] != ubytes[newbyte]) {
newotherbits = data->key[newbyte] ^ ubytes[newbyte];
goto different_byte_found;
}
}
if (data->key[newbyte] != 0) {
newotherbits = data->key[newbyte];
goto different_byte_found;
}
data->value = val;
return 1;
different_byte_found:
newotherbits |= newotherbits >> 1;
newotherbits |= newotherbits >> 2;
newotherbits |= newotherbits >> 4;
newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255;
c = data->key[newbyte];
newdirection = (1 + (newotherbits | c)) >> 8;
newnode = mm_alloc(map->pool, sizeof(cb_node_t));
if (newnode == NULL) {
return ENOMEM;
}
x = (uint8_t *)cbt_make_data(map, ubytes, ulen + 1, val);
if (x == NULL) {
mm_free(map->pool, newnode);
return ENOMEM;
}
newnode->byte = newbyte;
newnode->otherbits = newotherbits;
newnode->child[1 - newdirection] = x;
/* Insert into map */
wherep = &map->root;
for (;;) {
cb_node_t *q;
p = *wherep;
if (!ref_is_internal(p)) {
break;
}
q = ref_get_internal(p);
if (q->byte > newbyte) {
break;
}
if (q->byte == newbyte && q->otherbits > newotherbits) {
break;
}
c = 0;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
}
newnode->child[newdirection] = *wherep;
*wherep = (void *)(1 + (char *)newnode);
return 0;
}
/*! Deletes str from the map, returns 0 on success */
EXPORT int map_del(map_t *map, const char *str)
{
const uint8_t *ubytes = (void *)str;
const size_t ulen = strlen(str);
uint8_t *p = map->root;
void **wherep = NULL, **whereq = NULL;
cb_node_t *q = NULL;
cb_data_t *data = NULL;
int direction = 0;
if (map->root == NULL) {
return 1;
}
wherep = &map->root;
while (ref_is_internal(p)) {
uint8_t c = 0;
whereq = wherep;
q = ref_get_internal(p);
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
wherep = q->child + direction;
p = *wherep;
}
data = (cb_data_t *)p;
if (strcmp(str, (const char *)data->key) != 0) {
return 1;
}
mm_free(map->pool, p);
if (!whereq) {
map->root = NULL;
return 0;
}
*whereq = q->child[1 - direction];
mm_free(map->pool, q);
return 0;
}
/*! Clears the given map */
EXPORT void map_clear(map_t *map)
{
if (map->root) {
cbt_traverse_delete(map, map->root);
}
map->root = NULL;
}
/*! Calls callback for all strings in map with the given prefix */
EXPORT int map_walk_prefixed(map_t *map, const char *prefix,
int (*callback)(const char *, void *, void *), void *baton)
{
if (!map) {
return 0;
}
const uint8_t *ubytes = (void *)prefix;
const size_t ulen = strlen(prefix);
uint8_t *p = map->root;
uint8_t *top = p;
cb_data_t *data = NULL;
if (p == NULL) {
return 0;
}
while (ref_is_internal(p)) {
cb_node_t *q = ref_get_internal(p);
uint8_t c = 0;
int direction;
if (q->byte < ulen) {
c = ubytes[q->byte];
}
direction = (1 + (q->otherbits | c)) >> 8;
p = q->child[direction];
if (q->byte < ulen) {
top = p;
}
}
data = (cb_data_t *)p;
if (strlen((const char *)data->key) < ulen || memcmp(data->key, prefix, ulen) != 0) {
return 0; /* No strings match */
}
return cbt_traverse_prefixed(top, callback, baton);
}
|