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