1 /* $FreeBSD: head/lib/libiconv_modules/mapper_646/citrus_mapper_646.c 219019 2011-02-25 00:04:39Z gabor $ */ 2 /* $NetBSD: citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */ 3 4 /*- 5 * Copyright (c)2003 Citrus Project, 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #include <sys/queue.h> 32 33 #include <assert.h> 34 #include <errno.h> 35 #include <limits.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include "citrus_namespace.h" 41 #include "citrus_types.h" 42 #include "citrus_bcs.h" 43 #include "citrus_module.h" 44 #include "citrus_region.h" 45 #include "citrus_memstream.h" 46 #include "citrus_mmap.h" 47 #include "citrus_hash.h" 48 #include "citrus_mapper.h" 49 #include "citrus_mapper_646.h" 50 51 /* ---------------------------------------------------------------------- */ 52 53 _CITRUS_MAPPER_DECLS(mapper_646); 54 _CITRUS_MAPPER_DEF_OPS(mapper_646); 55 56 /* ---------------------------------------------------------------------- */ 57 58 #define ILSEQ 0xFFFFFFFE 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 _index_t m6_map[NUM_OF_SPECIALS]; 82 int m6_forward; 83 }; 84 85 int 86 _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops) 87 { 88 89 memcpy(ops, &_citrus_mapper_646_mapper_ops, 90 sizeof(_citrus_mapper_646_mapper_ops)); 91 92 return (0); 93 } 94 95 #define T_COMM '#' 96 static int 97 parse_file(struct _citrus_mapper_646 *m6, const char *path) 98 { 99 struct _memstream ms; 100 struct _region r; 101 const char *p; 102 char *pp; 103 size_t len; 104 char buf[PATH_MAX]; 105 int i, ret; 106 107 ret = _map_file(&r, path); 108 if (ret) 109 return (ret); 110 _memstream_bind(&ms, &r); 111 for (i = 0; i < NUM_OF_SPECIALS; i++) { 112 retry: 113 p = _memstream_getln(&ms, &len); 114 if (p == NULL) { 115 ret = EINVAL; 116 break; 117 } 118 p = _bcs_skip_ws_len(p, &len); 119 if (*p == T_COMM || len==0) 120 goto retry; 121 if (!_bcs_isdigit(*p)) { 122 ret = EINVAL; 123 break; 124 } 125 snprintf(buf, sizeof(buf), "%.*s", (int)len, p); 126 pp = __DECONST(void *, p); 127 m6->m6_map[i] = strtoul(buf, (char **)&pp, 0); 128 p = _bcs_skip_ws(buf); 129 if (*p != T_COMM && !*p) { 130 ret = EINVAL; 131 break; 132 } 133 } 134 _unmap_file(&r); 135 136 return (ret); 137 }; 138 139 static int 140 parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms, 141 const char *dir) 142 { 143 struct _region r; 144 char path[PATH_MAX]; 145 146 m6->m6_forward = 1; 147 _memstream_skip_ws(ms); 148 /* whether backward */ 149 if (_memstream_peek(ms) == '!') { 150 _memstream_getc(ms); 151 m6->m6_forward = 0; 152 } 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)); 160 } 161 162 static int 163 /*ARGSUSED*/ 164 _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma __unused, 165 struct _citrus_mapper * __restrict cm, const char * __restrict dir, 166 const void * __restrict var, size_t lenvar, 167 struct _citrus_mapper_traits * __restrict mt, size_t lenmt) 168 { 169 struct _citrus_mapper_646 *m6; 170 struct _memstream ms; 171 struct _region r; 172 int ret; 173 174 if (lenmt < sizeof(*mt)) 175 return (EINVAL); 176 177 m6 = malloc(sizeof(*m6)); 178 if (m6 == NULL) 179 return (errno); 180 181 _region_init(&r, __DECONST(void *, var), lenvar); 182 _memstream_bind(&ms, &r); 183 ret = parse_var(m6, &ms, dir); 184 if (ret) { 185 free(m6); 186 return (ret); 187 } 188 189 cm->cm_closure = m6; 190 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ 191 mt->mt_state_size = 0; /* stateless */ 192 193 return (0); 194 } 195 196 static void 197 /*ARGSUSED*/ 198 _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm) 199 { 200 201 if (cm && cm->cm_closure) 202 free(cm->cm_closure); 203 } 204 205 static int 206 /*ARGSUSED*/ 207 _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm, 208 _index_t * __restrict dst, _index_t src, void * __restrict ps __unused) 209 { 210 struct _citrus_mapper_646 *m6; 211 212 m6 = cm->cm_closure; 213 if (m6->m6_forward) { 214 /* forward */ 215 if (src >= 0x80) 216 return (_MAPPER_CONVERT_ILSEQ); 217 #define FORWARD(x) \ 218 if (src == (x)) { \ 219 if (m6->m6_map[INDEX_##x]==INVALID) \ 220 return (_MAPPER_CONVERT_NONIDENTICAL); \ 221 *dst = m6->m6_map[INDEX_##x]; \ 222 return (0); \ 223 } else 224 SPECIALS(FORWARD); 225 *dst = src; 226 } else { 227 /* backward */ 228 #define BACKWARD(x) \ 229 if (m6->m6_map[INDEX_##x] != INVALID && src == m6->m6_map[INDEX_##x]) { \ 230 *dst = (x); \ 231 return (0); \ 232 } else if (src == (x)) \ 233 return (_MAPPER_CONVERT_ILSEQ); \ 234 else 235 SPECIALS(BACKWARD); 236 if (src >= 0x80) 237 return (_MAPPER_CONVERT_NONIDENTICAL); 238 *dst = src; 239 } 240 241 return (_MAPPER_CONVERT_SUCCESS); 242 } 243 244 static void 245 /*ARGSUSED*/ 246 _citrus_mapper_646_mapper_init_state(void) 247 { 248 249 } 250