1 /* $NetBSD: citrus_mapper_646.c,v 1.1 2003/06/25 09:51:45 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.1 2003/06/25 09:51:45 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 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 INVALID 0xFFFFFFFF 60 #define SPECIALS(x) \ 61 x(0x23) \ 62 x(0x24) \ 63 x(0x40) \ 64 x(0x5B) \ 65 x(0x5C) \ 66 x(0x5D) \ 67 x(0x5E) \ 68 x(0x60) \ 69 x(0x7B) \ 70 x(0x7C) \ 71 x(0x7D) \ 72 x(0x7E) 73 74 #define INDEX(x) INDEX_##x, 75 76 enum { 77 SPECIALS(INDEX) 78 NUM_OF_SPECIALS 79 }; 80 struct _citrus_mapper_646 { 81 int m6_forward; 82 _index_t m6_map[NUM_OF_SPECIALS]; 83 }; 84 85 int 86 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops, 87 size_t lenops, uint32_t expected_version) 88 { 89 if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops)) 90 return EINVAL; 91 92 memcpy(ops, &_citrus_mapper_646_mapper_ops, 93 sizeof(_citrus_mapper_646_mapper_ops)); 94 95 return 0; 96 } 97 98 #define T_COMM '#' 99 static int 100 parse_file(struct _citrus_mapper_646 *m6, const char *path) 101 { 102 int ret, i; 103 struct _region r; 104 struct _memstream ms; 105 const char *p; 106 size_t len; 107 char buf[PATH_MAX]; 108 109 ret = _map_file(&r, path); 110 if (ret) 111 return ret; 112 _memstream_bind(&ms, &r); 113 for (i=0; i<NUM_OF_SPECIALS; i++) { 114 retry: 115 p = _memstream_getln(&ms, &len); 116 if (p==NULL) { 117 ret = EINVAL; 118 break; 119 } 120 p = _bcs_skip_ws_len(p, &len); 121 if (*p == T_COMM || len==0) 122 goto retry; 123 if (!_bcs_isdigit(*p)) { 124 ret = EINVAL; 125 break; 126 } 127 snprintf(buf, sizeof(buf), "%.*s", (int)len, p); 128 m6->m6_map[i] = strtoul(buf, (char **)&p, 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 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*/ 165 _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma, 166 struct _citrus_mapper * __restrict cm, 167 const char * __restrict dir, 168 const void * __restrict var, size_t lenvar, 169 struct _citrus_mapper_traits * __restrict mt, 170 size_t lenmt) 171 { 172 struct _citrus_mapper_646 *m6; 173 struct _memstream ms; 174 struct _region r; 175 int ret; 176 177 _DIAGASSERT(cm && dir && mt); 178 179 if (lenmt<sizeof(*mt)) 180 return EINVAL; 181 182 m6 = malloc(sizeof(*m6)); 183 if (m6 == NULL) 184 return errno; 185 186 _region_init(&r, (void *)var, lenvar); 187 _memstream_bind(&ms, &r); 188 ret = parse_var(m6, &ms, dir); 189 if (ret) { 190 free(m6); 191 return ret; 192 } 193 194 cm->cm_closure = m6; 195 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ 196 mt->mt_state_size = 0; /* stateless */ 197 198 return 0; 199 } 200 201 static void 202 /*ARGSUSED*/ 203 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm) 204 { 205 if (cm && cm->cm_closure) { 206 free(cm->cm_closure); 207 } 208 } 209 210 static int 211 /*ARGSUSED*/ 212 _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm, 213 _index_t * __restrict dst, _index_t src, 214 void * __restrict ps) 215 { 216 struct _citrus_mapper_646 *m6; 217 218 _DIAGASSERT(cm && cm->cm_closure); 219 220 m6 = cm->cm_closure; 221 if (m6->m6_forward) { 222 /* forward */ 223 if (src>=0x80) 224 return _MAPPER_CONVERT_INVAL; 225 #define FORWARD(x) \ 226 if (src==(x)) { \ 227 if (m6->m6_map[INDEX_##x]==INVALID) \ 228 return _MAPPER_CONVERT_INVAL; \ 229 *dst = m6->m6_map[INDEX_##x]; \ 230 return 0; \ 231 } else 232 SPECIALS(FORWARD); 233 *dst = src; 234 } else { 235 /* backward */ 236 #define BACKWARD(x) \ 237 if (m6->m6_map[INDEX_##x]!=INVALID && src==m6->m6_map[INDEX_##x]) { \ 238 *dst = (x); \ 239 return 0; \ 240 } else if (src==(x)) \ 241 return _MAPPER_CONVERT_INVAL; \ 242 else 243 SPECIALS(BACKWARD); 244 if (src>=0x80) 245 return _MAPPER_CONVERT_INVAL; 246 *dst = src; 247 } 248 249 return _MAPPER_CONVERT_SUCCESS; 250 } 251 252 static void 253 /*ARGSUSED*/ 254 _citrus_mapper_646_mapper_init_state(struct _citrus_mapper * __restrict cm, 255 void * __restrict ps) 256 { 257 } 258