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