xref: /freebsd/lib/libc/iconv/citrus_stdenc_template.h (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_stdenc_template.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
4d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
5d915a14eSPedro F. Giffuni  *
6ad30f8e7SGabor Kovesdan  * Copyright (c)2003 Citrus Project,
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 <iconv.h>
32ad30f8e7SGabor Kovesdan 
33ad30f8e7SGabor Kovesdan /*
34ad30f8e7SGabor Kovesdan  * CAUTION: THIS IS NOT STANDALONE FILE
35ad30f8e7SGabor Kovesdan  *
36ad30f8e7SGabor Kovesdan  * function templates of iconv standard encoding handler for each encodings.
37ad30f8e7SGabor Kovesdan  *
38ad30f8e7SGabor Kovesdan  */
39ad30f8e7SGabor Kovesdan 
40ad30f8e7SGabor Kovesdan /*
41ad30f8e7SGabor Kovesdan  * macros
42ad30f8e7SGabor Kovesdan  */
43ad30f8e7SGabor Kovesdan 
44ad30f8e7SGabor Kovesdan #undef _TO_EI
45ad30f8e7SGabor Kovesdan #undef _CE_TO_EI
46ad30f8e7SGabor Kovesdan #undef _TO_STATE
47ad30f8e7SGabor Kovesdan #define _TO_EI(_cl_)	((_ENCODING_INFO*)(_cl_))
48ad30f8e7SGabor Kovesdan #define _CE_TO_EI(_ce_)	(_TO_EI((_ce_)->ce_closure))
49ad30f8e7SGabor Kovesdan #define _TO_STATE(_ps_)	((_ENCODING_STATE*)(_ps_))
50ad30f8e7SGabor Kovesdan 
51*2300a22cSKyle Evans #ifndef _ENCODING_MB_CUR_MIN
52*2300a22cSKyle Evans /* Assume one byte minimum unless otherwise specified. */
53*2300a22cSKyle Evans #define	_ENCODING_MB_CUR_MIN(_ei_)	1
54*2300a22cSKyle Evans #endif
55*2300a22cSKyle Evans 
56ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
57ad30f8e7SGabor Kovesdan  * templates for public functions
58ad30f8e7SGabor Kovesdan  */
59ad30f8e7SGabor Kovesdan 
60ad30f8e7SGabor Kovesdan int
_FUNCNAME(stdenc_getops)61ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops,
62ad30f8e7SGabor Kovesdan     size_t lenops __unused)
63ad30f8e7SGabor Kovesdan {
64ad30f8e7SGabor Kovesdan 
65ad30f8e7SGabor Kovesdan 	memcpy(ops, &_FUNCNAME(stdenc_ops), sizeof(_FUNCNAME(stdenc_ops)));
66ad30f8e7SGabor Kovesdan 
67ad30f8e7SGabor Kovesdan 	return (0);
68ad30f8e7SGabor Kovesdan }
69ad30f8e7SGabor Kovesdan 
70ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_init)71ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_init)(struct _citrus_stdenc * __restrict ce,
72ad30f8e7SGabor Kovesdan     const void * __restrict var, size_t lenvar,
73ad30f8e7SGabor Kovesdan     struct _citrus_stdenc_traits * __restrict et)
74ad30f8e7SGabor Kovesdan {
75ad30f8e7SGabor Kovesdan 	_ENCODING_INFO *ei;
76ad30f8e7SGabor Kovesdan 	int ret;
77ad30f8e7SGabor Kovesdan 
78ad30f8e7SGabor Kovesdan 	ei = NULL;
79ad30f8e7SGabor Kovesdan 	if (sizeof(_ENCODING_INFO) > 0) {
80ad30f8e7SGabor Kovesdan 		ei = calloc(1, sizeof(_ENCODING_INFO));
81ad30f8e7SGabor Kovesdan 		if (ei == NULL)
82ad30f8e7SGabor Kovesdan 			return (errno);
83ad30f8e7SGabor Kovesdan 	}
84ad30f8e7SGabor Kovesdan 
85ad30f8e7SGabor Kovesdan 	ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar);
86ad30f8e7SGabor Kovesdan 	if (ret) {
87ad30f8e7SGabor Kovesdan 		free((void *)ei);
88ad30f8e7SGabor Kovesdan 		return (ret);
89ad30f8e7SGabor Kovesdan 	}
90ad30f8e7SGabor Kovesdan 
91ad30f8e7SGabor Kovesdan 	ce->ce_closure = ei;
92ad30f8e7SGabor Kovesdan 	et->et_state_size = sizeof(_ENCODING_STATE);
93ad30f8e7SGabor Kovesdan 	et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce));
94*2300a22cSKyle Evans 	et->et_mb_cur_min = _ENCODING_MB_CUR_MIN(_CE_TO_EI(ce));
95ad30f8e7SGabor Kovesdan 
96ad30f8e7SGabor Kovesdan 	return (0);
97ad30f8e7SGabor Kovesdan }
98ad30f8e7SGabor Kovesdan 
99ad30f8e7SGabor Kovesdan static void
_FUNCNAME(stdenc_uninit)100ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce)
101ad30f8e7SGabor Kovesdan {
102ad30f8e7SGabor Kovesdan 
103ad30f8e7SGabor Kovesdan 	if (ce) {
104ad30f8e7SGabor Kovesdan 		_FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce));
105ad30f8e7SGabor Kovesdan 		free(ce->ce_closure);
106ad30f8e7SGabor Kovesdan 	}
107ad30f8e7SGabor Kovesdan }
108ad30f8e7SGabor Kovesdan 
109ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_init_state)110ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce,
111ad30f8e7SGabor Kovesdan     void * __restrict ps)
112ad30f8e7SGabor Kovesdan {
113ad30f8e7SGabor Kovesdan 
114ad30f8e7SGabor Kovesdan 	_FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps));
115ad30f8e7SGabor Kovesdan 
116ad30f8e7SGabor Kovesdan 	return (0);
117ad30f8e7SGabor Kovesdan }
118ad30f8e7SGabor Kovesdan 
119ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_mbtocs)120ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce,
121ad30f8e7SGabor Kovesdan     _citrus_csid_t * __restrict csid, _citrus_index_t * __restrict idx,
1221243a98eSTijl Coosemans     char ** __restrict s, size_t n, void * __restrict ps,
123ad30f8e7SGabor Kovesdan     size_t * __restrict nresult, struct iconv_hooks *hooks)
124ad30f8e7SGabor Kovesdan {
125ad30f8e7SGabor Kovesdan 	wchar_t wc;
126ad30f8e7SGabor Kovesdan 	int ret;
127ad30f8e7SGabor Kovesdan 
128ad30f8e7SGabor Kovesdan 	ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n,
129ad30f8e7SGabor Kovesdan 	    _TO_STATE(ps), nresult);
130ad30f8e7SGabor Kovesdan 
131ad30f8e7SGabor Kovesdan 	if ((ret == 0) && *nresult != (size_t)-2)
132ad30f8e7SGabor Kovesdan 		ret = _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc);
133ad30f8e7SGabor Kovesdan 
134ad30f8e7SGabor Kovesdan 	if ((ret == 0) && (hooks != NULL) && (hooks->uc_hook != NULL))
135ad30f8e7SGabor Kovesdan 		hooks->uc_hook((unsigned int)*idx, hooks->data);
136ad30f8e7SGabor Kovesdan 	return (ret);
137ad30f8e7SGabor Kovesdan }
138ad30f8e7SGabor Kovesdan 
139ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_cstomb)140ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce,
141ad30f8e7SGabor Kovesdan     char * __restrict s, size_t n, _citrus_csid_t csid, _citrus_index_t idx,
142ad30f8e7SGabor Kovesdan     void * __restrict ps, size_t * __restrict nresult,
143ad30f8e7SGabor Kovesdan     struct iconv_hooks *hooks __unused)
144ad30f8e7SGabor Kovesdan {
145ad30f8e7SGabor Kovesdan 	wchar_t wc;
146ad30f8e7SGabor Kovesdan 	int ret;
147ad30f8e7SGabor Kovesdan 
148ad30f8e7SGabor Kovesdan 	wc = ret = 0;
149ad30f8e7SGabor Kovesdan 
150ad30f8e7SGabor Kovesdan 	if (csid != _CITRUS_CSID_INVALID)
151ad30f8e7SGabor Kovesdan 		ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx);
152ad30f8e7SGabor Kovesdan 
153ad30f8e7SGabor Kovesdan 	if (ret == 0)
154ad30f8e7SGabor Kovesdan 		ret = _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc,
155ad30f8e7SGabor Kovesdan 		    _TO_STATE(ps), nresult);
156ad30f8e7SGabor Kovesdan 	return (ret);
157ad30f8e7SGabor Kovesdan }
158ad30f8e7SGabor Kovesdan 
159ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_mbtowc)160ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce,
1611243a98eSTijl Coosemans     _citrus_wc_t * __restrict wc, char ** __restrict s, size_t n,
162ad30f8e7SGabor Kovesdan     void * __restrict ps, size_t * __restrict nresult,
163ad30f8e7SGabor Kovesdan     struct iconv_hooks *hooks)
164ad30f8e7SGabor Kovesdan {
165ad30f8e7SGabor Kovesdan 	int ret;
166ad30f8e7SGabor Kovesdan 
167ad30f8e7SGabor Kovesdan 	ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n,
168ad30f8e7SGabor Kovesdan 	    _TO_STATE(ps), nresult);
169ad30f8e7SGabor Kovesdan 	if ((ret == 0) && (hooks != NULL) && (hooks->wc_hook != NULL))
170ad30f8e7SGabor Kovesdan 		hooks->wc_hook(*wc, hooks->data);
171ad30f8e7SGabor Kovesdan 	return (ret);
172ad30f8e7SGabor Kovesdan }
173ad30f8e7SGabor Kovesdan 
174ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_wctomb)175ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce,
176ad30f8e7SGabor Kovesdan     char * __restrict s, size_t n, _citrus_wc_t wc, void * __restrict ps,
177ad30f8e7SGabor Kovesdan     size_t * __restrict nresult, struct iconv_hooks *hooks __unused)
178ad30f8e7SGabor Kovesdan {
179ad30f8e7SGabor Kovesdan 	int ret;
180ad30f8e7SGabor Kovesdan 
181ad30f8e7SGabor Kovesdan 	ret = _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps),
182ad30f8e7SGabor Kovesdan 	    nresult);
183ad30f8e7SGabor Kovesdan 	return (ret);
184ad30f8e7SGabor Kovesdan }
185ad30f8e7SGabor Kovesdan 
186ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_put_state_reset)187ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce __unused,
188ad30f8e7SGabor Kovesdan     char * __restrict s __unused, size_t n __unused,
189ad30f8e7SGabor Kovesdan     void * __restrict ps __unused, size_t * __restrict nresult)
190ad30f8e7SGabor Kovesdan {
191ad30f8e7SGabor Kovesdan 
192ad30f8e7SGabor Kovesdan #if _ENCODING_IS_STATE_DEPENDENT
193ad30f8e7SGabor Kovesdan 	return ((_FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps),
194ad30f8e7SGabor Kovesdan 	    nresult)));
195ad30f8e7SGabor Kovesdan #else
196ad30f8e7SGabor Kovesdan 	*nresult = 0;
197ad30f8e7SGabor Kovesdan 	return (0);
198ad30f8e7SGabor Kovesdan #endif
199ad30f8e7SGabor Kovesdan }
200ad30f8e7SGabor Kovesdan 
201ad30f8e7SGabor Kovesdan static int
_FUNCNAME(stdenc_get_state_desc)202ad30f8e7SGabor Kovesdan _FUNCNAME(stdenc_get_state_desc)(struct _citrus_stdenc * __restrict ce,
203ad30f8e7SGabor Kovesdan     void * __restrict ps, int id,
204ad30f8e7SGabor Kovesdan     struct _citrus_stdenc_state_desc * __restrict d)
205ad30f8e7SGabor Kovesdan {
206ad30f8e7SGabor Kovesdan 	int ret;
207ad30f8e7SGabor Kovesdan 
208ad30f8e7SGabor Kovesdan 	switch (id) {
209ad30f8e7SGabor Kovesdan 	case _STDENC_SDID_GENERIC:
210ad30f8e7SGabor Kovesdan 		ret = _FUNCNAME(stdenc_get_state_desc_generic)(
211ad30f8e7SGabor Kovesdan 		    _CE_TO_EI(ce), _TO_STATE(ps), &d->u.generic.state);
212ad30f8e7SGabor Kovesdan 		break;
213ad30f8e7SGabor Kovesdan 	default:
214ad30f8e7SGabor Kovesdan 		ret = EOPNOTSUPP;
215ad30f8e7SGabor Kovesdan 	}
216ad30f8e7SGabor Kovesdan 
217ad30f8e7SGabor Kovesdan 	return (ret);
218ad30f8e7SGabor Kovesdan }
219