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