1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_stdenc_template.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2003 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor */ 29219019Sgabor 30219019Sgabor#include <iconv.h> 31219019Sgabor 32219019Sgabor/* 33219019Sgabor * CAUTION: THIS IS NOT STANDALONE FILE 34219019Sgabor * 35219019Sgabor * function templates of iconv standard encoding handler for each encodings. 36219019Sgabor * 37219019Sgabor */ 38219019Sgabor 39219019Sgabor/* 40219019Sgabor * macros 41219019Sgabor */ 42219019Sgabor 43219019Sgabor#undef _TO_EI 44219019Sgabor#undef _CE_TO_EI 45219019Sgabor#undef _TO_STATE 46219019Sgabor#define _TO_EI(_cl_) ((_ENCODING_INFO*)(_cl_)) 47219019Sgabor#define _CE_TO_EI(_ce_) (_TO_EI((_ce_)->ce_closure)) 48219019Sgabor#define _TO_STATE(_ps_) ((_ENCODING_STATE*)(_ps_)) 49219019Sgabor 50219019Sgabor/* ---------------------------------------------------------------------- 51219019Sgabor * templates for public functions 52219019Sgabor */ 53219019Sgabor 54219019Sgaborint 55219019Sgabor_FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops, 56219019Sgabor size_t lenops __unused) 57219019Sgabor{ 58219019Sgabor 59219019Sgabor memcpy(ops, &_FUNCNAME(stdenc_ops), sizeof(_FUNCNAME(stdenc_ops))); 60219019Sgabor 61219019Sgabor return (0); 62219019Sgabor} 63219019Sgabor 64219019Sgaborstatic int 65219019Sgabor_FUNCNAME(stdenc_init)(struct _citrus_stdenc * __restrict ce, 66219019Sgabor const void * __restrict var, size_t lenvar, 67219019Sgabor struct _citrus_stdenc_traits * __restrict et) 68219019Sgabor{ 69219019Sgabor _ENCODING_INFO *ei; 70219019Sgabor int ret; 71219019Sgabor 72219019Sgabor ei = NULL; 73219019Sgabor if (sizeof(_ENCODING_INFO) > 0) { 74219019Sgabor ei = calloc(1, sizeof(_ENCODING_INFO)); 75219019Sgabor if (ei == NULL) 76219019Sgabor return (errno); 77219019Sgabor } 78219019Sgabor 79219019Sgabor ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar); 80219019Sgabor if (ret) { 81219019Sgabor free((void *)ei); 82219019Sgabor return (ret); 83219019Sgabor } 84219019Sgabor 85219019Sgabor ce->ce_closure = ei; 86219019Sgabor et->et_state_size = sizeof(_ENCODING_STATE); 87219019Sgabor et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce)); 88219019Sgabor 89219019Sgabor return (0); 90219019Sgabor} 91219019Sgabor 92219019Sgaborstatic void 93219019Sgabor_FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce) 94219019Sgabor{ 95219019Sgabor 96219019Sgabor if (ce) { 97219019Sgabor _FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce)); 98219019Sgabor free(ce->ce_closure); 99219019Sgabor } 100219019Sgabor} 101219019Sgabor 102219019Sgaborstatic int 103219019Sgabor_FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce, 104219019Sgabor void * __restrict ps) 105219019Sgabor{ 106219019Sgabor 107219019Sgabor _FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps)); 108219019Sgabor 109219019Sgabor return (0); 110219019Sgabor} 111219019Sgabor 112219019Sgaborstatic int 113219019Sgabor_FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce, 114219019Sgabor _citrus_csid_t * __restrict csid, _citrus_index_t * __restrict idx, 115282275Stijl char ** __restrict s, size_t n, void * __restrict ps, 116219019Sgabor size_t * __restrict nresult, struct iconv_hooks *hooks) 117219019Sgabor{ 118219019Sgabor wchar_t wc; 119219019Sgabor int ret; 120219019Sgabor 121219019Sgabor ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n, 122219019Sgabor _TO_STATE(ps), nresult); 123219019Sgabor 124219019Sgabor if ((ret == 0) && *nresult != (size_t)-2) 125219019Sgabor ret = _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc); 126219019Sgabor 127219019Sgabor if ((ret == 0) && (hooks != NULL) && (hooks->uc_hook != NULL)) 128219019Sgabor hooks->uc_hook((unsigned int)*idx, hooks->data); 129219019Sgabor return (ret); 130219019Sgabor} 131219019Sgabor 132219019Sgaborstatic int 133219019Sgabor_FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce, 134219019Sgabor char * __restrict s, size_t n, _citrus_csid_t csid, _citrus_index_t idx, 135219019Sgabor void * __restrict ps, size_t * __restrict nresult, 136219019Sgabor struct iconv_hooks *hooks __unused) 137219019Sgabor{ 138219019Sgabor wchar_t wc; 139219019Sgabor int ret; 140219019Sgabor 141219019Sgabor wc = ret = 0; 142219019Sgabor 143219019Sgabor if (csid != _CITRUS_CSID_INVALID) 144219019Sgabor ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx); 145219019Sgabor 146219019Sgabor if (ret == 0) 147219019Sgabor ret = _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, 148219019Sgabor _TO_STATE(ps), nresult); 149219019Sgabor return (ret); 150219019Sgabor} 151219019Sgabor 152219019Sgaborstatic int 153219019Sgabor_FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce, 154282275Stijl _citrus_wc_t * __restrict wc, char ** __restrict s, size_t n, 155219019Sgabor void * __restrict ps, size_t * __restrict nresult, 156219019Sgabor struct iconv_hooks *hooks) 157219019Sgabor{ 158219019Sgabor int ret; 159219019Sgabor 160219019Sgabor ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n, 161219019Sgabor _TO_STATE(ps), nresult); 162219019Sgabor if ((ret == 0) && (hooks != NULL) && (hooks->wc_hook != NULL)) 163219019Sgabor hooks->wc_hook(*wc, hooks->data); 164219019Sgabor return (ret); 165219019Sgabor} 166219019Sgabor 167219019Sgaborstatic int 168219019Sgabor_FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce, 169219019Sgabor char * __restrict s, size_t n, _citrus_wc_t wc, void * __restrict ps, 170219019Sgabor size_t * __restrict nresult, struct iconv_hooks *hooks __unused) 171219019Sgabor{ 172219019Sgabor int ret; 173219019Sgabor 174219019Sgabor ret = _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps), 175219019Sgabor nresult); 176219019Sgabor return (ret); 177219019Sgabor} 178219019Sgabor 179219019Sgaborstatic int 180219019Sgabor_FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce __unused, 181219019Sgabor char * __restrict s __unused, size_t n __unused, 182219019Sgabor void * __restrict ps __unused, size_t * __restrict nresult) 183219019Sgabor{ 184219019Sgabor 185219019Sgabor#if _ENCODING_IS_STATE_DEPENDENT 186219019Sgabor return ((_FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps), 187219019Sgabor nresult))); 188219019Sgabor#else 189219019Sgabor *nresult = 0; 190219019Sgabor return (0); 191219019Sgabor#endif 192219019Sgabor} 193219019Sgabor 194219019Sgaborstatic int 195219019Sgabor_FUNCNAME(stdenc_get_state_desc)(struct _citrus_stdenc * __restrict ce, 196219019Sgabor void * __restrict ps, int id, 197219019Sgabor struct _citrus_stdenc_state_desc * __restrict d) 198219019Sgabor{ 199219019Sgabor int ret; 200219019Sgabor 201219019Sgabor switch (id) { 202219019Sgabor case _STDENC_SDID_GENERIC: 203219019Sgabor ret = _FUNCNAME(stdenc_get_state_desc_generic)( 204219019Sgabor _CE_TO_EI(ce), _TO_STATE(ps), &d->u.generic.state); 205219019Sgabor break; 206219019Sgabor default: 207219019Sgabor ret = EOPNOTSUPP; 208219019Sgabor } 209219019Sgabor 210219019Sgabor return (ret); 211219019Sgabor} 212