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