xref: /minix3/external/bsd/bind/dist/contrib/idn/idnkit-1.0-src/lib/checker.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: checker.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 #ifndef lint
4 static char *rcsid = "Id: checker.c,v 1.1 2003/06/04 00:25:49 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/result.h>
57 #include <idn/assert.h>
58 #include <idn/logmacro.h>
59 #include <idn/checker.h>
60 #include <idn/strhash.h>
61 #include <idn/debug.h>
62 
63 /*
64  * Type for checking scheme.
65  */
66 typedef struct {
67 	char *prefix;
68 	char *parameter;
69 	idn_checker_createproc_t create;
70 	idn_checker_destroyproc_t destroy;
71 	idn_checker_lookupproc_t lookup;
72 	void *context;
73 } check_scheme_t;
74 
75 /*
76  * Standard checking schemes.
77  */
78 static const check_scheme_t rfc3491_prohibit_scheme = {
79 	"prohibit#RFC3491",
80 	"RFC3491",
81 	idn_nameprep_createproc,
82 	idn_nameprep_destroyproc,
83 	idn_nameprep_prohibitproc,
84 	NULL,
85 };
86 
87 static const check_scheme_t rfc3491_unasigned_scheme = {
88 	"unassigned#RFC3491",
89 	"RFC3491",
90 	idn_nameprep_createproc,
91 	idn_nameprep_destroyproc,
92 	idn_nameprep_unassignedproc,
93 	NULL,
94 };
95 
96 static const check_scheme_t rfc3491_bidi_scheme = {
97 	"bidi#RFC3491",
98 	"RFC3491",
99 	idn_nameprep_createproc,
100 	idn_nameprep_destroyproc,
101 	idn_nameprep_bidiproc,
102 	NULL,
103 };
104 
105 static const check_scheme_t filecheck_prohibit_scheme = {
106 	"prohibit#fileset",
107 	NULL,
108 	idn__filechecker_createproc,
109 	idn__filechecker_destroyproc,
110 	idn__filechecker_lookupproc,
111 	NULL,
112 };
113 
114 static const check_scheme_t filecheck_unassigned_scheme = {
115 	"unassigned#fileset",
116 	NULL,
117 	idn__filechecker_createproc,
118 	idn__filechecker_destroyproc,
119 	idn__filechecker_lookupproc,
120 	NULL,
121 };
122 
123 static const check_scheme_t *standard_check_schemes[] = {
124 	&rfc3491_unasigned_scheme,
125 	&rfc3491_prohibit_scheme,
126 	&rfc3491_bidi_scheme,
127 	&filecheck_prohibit_scheme,
128 	&filecheck_unassigned_scheme,
129 	NULL,
130 };
131 
132 /*
133  * Hash table for checking schemes.
134  */
135 static idn__strhash_t scheme_hash = NULL;
136 
137 /*
138  * Mapper object type.
139  */
140 struct idn_checker {
141 	int nschemes;
142 	int scheme_size;
143 	check_scheme_t *schemes;
144 	int reference_count;
145 };
146 
147 #define MAPPER_INITIAL_SCHEME_SIZE	1
148 
149 idn_result_t
idn_checker_initialize(void)150 idn_checker_initialize(void) {
151 	idn_result_t r;
152 	check_scheme_t **scheme;
153 
154 	TRACE(("idn_checker_initialize()\n"));
155 
156 	if (scheme_hash != NULL) {
157 		r = idn_success;	/* already initialized */
158 		goto ret;
159 	}
160 
161 	r = idn__strhash_create(&scheme_hash);
162 	if (r != idn_success) {
163 		goto ret;
164 	}
165 
166 	for (scheme = (check_scheme_t **)standard_check_schemes;
167 		*scheme != NULL; scheme++) {
168 		r = idn__strhash_put(scheme_hash, (*scheme)->prefix, *scheme);
169 		if (r != idn_success)
170 			goto ret;
171 	}
172 
173 	r = idn_success;
174 ret:
175 	if (r != idn_success) {
176 		if (scheme_hash != NULL) {
177 			idn__strhash_destroy(scheme_hash, NULL);
178 			scheme_hash = NULL;
179 		}
180 	}
181 	TRACE(("idn_checker_initialize(): %s\n", idn_result_tostring(r)));
182 	return (r);
183 }
184 
185 idn_result_t
idn_checker_create(idn_checker_t * ctxp)186 idn_checker_create(idn_checker_t *ctxp) {
187 	idn_checker_t ctx = NULL;
188 	idn_result_t r;
189 
190 	assert(scheme_hash != NULL);
191 	assert(ctxp != NULL);
192 
193 	TRACE(("idn_checker_create()\n"));
194 
195 	ctx = (idn_checker_t) malloc(sizeof(struct idn_checker));
196 	if (ctx == NULL) {
197 		r = idn_nomemory;
198 		goto ret;
199 	}
200 
201 	ctx->schemes = (check_scheme_t *) malloc(sizeof(check_scheme_t)
202 		 * MAPPER_INITIAL_SCHEME_SIZE);
203 	if (ctx->schemes == NULL) {
204 		r = idn_nomemory;
205 		goto ret;
206 	}
207 
208 	ctx->nschemes = 0;
209 	ctx->scheme_size = MAPPER_INITIAL_SCHEME_SIZE;
210 	ctx->reference_count = 1;
211 	*ctxp = ctx;
212 	r = idn_success;
213 ret:
214 	if (r != idn_success) {
215 		if (ctx != NULL)
216 			free(ctx->schemes);
217 		free(ctx);
218 	}
219 	TRACE(("idn_checker_create(): %s\n", idn_result_tostring(r)));
220 	return (r);
221 }
222 
223 void
idn_checker_destroy(idn_checker_t ctx)224 idn_checker_destroy(idn_checker_t ctx) {
225 	int i;
226 
227 	assert(scheme_hash != NULL);
228 	assert(ctx != NULL);
229 
230 	TRACE(("idn_checker_destroy()\n"));
231 
232 	ctx->reference_count--;
233 	if (ctx->reference_count <= 0) {
234 		TRACE(("idn_checker_destroy(): the object is destroyed\n"));
235 		for (i = 0; i < ctx->nschemes; i++)
236 			ctx->schemes[i].destroy(ctx->schemes[i].context);
237 		free(ctx->schemes);
238 		free(ctx);
239 	} else {
240 		TRACE(("idn_checker_destroy(): "
241 		       "update reference count (%d->%d)\n",
242 		       ctx->reference_count + 1, ctx->reference_count));
243 	}
244 }
245 
246 void
idn_checker_incrref(idn_checker_t ctx)247 idn_checker_incrref(idn_checker_t ctx) {
248 	assert(ctx != NULL && scheme_hash != NULL);
249 
250 	TRACE(("idn_checker_incrref()\n"));
251 	TRACE(("idn_checker_incrref: update reference count (%d->%d)\n",
252 		ctx->reference_count, ctx->reference_count + 1));
253 
254 	ctx->reference_count++;
255 }
256 
257 idn_result_t
idn_checker_add(idn_checker_t ctx,const char * scheme_name)258 idn_checker_add(idn_checker_t ctx, const char *scheme_name) {
259 	idn_result_t r;
260 	check_scheme_t *scheme;
261 	const char *scheme_prefix;
262 	const char *scheme_parameter;
263 	void *scheme_context = NULL;
264 	char *buffer = NULL;
265 
266 	assert(scheme_hash != NULL);
267 	assert(ctx != NULL);
268 
269 	TRACE(("idn_checker_add(scheme_name=%s)\n",
270 		idn__debug_xstring(scheme_name, 50)));
271 
272 	/*
273 	 * Split `scheme_name' into `scheme_prefix' and `scheme_parameter'.
274 	 */
275 	scheme_parameter = strchr(scheme_name, ':');
276 	if (scheme_parameter == NULL) {
277 		scheme_prefix = scheme_name;
278 		scheme_parameter = NULL;
279 	} else {
280 		ptrdiff_t scheme_prefixlen;
281 
282 		scheme_prefixlen = scheme_parameter - scheme_name;
283 		buffer = (char *) malloc(scheme_prefixlen + 1);
284 		if (buffer == NULL) {
285 			r = idn_nomemory;
286 			goto ret;
287 		}
288 		memcpy(buffer, scheme_name, scheme_prefixlen);
289 		*(buffer + scheme_prefixlen) = '\0';
290 		scheme_prefix = buffer;
291 		scheme_parameter++;
292 	}
293 
294 	/*
295 	 * Find a scheme.
296 	 */
297 	if (idn__strhash_get(scheme_hash, scheme_prefix, (void **)&scheme)
298 		!= idn_success) {
299 		ERROR(("idn_checker_add(): invalid scheme \"%-.30s\"\n",
300 		       scheme_name));
301 		r = idn_invalid_name;
302 		goto ret;
303 	}
304 	if (scheme_parameter == NULL && scheme->parameter != NULL)
305 		scheme_parameter = scheme->parameter;
306 
307 	/*
308 	 * Add the scheme.
309 	 */
310 	assert(ctx->nschemes <= ctx->scheme_size);
311 
312 	if (ctx->nschemes == ctx->scheme_size) {
313 		check_scheme_t *new_schemes;
314 
315 		new_schemes = (check_scheme_t *) realloc(ctx->schemes,
316 			sizeof(check_scheme_t) * ctx->scheme_size * 2);
317 		if (new_schemes == NULL) {
318 			r = idn_nomemory;
319 			goto ret;
320 		}
321 		ctx->schemes = new_schemes;
322 		ctx->scheme_size *= 2;
323 	}
324 
325 	r = scheme->create(scheme_parameter, &scheme_context);
326 	if (r != idn_success)
327 		goto ret;
328 
329 	memcpy(ctx->schemes + ctx->nschemes, scheme, sizeof(check_scheme_t));
330 	ctx->schemes[ctx->nschemes].context = scheme_context;
331 	ctx->nschemes++;
332 	r = idn_success;
333 
334 ret:
335 	free(buffer);
336 	if (r != idn_success)
337 		free(scheme_context);
338 	TRACE(("idn_checker_add(): %s\n", idn_result_tostring(r)));
339 	return (r);
340 }
341 
342 idn_result_t
idn_checker_addall(idn_checker_t ctx,const char ** scheme_names,int nschemes)343 idn_checker_addall(idn_checker_t ctx, const char **scheme_names,
344 		   int nschemes) {
345 	idn_result_t r;
346 	int i;
347 
348 	assert(scheme_hash != NULL);
349 	assert(ctx != NULL && scheme_names != NULL);
350 
351 	TRACE(("idn_checker_addall(nschemes=%d)\n", nschemes));
352 
353 	for (i = 0; i < nschemes; i++) {
354 		r = idn_checker_add(ctx, (const char *)*scheme_names);
355 		if (r != idn_success)
356 			goto ret;
357 		scheme_names++;
358 	}
359 
360 	r = idn_success;
361 ret:
362 	TRACE(("idn_checker_addall(): %s\n", idn_result_tostring(r)));
363 	return (r);
364 }
365 
366 idn_result_t
idn_checker_lookup(idn_checker_t ctx,const unsigned long * ucs4,const unsigned long ** found)367 idn_checker_lookup(idn_checker_t ctx, const unsigned long *ucs4,
368 		   const unsigned long **found) {
369 	idn_result_t r;
370 	int i;
371 
372 	assert(scheme_hash != NULL);
373 	assert(ctx != NULL && ucs4 != NULL && found != NULL);
374 
375 	TRACE(("idn_checker_lookup(ucs4=\"%s\")\n",
376 		idn__debug_ucs4xstring(ucs4, 50)));
377 
378 	/*
379 	 * Lookup.
380 	 */
381 	*found = NULL;
382 
383 	for (i = 0; i < ctx->nschemes; i++) {
384 		TRACE(("idn_checker_lookup(): lookup %s\n",
385 		       ctx->schemes[i].prefix));
386 
387 		r = (ctx->schemes[i].lookup)(ctx->schemes[i].context, ucs4,
388 					     found);
389 		if (r != idn_success)
390 			goto ret;
391 		if (*found != NULL)
392 			break;
393 	}
394 
395 	r = idn_success;
396 ret:
397 	if (*found == NULL) {
398 		TRACE(("idn_checker_lookup(): %s (not found)\n",
399 		       idn_result_tostring(r)));
400 	} else {
401 		TRACE(("idn_checker_lookup(): %s (found \\x%04lx)\n",
402 		       idn_result_tostring(r), **found));
403 	}
404 	return (r);
405 }
406 
407 idn_result_t
idn_checker_register(const char * prefix,idn_checker_createproc_t create,idn_checker_destroyproc_t destroy,idn_checker_lookupproc_t lookup)408 idn_checker_register(const char *prefix,
409 		    idn_checker_createproc_t create,
410 		    idn_checker_destroyproc_t destroy,
411 		    idn_checker_lookupproc_t lookup) {
412 	idn_result_t r;
413 	check_scheme_t *scheme = NULL;
414 
415 	assert(scheme_hash != NULL);
416 	assert(prefix != NULL && create != NULL && destroy != NULL &&
417 		lookup != NULL);
418 
419 	TRACE(("idn_checker_register(prefix=%s)\n", prefix));
420 
421 	scheme = (check_scheme_t *) malloc(sizeof(check_scheme_t));
422 	if (scheme == NULL) {
423 		r = idn_nomemory;
424 		goto ret;
425 	}
426 
427 	scheme->prefix = (char *) malloc(strlen(prefix) + 1);
428 	if (scheme->prefix == NULL) {
429 		r = idn_nomemory;
430 		goto ret;
431 	}
432 
433 	strcpy(scheme->prefix, prefix);
434 	scheme->parameter = NULL;
435 	scheme->create    = create;
436 	scheme->destroy   = destroy;
437 	scheme->lookup    = lookup;
438 
439 	r = idn__strhash_put(scheme_hash, prefix, scheme);
440 ret:
441 	if (r != idn_success) {
442 		if (scheme != NULL)
443 			free(scheme->prefix);
444 		free(scheme);
445 	}
446 	TRACE(("idn_checker_register(): %s\n", idn_result_tostring(r)));
447 	return (r);
448 }
449