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