Home | History | Annotate | Line # | Download | only in citrus
citrus_csmapper.c revision 1.7
      1 /*	$NetBSD: citrus_csmapper.c,v 1.7 2007/02/17 12:39:44 tnozaki Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2003 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 __RCSID("$NetBSD: citrus_csmapper.c,v 1.7 2007/02/17 12:39:44 tnozaki Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include "namespace.h"
     35 #include "reentrant.h"
     36 #include <assert.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <errno.h>
     41 #include <limits.h>
     42 #include <paths.h>
     43 #include <sys/types.h>
     44 #include <sys/queue.h>
     45 
     46 #include "citrus_namespace.h"
     47 #include "citrus_types.h"
     48 #include "citrus_bcs.h"
     49 #include "citrus_region.h"
     50 #include "citrus_memstream.h"
     51 #include "citrus_mmap.h"
     52 #include "citrus_module.h"
     53 #include "citrus_hash.h"
     54 #include "citrus_mapper.h"
     55 #include "citrus_csmapper.h"
     56 #include "citrus_pivot_file.h"
     57 #include "citrus_db.h"
     58 #include "citrus_db_hash.h"
     59 #include "citrus_lookup.h"
     60 
     61 #ifdef _REENTRANT
     62 static rwlock_t lock = RWLOCK_INITIALIZER;
     63 #endif
     64 static struct _citrus_mapper_area *maparea = NULL;
     65 
     66 #define CS_ALIAS	_PATH_CSMAPPER "/charset.alias"
     67 #define CS_PIVOT	_PATH_CSMAPPER "/charset.pivot"
     68 
     69 
     70 /* ---------------------------------------------------------------------- */
     71 
     72 static int
     73 get32(struct _region *r, u_int32_t *rval)
     74 {
     75 	if (_region_size(r) != 4)
     76 		return EFTYPE;
     77 
     78 	memcpy(rval, _region_head(r), (size_t)4);
     79 	*rval = be32toh(*rval);
     80 
     81 	return 0;
     82 }
     83 
     84 static int
     85 open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src)
     86 {
     87 	int ret;
     88 	struct _region r;
     89 
     90 	ret = _db_lookup_by_s(db, src, &r, NULL);
     91 	if (ret)
     92 		return ret;
     93 	ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL);
     94 	if (ret)
     95 		return ret;
     96 
     97 	return 0;
     98 }
     99 
    100 
    101 #define NO_SUCH_FILE	EOPNOTSUPP
    102 static int
    103 find_best_pivot_pvdb(const char *src, const char *dst, char *pivot,
    104 		     size_t pvlen, unsigned long *rnorm)
    105 {
    106 	int ret, num, i;
    107 	struct _region fr, r1, r2;
    108 	struct _citrus_db *db1, *db2, *db3;
    109 	char buf[LINE_MAX];
    110 	unsigned long norm;
    111 	u_int32_t val32;
    112 
    113 	ret = _map_file(&fr, CS_PIVOT ".pvdb");
    114 	if (ret) {
    115 		if (ret == ENOENT)
    116 			ret = NO_SUCH_FILE;
    117 		return ret;
    118 	}
    119 	ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL);
    120 	if (ret)
    121 		goto quit1;
    122 	ret = open_subdb(&db2, db1, src);
    123 	if (ret)
    124 		goto quit2;
    125 
    126 	num = _db_get_num_entries(db2);
    127 	*rnorm = ULONG_MAX;
    128 	for (i = 0; i < num; i++) {
    129 		/* iterate each pivot */
    130 		ret = _db_get_entry(db2, i, &r1, &r2);
    131 		if (ret)
    132 			goto quit3;
    133 		/* r1:pivot name, r2:norm among src and pivot */
    134 		ret = get32(&r2, &val32);
    135 		if (ret)
    136 			goto quit3;
    137 		norm = val32;
    138 		snprintf(buf, sizeof(buf), "%.*s",
    139 			 (int)_region_size(&r1), (char *)_region_head(&r1));
    140 		/* buf: pivot name */
    141 		ret = open_subdb(&db3, db1, buf);
    142 		if (ret)
    143 			goto quit3;
    144 		if (_db_lookup_by_s(db3, dst, &r2, NULL) != 0)
    145 			goto quit4;
    146 		/* r2: norm among pivot and dst */
    147 		ret = get32(&r2, &val32);
    148 		if (ret)
    149 			goto quit4;
    150 		norm += val32;
    151 		/* judge minimum norm */
    152 		if (norm < *rnorm) {
    153 			*rnorm = norm;
    154 			strlcpy(pivot, buf, pvlen);
    155 		}
    156 quit4:
    157 		_db_close(db3);
    158 		if (ret)
    159 			goto quit3;
    160 	}
    161 quit3:
    162 	_db_close(db2);
    163 quit2:
    164 	_db_close(db1);
    165 quit1:
    166 	_unmap_file(&fr);
    167 	if (ret)
    168 		return ret;
    169 
    170 	if (*rnorm == ULONG_MAX)
    171 		return ENOENT;
    172 
    173 	return 0;
    174 }
    175 
    176 /* ---------------------------------------------------------------------- */
    177 
    178 struct zone {
    179 	const char *begin, *end;
    180 };
    181 
    182 struct parse_arg {
    183 	char dst[PATH_MAX];
    184 	unsigned long norm;
    185 };
    186 
    187 static int
    188 parse_line(struct parse_arg *pa, struct _region *r)
    189 {
    190 	char buf[20];
    191 	struct zone z1, z2;
    192 	size_t len;
    193 
    194 	len = _region_size(r);
    195 	z1.begin = _bcs_skip_ws_len(_region_head(r), &len);
    196 	if (len == 0)
    197 		return EFTYPE;
    198 	z1.end = _bcs_skip_nonws_len(z1.begin, &len);
    199 	if (len == 0)
    200 		return EFTYPE;
    201 	z2.begin = _bcs_skip_ws_len(z1.end, &len);
    202 	if (len == 0)
    203 		return EFTYPE;
    204 	z2.end = _bcs_skip_nonws_len(z2.begin, &len);
    205 
    206 	/* z1 : dst name, z2 : norm */
    207 	snprintf(pa->dst, sizeof(pa->dst),
    208 		 "%.*s", (int)(z1.end-z1.begin), z1.begin);
    209 	snprintf(buf, sizeof(buf),
    210 		 "%.*s", (int)(z2.end-z2.begin), z2.begin);
    211 	pa->norm = strtoul(buf, NULL, 0);
    212 
    213 	return 0;
    214 }
    215 
    216 static int
    217 find_dst(struct parse_arg *pasrc, const char *dst)
    218 {
    219 	int ret;
    220 	struct parse_arg padst;
    221 	struct _lookup *cl;
    222 	struct _region data;
    223 
    224 	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
    225 	if (ret)
    226 		return ret;
    227 
    228 	ret = _lookup_seq_lookup(cl, pasrc->dst, &data);
    229 	while (ret == 0) {
    230 		ret = parse_line(&padst, &data);
    231 		if (ret)
    232 			break;
    233 		if (strcmp(dst, padst.dst) == 0) {
    234 			pasrc->norm += padst.norm;
    235 			break;
    236 		}
    237 		ret = _lookup_seq_next(cl, NULL, &data);
    238 	}
    239 	_lookup_seq_close(cl);
    240 
    241 	return ret;
    242 }
    243 
    244 static int
    245 find_best_pivot_lookup(const char *src, const char *dst, char *pivot,
    246 		       size_t pvlen, unsigned long *rnorm)
    247 {
    248 	int ret;
    249 	struct _lookup *cl;
    250 	struct _region data;
    251 	struct parse_arg pa;
    252 	unsigned long norm_min;
    253 	char pivot_min[PATH_MAX];
    254 
    255 	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
    256 	if (ret)
    257 		return ret;
    258 
    259 	norm_min = ULONG_MAX;
    260 
    261 	/* find pivot code */
    262 	ret = _lookup_seq_lookup(cl, src, &data);
    263 	while (ret == 0) {
    264 		ret = parse_line(&pa, &data);
    265 		if (ret)
    266 			break;
    267 		ret = find_dst(&pa, dst);
    268 		if (ret)
    269 			break;
    270 		if (pa.norm < norm_min) {
    271 			norm_min = pa.norm;
    272 			strlcpy(pivot_min, pa.dst, sizeof(pivot_min));
    273 		}
    274 		ret = _lookup_seq_next(cl, NULL, &data);
    275 	}
    276 	_lookup_seq_close(cl);
    277 
    278 	if (ret != ENOENT)
    279 		return ret;
    280 	if (norm_min == ULONG_MAX)
    281 		return ENOENT;
    282 	strlcpy(pivot, pivot_min, pvlen);
    283 	if (rnorm)
    284 		*rnorm = norm_min;
    285 
    286 	return 0;
    287 }
    288 
    289 static int
    290 find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen,
    291 		unsigned long *rnorm)
    292 {
    293 	int ret;
    294 
    295 	ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm);
    296 	if (ret == NO_SUCH_FILE)
    297 		ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm);
    298 
    299 	return ret;
    300 }
    301 
    302 static __inline int
    303 open_serial_mapper(struct _citrus_mapper_area *__restrict ma,
    304 		   struct _citrus_mapper * __restrict * __restrict rcm,
    305 		   const char *src, const char *pivot, const char *dst)
    306 {
    307 	char buf[PATH_MAX];
    308 
    309 	snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst);
    310 
    311 	return _mapper_open_direct(ma, rcm, "mapper_serial", buf);
    312 }
    313 
    314 static struct _citrus_csmapper *csm_none = NULL;
    315 static int
    316 get_none(struct _citrus_mapper_area *__restrict ma,
    317 	 struct _citrus_csmapper *__restrict *__restrict rcsm)
    318 {
    319 	int ret;
    320 
    321 	rwlock_wrlock(&lock);
    322 	if (csm_none) {
    323 		*rcsm = csm_none;
    324 		ret = 0;
    325 		goto quit;
    326 	}
    327 
    328 	ret = _mapper_open_direct(ma, &csm_none, "mapper_none", "");
    329 	if (ret)
    330 		goto quit;
    331 	_mapper_set_persistent(csm_none);
    332 
    333 	*rcsm = csm_none;
    334 	ret = 0;
    335 quit:
    336 	rwlock_unlock(&lock);
    337 	return ret;
    338 }
    339 
    340 int
    341 _citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm,
    342 		      const char * __restrict src, const char * __restrict dst,
    343 		      u_int32_t flags, unsigned long *rnorm)
    344 {
    345 	int ret;
    346 	char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX];
    347 	const char *realsrc, *realdst;
    348 	unsigned long norm;
    349 
    350 	norm = 0;	/* XXX gcc */
    351 
    352 	ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER);
    353 	if (ret)
    354 		return ret;
    355 
    356 	realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1),
    357 				_LOOKUP_CASE_IGNORE);
    358 	realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2),
    359 				_LOOKUP_CASE_IGNORE);
    360 	if (!strcmp(realsrc, realdst)) {
    361 		ret = get_none(maparea, rcsm);
    362 		if (ret == 0 && rnorm != NULL)
    363 			*rnorm = 0;
    364 		return ret;
    365 	}
    366 
    367 	snprintf(key, sizeof(key), "%s/%s", realsrc, realdst);
    368 
    369 	ret = _mapper_open(maparea, rcsm, key);
    370 	if (ret == 0) {
    371 		if (rnorm != NULL)
    372 			*rnorm = 0;
    373 		return 0;
    374 	}
    375 	if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0)
    376 		return ret;
    377 
    378 	ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm);
    379 	if (ret)
    380 		return ret;
    381 
    382 	ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst);
    383 	if (ret == 0 && rnorm != NULL)
    384 		*rnorm = norm;
    385 
    386 	return ret;
    387 }
    388