xref: /dragonfly/lib/libc/citrus/citrus_esdb.c (revision c3762235)
1 /* $FreeBSD: head/lib/libc/iconv/citrus_esdb.c 219019 2011-02-25 00:04:39Z gabor $ */
2 /* $NetBSD: citrus_esdb.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
3 
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
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 	num = 0;
267 
268 	ret = _lookup_seq_open(&cla, _PATH_ESDB "/" ESDB_ALIAS,
269 	    _LOOKUP_CASE_IGNORE);
270 	if (ret)
271 		goto quit0;
272 
273 	ret = _lookup_seq_open(&cld, _PATH_ESDB "/" ESDB_DIR,
274 	    _LOOKUP_CASE_IGNORE);
275 	if (ret)
276 		goto quit1;
277 
278 	/* count number of entries */
279 	num = _lookup_get_num_entries(cla) + _lookup_get_num_entries(cld);
280 
281 	_lookup_seq_rewind(cla);
282 	_lookup_seq_rewind(cld);
283 
284 	/* allocate list pointer space */
285 	list = malloc(num * sizeof(char *));
286 	num = 0;
287 	if (list == NULL) {
288 		ret = errno;
289 		goto quit3;
290 	}
291 
292 	/* get alias entries */
293 	while ((ret = _lookup_seq_next(cla, &key, &data)) == 0) {
294 		if (sorted)
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 		else
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) {
309 			ret = errno;
310 			goto quit3;
311 		}
312 		num++;
313 	}
314 	if (ret != ENOENT)
315 		goto quit3;
316 	/* get dir entries */
317 	while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) {
318 		if (!sorted)
319 			snprintf(buf, sizeof(buf), "%.*s",
320 			    (int)_region_size(&key),
321 			    (const char *)_region_head(&key));
322 		else {
323 			/* check duplicated entry */
324 			char *p;
325 			char buf1[PATH_MAX];
326 
327 			snprintf(buf1, sizeof(buf1), "%.*s",
328 			    (int)_region_size(&data),
329 			    (const char *)_region_head(&data));
330 			if ((p = strchr(buf1, '/')) != NULL)
331 				memcpy(buf1, p + 1, strlen(p) - 1);
332 			if ((p = strstr(buf1, ".esdb")) != NULL)
333 				*p = '\0';
334 			snprintf(buf, sizeof(buf), "%s/%.*s", buf1,
335 			    (int)_region_size(&key),
336 			    (const char *)_region_head(&key));
337 		}
338 		_bcs_convert_to_upper(buf);
339 		ret = _lookup_seq_lookup(cla, buf, NULL);
340 		if (ret) {
341 			if (ret != ENOENT)
342 				goto quit3;
343 			/* not duplicated */
344 			list[num] = strdup(buf);
345 			if (list[num] == NULL) {
346 				ret = errno;
347 				goto quit3;
348 			}
349 			num++;
350 		}
351 	}
352 	if (ret != ENOENT)
353 		goto quit3;
354 
355 	ret = 0;
356 	/* XXX: why reallocing the list space posteriorly?
357 	    shouldn't be done earlier? */
358 	q = realloc(list, num * sizeof(char *));
359 	if (!q) {
360 		ret = ENOMEM;
361 		goto quit3;
362 	}
363 	list = q;
364 	*rlist = list;
365 	*rnum = num;
366 quit3:
367 	if (ret)
368 		_citrus_esdb_free_list(list, num);
369 	_lookup_seq_close(cld);
370 quit1:
371 	_lookup_seq_close(cla);
372 quit0:
373 	return (ret);
374 }
375