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