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