Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / citrus_db.c
blob137d2727f01a656934a130ddbc61465da8bda070
1 /* $NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_db.c,v 1.3 2008/04/10 10:21:01 hasso Exp $ */
5 /*-
6 * Copyright (c)2003 Citrus Project,
7 * All rights reserved.
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.
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.
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>
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"
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;
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)
60 struct _memstream ms;
61 struct _citrus_db *db;
62 struct _citrus_db_header_x *dhx;
64 _memstream_bind(&ms, r);
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;
75 if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
76 _memstream_remainder(&ms))
77 return EFTYPE;
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;
87 return 0;
90 void
91 _citrus_db_close(struct _citrus_db *db)
93 free(db);
96 int
97 _citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
98 struct _citrus_region *data, struct _citrus_db_locator *dl)
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;
107 _memstream_bind(&ms, &db->db_region);
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;
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;
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;
138 /* jump to next entry having the same hash value. */
139 offset = be32toh(dex->dex_next_offset);
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);
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;
177 } while (offset != 0);
179 return ENOENT;
183 _citrus_db_lookup_by_string(struct _citrus_db *db, const char *key,
184 struct _citrus_region *data,
185 struct _citrus_db_locator *dl)
187 struct _region r;
189 _region_init(&r, __DECONST(char *, key), strlen(key));
191 return _citrus_db_lookup(db, &r, data, dl);
195 _citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key,
196 uint8_t *rval, struct _citrus_db_locator *dl)
198 int ret;
199 struct _region r;
201 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
202 if (ret)
203 return ret;
205 if (_region_size(&r) != 1)
206 return EFTYPE;
208 if (rval)
209 memcpy(rval, _region_head(&r), 1);
211 return 0;
215 _citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key,
216 uint16_t *rval, struct _citrus_db_locator *dl)
218 int ret;
219 struct _region r;
220 uint16_t val;
222 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
223 if (ret)
224 return ret;
226 if (_region_size(&r) != 2)
227 return EFTYPE;
229 if (rval) {
230 memcpy(&val, _region_head(&r), 2);
231 *rval = be16toh(val);
234 return 0;
238 _citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key,
239 uint32_t *rval, struct _citrus_db_locator *dl)
241 int ret;
242 struct _region r;
243 uint32_t val;
245 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
246 if (ret)
247 return ret;
249 if (_region_size(&r) != 4)
250 return EFTYPE;
252 if (rval) {
253 memcpy(&val, _region_head(&r), 4);
254 *rval = be32toh(val);
257 return 0;
261 _citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key,
262 const char **rdata,
263 struct _citrus_db_locator *dl)
265 int ret;
266 struct _region r;
268 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
269 if (ret)
270 return ret;
272 /* check whether the string is null terminated */
273 if (_region_size(&r) == 0)
274 return EFTYPE;
275 if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0')
276 return EFTYPE;
278 if (rdata)
279 *rdata = _region_head(&r);
281 return 0;
285 _citrus_db_get_number_of_entries(struct _citrus_db *db)
287 struct _memstream ms;
288 struct _citrus_db_header_x *dhx;
290 _memstream_bind(&ms, &db->db_region);
292 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
293 _DIAGASSERT(dhx);
294 return (int)be32toh(dhx->dhx_num_entries);
298 _citrus_db_get_entry(struct _citrus_db *db, int idx,
299 struct _region *key, struct _region *data)
301 uint32_t num_entries;
302 size_t offset;
303 struct _memstream ms;
304 struct _citrus_db_header_x *dhx;
305 struct _citrus_db_entry_x *dex;
307 _memstream_bind(&ms, &db->db_region);
309 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
310 _DIAGASSERT(dhx);
311 num_entries = be32toh(dhx->dhx_num_entries);
312 if (idx < 0 || (uint32_t)idx >= num_entries)
313 return EINVAL;
315 /* seek to the next entry */
316 offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
317 if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
318 return EFTYPE;
319 /* get the entry record */
320 dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
321 if (dex == NULL)
322 return EFTYPE;
323 /* seek to the head of the key. */
324 if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
325 return EFTYPE;
326 /* get the region of the key. */
327 if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
328 return EFTYPE;
329 /* seek to the head of the data. */
330 if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
331 return EFTYPE;
332 /* get the region of the data. */
333 if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)
334 return EFTYPE;
336 return 0;