1/*
2 * convert.c - convert domain name
3 */
4
5/*
6 * Copyright (c) 2000,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 <windows.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "wrapcommon.h"
54
55/*
56 * prepare/dispose conversion context
57 */
58
59void
60idnConvDone(idn_resconf_t ctx)
61{
62	if (ctx != NULL) {
63		idnLogReset();
64		idn_resconf_destroy(ctx);
65	}
66}
67
68idn_resconf_t
69idnConvInit(void)
70{
71	char encoding[256];
72	idn_resconf_t ctx;
73	idn_result_t r;
74
75	idnLogReset();
76
77	idnLogPrintf(idn_log_level_info, "idnkit version: %-.20s\n",
78		     idn_version_getstring());
79
80	/*
81	 * Initialize.
82	 */
83	if ((r = idn_resconf_initialize()) != idn_success) {
84		idnPrintf("idnConvInit: cannot initialize idn library: %s\n",
85			  idn_result_tostring(r));
86		return NULL;
87	}
88	if ((r = idn_resconf_create(&ctx)) != idn_success) {
89		idnPrintf("idnConvInit: cannot create context: %s\n",
90			  idn_result_tostring(r));
91		return NULL;
92	}
93	/*
94	 * load configuration file.
95	 */
96	if ((r = idn_resconf_loadfile(ctx, NULL)) != idn_success) {
97		idnPrintf("idnConvInit: cannot read configuration file: %s\n",
98			  idn_result_tostring(r));
99		if ((r = idn_resconf_setdefaults(ctx)) != idn_success) {
100			idnPrintf("idnConvInit: setting default configuration"
101				  " failed: %s\n",
102				  idn_result_tostring(r));
103			idnConvDone(ctx);
104			return (NULL);
105		}
106		idnPrintf("idnConvInit: using default configuration\n");
107	}
108	/*
109	 * Set local codeset.
110	 */
111	if (idnGetPrgEncoding(encoding, sizeof(encoding)) == TRUE) {
112		idnPrintf("Encoding PRG <%-.100s>\n", encoding);
113		r = idn_resconf_setlocalconvertername(ctx, encoding,
114						      IDN_CONVERTER_RTCHECK);
115		if (r != idn_success) {
116			idnPrintf("idnConvInit: invalid local codeset "
117				  "\"%-.100s\": %s\n",
118				  encoding, idn_result_tostring(r));
119			idnConvDone(ctx);
120			return NULL;
121		}
122	}
123	return ctx;
124}
125
126/*
127 * idnConvReq - convert domain name in a DNS request
128 *
129 *      convert local encoding to DNS encoding
130 */
131
132BOOL
133idnConvReq(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
134{
135	idn_result_t r;
136
137	idnLogReset();
138
139	idnLogPrintf(idn_log_level_trace, "idnConvReq(from=%-.100s)\n", from);
140	if (ctx == NULL) {
141		idnLogPrintf(idn_log_level_trace, "idnConvReq: ctx is NULL\n");
142		if (strlen(from) >= tolen)
143			return FALSE;
144		strcpy(to, from);
145		return TRUE;
146	}
147
148	r = idn_res_encodename(ctx, IDN_ENCODE_APP, from, to, tolen);
149
150	if (r == idn_success) {
151		return TRUE;
152	} else {
153		return FALSE;
154	}
155}
156
157/*
158 * idnConvRsp - convert domain name in a DNS response
159 *
160 *      convert DNS encoding to local encoding
161 */
162
163BOOL
164idnConvRsp(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen)
165{
166	idnLogReset();
167
168	idnLogPrintf(idn_log_level_trace, "idnConvRsp(from=%-.100s)\n", from);
169	if (ctx == NULL) {
170		if (strlen(from) >= tolen)
171			return FALSE;
172		strcpy(to, from);
173		return TRUE;
174	} else if (idn_res_decodename(ctx, IDN_DECODE_APP,
175				      from, to, tolen) == idn_success) {
176		return TRUE;
177	} else {
178		return FALSE;
179	}
180}
181