summaryrefslogtreecommitdiffstats
path: root/contrib/idn/idnkit-1.0-src/lib/aliaslist.c
blob: 2b5dca08d31a43ca1cf3d42614793edf5ac87e5f (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
#ifndef lint
static char *rcsid = "$Id: aliaslist.c,v 1.1.1.1 2003/06/04 00:25:47 marka Exp $";
#endif

/*
 * Copyright (c) 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 <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <idn/aliaslist.h>
#include <idn/assert.h>
#include <idn/logmacro.h>
#include <idn/result.h>

struct aliasitem {
	char *pattern;			/* name pattern */
	char *encoding;			/* MIME-preferred charset name */
	struct aliasitem *next;
};
typedef struct aliasitem *aliasitem_t;

struct idn__aliaslist {
	aliasitem_t first_item;		/* first item of the list */
};

static idn_result_t
additem_to_top(idn__aliaslist_t list,
	       const char *pattern, const char *encoding);

static idn_result_t
additem_to_bottom(idn__aliaslist_t list,
		  const char *pattern, const char *encoding);

static int		match(const char *pattern, const char *str);

static idn_result_t	create_item(const char *pattern, const char *encoding,
				    aliasitem_t *itemp);

#ifdef DEBUG
static void		dump_list(idn__aliaslist_t list);
#endif

idn_result_t
idn__aliaslist_create(idn__aliaslist_t *listp) {
	static int size = sizeof(struct idn__aliaslist);

	TRACE(("idn__aliaslist_create()\n"));

	assert(listp != NULL);

	if ((*listp = malloc(size)) == NULL) {
		return (idn_nomemory);
	}
	(*listp)->first_item = NULL;

	return (idn_success);
}

void
idn__aliaslist_destroy(idn__aliaslist_t list) {
	aliasitem_t current;
	aliasitem_t next;

	TRACE(("idn__aliaslist_destroy()\n"));

	assert(list != NULL);

	current = list->first_item;
	while (current != NULL) {
		if (current->pattern != NULL) {
			free(current->pattern);
		}
		if (current->encoding != NULL) {
			free(current->encoding);
		}
		next = current->next;
		free(current);
		current = next;
	}
	free(list);
}

idn_result_t
idn__aliaslist_aliasfile(idn__aliaslist_t list, const char *path) {
	FILE *fp;
	int line_no;
	idn_result_t r = idn_success;
	char line[200], alias[200], real[200];

	assert(path != NULL);

	TRACE(("idn__aliaslist_aliasfile(path=%s)\n", path));

	if ((fp = fopen(path, "r")) == NULL) {
		return (idn_nofile);
	}
	for (line_no = 1; fgets(line, sizeof(line), fp) != NULL; line_no++) {
		unsigned char *p = (unsigned char *)line;

		while (isascii(*p) && isspace(*p))
			p++;
		if (*p == '#' || *p == '\n')
			continue;
		if (sscanf((char *)p, "%s %s", alias, real) == 2) {
			r = additem_to_bottom(list, alias, real);
			if (r != idn_success)
				break;
		} else {
			INFO(("idn__aliaslist_aliasfile: file %s has "
			      "invalid contents at line %d\n",
			      path, line_no));
			r = idn_invalid_syntax;
			break;
		}
	}
	fclose(fp);

#ifdef DEBUG
	dump_list(list);
#endif

	return (r);
}

idn_result_t
idn__aliaslist_additem(idn__aliaslist_t list,
		       const char *pattern, const char *encoding,
		       int first_item) {
	if (first_item) {
		return additem_to_top(list, pattern, encoding);
	} else {
		return additem_to_bottom(list, pattern, encoding);
	}
}

static idn_result_t
additem_to_top(idn__aliaslist_t list,
	       const char *pattern, const char *encoding) {
	aliasitem_t new_item;
	idn_result_t r;

	TRACE(("additem_to_top()\n"));

	assert(list != NULL);
	assert(pattern != NULL);
	assert(encoding != NULL);

	if ((r = create_item(pattern, encoding, &new_item))
	    != idn_success) {
		WARNING(("additem_to_top: malloc failed\n"));
		return (r);
	}

	new_item->next = list->first_item;
	list->first_item = new_item;

#ifdef DEBUG
	dump_list(list);
#endif

	return (idn_success);
}

static idn_result_t
additem_to_bottom(idn__aliaslist_t list,
		  const char *pattern, const char *encoding) {
	aliasitem_t new_item;
	idn_result_t r;

	TRACE(("additem_to_bottom()\n"));

	assert(list != NULL);
	assert(pattern != NULL);
	assert(encoding != NULL);

	r = create_item(pattern, encoding, &new_item);
	if (r != idn_success) {
		WARNING(("additem_to_bottom: malloc failed\n"));
		return r;
	}

	if (list->first_item == NULL) {
		list->first_item = new_item;
	} else {
		aliasitem_t cur_item = list->first_item;
		for (;;) {
			if (cur_item->next == NULL) {
				break;
			}
			cur_item = cur_item->next;
		}
		cur_item->next = new_item;
	}

	return (idn_success);
}

idn_result_t
idn__aliaslist_find(idn__aliaslist_t list,
		   const char *pattern, char **encodingp) {
	aliasitem_t current;

	TRACE(("idn__aliaslist_find()\n"));

	assert(list != NULL);
	assert(pattern != NULL);

#ifdef DEBUG
	DUMP(("target pattern: %s\n", pattern));
#endif
	current = list->first_item;
	while (current != NULL) {
#ifdef DEBUG
		DUMP(("current pattern: %s, encoding: %s\n",
		      current->pattern, current->encoding));
#endif
		if (match(current->pattern, pattern)) {
			*encodingp = current->encoding;
			return (idn_success);
		}
		current = current->next;
	}

	TRACE(("idn__aliaslist_find(): not found\n"));
	*encodingp = (char *)pattern;
	return (idn_notfound);
}

/*
 * Wild card matching function that supports only '*'.
 */
static int
match(const char *pattern, const char *str) {
	for (;;) {
		int c;

		switch (c = *pattern++) {
		case '\0':
			return (*str == '\0');
		case '*':
			while (!match(pattern, str)) {
				if (*str == '\0')
					return (0);
				str++;
			}
			return (1);
			break;
		default:
			if (*str++ != c)
				return (0);
			break;
		}
	}
}

/*
 * List item creation.
 * pattern and encoding must not be NULL.
 */
static idn_result_t
create_item(const char *pattern, const char *encoding,
	    aliasitem_t *itemp) {
	static size_t size = sizeof(struct aliasitem);

	assert(pattern != NULL);
	assert(encoding != NULL);

	if ((*itemp = malloc(size)) == NULL)
		return (idn_nomemory);

	if (((*itemp)->pattern = malloc(strlen(pattern) + 1)) == NULL) {
		free(*itemp);
		*itemp = NULL;
		return (idn_nomemory);
	}

	if (((*itemp)->encoding = malloc(strlen(encoding) + 1)) == NULL) {
		free((*itemp)->pattern);
		free(*itemp);
		*itemp = NULL;
		return (idn_nomemory);
	}

	(void)strcpy((*itemp)->pattern, pattern);
	(void)strcpy((*itemp)->encoding, encoding);
	(*itemp)->next = NULL;

	return (idn_success);
}

#ifdef DEBUG
static void
dump_list(idn__aliaslist_t list) {
	aliasitem_t item;
	int i;

	TRACE(("dump_list()\n"));
	if (list == NULL) {
		TRACE(("list is NULL\n"));
		return;
	}
	item = list->first_item;
	i = 0;
	while (item != NULL) {
		DUMP(("%d: %s\t%s\n", i, item->pattern, item->encoding));
		item = item->next;
		i++;
	}
}
#endif