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