File: enum_names.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (411 lines) | stat: -rw-r--r-- 10,715 bytes parent folder | download | duplicates (2)
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
/*
 * tables of names for values defined in constants.h
 * Copyright (C) 2012-2017 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2012 Avesh Agarwal <avagarwa@redhat.com>
 * Copyright (C) 1998-2002,2015  D. Hugh Redelmeier.
 * Copyright (C) 2016-2017 Andrew Cagney
 * Copyright (C) 2017 Vukasin Karadzic <vukasin.karadzic@gmail.com>
 * Copyright (C) 2019 Andrew Cagney <cagney@gnu.org>
 * Copyright (C) 2020 Yulia Kuzovkova <ukuzovkova@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/*
 * Note that the array sizes are all specified; this is to enable range
 * checking by code that only includes constants.h.
 */

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <netinet/in.h>

#include <ietf_constants.h>
#include "passert.h"

#include "constants.h"
#include "enum_names.h"
#include "lswlog.h"
#include "ip_said.h"		/* for SPI_PASS et.al. */
#include "secrets.h"		/* for enum privae_key_kind */

/*
 * Iterate over the enum_names returning all the valid indexes.
 *
 * Use -1 as the starting point / sentinel.
 *
 * XXX: Works fine provided we ignore the enum_names object that
 * contains negative values stored in unsigned fields!
 */

long next_enum(enum_names *en, long l)
{
	if (l < -1) {
		llog_passert(&global_logger, HERE, "%ld should be >= -1", l);
	}

	unsigned long e;

	/*
	 * Advance L by 1 giving a starting candidate.
	 */
	if (l < 0) {
		e = 0;
	} else {
		e = l+1;
	}

	while (true) {
		/*
		 * Find a range that puts an upper bound on E.  E may
		 * fall within the range, but could also fall short.
		 */
		const struct enum_names *b = NULL;
		for (const struct enum_names *p = en;
		     p != NULL; p = p->en_next_range) {

			if (p->en_last - p->en_first + 1 != p->en_checklen) {
				llog_passert(&global_logger, HERE,
					     "p->en_last(%lu) - p->en_first(%lu) + 1 == p->en_checklen(%zu)",
					     p->en_last, p->en_first, p->en_checklen);
			}

			if (e > p->en_last) {
				continue;
			}

			if (e >= p->en_first) {
				b = p;
				break;
			}

			/* e < .en_first; save when closer */
			if (b == NULL ||
			    b->en_first > p->en_first) {
				b = p;
			}
		}
		if (b == NULL) {
			return -1;
		}

		/*
		 * If E isn't yet within the range, make it so.
		 */
		if (e < b->en_first) {
			e = b->en_first;
		}

		/*
		 * Find the first valid name within the range; if this
		 * fails, iterate round looking for a new range.
		 */
		while (e <= b->en_last) {
			if (b->en_names[e - b->en_first] != NULL) {
				return e;
			}
			e++;
		}
	}
}

/*
 * the enum_name range containing VAL, or NULL.
 */
const struct enum_names *enum_range(const struct enum_names *en, unsigned long val, const char **prefix)
{
	*prefix = NULL;
	for (enum_names *p = en; p != NULL; p = p->en_next_range) {
		passert(p->en_last - p->en_first + 1 == p->en_checklen);
		/* return most recent prefix */
		if (p->en_prefix != NULL) {
			*prefix = p->en_prefix;
		}
		if (p->en_first <= val && val <= p->en_last) {
			return p;
		}
	}
	return NULL;
}

/*
 * The actual name for VAL, using RANGE; possibly shortened using
 * PREFIX.
 */
const char *enum_range_name(const struct enum_names *range, unsigned long val,
			    const char *prefix, bool shorten)
{
	if (range == NULL) {
		return NULL;
	}
	passert(range->en_first <= val && val <= range->en_last);
	/* can be NULL */
	const char *name = range->en_names[val - range->en_first];
	if (name != NULL && prefix != NULL && shorten) {
		/* grr: can't use eat() */
		size_t pl = strlen(prefix);
		return strneq(name, prefix, pl) ? name + pl : name;
	} else {
		return name;
	}
}

static const char *find_enum(const struct enum_names *en, unsigned long val, bool shorten)
{
	const char *prefix = NULL;
	/* can be NULL */
	const struct enum_names *range = enum_range(en, val, &prefix);
	/* can be NULL */
	return enum_range_name(range, val, prefix, shorten);
}

bool enum_long(enum_names *ed, unsigned long val, enum_buf *b)
{
	/* can be NULL; handled here */
	b->buf = find_enum(ed, val, /*shorten?*/false);
	if (b->buf == NULL) {
		bad_name(val, b);
		return false;
	}

	return true;
}

bool enum_short(enum_names *ed, unsigned long val, enum_buf *b)
{
	/* can be NULL; handled here */
	b->buf = find_enum(ed, val, /*shorten?*/true);
	if (b->buf == NULL) {
		bad_name(val, b);
		return false;
	}

	return true;
}

const char *str_enum_long(enum_names *ed, unsigned long val, enum_buf *b)
{
	enum_long(ed, val, b);
	return b->buf;
}

const char *str_enum_short(enum_names *ed, unsigned long val, enum_buf *b)
{
	enum_short(ed, val, b);
	return b->buf;
}

size_t jam_enum_long(struct jambuf *buf, enum_names *en, unsigned long val)
{
	const char *name = find_enum(en, val, /*shorten*/false);
	if (name != NULL) {
		return jam_string(buf, name);
	}
	return jam_bad(buf, en->en_prefix, val);
}

size_t jam_enum_short(struct jambuf *buf, enum_names *en, unsigned long val)
{
	/* can be NULL; handled here */
	const char *name = find_enum(en, val, /*shorten*/true);
	if (name != NULL) {
		return jam_string(buf, name);
	}
	return jam_bad(buf, en->en_prefix, val);
}

size_t jam_enum_human(struct jambuf *buf, enum_names *en, unsigned long val)
{
	/* can be NULL; handled here */
	const char *name = find_enum(en, val, /*shorten?*/true);
	if (name != NULL) {
		return jam_string_human(buf, name);
	}
	return jam_bad(buf, en->en_prefix, val);
}

/*
 * Find the value for a name in an enum_names table.  If not found, returns -1.
 *
 * Strings are compared without regard to case.
 *
 * ??? the table contains unsigned long values BUT the function returns an
 * int so there is some potential for overflow.
 */
int enum_search(enum_names *ed, const char *str)
{
	for (enum_names *p = ed; p != NULL; p = p->en_next_range) {
		passert(p->en_last - p->en_first + 1 == p->en_checklen);
		for (unsigned long en = p->en_first; en <= p->en_last; en++) {
			const char *ptr = p->en_names[en - p->en_first];

			if (ptr != NULL && strcaseeq(ptr, str)) {
				passert(en <= INT_MAX);
				return en;
			}
		}
	}
	return -1;
}

int enum_match(enum_names *ed, shunk_t string)
{
	const char *prefix = NULL;
	for (enum_names *p = ed; p != NULL; p = p->en_next_range) {
		passert(p->en_last - p->en_first + 1 == p->en_checklen);
		prefix = (p->en_prefix == NULL ? prefix : p->en_prefix);
		for (unsigned long en = p->en_first; en <= p->en_last; en++) {
			const char *name = p->en_names[en - p->en_first];

			if (name == NULL) {
				continue;
			}

			passert(en <= INT_MAX);

			/*
			 * try matching all four variants of name:
			 * with and without prefix en->en_prefix and
			 * with and without suffix '(...)'
			 */
			size_t name_len = strlen(name);
			size_t prefix_len = (prefix == NULL ? 0 : strlen(prefix));

			/* suffix must not and will not overlap prefix */
			const char *suffix = strchr(name + prefix_len, '(');

			size_t suffix_len = (suffix != NULL && name[name_len - 1] == ')' ?
					     &name[name_len] - suffix : 0);

#			define try(guard, f, b) ( \
				(guard) && \
				name_len - ((f) + (b)) == string.len && \
				strncaseeq(name + (f), string.ptr, string.len))

			if (try(true, 0, 0) ||
			    try(suffix_len > 0, 0, suffix_len) ||
			    try(prefix_len > 0, prefix_len, 0) ||
			    try(prefix_len > 0 && suffix_len > 0, prefix_len, suffix_len))
			{
				return en;
			}
#			undef try
		}
	}
	return -1;
}

/* choose table from struct enum_enum_names */
static const struct enum_names *enum_enum_table(enum_enum_names *een,
						unsigned long table)
{
	if (!(een->een_last - een->een_first + 1 == een->een_checklen)) {
		/* *_{last,first} are longs */
		llog_passert(&global_logger, HERE,
			     ".een_last=%lu - .een_first=%lu + 1 == .een_checklen=%zu",
			     een->een_last, een->een_first, een->een_checklen);
	}

	if (een->een_first <= table && table <= een->een_last) {
		return een->een_enum_name[table - een->een_first];
	}

	return NULL;
}

bool enum_enum_name(enum_enum_names *een, unsigned long table,
		    unsigned long val, enum_buf *b)
{
	enum_names *en = enum_enum_table(een, table);
	if (en == NULL) {
		snprintf(b->tmp, sizeof(b->tmp), "%lu.%lu", table, val);
		b->buf = b->tmp;
		return false;
	}

	return enum_name(en, val, b);
}

const char *str_enum_enum(enum_enum_names *een, unsigned long table,
			  unsigned long val, enum_buf *b)
{
	enum_enum_name(een, table, val, b);
	return b->buf;
}

const char *str_enum_enum_short(enum_enum_names *een, unsigned long table,
				unsigned long val, enum_buf *b)
{
	enum_names *en = enum_enum_table(een, table);
	if (en == NULL) {
		/* assume the log context implies the table name */
		snprintf(b->tmp, sizeof(b->tmp), "%lu.%lu", table, val);
		b->buf = b->tmp;
		return b->buf;
	}

	return str_enum_short(en, val, b);
}

size_t jam_enum_enum(struct jambuf *buf, enum_enum_names *een,
		     unsigned long table, unsigned long val)
{
	enum_names *en = enum_enum_table(een, table);
	if (en == NULL) {
		/* XXX: dump something more meaningful */
		return jam(buf, "%lu.%lu", table, val);
	}
	return jam_enum(buf, en, val);
}

size_t jam_enum_enum_short(struct jambuf *buf, enum_enum_names *een,
			   unsigned long table, unsigned long val)
{
	enum_names *en = enum_enum_table(een, table);
	if (en == NULL) {
		/* XXX: dump something more meaningful */
		return jam(buf, "%lu.%lu", table, val);
	}
	return jam_enum_short(buf, en, val);
}

void check_enum_names(const struct enum_names_check *checklist)
{
	/* check that enum_names are well-formed */
	for (const struct enum_names_check *c = checklist;
	     c->name != NULL && c->enum_names != NULL; c++) {
		/*
		 * enum_name will check all linked enum_names if given
		 * a value that isn't covered.  -42 is probably not
		 * covered.
		 */
		enum_buf b;
		enum_name(c->enum_names, -42UL, &b);
	}
}

void check_enum_enum_names(const struct enum_enum_names_check *checklist)
{
	/* check that enum_enum_names are well-formed */
	for (const struct enum_enum_names_check *c = checklist;
	     c->name != NULL && c->enum_enum_names != NULL; c++) {
		/* check v2_transform_ID_enums, the only enum_enum_names */
		enum_enum_table(c->enum_enum_names, -42UL);
	}
}

void init_enum_names(void)
{
	check_enum_names(enum_names_checklist);
	check_enum_enum_names(enum_enum_names_checklist);
}