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