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