-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabnf.c
More file actions
467 lines (390 loc) · 11.8 KB
/
abnf.c
File metadata and controls
467 lines (390 loc) · 11.8 KB
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
/* Parse ABNF to locate dangling references and undefined symbols
* does limited syntax checking
*
* This is a quick&dirty hack donated to public domain by Chris Newman
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR 0
#define COMMENT 1
#define RULEDEF 2
#define AMENDRULE 3
#define HEXMODE 4
#define BINMODE 5
#define DECMODE 6
#define ALTERNATE 7
#define REPEAT 8
#define BEGINGROUP 9
#define ENDGROUP 10
#define BEGINOPTION 11
#define ENDOPTION 12
#define RANGE 13
#define LITLIST 14
#define NUMBER 15
#define HEXNUMBER 16
#define QUOTED 17
#define PROSE 18
#define RULENAME 19
#define NEWLINE 20
static const char *token_name[] = {
"error", "comment", "ruledef", "amendrule", "hexmode",
"binmode", "decmode", "alternate", "repeat", "begingroup",
"endgroup", "beginoption", "endoption", "range", "litlist",
"number", "hexnumber", "quoted", "prose", "rulename",
"newline"};
/* simple rulename datastructure
*/
struct rulename {
struct rulename *next;
int defined, referenced;
int len;
char data[1];
};
#define HTSIZE 1023
static struct rulename *htable[HTSIZE];
static unsigned char hex_table[256];
const static unsigned char hexchars[] = "0123456789ABCDEF";
static const char *baserules[] = {"ALPHA", "BIT", "CHAR", "CR", "CRLF",
"CTL", "DIGIT", "DQUOTE", "HEXDIG", "HTAB",
"LF", "LWSP", "OCTET", "SP", "VCHAR",
"WSP", NULL};
const unsigned char cvt_to_lowercase[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 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0xfb,
0xfc, 0xfd, 0xfe, 0xff};
#define TOLOWER(c) (cvt_to_lowercase[(unsigned char)(c)])
/* returns pointer to end of token, NULL if no tokens left in line
*/
char *gettoken(char *ptr, int *tokentype, int hexmode) {
*tokentype = ERROR;
switch (*ptr++) {
case '\0':
return (NULL);
case '\r':
if (*ptr == '\n') ++ptr;
case '\n':
*tokentype = NEWLINE;
break;
case ';':
*tokentype = COMMENT;
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') ++ptr;
break;
case '/':
*tokentype = ALTERNATE;
break;
case '*':
*tokentype = REPEAT;
break;
case '(':
*tokentype = BEGINGROUP;
break;
case ')':
*tokentype = ENDGROUP;
break;
case '[':
*tokentype = BEGINOPTION;
break;
case ']':
*tokentype = ENDOPTION;
break;
case '-':
*tokentype = RANGE;
break;
case '.':
*tokentype = LITLIST;
break;
case '=':
*tokentype = RULEDEF;
if (*ptr == '/') {
*tokentype = AMENDRULE;
++ptr;
}
break;
case '%':
switch (*ptr++) {
case 'x':
case 'X':
*tokentype = HEXMODE;
break;
case 'b':
case 'B':
*tokentype = BINMODE;
break;
case 'd':
case 'D':
*tokentype = DECMODE;
break;
default:
return ("Invalid literal");
}
break;
case '"':
while (*ptr && *ptr != '"') ++ptr;
if (*ptr != '"') {
return ("Unbalanced double quotes");
}
++ptr;
*tokentype = QUOTED;
break;
case '<':
while (*ptr && *ptr != '>') ++ptr;
if (*ptr != '>') {
return ("Unbalanced angle brakets");
}
++ptr;
*tokentype = PROSE;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (hexmode) goto GETHEX;
while (isdigit(*ptr)) ++ptr;
*tokentype = NUMBER;
break;
default:
/* deal with hex digits */
if (hexmode && isxdigit(ptr[-1])) {
GETHEX:
while (isxdigit(*ptr)) ++ptr;
*tokentype = HEXNUMBER;
break;
}
if (!isalpha(ptr[-1])) {
return ("Invalid character");
}
/* deal with rule names */
while (isalnum(*ptr) || *ptr == '-') ++ptr;
*tokentype = RULENAME;
break;
}
return (ptr);
}
/* find a rule in a hash table
*/
struct rulename *hashfind(const char *ptr, int len) {
int i, hash;
struct rulename **pptr;
/* lookup in hash table */
hash = len;
for (i = 0; i < len; ++i) {
hash = (hash * 13 + TOLOWER(ptr[i])) % HTSIZE;
}
pptr = &htable[hash];
while (*pptr && ((*pptr)->len != len || strncasecmp(ptr, (*pptr)->data, len) != 0)) {
pptr = &(*pptr)->next;
}
/* create new entry, if needed */
if (!*pptr) {
*pptr = calloc(sizeof(struct rulename) + len, 1);
if (!*pptr) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
(*pptr)->len = len;
memcpy((*pptr)->data, ptr, len);
}
return (*pptr);
}
int main(int argc, char **argv) {
int verbose = 0;
const unsigned char *uscan;
char *buf, *name, *src, *dst, *linestart;
char *ptr, *nptr;
const char **ruleptr;
int hexmode, tokentype, line;
int plevel = 0, blevel = 0;
int bused, bsize, amount;
struct rulename *lastrule;
int has_err = 0;
struct rulename *first_rule = NULL;
/* initialize hex table */
memset(hex_table, 16, sizeof(hex_table));
for (uscan = hexchars; *uscan; ++uscan) {
hex_table[(unsigned)*uscan] = uscan - hexchars;
}
/* initialize default rules */
for (ruleptr = baserules; *ruleptr != NULL; ++ruleptr) {
lastrule = hashfind(*ruleptr, strlen(*ruleptr));
lastrule->defined = lastrule->referenced = 1;
}
lastrule = NULL;
/* get program name */
name = strrchr(*argv, '/');
name = name == NULL ? *argv : name + 1;
/* parse args */
while (*++argv != NULL && **argv == '-') switch ((*argv)[1]) {
case 'v':
verbose = 1;
break;
case 'h':
printf("%s [-h] [-v]\n", name);
printf(" -v enable verbose output\n");
printf(" -h display this help\n");
exit(0);
default:
fprintf(stderr, "Invalid arguments, type `%s -h' for usage\n", name);
exit(1);
}
/* read the data into memory */
bsize = 4096;
buf = malloc(bsize);
if (buf != NULL) {
for (bused = 0;
(amount = fread(buf + bused, 1, bsize - bused, stdin)) != 0;
bused += amount) {
if (bused + amount == bsize) {
if (bsize > 10 * 1024 * 1024) {
fprintf(stderr, "Size limit exceeded\n");
exit(1);
}
buf = realloc(buf, bsize *= 2);
if (buf == NULL) break;
}
}
}
if (buf == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
if (!feof(stdin)) {
perror("input");
exit(1);
}
buf[bused] = '\0';
/* process lines */
ptr = linestart = buf;
hexmode = 0;
line = 1;
/* get tokens */
for (;;) {
/* skip whitespace */
while (*ptr == ' ' || *ptr == '\t') {
++ptr;
hexmode = 0;
}
/* get token, done if at end of line */
if ((nptr = gettoken(ptr, &tokentype, hexmode)) == NULL) break;
/* handle errors */
if (!tokentype) {
printf("Error (line %d, col %d): %s\n", line, (int)(ptr - linestart), nptr);
exit(1);
}
/* reset hexmode */
if (hexmode && tokentype != HEXNUMBER && tokentype != LITLIST && tokentype != RANGE) {
hexmode = 0;
}
/* handle last rulename */
if (lastrule != NULL) {
if (tokentype == RULEDEF) {
lastrule->defined = 1;
} else if (tokentype == AMENDRULE) {
lastrule->defined = lastrule->referenced = 1;
} else {
lastrule->referenced = 1;
}
lastrule = NULL;
}
if (verbose && tokentype != NEWLINE) {
printf("line %d, col %d, %s", line, (int)(ptr - linestart), token_name[tokentype]);
if (nptr != NULL) {
printf(": %.*s", (int)(nptr - ptr), ptr);
}
putchar('\n');
}
switch (tokentype) {
case NEWLINE:
hexmode = 0;
++line;
ptr = linestart = nptr;
continue;
case RULENAME:
lastrule = hashfind(ptr, nptr - ptr);
if (first_rule == NULL) first_rule = lastrule;
break;
case HEXMODE:
hexmode = 1;
break;
case RULEDEF:
case AMENDRULE:
if (plevel || blevel) {
UNBALANCED:
printf("Error (line %d): Unbalanced %s\n", line, plevel ? "parenthesis" : "square brackets");
exit(1);
}
break;
case BEGINGROUP:
++plevel;
break;
case ENDGROUP:
if (--plevel < 0) goto UNBALANCED;
break;
case BEGINOPTION:
++blevel;
break;
case ENDOPTION:
if (--blevel < 0) goto UNBALANCED;
break;
}
/* advance */
ptr = nptr;
}
/* final warnings */
if (plevel) {
printf("Error at end of text: Unbalanced parenthesis\n");
has_err = 1;
}
if (blevel) {
printf("Error at end of text: Unbalanced square brackets\n");
has_err = 1;
}
/* the first rule always counts as referenced */
if (first_rule) first_rule->referenced = 1;
/* list undefined and unreferenced rules */
for (line = 0; line < HTSIZE; ++line) {
lastrule = htable[line];
while (lastrule) {
if (!lastrule->defined) {
printf("undefined rule: %s\n", lastrule->data);
has_err = 1;
}
if (!lastrule->referenced) {
printf("unreferenced rule: %s\n", lastrule->data);
has_err = 1;
}
lastrule = lastrule->next;
}
}
if (has_err == 1) {
exit(1);
}
printf("ABNF validation (version 1.0) completed\n");
exit(0);
}