2 /* $NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung 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>
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_bcs.h"
47 #include "citrus_region.h"
48 #include "citrus_memstream.h"
49 #include "citrus_mmap.h"
50 #include "citrus_lookup.h"
51 #include "citrus_db.h"
52 #include "citrus_db_hash.h"
53 #include "citrus_esdb.h"
54 #include "citrus_esdb_file.h"
56 #define ESDB_DIR "esdb.dir"
57 #define ESDB_ALIAS "esdb.alias"
61 * resolve encoding scheme name aliases.
64 _citrus_esdb_alias(const char *esname, char *buf, size_t bufsize)
67 return (_lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize,
68 _LOOKUP_CASE_IGNORE));
74 * external representation -> local structure.
77 conv_esdb(struct _citrus_esdb *esdb, struct _region *fr)
79 struct _citrus_db *db;
82 uint32_t csid, i, num_charsets, tmp, version;
86 ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL);
91 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL);
104 /* get encoding/variable */
105 ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL);
108 esdb->db_encname = strdup(str);
109 if (esdb->db_encname == NULL) {
114 esdb->db_len_variable = 0;
115 esdb->db_variable = NULL;
116 ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL);
118 esdb->db_len_variable = strlen(str) + 1;
119 esdb->db_variable = strdup(str);
120 if (esdb->db_variable == NULL) {
124 } else if (ret != ENOENT)
127 /* get number of charsets */
128 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS,
129 &num_charsets, NULL);
132 esdb->db_num_charsets = num_charsets;
134 /* get invalid character */
135 ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL);
137 esdb->db_use_invalid = 1;
138 esdb->db_invalid = tmp;
139 } else if (ret == ENOENT)
140 esdb->db_use_invalid = 0;
145 esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets));
146 if (esdb->db_charsets == NULL) {
150 for (i = 0; i < num_charsets; i++) {
151 snprintf(buf, sizeof(buf),
152 _CITRUS_ESDB_SYM_CSID_PREFIX "%d", i);
153 ret = _db_lookup32_by_s(db, buf, &csid, NULL);
156 esdb->db_charsets[i].ec_csid = csid;
158 snprintf(buf, sizeof(buf),
159 _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i);
160 ret = _db_lookupstr_by_s(db, buf, &str, NULL);
163 esdb->db_charsets[i].ec_csname = strdup(str);
164 if (esdb->db_charsets[i].ec_csname == NULL) {
175 free(esdb->db_charsets[i - 1].ec_csname);
176 free(esdb->db_charsets);
178 free(esdb->db_variable);
180 free(esdb->db_encname);
194 _citrus_esdb_open(struct _citrus_esdb *db, const char *esname)
197 const char *realname, *encfile;
198 char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX];
201 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS);
202 realname = _lookup_alias(path, esname, buf1, sizeof(buf1),
203 _LOOKUP_CASE_IGNORE);
205 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR);
206 encfile = _lookup_simple(path, realname, buf2, sizeof(buf2),
207 _LOOKUP_CASE_IGNORE);
212 snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile);
213 ret = _map_file(&fr, path);
217 ret = conv_esdb(db, &fr);
225 * _citrus_esdb_close:
229 _citrus_esdb_close(struct _citrus_esdb *db)
232 for (int i = 0; i < db->db_num_charsets; i++)
233 free(db->db_charsets[i].ec_csname);
234 db->db_num_charsets = 0;
235 free(db->db_charsets); db->db_charsets = NULL;
236 free(db->db_encname); db->db_encname = NULL;
237 db->db_len_variable = 0;
238 free(db->db_variable); db->db_variable = NULL;
242 * _citrus_esdb_free_list:
246 _citrus_esdb_free_list(char **list, size_t num)
249 for (size_t i = 0; i < num; i++)
255 * _citrus_esdb_get_list:
259 _citrus_esdb_get_list(char ***rlist, size_t *rnum, bool sorted)
261 struct _citrus_lookup *cla, *cld;
262 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));
300 _bcs_convert_to_upper(buf);
301 list[num] = strdup(buf);
302 if (list[num] == NULL) {
310 /* get dir entries */
311 while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) {
313 snprintf(buf, sizeof(buf), "%.*s",
314 (int)_region_size(&key),
315 (const char *)_region_head(&key));
317 /* check duplicated entry */
321 snprintf(buf1, sizeof(buf1), "%.*s",
322 (int)_region_size(&data),
323 (const char *)_region_head(&data));
324 if ((p = strchr(buf1, '/')) != NULL)
325 memmove(buf1, p + 1, strlen(p) - 1);
326 if ((p = strstr(buf1, ".esdb")) != NULL)
328 snprintf(buf, sizeof(buf), "%s/%.*s", buf1,
329 (int)_region_size(&key),
330 (const char *)_region_head(&key));
332 _bcs_convert_to_upper(buf);
333 ret = _lookup_seq_lookup(cla, buf, NULL);
338 list[num] = strdup(buf);
339 if (list[num] == NULL) {
350 /* XXX: why reallocing the list space posteriorly?
351 shouldn't be done earlier? */
352 q = reallocarray(list, num, sizeof(char *));
362 _citrus_esdb_free_list(list, num);
363 _lookup_seq_close(cld);
365 _lookup_seq_close(cla);