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