summaryrefslogtreecommitdiffstats
path: root/src/util/name_mask.c
blob: 284d4fa9d308c166f08e47e69d820fd16473affb (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
/*++
/* NAME
/*	name_mask 3
/* SUMMARY
/*	map names to bit mask
/* SYNOPSIS
/*	#include <name_mask.h>
/*
/*	int	name_mask(context, table, names)
/*	const char *context;
/*	const NAME_MASK *table;
/*	const char *names;
/*
/*	long	long_name_mask(context, table, names)
/*	const char *context;
/*	const LONG_NAME_MASK *table;
/*	const char *names;
/*
/*	const char *str_name_mask(context, table, mask)
/*	const char *context;
/*	const NAME_MASK *table;
/*	int	mask;
/*
/*	const char *str_long_name_mask(context, table, mask)
/*	const char *context;
/*	const LONG_NAME_MASK *table;
/*	long	mask;
/*
/*	int	name_mask_opt(context, table, names, flags)
/*	const char *context;
/*	const NAME_MASK *table;
/*	const char *names;
/*	int	flags;
/*
/*	long	long_name_mask_opt(context, table, names, flags)
/*	const char *context;
/*	const LONG_NAME_MASK *table;
/*	const char *names;
/*	int	flags;
/*
/*	int	name_mask_delim_opt(context, table, names, delim, flags)
/*	const char *context;
/*	const NAME_MASK *table;
/*	const char *names;
/*	const char *delim;
/*	int	flags;
/*
/*	long	long_name_mask_delim_opt(context, table, names, delim, flags)
/*	const char *context;
/*	const LONG_NAME_MASK *table;
/*	const char *names;
/*	const char *delim;
/*	int	flags;
/*
/*	const char *str_name_mask_opt(buf, context, table, mask, flags)
/*	VSTRING	*buf;
/*	const char *context;
/*	const NAME_MASK *table;
/*	int	mask;
/*	int	flags;
/*
/*	const char *str_long_name_mask_opt(buf, context, table, mask, flags)
/*	VSTRING	*buf;
/*	const char *context;
/*	const LONG_NAME_MASK *table;
/*	long	mask;
/*	int	flags;
/* DESCRIPTION
/*	name_mask() takes a null-terminated \fItable\fR with (name, mask)
/*	values and computes the bit-wise OR of the masks that correspond
/*	to the names listed in the \fInames\fR argument, separated by
/*	comma and/or whitespace characters. The "long_" version returns
/*	a "long int" bitmask, rather than an "int" bitmask.
/*
/*	str_name_mask() translates a mask into its equivalent names.
/*	The result is written to a static buffer that is overwritten
/*	upon each call. The "long_" version converts a "long int"
/*	bitmask, rather than an "int" bitmask.
/*
/*	name_mask_opt() and str_name_mask_opt() are extended versions
/*	with additional fine control. name_mask_delim_opt() supports
/*	non-default delimiter characters.
/*
/*	Arguments:
/* .IP buf
/*	Null pointer or pointer to buffer storage.
/* .IP context
/*	What kind of names and
/*	masks are being manipulated, in order to make error messages
/*	more understandable. Typically, this would be the name of a
/*	user-configurable parameter.
/* .IP table
/*	Table with (name, bit mask) pairs.
/* .IP names
/*	A list of names that is to be converted into a bit mask.
/* .IP mask
/*	A bit mask.
/* .IP delim
/*	Delimiter characters to use instead of whitespace and commas.
/* .IP flags
/*	Bit-wise OR of one or more of the following.  Where features
/*	would have conflicting results (e.g., FATAL versus IGNORE),
/*	the feature that takes precedence is described first.
/*
/*	When converting from string to mask, at least one of the
/*	following must be specified: NAME_MASK_FATAL, NAME_MASK_RETURN,
/*	NAME_MASK_WARN or NAME_MASK_IGNORE.
/*
/*	When converting from mask to string, at least one of the
/*	following must be specified: NAME_MASK_NUMBER, NAME_MASK_FATAL,
/*	NAME_MASK_RETURN, NAME_MASK_WARN or NAME_MASK_IGNORE.
/* .RS
/* .IP NAME_MASK_NUMBER
/*	When converting from string to mask, accept hexadecimal
/*	inputs starting with "0x" followed by hexadecimal digits.
/*	Each hexadecimal input may specify multiple bits.  This
/*	feature is ignored for hexadecimal inputs that cannot be
/*	converted (malformed, out of range, etc.).
/*
/*	When converting from mask to string, represent bits not
/*	defined in \fItable\fR as "0x" followed by hexadecimal
/*	digits. This conversion always succeeds.
/* .IP NAME_MASK_FATAL
/*	Require that all names listed in \fIname\fR exist in
/*	\fItable\fR or that they can be parsed as a hexadecimal
/*	string, and require that all bits listed in \fImask\fR exist
/*	in \fItable\fR or that they can be converted to hexadecimal
/*	string.  Terminate with a fatal run-time error if this
/*	condition is not met.  This feature is enabled by default
/*	when calling name_mask() or str_name_mask().
/* .IP NAME_MASK_RETURN
/*	Require that all names listed in \fIname\fR exist in
/*	\fItable\fR or that they can be parsed as a hexadecimal
/*	string, and require that all bits listed in \fImask\fR exist
/*	in \fItable\fR or that they can be converted to hexadecimal
/*	string.  Log a warning, and return 0 (name_mask()) or a
/*	null pointer (str_name_mask()) if this condition is not
/*	met.  This feature is not enabled by default when calling
/*	name_mask() or str_name_mask().
/* .IP NAME_MASK_WARN
/*	Require that all names listed in \fIname\fR exist in
/*	\fItable\fR or that they can be parsed as a hexadecimal
/*	string, and require that all bits listed in \fImask\fR exist
/*	in \fItable\fR or that they can be converted to hexadecimal
/*	string.  Log a warning if this condition is not met, continue
/*	processing, and return all valid bits or names.  This feature
/*	is not enabled by default when calling name_mask() or
/*	str_name_mask().
/* .IP NAME_MASK_IGNORE
/*	Silently ignore names listed in \fIname\fR that don't exist
/*	in \fItable\fR and that can't be parsed as a hexadecimal
/*	string, and silently ignore bits listed in \fImask\fR that
/*	don't exist in \fItable\fR and that can't be converted to
/*	hexadecimal string.
/* .IP NAME_MASK_ANY_CASE
/*	Enable case-insensitive matching.
/*	This feature is not enabled by default when calling name_mask();
/*	it has no effect with str_name_mask().
/* .IP NAME_MASK_COMMA
/*	Use comma instead of space when converting a mask to string.
/* .IP NAME_MASK_PIPE
/*	Use "|" instead of space when converting a mask to string.
/* .RE
/*	The value NAME_MASK_NONE explicitly requests no features,
/*	and NAME_MASK_DEFAULT enables the default options.
/* DIAGNOSTICS
/*	Fatal: the \fInames\fR argument specifies a name not found in
/*	\fItable\fR, or the \fImask\fR specifies a bit not found in
/*	\fItable\fR.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#ifdef STRCASECMP_IN_STRINGS_H
#include <strings.h>
#endif

/* Utility library. */

#include <msg.h>
#include <mymalloc.h>
#include <stringops.h>
#include <name_mask.h>
#include <vstring.h>

static int hex_to_ulong(char *, unsigned long, unsigned long *);

#define STR(x) vstring_str(x)

/* name_mask_delim_opt - compute mask corresponding to list of names */

int     name_mask_delim_opt(const char *context, const NAME_MASK *table,
		            const char *names, const char *delim, int flags)
{
    const char *myname = "name_mask";
    char   *saved_names = mystrdup(names);
    char   *bp = saved_names;
    int     result = 0;
    const NAME_MASK *np;
    char   *name;
    int     (*lookup) (const char *, const char *);
    unsigned long ulval;

    if ((flags & NAME_MASK_REQUIRED) == 0)
	msg_panic("%s: missing NAME_MASK_FATAL/RETURN/WARN/IGNORE flag",
		  myname);

    if (flags & NAME_MASK_ANY_CASE)
	lookup = strcasecmp;
    else
	lookup = strcmp;

    /*
     * Break up the names string, and look up each component in the table. If
     * the name is found, merge its mask with the result.
     */
    while ((name = mystrtok(&bp, delim)) != 0) {
	for (np = table; /* void */ ; np++) {
	    if (np->name == 0) {
		if ((flags & NAME_MASK_NUMBER)
		    && hex_to_ulong(name, ~0U, &ulval)) {
		    result |= (unsigned int) ulval;
		} else if (flags & NAME_MASK_FATAL) {
		    msg_fatal("unknown %s value \"%s\" in \"%s\"",
			      context, name, names);
		} else if (flags & NAME_MASK_RETURN) {
		    msg_warn("unknown %s value \"%s\" in \"%s\"",
			     context, name, names);
		    myfree(saved_names);
		    return (0);
		} else if (flags & NAME_MASK_WARN) {
		    msg_warn("unknown %s value \"%s\" in \"%s\"",
			     context, name, names);
		}
		break;
	    }
	    if (lookup(name, np->name) == 0) {
		if (msg_verbose)
		    msg_info("%s: %s", myname, name);
		result |= np->mask;
		break;
	    }
	}
    }
    myfree(saved_names);
    return (result);
}

/* str_name_mask_opt - mask to string */

const char *str_name_mask_opt(VSTRING *buf, const char *context,
			              const NAME_MASK *table,
			              int mask, int flags)
{
    const char *myname = "name_mask";
    const NAME_MASK *np;
    ssize_t len;
    static VSTRING *my_buf = 0;
    int     delim = (flags & NAME_MASK_COMMA ? ',' :
		     (flags & NAME_MASK_PIPE ? '|' : ' '));

    if ((flags & STR_NAME_MASK_REQUIRED) == 0)
	msg_panic("%s: missing NAME_MASK_NUMBER/FATAL/RETURN/WARN/IGNORE flag",
		  myname);

    if (buf == 0) {
	if (my_buf == 0)
	    my_buf = vstring_alloc(1);
	buf = my_buf;
    }
    VSTRING_RESET(buf);

    for (np = table; mask != 0; np++) {
	if (np->name == 0) {
	    if (flags & NAME_MASK_NUMBER) {
		vstring_sprintf_append(buf, "0x%x%c", mask, delim);
	    } else if (flags & NAME_MASK_FATAL) {
		msg_fatal("%s: unknown %s bit in mask: 0x%x",
			  myname, context, mask);
	    } else if (flags & NAME_MASK_RETURN) {
		msg_warn("%s: unknown %s bit in mask: 0x%x",
			 myname, context, mask);
		return (0);
	    } else if (flags & NAME_MASK_WARN) {
		msg_warn("%s: unknown %s bit in mask: 0x%x",
			 myname, context, mask);
	    }
	    break;
	}
	if (mask & np->mask) {
	    mask &= ~np->mask;
	    vstring_sprintf_append(buf, "%s%c", np->name, delim);
	}
    }
    if ((len = VSTRING_LEN(buf)) > 0)
	vstring_truncate(buf, len - 1);
    VSTRING_TERMINATE(buf);

    return (STR(buf));
}

/* long_name_mask_delim_opt - compute mask corresponding to list of names */

long    long_name_mask_delim_opt(const char *context,
				         const LONG_NAME_MASK * table,
			               const char *names, const char *delim,
				         int flags)
{
    const char *myname = "name_mask";
    char   *saved_names = mystrdup(names);
    char   *bp = saved_names;
    long    result = 0;
    const LONG_NAME_MASK *np;
    char   *name;
    int     (*lookup) (const char *, const char *);
    unsigned long ulval;

    if ((flags & NAME_MASK_REQUIRED) == 0)
	msg_panic("%s: missing NAME_MASK_FATAL/RETURN/WARN/IGNORE flag",
		  myname);

    if (flags & NAME_MASK_ANY_CASE)
	lookup = strcasecmp;
    else
	lookup = strcmp;

    /*
     * Break up the names string, and look up each component in the table. If
     * the name is found, merge its mask with the result.
     */
    while ((name = mystrtok(&bp, delim)) != 0) {
	for (np = table; /* void */ ; np++) {
	    if (np->name == 0) {
		if ((flags & NAME_MASK_NUMBER)
		    && hex_to_ulong(name, ~0UL, &ulval)) {
		    result |= ulval;
		} else if (flags & NAME_MASK_FATAL) {
		    msg_fatal("unknown %s value \"%s\" in \"%s\"",
			      context, name, names);
		} else if (flags & NAME_MASK_RETURN) {
		    msg_warn("unknown %s value \"%s\" in \"%s\"",
			     context, name, names);
		    myfree(saved_names);
		    return (0);
		} else if (flags & NAME_MASK_WARN) {
		    msg_warn("unknown %s value \"%s\" in \"%s\"",
			     context, name, names);
		}
		break;
	    }
	    if (lookup(name, np->name) == 0) {
		if (msg_verbose)
		    msg_info("%s: %s", myname, name);
		result |= np->mask;
		break;
	    }
	}
    }

    myfree(saved_names);
    return (result);
}

/* str_long_name_mask_opt - mask to string */

const char *str_long_name_mask_opt(VSTRING *buf, const char *context,
				           const LONG_NAME_MASK * table,
				           long mask, int flags)
{
    const char *myname = "name_mask";
    ssize_t len;
    static VSTRING *my_buf = 0;
    int     delim = (flags & NAME_MASK_COMMA ? ',' :
		     (flags & NAME_MASK_PIPE ? '|' : ' '));
    const LONG_NAME_MASK *np;

    if ((flags & STR_NAME_MASK_REQUIRED) == 0)
	msg_panic("%s: missing NAME_MASK_NUMBER/FATAL/RETURN/WARN/IGNORE flag",
		  myname);

    if (buf == 0) {
	if (my_buf == 0)
	    my_buf = vstring_alloc(1);
	buf = my_buf;
    }
    VSTRING_RESET(buf);

    for (np = table; mask != 0; np++) {
	if (np->name == 0) {
	    if (flags & NAME_MASK_NUMBER) {
		vstring_sprintf_append(buf, "0x%lx%c", mask, delim);
	    } else if (flags & NAME_MASK_FATAL) {
		msg_fatal("%s: unknown %s bit in mask: 0x%lx",
			  myname, context, mask);
	    } else if (flags & NAME_MASK_RETURN) {
		msg_warn("%s: unknown %s bit in mask: 0x%lx",
			 myname, context, mask);
		return (0);
	    } else if (flags & NAME_MASK_WARN) {
		msg_warn("%s: unknown %s bit in mask: 0x%lx",
			 myname, context, mask);
	    }
	    break;
	}
	if (mask & np->mask) {
	    mask &= ~np->mask;
	    vstring_sprintf_append(buf, "%s%c", np->name, delim);
	}
    }
    if ((len = VSTRING_LEN(buf)) > 0)
	vstring_truncate(buf, len - 1);
    VSTRING_TERMINATE(buf);

    return (STR(buf));
}

/* hex_to_ulong - 0x... to unsigned long or smaller */

static int hex_to_ulong(char *value, unsigned long mask, unsigned long *ulp)
{
    unsigned long result;
    char   *cp;

    if (strncasecmp(value, "0x", 2) != 0)
	return (0);

    /*
     * Check for valid hex number. Since the value starts with 0x, strtoul()
     * will not allow a negative sign before the first nibble. So we don't
     * need to worry about explicit +/- signs.
     */
    errno = 0;
    result = strtoul(value, &cp, 16);
    if (*cp != '\0' || errno == ERANGE)
	return (0);

    *ulp = (result & mask);
    return (*ulp == result);
}

#ifdef TEST

 /*
  * Stand-alone test program.
  */
#include <stdlib.h>
#include <vstream.h>
#include <vstring_vstream.h>

int     main(int argc, char **argv)
{
    static const NAME_MASK demo_table[] = {
	"zero", 1 << 0,
	"one", 1 << 1,
	"two", 1 << 2,
	"three", 1 << 3,
	0, 0,
    };
    static const NAME_MASK feature_table[] = {
	"DEFAULT", NAME_MASK_DEFAULT,
	"FATAL", NAME_MASK_FATAL,
	"ANY_CASE", NAME_MASK_ANY_CASE,
	"RETURN", NAME_MASK_RETURN,
	"COMMA", NAME_MASK_COMMA,
	"PIPE", NAME_MASK_PIPE,
	"NUMBER", NAME_MASK_NUMBER,
	"WARN", NAME_MASK_WARN,
	"IGNORE", NAME_MASK_IGNORE,
	0,
    };
    int     in_feature_mask;
    int     out_feature_mask;
    int     demo_mask;
    const char *demo_str;
    VSTRING *out_buf = vstring_alloc(1);
    VSTRING *in_buf = vstring_alloc(1);

    if (argc != 3)
	msg_fatal("usage: %s in-feature-mask out-feature-mask", argv[0]);
    in_feature_mask = name_mask(argv[1], feature_table, argv[1]);
    out_feature_mask = name_mask(argv[2], feature_table, argv[2]);
    while (vstring_get_nonl(in_buf, VSTREAM_IN) != VSTREAM_EOF) {
	demo_mask = name_mask_opt("name", demo_table,
				  STR(in_buf), in_feature_mask);
	demo_str = str_name_mask_opt(out_buf, "mask", demo_table,
				     demo_mask, out_feature_mask);
	vstream_printf("%s -> 0x%x -> %s\n",
		       STR(in_buf), demo_mask,
		       demo_str ? demo_str : "(null)");
	vstream_fflush(VSTREAM_OUT);
    }
    vstring_free(in_buf);
    vstring_free(out_buf);
    exit(0);
}

#endif