citrus_mapper_646.c revision 1.2 1 1.2 tshiozak /* $NetBSD: citrus_mapper_646.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $ */
2 1.1 tshiozak
3 1.1 tshiozak /*-
4 1.1 tshiozak * Copyright (c)2003 Citrus Project,
5 1.1 tshiozak * All rights reserved.
6 1.1 tshiozak *
7 1.1 tshiozak * Redistribution and use in source and binary forms, with or without
8 1.1 tshiozak * modification, are permitted provided that the following conditions
9 1.1 tshiozak * are met:
10 1.1 tshiozak * 1. Redistributions of source code must retain the above copyright
11 1.1 tshiozak * notice, this list of conditions and the following disclaimer.
12 1.1 tshiozak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tshiozak * notice, this list of conditions and the following disclaimer in the
14 1.1 tshiozak * documentation and/or other materials provided with the distribution.
15 1.1 tshiozak *
16 1.1 tshiozak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 tshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 tshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 tshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 tshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 tshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 tshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 tshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 tshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 tshiozak * SUCH DAMAGE.
27 1.1 tshiozak */
28 1.1 tshiozak
29 1.1 tshiozak #include <sys/cdefs.h>
30 1.1 tshiozak #if defined(LIBC_SCCS) && !defined(lint)
31 1.2 tshiozak __RCSID("$NetBSD: citrus_mapper_646.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $");
32 1.1 tshiozak #endif /* LIBC_SCCS and not lint */
33 1.1 tshiozak
34 1.1 tshiozak #include <assert.h>
35 1.1 tshiozak #include <errno.h>
36 1.1 tshiozak #include <stdio.h>
37 1.1 tshiozak #include <stdlib.h>
38 1.1 tshiozak #include <string.h>
39 1.1 tshiozak #include <limits.h>
40 1.2 tshiozak #include <sys/queue.h>
41 1.1 tshiozak
42 1.1 tshiozak #include "citrus_namespace.h"
43 1.1 tshiozak #include "citrus_types.h"
44 1.1 tshiozak #include "citrus_bcs.h"
45 1.1 tshiozak #include "citrus_module.h"
46 1.1 tshiozak #include "citrus_region.h"
47 1.1 tshiozak #include "citrus_memstream.h"
48 1.1 tshiozak #include "citrus_mmap.h"
49 1.1 tshiozak #include "citrus_hash.h"
50 1.1 tshiozak #include "citrus_mapper.h"
51 1.1 tshiozak #include "citrus_mapper_646.h"
52 1.1 tshiozak
53 1.1 tshiozak /* ---------------------------------------------------------------------- */
54 1.1 tshiozak
55 1.1 tshiozak _CITRUS_MAPPER_DECLS(mapper_646);
56 1.1 tshiozak _CITRUS_MAPPER_DEF_OPS(mapper_646);
57 1.1 tshiozak
58 1.1 tshiozak /* ---------------------------------------------------------------------- */
59 1.1 tshiozak
60 1.1 tshiozak #define INVALID 0xFFFFFFFF
61 1.1 tshiozak #define SPECIALS(x) \
62 1.1 tshiozak x(0x23) \
63 1.1 tshiozak x(0x24) \
64 1.1 tshiozak x(0x40) \
65 1.1 tshiozak x(0x5B) \
66 1.1 tshiozak x(0x5C) \
67 1.1 tshiozak x(0x5D) \
68 1.1 tshiozak x(0x5E) \
69 1.1 tshiozak x(0x60) \
70 1.1 tshiozak x(0x7B) \
71 1.1 tshiozak x(0x7C) \
72 1.1 tshiozak x(0x7D) \
73 1.1 tshiozak x(0x7E)
74 1.1 tshiozak
75 1.1 tshiozak #define INDEX(x) INDEX_##x,
76 1.1 tshiozak
77 1.1 tshiozak enum {
78 1.1 tshiozak SPECIALS(INDEX)
79 1.1 tshiozak NUM_OF_SPECIALS
80 1.1 tshiozak };
81 1.1 tshiozak struct _citrus_mapper_646 {
82 1.1 tshiozak int m6_forward;
83 1.1 tshiozak _index_t m6_map[NUM_OF_SPECIALS];
84 1.1 tshiozak };
85 1.1 tshiozak
86 1.1 tshiozak int
87 1.1 tshiozak _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops,
88 1.1 tshiozak size_t lenops, uint32_t expected_version)
89 1.1 tshiozak {
90 1.1 tshiozak if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
91 1.1 tshiozak return EINVAL;
92 1.1 tshiozak
93 1.1 tshiozak memcpy(ops, &_citrus_mapper_646_mapper_ops,
94 1.1 tshiozak sizeof(_citrus_mapper_646_mapper_ops));
95 1.1 tshiozak
96 1.1 tshiozak return 0;
97 1.1 tshiozak }
98 1.1 tshiozak
99 1.1 tshiozak #define T_COMM '#'
100 1.1 tshiozak static int
101 1.1 tshiozak parse_file(struct _citrus_mapper_646 *m6, const char *path)
102 1.1 tshiozak {
103 1.1 tshiozak int ret, i;
104 1.1 tshiozak struct _region r;
105 1.1 tshiozak struct _memstream ms;
106 1.1 tshiozak const char *p;
107 1.1 tshiozak size_t len;
108 1.1 tshiozak char buf[PATH_MAX];
109 1.1 tshiozak
110 1.1 tshiozak ret = _map_file(&r, path);
111 1.1 tshiozak if (ret)
112 1.1 tshiozak return ret;
113 1.1 tshiozak _memstream_bind(&ms, &r);
114 1.1 tshiozak for (i=0; i<NUM_OF_SPECIALS; i++) {
115 1.1 tshiozak retry:
116 1.1 tshiozak p = _memstream_getln(&ms, &len);
117 1.1 tshiozak if (p==NULL) {
118 1.1 tshiozak ret = EINVAL;
119 1.1 tshiozak break;
120 1.1 tshiozak }
121 1.1 tshiozak p = _bcs_skip_ws_len(p, &len);
122 1.1 tshiozak if (*p == T_COMM || len==0)
123 1.1 tshiozak goto retry;
124 1.1 tshiozak if (!_bcs_isdigit(*p)) {
125 1.1 tshiozak ret = EINVAL;
126 1.1 tshiozak break;
127 1.1 tshiozak }
128 1.1 tshiozak snprintf(buf, sizeof(buf), "%.*s", (int)len, p);
129 1.1 tshiozak m6->m6_map[i] = strtoul(buf, (char **)&p, 0);
130 1.1 tshiozak p = _bcs_skip_ws(buf);
131 1.1 tshiozak if (*p != T_COMM && !*p) {
132 1.1 tshiozak ret = EINVAL;
133 1.1 tshiozak break;
134 1.1 tshiozak }
135 1.1 tshiozak }
136 1.1 tshiozak _unmap_file(&r);
137 1.1 tshiozak
138 1.1 tshiozak return ret;
139 1.1 tshiozak };
140 1.1 tshiozak
141 1.1 tshiozak static int
142 1.1 tshiozak parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms,
143 1.1 tshiozak const char *dir)
144 1.1 tshiozak {
145 1.1 tshiozak struct _region r;
146 1.1 tshiozak char path[PATH_MAX];
147 1.1 tshiozak
148 1.1 tshiozak m6->m6_forward = 1;
149 1.1 tshiozak _memstream_skip_ws(ms);
150 1.1 tshiozak /* whether backward */
151 1.1 tshiozak if (_memstream_peek(ms) == '!') {
152 1.1 tshiozak _memstream_getc(ms);
153 1.1 tshiozak m6->m6_forward = 0;
154 1.1 tshiozak }
155 1.1 tshiozak /* get description file path */
156 1.1 tshiozak _memstream_getregion(ms, &r, _memstream_remainder(ms));
157 1.1 tshiozak snprintf(path, sizeof(path), "%s/%.*s",
158 1.1 tshiozak dir, (int)_region_size(&r), (char *)_region_head(&r));
159 1.1 tshiozak /* remove trailing white spaces */
160 1.1 tshiozak path[_bcs_skip_nonws(path)-path] = '\0';
161 1.1 tshiozak return parse_file(m6, path);
162 1.1 tshiozak }
163 1.1 tshiozak
164 1.1 tshiozak static int
165 1.1 tshiozak /*ARGSUSED*/
166 1.1 tshiozak _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma,
167 1.1 tshiozak struct _citrus_mapper * __restrict cm,
168 1.1 tshiozak const char * __restrict dir,
169 1.1 tshiozak const void * __restrict var, size_t lenvar,
170 1.1 tshiozak struct _citrus_mapper_traits * __restrict mt,
171 1.1 tshiozak size_t lenmt)
172 1.1 tshiozak {
173 1.1 tshiozak struct _citrus_mapper_646 *m6;
174 1.1 tshiozak struct _memstream ms;
175 1.1 tshiozak struct _region r;
176 1.1 tshiozak int ret;
177 1.1 tshiozak
178 1.1 tshiozak _DIAGASSERT(cm && dir && mt);
179 1.1 tshiozak
180 1.1 tshiozak if (lenmt<sizeof(*mt))
181 1.1 tshiozak return EINVAL;
182 1.1 tshiozak
183 1.1 tshiozak m6 = malloc(sizeof(*m6));
184 1.1 tshiozak if (m6 == NULL)
185 1.1 tshiozak return errno;
186 1.1 tshiozak
187 1.1 tshiozak _region_init(&r, (void *)var, lenvar);
188 1.1 tshiozak _memstream_bind(&ms, &r);
189 1.1 tshiozak ret = parse_var(m6, &ms, dir);
190 1.1 tshiozak if (ret) {
191 1.1 tshiozak free(m6);
192 1.1 tshiozak return ret;
193 1.1 tshiozak }
194 1.1 tshiozak
195 1.1 tshiozak cm->cm_closure = m6;
196 1.1 tshiozak mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
197 1.1 tshiozak mt->mt_state_size = 0; /* stateless */
198 1.1 tshiozak
199 1.1 tshiozak return 0;
200 1.1 tshiozak }
201 1.1 tshiozak
202 1.1 tshiozak static void
203 1.1 tshiozak /*ARGSUSED*/
204 1.1 tshiozak _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm)
205 1.1 tshiozak {
206 1.1 tshiozak if (cm && cm->cm_closure) {
207 1.1 tshiozak free(cm->cm_closure);
208 1.1 tshiozak }
209 1.1 tshiozak }
210 1.1 tshiozak
211 1.1 tshiozak static int
212 1.1 tshiozak /*ARGSUSED*/
213 1.1 tshiozak _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm,
214 1.1 tshiozak _index_t * __restrict dst, _index_t src,
215 1.1 tshiozak void * __restrict ps)
216 1.1 tshiozak {
217 1.1 tshiozak struct _citrus_mapper_646 *m6;
218 1.1 tshiozak
219 1.1 tshiozak _DIAGASSERT(cm && cm->cm_closure);
220 1.1 tshiozak
221 1.1 tshiozak m6 = cm->cm_closure;
222 1.1 tshiozak if (m6->m6_forward) {
223 1.1 tshiozak /* forward */
224 1.1 tshiozak if (src>=0x80)
225 1.1 tshiozak return _MAPPER_CONVERT_INVAL;
226 1.1 tshiozak #define FORWARD(x) \
227 1.1 tshiozak if (src==(x)) { \
228 1.1 tshiozak if (m6->m6_map[INDEX_##x]==INVALID) \
229 1.1 tshiozak return _MAPPER_CONVERT_INVAL; \
230 1.1 tshiozak *dst = m6->m6_map[INDEX_##x]; \
231 1.1 tshiozak return 0; \
232 1.1 tshiozak } else
233 1.1 tshiozak SPECIALS(FORWARD);
234 1.1 tshiozak *dst = src;
235 1.1 tshiozak } else {
236 1.1 tshiozak /* backward */
237 1.1 tshiozak #define BACKWARD(x) \
238 1.1 tshiozak if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \
239 1.1 tshiozak *dst = (x); \
240 1.1 tshiozak return 0; \
241 1.1 tshiozak } else if (src==(x)) \
242 1.1 tshiozak return _MAPPER_CONVERT_INVAL; \
243 1.1 tshiozak else
244 1.1 tshiozak SPECIALS(BACKWARD);
245 1.1 tshiozak if (src>=0x80)
246 1.1 tshiozak return _MAPPER_CONVERT_INVAL;
247 1.1 tshiozak *dst = src;
248 1.1 tshiozak }
249 1.1 tshiozak
250 1.1 tshiozak return _MAPPER_CONVERT_SUCCESS;
251 1.1 tshiozak }
252 1.1 tshiozak
253 1.1 tshiozak static void
254 1.1 tshiozak /*ARGSUSED*/
255 1.1 tshiozak _citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm,
256 1.1 tshiozak void * __restrict ps)
257 1.1 tshiozak {
258 1.1 tshiozak }
259