2 /* $NetBSD: citrus_lookup.c,v 1.7 2012/05/04 16:45:05 joerg Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
46 #include "citrus_namespace.h"
47 #include "citrus_bcs.h"
48 #include "citrus_region.h"
49 #include "citrus_memstream.h"
50 #include "citrus_mmap.h"
51 #include "citrus_db.h"
52 #include "citrus_db_hash.h"
53 #include "citrus_lookup.h"
54 #include "citrus_lookup_file.h"
56 struct _citrus_lookup {
59 struct _citrus_db *db;
60 struct _citrus_region file;
62 struct _db_locator locator;
70 #define cl_dbidx u.db.idx
71 #define cl_dbfile u.db.file
72 #define cl_dbnum u.db.num
73 #define cl_dblocator u.db.locator
74 #define cl_plainr u.plain.r
75 #define cl_plainms u.plain.ms
80 int (*cl_next)(struct _citrus_lookup *, struct _region *,
82 int (*cl_lookup)(struct _citrus_lookup *, const char *,
84 int (*cl_num_entries)(struct _citrus_lookup *);
85 void (*cl_close)(struct _citrus_lookup *);
89 seq_get_num_entries_db(struct _citrus_lookup *cl)
92 return (cl->cl_dbnum);
96 seq_next_db(struct _citrus_lookup *cl, struct _region *key,
102 _region_init(key, cl->cl_key, cl->cl_keylen);
103 return (_db_lookup_by_s(cl->cl_db, cl->cl_key, data,
111 if (cl->cl_dbidx >= cl->cl_dbnum)
114 return (_db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data));
118 seq_lookup_db(struct _citrus_lookup *cl, const char *key, struct _region *data)
123 cl->cl_key = strdup(key);
124 if (cl->cl_ignore_case)
125 _bcs_convert_to_lower(cl->cl_key);
126 cl->cl_keylen = strlen(cl->cl_key);
127 _db_locator_init(&cl->cl_dblocator);
128 return (_db_lookup_by_s(cl->cl_db, cl->cl_key, data,
133 seq_close_db(struct _citrus_lookup *cl)
136 _db_close(cl->cl_db);
137 _unmap_file(&cl->cl_dbfile);
141 seq_open_db(struct _citrus_lookup *cl, const char *name)
147 snprintf(path, sizeof(path), "%s.db", name);
148 ret = _map_file(&r, path);
152 ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
160 cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
163 cl->cl_lookup = &seq_lookup_db;
164 cl->cl_next = &seq_next_db;
165 cl->cl_num_entries = &seq_get_num_entries_db;
166 cl->cl_close = &seq_close_db;
173 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
174 struct _region *data)
180 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
184 p = _memstream_getln(&cl->cl_plainms, &len);
188 q = memchr(p, T_COMM, len);
192 /* ignore trailing spaces */
193 _bcs_trunc_rws_len(p, &len);
194 p = _bcs_skip_ws_len(p, &len);
195 q = _bcs_skip_nonws_len(p, &len);
198 if (cl->cl_key && ((size_t)(q - p) != cl->cl_keylen ||
199 memcmp(p, cl->cl_key, (size_t)(q - p)) != 0))
204 _region_init(key, __DECONST(void *, p), (size_t)(q - p));
205 p = _bcs_skip_ws_len(q, &len);
207 _region_init(data, len ? __DECONST(void *, p) : NULL, len);
213 seq_get_num_entries_plain(struct _citrus_lookup *cl)
218 while (seq_next_plain(cl, NULL, NULL) == 0)
225 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
226 struct _region *data)
233 cl->cl_key = strdup(key);
234 if (cl->cl_ignore_case)
235 _bcs_convert_to_lower(cl->cl_key);
236 cl->cl_keylen = strlen(cl->cl_key);
237 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
238 p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
242 _region_init(data, __DECONST(void *, p), len);
248 seq_close_plain(struct _citrus_lookup *cl)
251 _unmap_file(&cl->cl_plainr);
255 seq_open_plain(struct _citrus_lookup *cl, const char *name)
259 /* open read stream */
260 ret = _map_file(&cl->cl_plainr, name);
265 cl->cl_next = &seq_next_plain;
266 cl->cl_lookup = &seq_lookup_plain;
267 cl->cl_num_entries = &seq_get_num_entries_plain;
268 cl->cl_close = &seq_close_plain;
274 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name,
278 struct _citrus_lookup *cl;
280 cl = malloc(sizeof(*cl));
286 cl->cl_ignore_case = ignore_case;
287 ret = seq_open_db(cl, name);
289 ret = seq_open_plain(cl, name);
299 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
309 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
310 struct _region *key, struct _region *data)
313 return ((*cl->cl_next)(cl, key, data));
317 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
318 struct _region *data)
321 return ((*cl->cl_lookup)(cl, key, data));
325 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
328 return ((*cl->cl_num_entries)(cl));
332 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
341 _citrus_lookup_simple(const char *name, const char *key,
342 char *linebuf, size_t linebufsize, int ignore_case)
344 struct _citrus_lookup *cl;
348 ret = _citrus_lookup_seq_open(&cl, name, ignore_case);
352 ret = _citrus_lookup_seq_lookup(cl, key, &data);
354 _citrus_lookup_seq_close(cl);
358 snprintf(linebuf, linebufsize, "%.*s", (int)_region_size(&data),
359 (const char *)_region_head(&data));
361 _citrus_lookup_seq_close(cl);