some coverity fixes.
[minix.git] / lib / libc / citrus / citrus_ctype.c
blobd661c2a9f7296535a7870b8e229413204c2f6175
1 /* $NetBSD: citrus_ctype.c,v 1.5 2008/06/14 16:01:07 tnozaki Exp $ */
3 /*-
4 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_ctype.c,v 1.5 2008/06/14 16:01:07 tnozaki Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/types.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <stddef.h>
43 #include <wchar.h>
44 #include "citrus_module.h"
45 #include "citrus_ctype.h"
46 #include "citrus_ctype_fallback.h"
47 #include "citrus_none.h"
48 #include _CITRUS_DEFAULT_CTYPE_HEADER
50 _citrus_ctype_rec_t _citrus_ctype_default = {
51 &_CITRUS_DEFAULT_CTYPE_OPS, /* cc_ops */
52 NULL, /* cc_closure */
53 NULL /* cc_module */
56 #ifdef _I18N_DYNAMIC
58 static int _initctypemodule(_citrus_ctype_t, char const *, _citrus_module_t,
59 void *, size_t, size_t);
61 static int
62 _initctypemodule(_citrus_ctype_t cc, char const *modname,
63 _citrus_module_t handle, void *variable, size_t lenvar,
64 size_t szpriv)
66 int ret;
67 _citrus_ctype_getops_t getops;
69 _DIAGASSERT(cc != NULL);
71 cc->cc_module = handle;
73 getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module,
74 modname,
75 "ctype");
76 if (getops == NULL)
77 return (EINVAL);
79 cc->cc_ops = (_citrus_ctype_ops_rec_t *)malloc(sizeof(*cc->cc_ops));
80 if (cc->cc_ops == NULL)
81 return (ENOMEM);
83 ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops),
84 _CITRUS_CTYPE_ABI_VERSION);
85 if (ret)
86 goto bad;
88 /* If return ABI version is not expected, fixup it here*/
89 switch (cc->cc_ops->co_abi_version) {
90 case 0x00000001:
91 cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback;
92 cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback;
93 /* FALLTHROUGH */
94 case 0x00000002:
95 /* FALLTHROUGH */
96 default:
97 break;
100 /* validation check */
101 if (cc->cc_ops->co_init == NULL ||
102 cc->cc_ops->co_uninit == NULL ||
103 cc->cc_ops->co_get_mb_cur_max == NULL ||
104 cc->cc_ops->co_mblen == NULL ||
105 cc->cc_ops->co_mbrlen == NULL ||
106 cc->cc_ops->co_mbrtowc == NULL ||
107 cc->cc_ops->co_mbsinit == NULL ||
108 cc->cc_ops->co_mbsrtowcs == NULL ||
109 cc->cc_ops->co_mbstowcs == NULL ||
110 cc->cc_ops->co_mbtowc == NULL ||
111 cc->cc_ops->co_wcrtomb == NULL ||
112 cc->cc_ops->co_wcsrtombs == NULL ||
113 cc->cc_ops->co_wcstombs == NULL ||
114 cc->cc_ops->co_wctomb == NULL ||
115 cc->cc_ops->co_btowc == NULL ||
116 cc->cc_ops->co_wctob == NULL)
117 goto bad;
119 /* init and get closure */
120 ret = (*cc->cc_ops->co_init)(
121 &cc->cc_closure, variable, lenvar, szpriv);
122 if (ret)
123 goto bad;
125 return (0);
127 bad:
128 if (cc->cc_ops)
129 free(cc->cc_ops);
130 cc->cc_ops = NULL;
132 return (ret);
136 _citrus_ctype_open(_citrus_ctype_t *rcc,
137 char const *encname, void *variable, size_t lenvar,
138 size_t szpriv)
140 int ret;
141 _citrus_module_t handle;
142 _citrus_ctype_t cc;
144 _DIAGASSERT(encname != NULL);
145 _DIAGASSERT(!lenvar || variable!=NULL);
146 _DIAGASSERT(rcc != NULL);
148 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) {
149 *rcc = &_citrus_ctype_default;
150 return (0);
152 ret = _citrus_load_module(&handle, encname);
153 if (ret)
154 return (ret);
156 cc = calloc(1, sizeof(*cc));
157 if (!cc) {
158 _citrus_unload_module(handle);
159 return (errno);
162 ret = _initctypemodule(cc, encname, handle, variable, lenvar, szpriv);
163 if (ret) {
164 _citrus_unload_module(cc->cc_module);
165 free(cc);
166 return (ret);
169 *rcc = cc;
171 return (0);
174 void
175 _citrus_ctype_close(_citrus_ctype_t cc)
178 _DIAGASSERT(cc != NULL);
180 if (cc == &_citrus_ctype_default)
181 return;
182 (*cc->cc_ops->co_uninit)(cc->cc_closure);
183 free(cc->cc_ops);
184 _citrus_unload_module(cc->cc_module);
185 free(cc);
188 #else
189 /* !_I18N_DYNAMIC */
192 /*ARGSUSED*/
193 _citrus_ctype_open(_citrus_ctype_t *rcc,
194 char const *encname, void *variable, size_t lenvar,
195 size_t szpriv)
197 if (!strcmp(encname, _CITRUS_DEFAULT_CTYPE_NAME)) {
198 *rcc = &_citrus_ctype_default;
199 return (0);
201 return (EINVAL);
204 void
205 /*ARGSUSED*/
206 _citrus_ctype_close(_citrus_ctype_t cc)
210 #endif