1 /*	$NetBSD: mapselector.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 #ifndef lint
4 static char *rcsid = "Id: mapselector.c,v 1.1 2003/06/04 00:25:56 marka Exp ";
5 #endif
6 
7 /*
8  * Copyright (c) 2001,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 <config.h>
51 
52 #include <stddef.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 #include <idn/assert.h>
57 #include <idn/logmacro.h>
58 #include <idn/result.h>
59 #include <idn/mapselector.h>
60 #include <idn/strhash.h>
61 #include <idn/debug.h>
62 #include <idn/util.h>
63 #include <idn/ucs4.h>
64 
65 struct idn_mapselector {
66 	idn__strhash_t maphash;
67 	int reference_count;
68 };
69 
70 /*
71  * Maximum length of a top level domain name. (e.g. `com', `jp', ...)
72  */
73 #define MAPSELECTOR_MAX_TLD_LENGTH	63
74 
75 static void string_ascii_tolower(char *string);
76 
77 
78 const unsigned long *
79 idn_mapselector_getnotld(void) {
80 	static const unsigned long notld[] = {0x002d, 0x0000};  /* "-" */
81 	return (notld);
82 }
83 
84 const unsigned long *
85 idn_mapselector_getdefaulttld(void) {
86 	const static unsigned long defaulttld[] = {0x002e, 0x0000};  /* "." */
87 	return (defaulttld);
88 }
89 
90 idn_result_t
91 idn_mapselector_initialize(void) {
92 	idn_result_t r;
93 
94 	TRACE(("idn_mapselector_initialize()\n"));
95 
96 	r = idn_mapper_initialize();
97 
98 	TRACE(("idn_mapselector_initialize(): %s\n", idn_result_tostring(r)));
99 	return (r);
100 }
101 
102 idn_result_t
103 idn_mapselector_create(idn_mapselector_t *ctxp) {
104 	idn_mapselector_t ctx = NULL;
105 	idn_result_t r;
106 
107 	assert(ctxp != NULL);
108 	TRACE(("idn_mapselector_create()\n"));
109 
110 	ctx = (idn_mapselector_t)malloc(sizeof(struct idn_mapselector));
111 	if (ctx == NULL) {
112 		r = idn_nomemory;
113 		goto ret;
114 	}
115 
116 	ctx->maphash = NULL;
117 	ctx->reference_count = 1;
118 
119 	r = idn__strhash_create(&(ctx->maphash));
120 	if (r != idn_success)
121 		goto ret;
122 
123 	*ctxp = ctx;
124 	r = idn_success;
125 
126 ret:
127 	if (r != idn_success) {
128 		if (ctx != NULL)
129 			free(ctx->maphash);
130 		free(ctx);
131 	}
132 	TRACE(("idn_mapselector_create(): %s\n", idn_result_tostring(r)));
133 	return (r);
134 }
135 
136 void
137 idn_mapselector_destroy(idn_mapselector_t ctx) {
138 	assert(ctx != NULL);
139 
140 	TRACE(("idn_mapselector_destroy()\n"));
141 
142 	ctx->reference_count--;
143 	if (ctx->reference_count <= 0) {
144 		TRACE(("idn_mapselector_destroy(): "
145 		       "the object is destroyed\n"));
146 		idn__strhash_destroy(ctx->maphash,
147 			(idn__strhash_freeproc_t)&idn_mapper_destroy);
148 		free(ctx);
149 	} else {
150 		TRACE(("idn_mapselector_destroy(): "
151 		       "update reference count (%d->%d)\n",
152 		       ctx->reference_count + 1, ctx->reference_count));
153 	}
154 }
155 
156 void
157 idn_mapselector_incrref(idn_mapselector_t ctx) {
158 	assert(ctx != NULL);
159 
160 	TRACE(("idn_mapselector_incrref()\n"));
161 	TRACE(("idn_mapselector_incrref: update reference count (%d->%d)\n",
162 		ctx->reference_count, ctx->reference_count + 1));
163 
164 	ctx->reference_count++;
165 }
166 
167 idn_result_t
168 idn_mapselector_add(idn_mapselector_t ctx, const char *tld, const char *name) {
169 	idn_result_t r;
170 	idn_mapper_t mapper;
171 	char hash_key[MAPSELECTOR_MAX_TLD_LENGTH + 1];
172 
173 	assert(ctx != NULL && tld != NULL);
174 
175 	TRACE(("idn_mapselector_add(tld=%s, name=%s)\n", tld, name));
176 
177 	if (!(tld[0] == '.' && tld[1] == '\0')) {
178 		if (tld[0] == '.')
179 			tld++;
180 		if (strchr(tld, '.') != NULL) {
181 			ERROR(("idn_mapselector_add: "
182 			       "invalid TLD \"%-.100s\"\n", tld));
183 			r = idn_invalid_name;
184 			goto ret;
185 		}
186 	}
187 	if (strlen(tld) > MAPSELECTOR_MAX_TLD_LENGTH) {
188 		ERROR(("idn_mapselector_add: "
189 		       "too long TLD \"%-.100s\"\n", tld));
190 		r = idn_invalid_name;
191 		goto ret;
192 	}
193 	strcpy(hash_key, tld);
194 	string_ascii_tolower(hash_key);
195 
196 	if (idn__strhash_get(ctx->maphash, hash_key, (void **)&mapper)
197 		!= idn_success) {
198 		r = idn_mapper_create(&mapper);
199 		if (r != idn_success)
200 			goto ret;
201 
202 		r = idn__strhash_put(ctx->maphash, hash_key, mapper);
203 		if (r != idn_success)
204 			goto ret;
205 	}
206 
207 	r = idn_mapper_add(mapper, name);
208 ret:
209 	TRACE(("idn_mapselector_add(): %s\n", idn_result_tostring(r)));
210 	return (r);
211 }
212 
213 idn_result_t
214 idn_mapselector_addall(idn_mapselector_t ctx, const char *tld,
215 		       const char **scheme_names, int nschemes) {
216 	idn_result_t r;
217 	int i;
218 
219 	assert(ctx != NULL && tld != NULL && scheme_names != NULL);
220 
221 	TRACE(("idn_mapselector_addall(tld=%s, nschemes=%d)\n",
222 	      tld, nschemes));
223 
224 	for (i = 0; i < nschemes; i++) {
225 		r = idn_mapselector_add(ctx, tld, (const char *)*scheme_names);
226 		if (r != idn_success)
227 			goto ret;
228 		scheme_names++;
229 	}
230 
231 	r = idn_success;
232 ret:
233 	TRACE(("idn_mapselector_addall(): %s\n", idn_result_tostring(r)));
234 	return (r);
235 }
236 
237 idn_mapper_t
238 idn_mapselector_mapper(idn_mapselector_t ctx, const char *tld) {
239 	idn_result_t r;
240 	idn_mapper_t mapper;
241 	char hash_key[MAPSELECTOR_MAX_TLD_LENGTH + 1];
242 
243 	assert(ctx != NULL && tld != NULL);
244 
245 	TRACE(("idn_mapselector_mapper(tld=%s)\n", tld));
246 
247 	if (!(tld[0] == '.' && tld[1] == '\0')) {
248 		if (tld[0] == '.')
249 			tld++;
250 		if (strchr(tld, '.') != NULL)
251 			return (NULL);
252 	}
253 	if (strlen(tld) > MAPSELECTOR_MAX_TLD_LENGTH)
254 		return (NULL);
255 	strcpy(hash_key, tld);
256 	string_ascii_tolower(hash_key);
257 
258 	mapper = NULL;
259 	r = idn__strhash_get(ctx->maphash, hash_key, (void **)&mapper);
260 	if (r != idn_success)
261 		return (NULL);
262 
263 	idn_mapper_incrref(mapper);
264 
265 	return (mapper);
266 }
267 
268 idn_result_t
269 idn_mapselector_map(idn_mapselector_t ctx, const unsigned long *from,
270 		    const char *tld, unsigned long *to, size_t tolen) {
271 	idn_result_t r;
272 	idn_mapper_t mapper = NULL;
273 	char hash_key[MAPSELECTOR_MAX_TLD_LENGTH + 1];
274 	size_t fromlen;
275 
276 	assert(ctx != NULL && from != NULL && to != NULL);
277 
278 	TRACE(("idn_mapselector_map(from=\"%s\", tld=\"%s\", tolen=%d)\n",
279 	       idn__debug_ucs4xstring(from, 50), idn__debug_xstring(tld, 50),
280 	       (int)tolen));
281 
282 	if (!(tld[0] == '.' && tld[1] == '\0')) {
283 		if (tld[0] == '.')
284 			tld++;
285 		if (strchr(tld, '.') != NULL) {
286 			r = idn_invalid_name;
287 			goto ret;
288 		}
289 	}
290 	if (strlen(tld) > MAPSELECTOR_MAX_TLD_LENGTH) {
291 		r = idn_invalid_name;
292 		goto ret;
293 	}
294 	strcpy(hash_key, tld);
295 	string_ascii_tolower(hash_key);
296 
297 	fromlen = idn_ucs4_strlen(from);
298 
299 	/*
300 	 * Get mapper for the TLD.
301 	 */
302 	if (idn__strhash_get(ctx->maphash, hash_key, (void **)&mapper)
303 	    != idn_success) {
304 		strcpy(hash_key, IDN_MAPSELECTOR_DEFAULTTLD);
305 		idn__strhash_get(ctx->maphash, hash_key, (void **)&mapper);
306 	}
307 
308 	/*
309 	 * Map.
310 	 * If default mapper has not been registered, copy the string.
311 	 */
312 	if (mapper == NULL) {
313 		TRACE(("idn_mapselector_map(): no mapper\n"));
314 		if (fromlen + 1 > tolen) {
315 			r = idn_buffer_overflow;
316 			goto ret;
317 		}
318 		memcpy(to, from, (fromlen + 1) * sizeof(*from));
319 	} else {
320 		TRACE(("idn_mapselector_map(): tld=%s\n", tld));
321 		r = idn_mapper_map(mapper, from, to, tolen);
322 		if (r != idn_success)
323 			goto ret;
324 	}
325 
326 	r = idn_success;
327 ret:
328 	if (r == idn_success) {
329 		TRACE(("idn_mapselector_map(): succcess (to=\"%s\")\n",
330 		       idn__debug_ucs4xstring(to, 50)));
331 	} else {
332 		TRACE(("idn_mapselector_map(): %s\n", idn_result_tostring(r)));
333 	}
334 	return (r);
335 }
336 
337 idn_result_t
338 idn_mapselector_map2(idn_mapselector_t ctx, const unsigned long *from,
339 		     const unsigned long *tld, unsigned long *to,
340 		     size_t tolen) {
341 	char tld_utf8[MAPSELECTOR_MAX_TLD_LENGTH + 1];
342 	idn_result_t r;
343 
344 	assert(ctx != NULL && from != NULL && to != NULL);
345 
346 	TRACE(("idn_mapselector_map2(from=\"%s\", tld=\"%s\")\n",
347 	       idn__debug_ucs4xstring(from, 50),
348 	       idn__debug_ucs4xstring(tld, 50)));
349 
350 	r = idn_ucs4_ucs4toutf8(tld, tld_utf8, sizeof(tld_utf8));
351 	if (r == idn_buffer_overflow) {
352 		r = idn_invalid_name;
353 		goto ret;
354 	} else if (r != idn_success) {
355 		goto ret;
356 	}
357 
358 	r = idn_mapselector_map(ctx, from, tld_utf8, to, tolen);
359 ret:
360 	if (r == idn_success) {
361 		TRACE(("idn_mapselector_map2(): success (to=\"%s\")\n",
362 		       idn__debug_ucs4xstring(to, 50)));
363 	} else {
364 	    TRACE(("idn_mapselector_map2(): %s\n", idn_result_tostring(r)));
365 	}
366 	return (r);
367 }
368 
369 static void
370 string_ascii_tolower(char *string) {
371 	unsigned char *p;
372 
373 	for (p = (unsigned char *) string; *p != '\0'; p++) {
374 		if ('A' <= *p && *p <= 'Z')
375 			*p = *p - 'A' + 'a';
376 	}
377 }
378