1 /*	$NetBSD: convert.c,v 1.3 2014/12/10 04:37:56 christos Exp $	*/
2 
3 /*
4  * convert.c - convert domain name
5  */
6 
7 /*
8  * Copyright (c) 2000,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 <windows.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include "wrapcommon.h"
56 
57 /*
58  * prepare/dispose conversion context
59  */
60 
61 void
idnConvDone(idn_resconf_t ctx)62 idnConvDone(idn_resconf_t ctx)
63 {
64 	if (ctx != NULL) {
65 		idnLogReset();
66 		idn_resconf_destroy(ctx);
67 	}
68 }
69 
70 idn_resconf_t
idnConvInit(void)71 idnConvInit(void)
72 {
73 	char encoding[256];
74 	idn_resconf_t ctx;
75 	idn_result_t r;
76 
77 	idnLogReset();
78 
79 	idnLogPrintf(idn_log_level_info, "idnkit version: %-.20s\n",
80 		     idn_version_getstring());
81 
82 	/*
83 	 * Initialize.
84 	 */
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;
89 	}
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;
94 	}
95 	/*
96 	 * load configuration file.
97 	 */
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);
107 		}
108 		idnPrintf("idnConvInit: using default configuration\n");
109 	}
110 	/*
111 	 * Set local codeset.
112 	 */
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;
123 		}
124 	}
125 	return ctx;
126 }
127 
128 /*
129  * idnConvReq - convert domain name in a DNS request
130  *
131  *      convert local encoding to DNS encoding
132  */
133 
134 BOOL
idnConvReq(idn_resconf_t ctx,const char FAR * from,char FAR * to,size_t tolen)135 idnConvReq(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
136 {
137 	idn_result_t r;
138 
139 	idnLogReset();
140 
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;
148 	}
149 
150 	r = idn_res_encodename(ctx, IDN_ENCODE_APP, from, to, tolen);
151 
152 	if (r == idn_success) {
153 		return TRUE;
154 	} else {
155 		return FALSE;
156 	}
157 }
158 
159 /*
160  * idnConvRsp - convert domain name in a DNS response
161  *
162  *      convert DNS encoding to local encoding
163  */
164 
165 BOOL
idnConvRsp(idn_resconf_t ctx,const char FAR * from,char FAR * to,size_t tolen)166 idnConvRsp(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
167 {
168 	idnLogReset();
169 
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;
181 	}
182 }
183