Correct a bug in seekdir/readdir which could cause the directory entry
[dragonfly.git] / lib / libc / citrus / citrus_stdenc_template.h
blobc26046e0a41d6ac070515e07db4d8fe9cca3a46a
1 /* $NetBSD: citrus_stdenc_template.h,v 1.4 2008/02/09 14:56:20 junyoung Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_stdenc_template.h,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
4 /*-
5 * Copyright (c)2003 Citrus Project,
6 * All rights reserved.
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.
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.
31 * CAUTION: THIS IS NOT STANDALONE FILE
33 * function templates of iconv standard encoding handler for each encodings.
38 * macros
41 #undef _TO_EI
42 #undef _CE_TO_EI
43 #undef _TO_STATE
44 #define _TO_EI(_cl_) ((_ENCODING_INFO*)(_cl_))
45 #define _CE_TO_EI(_ce_) (_TO_EI((_ce_)->ce_closure))
46 #define _TO_STATE(_ps_) ((_ENCODING_STATE*)(_ps_))
48 /* ----------------------------------------------------------------------
49 * templates for public functions
52 int
53 _FUNCNAME(stdenc_getops)(struct _citrus_stdenc_ops *ops, size_t lenops,
54 uint32_t expected_version)
56 if (expected_version<_CITRUS_STDENC_ABI_VERSION || lenops<sizeof(*ops))
57 return (EINVAL);
59 memcpy(ops, &_FUNCNAME(stdenc_ops), sizeof(_FUNCNAME(stdenc_ops)));
61 return (0);
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)
69 int ret;
70 _ENCODING_INFO *ei;
72 ei = NULL;
73 if (sizeof(_ENCODING_INFO) > 0) {
74 ei = calloc(1, sizeof(_ENCODING_INFO));
75 if (ei == NULL) {
76 return errno;
80 ret = _FUNCNAME(encoding_module_init)(ei, var, lenvar);
81 if (ret) {
82 free((void *)ei);
83 return ret;
86 ce->ce_closure = ei;
87 et->et_state_size = sizeof(_ENCODING_STATE);
88 et->et_mb_cur_max = _ENCODING_MB_CUR_MAX(_CE_TO_EI(ce));
90 return 0;
93 static void
94 _FUNCNAME(stdenc_uninit)(struct _citrus_stdenc * __restrict ce)
96 if (ce) {
97 _FUNCNAME(encoding_module_uninit)(_CE_TO_EI(ce));
98 free(ce->ce_closure);
102 static int
103 _FUNCNAME(stdenc_init_state)(struct _citrus_stdenc * __restrict ce,
104 void * __restrict ps)
106 _FUNCNAME(init_state)(_CE_TO_EI(ce), _TO_STATE(ps));
108 return 0;
111 static int
112 _FUNCNAME(stdenc_mbtocs)(struct _citrus_stdenc * __restrict ce,
113 _citrus_csid_t * __restrict csid,
114 _citrus_index_t * __restrict idx,
115 const char ** __restrict s, size_t n,
116 void * __restrict ps, size_t * __restrict nresult)
118 int ret;
119 wchar_t wc;
121 _DIAGASSERT(nresult != NULL);
123 ret = _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), &wc, s, n,
124 _TO_STATE(ps), nresult);
126 if (!ret && *nresult != (size_t)-2)
127 _FUNCNAME(stdenc_wctocs)(_CE_TO_EI(ce), csid, idx, wc);
129 return ret;
132 static int
133 _FUNCNAME(stdenc_cstomb)(struct _citrus_stdenc * __restrict ce,
134 char * __restrict s, size_t n,
135 _citrus_csid_t csid, _citrus_index_t idx,
136 void * __restrict ps, size_t * __restrict nresult)
138 int ret;
139 wchar_t wc;
141 _DIAGASSERT(nresult != NULL);
143 wc = 0;
145 if (csid != _CITRUS_CSID_INVALID) {
146 ret = _FUNCNAME(stdenc_cstowc)(_CE_TO_EI(ce), &wc, csid, idx);
147 if (ret)
148 return ret;
151 return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps),
152 nresult);
155 static int
156 _FUNCNAME(stdenc_mbtowc)(struct _citrus_stdenc * __restrict ce,
157 _citrus_wc_t * __restrict wc,
158 const char ** __restrict s, size_t n,
159 void * __restrict ps, size_t * __restrict nresult)
161 return _FUNCNAME(mbrtowc_priv)(_CE_TO_EI(ce), wc, s, n,
162 _TO_STATE(ps), nresult);
165 static int
166 _FUNCNAME(stdenc_wctomb)(struct _citrus_stdenc * __restrict ce,
167 char * __restrict s, size_t n, _citrus_wc_t wc,
168 void * __restrict ps, size_t * __restrict nresult)
170 return _FUNCNAME(wcrtomb_priv)(_CE_TO_EI(ce), s, n, wc, _TO_STATE(ps),
171 nresult);
174 static int
175 _FUNCNAME(stdenc_put_state_reset)(struct _citrus_stdenc * __restrict ce,
176 char * __restrict s, size_t n,
177 void * __restrict ps,
178 size_t * __restrict nresult)
180 #if _ENCODING_IS_STATE_DEPENDENT
181 return _FUNCNAME(put_state_reset)(_CE_TO_EI(ce), s, n, _TO_STATE(ps),
182 nresult);
183 #else
184 *nresult = 0;
185 return 0;
186 #endif
189 static int
190 _FUNCNAME(stdenc_get_state_desc)(struct _citrus_stdenc * __restrict ce,
191 void * __restrict ps,
192 int id,
193 struct _citrus_stdenc_state_desc * __restrict d)
195 int ret;
197 switch (id) {
198 case _STDENC_SDID_GENERIC:
199 ret = _FUNCNAME(stdenc_get_state_desc_generic)(
200 _CE_TO_EI(ce), _TO_STATE(ps), &d->u.generic.state);
201 break;
202 default:
203 ret = EOPNOTSUPP;
206 return ret;