Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / modules / citrus_mapper_std.c
1 /* $NetBSD: citrus_mapper_std.c,v 1.8 2006/09/11 13:06:33 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
3
4 /*-
5  * Copyright (c)2003, 2006 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/types.h>
31 #include <sys/endian.h>
32 #include <sys/queue.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <string.h>
40
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_region.h"
45 #include "citrus_mmap.h"
46 #include "citrus_module.h"
47 #include "citrus_hash.h"
48 #include "citrus_mapper.h"
49 #include "citrus_db.h"
50 #include "citrus_db_hash.h"
51
52 #include "citrus_mapper_std.h"
53 #include "citrus_mapper_std_file.h"
54
55 /* ---------------------------------------------------------------------- */
56
57 _CITRUS_MAPPER_DECLS(mapper_std);
58 _CITRUS_MAPPER_DEF_OPS(mapper_std);
59
60
61 /* ---------------------------------------------------------------------- */
62
63 int
64 _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops,
65                                  u_int32_t expected_version)
66 {
67         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
68                 return (EINVAL);
69
70         memcpy(ops, &_citrus_mapper_std_mapper_ops,
71                sizeof(_citrus_mapper_std_mapper_ops));
72
73         return (0);
74 }
75
76 /* ---------------------------------------------------------------------- */
77
78 static int
79 /*ARGSUSED*/
80 rowcol_convert(struct _citrus_mapper_std * __restrict ms,
81                _index_t * __restrict dst, _index_t src,
82                void * __restrict ps)
83 {
84         struct _citrus_mapper_std_rowcol *rc;
85         size_t i;
86         struct _citrus_mapper_std_linear_zone *lz;
87         _index_t n, idx = 0;
88         u_int32_t conv;
89
90         _DIAGASSERT(ms != NULL);
91         _DIAGASSERT(dst != NULL);
92         /* ps may be unused */
93         rc = &ms->ms_rowcol;
94
95         for (i = rc->rc_src_rowcol_len * rc->rc_src_rowcol_bits,
96              lz = &rc->rc_src_rowcol[0]; i > 0; ++lz) {
97                 i -= rc->rc_src_rowcol_bits;
98                 n = (src >> i) & rc->rc_src_rowcol_mask;
99                 if (n < lz->begin || n > lz->end) {
100                         switch (rc->rc_oob_mode) {
101                         case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL:
102                                 *dst = rc->rc_dst_invalid;
103                                 return _MAPPER_CONVERT_NONIDENTICAL;
104                         case _CITRUS_MAPPER_STD_OOB_ILSEQ:
105                                 return _MAPPER_CONVERT_ILSEQ;
106                         default:
107                                 return _MAPPER_CONVERT_FATAL;
108                         }
109                 }
110                 idx = idx * lz->width + n - lz->begin;
111         }
112         switch (rc->rc_dst_unit_bits) {
113         case 8:
114                 conv = _region_peek8(&rc->rc_table, idx);
115                 break;
116         case 16:
117                 conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
118                 break;
119         case 32:
120                 conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
121                 break;
122         default:
123                 return _MAPPER_CONVERT_FATAL;
124         }
125
126         if (conv == rc->rc_dst_invalid) {
127                 *dst = rc->rc_dst_invalid;
128                 return _MAPPER_CONVERT_NONIDENTICAL;
129         }
130         if (conv == rc->rc_dst_ilseq)
131                 return _MAPPER_CONVERT_ILSEQ;
132
133         *dst = conv;
134
135         return _MAPPER_CONVERT_SUCCESS;
136 }
137
138 static __inline int
139 set_linear_zone(struct _citrus_mapper_std_linear_zone *lz,
140         u_int32_t begin, u_int32_t end)
141 {
142         _DIAGASSERT(lz != NULL);
143
144         if (begin > end)
145                 return EFTYPE;
146
147         lz->begin = begin;
148         lz->end = end;
149         lz->width= end - begin + 1;
150
151         return 0;
152 }
153
154 static __inline int
155 rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol *rc,
156         struct _region *r)
157 {
158         const struct _citrus_mapper_std_rowcol_info_compat_x *rcx;
159         struct _citrus_mapper_std_linear_zone *lz;
160         u_int32_t m, n;
161         int ret;
162
163         _DIAGASSERT(rc != NULL);
164         _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx));
165         rcx = _region_head(r);
166
167         rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
168         rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
169         m = be32toh(rcx->rcx_src_col_bits);
170         n = 1 << (m - 1);
171         n |= n - 1;
172         rc->rc_src_rowcol_bits = m;
173         rc->rc_src_rowcol_mask = n;
174
175         rc->rc_src_rowcol = malloc(2 *
176             sizeof(*rc->rc_src_rowcol));
177         if (rc->rc_src_rowcol == NULL)
178                 return ENOMEM;
179         lz = rc->rc_src_rowcol;
180         rc->rc_src_rowcol_len = 1;
181         m = be32toh(rcx->rcx_src_row_begin);
182         n = be32toh(rcx->rcx_src_row_end);
183         if (m + n > 0) {
184                 ret = set_linear_zone(lz, m, n);
185                 if (ret != 0)
186                         return ret;
187                 ++rc->rc_src_rowcol_len, ++lz;
188         }
189         m = be32toh(rcx->rcx_src_col_begin);
190         n = be32toh(rcx->rcx_src_col_end);
191
192         return set_linear_zone(lz, m, n);
193 }
194
195 static __inline int
196 rowcol_parse_variable(struct _citrus_mapper_std_rowcol *rc,
197         struct _region *r)
198 {
199         const struct _citrus_mapper_std_rowcol_info_x *rcx;
200         struct _citrus_mapper_std_linear_zone *lz;
201         u_int32_t m, n;
202         size_t i;
203         int ret;
204
205         _DIAGASSERT(rc != NULL);
206         _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx));
207         rcx = _region_head(r);
208
209         rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
210         rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
211
212         m = be32toh(rcx->rcx_src_rowcol_bits);
213         n = 1 << (m - 1);
214         n |= n - 1;
215         rc->rc_src_rowcol_bits = m;
216         rc->rc_src_rowcol_mask = n;
217
218         rc->rc_src_rowcol_len = be32toh(rcx->rcx_src_rowcol_len);
219         if (rc->rc_src_rowcol_len > _CITRUS_MAPPER_STD_ROWCOL_MAX)
220                 return EFTYPE;
221         rc->rc_src_rowcol = malloc(rc->rc_src_rowcol_len *
222             sizeof(*rc->rc_src_rowcol));
223         if (rc->rc_src_rowcol == NULL)
224                 return ENOMEM;
225         for (i = 0, lz = rc->rc_src_rowcol;
226              i < rc->rc_src_rowcol_len; ++i, ++lz) {
227                 m = be32toh(rcx->rcx_src_rowcol[i].begin),
228                 n = be32toh(rcx->rcx_src_rowcol[i].end);
229                 ret = set_linear_zone(lz, m, n);
230                 if (ret != 0) {
231                         free(rc->rc_src_rowcol);
232                         rc->rc_src_rowcol = NULL;
233                         return ret;
234                 }
235         }
236         return 0;
237 }
238
239 static void
240 rowcol_uninit(struct _citrus_mapper_std *ms)
241 {
242         struct _citrus_mapper_std_rowcol *rc;
243         _DIAGASSERT(ms != NULL);
244
245         rc = &ms->ms_rowcol;
246         free(rc->rc_src_rowcol);
247 }
248
249 static int
250 rowcol_init(struct _citrus_mapper_std *ms)
251 {
252         int ret;
253         struct _citrus_mapper_std_rowcol *rc;
254         const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix;
255         struct _region r;
256         u_int64_t table_size;
257         size_t i;
258         struct _citrus_mapper_std_linear_zone *lz;
259
260         _DIAGASSERT(ms != NULL);
261         ms->ms_convert = &rowcol_convert;
262         ms->ms_uninit = &rowcol_uninit;
263         rc = &ms->ms_rowcol;
264
265         /* get table region */
266         ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
267                               &rc->rc_table, NULL);
268         if (ret) {
269                 if (ret==ENOENT)
270                         ret = EFTYPE;
271                 return ret;
272         }
273
274         /* get table information */
275         ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
276         if (ret) {
277                 if (ret==ENOENT)
278                         ret = EFTYPE;
279                 return ret;
280         }
281         switch (_region_size(&r)) {
282         case _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE:
283                 ret = rowcol_parse_variable_compat(rc, &r);
284                 break;
285         case _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE:
286                 ret = rowcol_parse_variable(rc, &r);
287                 break;
288         default:
289                 return EFTYPE;
290         }
291         if (ret != 0)
292                 return ret;
293         /* sanity check */
294         switch (rc->rc_src_rowcol_bits) {
295         case 8: case 16: case 32:
296                 if (rc->rc_src_rowcol_len <= 32 / rc->rc_src_rowcol_bits)
297                         break;
298         /*FALLTHROUGH*/
299         default:
300                 return EFTYPE;
301         }
302
303         /* ilseq extension */
304         rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL;
305         rc->rc_dst_ilseq = rc->rc_dst_invalid;
306         ret = _db_lookup_by_s(ms->ms_db,
307                               _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ,
308                               &r, NULL);
309         if (ret && ret != ENOENT)
310                 return ret;
311         if (_region_size(&r) < sizeof(*eix))
312                 return EFTYPE;
313         if (ret == 0) {
314                 eix = _region_head(&r);
315                 rc->rc_oob_mode = be32toh(eix->eix_oob_mode);
316                 rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq);
317         }
318
319         /* calcurate expected table size */
320         i = rc->rc_src_rowcol_len;
321         lz = &rc->rc_src_rowcol[--i];
322         table_size = lz->width;
323         while (i > 0) {
324                 lz = &rc->rc_src_rowcol[--i];
325                 table_size *= lz->width;
326         }
327         table_size *= rc->rc_dst_unit_bits/8;
328
329         if (table_size > UINT32_MAX ||
330             _region_size(&rc->rc_table) < table_size)
331                 return EFTYPE;
332
333         return 0;
334 }
335
336 typedef int (*initfunc_t)(struct _citrus_mapper_std *);
337 static const struct {
338         const char                      *t_name;
339         initfunc_t                      t_init;
340 } types[] = {
341         { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init },
342 };
343 #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
344
345 static int
346 /*ARGSUSED*/
347 _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma,
348                                struct _citrus_mapper * __restrict cm,
349                                const char * __restrict curdir,
350                                const void * __restrict var, size_t lenvar,
351                                struct _citrus_mapper_traits * __restrict mt,
352                                size_t lenmt)
353 {
354         char path[PATH_MAX];
355         const char *type;
356         int ret, id;
357         struct _citrus_mapper_std *ms;
358
359         /* set traits */
360         if (lenmt<sizeof(*mt)) {
361                 ret = EINVAL;
362                 goto err0;
363         }
364         mt->mt_src_max = mt->mt_dst_max = 1;    /* 1:1 converter */
365         mt->mt_state_size = 0;                  /* stateless */
366
367         /* alloc mapper std structure */
368         ms = malloc(sizeof(*ms));
369         if (ms==NULL) {
370                 ret = errno;
371                 goto err0;
372         }
373
374         /* open mapper file */
375         snprintf(path, sizeof(path),
376                  "%s/%.*s", curdir, (int)lenvar, (const char *)var);
377         ret = _map_file(&ms->ms_file, path);
378         if (ret)
379                 goto err1;
380
381         ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
382                        &_db_hash_std, NULL);
383         if (ret)
384                 goto err2;
385
386         /* get mapper type */
387         ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
388                                  &type, NULL);
389         if (ret) {
390                 if (ret==ENOENT)
391                         ret = EFTYPE;
392                 goto err3;
393         }
394         for (id=0; id<NUM_OF_TYPES; id++)
395                 if (_bcs_strcasecmp(type, types[id].t_name) == 0)
396                         break;
397
398         if (id == NUM_OF_TYPES)
399                 goto err3;
400
401         /* init the per-type structure */
402         ret = (*types[id].t_init)(ms);
403         if (ret)
404                 goto err3;
405
406         cm->cm_closure = ms;
407
408         return 0;
409
410 err3:
411         _db_close(ms->ms_db);
412 err2:
413         _unmap_file(&ms->ms_file);
414 err1:
415         free(ms);
416 err0:
417         return ret;
418 }
419
420 static void
421 /*ARGSUSED*/
422 _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
423 {
424         struct _citrus_mapper_std *ms;
425
426         _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL);
427
428         ms = cm->cm_closure;
429         if (ms->ms_uninit)
430                 (*ms->ms_uninit)(ms);
431         _db_close(ms->ms_db);
432         _unmap_file(&ms->ms_file);
433         free(ms);
434 }
435
436 static void
437 /*ARGSUSED*/
438 _citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm,
439                                      void * __restrict ps)
440 {
441 }
442
443 static int
444 /*ARGSUSED*/
445 _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
446                                   _index_t * __restrict dst, _index_t src,
447                                   void * __restrict ps)
448 {
449         struct _citrus_mapper_std *ms;
450
451         _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL);
452
453         ms = cm->cm_closure;
454
455         _DIAGASSERT(ms->ms_convert != NULL);
456
457         return (*ms->ms_convert)(ms, dst, src, ps);
458 }