Branch data Line data Source code
1 : : /* cdb_findnext.c: sequential cdb_find routines
2 : : *
3 : : * This file is a part of tinycdb package.
4 : : * Copyright (C) 2001-2023 Michael Tokarev <mjt+cdb@corpit.ru>
5 : : *
6 : : * Permission is hereby granted, free of charge, to any person obtaining a
7 : : * copy of this software and associated documentation files (the "Software"),
8 : : * to deal in the Software without restriction, including without limitation
9 : : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 : : * and/or sell copies of the Software, and to permit persons to whom the
11 : : * Software is furnished to do so, subject to the following conditions:
12 : : *
13 : : * The above copyright notice and this permission notice shall be included
14 : : * in all copies or substantial portions of the Software.
15 : : *
16 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 : : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 : : * DEALINGS IN THE SOFTWARE.
23 : : */
24 : :
25 : : /* see cdb_find.c for comments */
26 : :
27 : : #include "cdb_int.h"
28 : :
29 : : int
30 : 0 : cdb_findinit(struct cdb_find *cdbfp, struct cdb *cdbp,
31 : : const void *key, unsigned klen)
32 : : {
33 : : unsigned n, pos;
34 : :
35 : 0 : cdbfp->cdb_cdbp = cdbp;
36 : 0 : cdbfp->cdb_key = key;
37 : 0 : cdbfp->cdb_klen = klen;
38 : 0 : cdbfp->cdb_hval = cdb_hash(key, klen);
39 : :
40 : 0 : cdbfp->cdb_htp = cdbp->cdb_mem + ((cdbfp->cdb_hval << 3) & 2047);
41 : 0 : n = cdb_unpack(cdbfp->cdb_htp + 4);
42 : 0 : cdbfp->cdb_httodo = n << 3;
43 [ # # ]: 0 : if (!n)
44 : 0 : return 0;
45 : 0 : pos = cdb_unpack(cdbfp->cdb_htp);
46 [ # # ]: 0 : if (n > (cdbp->cdb_fsize >> 3)
47 [ # # ]: 0 : || pos < cdbp->cdb_dend
48 [ # # ]: 0 : || pos > cdbp->cdb_fsize
49 [ # # ]: 0 : || cdbfp->cdb_httodo > cdbp->cdb_fsize - pos)
50 : 0 : return errno = EPROTO, -1;
51 : :
52 : 0 : cdbfp->cdb_htab = cdbp->cdb_mem + pos;
53 : 0 : cdbfp->cdb_htend = cdbfp->cdb_htab + cdbfp->cdb_httodo;
54 : 0 : cdbfp->cdb_htp = cdbfp->cdb_htab + (((cdbfp->cdb_hval >> 8) % n) << 3);
55 : :
56 : 0 : return 1;
57 : : }
58 : :
59 : : int
60 : 0 : cdb_findnext(struct cdb_find *cdbfp) {
61 : 0 : struct cdb *cdbp = cdbfp->cdb_cdbp;
62 : : unsigned pos, n;
63 : 0 : unsigned klen = cdbfp->cdb_klen;
64 : :
65 [ # # ]: 0 : while(cdbfp->cdb_httodo) {
66 : 0 : pos = cdb_unpack(cdbfp->cdb_htp + 4);
67 [ # # ]: 0 : if (!pos)
68 : 0 : return 0;
69 : 0 : n = cdb_unpack(cdbfp->cdb_htp) == cdbfp->cdb_hval;
70 [ # # ]: 0 : if ((cdbfp->cdb_htp += 8) >= cdbfp->cdb_htend)
71 : 0 : cdbfp->cdb_htp = cdbfp->cdb_htab;
72 : 0 : cdbfp->cdb_httodo -= 8;
73 [ # # ]: 0 : if (n) {
74 [ # # ]: 0 : if (pos > cdbp->cdb_fsize - 8)
75 : 0 : return errno = EPROTO, -1;
76 [ # # ]: 0 : if (cdb_unpack(cdbp->cdb_mem + pos) == klen) {
77 [ # # ]: 0 : if (cdbp->cdb_fsize - klen < pos + 8)
78 : 0 : return errno = EPROTO, -1;
79 : 0 : if (memcmp(cdbfp->cdb_key,
80 [ # # ]: 0 : cdbp->cdb_mem + pos + 8, klen) == 0) {
81 : 0 : n = cdb_unpack(cdbp->cdb_mem + pos + 4);
82 : 0 : pos += 8;
83 [ # # ]: 0 : if (cdbp->cdb_fsize < n ||
84 [ # # ]: 0 : cdbp->cdb_fsize - n < pos + klen)
85 : 0 : return errno = EPROTO, -1;
86 : 0 : cdbp->cdb_kpos = pos;
87 : 0 : cdbp->cdb_klen = klen;
88 : 0 : cdbp->cdb_vpos = pos + klen;
89 : 0 : cdbp->cdb_vlen = n;
90 : 0 : return 1;
91 : : }
92 : : }
93 : : }
94 : : }
95 : :
96 : 0 : return 0;
97 : :
98 : : }
|