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