summaryrefslogtreecommitdiffstats
path: root/contrib/idn/idnkit-1.0-src/tools/idnconv/util.c
blob: 35677e922550eaff926cfb526112c694340174d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#ifndef lint
static char *rcsid = "$Id: util.c,v 1.1.1.1 2003/06/04 00:27:08 marka Exp $";
#endif

/*
 * Copyright (c) 2000,2002 Japan Network Information Center.
 * All rights reserved.
 *  
 * By using this file, you agree to the terms and conditions set forth bellow.
 * 
 * 			LICENSE TERMS AND CONDITIONS 
 * 
 * The following License Terms and Conditions apply, unless a different
 * license is obtained from Japan Network Information Center ("JPNIC"),
 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
 * Chiyoda-ku, Tokyo 101-0047, Japan.
 * 
 * 1. Use, Modification and Redistribution (including distribution of any
 *    modified or derived work) in source and/or binary forms is permitted
 *    under this License Terms and Conditions.
 * 
 * 2. Redistribution of source code must retain the copyright notices as they
 *    appear in each source code file, this License Terms and Conditions.
 * 
 * 3. Redistribution in binary form must reproduce the Copyright Notice,
 *    this License Terms and Conditions, in the documentation and/or other
 *    materials provided with the distribution.  For the purposes of binary
 *    distribution the "Copyright Notice" refers to the following language:
 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
 * 
 * 4. The name of JPNIC may not be used to endorse or promote products
 *    derived from this Software without specific prior written approval of
 *    JPNIC.
 * 
 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */

#include <config.h>

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include <idn/resconf.h>
#include <idn/converter.h>
#include <idn/res.h>
#include <idn/utf8.h>

#include "util.h"
#include "selectiveencode.h"

extern int		line_number;

idn_result_t
selective_encode(idn_resconf_t conf, idn_action_t actions,
		 char *from, char *to, int tolen)
{
	for (;;) {
		int len;
		char *region_start, *region_end;
		idn_result_t r;
		char save;

		/*
		 * Find the region that needs conversion.
		 */
		r = idn_selectiveencode_findregion(from, &region_start,
						   &region_end);
		if (r == idn_notfound) {
			/*
			 * Not found.  Just copy the whole thing.
			 */
			if (tolen <= strlen(from))
				return (idn_buffer_overflow);
			(void)strcpy(to, from);
			return (idn_success);
		} else if (r != idn_success) {
			/* This should not happen.. */
			errormsg("internal error at line %d: %s\n",
				 line_number, idn_result_tostring(r));
			return (r);
		}

		/*
		 * We have found a region to convert.
		 * First, copy the prefix part verbatim.
		 */
		len = region_start - from;
		if (tolen < len) {
			errormsg("internal buffer overflow at line %d\n",
				 line_number);
			return (idn_buffer_overflow);
		}
		(void)memcpy(to, from, len);
		to += len;
		tolen -= len;

		/*
		 * Terminate the region with NUL.
		 */
		save = *region_end;
		*region_end = '\0';

		/*
		 * Encode the region.
		 */
		r = idn_res_encodename(conf, actions, region_start, to, tolen);

		/*
		 * Restore character.
		 */
		*region_end = save;

		if (r != idn_success)
			return (r);

		len = strlen(to);
		to += len;
		tolen -= len;

		from = region_end;
	}
}

idn_result_t
selective_decode(idn_resconf_t conf, idn_action_t actions,
		 char *from, char *to, int tolen)
{
	char *domain_name;
	char *ignored_chunk;
	char save;
	int len;
	idn_result_t r;

	/*
	 * While `*from' points to a character in a string which may be
	 * a domain name, `domain_name' refers to the beginning of the
	 * domain name.
	 */
	domain_name = NULL;

	/*
	 * We ignore chunks matching to the regular expression:
	 *    [\-\.][0-9A-Za-z\-\.]*
	 *
	 * While `*from' points to a character in such a chunk,
	 * `ignored_chunk' refers to the beginning of the chunk.
	 */
	ignored_chunk = NULL;

	for (;;) {
		if (*from == '-') {
			/*
			 * We don't recognize `.-' as a part of domain name.
			 */
			if (domain_name != NULL) {
				if (*(from - 1) == '.') {
					ignored_chunk = domain_name;
					domain_name = NULL;
				}
			} else if (ignored_chunk == NULL) {
				ignored_chunk = from;
			}

		} else if (*from == '.') {
			/*
			 * We don't recognize `-.' nor `..' as a part of
			 * domain name.
			 */
			if (domain_name != NULL) {
				if (*(from - 1) == '-' || *(from - 1) == '.') {
					ignored_chunk = domain_name;
					domain_name = NULL;
				}
			} else if (ignored_chunk == NULL) {
				ignored_chunk = from;
			}

		} else if (('a' <= *from && *from <= 'z') ||
			   ('A' <= *from && *from <= 'Z') ||
			   ('0' <= *from && *from <= '9')) {
			if (ignored_chunk == NULL && domain_name == NULL)
				domain_name = from;

		} else {
			if (ignored_chunk != NULL) {
				/*
				 * `from' reaches the end of the ignored chunk.
				 * Copy the chunk to `to'.
				 */
				len = from - ignored_chunk;
				if (tolen < len)
					return (idn_buffer_overflow);
				(void)memcpy(to, ignored_chunk, len);
				to += len;
				tolen -= len;

			} else if (domain_name != NULL) {
				/*
				 * `from' reaches the end of the domain name.
				 * Decode the domain name, and copy the result
				 * to `to'.
				 */
				save = *from;
				*from = '\0';
				r = idn_res_decodename(conf, actions,
						       domain_name, to, tolen);
				*from = save;

				if (r == idn_success) {
					len = strlen(to);
				} else if (r == idn_invalid_encoding) {
					len = from - domain_name;
					if (tolen < len)
						return (idn_buffer_overflow);
					(void)memcpy(to, domain_name, len);
				} else {
					return (r);
				}
				to += len;
				tolen -= len;
			}

			/*
			 * Copy a character `*from' to `to'.
			 */
			if (tolen < 1)
				return (idn_buffer_overflow);
			*to = *from;
			to++;
			tolen--;

			domain_name = NULL;
			ignored_chunk = NULL;

			if (*from == '\0')
				break;
		}

		from++;
	}

	return (idn_success);
}

void
set_defaults(idn_resconf_t conf) {
	idn_result_t r;

	if ((r = idn_resconf_setdefaults(conf)) != idn_success) {
		errormsg("error setting default configuration: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
load_conf_file(idn_resconf_t conf, const char *file) {
	idn_result_t r;

	if ((r = idn_resconf_loadfile(conf, file)) != idn_success) {
		errormsg("error reading configuration file: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_encoding_alias(const char *encoding_alias) {
	idn_result_t r;

	if ((r = idn_converter_resetalias()) != idn_success) {
		errormsg("cannot reset alias information: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}

	if ((r = idn_converter_aliasfile(encoding_alias)) != idn_success) {
		errormsg("cannot read alias file %s: %s\n",
			 encoding_alias, idn_result_tostring(r));
		exit(1);
	}
}

void
set_localcode(idn_resconf_t conf, const char *code) {
	idn_result_t r;

	r = idn_resconf_setlocalconvertername(conf, code,
					      IDN_CONVERTER_RTCHECK);
	if (r != idn_success) {
		errormsg("cannot create converter for codeset %s: %s\n",
			 code, idn_result_tostring(r));
		exit(1);
	}
}

void
set_idncode(idn_resconf_t conf, const char *code) {
	idn_result_t r;

	r = idn_resconf_setidnconvertername(conf, code,
					    IDN_CONVERTER_RTCHECK);
	if (r != idn_success) {
		errormsg("cannot create converter for codeset %s: %s\n",
			 code, idn_result_tostring(r));
		exit(1);
	}
}

void
set_delimitermapper(idn_resconf_t conf, unsigned long *delimiters,
		    int ndelimiters) {
	idn_result_t r;

	r = idn_resconf_addalldelimitermapucs(conf, delimiters, ndelimiters);
	if (r != idn_success) {
		errormsg("cannot add delimiter: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_localmapper(idn_resconf_t conf, char **mappers, int nmappers) {
	idn_result_t r;

	/* Add mapping. */
	r = idn_resconf_addalllocalmapselectornames(conf, 
						    IDN_MAPSELECTOR_DEFAULTTLD,
						    (const char **)mappers,
						    nmappers);
	if (r != idn_success) {
		errormsg("cannot add local map: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_nameprep(idn_resconf_t conf, char *version) {
	idn_result_t r;

	r = idn_resconf_setnameprepversion(conf, version);
	if (r != idn_success) {
		errormsg("error setting nameprep %s: %s\n",
			 version, idn_result_tostring(r));
		exit(1);
	}
}

void
set_mapper(idn_resconf_t conf, char **mappers, int nmappers) {
	idn_result_t r;

	/* Configure mapper. */
	r = idn_resconf_addallmappernames(conf, (const char **)mappers,
					  nmappers);
	if (r != idn_success) {
		errormsg("cannot add nameprep map: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_normalizer(idn_resconf_t conf, char **normalizers, int nnormalizer) {
	idn_result_t r;

	r = idn_resconf_addallnormalizernames(conf,
					      (const char **)normalizers,
					      nnormalizer);
	if (r != idn_success) {
		errormsg("cannot add normalizer: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_prohibit_checkers(idn_resconf_t conf, char **prohibits, int nprohibits) {
	idn_result_t r;

	r = idn_resconf_addallprohibitcheckernames(conf,
						   (const char **)prohibits,
						   nprohibits);
	if (r != idn_success) {
		errormsg("cannot add prohibit checker: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
set_unassigned_checkers(idn_resconf_t conf, char **unassigns, int nunassigns) {
	idn_result_t r;

	r = idn_resconf_addallunassignedcheckernames(conf,
						     (const char **)unassigns,
						     nunassigns);
	if (r != idn_success) {
		errormsg("cannot add unassigned checker: %s\n",
			 idn_result_tostring(r));
		exit(1);
	}
}

void
errormsg(const char *fmt, ...) {
	va_list args;

	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
}


/*
 * Dynamic Stirng Buffer Utility
 */

void
strbuf_init(idnconv_strbuf_t *buf) {
	/*
	 * Initialize the given string buffer.
	 * Caller must allocate the structure (idnconv_strbuf_t)
	 * as an automatic variable or by malloc().
	 */
	buf->str = buf->local_buf;
	buf->str[0] = '\0';
	buf->size = sizeof(buf->local_buf);
}

void
strbuf_reset(idnconv_strbuf_t *buf) {
	/*
	 * Reset the given string buffer.
	 * Free memory allocated by this utility, and 
	 * re-initialize.
	 */
	if (buf->str != NULL && buf->str != buf->local_buf) {
		free(buf->str);
	}
	strbuf_init(buf);
}

char *
strbuf_get(idnconv_strbuf_t *buf) {
	/*
	 * Get the pointer of the buffer.
	 */
	return (buf->str);
}

size_t
strbuf_size(idnconv_strbuf_t *buf) {
	/*
	 * Get the allocated size of the buffer.
	 */
	return (buf->size);
}

char *
strbuf_copy(idnconv_strbuf_t *buf, const char *str) {
	/*
	 * Copy STR to BUF.
	 */
	size_t	len = strlen(str);

	if (strbuf_alloc(buf, len + 1) == NULL)
		return (NULL);
	strcpy(buf->str, str);
	return (buf->str);
}

char *
strbuf_append(idnconv_strbuf_t *buf, const char *str) {
	/*
	 * Append STR to the end of BUF.
	 */
	size_t	len1 = strlen(buf->str);
	size_t	len2 = strlen(str);
	char *p;
#define MARGIN	50

	p = strbuf_alloc(buf, len1 + len2 + 1 + MARGIN);
	if (p != NULL)
		strcpy(buf->str + len1, str);
	return (p);
}

char *
strbuf_alloc(idnconv_strbuf_t *buf, size_t size) {
	/*
	 * Reallocate the buffer of BUF if needed
	 * so that BUF can hold SIZE bytes of data at least.
	 */
	char *p;

	if (buf->size >= size)
		return (buf->str);
	if (buf->str == buf->local_buf) {
		if ((p = malloc(size)) == NULL)
			return (NULL);
		memcpy(p, buf->local_buf, sizeof(buf->local_buf));
	} else {
		if ((p = realloc(buf->str, size)) == NULL)
			return (NULL);
	}
	buf->str = p;
	buf->size = size;
	return (buf->str);
}

char *
strbuf_double(idnconv_strbuf_t *buf) {
	/*
	 * Double the size of the buffer of BUF.
	 */
	return (strbuf_alloc(buf, buf->size * 2));
}

char *
strbuf_getline(idnconv_strbuf_t *buf, FILE *fp) {
	/*
	 * Read a line from FP.
	 */
	char s[256];

	buf->str[0] = '\0';
	while (fgets(s, sizeof(s), fp) != NULL) {
		if (strbuf_append(buf, s) == NULL)
			return (NULL);
		if (strlen(s) < sizeof(s) - 1 || s[sizeof(s) - 2] == '\n')
			return (buf->str);
	}
	if (buf->str[0] != '\0')
		return (buf->str);
	return (NULL);
}