Home | History | Annotate | Line # | Download | only in modules
citrus_mapper_646.c revision 1.2
      1 /*	$NetBSD: citrus_mapper_646.c,v 1.2 2003/06/27 17:53:31 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.2 2003/06/27 17:53:31 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 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 	int		m6_forward;
     83 	_index_t	m6_map[NUM_OF_SPECIALS];
     84 };
     85 
     86 int
     87 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops,
     88 				 size_t lenops, uint32_t expected_version)
     89 {
     90 	if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
     91 		return EINVAL;
     92 
     93 	memcpy(ops, &_citrus_mapper_646_mapper_ops,
     94 	       sizeof(_citrus_mapper_646_mapper_ops));
     95 
     96 	return 0;
     97 }
     98 
     99 #define T_COMM '#'
    100 static int
    101 parse_file(struct _citrus_mapper_646 *m6, const char *path)
    102 {
    103 	int ret, i;
    104 	struct _region r;
    105 	struct _memstream ms;
    106 	const char *p;
    107 	size_t len;
    108 	char buf[PATH_MAX];
    109 
    110 	ret = _map_file(&r, path);
    111 	if (ret)
    112 		return ret;
    113 	_memstream_bind(&ms, &r);
    114 	for (i=0; i<NUM_OF_SPECIALS; i++) {
    115 retry:
    116 		p = _memstream_getln(&ms, &len);
    117 		if (p==NULL) {
    118 			ret = EINVAL;
    119 			break;
    120 		}
    121 		p = _bcs_skip_ws_len(p, &len);
    122 		if (*p == T_COMM || len==0)
    123 			goto retry;
    124 		if (!_bcs_isdigit(*p)) {
    125 			ret = EINVAL;
    126 			break;
    127 		}
    128 		snprintf(buf, sizeof(buf), "%.*s", (int)len, p);
    129 		m6->m6_map[i] = strtoul(buf, (char **)&p, 0);
    130 		p = _bcs_skip_ws(buf);
    131 		if (*p != T_COMM && !*p) {
    132 			ret = EINVAL;
    133 			break;
    134 		}
    135 	}
    136 	_unmap_file(&r);
    137 
    138 	return ret;
    139 };
    140 
    141 static int
    142 parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms,
    143 	  const char *dir)
    144 {
    145 	struct _region r;
    146 	char path[PATH_MAX];
    147 
    148 	m6->m6_forward = 1;
    149 	_memstream_skip_ws(ms);
    150 	/* whether backward */
    151 	if (_memstream_peek(ms) == '!') {
    152 		_memstream_getc(ms);
    153 		m6->m6_forward = 0;
    154 	}
    155 	/* get description file path */
    156 	_memstream_getregion(ms, &r, _memstream_remainder(ms));
    157 	snprintf(path, sizeof(path), "%s/%.*s",
    158 		 dir, (int)_region_size(&r), (char *)_region_head(&r));
    159 	/* remove trailing white spaces */
    160 	path[_bcs_skip_nonws(path)-path] = '\0';
    161 	return parse_file(m6, path);
    162 }
    163 
    164 static int
    165 /*ARGSUSED*/
    166 _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma,
    167 			       struct _citrus_mapper * __restrict cm,
    168 			       const char * __restrict dir,
    169 			       const void * __restrict var, size_t lenvar,
    170 			       struct _citrus_mapper_traits * __restrict mt,
    171 			       size_t lenmt)
    172 {
    173 	struct _citrus_mapper_646 *m6;
    174 	struct _memstream ms;
    175 	struct _region r;
    176 	int ret;
    177 
    178 	_DIAGASSERT(cm && dir && mt);
    179 
    180 	if (lenmt<sizeof(*mt))
    181 		return EINVAL;
    182 
    183 	m6 = malloc(sizeof(*m6));
    184 	if (m6 == NULL)
    185 		return errno;
    186 
    187 	_region_init(&r, (void *)var, lenvar);
    188 	_memstream_bind(&ms, &r);
    189 	ret = parse_var(m6, &ms, dir);
    190 	if (ret) {
    191 		free(m6);
    192 		return ret;
    193 	}
    194 
    195 	cm->cm_closure = m6;
    196 	mt->mt_src_max = mt->mt_dst_max = 1;	/* 1:1 converter */
    197 	mt->mt_state_size = 0;			/* stateless */
    198 
    199 	return 0;
    200 }
    201 
    202 static void
    203 /*ARGSUSED*/
    204 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm)
    205 {
    206 	if (cm && cm->cm_closure) {
    207 		free(cm->cm_closure);
    208 	}
    209 }
    210 
    211 static int
    212 /*ARGSUSED*/
    213 _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm,
    214 				  _index_t * __restrict dst, _index_t src,
    215 				  void * __restrict ps)
    216 {
    217 	struct _citrus_mapper_646 *m6;
    218 
    219 	_DIAGASSERT(cm && cm->cm_closure);
    220 
    221 	m6 = cm->cm_closure;
    222 	if (m6->m6_forward) {
    223 		/* forward */
    224 		if (src>=0x80)
    225 			return _MAPPER_CONVERT_INVAL;
    226 #define FORWARD(x)				\
    227 if (src==(x)) {					\
    228 	if (m6->m6_map[INDEX_##x]==INVALID)	\
    229 		return _MAPPER_CONVERT_INVAL;	\
    230 	*dst = m6->m6_map[INDEX_##x];		\
    231 	return 0;				\
    232 } else
    233 		SPECIALS(FORWARD);
    234 		*dst = src;
    235 	} else {
    236 		/* backward */
    237 #define BACKWARD(x)							\
    238 if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) {	\
    239 	*dst = (x);							\
    240 	return 0;							\
    241 } else if (src==(x))							\
    242 	return _MAPPER_CONVERT_INVAL;					\
    243 else
    244 		SPECIALS(BACKWARD);
    245 		if (src>=0x80)
    246 			return _MAPPER_CONVERT_INVAL;
    247 		*dst = src;
    248 	}
    249 
    250 	return _MAPPER_CONVERT_SUCCESS;
    251 }
    252 
    253 static void
    254 /*ARGSUSED*/
    255 _citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm,
    256 				     void * __restrict ps)
    257 {
    258 }
    259