2 /* $NetBSD: citrus_esdb.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/types.h>
42 #include "citrus_namespace.h"
43 #include "citrus_types.h"
44 #include "citrus_bcs.h"
45 #include "citrus_region.h"
46 #include "citrus_memstream.h"
47 #include "citrus_mmap.h"
48 #include "citrus_lookup.h"
49 #include "citrus_db.h"
50 #include "citrus_db_hash.h"
51 #include "citrus_esdb.h"
52 #include "citrus_esdb_file.h"
54 #define ESDB_DIR "esdb.dir"
55 #define ESDB_ALIAS "esdb.alias"
59 * resolve encoding scheme name aliases.
62 _citrus_esdb_alias(const char *esname, char *buf, size_t bufsize)
65 return (_lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize,
66 _LOOKUP_CASE_IGNORE));
72 * external representation -> local structure.
75 conv_esdb(struct _citrus_esdb *esdb, struct _region *fr)
77 struct _citrus_db *db;
80 uint32_t csid, i, num_charsets, tmp, version;
84 ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL);
89 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL);
102 /* get encoding/variable */
103 ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL);
106 esdb->db_encname = strdup(str);
107 if (esdb->db_encname == NULL) {
112 esdb->db_len_variable = 0;
113 esdb->db_variable = NULL;
114 ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL);
116 esdb->db_len_variable = strlen(str) + 1;
117 esdb->db_variable = strdup(str);
118 if (esdb->db_variable == NULL) {
122 } else if (ret != ENOENT)
125 /* get number of charsets */
126 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS,
127 &num_charsets, NULL);
130 esdb->db_num_charsets = num_charsets;
132 /* get invalid character */
133 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL);
135 esdb->db_use_invalid = 1;
136 esdb->db_invalid = tmp;
137 } else if (ret == ENOENT)
138 esdb->db_use_invalid = 0;
143 esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets));
144 if (esdb->db_charsets == NULL) {
148 for (i = 0; i < num_charsets; i++) {
149 snprintf(buf, sizeof(buf),
150 _CITRUS_ESDB_SYM_CSID_PREFIX "%d", i);
151 ret = _db_lookup32_by_s(db, buf, &csid, NULL);
154 esdb->db_charsets[i].ec_csid = csid;
156 snprintf(buf, sizeof(buf),
157 _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i);
158 ret = _db_lookupstr_by_s(db, buf, &str, NULL);
161 esdb->db_charsets[i].ec_csname = strdup(str);
162 if (esdb->db_charsets[i].ec_csname == NULL) {
173 free(esdb->db_charsets[i - 1].ec_csname);
174 free(esdb->db_charsets);
176 free(esdb->db_variable);
178 free(esdb->db_encname);
192 _citrus_esdb_open(struct _citrus_esdb *db, const char *esname)
195 const char *realname, *encfile;
196 char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX];
199 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS);
200 realname = _lookup_alias(path, esname, buf1, sizeof(buf1),
201 _LOOKUP_CASE_IGNORE);
203 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR);
204 encfile = _lookup_simple(path, realname, buf2, sizeof(buf2),
205 _LOOKUP_CASE_IGNORE);
210 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile);
211 ret = _map_file(&fr, path);
215 ret = conv_esdb(db, &fr);
223 * _citrus_esdb_close:
227 _citrus_esdb_close(struct _citrus_esdb *db)
230 for (int i = 0; i < db->db_num_charsets; i++)
231 free(db->db_charsets[i].ec_csname);
232 db->db_num_charsets = 0;
233 free(db->db_charsets); db->db_charsets = NULL;
234 free(db->db_encname); db->db_encname = NULL;
235 db->db_len_variable = 0;
236 free(db->db_variable); db->db_variable = NULL;
240 * _citrus_esdb_free_list:
244 _citrus_esdb_free_list(char **list, size_t num)
247 for (size_t i = 0; i < num; i++)
253 * _citrus_esdb_get_list:
257 _citrus_esdb_get_list(char ***rlist, size_t *rnum, bool sorted)
259 struct _citrus_lookup *cla, *cld;
260 struct _region key, data;
268 ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS,
269 _LOOKUP_CASE_IGNORE);
273 ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR,
274 _LOOKUP_CASE_IGNORE);
278 /* count number of entries */
279 num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld);
281 _lookup_seq_rewind(cla);
282 _lookup_seq_rewind(cld);
284 /* allocate list pointer space */
285 list = malloc(num * sizeof(char *));
292 /* get alias entries */
293 while ((ret = _lookup_seq_next(cla, &key, &data)) == 0) {
295 snprintf(buf, sizeof(buf), "%.*s/%.*s",
296 (int)_region_size(&data),
297 (const char *)_region_head(&data),
298 (int)_region_size(&key),
299 (const char *)_region_head(&key));
301 snprintf(buf, sizeof(buf), "%.*s/%.*s",
302 (int)_region_size(&data),
303 (const char *)_region_head(&data),
304 (int)_region_size(&key),
305 (const char *)_region_head(&key));
306 _bcs_convert_to_upper(buf);
307 list[num] = strdup(buf);
308 if (list[num] == NULL) {
316 /* get dir entries */
317 while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) {
319 snprintf(buf, sizeof(buf), "%.*s",
320 (int)_region_size(&key),
321 (const char *)_region_head(&key));
323 /* check duplicated entry */
327 snprintf(buf1, sizeof(buf1), "%.*s",
328 (int)_region_size(&data),
329 (const char *)_region_head(&data));
330 if ((p = strchr(buf1, '/')) != NULL)
331 memmove(buf1, p + 1, strlen(p) - 1);
332 if ((p = strstr(buf1, ".esdb")) != NULL)
334 snprintf(buf, sizeof(buf), "%s/%.*s", buf1,
335 (int)_region_size(&key),
336 (const char *)_region_head(&key));
338 _bcs_convert_to_upper(buf);
339 ret = _lookup_seq_lookup(cla, buf, NULL);
344 list[num] = strdup(buf);
345 if (list[num] == NULL) {
356 /* XXX: why reallocing the list space posteriorly?
357 shouldn't be done earlier? */
358 q = realloc(list, num * sizeof(char *));
368 _citrus_esdb_free_list(list, num);
369 _lookup_seq_close(cld);
371 _lookup_seq_close(cla);