Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / mapselector.c
blob66dc605f0d6518b8e33372f9547c7579826b8f11
1 /* $NetBSD: mapselector.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
3 #ifndef lint
4 static char *rcsid = "Id: mapselector.c,v 1.1 2003/06/04 00:25:56 marka Exp ";
5 #endif
7 /*
8 * Copyright (c) 2001,2002 Japan Network Information Center.
9 * All rights reserved.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
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.
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.
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.
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."
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.
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.
50 #include <config.h>
52 #include <stddef.h>
53 #include <stdlib.h>
54 #include <string.h>
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>
65 struct idn_mapselector {
66 idn__strhash_t maphash;
67 int reference_count;
71 * Maximum length of a top level domain name. (e.g. `com', `jp', ...)
73 #define MAPSELECTOR_MAX_TLD_LENGTH 63
75 static void string_ascii_tolower(char *string);
78 const unsigned long *
79 idn_mapselector_getnotld(void) {
80 static const unsigned long notld[] = {0x002d, 0x0000}; /* "-" */
81 return (notld);
84 const unsigned long *
85 idn_mapselector_getdefaulttld(void) {
86 const static unsigned long defaulttld[] = {0x002e, 0x0000}; /* "." */
87 return (defaulttld);
90 idn_result_t
91 idn_mapselector_initialize(void) {
92 idn_result_t r;
94 TRACE(("idn_mapselector_initialize()\n"));
96 r = idn_mapper_initialize();
98 TRACE(("idn_mapselector_initialize(): %s\n", idn_result_tostring(r)));
99 return (r);
102 idn_result_t
103 idn_mapselector_create(idn_mapselector_t *ctxp) {
104 idn_mapselector_t ctx = NULL;
105 idn_result_t r;
107 assert(ctxp != NULL);
108 TRACE(("idn_mapselector_create()\n"));
110 ctx = (idn_mapselector_t)malloc(sizeof(struct idn_mapselector));
111 if (ctx == NULL) {
112 r = idn_nomemory;
113 goto ret;
116 ctx->maphash = NULL;
117 ctx->reference_count = 1;
119 r = idn__strhash_create(&(ctx->maphash));
120 if (r != idn_success)
121 goto ret;
123 *ctxp = ctx;
124 r = idn_success;
126 ret:
127 if (r != idn_success) {
128 if (ctx != NULL)
129 free(ctx->maphash);
130 free(ctx);
132 TRACE(("idn_mapselector_create(): %s\n", idn_result_tostring(r)));
133 return (r);
136 void
137 idn_mapselector_destroy(idn_mapselector_t ctx) {
138 assert(ctx != NULL);
140 TRACE(("idn_mapselector_destroy()\n"));
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));
156 void
157 idn_mapselector_incrref(idn_mapselector_t ctx) {
158 assert(ctx != NULL);
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));
164 ctx->reference_count++;
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];
173 assert(ctx != NULL && tld != NULL);
175 TRACE(("idn_mapselector_add(tld=%s, name=%s)\n", tld, name));
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;
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;
193 strcpy(hash_key, tld);
194 string_ascii_tolower(hash_key);
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;
202 r = idn__strhash_put(ctx->maphash, hash_key, mapper);
203 if (r != idn_success)
204 goto ret;
207 r = idn_mapper_add(mapper, name);
208 ret:
209 TRACE(("idn_mapselector_add(): %s\n", idn_result_tostring(r)));
210 return (r);
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;
219 assert(ctx != NULL && tld != NULL && scheme_names != NULL);
221 TRACE(("idn_mapselector_addall(tld=%s, nschemes=%d)\n",
222 tld, nschemes));
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++;
231 r = idn_success;
232 ret:
233 TRACE(("idn_mapselector_addall(): %s\n", idn_result_tostring(r)));
234 return (r);
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];
243 assert(ctx != NULL && tld != NULL);
245 TRACE(("idn_mapselector_mapper(tld=%s)\n", tld));
247 if (!(tld[0] == '.' && tld[1] == '\0')) {
248 if (tld[0] == '.')
249 tld++;
250 if (strchr(tld, '.') != NULL)
251 return (NULL);
253 if (strlen(tld) > MAPSELECTOR_MAX_TLD_LENGTH)
254 return (NULL);
255 strcpy(hash_key, tld);
256 string_ascii_tolower(hash_key);
258 mapper = NULL;
259 r = idn__strhash_get(ctx->maphash, hash_key, (void **)&mapper);
260 if (r != idn_success)
261 return (NULL);
263 idn_mapper_incrref(mapper);
265 return (mapper);
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;
276 assert(ctx != NULL && from != NULL && to != NULL);
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));
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;
290 if (strlen(tld) > MAPSELECTOR_MAX_TLD_LENGTH) {
291 r = idn_invalid_name;
292 goto ret;
294 strcpy(hash_key, tld);
295 string_ascii_tolower(hash_key);
297 fromlen = idn_ucs4_strlen(from);
300 * Get mapper for the TLD.
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);
309 * Map.
310 * If default mapper has not been registered, copy the string.
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;
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;
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)));
334 return (r);
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;
344 assert(ctx != NULL && from != NULL && to != NULL);
346 TRACE(("idn_mapselector_map2(from=\"%s\", tld=\"%s\")\n",
347 idn__debug_ucs4xstring(from, 50),
348 idn__debug_ucs4xstring(tld, 50)));
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;
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)));
366 return (r);
369 static void
370 string_ascii_tolower(char *string) {
371 unsigned char *p;
373 for (p = (unsigned char *) string; *p != '\0'; p++) {
374 if ('A' <= *p && *p <= 'Z')
375 *p = *p - 'A' + 'a';