1 1.4 junyoung /* $NetBSD: citrus_stdenc_template.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */ 2 1.1 tshiozak 3 1.1 tshiozak /*- 4 1.1 tshiozak * Copyright (c)2003 Citrus Project, 5 1.1 tshiozak * All rights reserved. 6 1.1 tshiozak * 7 1.1 tshiozak * Redistribution and use in source and binary forms, with or without 8 1.1 tshiozak * modification, are permitted provided that the following conditions 9 1.1 tshiozak * are met: 10 1.1 tshiozak * 1. Redistributions of source code must retain the above copyright 11 1.1 tshiozak * notice, this list of conditions and the following disclaimer. 12 1.1 tshiozak * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 tshiozak * notice, this list of conditions and the following disclaimer in the 14 1.1 tshiozak * documentation and/or other materials provided with the distribution. 15 1.1 tshiozak * 16 1.1 tshiozak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 tshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 tshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 tshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 tshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 tshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 tshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 tshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 tshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 tshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 tshiozak * SUCH DAMAGE. 27 1.1 tshiozak */ 28 1.1 tshiozak 29 1.1 tshiozak /* 30 1.1 tshiozak * CAUTION: THIS IS NOT STANDALONE FILE 31 1.1 tshiozak * 32 1.1 tshiozak * function templates of iconv standard encoding handler for each encodings. 33 1.1 tshiozak * 34 1.1 tshiozak */ 35 1.1 tshiozak 36 1.1 tshiozak /* 37 1.1 tshiozak * macros 38 1.1 tshiozak */ 39 1.1 tshiozak 40 1.1 tshiozak #undef _TO_EI 41 1.1 tshiozak #undef _CE_TO_EI 42 1.1 tshiozak #undef _TO_STATE 43 1.1 tshiozak #define _TO_EI(_cl_) ((_ENCODING_INFO*)(_cl_)) 44 1.1 tshiozak #define _CE_TO_EI(_ce_) (_TO_EI((_ce_)->ce_closure)) 45 1.1 tshiozak #define _TO_STATE(_ps_) ((_ENCODING_STATE*)(_ps_)) 46 1.1 tshiozak 47 1.1 tshiozak /* ---------------------------------------------------------------------- 48 1.1 tshiozak * templates for public functions 49 1.1 tshiozak */ 50 1.1 tshiozak 51 1.1 tshiozak int 52 1.1 tshiozak _FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops, size_t lenops, 53 1.4 junyoung uint32_t expected_version) 54 1.1 tshiozak { 55 1.1 tshiozak if (expected_version<_CITRUS_STDENC_ABI_VERSION || lenops<sizeof(*ops)) 56 1.1 tshiozak return (EINVAL); 57 1.1 tshiozak 58 1.1 tshiozak memcpy(ops, &_FUNCNAME(stdenc_ops), sizeof(_FUNCNAME(stdenc_ops))); 59 1.1 tshiozak 60 1.1 tshiozak return (0); 61 1.1 tshiozak } 62 1.1 tshiozak 63 1.1 tshiozak static int 64 1.1 tshiozak _FUNCNAME(stdenc_init)(struct _citrus_stdenc * __restrict ce, 65 1.1 tshiozak const void * __restrict var, size_t lenvar, 66 1.1 tshiozak struct _citrus_stdenc_traits * __restrict et) 67 1.1 tshiozak { 68 1.1 tshiozak int ret; 69 1.1 tshiozak _ENCODING_INFO *ei; 70 1.1 tshiozak 71 1.1 tshiozak ei = NULL; 72 1.1 tshiozak if (sizeof(_ENCODING_INFO) > 0) { 73 1.1 tshiozak ei = calloc(1, sizeof(_ENCODING_INFO)); 74 1.1 tshiozak if (ei == NULL) { 75 1.1 tshiozak return errno; 76 1.1 tshiozak } 77 1.1 tshiozak } 78 1.1 tshiozak 79 1.1 tshiozak ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar); 80 1.1 tshiozak if (ret) { 81 1.1 tshiozak free((void *)ei); 82 1.1 tshiozak return ret; 83 1.1 tshiozak } 84 1.1 tshiozak 85 1.1 tshiozak ce->ce_closure = ei; 86 1.1 tshiozak et->et_state_size = sizeof(_ENCODING_STATE); 87 1.1 tshiozak et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce)); 88 1.1 tshiozak 89 1.1 tshiozak return 0; 90 1.1 tshiozak } 91 1.1 tshiozak 92 1.1 tshiozak static void 93 1.1 tshiozak _FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce) 94 1.1 tshiozak { 95 1.1 tshiozak if (ce) { 96 1.1 tshiozak _FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce)); 97 1.1 tshiozak free(ce->ce_closure); 98 1.1 tshiozak } 99 1.1 tshiozak } 100 1.1 tshiozak 101 1.1 tshiozak static int 102 1.1 tshiozak _FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce, 103 1.1 tshiozak void * __restrict ps) 104 1.1 tshiozak { 105 1.1 tshiozak _FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps)); 106 1.1 tshiozak 107 1.1 tshiozak return 0; 108 1.1 tshiozak } 109 1.1 tshiozak 110 1.1 tshiozak static int 111 1.1 tshiozak _FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce, 112 1.1 tshiozak _citrus_csid_t * __restrict csid, 113 1.1 tshiozak _citrus_index_t * __restrict idx, 114 1.1 tshiozak const char ** __restrict s, size_t n, 115 1.1 tshiozak void * __restrict ps, size_t * __restrict nresult) 116 1.1 tshiozak { 117 1.1 tshiozak int ret; 118 1.1 tshiozak wchar_t wc; 119 1.1 tshiozak 120 1.1 tshiozak _DIAGASSERT(nresult != NULL); 121 1.1 tshiozak 122 1.1 tshiozak ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n, 123 1.1 tshiozak _TO_STATE(ps), nresult); 124 1.1 tshiozak 125 1.1 tshiozak if (!ret && *nresult != (size_t)-2) 126 1.1 tshiozak _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc); 127 1.1 tshiozak 128 1.1 tshiozak return ret; 129 1.1 tshiozak } 130 1.1 tshiozak 131 1.1 tshiozak static int 132 1.1 tshiozak _FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce, 133 1.1 tshiozak char * __restrict s, size_t n, 134 1.1 tshiozak _citrus_csid_t csid, _citrus_index_t idx, 135 1.1 tshiozak void * __restrict ps, size_t * __restrict nresult) 136 1.1 tshiozak { 137 1.1 tshiozak int ret; 138 1.1 tshiozak wchar_t wc; 139 1.1 tshiozak 140 1.1 tshiozak _DIAGASSERT(nresult != NULL); 141 1.1 tshiozak 142 1.1 tshiozak wc = 0; 143 1.1 tshiozak 144 1.1 tshiozak if (csid != _CITRUS_CSID_INVALID) { 145 1.1 tshiozak ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx); 146 1.1 tshiozak if (ret) 147 1.1 tshiozak return ret; 148 1.1 tshiozak } 149 1.1 tshiozak 150 1.1 tshiozak return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), 151 1.1 tshiozak nresult); 152 1.1 tshiozak } 153 1.1 tshiozak 154 1.1 tshiozak static int 155 1.1 tshiozak _FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce, 156 1.1 tshiozak _citrus_wc_t * __restrict wc, 157 1.1 tshiozak const char ** __restrict s, size_t n, 158 1.1 tshiozak void * __restrict ps, size_t * __restrict nresult) 159 1.1 tshiozak { 160 1.1 tshiozak return _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n, 161 1.1 tshiozak _TO_STATE(ps), nresult); 162 1.1 tshiozak } 163 1.1 tshiozak 164 1.1 tshiozak static int 165 1.1 tshiozak _FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce, 166 1.1 tshiozak char * __restrict s, size_t n, _citrus_wc_t wc, 167 1.1 tshiozak void * __restrict ps, size_t * __restrict nresult) 168 1.1 tshiozak { 169 1.1 tshiozak return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), 170 1.1 tshiozak nresult); 171 1.2 tshiozak } 172 1.2 tshiozak 173 1.2 tshiozak static int 174 1.2 tshiozak _FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce, 175 1.2 tshiozak char * __restrict s, size_t n, 176 1.2 tshiozak void * __restrict ps, 177 1.2 tshiozak size_t * __restrict nresult) 178 1.2 tshiozak { 179 1.2 tshiozak #if _ENCODING_IS_STATE_DEPENDENT 180 1.2 tshiozak return _FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps), 181 1.2 tshiozak nresult); 182 1.2 tshiozak #else 183 1.2 tshiozak *nresult = 0; 184 1.2 tshiozak return 0; 185 1.2 tshiozak #endif 186 1.1 tshiozak } 187 1.3 tshiozak 188 1.3 tshiozak static int 189 1.3 tshiozak _FUNCNAME(stdenc_get_state_desc)(struct _citrus_stdenc * __restrict ce, 190 1.3 tshiozak void * __restrict ps, 191 1.3 tshiozak int id, 192 1.3 tshiozak struct _citrus_stdenc_state_desc * __restrict d) 193 1.3 tshiozak { 194 1.3 tshiozak int ret; 195 1.3 tshiozak 196 1.3 tshiozak switch (id) { 197 1.3 tshiozak case _STDENC_SDID_GENERIC: 198 1.3 tshiozak ret = _FUNCNAME(stdenc_get_state_desc_generic)( 199 1.3 tshiozak _CE_TO_EI(ce), _TO_STATE(ps), &d->u.generic.state); 200 1.3 tshiozak break; 201 1.3 tshiozak default: 202 1.3 tshiozak ret = EOPNOTSUPP; 203 1.3 tshiozak } 204 1.3 tshiozak 205 1.3 tshiozak return ret; 206 1.3 tshiozak } 207