1 /* $FreeBSD: head/lib/libc/iconv/citrus_db.c 219019 2011-02-25 00:04:39Z gabor $ */
2 /* $NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #include <sys/endian.h>
32 #include <sys/types.h>
41 #include "citrus_namespace.h"
42 #include "citrus_bcs.h"
43 #include "citrus_region.h"
44 #include "citrus_memstream.h"
45 #include "citrus_mmap.h"
46 #include "citrus_db.h"
47 #include "citrus_db_factory.h"
48 #include "citrus_db_file.h"
51 struct _region db_region
;
52 _citrus_db_hash_func_t db_hashfunc
;
53 void *db_hashfunc_closure
;
57 _citrus_db_open(struct _citrus_db
**rdb
, struct _region
*r
, const char *magic
,
58 _citrus_db_hash_func_t hashfunc
, void *hashfunc_closure
)
60 struct _citrus_db
*db
;
61 struct _citrus_db_header_x
*dhx
;
64 _memstream_bind(&ms
, r
);
67 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
70 if (strncmp(dhx
->dhx_magic
, magic
, _CITRUS_DB_MAGIC_SIZE
) != 0)
72 if (_memstream_seek(&ms
, be32toh(dhx
->dhx_entry_offset
), SEEK_SET
))
75 if (be32toh(dhx
->dhx_num_entries
)*_CITRUS_DB_ENTRY_SIZE
>
76 _memstream_remainder(&ms
))
79 db
= malloc(sizeof(*db
));
83 db
->db_hashfunc
= hashfunc
;
84 db
->db_hashfunc_closure
= hashfunc_closure
;
91 _citrus_db_close(struct _citrus_db
*db
)
98 _citrus_db_lookup(struct _citrus_db
*db
, struct _citrus_region
*key
,
99 struct _citrus_region
*data
, struct _citrus_db_locator
*dl
)
101 struct _citrus_db_entry_x
*dex
;
102 struct _citrus_db_header_x
*dhx
;
103 struct _citrus_region r
;
104 struct _memstream ms
;
105 uint32_t hashval
, num_entries
;
108 _memstream_bind(&ms
, &db
->db_region
);
110 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
111 num_entries
= be32toh(dhx
->dhx_num_entries
);
112 if (num_entries
== 0)
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
))
121 hashval
= db
->db_hashfunc(key
)%num_entries
;
122 offset
= be32toh(dhx
->dhx_entry_offset
) +
123 hashval
* _CITRUS_DB_ENTRY_SIZE
;
125 dl
->dl_hashval
= hashval
;
128 /* seek to the next entry */
129 if (_citrus_memory_stream_seek(&ms
, offset
, SEEK_SET
))
131 /* get the entry record */
132 dex
= _memstream_getregion(&ms
, NULL
, _CITRUS_DB_ENTRY_SIZE
);
136 /* jump to next entry having the same hash value. */
137 offset
= be32toh(dex
->dex_next_offset
);
139 /* save the current position */
141 dl
->dl_offset
= offset
;
143 dl
->dl_offset
= _region_size(&db
->db_region
);
146 /* compare hash value. */
147 if (be32toh(dex
->dex_hash_value
) != hashval
)
150 /* compare key length */
151 if (be32toh(dex
->dex_key_size
) == _region_size(key
)) {
152 /* seek to the head of the key. */
153 if (_memstream_seek(&ms
, be32toh(dex
->dex_key_offset
),
156 /* get the region of the key */
157 if (_memstream_getregion(&ms
, &r
,
158 _region_size(key
)) == NULL
)
160 /* compare key byte stream */
161 if (memcmp(_region_head(&r
), _region_head(key
),
162 _region_size(key
)) == 0) {
165 &ms
, be32toh(dex
->dex_data_offset
),
168 if (_memstream_getregion(
170 be32toh(dex
->dex_data_size
)) == NULL
)
175 } while (offset
!= 0);
181 _citrus_db_lookup_by_string(struct _citrus_db
*db
, const char *key
,
182 struct _citrus_region
*data
, struct _citrus_db_locator
*dl
)
186 _region_init(&r
, __DECONST(void *, key
), strlen(key
));
188 return (_citrus_db_lookup(db
, &r
, data
, dl
));
192 _citrus_db_lookup8_by_string(struct _citrus_db
*db
, const char *key
,
193 uint8_t *rval
, struct _citrus_db_locator
*dl
)
198 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
202 if (_region_size(&r
) != 1)
206 memcpy(rval
, _region_head(&r
), 1);
212 _citrus_db_lookup16_by_string(struct _citrus_db
*db
, const char *key
,
213 uint16_t *rval
, struct _citrus_db_locator
*dl
)
219 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
223 if (_region_size(&r
) != 2)
227 memcpy(&val
, _region_head(&r
), 2);
228 *rval
= be16toh(val
);
235 _citrus_db_lookup32_by_string(struct _citrus_db
*db
, const char *key
,
236 uint32_t *rval
, struct _citrus_db_locator
*dl
)
242 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
246 if (_region_size(&r
) != 4)
250 memcpy(&val
, _region_head(&r
), 4);
251 *rval
= be32toh(val
);
258 _citrus_db_lookup_string_by_string(struct _citrus_db
*db
, const char *key
,
259 const char **rdata
, struct _citrus_db_locator
*dl
)
264 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
268 /* check whether the string is null terminated */
269 if (_region_size(&r
) == 0)
271 if (*((const char*)_region_head(&r
)+_region_size(&r
)-1) != '\0')
275 *rdata
= _region_head(&r
);
281 _citrus_db_get_number_of_entries(struct _citrus_db
*db
)
283 struct _citrus_db_header_x
*dhx
;
284 struct _memstream ms
;
286 _memstream_bind(&ms
, &db
->db_region
);
288 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
289 return ((int)be32toh(dhx
->dhx_num_entries
));
293 _citrus_db_get_entry(struct _citrus_db
*db
, int idx
, struct _region
*key
,
294 struct _region
*data
)
296 struct _citrus_db_entry_x
*dex
;
297 struct _citrus_db_header_x
*dhx
;
298 struct _memstream ms
;
299 uint32_t num_entries
;
302 _memstream_bind(&ms
, &db
->db_region
);
304 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
305 num_entries
= be32toh(dhx
->dhx_num_entries
);
306 if (idx
< 0 || (uint32_t)idx
>= num_entries
)
309 /* seek to the next entry */
310 offset
= be32toh(dhx
->dhx_entry_offset
) + idx
* _CITRUS_DB_ENTRY_SIZE
;
311 if (_citrus_memory_stream_seek(&ms
, offset
, SEEK_SET
))
313 /* get the entry record */
314 dex
= _memstream_getregion(&ms
, NULL
, _CITRUS_DB_ENTRY_SIZE
);
317 /* seek to the head of the key. */
318 if (_memstream_seek(&ms
, be32toh(dex
->dex_key_offset
), SEEK_SET
))
320 /* get the region of the key. */
321 if (_memstream_getregion(&ms
, key
, be32toh(dex
->dex_key_size
))==NULL
)
323 /* seek to the head of the data. */
324 if (_memstream_seek(&ms
, be32toh(dex
->dex_data_offset
), SEEK_SET
))
326 /* get the region of the data. */
327 if (_memstream_getregion(&ms
, data
, be32toh(dex
->dex_data_size
))==NULL
)