1 /* $NetBSD: citrus_ctype.c,v 1.7 2013/05/28 16:57:56 joerg Exp $ */ 2 3 /*- 4 * Copyright (c)1999, 2000, 2001, 2002 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_ctype.c,v 1.7 2013/05/28 16:57:56 joerg Exp $"); 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include <sys/types.h> 35 #include <assert.h> 36 #include <errno.h> 37 #include <limits.h> 38 #include <string.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <unistd.h> 42 #include <stddef.h> 43 #include <wchar.h> 44 #include "citrus_module.h" 45 #include "citrus_ctype.h" 46 #include "citrus_ctype_fallback.h" 47 #include "citrus_none.h" 48 #include _CITRUS_DEFAULT_CTYPE_HEADER 49 50 _citrus_ctype_rec_t _citrus_ctype_default = { 51 &_CITRUS_DEFAULT_CTYPE_OPS, /* cc_ops */ 52 NULL, /* cc_closure */ 53 NULL /* cc_module */ 54 }; 55 56 #ifdef _I18N_DYNAMIC 57 58 static int _initctypemodule(_citrus_ctype_t, char const *, _citrus_module_t, 59 void *, size_t, size_t); 60 61 static int 62 _initctypemodule(_citrus_ctype_t cc, char const *modname, 63 _citrus_module_t handle, void *variable, size_t lenvar, 64 size_t szpriv) 65 { 66 int ret; 67 _citrus_ctype_getops_t getops; 68 69 _DIAGASSERT(cc != NULL); 70 71 cc->cc_module = handle; 72 73 getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module, 74 modname, 75 "ctype"); 76 if (getops == NULL) 77 return (EINVAL); 78 79 cc->cc_ops = (_citrus_ctype_ops_rec_t *)malloc(sizeof(*cc->cc_ops)); 80 if (cc->cc_ops == NULL) 81 return (ENOMEM); 82 83 ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops), 84 _CITRUS_CTYPE_ABI_VERSION); 85 if (ret) 86 goto bad; 87 88 /* If return ABI version is not expected, fixup it here*/ 89 switch (cc->cc_ops->co_abi_version) { 90 case 0x00000001: 91 cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback; 92 cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback; 93 /* FALLTHROUGH */ 94 case 0x00000002: 95 cc->cc_ops->co_mbsnrtowcs = &_citrus_ctype_mbsnrtowcs_fallback; 96 cc->cc_ops->co_wcsnrtombs = &_citrus_ctype_wcsnrtombs_fallback; 97 /* FALLTHROUGH */ 98 default: 99 break; 100 } 101 102 /* validation check */ 103 if (cc->cc_ops->co_init == NULL || 104 cc->cc_ops->co_uninit == NULL || 105 cc->cc_ops->co_get_mb_cur_max == NULL || 106 cc->cc_ops->co_mblen == NULL || 107 cc->cc_ops->co_mbrlen == NULL || 108 cc->cc_ops->co_mbrtowc == NULL || 109 cc->cc_ops->co_mbsinit == NULL || 110 cc->cc_ops->co_mbsrtowcs == NULL || 111 cc->cc_ops->co_mbsnrtowcs == NULL || 112 cc->cc_ops->co_mbstowcs == NULL || 113 cc->cc_ops->co_mbtowc == NULL || 114 cc->cc_ops->co_wcrtomb == NULL || 115 cc->cc_ops->co_wcsrtombs == NULL || 116 cc->cc_ops->co_wcsnrtombs == NULL || 117 cc->cc_ops->co_wcstombs == NULL || 118 cc->cc_ops->co_wctomb == NULL || 119 cc->cc_ops->co_btowc == NULL || 120 cc->cc_ops->co_wctob == NULL) { 121 ret = EINVAL; 122 goto bad; 123 } 124 125 /* init and get closure */ 126 ret = (*cc->cc_ops->co_init)( 127 &cc->cc_closure, variable, lenvar, szpriv); 128 if (ret) 129 goto bad; 130 131 return (0); 132 133 bad: 134 if (cc->cc_ops) 135 free(cc->cc_ops); 136 cc->cc_ops = NULL; 137 138 return (ret); 139 } 140 141 int 142 _citrus_ctype_open(_citrus_ctype_t *rcc, 143 char const *encname, void *variable, size_t lenvar, 144 size_t szpriv) 145 { 146 int ret; 147 _citrus_module_t handle; 148 _citrus_ctype_t cc; 149 150 _DIAGASSERT(encname != NULL); 151 _DIAGASSERT(!lenvar || variable!=NULL); 152 _DIAGASSERT(rcc != NULL); 153 154 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { 155 *rcc = &_citrus_ctype_default; 156 return (0); 157 } 158 ret = _citrus_load_module(&handle, encname); 159 if (ret) 160 return (ret); 161 162 cc = calloc(1, sizeof(*cc)); 163 if (!cc) { 164 _citrus_unload_module(handle); 165 return (errno); 166 } 167 168 ret = _initctypemodule(cc, encname, handle, variable, lenvar, szpriv); 169 if (ret) { 170 _citrus_unload_module(cc->cc_module); 171 free(cc); 172 return (ret); 173 } 174 175 *rcc = cc; 176 177 return (0); 178 } 179 180 void 181 _citrus_ctype_close(_citrus_ctype_t cc) 182 { 183 184 _DIAGASSERT(cc != NULL); 185 186 if (cc == &_citrus_ctype_default) 187 return; 188 (*cc->cc_ops->co_uninit)(cc->cc_closure); 189 free(cc->cc_ops); 190 _citrus_unload_module(cc->cc_module); 191 free(cc); 192 } 193 194 #else 195 /* !_I18N_DYNAMIC */ 196 197 int 198 /*ARGSUSED*/ 199 _citrus_ctype_open(_citrus_ctype_t *rcc, 200 char const *encname, void *variable, size_t lenvar, 201 size_t szpriv) 202 { 203 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) { 204 *rcc = &_citrus_ctype_default; 205 return (0); 206 } 207 return (EINVAL); 208 } 209 210 void 211 /*ARGSUSED*/ 212 _citrus_ctype_close(_citrus_ctype_t cc) 213 { 214 } 215 216 #endif 217