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