/[LeafOK_CVS]/pvpgn-1.7.4/src/tinycdb/cdb_find.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/tinycdb/cdb_find.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
CVS Tags: pvpgn_1-7-4-0_MIL
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /* cdb_find routine
2     *
3     * This file is a part of tinycdb package by Michael Tokarev, mjt@corpit.ru.
4     * Public domain.
5     */
6    
7     #include "common/setup_before.h"
8     #include "cdb_int.h"
9     #include "common/setup_after.h"
10    
11     int
12     cdb_find(struct cdb *cdbp, const void *key, unsigned klen)
13     {
14     const unsigned char *htp; /* hash table pointer */
15     const unsigned char *htab; /* hash table */
16     const unsigned char *htend; /* end of hash table */
17     unsigned httodo; /* ht bytes left to look */
18     unsigned pos, n;
19    
20     unsigned hval;
21    
22     if (klen >= cdbp->cdb_dend) /* if key size is too large */
23     return 0;
24    
25     hval = cdb_hash(key, klen);
26    
27     /* find (pos,n) hash table to use */
28     /* first 2048 bytes (toc) are always available */
29     /* (hval % 256) * 8 */
30     htp = cdbp->cdb_mem + ((hval << 3) & 2047); /* index in toc (256x8) */
31     n = cdb_unpack(htp + 4); /* table size */
32     if (!n) /* empty table */
33     return 0; /* not found */
34     httodo = n << 3; /* bytes of htab to lookup */
35     pos = cdb_unpack(htp); /* htab position */
36     if (n > (cdbp->cdb_fsize >> 3) /* overflow of httodo ? */
37     || pos < cdbp->cdb_dend /* is htab inside data section ? */
38     || pos > cdbp->cdb_fsize /* htab start within file ? */
39     || httodo > cdbp->cdb_fsize - pos) /* entrie htab within file ? */
40     return errno = EPROTO, -1;
41    
42     htab = cdbp->cdb_mem + pos; /* htab pointer */
43     htend = htab + httodo; /* after end of htab */
44     /* htab starting position: rest of hval modulo htsize, 8bytes per elt */
45     htp = htab + (((hval >> 8) % n) << 3);
46    
47     for(;;) {
48     pos = cdb_unpack(htp + 4); /* record position */
49     if (!pos)
50     return 0;
51     if (cdb_unpack(htp) == hval) {
52     if (pos > cdbp->cdb_dend - 8) /* key+val lengths */
53     return errno = EPROTO, -1;
54     if (cdb_unpack(cdbp->cdb_mem + pos) == klen) {
55     if (cdbp->cdb_dend - klen < pos + 8)
56     return errno = EPROTO, -1;
57     if (memcmp(key, cdbp->cdb_mem + pos + 8, klen) == 0) {
58     n = cdb_unpack(cdbp->cdb_mem + pos + 4);
59     pos += 8;
60     if (cdbp->cdb_dend < n || cdbp->cdb_dend - n < pos + klen)
61     return errno = EPROTO, -1;
62     cdbp->cdb_kpos = pos;
63     cdbp->cdb_klen = klen;
64     cdbp->cdb_vpos = pos + klen;
65     cdbp->cdb_vlen = n;
66     return 1;
67     }
68     }
69     }
70     httodo -= 8;
71     if (!httodo)
72     return 0;
73     if ((htp += 8) >= htend)
74     htp = htab;
75     }
76    
77     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1