keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libc / citrus / modules / citrus_mapper_646.c
1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.2 2008/04/10 10:21:01 hasso 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 <sys/types.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 <string.h>
38
39 #include "citrus_namespace.h"
40 #include "citrus_types.h"
41 #include "citrus_bcs.h"
42 #include "citrus_module.h"
43 #include "citrus_region.h"
44 #include "citrus_memstream.h"
45 #include "citrus_mmap.h"
46 #include "citrus_hash.h"
47 #include "citrus_mapper.h"
48 #include "citrus_mapper_646.h"
49
50 /* ---------------------------------------------------------------------- */
51
52 _CITRUS_MAPPER_DECLS(mapper_646);
53 _CITRUS_MAPPER_DEF_OPS(mapper_646);
54
55 /* ---------------------------------------------------------------------- */
56
57 #define ILSEQ   0xFFFFFFFE
58 #define INVALID 0xFFFFFFFF
59 #define SPECIALS(x)                             \
60         x(0x23)                                 \
61         x(0x24)                                 \
62         x(0x40)                                 \
63         x(0x5B)                                 \
64         x(0x5C)                                 \
65         x(0x5D)                                 \
66         x(0x5E)                                 \
67         x(0x60)                                 \
68         x(0x7B)                                 \
69         x(0x7C)                                 \
70         x(0x7D)                                 \
71         x(0x7E)
72
73 #define INDEX(x) INDEX_##x,
74
75 enum {
76         SPECIALS(INDEX)
77         NUM_OF_SPECIALS
78 };
79 struct _citrus_mapper_646 {
80         int             m6_forward;
81         _index_t        m6_map[NUM_OF_SPECIALS];
82 };
83
84 int
85 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops,
86                                  size_t lenops, uint32_t expected_version)
87 {
88         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
89                 return EINVAL;
90
91         memcpy(ops, &_citrus_mapper_646_mapper_ops,
92                sizeof(_citrus_mapper_646_mapper_ops));
93
94         return 0;
95 }
96
97 #define T_COMM '#'
98 static int
99 parse_file(struct _citrus_mapper_646 *m6, const char *path)
100 {
101         int ret, i;
102         struct _region r;
103         struct _memstream ms;
104         const char *p;
105         size_t len;
106         char buf[PATH_MAX];
107
108         ret = _map_file(&r, path);
109         if (ret)
110                 return ret;
111         _memstream_bind(&ms, &r);
112         for (i=0; i<NUM_OF_SPECIALS; i++) {
113 retry:
114                 p = _memstream_getln(&ms, &len);
115                 if (p==NULL) {
116                         ret = EINVAL;
117                         break;
118                 }
119                 p = _bcs_skip_ws_len(p, &len);
120                 if (*p == T_COMM || len==0)
121                         goto retry;
122                 if (!_bcs_isdigit(*p)) {
123                         ret = EINVAL;
124                         break;
125                 }
126                 snprintf(buf, sizeof(buf), "%.*s", (int)len, p);
127                 m6->m6_map[i] = strtoul(buf, (char **)&p, 0);
128                 p = _bcs_skip_ws(buf);
129                 if (*p != T_COMM && !*p) {
130                         ret = EINVAL;
131                         break;
132                 }
133         }
134         _unmap_file(&r);
135
136         return ret;
137 };
138
139 static int
140 parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms,
141           const char *dir)
142 {
143         struct _region r;
144         char path[PATH_MAX];
145
146         m6->m6_forward = 1;
147         _memstream_skip_ws(ms);
148         /* whether backward */
149         if (_memstream_peek(ms) == '!') {
150                 _memstream_getc(ms);
151                 m6->m6_forward = 0;
152         }
153         /* get description file path */
154         _memstream_getregion(ms, &r, _memstream_remainder(ms));
155         snprintf(path, sizeof(path), "%s/%.*s",
156                  dir, (int)_region_size(&r), (char *)_region_head(&r));
157         /* remove trailing white spaces */
158         path[_bcs_skip_nonws(path)-path] = '\0';
159         return parse_file(m6, path);
160 }
161
162 static int
163 /*ARGSUSED*/
164 _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma,
165                                struct _citrus_mapper * __restrict cm,
166                                const char * __restrict dir,
167                                const void * __restrict var, size_t lenvar,
168                                struct _citrus_mapper_traits * __restrict mt,
169                                size_t lenmt)
170 {
171         struct _citrus_mapper_646 *m6;
172         struct _memstream ms;
173         struct _region r;
174         int ret;
175
176         _DIAGASSERT(cm && dir && mt);
177
178         if (lenmt<sizeof(*mt))
179                 return EINVAL;
180
181         m6 = malloc(sizeof(*m6));
182         if (m6 == NULL)
183                 return errno;
184
185         _region_init(&r, (void *)var, lenvar);
186         _memstream_bind(&ms, &r);
187         ret = parse_var(m6, &ms, dir);
188         if (ret) {
189                 free(m6);
190                 return ret;
191         }
192
193         cm->cm_closure = m6;
194         mt->mt_src_max = mt->mt_dst_max = 1;    /* 1:1 converter */
195         mt->mt_state_size = 0;                  /* stateless */
196
197         return 0;
198 }
199
200 static void
201 /*ARGSUSED*/
202 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm)
203 {
204         if (cm && cm->cm_closure) {
205                 free(cm->cm_closure);
206         }
207 }
208
209 static int
210 /*ARGSUSED*/
211 _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm,
212                                   _index_t * __restrict dst, _index_t src,
213                                   void * __restrict ps)
214 {
215         struct _citrus_mapper_646 *m6;
216
217         _DIAGASSERT(cm && cm->cm_closure);
218
219         m6 = cm->cm_closure;
220         if (m6->m6_forward) {
221                 /* forward */
222                 if (src>=0x80)
223                         return _MAPPER_CONVERT_ILSEQ;
224 #define FORWARD(x)                                      \
225 if (src==(x)) {                                         \
226         if (m6->m6_map[INDEX_##x]==INVALID)             \
227                 return _MAPPER_CONVERT_NONIDENTICAL;    \
228         *dst = m6->m6_map[INDEX_##x];                   \
229         return 0;                                       \
230 } else
231                 SPECIALS(FORWARD);
232                 *dst = src;
233         } else {
234                 /* backward */
235 #define BACKWARD(x)                                                     \
236 if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) {     \
237         *dst = (x);                                                     \
238         return 0;                                                       \
239 } else if (src==(x))                                                    \
240         return _MAPPER_CONVERT_ILSEQ;                                   \
241 else
242                 SPECIALS(BACKWARD);
243                 if (src>=0x80)
244                         return _MAPPER_CONVERT_NONIDENTICAL;
245                 *dst = src;
246         }
247
248         return _MAPPER_CONVERT_SUCCESS;
249 }
250
251 static void
252 /*ARGSUSED*/
253 _citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm,
254                                      void * __restrict ps)
255 {
256 }