2 /* Adapted from DJB's original cdb-0.75 package */
13 #define EPROTO -15 /* cdb 0.75's default for PROTOless systems */
17 void cdb_free(struct cdb *c)
20 munmap(c->map, c->size);
25 void cdb_findstart(struct cdb *c)
30 void cdb_init(struct cdb *c, int fd)
39 if (fstat(fd, &st) == 0)
40 if ((size_t) st.st_size <= 0xffffffff) {
41 x = (char *) mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
49 int cdb_read(struct cdb *c, char *buf, unsigned int len, uint32 pos)
52 if ((pos > c->size) || (c->size - pos < len)) {
55 memcpy(buf, c->map + pos, len);
57 if (lseek(c->fd, pos, SEEK_SET) == -1) {
60 /* if (seek_set(c->fd,pos) == -1) return -1; */
64 r = read(c->fd, buf, len);
65 } while ((r == -1) && (errno == EINTR));
83 static int match(struct cdb *c, char *key, unsigned int len, uint32 pos)
93 if (cdb_read(c, buf, n, pos) == -1) {
96 if (memcmp(buf, key, n)) {
106 int cdb_findnext(struct cdb *c, char *key, unsigned int len)
113 u = cdb_hash(key, len);
114 if (cdb_read(c, buf, 8, (u << 3) & 2047) == -1) {
117 uint32_unpack(buf + 4, &c->hslots);
121 uint32_unpack(buf, &c->hpos);
126 c->kpos = c->hpos + u;
129 while (c->loop < c->hslots) {
130 if (cdb_read(c, buf, 8, c->kpos) == -1) {
133 uint32_unpack(buf + 4, &pos);
139 if (c->kpos == c->hpos + (c->hslots << 3)) {
142 uint32_unpack(buf, &u);
144 if (cdb_read(c, buf, 8, pos) == -1) {
147 uint32_unpack(buf, &u);
149 switch (match(c, key, len, pos + 8)) {
153 uint32_unpack(buf + 4, &c->dlen);
154 c->dpos = pos + 8 + len;
163 int cdb_find(struct cdb *c, char *key, unsigned int len)
166 return cdb_findnext(c, key, len);