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