1ad30f8e7SGabor Kovesdan /* $FreeBSD$ */ 2ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_none.c,v 1.18 2008/06/14 16:01:07 tnozaki Exp $ */ 3ad30f8e7SGabor Kovesdan 4ad30f8e7SGabor Kovesdan /*- 5ad30f8e7SGabor Kovesdan * Copyright (c) 2002 Citrus Project, 6ad30f8e7SGabor Kovesdan * Copyright (c) 2010 Gabor Kovesdan <gabor@FreeBSD.org>, 7ad30f8e7SGabor Kovesdan * All rights reserved. 8ad30f8e7SGabor Kovesdan * 9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions 11ad30f8e7SGabor Kovesdan * are met: 12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution. 17ad30f8e7SGabor Kovesdan * 18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28ad30f8e7SGabor Kovesdan * SUCH DAMAGE. 29ad30f8e7SGabor Kovesdan */ 30ad30f8e7SGabor Kovesdan 31ad30f8e7SGabor Kovesdan #include <sys/cdefs.h> 32ad30f8e7SGabor Kovesdan #include <sys/types.h> 33ad30f8e7SGabor Kovesdan 34ad30f8e7SGabor Kovesdan #include <assert.h> 35ad30f8e7SGabor Kovesdan #include <errno.h> 36ad30f8e7SGabor Kovesdan #include <iconv.h> 37ad30f8e7SGabor Kovesdan #include <stddef.h> 38ad30f8e7SGabor Kovesdan #include <stdio.h> 39ad30f8e7SGabor Kovesdan #include <stdlib.h> 40ad30f8e7SGabor Kovesdan #include <string.h> 41ad30f8e7SGabor Kovesdan #include <wchar.h> 42ad30f8e7SGabor Kovesdan 43ad30f8e7SGabor Kovesdan #include "citrus_namespace.h" 44ad30f8e7SGabor Kovesdan #include "citrus_types.h" 45ad30f8e7SGabor Kovesdan #include "citrus_module.h" 46ad30f8e7SGabor Kovesdan #include "citrus_none.h" 47ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h" 48ad30f8e7SGabor Kovesdan 49ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(NONE); 50ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(NONE); 51ad30f8e7SGabor Kovesdan struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = { 52ad30f8e7SGabor Kovesdan 0, /* et_state_size */ 53ad30f8e7SGabor Kovesdan 1, /* mb_cur_max */ 54ad30f8e7SGabor Kovesdan }; 55ad30f8e7SGabor Kovesdan 56ad30f8e7SGabor Kovesdan static int 57ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce, 58ad30f8e7SGabor Kovesdan const void *var __unused, size_t lenvar __unused, 59ad30f8e7SGabor Kovesdan struct _citrus_stdenc_traits * __restrict et) 60ad30f8e7SGabor Kovesdan { 61ad30f8e7SGabor Kovesdan 62ad30f8e7SGabor Kovesdan et->et_state_size = 0; 63ad30f8e7SGabor Kovesdan et->et_mb_cur_max = 1; 64ad30f8e7SGabor Kovesdan 65ad30f8e7SGabor Kovesdan ce->ce_closure = NULL; 66ad30f8e7SGabor Kovesdan 67ad30f8e7SGabor Kovesdan return (0); 68ad30f8e7SGabor Kovesdan } 69ad30f8e7SGabor Kovesdan 70ad30f8e7SGabor Kovesdan static void 71ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce __unused) 72ad30f8e7SGabor Kovesdan { 73ad30f8e7SGabor Kovesdan 74ad30f8e7SGabor Kovesdan } 75ad30f8e7SGabor Kovesdan 76ad30f8e7SGabor Kovesdan static int 77ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce __unused, 78ad30f8e7SGabor Kovesdan void * __restrict ps __unused) 79ad30f8e7SGabor Kovesdan { 80ad30f8e7SGabor Kovesdan 81ad30f8e7SGabor Kovesdan return (0); 82ad30f8e7SGabor Kovesdan } 83ad30f8e7SGabor Kovesdan 84ad30f8e7SGabor Kovesdan static int 85ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce __unused, 861243a98eSTijl Coosemans _csid_t *csid, _index_t *idx, char **s, size_t n, 87ad30f8e7SGabor Kovesdan void *ps __unused, size_t *nresult, struct iconv_hooks *hooks) 88ad30f8e7SGabor Kovesdan { 89ad30f8e7SGabor Kovesdan 90ad30f8e7SGabor Kovesdan if (n < 1) { 91ad30f8e7SGabor Kovesdan *nresult = (size_t)-2; 92ad30f8e7SGabor Kovesdan return (0); 93ad30f8e7SGabor Kovesdan } 94ad30f8e7SGabor Kovesdan 95ad30f8e7SGabor Kovesdan *csid = 0; 96ad30f8e7SGabor Kovesdan *idx = (_index_t)(unsigned char)*(*s)++; 97ad30f8e7SGabor Kovesdan *nresult = *idx == 0 ? 0 : 1; 98ad30f8e7SGabor Kovesdan 99ad30f8e7SGabor Kovesdan if ((hooks != NULL) && (hooks->uc_hook != NULL)) 100ad30f8e7SGabor Kovesdan hooks->uc_hook((unsigned int)*idx, hooks->data); 101ad30f8e7SGabor Kovesdan 102ad30f8e7SGabor Kovesdan return (0); 103ad30f8e7SGabor Kovesdan } 104ad30f8e7SGabor Kovesdan 105ad30f8e7SGabor Kovesdan static int 106ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce __unused, 107ad30f8e7SGabor Kovesdan char *s, size_t n, _csid_t csid, _index_t idx, void *ps __unused, 108ad30f8e7SGabor Kovesdan size_t *nresult, struct iconv_hooks *hooks __unused) 109ad30f8e7SGabor Kovesdan { 110ad30f8e7SGabor Kovesdan 111ad30f8e7SGabor Kovesdan if (csid == _CITRUS_CSID_INVALID) { 112ad30f8e7SGabor Kovesdan *nresult = 0; 113ad30f8e7SGabor Kovesdan return (0); 114ad30f8e7SGabor Kovesdan } 115ad30f8e7SGabor Kovesdan if (csid != 0) 116ad30f8e7SGabor Kovesdan return (EILSEQ); 117ad30f8e7SGabor Kovesdan 118ad30f8e7SGabor Kovesdan if ((idx & 0x000000FF) == idx) { 119ad30f8e7SGabor Kovesdan if (n < 1) { 120ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 121ad30f8e7SGabor Kovesdan return (E2BIG); 122ad30f8e7SGabor Kovesdan } 123ad30f8e7SGabor Kovesdan *s = (char)idx; 124ad30f8e7SGabor Kovesdan *nresult = 1; 125ad30f8e7SGabor Kovesdan } else if ((idx & 0x0000FFFF) == idx) { 126ad30f8e7SGabor Kovesdan if (n < 2) { 127ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 128ad30f8e7SGabor Kovesdan return (E2BIG); 129ad30f8e7SGabor Kovesdan } 130ad30f8e7SGabor Kovesdan s[0] = (char)idx; 131ad30f8e7SGabor Kovesdan /* XXX: might be endian dependent */ 132ad30f8e7SGabor Kovesdan s[1] = (char)(idx >> 8); 133ad30f8e7SGabor Kovesdan *nresult = 2; 134ad30f8e7SGabor Kovesdan } else if ((idx & 0x00FFFFFF) == idx) { 135ad30f8e7SGabor Kovesdan if (n < 3) { 136ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 137ad30f8e7SGabor Kovesdan return (E2BIG); 138ad30f8e7SGabor Kovesdan } 139ad30f8e7SGabor Kovesdan s[0] = (char)idx; 140ad30f8e7SGabor Kovesdan /* XXX: might be endian dependent */ 141ad30f8e7SGabor Kovesdan s[1] = (char)(idx >> 8); 142ad30f8e7SGabor Kovesdan s[2] = (char)(idx >> 16); 143ad30f8e7SGabor Kovesdan *nresult = 3; 144ad30f8e7SGabor Kovesdan } else { 145ad30f8e7SGabor Kovesdan if (n < 3) { 146ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 147ad30f8e7SGabor Kovesdan return (E2BIG); 148ad30f8e7SGabor Kovesdan } 149ad30f8e7SGabor Kovesdan s[0] = (char)idx; 150ad30f8e7SGabor Kovesdan /* XXX: might be endian dependent */ 151ad30f8e7SGabor Kovesdan s[1] = (char)(idx >> 8); 152ad30f8e7SGabor Kovesdan s[2] = (char)(idx >> 16); 153ad30f8e7SGabor Kovesdan s[3] = (char)(idx >> 24); 154ad30f8e7SGabor Kovesdan *nresult = 4; 155ad30f8e7SGabor Kovesdan } 156ad30f8e7SGabor Kovesdan 157ad30f8e7SGabor Kovesdan return (0); 158ad30f8e7SGabor Kovesdan } 159ad30f8e7SGabor Kovesdan 160ad30f8e7SGabor Kovesdan static int 161ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce __unused, 1621243a98eSTijl Coosemans _wc_t * __restrict pwc, char ** __restrict s, size_t n, 163ad30f8e7SGabor Kovesdan void * __restrict pspriv __unused, size_t * __restrict nresult, 164ad30f8e7SGabor Kovesdan struct iconv_hooks *hooks) 165ad30f8e7SGabor Kovesdan { 166ad30f8e7SGabor Kovesdan 167*2b61d291SPedro F. Giffuni if (*s == NULL) { 168ad30f8e7SGabor Kovesdan *nresult = 0; 169ad30f8e7SGabor Kovesdan return (0); 170ad30f8e7SGabor Kovesdan } 171ad30f8e7SGabor Kovesdan if (n == 0) { 172ad30f8e7SGabor Kovesdan *nresult = (size_t)-2; 173ad30f8e7SGabor Kovesdan return (0); 174ad30f8e7SGabor Kovesdan } 175ad30f8e7SGabor Kovesdan 176ad30f8e7SGabor Kovesdan if (pwc != NULL) 177ad30f8e7SGabor Kovesdan *pwc = (_wc_t)(unsigned char) **s; 178ad30f8e7SGabor Kovesdan 179*2b61d291SPedro F. Giffuni *nresult = **s == '\0' ? 0 : 1; 180ad30f8e7SGabor Kovesdan 181ad30f8e7SGabor Kovesdan if ((hooks != NULL) && (hooks->wc_hook != NULL)) 182ad30f8e7SGabor Kovesdan hooks->wc_hook(*pwc, hooks->data); 183ad30f8e7SGabor Kovesdan 184ad30f8e7SGabor Kovesdan return (0); 185ad30f8e7SGabor Kovesdan } 186ad30f8e7SGabor Kovesdan 187ad30f8e7SGabor Kovesdan static int 188ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce __unused, 189ad30f8e7SGabor Kovesdan char * __restrict s, size_t n, _wc_t wc, 190ad30f8e7SGabor Kovesdan void * __restrict pspriv __unused, size_t * __restrict nresult, 191ad30f8e7SGabor Kovesdan struct iconv_hooks *hooks __unused) 192ad30f8e7SGabor Kovesdan { 193ad30f8e7SGabor Kovesdan 194ad30f8e7SGabor Kovesdan if ((wc & ~0xFFU) != 0) { 195ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 196ad30f8e7SGabor Kovesdan return (EILSEQ); 197ad30f8e7SGabor Kovesdan } 198ad30f8e7SGabor Kovesdan if (n == 0) { 199ad30f8e7SGabor Kovesdan *nresult = (size_t)-1; 2000e6d7a0eSGabor Kovesdan return (E2BIG); 201ad30f8e7SGabor Kovesdan } 202ad30f8e7SGabor Kovesdan 203ad30f8e7SGabor Kovesdan *nresult = 1; 204ad30f8e7SGabor Kovesdan if (s != NULL && n > 0) 205ad30f8e7SGabor Kovesdan *s = (char)wc; 206ad30f8e7SGabor Kovesdan 207ad30f8e7SGabor Kovesdan return (0); 208ad30f8e7SGabor Kovesdan } 209ad30f8e7SGabor Kovesdan 210ad30f8e7SGabor Kovesdan static int 211ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce __unused, 212ad30f8e7SGabor Kovesdan char * __restrict s __unused, size_t n __unused, 213ad30f8e7SGabor Kovesdan void * __restrict pspriv __unused, size_t * __restrict nresult) 214ad30f8e7SGabor Kovesdan { 215ad30f8e7SGabor Kovesdan 216ad30f8e7SGabor Kovesdan *nresult = 0; 217ad30f8e7SGabor Kovesdan 218ad30f8e7SGabor Kovesdan return (0); 219ad30f8e7SGabor Kovesdan } 220ad30f8e7SGabor Kovesdan 221ad30f8e7SGabor Kovesdan static int 222ad30f8e7SGabor Kovesdan _citrus_NONE_stdenc_get_state_desc(struct _stdenc * __restrict ce __unused, 223ad30f8e7SGabor Kovesdan void * __restrict ps __unused, int id, 224ad30f8e7SGabor Kovesdan struct _stdenc_state_desc * __restrict d) 225ad30f8e7SGabor Kovesdan { 226ad30f8e7SGabor Kovesdan int ret = 0; 227ad30f8e7SGabor Kovesdan 228ad30f8e7SGabor Kovesdan switch (id) { 229ad30f8e7SGabor Kovesdan case _STDENC_SDID_GENERIC: 230ad30f8e7SGabor Kovesdan d->u.generic.state = _STDENC_SDGEN_INITIAL; 231ad30f8e7SGabor Kovesdan break; 232ad30f8e7SGabor Kovesdan default: 233ad30f8e7SGabor Kovesdan ret = EOPNOTSUPP; 234ad30f8e7SGabor Kovesdan } 235ad30f8e7SGabor Kovesdan 236ad30f8e7SGabor Kovesdan return (ret); 237ad30f8e7SGabor Kovesdan } 238