1ad30f8e7SGabor Kovesdan /* $FreeBSD$ */ 2ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_mapper_646.c,v 1.4 2003/07/14 11:37:49 tshiozak Exp $ */ 3ad30f8e7SGabor Kovesdan 4ad30f8e7SGabor Kovesdan /*- 5ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project, 6ad30f8e7SGabor Kovesdan * All rights reserved. 7ad30f8e7SGabor Kovesdan * 8ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 9ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions 10ad30f8e7SGabor Kovesdan * are met: 11ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 12ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 13ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 14ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 15ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution. 16ad30f8e7SGabor Kovesdan * 17ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27ad30f8e7SGabor Kovesdan * SUCH DAMAGE. 28ad30f8e7SGabor Kovesdan */ 29ad30f8e7SGabor Kovesdan 30ad30f8e7SGabor Kovesdan #include <sys/cdefs.h> 31ad30f8e7SGabor Kovesdan #include <sys/queue.h> 32ad30f8e7SGabor Kovesdan 33ad30f8e7SGabor Kovesdan #include <assert.h> 34ad30f8e7SGabor Kovesdan #include <errno.h> 35ad30f8e7SGabor Kovesdan #include <limits.h> 36ad30f8e7SGabor Kovesdan #include <stdio.h> 37ad30f8e7SGabor Kovesdan #include <stdlib.h> 38ad30f8e7SGabor Kovesdan #include <string.h> 39ad30f8e7SGabor Kovesdan 40ad30f8e7SGabor Kovesdan #include "citrus_namespace.h" 41ad30f8e7SGabor Kovesdan #include "citrus_types.h" 42ad30f8e7SGabor Kovesdan #include "citrus_bcs.h" 43ad30f8e7SGabor Kovesdan #include "citrus_module.h" 44ad30f8e7SGabor Kovesdan #include "citrus_region.h" 45ad30f8e7SGabor Kovesdan #include "citrus_memstream.h" 46ad30f8e7SGabor Kovesdan #include "citrus_mmap.h" 47ad30f8e7SGabor Kovesdan #include "citrus_hash.h" 48ad30f8e7SGabor Kovesdan #include "citrus_mapper.h" 49ad30f8e7SGabor Kovesdan #include "citrus_mapper_646.h" 50ad30f8e7SGabor Kovesdan 51ad30f8e7SGabor Kovesdan /* ---------------------------------------------------------------------- */ 52ad30f8e7SGabor Kovesdan 53ad30f8e7SGabor Kovesdan _CITRUS_MAPPER_DECLS(mapper_646); 54ad30f8e7SGabor Kovesdan _CITRUS_MAPPER_DEF_OPS(mapper_646); 55ad30f8e7SGabor Kovesdan 56ad30f8e7SGabor Kovesdan /* ---------------------------------------------------------------------- */ 57ad30f8e7SGabor Kovesdan 58ad30f8e7SGabor Kovesdan #define ILSEQ 0xFFFFFFFE 59ad30f8e7SGabor Kovesdan #define INVALID 0xFFFFFFFF 60ad30f8e7SGabor Kovesdan #define SPECIALS(x) \ 61ad30f8e7SGabor Kovesdan x(0x23) \ 62ad30f8e7SGabor Kovesdan x(0x24) \ 63ad30f8e7SGabor Kovesdan x(0x40) \ 64ad30f8e7SGabor Kovesdan x(0x5B) \ 65ad30f8e7SGabor Kovesdan x(0x5C) \ 66ad30f8e7SGabor Kovesdan x(0x5D) \ 67ad30f8e7SGabor Kovesdan x(0x5E) \ 68ad30f8e7SGabor Kovesdan x(0x60) \ 69ad30f8e7SGabor Kovesdan x(0x7B) \ 70ad30f8e7SGabor Kovesdan x(0x7C) \ 71ad30f8e7SGabor Kovesdan x(0x7D) \ 72ad30f8e7SGabor Kovesdan x(0x7E) 73ad30f8e7SGabor Kovesdan 74ad30f8e7SGabor Kovesdan #define INDEX(x) INDEX_##x, 75ad30f8e7SGabor Kovesdan 76ad30f8e7SGabor Kovesdan enum { 77ad30f8e7SGabor Kovesdan SPECIALS(INDEX) 78ad30f8e7SGabor Kovesdan NUM_OF_SPECIALS 79ad30f8e7SGabor Kovesdan }; 80ad30f8e7SGabor Kovesdan struct _citrus_mapper_646 { 81ad30f8e7SGabor Kovesdan _index_t m6_map[NUM_OF_SPECIALS]; 82ad30f8e7SGabor Kovesdan int m6_forward; 83ad30f8e7SGabor Kovesdan }; 84ad30f8e7SGabor Kovesdan 85ad30f8e7SGabor Kovesdan int 86ad30f8e7SGabor Kovesdan _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops) 87ad30f8e7SGabor Kovesdan { 88ad30f8e7SGabor Kovesdan 89ad30f8e7SGabor Kovesdan memcpy(ops, &_citrus_mapper_646_mapper_ops, 90ad30f8e7SGabor Kovesdan sizeof(_citrus_mapper_646_mapper_ops)); 91ad30f8e7SGabor Kovesdan 92ad30f8e7SGabor Kovesdan return (0); 93ad30f8e7SGabor Kovesdan } 94ad30f8e7SGabor Kovesdan 95ad30f8e7SGabor Kovesdan #define T_COMM '#' 96ad30f8e7SGabor Kovesdan static int 97ad30f8e7SGabor Kovesdan parse_file(struct _citrus_mapper_646 *m6, const char *path) 98ad30f8e7SGabor Kovesdan { 99ad30f8e7SGabor Kovesdan struct _memstream ms; 100ad30f8e7SGabor Kovesdan struct _region r; 101ad30f8e7SGabor Kovesdan const char *p; 102ad30f8e7SGabor Kovesdan char *pp; 103ad30f8e7SGabor Kovesdan size_t len; 104ad30f8e7SGabor Kovesdan char buf[PATH_MAX]; 105ad30f8e7SGabor Kovesdan int i, ret; 106ad30f8e7SGabor Kovesdan 107ad30f8e7SGabor Kovesdan ret = _map_file(&r, path); 108ad30f8e7SGabor Kovesdan if (ret) 109ad30f8e7SGabor Kovesdan return (ret); 110ad30f8e7SGabor Kovesdan _memstream_bind(&ms, &r); 111ad30f8e7SGabor Kovesdan for (i = 0; i < NUM_OF_SPECIALS; i++) { 112ad30f8e7SGabor Kovesdan retry: 113ad30f8e7SGabor Kovesdan p = _memstream_getln(&ms, &len); 114ad30f8e7SGabor Kovesdan if (p == NULL) { 115ad30f8e7SGabor Kovesdan ret = EINVAL; 116ad30f8e7SGabor Kovesdan break; 117ad30f8e7SGabor Kovesdan } 118ad30f8e7SGabor Kovesdan p = _bcs_skip_ws_len(p, &len); 119ad30f8e7SGabor Kovesdan if (*p == T_COMM || len==0) 120ad30f8e7SGabor Kovesdan goto retry; 121ad30f8e7SGabor Kovesdan if (!_bcs_isdigit(*p)) { 122ad30f8e7SGabor Kovesdan ret = EINVAL; 123ad30f8e7SGabor Kovesdan break; 124ad30f8e7SGabor Kovesdan } 125ad30f8e7SGabor Kovesdan snprintf(buf, sizeof(buf), "%.*s", (int)len, p); 126ad30f8e7SGabor Kovesdan pp = __DECONST(void *, p); 127ad30f8e7SGabor Kovesdan m6->m6_map[i] = strtoul(buf, (char **)&pp, 0); 128ad30f8e7SGabor Kovesdan p = _bcs_skip_ws(buf); 129ad30f8e7SGabor Kovesdan if (*p != T_COMM && !*p) { 130ad30f8e7SGabor Kovesdan ret = EINVAL; 131ad30f8e7SGabor Kovesdan break; 132ad30f8e7SGabor Kovesdan } 133ad30f8e7SGabor Kovesdan } 134ad30f8e7SGabor Kovesdan _unmap_file(&r); 135ad30f8e7SGabor Kovesdan 136ad30f8e7SGabor Kovesdan return (ret); 137ad30f8e7SGabor Kovesdan }; 138ad30f8e7SGabor Kovesdan 139ad30f8e7SGabor Kovesdan static int 140ad30f8e7SGabor Kovesdan parse_var(struct _citrus_mapper_646 *m6, struct _memstream *ms, 141ad30f8e7SGabor Kovesdan const char *dir) 142ad30f8e7SGabor Kovesdan { 143ad30f8e7SGabor Kovesdan struct _region r; 144ad30f8e7SGabor Kovesdan char path[PATH_MAX]; 145ad30f8e7SGabor Kovesdan 146ad30f8e7SGabor Kovesdan m6->m6_forward = 1; 147ad30f8e7SGabor Kovesdan _memstream_skip_ws(ms); 148ad30f8e7SGabor Kovesdan /* whether backward */ 149ad30f8e7SGabor Kovesdan if (_memstream_peek(ms) == '!') { 150ad30f8e7SGabor Kovesdan _memstream_getc(ms); 151ad30f8e7SGabor Kovesdan m6->m6_forward = 0; 152ad30f8e7SGabor Kovesdan } 153ad30f8e7SGabor Kovesdan /* get description file path */ 154ad30f8e7SGabor Kovesdan _memstream_getregion(ms, &r, _memstream_remainder(ms)); 155ad30f8e7SGabor Kovesdan snprintf(path, sizeof(path), "%s/%.*s", 156ad30f8e7SGabor Kovesdan dir, (int)_region_size(&r), (char *)_region_head(&r)); 157ad30f8e7SGabor Kovesdan /* remove trailing white spaces */ 158ad30f8e7SGabor Kovesdan path[_bcs_skip_nonws(path)-path] = '\0'; 159ad30f8e7SGabor Kovesdan return (parse_file(m6, path)); 160ad30f8e7SGabor Kovesdan } 161ad30f8e7SGabor Kovesdan 162ad30f8e7SGabor Kovesdan static int 163ad30f8e7SGabor Kovesdan /*ARGSUSED*/ 164ad30f8e7SGabor Kovesdan _citrus_mapper_646_mapper_init(struct _citrus_mapper_area *__restrict ma __unused, 165ad30f8e7SGabor Kovesdan struct _citrus_mapper * __restrict cm, const char * __restrict dir, 166ad30f8e7SGabor Kovesdan const void * __restrict var, size_t lenvar, 167ad30f8e7SGabor Kovesdan struct _citrus_mapper_traits * __restrict mt, size_t lenmt) 168ad30f8e7SGabor Kovesdan { 169ad30f8e7SGabor Kovesdan struct _citrus_mapper_646 *m6; 170ad30f8e7SGabor Kovesdan struct _memstream ms; 171ad30f8e7SGabor Kovesdan struct _region r; 172ad30f8e7SGabor Kovesdan int ret; 173ad30f8e7SGabor Kovesdan 174ad30f8e7SGabor Kovesdan if (lenmt < sizeof(*mt)) 175ad30f8e7SGabor Kovesdan return (EINVAL); 176ad30f8e7SGabor Kovesdan 177ad30f8e7SGabor Kovesdan m6 = malloc(sizeof(*m6)); 178ad30f8e7SGabor Kovesdan if (m6 == NULL) 179ad30f8e7SGabor Kovesdan return (errno); 180ad30f8e7SGabor Kovesdan 181ad30f8e7SGabor Kovesdan _region_init(&r, __DECONST(void *, var), lenvar); 182ad30f8e7SGabor Kovesdan _memstream_bind(&ms, &r); 183ad30f8e7SGabor Kovesdan ret = parse_var(m6, &ms, dir); 184ad30f8e7SGabor Kovesdan if (ret) { 185ad30f8e7SGabor Kovesdan free(m6); 186ad30f8e7SGabor Kovesdan return (ret); 187ad30f8e7SGabor Kovesdan } 188ad30f8e7SGabor Kovesdan 189ad30f8e7SGabor Kovesdan cm->cm_closure = m6; 190ad30f8e7SGabor Kovesdan mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ 191ad30f8e7SGabor Kovesdan mt->mt_state_size = 0; /* stateless */ 192ad30f8e7SGabor Kovesdan 193ad30f8e7SGabor Kovesdan return (0); 194ad30f8e7SGabor Kovesdan } 195ad30f8e7SGabor Kovesdan 196ad30f8e7SGabor Kovesdan static void 197ad30f8e7SGabor Kovesdan /*ARGSUSED*/ 198ad30f8e7SGabor Kovesdan _citrus_mapper_646_mapper_uninit(struct _citrus_mapper *cm) 199ad30f8e7SGabor Kovesdan { 200ad30f8e7SGabor Kovesdan 201ad30f8e7SGabor Kovesdan if (cm && cm->cm_closure) 202ad30f8e7SGabor Kovesdan free(cm->cm_closure); 203ad30f8e7SGabor Kovesdan } 204ad30f8e7SGabor Kovesdan 205ad30f8e7SGabor Kovesdan static int 206ad30f8e7SGabor Kovesdan /*ARGSUSED*/ 207ad30f8e7SGabor Kovesdan _citrus_mapper_646_mapper_convert(struct _citrus_mapper * __restrict cm, 208ad30f8e7SGabor Kovesdan _index_t * __restrict dst, _index_t src, void * __restrict ps __unused) 209ad30f8e7SGabor Kovesdan { 210ad30f8e7SGabor Kovesdan struct _citrus_mapper_646 *m6; 211ad30f8e7SGabor Kovesdan 212ad30f8e7SGabor Kovesdan m6 = cm->cm_closure; 213ad30f8e7SGabor Kovesdan if (m6->m6_forward) { 214ad30f8e7SGabor Kovesdan /* forward */ 215ad30f8e7SGabor Kovesdan if (src >= 0x80) 216ad30f8e7SGabor Kovesdan return (_MAPPER_CONVERT_ILSEQ); 217ad30f8e7SGabor Kovesdan #define FORWARD(x) \ 218ad30f8e7SGabor Kovesdan if (src == (x)) { \ 219ad30f8e7SGabor Kovesdan if (m6->m6_map[INDEX_##x]==INVALID) \ 220ad30f8e7SGabor Kovesdan return (_MAPPER_CONVERT_NONIDENTICAL); \ 221ad30f8e7SGabor Kovesdan *dst = m6->m6_map[INDEX_##x]; \ 222ad30f8e7SGabor Kovesdan return (0); \ 223ad30f8e7SGabor Kovesdan } else 224ad30f8e7SGabor Kovesdan SPECIALS(FORWARD); 225ad30f8e7SGabor Kovesdan *dst = src; 226ad30f8e7SGabor Kovesdan } else { 227ad30f8e7SGabor Kovesdan /* backward */ 228ad30f8e7SGabor Kovesdan #define BACKWARD(x) \ 229ad30f8e7SGabor Kovesdan if (m6->m6_map[INDEX_##x] != INVALID && src == m6->m6_map[INDEX_##x]) { \ 230ad30f8e7SGabor Kovesdan *dst = (x); \ 231ad30f8e7SGabor Kovesdan return (0); \ 232ad30f8e7SGabor Kovesdan } else if (src == (x)) \ 233ad30f8e7SGabor Kovesdan return (_MAPPER_CONVERT_ILSEQ); \ 234ad30f8e7SGabor Kovesdan else 235ad30f8e7SGabor Kovesdan SPECIALS(BACKWARD); 236ad30f8e7SGabor Kovesdan if (src >= 0x80) 237ad30f8e7SGabor Kovesdan return (_MAPPER_CONVERT_NONIDENTICAL); 238ad30f8e7SGabor Kovesdan *dst = src; 239ad30f8e7SGabor Kovesdan } 240ad30f8e7SGabor Kovesdan 241ad30f8e7SGabor Kovesdan return (_MAPPER_CONVERT_SUCCESS); 242ad30f8e7SGabor Kovesdan } 243ad30f8e7SGabor Kovesdan 244ad30f8e7SGabor Kovesdan static void 245ad30f8e7SGabor Kovesdan /*ARGSUSED*/ 246ad30f8e7SGabor Kovesdan _citrus_mapper_646_mapper_init_state(void) 247ad30f8e7SGabor Kovesdan { 248ad30f8e7SGabor Kovesdan 249ad30f8e7SGabor Kovesdan } 250