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