citrus_stdenc.c revision 1.1 1 /* $NetBSD: citrus_stdenc.c,v 1.1 2003/06/25 09:51:40 tshiozak 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 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_stdenc.c,v 1.1 2003/06/25 09:51:40 tshiozak Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include "namespace.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "citrus_namespace.h"
41 #include "citrus_types.h"
42 #include "citrus_module.h"
43 #include "citrus_stdenc.h"
44 #include "citrus_none.h"
45
46 struct _citrus_stdenc _citrus_stdenc_default = {
47 &_citrus_NONE_stdenc_ops, /* ce_ops */
48 NULL, /* ce_closure */
49 NULL, /* ce_module */
50 &_citrus_NONE_stdenc_traits, /* ce_traits */
51 };
52
53 #ifdef _I18N_DYNAMIC
54
55 int
56 _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,
57 char const * __restrict encname,
58 const void * __restrict variable, size_t lenvar)
59 {
60 int ret;
61 _citrus_module_t handle;
62 struct _citrus_stdenc *ce;
63 _citrus_stdenc_getops_t getops;
64
65 _DIAGASSERT(encname != NULL);
66 _DIAGASSERT(!lenvar || variable!=NULL);
67 _DIAGASSERT(rcc != NULL);
68
69 if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) {
70 *rce = &_citrus_stdenc_default;
71 return (0);
72 }
73 ce = malloc(sizeof(*ce));
74 if (ce==NULL) {
75 ret = errno;
76 goto bad;
77 }
78 ce->ce_ops = NULL;
79 ce->ce_closure = NULL;
80 ce->ce_module = NULL;
81 ce->ce_traits = NULL;
82
83 ret = _citrus_load_module(&handle, encname);
84 if (ret)
85 goto bad;
86
87 ce->ce_module = handle;
88
89 getops =
90 (_citrus_stdenc_getops_t)_citrus_find_getops(ce->ce_module,
91 encname, "stdenc");
92 if (getops == NULL) {
93 ret = EINVAL;
94 goto bad;
95 }
96
97 ce->ce_ops = (struct _citrus_stdenc_ops *)malloc(sizeof(*ce->ce_ops));
98 if (ce->ce_ops == NULL) {
99 ret = errno;
100 goto bad;
101 }
102
103 ret = (*getops)(ce->ce_ops, sizeof(*ce->ce_ops),
104 _CITRUS_STDENC_ABI_VERSION);
105 if (ret)
106 goto bad;
107
108 /* If return ABI version is not expected, should fixup it */
109
110 /* validation check */
111 if (ce->ce_ops->eo_init == NULL ||
112 ce->ce_ops->eo_uninit == NULL ||
113 ce->ce_ops->eo_init_state == NULL ||
114 ce->ce_ops->eo_mbtocs == NULL ||
115 ce->ce_ops->eo_cstomb == NULL ||
116 ce->ce_ops->eo_mbtowc == NULL ||
117 ce->ce_ops->eo_wctomb == NULL)
118 goto bad;
119
120 /* allocate traits */
121 ce->ce_traits = malloc(sizeof(*ce->ce_traits));
122 if (ce->ce_traits == NULL) {
123 ret = errno;
124 goto bad;
125 }
126 /* init and get closure */
127 ret = (*ce->ce_ops->eo_init)(ce, variable, lenvar, ce->ce_traits);
128 if (ret)
129 goto bad;
130
131 *rce = ce;
132
133 return (0);
134
135 bad:
136 _citrus_stdenc_close(ce);
137 return (ret);
138 }
139
140 void
141 _citrus_stdenc_close(struct _citrus_stdenc *ce)
142 {
143
144 _DIAGASSERT(ci != NULL);
145
146 if (ce == &_citrus_stdenc_default)
147 return;
148
149 if (ce->ce_module) {
150 if (ce->ce_ops) {
151 if (ce->ce_closure && ce->ce_ops->eo_uninit)
152 (*ce->ce_ops->eo_uninit)(ce);
153 free(ce->ce_ops);
154 }
155 free(ce->ce_traits);
156 _citrus_unload_module(ce->ce_module);
157 }
158 free(ce);
159 }
160
161 #else
162 /* !_I18N_DYNAMIC */
163
164 int
165 /*ARGSUSED*/
166 _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,
167 char const * __restrict encname,
168 const void * __restrict variable, size_t lenvar)
169 {
170 if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) {
171 *rce = &_citrus_stdenc_default;
172 return (0);
173 }
174 return (EINVAL);
175 }
176
177 void
178 /*ARGSUSED*/
179 _citrus_stdenc_close(struct _citrus_stdenc *ce)
180 {
181 }
182
183 #endif
184