19ca40936STijl Coosemans /* $NetBSD: citrus_stdenc.c,v 1.4 2011/11/19 18:39:58 tnozaki Exp $ */
2ad30f8e7SGabor Kovesdan
3ad30f8e7SGabor Kovesdan /*-
4*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause
5*d915a14eSPedro F. Giffuni *
6ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project,
7ad30f8e7SGabor Kovesdan * All rights reserved.
8ad30f8e7SGabor Kovesdan *
9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan * are met:
12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan *
18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan */
30ad30f8e7SGabor Kovesdan
31ad30f8e7SGabor Kovesdan
32ad30f8e7SGabor Kovesdan #include <assert.h>
33ad30f8e7SGabor Kovesdan #include <errno.h>
34ad30f8e7SGabor Kovesdan #include <stdlib.h>
35ad30f8e7SGabor Kovesdan #include <string.h>
36ad30f8e7SGabor Kovesdan
37ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
38ad30f8e7SGabor Kovesdan #include "citrus_types.h"
39ad30f8e7SGabor Kovesdan #include "citrus_module.h"
40ad30f8e7SGabor Kovesdan #include "citrus_none.h"
41ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
42ad30f8e7SGabor Kovesdan
43ad30f8e7SGabor Kovesdan struct _citrus_stdenc _citrus_stdenc_default = {
44ad30f8e7SGabor Kovesdan &_citrus_NONE_stdenc_ops, /* ce_ops */
45ad30f8e7SGabor Kovesdan NULL, /* ce_closure */
46ad30f8e7SGabor Kovesdan NULL, /* ce_module */
47ad30f8e7SGabor Kovesdan &_citrus_NONE_stdenc_traits, /* ce_traits */
48ad30f8e7SGabor Kovesdan };
49ad30f8e7SGabor Kovesdan
50ad30f8e7SGabor Kovesdan int
_citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,char const * __restrict encname,const void * __restrict variable,size_t lenvar)51ad30f8e7SGabor Kovesdan _citrus_stdenc_open(struct _citrus_stdenc * __restrict * __restrict rce,
52ad30f8e7SGabor Kovesdan char const * __restrict encname, const void * __restrict variable,
53ad30f8e7SGabor Kovesdan size_t lenvar)
54ad30f8e7SGabor Kovesdan {
55ad30f8e7SGabor Kovesdan struct _citrus_stdenc *ce;
56ad30f8e7SGabor Kovesdan _citrus_module_t handle;
57ad30f8e7SGabor Kovesdan _citrus_stdenc_getops_t getops;
58ad30f8e7SGabor Kovesdan int ret;
59ad30f8e7SGabor Kovesdan
60ad30f8e7SGabor Kovesdan if (!strcmp(encname, _CITRUS_DEFAULT_STDENC_NAME)) {
61ad30f8e7SGabor Kovesdan *rce = &_citrus_stdenc_default;
62ad30f8e7SGabor Kovesdan return (0);
63ad30f8e7SGabor Kovesdan }
64ad30f8e7SGabor Kovesdan ce = malloc(sizeof(*ce));
65ad30f8e7SGabor Kovesdan if (ce == NULL) {
66ad30f8e7SGabor Kovesdan ret = errno;
67ad30f8e7SGabor Kovesdan goto bad;
68ad30f8e7SGabor Kovesdan }
69ad30f8e7SGabor Kovesdan ce->ce_ops = NULL;
70ad30f8e7SGabor Kovesdan ce->ce_closure = NULL;
71ad30f8e7SGabor Kovesdan ce->ce_module = NULL;
72ad30f8e7SGabor Kovesdan ce->ce_traits = NULL;
73ad30f8e7SGabor Kovesdan
74ad30f8e7SGabor Kovesdan ret = _citrus_load_module(&handle, encname);
75ad30f8e7SGabor Kovesdan if (ret)
76ad30f8e7SGabor Kovesdan goto bad;
77ad30f8e7SGabor Kovesdan
78ad30f8e7SGabor Kovesdan ce->ce_module = handle;
79ad30f8e7SGabor Kovesdan
80ad30f8e7SGabor Kovesdan getops = (_citrus_stdenc_getops_t)_citrus_find_getops(ce->ce_module,
81ad30f8e7SGabor Kovesdan encname, "stdenc");
82ad30f8e7SGabor Kovesdan if (getops == NULL) {
83ad30f8e7SGabor Kovesdan ret = EINVAL;
84ad30f8e7SGabor Kovesdan goto bad;
85ad30f8e7SGabor Kovesdan }
86ad30f8e7SGabor Kovesdan
87ad30f8e7SGabor Kovesdan ce->ce_ops = (struct _citrus_stdenc_ops *)malloc(sizeof(*ce->ce_ops));
88ad30f8e7SGabor Kovesdan if (ce->ce_ops == NULL) {
89ad30f8e7SGabor Kovesdan ret = errno;
90ad30f8e7SGabor Kovesdan goto bad;
91ad30f8e7SGabor Kovesdan }
92ad30f8e7SGabor Kovesdan
93ad30f8e7SGabor Kovesdan ret = (*getops)(ce->ce_ops, sizeof(*ce->ce_ops));
94ad30f8e7SGabor Kovesdan if (ret)
95ad30f8e7SGabor Kovesdan goto bad;
96ad30f8e7SGabor Kovesdan
97ad30f8e7SGabor Kovesdan /* validation check */
98ad30f8e7SGabor Kovesdan if (ce->ce_ops->eo_init == NULL ||
99ad30f8e7SGabor Kovesdan ce->ce_ops->eo_uninit == NULL ||
100ad30f8e7SGabor Kovesdan ce->ce_ops->eo_init_state == NULL ||
101ad30f8e7SGabor Kovesdan ce->ce_ops->eo_mbtocs == NULL ||
102ad30f8e7SGabor Kovesdan ce->ce_ops->eo_cstomb == NULL ||
103ad30f8e7SGabor Kovesdan ce->ce_ops->eo_mbtowc == NULL ||
104ad30f8e7SGabor Kovesdan ce->ce_ops->eo_wctomb == NULL ||
1059ca40936STijl Coosemans ce->ce_ops->eo_get_state_desc == NULL) {
1069ca40936STijl Coosemans ret = EINVAL;
107ad30f8e7SGabor Kovesdan goto bad;
1089ca40936STijl Coosemans }
109ad30f8e7SGabor Kovesdan
110ad30f8e7SGabor Kovesdan /* allocate traits */
111ad30f8e7SGabor Kovesdan ce->ce_traits = malloc(sizeof(*ce->ce_traits));
112ad30f8e7SGabor Kovesdan if (ce->ce_traits == NULL) {
113ad30f8e7SGabor Kovesdan ret = errno;
114ad30f8e7SGabor Kovesdan goto bad;
115ad30f8e7SGabor Kovesdan }
116ad30f8e7SGabor Kovesdan /* init and get closure */
117ad30f8e7SGabor Kovesdan ret = (*ce->ce_ops->eo_init)(ce, variable, lenvar, ce->ce_traits);
118ad30f8e7SGabor Kovesdan if (ret)
119ad30f8e7SGabor Kovesdan goto bad;
120ad30f8e7SGabor Kovesdan
121ad30f8e7SGabor Kovesdan *rce = ce;
122ad30f8e7SGabor Kovesdan
123ad30f8e7SGabor Kovesdan return (0);
124ad30f8e7SGabor Kovesdan
125ad30f8e7SGabor Kovesdan bad:
126ad30f8e7SGabor Kovesdan _citrus_stdenc_close(ce);
127ad30f8e7SGabor Kovesdan return (ret);
128ad30f8e7SGabor Kovesdan }
129ad30f8e7SGabor Kovesdan
130ad30f8e7SGabor Kovesdan void
_citrus_stdenc_close(struct _citrus_stdenc * ce)131ad30f8e7SGabor Kovesdan _citrus_stdenc_close(struct _citrus_stdenc *ce)
132ad30f8e7SGabor Kovesdan {
133ad30f8e7SGabor Kovesdan
134ad30f8e7SGabor Kovesdan if (ce == &_citrus_stdenc_default)
135ad30f8e7SGabor Kovesdan return;
136ad30f8e7SGabor Kovesdan
137ad30f8e7SGabor Kovesdan if (ce->ce_module) {
138ad30f8e7SGabor Kovesdan if (ce->ce_ops) {
139ad30f8e7SGabor Kovesdan if (ce->ce_closure && ce->ce_ops->eo_uninit)
140ad30f8e7SGabor Kovesdan (*ce->ce_ops->eo_uninit)(ce);
141ad30f8e7SGabor Kovesdan free(ce->ce_ops);
142ad30f8e7SGabor Kovesdan }
143ad30f8e7SGabor Kovesdan free(ce->ce_traits);
144ad30f8e7SGabor Kovesdan _citrus_unload_module(ce->ce_module);
145ad30f8e7SGabor Kovesdan }
146ad30f8e7SGabor Kovesdan free(ce);
147ad30f8e7SGabor Kovesdan }
148