diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2021-07-23 16:43:57 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2021-07-23 16:43:57 +0000 |
commit | dd6bb6c6b8298ebeff2d1882becb36580b969d6f (patch) | |
tree | 3d922a5791e7e34e2b041ea5f3489360bfa798e1 /src/libstddjb/cdb_find.c | |
parent | 122f9363682e5de8ce4056c4c05c1eaf8935cf19 (diff) | |
download | skalibs-dd6bb6c6b8298ebeff2d1882becb36580b969d6f.tar.xz |
New 2.11.0.0 branch with several modifications
- libbiguint removed
- cdb_make changed to cdbmake (because different ui)
- cdb redesigned
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb/cdb_find.c')
-rw-r--r-- | src/libstddjb/cdb_find.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/libstddjb/cdb_find.c b/src/libstddjb/cdb_find.c new file mode 100644 index 0000000..a9c66e3 --- /dev/null +++ b/src/libstddjb/cdb_find.c @@ -0,0 +1,57 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> + +#include <skalibs/uint32.h> +#include <skalibs/cdb.h> +#include "cdb-internal.h" + +int cdb_find (cdb const *c, cdb_reader *d, cdb_data *out, char const *key, uint32_t len) +{ + if (!d->loop) + { + uint32_t u = cdb_hash(key, len) ; + char const *p = cdb_p(c, 8, (u << 3) & 2047) ; + if (!p) return -1 ; + uint32_unpack(p + 4, &d->hslots) ; + if (!d->hslots) return 0 ; + uint32_unpack(p, &d->hpos) ; + d->khash = u ; + u >>= 8 ; + u %= d->hslots ; + u <<= 3 ; + d->kpos = d->hpos + u ; + } + + while (d->loop < d->hslots) + { + uint32_t pos, u ; + char const *p = cdb_p(c, 8, d->kpos) ; + if (!p) return -1 ; + uint32_unpack(p + 4, &pos) ; + if (!pos) return 0 ; + d->loop++ ; + d->kpos += 8 ; + if (d->kpos == d->hpos + (d->hslots << 3)) d->kpos = d->hpos ; + uint32_unpack(p, &u) ; + if (u == d->khash) + { + p = cdb_p(c, 8, pos) ; + if (!p) return -1 ; + uint32_unpack(p, &u) ; + if (u == len) + { + char const *k = cdb_p(c, len, pos + 8) ; + if (!k) return -1 ; + if (!memcmp(key, k, len)) + { + uint32_unpack(p + 4, &out->len) ; + out->s = c->map + pos + 8 + len ; + return 1 ; + } + } + } + } + return 0 ; +} |