xref: /freebsd/lib/libc/iconv/citrus_esdb.c (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
1 /* $NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c)2003 Citrus Project,
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 
33 #include <assert.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <paths.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
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"
53 
54 #define ESDB_DIR	"esdb.dir"
55 #define ESDB_ALIAS	"esdb.alias"
56 
57 /*
58  * _citrus_esdb_alias:
59  *	resolve encoding scheme name aliases.
60  */
61 const char *
62 _citrus_esdb_alias(const char *esname, char *buf, size_t bufsize)
63 {
64 
65 	return (_lookup_alias(_PATH_ESDB "/" ESDB_ALIAS, esname, buf, bufsize,
66 	    _LOOKUP_CASE_IGNORE));
67 }
68 
69 
70 /*
71  * conv_esdb:
72  *	external representation -> local structure.
73  */
74 static int
75 conv_esdb(struct _citrus_esdb *esdb, struct _region *fr)
76 {
77 	struct _citrus_db *db;
78 	const char *str;
79 	char buf[100];
80 	uint32_t csid, i, num_charsets, tmp, version;
81 	int ret;
82 
83 	/* open db */
84 	ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL);
85 	if (ret)
86 		goto err0;
87 
88 	/* check version */
89 	ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL);
90 	if (ret)
91 		goto err1;
92 	switch (version) {
93 	case 0x00000001:
94 		/* current version */
95 		/* initial version */
96 		break;
97 	default:
98 		ret = EFTYPE;
99 		goto err1;
100 	}
101 
102 	/* get encoding/variable */
103 	ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL);
104 	if (ret)
105 		goto err1;
106 	esdb->db_encname = strdup(str);
107 	if (esdb->db_encname == NULL) {
108 		ret = errno;
109 		goto err1;
110 	}
111 
112 	esdb->db_len_variable = 0;
113 	esdb->db_variable = NULL;
114 	ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL);
115 	if (ret == 0) {
116 		esdb->db_len_variable = strlen(str) + 1;
117 		esdb->db_variable = strdup(str);
118 		if (esdb->db_variable == NULL) {
119 			ret = errno;
120 			goto err2;
121 		}
122 	} else if (ret != ENOENT)
123 		goto err2;
124 
125 	/* get number of charsets */
126 	ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS,
127 	    &num_charsets, NULL);
128 	if (ret)
129 		goto err3;
130 	esdb->db_num_charsets = num_charsets;
131 
132 	/* get invalid character */
133 	ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL);
134 	if (ret == 0) {
135 		esdb->db_use_invalid = 1;
136 		esdb->db_invalid = tmp;
137 	} else if (ret == ENOENT)
138 		esdb->db_use_invalid = 0;
139 	else
140 		goto err3;
141 
142 	/* get charsets */
143 	esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets));
144 	if (esdb->db_charsets == NULL) {
145 		ret = errno;
146 		goto err3;
147 	}
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);
152 		if (ret)
153 			goto err4;
154 		esdb->db_charsets[i].ec_csid = csid;
155 
156 		snprintf(buf, sizeof(buf),
157 		    _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i);
158 		ret = _db_lookupstr_by_s(db, buf, &str, NULL);
159 		if (ret)
160 			goto err4;
161 		esdb->db_charsets[i].ec_csname = strdup(str);
162 		if (esdb->db_charsets[i].ec_csname == NULL) {
163 			ret = errno;
164 			goto err4;
165 		}
166 	}
167 
168 	_db_close(db);
169 	return (0);
170 
171 err4:
172 	for (; i > 0; i--)
173 		free(esdb->db_charsets[i - 1].ec_csname);
174 	free(esdb->db_charsets);
175 err3:
176 	free(esdb->db_variable);
177 err2:
178 	free(esdb->db_encname);
179 err1:
180 	_db_close(db);
181 	if (ret == ENOENT)
182 		ret = EFTYPE;
183 err0:
184 	return (ret);
185 }
186 
187 /*
188  * _citrus_esdb_open:
189  *	open an ESDB file.
190  */
191 int
192 _citrus_esdb_open(struct _citrus_esdb *db, const char *esname)
193 {
194 	struct _region fr;
195 	const char *realname, *encfile;
196 	char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX];
197 	int ret;
198 
199 	snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_ALIAS);
200 	realname = _lookup_alias(path, esname, buf1, sizeof(buf1),
201 	    _LOOKUP_CASE_IGNORE);
202 
203 	snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, ESDB_DIR);
204 	encfile = _lookup_simple(path, realname, buf2, sizeof(buf2),
205 	    _LOOKUP_CASE_IGNORE);
206 	if (encfile == NULL)
207 		return (ENOENT);
208 
209 	/* open file */
210 	snprintf(path, sizeof(path), "%s/%s", _PATH_ESDB, encfile);
211 	ret = _map_file(&fr, path);
212 	if (ret)
213 		return (ret);
214 
215 	ret = conv_esdb(db, &fr);
216 
217 	_unmap_file(&fr);
218 
219 	return (ret);
220 }
221 
222 /*
223  * _citrus_esdb_close:
224  *	free an ESDB.
225  */
226 void
227 _citrus_esdb_close(struct _citrus_esdb *db)
228 {
229 
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;
237 }
238 
239 /*
240  * _citrus_esdb_free_list:
241  *	free the list.
242  */
243 void
244 _citrus_esdb_free_list(char **list, size_t num)
245 {
246 
247 	for (size_t i = 0; i < num; i++)
248 		free(list[i]);
249 	free(list);
250 }
251 
252 /*
253  * _citrus_esdb_get_list:
254  *	get esdb entries.
255  */
256 int
257 _citrus_esdb_get_list(char ***rlist, size_t *rnum, bool sorted)
258 {
259 	struct _citrus_lookup *cla, *cld;
260 	struct _region key, data;
261 	char **list, **q;
262 	char buf[PATH_MAX];
263 	size_t num;
264 	int ret;
265 
266 	ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS,
267 	    _LOOKUP_CASE_IGNORE);
268 	if (ret)
269 		goto quit0;
270 
271 	ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR,
272 	    _LOOKUP_CASE_IGNORE);
273 	if (ret)
274 		goto quit1;
275 
276 	/* count number of entries */
277 	num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld);
278 
279 	_lookup_seq_rewind(cla);
280 	_lookup_seq_rewind(cld);
281 
282 	/* allocate list pointer space */
283 	list = malloc(num * sizeof(char *));
284 	num = 0;
285 	if (list == NULL) {
286 		ret = errno;
287 		goto quit3;
288 	}
289 
290 	/* get alias entries */
291 	while ((ret = _lookup_seq_next(cla, &key, &data)) == 0) {
292 		/* XXX: sorted? */
293 		snprintf(buf, sizeof(buf), "%.*s/%.*s",
294 		    (int)_region_size(&data),
295 		    (const char *)_region_head(&data),
296 		    (int)_region_size(&key),
297 		    (const char *)_region_head(&key));
298 		_bcs_convert_to_upper(buf);
299 		list[num] = strdup(buf);
300 		if (list[num] == NULL) {
301 			ret = errno;
302 			goto quit3;
303 		}
304 		num++;
305 	}
306 	if (ret != ENOENT)
307 		goto quit3;
308 	/* get dir entries */
309 	while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) {
310 		if (!sorted)
311 			snprintf(buf, sizeof(buf), "%.*s",
312 			    (int)_region_size(&key),
313 			    (const char *)_region_head(&key));
314 		else {
315 			/* check duplicated entry */
316 			char *p;
317 			char buf1[PATH_MAX];
318 
319 			snprintf(buf1, sizeof(buf1), "%.*s",
320 			    (int)_region_size(&data),
321 			    (const char *)_region_head(&data));
322 			if ((p = strchr(buf1, '/')) != NULL)
323 				memmove(buf1, p + 1, strlen(p) - 1);
324 			if ((p = strstr(buf1, ".esdb")) != NULL)
325 				*p = '\0';
326 			snprintf(buf, sizeof(buf), "%s/%.*s", buf1,
327 			    (int)_region_size(&key),
328 			    (const char *)_region_head(&key));
329 		}
330 		_bcs_convert_to_upper(buf);
331 		ret = _lookup_seq_lookup(cla, buf, NULL);
332 		if (ret) {
333 			if (ret != ENOENT)
334 				goto quit3;
335 			/* not duplicated */
336 			list[num] = strdup(buf);
337 			if (list[num] == NULL) {
338 				ret = errno;
339 				goto quit3;
340 			}
341 			num++;
342 		}
343 	}
344 	if (ret != ENOENT)
345 		goto quit3;
346 
347 	ret = 0;
348 	/* XXX: why reallocing the list space posteriorly?
349 	    shouldn't be done earlier? */
350 	q = reallocarray(list, num, sizeof(char *));
351 	if (!q) {
352 		ret = ENOMEM;
353 		goto quit3;
354 	}
355 	list = q;
356 	*rlist = list;
357 	*rnum = num;
358 quit3:
359 	if (ret)
360 		_citrus_esdb_free_list(list, num);
361 	_lookup_seq_close(cld);
362 quit1:
363 	_lookup_seq_close(cla);
364 quit0:
365 	return (ret);
366 }
367