-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcdb.c
201 lines (175 loc) · 4.23 KB
/
cdb.c
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
/*
* cdb.c: This file is part of the `djbdns' project, originally written
* by Dr. D J Bernstein and later released under public-domain since late
* December 2007 (http://cr.yp.to/distributors.html).
*
* Copyright (C) 2009 - 2013 Prasad J Pandit
*
* This program is a free software; you can redistribute it and/or modify
* it under the terms of GNU General Public License as published by Free
* Software Foundation; either version 2 of the license or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* of FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "cdb.h"
#include "seek.h"
#include "byte.h"
#include "error.h"
void
cdb_free (struct cdb *c)
{
if (c->map)
{
munmap (c->map, c->size);
c->map = 0;
}
}
/*
void __inline__
cdb_findstart (struct cdb *c)
{
c->loop = 0;
}
*/
void
cdb_init (struct cdb *c, int fd)
{
char *x;
struct stat st;
cdb_free (c);
cdb_findstart (c);
c->fd = fd;
if ((fstat (fd, &st) == 0) && (st.st_size <= 0xffffffff))
{
x = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (x + 1)
{
c->size = st.st_size;
c->map = x;
}
}
}
int
cdb_read (struct cdb *c, char *buf, unsigned int len, uint32 pos)
{
if (c->map)
{
if ((pos > c->size) || (c->size - pos < len))
goto FORMAT;
byte_copy (buf, len, c->map + pos);
}
else
{
if (seek_set (c->fd, pos) == -1)
return -1;
while (len > 0)
{
int r;
do
r = read (c->fd, buf, len);
while ((r == -1) && (errno == error_intr));
if (r == -1)
return -1;
if (r == 0)
goto FORMAT;
buf += r;
len -= r;
}
}
return 0;
FORMAT:
errno = error_proto;
return -1;
}
static int
match (struct cdb *c, const char *key, unsigned int len, uint32 pos)
{
int n;
char buf[32];
while (len > 0)
{
n = sizeof buf;
if ((unsigned)n > len)
n = len;
if (cdb_read (c, buf, n, pos) == -1)
return -1;
if (byte_diff (buf, n, key))
return 0;
pos += n;
key += n;
len -= n;
}
return 1;
}
int
cdb_findnext (struct cdb *c, const char *key, unsigned int len)
{
uint32 u;
uint32 pos;
char buf[8];
if (!c->loop)
{
u = cdb_hash (key, len);
if (cdb_read (c, buf, 8, (u << 3) & 2047) == -1)
return -1;
uint32_unpack (buf + 4, &c->hslots);
if (!c->hslots)
return 0;
uint32_unpack (buf, &c->hpos);
c->khash = u;
u >>= 8;
u %= c->hslots;
u <<= 3;
c->kpos = c->hpos + u;
}
while (c->loop < c->hslots)
{
if (cdb_read (c, buf, 8, c->kpos) == -1)
return -1;
uint32_unpack (buf + 4, &pos);
if (!pos)
return 0;
c->loop += 1;
c->kpos += 8;
if (c->kpos == c->hpos + (c->hslots << 3))
c->kpos = c->hpos;
uint32_unpack (buf, &u);
if (u == c->khash)
{
if (cdb_read (c, buf, 8, pos) == -1)
return -1;
uint32_unpack (buf, &u);
if (u == len)
{
switch (match (c, key, len, pos + 8))
{
case -1:
return -1;
case 1:
uint32_unpack (buf + 4, &c->dlen);
c->dpos = pos + 8 + len;
return 1;
}
}
}
}
return 0;
}
int
cdb_find (struct cdb *c, const char *key, unsigned int len)
{
cdb_findstart (c);
return cdb_findnext (c, key, len);
}