1 /* $FreeBSD$ */ 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 /* XXX: 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 _bcs_convert_to_upper(buf); 301 list[num] = strdup(buf); 302 if (list[num] == NULL) { 303 ret = errno; 304 goto quit3; 305 } 306 num++; 307 } 308 if (ret != ENOENT) 309 goto quit3; 310 /* get dir entries */ 311 while ((ret = _lookup_seq_next(cld, &key, &data)) == 0) { 312 if (!sorted) 313 snprintf(buf, sizeof(buf), "%.*s", 314 (int)_region_size(&key), 315 (const char *)_region_head(&key)); 316 else { 317 /* check duplicated entry */ 318 char *p; 319 char buf1[PATH_MAX]; 320 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) 327 *p = '\0'; 328 snprintf(buf, sizeof(buf), "%s/%.*s", buf1, 329 (int)_region_size(&key), 330 (const char *)_region_head(&key)); 331 } 332 _bcs_convert_to_upper(buf); 333 ret = _lookup_seq_lookup(cla, buf, NULL); 334 if (ret) { 335 if (ret != ENOENT) 336 goto quit3; 337 /* not duplicated */ 338 list[num] = strdup(buf); 339 if (list[num] == NULL) { 340 ret = errno; 341 goto quit3; 342 } 343 num++; 344 } 345 } 346 if (ret != ENOENT) 347 goto quit3; 348 349 ret = 0; 350 /* XXX: why reallocing the list space posteriorly? 351 shouldn't be done earlier? */ 352 q = realloc(list, num * sizeof(char *)); 353 if (!q) { 354 ret = ENOMEM; 355 goto quit3; 356 } 357 list = q; 358 *rlist = list; 359 *rnum = num; 360 quit3: 361 if (ret) 362 _citrus_esdb_free_list(list, num); 363 _lookup_seq_close(cld); 364 quit1: 365 _lookup_seq_close(cla); 366 quit0: 367 return (ret); 368 } 369