1 /* $NetBSD: citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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 */
40 #include <sys/queue.h>
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"
53 /* ---------------------------------------------------------------------- */
55 _CITRUS_MAPPER_DECLS(mapper_646
);
56 _CITRUS_MAPPER_DEF_OPS(mapper_646
);
58 /* ---------------------------------------------------------------------- */
60 #define ILSEQ 0xFFFFFFFE
61 #define INVALID 0xFFFFFFFF
76 #define INDEX(x) INDEX_##x,
82 struct _citrus_mapper_646
{
84 _index_t m6_map
[NUM_OF_SPECIALS
];
88 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops
*ops
,
89 size_t lenops
, uint32_t expected_version
)
91 if (expected_version
<_CITRUS_MAPPER_ABI_VERSION
|| lenops
<sizeof(*ops
))
94 memcpy(ops
, &_citrus_mapper_646_mapper_ops
,
95 sizeof(_citrus_mapper_646_mapper_ops
));
102 parse_file(struct _citrus_mapper_646
*m6
, const char *path
)
106 struct _memstream ms
;
111 ret
= _map_file(&r
, path
);
114 _memstream_bind(&ms
, &r
);
115 for (i
=0; i
<NUM_OF_SPECIALS
; i
++) {
117 p
= _memstream_getln(&ms
, &len
);
122 p
= _bcs_skip_ws_len(p
, &len
);
123 if (*p
== T_COMM
|| len
==0)
125 if (!_bcs_isdigit(*p
)) {
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
) {
143 parse_var(struct _citrus_mapper_646
*m6
, struct _memstream
*ms
,
150 _memstream_skip_ws(ms
);
151 /* whether backward */
152 if (_memstream_peek(ms
) == '!') {
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
);
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
,
174 struct _citrus_mapper_646
*m6
;
175 struct _memstream ms
;
179 _DIAGASSERT(cm
&& dir
&& mt
);
181 if (lenmt
<sizeof(*mt
))
184 m6
= malloc(sizeof(*m6
));
188 _region_init(&r
, (void *)var
, lenvar
);
189 _memstream_bind(&ms
, &r
);
190 ret
= parse_var(m6
, &ms
, dir
);
197 mt
->mt_src_max
= mt
->mt_dst_max
= 1; /* 1:1 converter */
198 mt
->mt_state_size
= 0; /* stateless */
205 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper
*cm
)
207 if (cm
&& cm
->cm_closure
) {
208 free(cm
->cm_closure
);
214 _citrus_mapper_646_mapper_convert(struct _citrus_mapper
* __restrict cm
,
215 _index_t
* __restrict dst
, _index_t src
,
216 void * __restrict ps
)
218 struct _citrus_mapper_646
*m6
;
220 _DIAGASSERT(cm
&& cm
->cm_closure
);
223 if (m6
->m6_forward
) {
226 return _MAPPER_CONVERT_ILSEQ
;
229 if (m6->m6_map[INDEX_##x]==INVALID) \
230 return _MAPPER_CONVERT_NONIDENTICAL; \
231 *dst = m6->m6_map[INDEX_##x]; \
238 #define BACKWARD(x) \
239 if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \
242 } else if (src==(x)) \
243 return _MAPPER_CONVERT_ILSEQ; \
247 return _MAPPER_CONVERT_NONIDENTICAL
;
251 return _MAPPER_CONVERT_SUCCESS
;
256 _citrus_mapper_646_mapper_init_state(struct _citrus_mapper
* __restrict cm
,
257 void * __restrict ps
)