summaryrefslogtreecommitdiffstats
path: root/lib/libc/iconv/citrus_csmapper.c
blob: e2d10d5a4ba7cff5b8dcdd5b4e1984a95b07ece2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* $FreeBSD$ */
/* $NetBSD: citrus_csmapper.c,v 1.10 2009/01/11 02:46:24 christos Exp $ */

/*-
 * Copyright (c)2003 Citrus Project,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/endian.h>
#include <sys/types.h>
#include <sys/queue.h>

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "citrus_namespace.h"
#include "citrus_types.h"
#include "citrus_bcs.h"
#include "citrus_region.h"
#include "citrus_lock.h"
#include "citrus_memstream.h"
#include "citrus_mmap.h"
#include "citrus_module.h"
#include "citrus_hash.h"
#include "citrus_mapper.h"
#include "citrus_csmapper.h"
#include "citrus_pivot_file.h"
#include "citrus_db.h"
#include "citrus_db_hash.h"
#include "citrus_lookup.h"

static struct _citrus_mapper_area	*maparea = NULL;

static pthread_rwlock_t			ma_lock = PTHREAD_RWLOCK_INITIALIZER;

#define CS_ALIAS	_PATH_CSMAPPER "/charset.alias"
#define CS_PIVOT	_PATH_CSMAPPER "/charset.pivot"


/* ---------------------------------------------------------------------- */

static int
get32(struct _region *r, uint32_t *rval)
{

	if (_region_size(r) != 4)
		return (EFTYPE);

	memcpy(rval, _region_head(r), (size_t)4);
	*rval = be32toh(*rval);

	return (0);
}

static int
open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src)
{
	struct _region r;
	int ret;

	ret = _db_lookup_by_s(db, src, &r, NULL);
	if (ret)
		return (ret);
	ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL);
	if (ret)
		return (ret);

	return (0);
}


#define NO_SUCH_FILE	EOPNOTSUPP
static int
find_best_pivot_pvdb(const char *src, const char *dst, char *pivot,
    size_t pvlen, unsigned long *rnorm)
{
	struct _citrus_db *db1, *db2, *db3;
	struct _region fr, r1, r2;
	char buf[LINE_MAX];
	uint32_t val32;
	unsigned long norm;
	int i, num, ret;

	ret = _map_file(&fr, CS_PIVOT ".pvdb");
	if (ret) {
		if (ret == ENOENT)
			ret = NO_SUCH_FILE;
		return (ret);
	}
	ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL);
	if (ret)
		goto quit1;
	ret = open_subdb(&db2, db1, src);
	if (ret)
		goto quit2;

	num = _db_get_num_entries(db2);
	*rnorm = ULONG_MAX;
	for (i = 0; i < num; i++) {
		/* iterate each pivot */
		ret = _db_get_entry(db2, i, &r1, &r2);
		if (ret)
			goto quit3;
		/* r1:pivot name, r2:norm among src and pivot */
		ret = get32(&r2, &val32);
		if (ret)
			goto quit3;
		norm = val32;
		snprintf(buf, sizeof(buf), "%.*s",
			 (int)_region_size(&r1), (char *)_region_head(&r1));
		/* buf: pivot name */
		ret = open_subdb(&db3, db1, buf);
		if (ret)
			goto quit3;
		if (_db_lookup_by_s(db3, dst, &r2, NULL) != 0)
			goto quit4;
		/* r2: norm among pivot and dst */
		ret = get32(&r2, &val32);
		if (ret)
			goto quit4;
		norm += val32;
		/* judge minimum norm */
		if (norm < *rnorm) {
			*rnorm = norm;
			strlcpy(pivot, buf, pvlen);
		}
quit4:
		_db_close(db3);
		if (ret)
			goto quit3;
	}
quit3:
	_db_close(db2);
quit2:
	_db_close(db1);
quit1:
	_unmap_file(&fr);
	if (ret)
		return (ret);

	if (*rnorm == ULONG_MAX)
		return (ENOENT);

	return (0);
}

/* ---------------------------------------------------------------------- */

struct zone {
	const char *begin, *end;
};

struct parse_arg {
	char dst[PATH_MAX];
	unsigned long norm;
};

static int
parse_line(struct parse_arg *pa, struct _region *r)
{
	struct zone z1, z2;
	char buf[20];
	size_t len;

	len = _region_size(r);
	z1.begin = _bcs_skip_ws_len(_region_head(r), &len);
	if (len == 0)
		return (EFTYPE);
	z1.end = _bcs_skip_nonws_len(z1.begin, &len);
	if (len == 0)
		return (EFTYPE);
	z2.begin = _bcs_skip_ws_len(z1.end, &len);
	if (len == 0)
		return (EFTYPE);
	z2.end = _bcs_skip_nonws_len(z2.begin, &len);

	/* z1 : dst name, z2 : norm */
	snprintf(pa->dst, sizeof(pa->dst),
	    "%.*s", (int)(z1.end-z1.begin), z1.begin);
	snprintf(buf, sizeof(buf),
	    "%.*s", (int)(z2.end-z2.begin), z2.begin);
	pa->norm = _bcs_strtoul(buf, NULL, 0);

	return (0);
}

static int
find_dst(struct parse_arg *pasrc, const char *dst)
{
	struct _lookup *cl;
	struct parse_arg padst;
	struct _region data;
	int ret;

	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
	if (ret)
		return (ret);

	ret = _lookup_seq_lookup(cl, pasrc->dst, &data);
	while (ret == 0) {
		ret = parse_line(&padst, &data);
		if (ret)
			break;
		if (strcmp(dst, padst.dst) == 0) {
			pasrc->norm += padst.norm;
			break;
		}
		ret = _lookup_seq_next(cl, NULL, &data);
	}
	_lookup_seq_close(cl);

	return (ret);
}

static int
find_best_pivot_lookup(const char *src, const char *dst, char *pivot,
    size_t pvlen, unsigned long *rnorm)
{
	struct _lookup *cl;
	struct _region data;
	struct parse_arg pa;
	char pivot_min[PATH_MAX];
	unsigned long norm_min;
	int ret;

	ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
	if (ret)
		return (ret);

	norm_min = ULONG_MAX;

	/* find pivot code */
	ret = _lookup_seq_lookup(cl, src, &data);
	while (ret == 0) {
		ret = parse_line(&pa, &data);
		if (ret)
			break;
		ret = find_dst(&pa, dst);
		if (ret)
			break;
		if (pa.norm < norm_min) {
			norm_min = pa.norm;
			strlcpy(pivot_min, pa.dst, sizeof(pivot_min));
		}
		ret = _lookup_seq_next(cl, NULL, &data);
	}
	_lookup_seq_close(cl);

	if (ret != ENOENT)
		return (ret);
	if (norm_min == ULONG_MAX)
		return (ENOENT);
	strlcpy(pivot, pivot_min, pvlen);
	if (rnorm)
		*rnorm = norm_min;

	return (0);
}

static int
find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen,
    unsigned long *rnorm)
{
	int ret;

	ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm);
	if (ret == NO_SUCH_FILE)
		ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm);

	return (ret);
}

static __inline int
open_serial_mapper(struct _citrus_mapper_area *__restrict ma,
    struct _citrus_mapper * __restrict * __restrict rcm,
    const char *src, const char *pivot, const char *dst)
{
	char buf[PATH_MAX];

	snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst);

	return (_mapper_open_direct(ma, rcm, "mapper_serial", buf));
}

static struct _citrus_csmapper *csm_none = NULL;
static int
get_none(struct _citrus_mapper_area *__restrict ma,
    struct _citrus_csmapper *__restrict *__restrict rcsm)
{
	int ret;

	WLOCK(&ma_lock);
	if (csm_none) {
		*rcsm = csm_none;
		ret = 0;
		goto quit;
	}

	ret = _mapper_open_direct(ma, &csm_none, "mapper_none", "");
	if (ret)
		goto quit;
	_mapper_set_persistent(csm_none);

	*rcsm = csm_none;
	ret = 0;
quit:
	UNLOCK(&ma_lock);
	return (ret);
}

int
_citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm,
    const char * __restrict src, const char * __restrict dst, uint32_t flags,
    unsigned long *rnorm)
{
	const char *realsrc, *realdst;
	char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX];
	unsigned long norm;
	int ret;

	norm = 0;

	ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER);
	if (ret)
		return (ret);

	realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1),
	    _LOOKUP_CASE_IGNORE);
	realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2),
	    _LOOKUP_CASE_IGNORE);
	if (!strcmp(realsrc, realdst)) {
		ret = get_none(maparea, rcsm);
		if (ret == 0 && rnorm != NULL)
			*rnorm = 0;
		return (ret);
	}

	snprintf(key, sizeof(key), "%s/%s", realsrc, realdst);

	ret = _mapper_open(maparea, rcsm, key);
	if (ret == 0) {
		if (rnorm != NULL)
			*rnorm = 0;
		return (0);
	}
	if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0)
		return (ret);

	ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm);
	if (ret)
		return (ret);

	ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst);
	if (ret == 0 && rnorm != NULL)
		*rnorm = norm;

	return (ret);
}
OpenPOWER on IntegriCloud