1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_646.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/queue.h>
39 #include "citrus_namespace.h"
40 #include "citrus_types.h"
41 #include "citrus_bcs.h"
42 #include "citrus_module.h"
43 #include "citrus_region.h"
44 #include "citrus_memstream.h"
45 #include "citrus_mmap.h"
46 #include "citrus_hash.h"
47 #include "citrus_mapper.h"
48 #include "citrus_mapper_646.h"
50 /* ---------------------------------------------------------------------- */
52 _CITRUS_MAPPER_DECLS(mapper_646
);
53 _CITRUS_MAPPER_DEF_OPS(mapper_646
);
55 /* ---------------------------------------------------------------------- */
57 #define ILSEQ 0xFFFFFFFE
58 #define INVALID 0xFFFFFFFF
73 #define INDEX(x) INDEX_##x,
79 struct _citrus_mapper_646
{
81 _index_t m6_map
[NUM_OF_SPECIALS
];
85 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops
*ops
,
86 size_t lenops
, uint32_t expected_version
)
88 if (expected_version
<_CITRUS_MAPPER_ABI_VERSION
|| lenops
<sizeof(*ops
))
91 memcpy(ops
, &_citrus_mapper_646_mapper_ops
,
92 sizeof(_citrus_mapper_646_mapper_ops
));
99 parse_file(struct _citrus_mapper_646
*m6
, const char *path
)
103 struct _memstream ms
;
108 ret
= _map_file(&r
, path
);
111 _memstream_bind(&ms
, &r
);
112 for (i
=0; i
<NUM_OF_SPECIALS
; i
++) {
114 p
= _memstream_getln(&ms
, &len
);
119 p
= _bcs_skip_ws_len(p
, &len
);
120 if (*p
== T_COMM
|| len
==0)
122 if (!_bcs_isdigit(*p
)) {
126 snprintf(buf
, sizeof(buf
), "%.*s", (int)len
, p
);
127 m6
->m6_map
[i
] = strtoul(buf
, (char **)&p
, 0);
128 p
= _bcs_skip_ws(buf
);
129 if (*p
!= T_COMM
&& !*p
) {
140 parse_var(struct _citrus_mapper_646
*m6
, struct _memstream
*ms
,
147 _memstream_skip_ws(ms
);
148 /* whether backward */
149 if (_memstream_peek(ms
) == '!') {
153 /* get description file path */
154 _memstream_getregion(ms
, &r
, _memstream_remainder(ms
));
155 snprintf(path
, sizeof(path
), "%s/%.*s",
156 dir
, (int)_region_size(&r
), (char *)_region_head(&r
));
157 /* remove trailing white spaces */
158 path
[_bcs_skip_nonws(path
)-path
] = '\0';
159 return parse_file(m6
, path
);
164 _citrus_mapper_646_mapper_init(struct _citrus_mapper_area
*__restrict ma
,
165 struct _citrus_mapper
* __restrict cm
,
166 const char * __restrict dir
,
167 const void * __restrict var
, size_t lenvar
,
168 struct _citrus_mapper_traits
* __restrict mt
,
171 struct _citrus_mapper_646
*m6
;
172 struct _memstream ms
;
176 _DIAGASSERT(cm
&& dir
&& mt
);
178 if (lenmt
<sizeof(*mt
))
181 m6
= malloc(sizeof(*m6
));
185 _region_init(&r
, (void *)var
, lenvar
);
186 _memstream_bind(&ms
, &r
);
187 ret
= parse_var(m6
, &ms
, dir
);
194 mt
->mt_src_max
= mt
->mt_dst_max
= 1; /* 1:1 converter */
195 mt
->mt_state_size
= 0; /* stateless */
202 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper
*cm
)
204 if (cm
&& cm
->cm_closure
) {
205 free(cm
->cm_closure
);
211 _citrus_mapper_646_mapper_convert(struct _citrus_mapper
* __restrict cm
,
212 _index_t
* __restrict dst
, _index_t src
,
213 void * __restrict ps
)
215 struct _citrus_mapper_646
*m6
;
217 _DIAGASSERT(cm
&& cm
->cm_closure
);
220 if (m6
->m6_forward
) {
223 return _MAPPER_CONVERT_ILSEQ
;
226 if (m6->m6_map[INDEX_##x]==INVALID) \
227 return _MAPPER_CONVERT_NONIDENTICAL; \
228 *dst = m6->m6_map[INDEX_##x]; \
235 #define BACKWARD(x) \
236 if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \
239 } else if (src==(x)) \
240 return _MAPPER_CONVERT_ILSEQ; \
244 return _MAPPER_CONVERT_NONIDENTICAL
;
248 return _MAPPER_CONVERT_SUCCESS
;
253 _citrus_mapper_646_mapper_init_state(struct _citrus_mapper
* __restrict cm
,
254 void * __restrict ps
)