1 /* $NetBSD: citrus_ctype.c,v 1.7 2013/05/28 16:57:56 joerg Exp $ */
4 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_ctype.c,v 1.7 2013/05/28 16:57:56 joerg Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/types.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 */
58 static int _initctypemodule(_citrus_ctype_t
, char const *, _citrus_module_t
,
59 void *, size_t, size_t);
62 _initctypemodule(_citrus_ctype_t cc
, char const *modname
,
63 _citrus_module_t handle
, void *variable
, size_t lenvar
,
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
,
79 cc
->cc_ops
= (_citrus_ctype_ops_rec_t
*)malloc(sizeof(*cc
->cc_ops
));
80 if (cc
->cc_ops
== NULL
)
83 ret
= (*getops
)(cc
->cc_ops
, sizeof(*cc
->cc_ops
),
84 _CITRUS_CTYPE_ABI_VERSION
);
88 /* If return ABI version is not expected, fixup it here*/
89 switch (cc
->cc_ops
->co_abi_version
) {
91 cc
->cc_ops
->co_btowc
= &_citrus_ctype_btowc_fallback
;
92 cc
->cc_ops
->co_wctob
= &_citrus_ctype_wctob_fallback
;
95 cc
->cc_ops
->co_mbsnrtowcs
= &_citrus_ctype_mbsnrtowcs_fallback
;
96 cc
->cc_ops
->co_wcsnrtombs
= &_citrus_ctype_wcsnrtombs_fallback
;
102 /* validation check */
103 if (cc
->cc_ops
->co_init
== NULL
||
104 cc
->cc_ops
->co_uninit
== NULL
||
105 cc
->cc_ops
->co_get_mb_cur_max
== NULL
||
106 cc
->cc_ops
->co_mblen
== NULL
||
107 cc
->cc_ops
->co_mbrlen
== NULL
||
108 cc
->cc_ops
->co_mbrtowc
== NULL
||
109 cc
->cc_ops
->co_mbsinit
== NULL
||
110 cc
->cc_ops
->co_mbsrtowcs
== NULL
||
111 cc
->cc_ops
->co_mbsnrtowcs
== NULL
||
112 cc
->cc_ops
->co_mbstowcs
== NULL
||
113 cc
->cc_ops
->co_mbtowc
== NULL
||
114 cc
->cc_ops
->co_wcrtomb
== NULL
||
115 cc
->cc_ops
->co_wcsrtombs
== NULL
||
116 cc
->cc_ops
->co_wcsnrtombs
== NULL
||
117 cc
->cc_ops
->co_wcstombs
== NULL
||
118 cc
->cc_ops
->co_wctomb
== NULL
||
119 cc
->cc_ops
->co_btowc
== NULL
||
120 cc
->cc_ops
->co_wctob
== NULL
) {
125 /* init and get closure */
126 ret
= (*cc
->cc_ops
->co_init
)(
127 &cc
->cc_closure
, variable
, lenvar
, szpriv
);
142 _citrus_ctype_open(_citrus_ctype_t
*rcc
,
143 char const *encname
, void *variable
, size_t lenvar
,
147 _citrus_module_t handle
;
150 _DIAGASSERT(encname
!= NULL
);
151 _DIAGASSERT(!lenvar
|| variable
!=NULL
);
152 _DIAGASSERT(rcc
!= NULL
);
154 if (!strcmp(encname
, _CITRUS_DEFAULT_CTYPE_NAME
)) {
155 *rcc
= &_citrus_ctype_default
;
158 ret
= _citrus_load_module(&handle
, encname
);
162 cc
= calloc(1, sizeof(*cc
));
164 _citrus_unload_module(handle
);
168 ret
= _initctypemodule(cc
, encname
, handle
, variable
, lenvar
, szpriv
);
170 _citrus_unload_module(cc
->cc_module
);
181 _citrus_ctype_close(_citrus_ctype_t cc
)
184 _DIAGASSERT(cc
!= NULL
);
186 if (cc
== &_citrus_ctype_default
)
188 (*cc
->cc_ops
->co_uninit
)(cc
->cc_closure
);
190 _citrus_unload_module(cc
->cc_module
);
199 _citrus_ctype_open(_citrus_ctype_t
*rcc
,
200 char const *encname
, void *variable
, size_t lenvar
,
203 if (!strcmp(encname
, _CITRUS_DEFAULT_CTYPE_NAME
)) {
204 *rcc
= &_citrus_ctype_default
;
212 _citrus_ctype_close(_citrus_ctype_t cc
)