960f6955eda2c1645f776249d847bc721c9f1dfc
[dragonfly.git] / lib / libc / citrus / citrus_csmapper.c
1 /*      $NetBSD: src/lib/libc/citrus/citrus_csmapper.c,v 1.5 2004/12/30 05:01:50 christos Exp $ */
2 /*      $DragonFly: src/lib/libc/citrus/citrus_csmapper.c,v 1.1 2005/03/11 23:33:53 joerg 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/types.h>
32 #include <sys/endian.h>
33 #include <sys/queue.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <paths.h>
38 #include <pthread.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "un-namespace.h"
43
44 #include "libc_private.h"
45
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_bcs.h"
49 #include "citrus_region.h"
50 #include "citrus_memstream.h"
51 #include "citrus_mmap.h"
52 #include "citrus_module.h"
53 #include "citrus_hash.h"
54 #include "citrus_mapper.h"
55 #include "citrus_csmapper.h"
56 #include "citrus_pivot_file.h"
57 #include "citrus_db.h"
58 #include "citrus_db_hash.h"
59 #include "citrus_lookup.h"
60
61 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
62 static struct _citrus_mapper_area *maparea = NULL;
63
64 #define CS_ALIAS        _PATH_CSMAPPER "/charset.alias"
65 #define CS_PIVOT        _PATH_CSMAPPER "/charset.pivot"
66
67
68 /* ---------------------------------------------------------------------- */
69
70 static int
71 get32(struct _region *r, uint32_t *rval)
72 {
73         if (_region_size(r) != 4)
74                 return EFTYPE;
75
76         memcpy(rval, _region_head(r), 4);
77         *rval = be32toh(*rval);
78
79         return 0;
80 }
81
82 static int
83 open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src)
84 {
85         int ret;
86         struct _region r;
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
100 static int
101 find_best_pivot_pvdb(const char *src, const char *dst, char *pivot,
102                      size_t pvlen, unsigned long *rnorm)
103 {
104         int ret, num, i;
105         struct _region fr, r1, r2;
106         struct _citrus_db *db1, *db2, *db3;
107         char buf[LINE_MAX];
108         unsigned long norm;
109         uint32_t val32;
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                 ret = _db_lookup_by_s(db3, dst, &r2, NULL);
143                 if (ret)
144                         goto quit4;
145                 /* r2: norm among pivot and dst */
146                 ret = get32(&r2, &val32);
147                 if (ret)
148                         goto quit4;
149                 norm += val32;
150                 /* judge minimum norm */
151                 if (norm < *rnorm) {
152                         *rnorm = norm;
153                         strlcpy(pivot, buf, pvlen);
154                 }
155 quit4:
156                 _db_close(db3);
157                 if (ret)
158                         goto quit3;
159         }
160 quit3:
161         _db_close(db2);
162 quit2:
163         _db_close(db1);
164 quit1:
165         _unmap_file(&fr);
166         if (ret)
167                 return ret;
168
169         if (*rnorm == ULONG_MAX)
170                 return ENOENT;
171
172         return 0;
173 }
174
175 /* ---------------------------------------------------------------------- */
176
177 struct zone {
178         const char *begin, *end;
179 };
180
181 struct parse_arg {
182         char dst[PATH_MAX];
183         unsigned long norm;
184 };
185
186 static int
187 parse_line(struct parse_arg *pa, struct _region *r)
188 {
189         char buf[20];
190         struct zone z1, z2;
191         size_t len;
192
193         len = _region_size(r);
194         z1.begin = _bcs_skip_ws_len(_region_head(r), &len);
195         if (len == 0)
196                 return EFTYPE;
197         z1.end = _bcs_skip_nonws_len(z1.begin, &len);
198         if (len == 0)
199                 return EFTYPE;
200         z2.begin = _bcs_skip_ws_len(z1.end, &len);
201         if (len == 0)
202                 return EFTYPE;
203         z2.end = _bcs_skip_nonws_len(z2.begin, &len);
204
205         /* z1 : dst name, z2 : norm */
206         snprintf(pa->dst, sizeof(pa->dst),
207                  "%.*s", (int)(z1.end-z1.begin), z1.begin);
208         snprintf(buf, sizeof(buf),
209                  "%.*s", (int)(z2.end-z2.begin), z2.begin);
210         pa->norm = strtoul(buf, NULL, 0);
211
212         return 0;
213 }
214
215 static int
216 find_dst(struct parse_arg *pasrc, const char *dst)
217 {
218         int ret;
219         struct parse_arg padst;
220         struct _lookup *cl;
221         struct _region data;
222
223         ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
224         if (ret)
225                 return ret;
226
227         ret = _lookup_seq_lookup(cl, pasrc->dst, &data);
228         while (ret == 0) {
229                 ret = parse_line(&padst, &data);
230                 if (ret)
231                         break;
232                 if (strcmp(dst, padst.dst) == 0) {
233                         pasrc->norm += padst.norm;
234                         break;
235                 }
236                 ret = _lookup_seq_next(cl, NULL, &data);
237         }
238         _lookup_seq_close(cl);
239
240         return ret;
241 }
242
243 static int
244 find_best_pivot_lookup(const char *src, const char *dst, char *pivot,
245                        size_t pvlen, unsigned long *rnorm)
246 {
247         int ret;
248         struct _lookup *cl;
249         struct _region data;
250         struct parse_arg pa;
251         unsigned long norm_min;
252         char pivot_min[PATH_MAX];
253
254         ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE);
255         if (ret)
256                 return ret;
257
258         norm_min = ULONG_MAX;
259
260         /* find pivot code */
261         ret = _lookup_seq_lookup(cl, src, &data);
262         while (ret == 0) {
263                 ret = parse_line(&pa, &data);
264                 if (ret)
265                         break;
266                 ret = find_dst(&pa, dst);
267                 if (ret)
268                         break;
269                 if (pa.norm < norm_min) {
270                         norm_min = pa.norm;
271                         strlcpy(pivot_min, pa.dst, sizeof(pivot_min));
272                 }
273                 ret = _lookup_seq_next(cl, NULL, &data);
274         }
275         _lookup_seq_close(cl);
276
277         if (ret != ENOENT)
278                 return ret;
279         if (norm_min == ULONG_MAX)
280                 return ENOENT;
281         strlcpy(pivot, pivot_min, pvlen);
282         if (rnorm)
283                 *rnorm = norm_min;
284
285         return 0;
286 }
287
288 static int
289 find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen,
290                 unsigned long *rnorm)
291 {
292         int ret;
293
294         ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm);
295         if (ret == NO_SUCH_FILE)
296                 ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm);
297
298         return ret;
299 }
300
301 static __inline int
302 open_serial_mapper(struct _citrus_mapper_area *__restrict ma,
303                    struct _citrus_mapper * __restrict * __restrict rcm,
304                    const char *src, const char *pivot, const char *dst)
305 {
306         char buf[PATH_MAX];
307
308         snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst);
309
310         return _mapper_open_direct(ma, rcm, "mapper_serial", buf);
311 }
312
313 static struct _citrus_csmapper *csm_none = NULL;
314 static int
315 get_none(struct _citrus_mapper_area *__restrict ma,
316          struct _citrus_csmapper *__restrict *__restrict rcsm)
317 {
318         int ret;
319
320         if (__isthreaded)
321                 _pthread_mutex_lock(&lock);
322
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         if (__isthreaded)
338                 _pthread_mutex_unlock(&lock);
339
340         return ret;
341 }
342
343 int
344 _citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm,
345                       const char * __restrict src, const char * __restrict dst,
346                       uint32_t flags, unsigned long *rnorm)
347 {
348         int ret;
349         char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX];
350         const char *realsrc, *realdst;
351         unsigned long norm;
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 }