etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / api.c
blob5a0b76cb235ca4e0fc3339186462d7c71048d33a
1 /* $NetBSD: api.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
3 #ifndef lint
4 static char *rcsid = "Id: api.c,v 1.1 2003/06/04 00:25:48 marka Exp ";
5 #endif
7 /*
8 * Copyright (c) 2001,2002 Japan Network Information Center.
9 * All rights reserved.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
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.
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.
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.
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."
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.
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.
50 #include <config.h>
52 #include <string.h>
53 #include <stdlib.h>
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>
64 static int initialized;
65 static idn_resconf_t default_conf;
67 static char *conf_file;
69 void
70 idn_enable(int on_off) {
71 idn_res_enable(on_off);
74 idn_result_t
75 idn__setconffile(const char *file) {
76 idn_result_t r;
77 char *s;
79 TRACE(("idn__setconffile(%s)\n", (file == NULL) ? "<null>" : file));
81 if (initialized) {
82 r = idn_failure;
83 goto ret;
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;
94 strcpy(s, file);
96 free(conf_file);
97 conf_file = s;
99 r = idn_success;
100 ret:
101 TRACE(("idn__setconffile(): %s\n", idn_result_tostring(r)));
102 return (r);
105 idn_result_t
106 idn_nameinit(int load_file) {
107 idn_result_t r;
109 TRACE(("idn_nameinit()\n"));
111 if (initialized) {
112 r = idn_success;
113 goto ret;
116 idn_resconf_initialize();
118 r = idn_resconf_create(&default_conf);
119 if (r != idn_success)
120 goto ret;
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;
129 initialized = 1;
131 ret:
132 if (r != idn_success && default_conf != NULL) {
133 idn_resconf_destroy(default_conf);
134 default_conf = NULL;
136 TRACE(("idn_nameinit(): %s\n", idn_result_tostring(r)));
137 return (r);
140 idn_result_t
141 idn_encodename(idn_action_t actions, const char *from, char *to, size_t tolen) {
142 idn_result_t r;
144 assert(from != NULL && to != NULL);
146 TRACE(("idn_encodename(actions=%s, from=\"%s\")\n",
147 idn__res_actionstostring(actions),
148 idn__debug_xstring(from, 50)));
150 if (!initialized && ((r = idn_nameinit(0)) != idn_success))
151 goto ret;
153 r = idn_res_encodename(default_conf, actions, from, to, tolen);
155 ret:
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)));
162 return (r);
165 idn_result_t
166 idn_decodename(idn_action_t actions, const char *from, char *to, size_t tolen) {
167 idn_result_t r;
169 assert(from != NULL && to != NULL);
171 TRACE(("idn_decodename(actions=%s, from=\"%s\", tolen=%d)\n",
172 idn__res_actionstostring(actions),
173 idn__debug_xstring(from, 50), (int)tolen));
175 if (!initialized && ((r = idn_nameinit(0)) != idn_success))
176 goto ret;
178 r = idn_res_decodename(default_conf, actions, from, to, tolen);
180 ret:
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)));
187 return (r);
190 idn_result_t
191 idn_decodename2(idn_action_t actions, const char *from, char *to, size_t tolen,
192 const char *auxencoding) {
193 idn_result_t r;
195 assert(from != NULL && to != NULL);
197 TRACE(("idn_decodename2(actions=%s, from=\"%s\", tolen=%d)\n",
198 idn__res_actionstostring(actions),
199 idn__debug_xstring(from, 50), (int)tolen));
201 if (!initialized && ((r = idn_nameinit(0)) != idn_success))
202 goto ret;
204 r = idn_res_decodename2(default_conf, actions, from, to, tolen,
205 auxencoding);
207 ret:
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)));
214 return (r);
218 * These functions are for backward compatibility.
220 #ifdef ENABLE_MDNKIT_COMPAT
222 idn_result_t
223 mdn_nameinit(void) {
224 return idn_nameinit(1);
227 idn_result_t
228 mdn_encodename(int actions, const char *from, char *to, size_t tolen) {
229 idn_result_t r;
231 assert(from != NULL && to != NULL);
233 TRACE(("mdn_encodename(actions=%s, from=\"%s\")\n",
234 idn__res_actionstostring(actions),
235 idn__debug_xstring(from, 50)));
237 if (!initialized && ((r = idn_nameinit(1)) != idn_success))
238 return (r);
240 return (idn_res_encodename(default_conf, actions, from, to, tolen));
243 idn_result_t
244 mdn_decodename(int actions, const char *from, char *to, size_t tolen) {
245 idn_result_t r;
247 assert(from != NULL && to != NULL);
249 TRACE(("idn_decodename(actions=%s, from=\"%s\", tolen=%d)\n",
250 idn__res_actionstostring(actions),
251 idn__debug_xstring(from, 50), (int)tolen));
253 if (!initialized && ((r = idn_nameinit(1)) != idn_success))
254 return (r);
256 return (idn_res_decodename(default_conf, actions, from, to, tolen));
259 #endif /* ENABLE_MDNKIT_COMPAT */