Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / wsock / common / convert.c
blob9a1965a703bf36a471859d187f17c0432dcb9144
1 /* $NetBSD$ */
3 /*
4 * convert.c - convert domain name
5 */
7 /*
8 * Copyright (c) 2000,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 <windows.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #include "wrapcommon.h"
58 * prepare/dispose conversion context
61 void
62 idnConvDone(idn_resconf_t ctx)
64 if (ctx != NULL) {
65 idnLogReset();
66 idn_resconf_destroy(ctx);
70 idn_resconf_t
71 idnConvInit(void)
73 char encoding[256];
74 idn_resconf_t ctx;
75 idn_result_t r;
77 idnLogReset();
79 idnLogPrintf(idn_log_level_info, "idnkit version: %-.20s\n",
80 idn_version_getstring());
83 * Initialize.
85 if ((r = idn_resconf_initialize()) != idn_success) {
86 idnPrintf("idnConvInit: cannot initialize idn library: %s\n",
87 idn_result_tostring(r));
88 return NULL;
90 if ((r = idn_resconf_create(&ctx)) != idn_success) {
91 idnPrintf("idnConvInit: cannot create context: %s\n",
92 idn_result_tostring(r));
93 return NULL;
96 * load configuration file.
98 if ((r = idn_resconf_loadfile(ctx, NULL)) != idn_success) {
99 idnPrintf("idnConvInit: cannot read configuration file: %s\n",
100 idn_result_tostring(r));
101 if ((r = idn_resconf_setdefaults(ctx)) != idn_success) {
102 idnPrintf("idnConvInit: setting default configuration"
103 " failed: %s\n",
104 idn_result_tostring(r));
105 idnConvDone(ctx);
106 return (NULL);
108 idnPrintf("idnConvInit: using default configuration\n");
111 * Set local codeset.
113 if (idnGetPrgEncoding(encoding, sizeof(encoding)) == TRUE) {
114 idnPrintf("Encoding PRG <%-.100s>\n", encoding);
115 r = idn_resconf_setlocalconvertername(ctx, encoding,
116 IDN_CONVERTER_RTCHECK);
117 if (r != idn_success) {
118 idnPrintf("idnConvInit: invalid local codeset "
119 "\"%-.100s\": %s\n",
120 encoding, idn_result_tostring(r));
121 idnConvDone(ctx);
122 return NULL;
125 return ctx;
129 * idnConvReq - convert domain name in a DNS request
131 * convert local encoding to DNS encoding
134 BOOL
135 idnConvReq(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
137 idn_result_t r;
139 idnLogReset();
141 idnLogPrintf(idn_log_level_trace, "idnConvReq(from=%-.100s)\n", from);
142 if (ctx == NULL) {
143 idnLogPrintf(idn_log_level_trace, "idnConvReq: ctx is NULL\n");
144 if (strlen(from) >= tolen)
145 return FALSE;
146 strcpy(to, from);
147 return TRUE;
150 r = idn_res_encodename(ctx, IDN_ENCODE_APP, from, to, tolen);
152 if (r == idn_success) {
153 return TRUE;
154 } else {
155 return FALSE;
160 * idnConvRsp - convert domain name in a DNS response
162 * convert DNS encoding to local encoding
165 BOOL
166 idnConvRsp(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
168 idnLogReset();
170 idnLogPrintf(idn_log_level_trace, "idnConvRsp(from=%-.100s)\n", from);
171 if (ctx == NULL) {
172 if (strlen(from) >= tolen)
173 return FALSE;
174 strcpy(to, from);
175 return TRUE;
176 } else if (idn_res_decodename(ctx, IDN_DECODE_APP,
177 from, to, tolen) == idn_success) {
178 return TRUE;
179 } else {
180 return FALSE;