summaryrefslogtreecommitdiff
path: root/src/aliases.c
blob: 129268542884ae177d8a6358bf4bffb73f91956f (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
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "internal.h"

struct alias {
	struct alias *next;
	struct canon *canon;
	unsigned short name_len;
	char name[1];
};

#define HASH_SIZE (43)
static struct canon *canon_tab[HASH_SIZE];
static struct alias *alias_tab[HASH_SIZE];

static bool create_alias(const char *alias, struct canon *c);
static struct canon *create_canon(const char *canon, short mibenum);
static int hash_val(const char *alias);

#ifdef TEST
static void dump_alias_data(void);

int main (void)
{
	struct canon *c;

	create_alias_data("Unicode:Files.Aliases");

	dump_alias_data();

	c = alias_canonicalise("moose");
	if (c)
		printf("!!!\n");

	c = alias_canonicalise("csinvariant");
	if (c)
		printf("%s %d\n", c->name, c->mib_enum);

	c = alias_canonicalise("nats-sefi-add");
	if (c)
		printf("%s %d\n", c->name, c->mib_enum);

	printf("%d\n", mibenum_from_name(c->name));

	printf("%s\n", mibenum_to_name(c->mib_enum));

	free_alias_data();

	return 0;
}
#endif

/**
 * Create an alias
 *
 * \param alias The alias name
 * \param c The canonical form
 * \return true on success, false otherwise
 */
bool create_alias(const char *alias, struct canon *c)
{
	struct alias *a;
	int hash;

	if (!alias || !c)
		return false;

	a = malloc(sizeof(struct alias) + strlen(alias) + 1);
	if (!a)
		return false;

	a->canon = c;
	a->name_len = strlen(alias);
	strcpy(a->name, alias);
	a->name[a->name_len] = '\0';

	hash = hash_val(alias);

	a->next = alias_tab[hash];
	alias_tab[hash] = a;

	return true;
}

/**
 * Create a canonical form
 *
 * \param canon The canonical name
 * \param mibenum The MIB enum value
 * \return Pointer to struct canon or NULL on error
 */
struct canon *create_canon(const char *canon, short mibenum)
{
	struct canon *c;
	int hash, len;

	if (!canon)
		return NULL;

	len = strlen(canon);

	c = malloc(sizeof(struct canon) + len + 1);
	if (!c)
		return NULL;

	c->mib_enum = mibenum;
	c->name_len = len;
	strcpy(c->name, canon);
	c->name[len] = '\0';

	hash = hash_val(canon);

	c->next = canon_tab[hash];
	canon_tab[hash] = c;

	return c;
}

/**
 * Hash function
 *
 * \param alias String to hash
 * \return The hashed value
 */
int hash_val(const char *alias)
{
	const char *s = alias;
	unsigned int h = 5381;

	if (!alias)
		return 0;

	while (*s)
		h = (h * 33) ^ (*s++ & ~0x20); /* case insensitive */

	return h % HASH_SIZE;
}

/**
 * Free all alias data
 */
void free_alias_data(void)
{
	struct canon *c, *d;
	struct alias *a, *b;
	int i;

	for (i = 0; i != HASH_SIZE; i++) {
		for (c = canon_tab[i]; c; c = d) {
			d = c->next;
			free(c);
		}
		canon_tab[i] = NULL;

		for (a = alias_tab[i]; a; a = b) {
			b = a->next;
			free(a);
		}
		alias_tab[i] = NULL;
	}
}

#ifdef TEST
/**
 * Dump all alias data to stdout
 */
void dump_alias_data(void)
{
	struct canon *c;
	struct alias *a;
	int i;
	size_t size = 0;

	for (i = 0; i != HASH_SIZE; i++) {
		for (c = canon_tab[i]; c; c = c->next) {
			printf("%d %s\n", i, c->name);
			size += offsetof(struct canon, name) + c->name_len;
		}

		for (a = alias_tab[i]; a; a = a->next) {
			printf("%d %s\n", i, a->name);
			size += offsetof(struct alias, name) + a->name_len;
		}
	}

	size += (sizeof(canon_tab) / sizeof(canon_tab[0]));
	size += (sizeof(alias_tab) / sizeof(alias_tab[0]));

	printf("%d\n", size);
}
#endif

/**
 * Create alias data from Aliases file
 *
 * \param filename  The path to the Aliases file
 * \return 1 on success, 0 on failure.
 */
int create_alias_data(const char *filename)
{
	char buf[300];
	FILE *fp;

	if (!filename)
		return 0;

	fp = fopen(filename, "r");
	if (!fp)
		return 0;

	while (fgets(buf, sizeof buf, fp)) {
		char *p, *aliases = 0, *mib, *end;
		struct canon *cf;

		if (buf[0] == 0 || buf[0] == '#')
			/* skip blank lines or comments */
			continue;

		buf[strlen(buf) - 1] = 0; /* lose terminating newline */
		end = buf + strlen(buf);

		/* find end of canonical form */
		for (p = buf; *p && !isspace(*p) && !iscntrl(*p); p++)
			; /* do nothing */
		if (p >= end)
			continue;
		*p++ = '\0'; /* terminate canonical form */

		/* skip whitespace */
		for (; *p && isspace(*p); p++)
			; /* do nothing */
		if (p >= end)
			continue;
		mib = p;

		/* find end of mibenum */
		for (; *p && !isspace(*p) && !iscntrl(*p); p++)
			; /* do nothing */
		if (p < end)
			*p++ = '\0'; /* terminate mibenum */

		cf = create_canon(buf, atoi(mib));
		if (!cf)
			continue;

		/* skip whitespace */
		for (; p < end && *p && isspace(*p); p++)
			; /* do nothing */
		if (p >= end)
			continue;
		aliases = p;

		while (p < end) {
			/* find end of alias */
			for (; *p && !isspace(*p) && !iscntrl(*p); p++)
				; /* do nothing */
			if (p > end)
				/* stop if we've gone past the end */
				break;
			/* terminate current alias */
			*p++ = '\0';

			if (!create_alias(aliases, cf))
				break;

			/* in terminating, we may have advanced
			 * past the end - check this here */
			if (p >= end)
				break;

			/* skip whitespace */
			for (; *p && isspace(*p); p++)
				; /* do nothing */

			if (p >= end)
				/* gone past end => stop */
				break;

			/* update pointer to current alias */
			aliases = p;
		}
	}

	fclose(fp);

	return 1;
}

/**
 * Retrieve the canonical form of an alias name
 *
 * \param alias The alias name
 * \return Pointer to struct canon or NULL if not found
 */
struct canon *alias_canonicalise(const char *alias)
{
	int hash, len;
	struct canon *c;
	struct alias *a;

	if (!alias)
		return NULL;

	hash = hash_val(alias);
	len = strlen(alias);

	for (c = canon_tab[hash]; c; c = c->next)
		if (c->name_len == len && strcasecmp(c->name, alias) == 0)
			break;
	if (c)
		return c;

	for (a = alias_tab[hash]; a; a = a->next)
		if (a->name_len == len && strcasecmp(a->name, alias) == 0)
			break;
	if (a)
		return a->canon;

	return NULL;
}

/**
 * Retrieve the MIB enum value assigned to an encoding name
 *
 * \param alias The alias to lookup
 * \return The MIB enum value, or 0 if not found
 */
short mibenum_from_name(const char *alias)
{
	struct canon *c;

	if (!alias)
		return 0;

	c = alias_canonicalise(alias);
	if (!c)
		return 0;

	return c->mib_enum;
}

/**
 * Retrieve the canonical name of an encoding from the MIB enum
 *
 * \param mibenum The MIB enum value
 * \return Pointer to canonical name, or NULL if not found
 */
const char *mibenum_to_name(short mibenum)
{
	int i;
	struct canon *c;

	for (i = 0; i != HASH_SIZE; i++)
		for (c = canon_tab[i]; c; c = c->next)
			if (c->mib_enum == mibenum)
				return c->name;

	return NULL;
}