xref: /dflybsd-src/lib/libc/citrus/citrus_db.c (revision 402e4fa9c42adb64c3df20a1dbea61ff1afd9f2c)
1 /*	$NetBSD: src/lib/libc/citrus/citrus_db.c,v 1.3 2004/01/02 21:49:35 itojun Exp $	*/
2 /*	$DragonFly: src/lib/libc/citrus/citrus_db.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */
3 
4 
5 /*-
6  * Copyright (c)2003 Citrus Project,
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/endian.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "citrus_namespace.h"
41 #include "citrus_bcs.h"
42 #include "citrus_region.h"
43 #include "citrus_memstream.h"
44 #include "citrus_mmap.h"
45 #include "citrus_db.h"
46 #include "citrus_db_file.h"
47 
48 struct _citrus_db {
49 	/* private */
50 	struct _region db_region;
51 	uint32_t (*db_hashfunc)(void *, struct _citrus_region *);
52 	void *db_hashfunc_closure;
53 };
54 
55 int
56 _citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic,
57 		uint32_t (*hashfunc)(void *, struct _citrus_region *),
58 		void *hashfunc_closure)
59 {
60 	struct _memstream ms;
61 	struct _citrus_db *db;
62 	struct _citrus_db_header_x *dhx;
63 
64 	_memstream_bind(&ms, r);
65 
66 	/* sanity check */
67 	dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
68 	if (dhx == NULL)
69 		return EFTYPE;
70 	if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0)
71 		return EFTYPE;
72 	if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET))
73 		return EFTYPE;
74 
75 	if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
76 	    _memstream_remainder(&ms))
77 		return EFTYPE;
78 
79 	db = malloc(sizeof(*db));
80 	if (db==NULL)
81 		return errno;
82 	db->db_region = *r;
83 	db->db_hashfunc = hashfunc;
84 	db->db_hashfunc_closure = hashfunc_closure;
85 	*rdb = db;
86 
87 	return 0;
88 }
89 
90 void
91 _citrus_db_close(struct _citrus_db *db)
92 {
93 	free(db);
94 }
95 
96 int
97 _citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
98 		  struct _citrus_region *data, struct _citrus_db_locator *dl)
99 {
100 	uint32_t hashval, num_entries;
101 	size_t offset;
102 	struct _memstream ms;
103 	struct _citrus_db_header_x *dhx;
104 	struct _citrus_db_entry_x *dex;
105 	struct _citrus_region r;
106 
107 	_memstream_bind(&ms, &db->db_region);
108 
109 	dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
110 	_DIAGASSERT(dhx);
111 	num_entries = be32toh(dhx->dhx_num_entries);
112 	if (num_entries == 0)
113 		return ENOENT;
114 
115 	if (dl != NULL && dl->dl_offset>0) {
116 		hashval = dl->dl_hashval;
117 		offset = dl->dl_offset;
118 		if (offset >= _region_size(&db->db_region))
119 			return ENOENT;
120 	} else {
121 		hashval =
122 		    db->db_hashfunc(db->db_hashfunc_closure, key)%num_entries;
123 		offset =
124 		    be32toh(dhx->dhx_entry_offset) +
125 		    hashval * _CITRUS_DB_ENTRY_SIZE;
126 		if (dl)
127 			dl->dl_hashval = hashval;
128 	}
129 	do {
130 		/* seek to the next entry */
131 		if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
132 			return EFTYPE;
133 		/* get the entry record */
134 		dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
135 		if (dex == NULL)
136 			return EFTYPE;
137 
138 		/* jump to next entry having the same hash value. */
139 		offset = be32toh(dex->dex_next_offset);
140 
141 		/* save the current position */
142 		if (dl) {
143 			dl->dl_offset = offset;
144 			if (offset==0)
145 				dl->dl_offset = _region_size(&db->db_region);
146 		}
147 
148 		/* compare hash value. */
149 		if (be32toh(dex->dex_hash_value) != hashval)
150 			/* not found */
151 			break;
152 		/* compare key length */
153 		if (be32toh(dex->dex_key_size) == _region_size(key)) {
154 			/* seek to the head of the key. */
155 			if (_memstream_seek(&ms, be32toh(dex->dex_key_offset),
156 					    SEEK_SET))
157 				return EFTYPE;
158 			/* get the region of the key */
159 			if (_memstream_getregion(&ms, &r,
160 						 _region_size(key)) == NULL)
161 				return EFTYPE;
162 			/* compare key byte stream */
163 			if (memcmp(_region_head(&r), _region_head(key),
164 				   _region_size(key)) == 0) {
165 				/* match */
166 				if (_memstream_seek(
167 					&ms, be32toh(dex->dex_data_offset),
168 					SEEK_SET))
169 					return EFTYPE;
170 				if (_memstream_getregion(
171 					&ms, data,
172 					be32toh(dex->dex_data_size)) == NULL)
173 					return EFTYPE;
174 				return 0;
175 			}
176 		}
177 	} while (offset != 0);
178 
179 	return ENOENT;
180 }
181 
182 int
183 _citrus_db_lookup_by_string(struct _citrus_db *db, const char *key,
184 			    struct _citrus_region *data,
185 			    struct _citrus_db_locator *dl)
186 {
187 	struct _region r;
188 
189 	/* LINTED: discard const */
190 	_region_init(&r, (char *)key, strlen(key));
191 
192 	return _citrus_db_lookup(db, &r, data, dl);
193 }
194 
195 int
196 _citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key,
197 			     uint8_t *rval, struct _citrus_db_locator *dl)
198 {
199 	int ret;
200 	struct _region r;
201 
202 	ret = _citrus_db_lookup_by_string(db, key, &r, dl);
203 	if (ret)
204 		return ret;
205 
206 	if (_region_size(&r) != 1)
207 		return EFTYPE;
208 
209 	if (rval)
210 		memcpy(rval, _region_head(&r), 1);
211 
212 	return 0;
213 }
214 
215 int
216 _citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key,
217 			      uint16_t *rval, struct _citrus_db_locator *dl)
218 {
219 	int ret;
220 	struct _region r;
221 	uint16_t val;
222 
223 	ret = _citrus_db_lookup_by_string(db, key, &r, dl);
224 	if (ret)
225 		return ret;
226 
227 	if (_region_size(&r) != 2)
228 		return EFTYPE;
229 
230 	if (rval) {
231 		memcpy(&val, _region_head(&r), 2);
232 		*rval = be16toh(val);
233 	}
234 
235 	return 0;
236 }
237 
238 int
239 _citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key,
240 			      uint32_t *rval, struct _citrus_db_locator *dl)
241 {
242 	int ret;
243 	struct _region r;
244 	uint32_t val;
245 
246 	ret = _citrus_db_lookup_by_string(db, key, &r, dl);
247 	if (ret)
248 		return ret;
249 
250 	if (_region_size(&r) != 4)
251 		return EFTYPE;
252 
253 	if (rval) {
254 		memcpy(&val, _region_head(&r), 4);
255 		*rval = be32toh(val);
256 	}
257 
258 	return 0;
259 }
260 
261 int
262 _citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key,
263 				   const char **rdata,
264 				   struct _citrus_db_locator *dl)
265 {
266 	int ret;
267 	struct _region r;
268 
269 	ret = _citrus_db_lookup_by_string(db, key, &r, dl);
270 	if (ret)
271 		return ret;
272 
273 	/* check whether the string is null terminated */
274 	if (_region_size(&r) == 0)
275 		return EFTYPE;
276 	if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0')
277 		return EFTYPE;
278 
279 	if (rdata)
280 		*rdata = _region_head(&r);
281 
282 	return 0;
283 }
284 
285 int
286 _citrus_db_get_number_of_entries(struct _citrus_db *db)
287 {
288 	struct _memstream ms;
289 	struct _citrus_db_header_x *dhx;
290 
291 	_memstream_bind(&ms, &db->db_region);
292 
293 	dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
294 	_DIAGASSERT(dhx);
295 	return (int)be32toh(dhx->dhx_num_entries);
296 }
297 
298 int
299 _citrus_db_get_entry(struct _citrus_db *db, int idx,
300 		     struct _region *key, struct _region *data)
301 {
302 	uint32_t num_entries;
303 	size_t offset;
304 	struct _memstream ms;
305 	struct _citrus_db_header_x *dhx;
306 	struct _citrus_db_entry_x *dex;
307 
308 	_memstream_bind(&ms, &db->db_region);
309 
310 	dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
311 	_DIAGASSERT(dhx);
312 	num_entries = be32toh(dhx->dhx_num_entries);
313 	if (idx < 0 || (uint32_t)idx >= num_entries)
314 		return EINVAL;
315 
316 	/* seek to the next entry */
317 	offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
318 	if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
319 		return EFTYPE;
320 	/* get the entry record */
321 	dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
322 	if (dex == NULL)
323 		return EFTYPE;
324 	/* seek to the head of the key. */
325 	if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
326 		return EFTYPE;
327 	/* get the region of the key. */
328 	if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
329 		return EFTYPE;
330 	/* seek to the head of the data. */
331 	if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
332 		return EFTYPE;
333 	/* get the region of the data. */
334 	if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)
335 		return EFTYPE;
336 
337 	return 0;
338 }
339