Home | History | Annotate | Line # | Download | only in citrus
      1 /*	$NetBSD: citrus_csmapper.c,v 1.13 2025/12/16 12:39:01 nia 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.13 2025/12/16 12:39:01 nia 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/endian.h>
     45 #include <sys/queue.h>
     46 
     47 #include "citrus_namespace.h"
     48 #include "citrus_types.h"
     49 #include "citrus_bcs.h"
     50 #include "citrus_region.h"
     51 #include "citrus_memstream.h"
     52 #include "citrus_mmap.h"
     53 #include "citrus_module.h"
     54 #include "citrus_hash.h"
     55 #include "citrus_mapper.h"
     56 #include "citrus_csmapper.h"
     57 #include "citrus_pivot_file.h"
     58 #include "citrus_db.h"
     59 #include "citrus_db_hash.h"
     60 #include "citrus_lookup.h"
     61 
     62 #ifdef _REENTRANT
     63 static rwlock_t lock = RWLOCK_INITIALIZER;
     64 #endif
     65 static struct _citrus_mapper_area *maparea = NULL;
     66 
     67 #define CS_ALIAS	_PATH_CSMAPPER "/charset.alias"
     68 #define CS_PIVOT	_PATH_CSMAPPER "/charset.pivot"
     69 
     70 
     71 /* ---------------------------------------------------------------------- */
     72 
     73 static int
     74 get32(struct _region *r, uint32_t *rval)
     75 {
     76 	if (_region_size(r) != 4)
     77 		return EFTYPE;
     78 
     79 	memcpy(rval, _region_head(r), (size_t)4);
     80 	*rval = be32toh(*rval);
     81 
     82 	return 0;
     83 }
     84 
     85 static int
     86 open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src)
     87 {
     88 	int ret;
     89 	struct _region r;
     90 
     91 	ret = _db_lookup_by_s(db, src, &r, NULL);
     92 	if (ret)
     93 		return ret;
     94 	ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL);
     95 	if (ret)
     96 		return ret;
     97 
     98 	return 0;
     99 }
    100 
    101 
    102 #define NO_SUCH_FILE	EOPNOTSUPP
    103 static int
    104 find_best_pivot_pvdb(const char *src, const char *dst, char *pivot,
    105 		     size_t pvlen, unsigned long *rnorm)
    106 {
    107 	int ret, num, i;
    108 	struct _region fr, r1, r2;
    109 	struct _citrus_db *db1, *db2, *db3;
    110 	char buf[LINE_MAX];
    111 	unsigned long norm;
    112 	uint32_t val32;
    113 
    114 	ret = _map_file(&fr, CS_PIVOT ".pvdb");
    115 	if (ret) {
    116 		if (ret == ENOENT)
    117 			ret = NO_SUCH_FILE;
    118 		return ret;
    119 	}
    120 	ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL);
    121 	if (ret)
    122 		goto quit1;
    123 	ret = open_subdb(&db2, db1, src);
    124 	if (ret)
    125 		goto quit2;
    126 
    127 	num = _db_get_num_entries(db2);
    128 	*rnorm = ULONG_MAX;
    129 	for (i = 0; i < num; i++) {
    130 		/* iterate each pivot */
    131 		ret = _db_get_entry(db2, i, &r1, &r2);
    132 		if (ret)
    133 			goto quit3;
    134 		/* r1:pivot name, r2:norm among src and pivot */
    135 		ret = get32(&r2, &val32);
    136 		if (ret)
    137 			goto quit3;
    138 		norm = val32;
    139 		snprintf(buf, sizeof(buf), "%.*s",
    140 			 (int)_region_size(&r1), (char *)_region_head(&r1));
    141 		/* buf: pivot name */
    142 		ret = open_subdb(&db3, db1, buf);
    143 		if (ret)
    144 			goto quit3;
    145 		if (_db_lookup_by_s(db3, dst, &r2, NULL) != 0)
    146 			/* don't break the loop, test all src/dst pairs. */
    147 			goto quit4;
    148 		/* r2: norm among pivot and dst */
    149 		ret = get32(&r2, &val32);
    150 		if (ret)
    151 			goto quit4;
    152 		norm += val32;
    153 		/* judge minimum norm */
    154 		if (norm < *rnorm) {
    155 			*rnorm = norm;
    156 			strlcpy(pivot, buf, pvlen);
    157 		}
    158 quit4:
    159 		_db_close(db3);
    160 		if (ret)
    161 			goto quit3;
    162 	}
    163 quit3:
    164 	_db_close(db2);
    165 quit2:
    166 	_db_close(db1);
    167 quit1:
    168 	_unmap_file(&fr);
    169 	if (ret)
    170 		return ret;
    171 
    172 	if (*rnorm == ULONG_MAX)
    173 		return ENOENT;
    174 
    175 	return 0;
    176 }
    177 
    178 /* ---------------------------------------------------------------------- */
    179 
    180 struct zone {
    181 	const char *begin, *end;
    182 };
    183 
    184 struct parse_arg {
    185 	char dst[PATH_MAX];
    186 	unsigned long norm;
    187 };
    188 
    189 static int
    190 parse_line(struct parse_arg *pa, struct _region *r)
    191 {
    192 	char buf[20];
    193 	struct zone z1, z2;
    194 	size_t len;
    195 
    196 	len = _region_size(r);
    197 	z1.begin = _bcs_skip_ws_len(_region_head(r), &len);
    198 	if (len == 0)
    199 		return EFTYPE;
    200 	z1.end = _bcs_skip_nonws_len(z1.begin, &len);
    201 	if (len == 0)
    202 		return EFTYPE;
    203 	z2.begin = _bcs_skip_ws_len(z1.end, &len);
    204 	if (len == 0)
    205 		return EFTYPE;
    206 	z2.end = _bcs_skip_nonws_len(z2.begin, &len);
    207 
    208 	/* z1 : dst name, z2 : norm */
    209 	snprintf(pa->dst, sizeof(pa->dst),
    210 		 "%.*s", (int)(z1.end-z1.begin), z1.begin);
    211 	snprintf(buf, sizeof(buf),
    212 		 "%.*s", (int)(z2.end-z2.begin), z2.begin);
    213 	pa->norm = _bcs_strtoul(buf, NULL, 0);
    214 
    215 	return 0;
    216 }
    217 
    218 static int
    219 find_dst(struct parse_arg *pasrc, const char *dst)
    220 {
    221 	int ret;
    222 	struct parse_arg padst;
    223 	struct _lookup *cl;
    224 	struct _region data;
    225 
    226 	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
    227 	if (ret)
    228 		return ret;
    229 
    230 	ret = _lookup_seq_lookup(cl, pasrc->dst, &data);
    231 	while (ret == 0) {
    232 		ret = parse_line(&padst, &data);
    233 		if (ret)
    234 			break;
    235 		if (strcmp(dst, padst.dst) == 0) {
    236 			pasrc->norm += padst.norm;
    237 			break;
    238 		}
    239 		ret = _lookup_seq_next(cl, NULL, &data);
    240 	}
    241 	_lookup_seq_close(cl);
    242 
    243 	return ret;
    244 }
    245 
    246 static int
    247 find_best_pivot_lookup(const char *src, const char *dst, char *pivot,
    248 		       size_t pvlen, unsigned long *rnorm)
    249 {
    250 	int ret;
    251 	struct _lookup *cl;
    252 	struct _region data;
    253 	struct parse_arg pa;
    254 	unsigned long norm_min;
    255 	char pivot_min[PATH_MAX];
    256 
    257 	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
    258 	if (ret)
    259 		return ret;
    260 
    261 	norm_min = ULONG_MAX;
    262 
    263 	/* find pivot code */
    264 	ret = _lookup_seq_lookup(cl, src, &data);
    265 	while (ret == 0) {
    266 		ret = parse_line(&pa, &data);
    267 		if (ret)
    268 			break;
    269 		ret = find_dst(&pa, dst);
    270 		if (ret)
    271 			break;
    272 		if (pa.norm < norm_min) {
    273 			norm_min = pa.norm;
    274 			strlcpy(pivot_min, pa.dst, sizeof(pivot_min));
    275 		}
    276 		ret = _lookup_seq_next(cl, NULL, &data);
    277 	}
    278 	_lookup_seq_close(cl);
    279 
    280 	if (ret != ENOENT)
    281 		return ret;
    282 	if (norm_min == ULONG_MAX)
    283 		return ENOENT;
    284 	strlcpy(pivot, pivot_min, pvlen);
    285 	if (rnorm)
    286 		*rnorm = norm_min;
    287 
    288 	return 0;
    289 }
    290 
    291 static int
    292 find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen,
    293 		unsigned long *rnorm)
    294 {
    295 	int ret;
    296 
    297 	ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm);
    298 	if (ret == NO_SUCH_FILE)
    299 		ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm);
    300 
    301 	return ret;
    302 }
    303 
    304 static __inline int
    305 open_serial_mapper(struct _citrus_mapper_area *__restrict ma,
    306 		   struct _citrus_mapper * __restrict * __restrict rcm,
    307 		   const char *src, const char *pivot, const char *dst)
    308 {
    309 	char buf[4 * PATH_MAX];
    310 
    311 	snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst);
    312 
    313 	return _mapper_open_direct(ma, rcm, "mapper_serial", buf);
    314 }
    315 
    316 static struct _citrus_csmapper *csm_none = NULL;
    317 static int
    318 get_none(struct _citrus_mapper_area *__restrict ma,
    319 	 struct _citrus_csmapper *__restrict *__restrict rcsm)
    320 {
    321 	int ret;
    322 
    323 	rwlock_wrlock(&lock);
    324 	if (csm_none) {
    325 		*rcsm = csm_none;
    326 		ret = 0;
    327 		goto quit;
    328 	}
    329 
    330 	ret = _mapper_open_direct(ma, &csm_none, "mapper_none", "");
    331 	if (ret)
    332 		goto quit;
    333 	_mapper_set_persistent(csm_none);
    334 
    335 	*rcsm = csm_none;
    336 	ret = 0;
    337 quit:
    338 	rwlock_unlock(&lock);
    339 	return ret;
    340 }
    341 
    342 int
    343 _citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm,
    344 		      const char * __restrict src, const char * __restrict dst,
    345 		      uint32_t flags, unsigned long *rnorm)
    346 {
    347 	int ret;
    348 	char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX];
    349 	const char *realsrc, *realdst;
    350 	unsigned long norm;
    351 
    352 	norm = 0;	/* XXX gcc */
    353 
    354 	ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER);
    355 	if (ret)
    356 		return ret;
    357 
    358 	realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1),
    359 				_LOOKUP_CASE_IGNORE);
    360 	realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2),
    361 				_LOOKUP_CASE_IGNORE);
    362 	if (!strcmp(realsrc, realdst)) {
    363 		ret = get_none(maparea, rcsm);
    364 		if (ret == 0 && rnorm != NULL)
    365 			*rnorm = 0;
    366 		return ret;
    367 	}
    368 
    369 	snprintf(key, sizeof(key), "%s/%s", realsrc, realdst);
    370 
    371 	ret = _mapper_open(maparea, rcsm, key);
    372 	if (ret == 0) {
    373 		if (rnorm != NULL)
    374 			*rnorm = 0;
    375 		return 0;
    376 	}
    377 	if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0)
    378 		return ret;
    379 
    380 	ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm);
    381 	if (ret)
    382 		return ret;
    383 
    384 	ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst);
    385 	if (ret == 0 && rnorm != NULL)
    386 		*rnorm = norm;
    387 
    388 	return ret;
    389 }
    390