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