1/*	$NetBSD: api.c,v 1.2.6.1 2012/06/06 18:18:07 bouyer Exp $	*/
2
3#ifndef lint
4static char *rcsid = "Id: api.c,v 1.1 2003/06/04 00:25:48 marka Exp ";
5#endif
6
7/*
8 * Copyright (c) 2001,2002 Japan Network Information Center.
9 * All rights reserved.
10 *
11 * By using this file, you agree to the terms and conditions set forth bellow.
12 *
13 * 			LICENSE TERMS AND CONDITIONS
14 *
15 * The following License Terms and Conditions apply, unless a different
16 * license is obtained from Japan Network Information Center ("JPNIC"),
17 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18 * Chiyoda-ku, Tokyo 101-0047, Japan.
19 *
20 * 1. Use, Modification and Redistribution (including distribution of any
21 *    modified or derived work) in source and/or binary forms is permitted
22 *    under this License Terms and Conditions.
23 *
24 * 2. Redistribution of source code must retain the copyright notices as they
25 *    appear in each source code file, this License Terms and Conditions.
26 *
27 * 3. Redistribution in binary form must reproduce the Copyright Notice,
28 *    this License Terms and Conditions, in the documentation and/or other
29 *    materials provided with the distribution.  For the purposes of binary
30 *    distribution the "Copyright Notice" refers to the following language:
31 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
32 *
33 * 4. The name of JPNIC may not be used to endorse or promote products
34 *    derived from this Software without specific prior written approval of
35 *    JPNIC.
36 *
37 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
41 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
48 */
49
50#include <config.h>
51
52#include <string.h>
53#include <stdlib.h>
54
55#include <idn/result.h>
56#include <idn/assert.h>
57#include <idn/log.h>
58#include <idn/logmacro.h>
59#include <idn/resconf.h>
60#include <idn/api.h>
61#include <idn/debug.h>
62#include <idn/res.h>
63
64static int initialized;
65static idn_resconf_t default_conf;
66
67static char *conf_file;
68
69void
70idn_enable(int on_off) {
71	idn_res_enable(on_off);
72}
73
74idn_result_t
75idn__setconffile(const char *file) {
76	idn_result_t r;
77	char *s;
78
79	TRACE(("idn__setconffile(%s)\n", (file == NULL) ? "<null>" : file));
80
81	if (initialized) {
82		r = idn_failure;
83		goto ret;
84	}
85
86	if (file == NULL)
87		s = NULL;
88	else {
89		s = (char *)malloc(strlen(file) + 1);
90		if (s == NULL) {
91			r = idn_nomemory;
92			goto ret;
93		}
94		strcpy(s, file);
95	}
96	free(conf_file);
97	conf_file = s;
98
99	r = idn_success;
100ret:
101	TRACE(("idn__setconffile(): %s\n", idn_result_tostring(r)));
102	return (r);
103}
104
105idn_result_t
106idn_nameinit(int load_file) {
107	idn_result_t r;
108
109	TRACE(("idn_nameinit()\n"));
110
111	if (initialized) {
112		r = idn_success;
113		goto ret;
114	}
115
116	idn_resconf_initialize();
117
118	r = idn_resconf_create(&default_conf);
119	if (r != idn_success)
120		goto ret;
121
122	if (load_file)
123		r = idn_resconf_loadfile(default_conf, conf_file);
124	else
125		r = idn_resconf_setdefaults(default_conf);
126	if (r != idn_success)
127		goto ret;
128
129	initialized = 1;
130
131ret:
132	if (r != idn_success && default_conf != NULL) {
133		idn_resconf_destroy(default_conf);
134		default_conf = NULL;
135	}
136	TRACE(("idn_nameinit(): %s\n", idn_result_tostring(r)));
137	return (r);
138}
139
140idn_result_t
141idn_encodename(idn_action_t actions, const char *from, char *to, size_t tolen) {
142	idn_result_t r;
143
144	assert(from != NULL && to != NULL);
145
146	TRACE(("idn_encodename(actions=%s, from=\"%s\")\n",
147	       idn__res_actionstostring(actions),
148	       idn__debug_xstring(from, 50)));
149
150	if (!initialized && ((r = idn_nameinit(0)) != idn_success))
151		goto ret;
152
153	r = idn_res_encodename(default_conf, actions, from, to, tolen);
154
155ret:
156	if (r == idn_success) {
157		TRACE(("idn_encodename(): success (to=\"%s\")\n",
158		       idn__debug_xstring(to, 50)));
159	} else {
160		TRACE(("idn_encodename(): %s\n", idn_result_tostring(r)));
161	}
162	return (r);
163}
164
165idn_result_t
166idn_decodename(idn_action_t actions, const char *from, char *to, size_t tolen) {
167	idn_result_t r;
168
169	assert(from != NULL && to != NULL);
170
171	TRACE(("idn_decodename(actions=%s, from=\"%s\", tolen=%d)\n",
172	       idn__res_actionstostring(actions),
173	       idn__debug_xstring(from, 50), (int)tolen));
174
175	if (!initialized && ((r = idn_nameinit(0)) != idn_success))
176		goto ret;
177
178	r = idn_res_decodename(default_conf, actions, from, to, tolen);
179
180ret:
181	if (r == idn_success) {
182		TRACE(("idn_decodename(): success (to=\"%s\")\n",
183		       idn__debug_xstring(to, 50)));
184	} else {
185		TRACE(("idn_decodename(): %s\n", idn_result_tostring(r)));
186	}
187	return (r);
188}
189
190idn_result_t
191idn_decodename2(idn_action_t actions, const char *from, char *to, size_t tolen,
192		const char *auxencoding) {
193	idn_result_t r;
194
195	assert(from != NULL && to != NULL);
196
197	TRACE(("idn_decodename2(actions=%s, from=\"%s\", tolen=%d)\n",
198	       idn__res_actionstostring(actions),
199	       idn__debug_xstring(from, 50), (int)tolen));
200
201	if (!initialized && ((r = idn_nameinit(0)) != idn_success))
202		goto ret;
203
204	r = idn_res_decodename2(default_conf, actions, from, to, tolen,
205				auxencoding);
206
207ret:
208	if (r == idn_success) {
209		TRACE(("idn_decodename2(): success (to=\"%s\")\n",
210		       idn__debug_xstring(to, 50)));
211	} else {
212		TRACE(("idn_decodename2(): %s\n", idn_result_tostring(r)));
213	}
214	return (r);
215}
216
217/*
218 * These functions are for backward compatibility.
219 */
220#ifdef ENABLE_MDNKIT_COMPAT
221
222idn_result_t
223mdn_nameinit(void) {
224	return idn_nameinit(1);
225}
226
227idn_result_t
228mdn_encodename(int actions, const char *from, char *to, size_t tolen) {
229	idn_result_t r;
230
231	assert(from != NULL && to != NULL);
232
233	TRACE(("mdn_encodename(actions=%s, from=\"%s\")\n",
234	       idn__res_actionstostring(actions),
235	       idn__debug_xstring(from, 50)));
236
237	if (!initialized && ((r = idn_nameinit(1)) != idn_success))
238		return (r);
239
240	return (idn_res_encodename(default_conf, actions, from, to, tolen));
241}
242
243idn_result_t
244mdn_decodename(int actions, const char *from, char *to, size_t tolen) {
245	idn_result_t r;
246
247	assert(from != NULL && to != NULL);
248
249	TRACE(("idn_decodename(actions=%s, from=\"%s\", tolen=%d)\n",
250	       idn__res_actionstostring(actions),
251	       idn__debug_xstring(from, 50), (int)tolen));
252
253	if (!initialized && ((r = idn_nameinit(1)) != idn_success))
254		return (r);
255
256	return (idn_res_decodename(default_conf, actions, from, to, tolen));
257}
258
259#endif /* ENABLE_MDNKIT_COMPAT */
260