1 /* Simplified ASN.1 notation parser
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
22 #include <linux/asn1_ber_bytecode.h>
28 DIRECTIVE_APPLICATION,
40 DIRECTIVE_CONSTRAINED,
44 DIRECTIVE_DEFINITIONS,
47 DIRECTIVE_ENCODING_CONTROL,
53 DIRECTIVE_EXTENSIBILITY,
57 DIRECTIVE_GeneralString,
58 DIRECTIVE_GeneralizedTime,
59 DIRECTIVE_GraphicString,
67 DIRECTIVE_INSTRUCTIONS,
69 DIRECTIVE_INTERSECTION,
70 DIRECTIVE_ISO646String,
73 DIRECTIVE_MINUS_INFINITY,
75 DIRECTIVE_NumericString,
80 DIRECTIVE_ObjectDescriptor,
83 DIRECTIVE_PLUS_INFINITY,
86 DIRECTIVE_PrintableString,
88 DIRECTIVE_RELATIVE_OID,
97 DIRECTIVE_TeletexString,
102 DIRECTIVE_UTF8String,
103 DIRECTIVE_UniversalString,
104 DIRECTIVE_VideotexString,
105 DIRECTIVE_VisibleString,
108 TOKEN_ASSIGNMENT = NR__DIRECTIVES,
122 static const unsigned char token_to_tag[NR__TOKENS] = {
124 [DIRECTIVE_BOOLEAN] = ASN1_BOOL,
125 [DIRECTIVE_INTEGER] = ASN1_INT,
126 [DIRECTIVE_BIT] = ASN1_BTS,
127 [DIRECTIVE_OCTET] = ASN1_OTS,
128 [DIRECTIVE_NULL] = ASN1_NULL,
129 [DIRECTIVE_OBJECT] = ASN1_OID,
130 [DIRECTIVE_ObjectDescriptor] = ASN1_ODE,
131 [DIRECTIVE_EXTERNAL] = ASN1_EXT,
132 [DIRECTIVE_REAL] = ASN1_REAL,
133 [DIRECTIVE_ENUMERATED] = ASN1_ENUM,
134 [DIRECTIVE_EMBEDDED] = 0,
135 [DIRECTIVE_UTF8String] = ASN1_UTF8STR,
136 [DIRECTIVE_RELATIVE_OID] = ASN1_RELOID,
139 [DIRECTIVE_SEQUENCE] = ASN1_SEQ,
140 [DIRECTIVE_SET] = ASN1_SET,
141 [DIRECTIVE_NumericString] = ASN1_NUMSTR,
142 [DIRECTIVE_PrintableString] = ASN1_PRNSTR,
143 [DIRECTIVE_T61String] = ASN1_TEXSTR,
144 [DIRECTIVE_TeletexString] = ASN1_TEXSTR,
145 [DIRECTIVE_VideotexString] = ASN1_VIDSTR,
146 [DIRECTIVE_IA5String] = ASN1_IA5STR,
147 [DIRECTIVE_UTCTime] = ASN1_UNITIM,
148 [DIRECTIVE_GeneralizedTime] = ASN1_GENTIM,
149 [DIRECTIVE_GraphicString] = ASN1_GRASTR,
150 [DIRECTIVE_VisibleString] = ASN1_VISSTR,
151 [DIRECTIVE_GeneralString] = ASN1_GENSTR,
152 [DIRECTIVE_UniversalString] = ASN1_UNITIM,
153 [DIRECTIVE_CHARACTER] = ASN1_CHRSTR,
154 [DIRECTIVE_BMPString] = ASN1_BMPSTR,
157 static const char asn1_classes[4][5] = {
158 [ASN1_UNIV] = "UNIV",
159 [ASN1_APPL] = "APPL",
160 [ASN1_CONT] = "CONT",
164 static const char asn1_methods[2][5] = {
165 [ASN1_UNIV] = "PRIM",
169 static const char *const asn1_universal_tags[32] = {
204 static const char *filename;
205 static const char *grammar_name;
206 static const char *outputname;
207 static const char *headername;
209 static const char *const directives[NR__DIRECTIVES] = {
210 #define _(X) [DIRECTIVE_##X] = #X
233 [DIRECTIVE_ENCODING_CONTROL] = "ENCODING-CONTROL",
259 [DIRECTIVE_MINUS_INFINITY] = "MINUS-INFINITY",
260 [DIRECTIVE_NULL] = "NULL",
269 [DIRECTIVE_PLUS_INFINITY] = "PLUS-INFINITY",
274 [DIRECTIVE_RELATIVE_OID] = "RELATIVE-OID",
301 static struct action *action_list;
302 static unsigned nr_actions;
306 enum token_type token_type : 8;
308 struct action *action;
313 static struct token *token_list;
314 static unsigned nr_tokens;
315 static bool verbose_opt;
316 static bool debug_opt;
318 #define verbose(fmt, ...) do { if (verbose_opt) printf(fmt, ## __VA_ARGS__); } while (0)
319 #define debug(fmt, ...) do { if (debug_opt) printf(fmt, ## __VA_ARGS__); } while (0)
321 static int directive_compare(const void *_key, const void *_pdir)
323 const struct token *token = _key;
324 const char *const *pdir = _pdir, *dir = *pdir;
329 clen = (dlen < token->size) ? dlen : token->size;
331 //debug("cmp(%*.*s,%s) = ",
332 // (int)token->size, (int)token->size, token->value,
335 val = memcmp(token->value, dir, clen);
337 //debug("%d [cmp]\n", val);
341 if (dlen == token->size) {
345 //debug("%d\n", (int)dlen - (int)token->size);
346 return dlen - token->size; /* shorter -> negative */
350 * Tokenise an ASN.1 grammar
352 static void tokenise(char *buffer, char *end)
354 struct token *tokens;
355 char *line, *nl, *p, *q;
356 unsigned tix, lineno;
358 /* Assume we're going to have half as many tokens as we have
361 token_list = tokens = calloc((end - buffer) / 2, sizeof(struct token));
369 while (buffer < end) {
370 /* First of all, break out a line */
373 nl = memchr(line, '\n', end - buffer);
381 /* Remove "--" comments */
384 while ((p = memchr(p, '-', nl - p))) {
386 /* Found a comment; see if there's a terminator */
388 while ((q = memchr(q, '-', nl - q))) {
390 /* There is - excise the comment */
392 memmove(p, q, nl - q);
407 /* Skip white space */
408 while (p < nl && isspace(*p))
413 tokens[tix].line = lineno;
414 tokens[tix].value = p;
416 /* Handle string tokens */
420 /* Can be a directive, type name or element
421 * name. Find the end of the name.
424 while (q < nl && (isalnum(*q) || *q == '-' || *q == '_'))
426 tokens[tix].size = q - p;
429 /* If it begins with a lowercase letter then
430 * it's an element name
432 if (islower(tokens[tix].value[0])) {
433 tokens[tix++].token_type = TOKEN_ELEMENT_NAME;
437 /* Otherwise we need to search the directive
440 dir = bsearch(&tokens[tix], directives,
441 sizeof(directives) / sizeof(directives[1]),
442 sizeof(directives[1]),
445 tokens[tix++].token_type = dir - directives;
449 tokens[tix++].token_type = TOKEN_TYPE_NAME;
455 /* Find the end of the number */
457 while (q < nl && (isdigit(*q)))
459 tokens[tix].size = q - p;
461 tokens[tix++].token_type = TOKEN_NUMBER;
466 if (memcmp(p, "::=", 3) == 0) {
468 tokens[tix].size = 3;
469 tokens[tix++].token_type = TOKEN_ASSIGNMENT;
475 if (memcmp(p, "({", 2) == 0) {
477 tokens[tix].size = 2;
478 tokens[tix++].token_type = TOKEN_OPEN_ACTION;
481 if (memcmp(p, "})", 2) == 0) {
483 tokens[tix].size = 2;
484 tokens[tix++].token_type = TOKEN_CLOSE_ACTION;
490 tokens[tix].size = 1;
494 tokens[tix++].token_type = TOKEN_OPEN_CURLY;
498 tokens[tix++].token_type = TOKEN_CLOSE_CURLY;
502 tokens[tix++].token_type = TOKEN_OPEN_SQUARE;
506 tokens[tix++].token_type = TOKEN_CLOSE_SQUARE;
510 tokens[tix++].token_type = TOKEN_COMMA;
517 fprintf(stderr, "%s:%u: Unknown character in grammar: '%c'\n",
518 filename, lineno, *p);
524 verbose("Extracted %u tokens\n", nr_tokens);
529 for (n = 0; n < nr_tokens; n++)
530 debug("Token %3u: '%*.*s'\n",
532 (int)token_list[n].size, (int)token_list[n].size,
533 token_list[n].value);
538 static void build_type_list(void);
539 static void parse(void);
540 static void dump_elements(void);
541 static void render(FILE *out, FILE *hdr);
546 int main(int argc, char **argv)
552 char *kbuild_verbose;
555 kbuild_verbose = getenv("KBUILD_VERBOSE");
557 verbose_opt = atoi(kbuild_verbose);
560 if (strcmp(argv[1], "-v") == 0)
562 else if (strcmp(argv[1], "-d") == 0)
566 memmove(&argv[1], &argv[2], (argc - 2) * sizeof(char *));
571 fprintf(stderr, "Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n",
577 outputname = argv[2];
578 headername = argv[3];
580 fd = open(filename, O_RDONLY);
586 if (fstat(fd, &st) < 0) {
591 if (!(buffer = malloc(st.st_size + 1))) {
596 if ((readlen = read(fd, buffer, st.st_size)) < 0) {
606 if (readlen != st.st_size) {
607 fprintf(stderr, "%s: Short read\n", filename);
611 p = strrchr(argv[1], '/');
612 p = p ? p + 1 : argv[1];
613 grammar_name = strdup(p);
618 p = strchr(grammar_name, '.');
623 tokenise(buffer, buffer + readlen);
628 out = fopen(outputname, "w");
634 hdr = fopen(headername, "w");
642 if (fclose(out) < 0) {
647 if (fclose(hdr) < 0) {
668 struct type *type_def;
671 struct action *action;
672 struct element *children;
673 struct element *next;
674 struct element *render_next;
675 struct element *list_next;
677 enum compound compound : 8;
678 enum asn1_class class : 8;
679 enum asn1_method method : 8;
681 unsigned entry_index;
683 #define ELEMENT_IMPLICIT 0x0001
684 #define ELEMENT_EXPLICIT 0x0002
685 #define ELEMENT_TAG_SPECIFIED 0x0004
686 #define ELEMENT_RENDERED 0x0008
687 #define ELEMENT_SKIPPABLE 0x0010
688 #define ELEMENT_CONDITIONAL 0x0020
694 struct element *element;
697 #define TYPE_STOP_MARKER 0x0001
698 #define TYPE_BEGIN 0x0002
701 static struct type *type_list;
702 static struct type **type_index;
703 static unsigned nr_types;
705 static int type_index_compare(const void *_a, const void *_b)
707 const struct type *const *a = _a, *const *b = _b;
709 if ((*a)->name->size != (*b)->name->size)
710 return (*a)->name->size - (*b)->name->size;
712 return memcmp((*a)->name->value, (*b)->name->value,
716 static int type_finder(const void *_key, const void *_ti)
718 const struct token *token = _key;
719 const struct type *const *ti = _ti;
720 const struct type *type = *ti;
722 if (token->size != type->name->size)
723 return token->size - type->name->size;
725 return memcmp(token->value, type->name->value,
730 * Build up a list of types and a sorted index to that list.
732 static void build_type_list(void)
738 for (n = 0; n < nr_tokens - 1; n++)
739 if (token_list[n + 0].token_type == TOKEN_TYPE_NAME &&
740 token_list[n + 1].token_type == TOKEN_ASSIGNMENT)
744 fprintf(stderr, "%s: No defined types\n", filename);
749 types = type_list = calloc(nr + 1, sizeof(type_list[0]));
754 type_index = calloc(nr, sizeof(type_index[0]));
761 types[t].flags |= TYPE_BEGIN;
762 for (n = 0; n < nr_tokens - 1; n++) {
763 if (token_list[n + 0].token_type == TOKEN_TYPE_NAME &&
764 token_list[n + 1].token_type == TOKEN_ASSIGNMENT) {
765 types[t].name = &token_list[n];
766 type_index[t] = &types[t];
770 types[t].name = &token_list[n + 1];
771 types[t].flags |= TYPE_STOP_MARKER;
773 qsort(type_index, nr, sizeof(type_index[0]), type_index_compare);
775 verbose("Extracted %u types\n", nr_types);
777 for (n = 0; n < nr_types; n++) {
778 struct type *type = type_index[n];
780 (int)type->name->size,
781 (int)type->name->size,
787 static struct element *parse_type(struct token **_cursor, struct token *stop,
791 * Parse the token stream
793 static void parse(void)
795 struct token *cursor;
798 /* Parse one type definition statement at a time */
803 if (cursor[0].token_type != TOKEN_TYPE_NAME ||
804 cursor[1].token_type != TOKEN_ASSIGNMENT)
808 type->element = parse_type(&cursor, type[1].name, NULL);
809 type->element->type_def = type;
811 if (cursor != type[1].name) {
812 fprintf(stderr, "%s:%d: Parse error at token '%*.*s'\n",
813 filename, cursor->line,
814 (int)cursor->size, (int)cursor->size, cursor->value);
818 } while (type++, !(type->flags & TYPE_STOP_MARKER));
820 verbose("Extracted %u actions\n", nr_actions);
823 static struct element *element_list;
825 static struct element *alloc_elem(struct token *type)
827 struct element *e = calloc(1, sizeof(*e));
832 e->list_next = element_list;
837 static struct element *parse_compound(struct token **_cursor, struct token *end,
841 * Parse one type definition statement
843 static struct element *parse_type(struct token **_cursor, struct token *end,
846 struct element *top, *element;
847 struct action *action, **ppaction;
848 struct token *cursor = *_cursor;
851 int labelled = 0, implicit = 0;
853 top = element = alloc_elem(cursor);
854 element->class = ASN1_UNIV;
855 element->method = ASN1_PRIM;
856 element->tag = token_to_tag[cursor->token_type];
857 element->name = name;
859 /* Extract the tag value if one given */
860 if (cursor->token_type == TOKEN_OPEN_SQUARE) {
864 switch (cursor->token_type) {
865 case DIRECTIVE_UNIVERSAL:
866 element->class = ASN1_UNIV;
869 case DIRECTIVE_APPLICATION:
870 element->class = ASN1_APPL;
874 element->class = ASN1_CONT;
876 case DIRECTIVE_PRIVATE:
877 element->class = ASN1_PRIV;
881 fprintf(stderr, "%s:%d: Unrecognised tag class token '%*.*s'\n",
882 filename, cursor->line,
883 (int)cursor->size, (int)cursor->size, cursor->value);
889 if (cursor->token_type != TOKEN_NUMBER) {
890 fprintf(stderr, "%s:%d: Missing tag number '%*.*s'\n",
891 filename, cursor->line,
892 (int)cursor->size, (int)cursor->size, cursor->value);
896 element->tag &= ~0x1f;
897 element->tag |= strtoul(cursor->value, &p, 10);
898 element->flags |= ELEMENT_TAG_SPECIFIED;
899 if (p - cursor->value != cursor->size)
905 if (cursor->token_type != TOKEN_CLOSE_SQUARE) {
906 fprintf(stderr, "%s:%d: Missing closing square bracket '%*.*s'\n",
907 filename, cursor->line,
908 (int)cursor->size, (int)cursor->size, cursor->value);
917 /* Handle implicit and explicit markers */
918 if (cursor->token_type == DIRECTIVE_IMPLICIT) {
919 element->flags |= ELEMENT_IMPLICIT;
924 } else if (cursor->token_type == DIRECTIVE_EXPLICIT) {
925 element->flags |= ELEMENT_EXPLICIT;
933 element->method |= ASN1_CONS;
934 element->compound = implicit ? TAG_OVERRIDE : SEQUENCE;
935 element->children = alloc_elem(cursor);
936 element = element->children;
937 element->class = ASN1_UNIV;
938 element->method = ASN1_PRIM;
939 element->tag = token_to_tag[cursor->token_type];
940 element->name = name;
943 /* Extract the type we're expecting here */
944 element->type = cursor;
945 switch (cursor->token_type) {
947 element->compound = ANY;
952 case DIRECTIVE_BOOLEAN:
953 case DIRECTIVE_ENUMERATED:
954 case DIRECTIVE_INTEGER:
955 element->compound = NOT_COMPOUND;
959 case DIRECTIVE_EXTERNAL:
960 element->method = ASN1_CONS;
962 case DIRECTIVE_BMPString:
963 case DIRECTIVE_GeneralString:
964 case DIRECTIVE_GraphicString:
965 case DIRECTIVE_IA5String:
966 case DIRECTIVE_ISO646String:
967 case DIRECTIVE_NumericString:
968 case DIRECTIVE_PrintableString:
969 case DIRECTIVE_T61String:
970 case DIRECTIVE_TeletexString:
971 case DIRECTIVE_UniversalString:
972 case DIRECTIVE_UTF8String:
973 case DIRECTIVE_VideotexString:
974 case DIRECTIVE_VisibleString:
975 case DIRECTIVE_ObjectDescriptor:
976 case DIRECTIVE_GeneralizedTime:
977 case DIRECTIVE_UTCTime:
978 element->compound = NOT_COMPOUND;
983 case DIRECTIVE_OCTET:
984 element->compound = NOT_COMPOUND;
988 if (cursor->token_type != DIRECTIVE_STRING)
993 case DIRECTIVE_OBJECT:
994 element->compound = NOT_COMPOUND;
998 if (cursor->token_type != DIRECTIVE_IDENTIFIER)
1003 case TOKEN_TYPE_NAME:
1004 element->compound = TYPE_REF;
1005 ref = bsearch(cursor, type_index, nr_types, sizeof(type_index[0]),
1008 fprintf(stderr, "%s:%d: Type '%*.*s' undefined\n",
1009 filename, cursor->line,
1010 (int)cursor->size, (int)cursor->size, cursor->value);
1013 cursor->type = *ref;
1014 (*ref)->ref_count++;
1018 case DIRECTIVE_CHOICE:
1019 element->compound = CHOICE;
1021 element->children = parse_compound(&cursor, end, 1);
1024 case DIRECTIVE_SEQUENCE:
1025 element->compound = SEQUENCE;
1026 element->method = ASN1_CONS;
1030 if (cursor->token_type == DIRECTIVE_OF) {
1031 element->compound = SEQUENCE_OF;
1035 element->children = parse_type(&cursor, end, NULL);
1037 element->children = parse_compound(&cursor, end, 0);
1042 element->compound = SET;
1043 element->method = ASN1_CONS;
1047 if (cursor->token_type == DIRECTIVE_OF) {
1048 element->compound = SET_OF;
1052 element->children = parse_type(&cursor, end, NULL);
1054 element->children = parse_compound(&cursor, end, 1);
1059 fprintf(stderr, "%s:%d: Token '%*.*s' does not introduce a type\n",
1060 filename, cursor->line,
1061 (int)cursor->size, (int)cursor->size, cursor->value);
1065 /* Handle elements that are optional */
1066 if (cursor < end && (cursor->token_type == DIRECTIVE_OPTIONAL ||
1067 cursor->token_type == DIRECTIVE_DEFAULT)
1070 top->flags |= ELEMENT_SKIPPABLE;
1073 if (cursor < end && cursor->token_type == TOKEN_OPEN_ACTION) {
1077 if (cursor->token_type != TOKEN_ELEMENT_NAME) {
1078 fprintf(stderr, "%s:%d: Token '%*.*s' is not an action function name\n",
1079 filename, cursor->line,
1080 (int)cursor->size, (int)cursor->size, cursor->value);
1084 action = malloc(sizeof(struct action) + cursor->size + 1);
1090 memcpy(action->name, cursor->value, cursor->size);
1091 action->name[cursor->size] = 0;
1093 for (ppaction = &action_list;
1095 ppaction = &(*ppaction)->next
1097 int cmp = strcmp(action->name, (*ppaction)->name);
1104 action->next = *ppaction;
1110 action->next = NULL;
1115 element->action = action;
1116 cursor->action = action;
1120 if (cursor->token_type != TOKEN_CLOSE_ACTION) {
1121 fprintf(stderr, "%s:%d: Missing close action, got '%*.*s'\n",
1122 filename, cursor->line,
1123 (int)cursor->size, (int)cursor->size, cursor->value);
1133 fprintf(stderr, "%s:%d: Unexpected token '%*.*s'\n",
1134 filename, cursor->line,
1135 (int)cursor->size, (int)cursor->size, cursor->value);
1139 fprintf(stderr, "%s: Unexpectedly hit EOF\n", filename);
1144 * Parse a compound type list
1146 static struct element *parse_compound(struct token **_cursor, struct token *end,
1149 struct element *children, **child_p = &children, *element;
1150 struct token *cursor = *_cursor, *name;
1152 if (cursor->token_type != TOKEN_OPEN_CURLY) {
1153 fprintf(stderr, "%s:%d: Expected compound to start with brace not '%*.*s'\n",
1154 filename, cursor->line,
1155 (int)cursor->size, (int)cursor->size, cursor->value);
1162 if (cursor->token_type == TOKEN_OPEN_CURLY) {
1163 fprintf(stderr, "%s:%d: Empty compound\n",
1164 filename, cursor->line);
1170 if (cursor->token_type == TOKEN_ELEMENT_NAME) {
1177 element = parse_type(&cursor, end, name);
1179 element->flags |= ELEMENT_SKIPPABLE | ELEMENT_CONDITIONAL;
1182 child_p = &element->next;
1186 if (cursor->token_type != TOKEN_COMMA)
1193 children->flags &= ~ELEMENT_CONDITIONAL;
1195 if (cursor->token_type != TOKEN_CLOSE_CURLY) {
1196 fprintf(stderr, "%s:%d: Expected compound closure, got '%*.*s'\n",
1197 filename, cursor->line,
1198 (int)cursor->size, (int)cursor->size, cursor->value);
1207 fprintf(stderr, "%s: Unexpectedly hit EOF\n", filename);
1211 static void dump_element(const struct element *e, int level)
1213 const struct element *c;
1214 const struct type *t = e->type_def;
1215 const char *name = e->name ? e->name->value : ".";
1216 int nsize = e->name ? e->name->size : 1;
1217 const char *tname = t && t->name ? t->name->value : ".";
1218 int tnsize = t && t->name ? t->name->size : 1;
1221 if (e->class == 0 && e->method == 0 && e->tag == 0)
1222 strcpy(tag, "<...>");
1223 else if (e->class == ASN1_UNIV)
1224 sprintf(tag, "%s %s %s",
1225 asn1_classes[e->class],
1226 asn1_methods[e->method],
1227 asn1_universal_tags[e->tag]);
1229 sprintf(tag, "%s %s %u",
1230 asn1_classes[e->class],
1231 asn1_methods[e->method],
1234 printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n",
1235 e->flags & ELEMENT_IMPLICIT ? 'I' : '-',
1236 e->flags & ELEMENT_EXPLICIT ? 'E' : '-',
1237 e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-',
1238 e->flags & ELEMENT_SKIPPABLE ? 'S' : '-',
1239 e->flags & ELEMENT_CONDITIONAL ? 'C' : '-',
1240 "-tTqQcaro"[e->compound],
1243 tnsize, tnsize, tname,
1245 e->action ? e->action->name : "");
1246 if (e->compound == TYPE_REF)
1247 dump_element(e->type->type->element, level + 3);
1249 for (c = e->children; c; c = c->next)
1250 dump_element(c, level + 3);
1253 static void dump_elements(void)
1256 dump_element(type_list[0].element, 0);
1259 static void render_element(FILE *out, struct element *e, struct element *tag);
1260 static void render_out_of_line_list(FILE *out);
1262 static int nr_entries;
1263 static int render_depth = 1;
1264 static struct element *render_list, **render_list_p = &render_list;
1266 __attribute__((format(printf, 2, 3)))
1267 static void render_opcode(FILE *out, const char *fmt, ...)
1272 fprintf(out, "\t[%4d] =%*s", nr_entries, render_depth, "");
1274 vfprintf(out, fmt, va);
1280 __attribute__((format(printf, 2, 3)))
1281 static void render_more(FILE *out, const char *fmt, ...)
1287 vfprintf(out, fmt, va);
1293 * Render the grammar into a state machine definition.
1295 static void render(FILE *out, FILE *hdr)
1298 struct action *action;
1302 fprintf(hdr, "/*\n");
1303 fprintf(hdr, " * Automatically generated by asn1_compiler. Do not edit\n");
1304 fprintf(hdr, " *\n");
1305 fprintf(hdr, " * ASN.1 parser for %s\n", grammar_name);
1306 fprintf(hdr, " */\n");
1307 fprintf(hdr, "#include <linux/asn1_decoder.h>\n");
1309 fprintf(hdr, "extern const struct asn1_decoder %s_decoder;\n", grammar_name);
1315 fprintf(out, "/*\n");
1316 fprintf(out, " * Automatically generated by asn1_compiler. Do not edit\n");
1317 fprintf(out, " *\n");
1318 fprintf(out, " * ASN.1 parser for %s\n", grammar_name);
1319 fprintf(out, " */\n");
1320 fprintf(out, "#include <linux/asn1_ber_bytecode.h>\n");
1321 fprintf(out, "#include \"%s-asn1.h\"\n", grammar_name);
1328 /* Tabulate the action functions we might have to call */
1331 for (action = action_list; action; action = action->next) {
1332 action->index = index++;
1334 "extern int %s(void *, size_t, unsigned char,"
1335 " const void *, size_t);\n",
1340 fprintf(out, "enum %s_actions {\n", grammar_name);
1341 for (action = action_list; action; action = action->next)
1342 fprintf(out, "\tACT_%s = %u,\n",
1343 action->name, action->index);
1344 fprintf(out, "\tNR__%s_actions = %u\n", grammar_name, nr_actions);
1345 fprintf(out, "};\n");
1348 fprintf(out, "static const asn1_action_t %s_action_table[NR__%s_actions] = {\n",
1349 grammar_name, grammar_name);
1350 for (action = action_list; action; action = action->next)
1351 fprintf(out, "\t[%4u] = %s,\n", action->index, action->name);
1352 fprintf(out, "};\n");
1359 /* We do two passes - the first one calculates all the offsets */
1360 verbose("Pass 1\n");
1362 root = &type_list[0];
1363 render_element(NULL, root->element, NULL);
1364 render_opcode(NULL, "ASN1_OP_COMPLETE,\n");
1365 render_out_of_line_list(NULL);
1367 for (e = element_list; e; e = e->list_next)
1368 e->flags &= ~ELEMENT_RENDERED;
1370 /* And then we actually render */
1371 verbose("Pass 2\n");
1373 fprintf(out, "static const unsigned char %s_machine[] = {\n",
1377 root = &type_list[0];
1378 render_element(out, root->element, NULL);
1379 render_opcode(out, "ASN1_OP_COMPLETE,\n");
1380 render_out_of_line_list(out);
1382 fprintf(out, "};\n");
1385 fprintf(out, "const struct asn1_decoder %s_decoder = {\n", grammar_name);
1386 fprintf(out, "\t.machine = %s_machine,\n", grammar_name);
1387 fprintf(out, "\t.machlen = sizeof(%s_machine),\n", grammar_name);
1388 fprintf(out, "\t.actions = %s_action_table,\n", grammar_name);
1389 fprintf(out, "};\n");
1393 * Render the out-of-line elements
1395 static void render_out_of_line_list(FILE *out)
1397 struct element *e, *ce;
1401 while ((e = render_list)) {
1402 render_list = e->render_next;
1404 render_list_p = &render_list;
1406 render_more(out, "\n");
1407 e->entry_index = entry = nr_entries;
1409 for (ce = e->children; ce; ce = ce->next)
1410 render_element(out, ce, NULL);
1413 act = e->action ? "_ACT" : "";
1414 switch (e->compound) {
1416 render_opcode(out, "ASN1_OP_END_SEQ%s,\n", act);
1419 render_opcode(out, "ASN1_OP_END_SEQ_OF%s,\n", act);
1420 render_opcode(out, "_jump_target(%u),\n", entry);
1423 render_opcode(out, "ASN1_OP_END_SET%s,\n", act);
1426 render_opcode(out, "ASN1_OP_END_SET_OF%s,\n", act);
1427 render_opcode(out, "_jump_target(%u),\n", entry);
1433 render_opcode(out, "_action(ACT_%s),\n",
1435 render_opcode(out, "ASN1_OP_RETURN,\n");
1440 * Render an element.
1442 static void render_element(FILE *out, struct element *e, struct element *tag)
1444 struct element *ec, *x;
1445 const char *cond, *act;
1446 int entry, skippable = 0, outofline = 0;
1448 if (e->flags & ELEMENT_SKIPPABLE ||
1449 (tag && tag->flags & ELEMENT_SKIPPABLE))
1452 if ((e->type_def && e->type_def->ref_count > 1) ||
1456 if (e->type_def && out) {
1457 render_more(out, "\t// %*.*s\n",
1458 (int)e->type_def->name->size, (int)e->type_def->name->size,
1459 e->type_def->name->value);
1462 /* Render the operation */
1463 cond = (e->flags & ELEMENT_CONDITIONAL ||
1464 (tag && tag->flags & ELEMENT_CONDITIONAL)) ? "COND_" : "";
1465 act = e->action ? "_ACT" : "";
1466 switch (e->compound) {
1468 render_opcode(out, "ASN1_OP_%sMATCH_ANY%s%s,",
1469 cond, act, skippable ? "_OR_SKIP" : "");
1471 render_more(out, "\t\t// %*.*s",
1472 (int)e->name->size, (int)e->name->size,
1474 render_more(out, "\n");
1475 goto dont_render_tag;
1478 render_element(out, e->children, e);
1485 render_opcode(out, "ASN1_OP_%sMATCH%s%s,",
1487 outofline ? "_JUMP" : "",
1488 skippable ? "_OR_SKIP" : "");
1492 goto dont_render_tag;
1495 if (e->class == ASN1_UNIV && e->method == ASN1_PRIM && e->tag == 0)
1496 goto dont_render_tag;
1498 render_opcode(out, "ASN1_OP_%sMATCH%s%s,",
1500 skippable ? "_OR_SKIP" : "");
1506 render_more(out, "\t\t// %*.*s",
1507 (int)x->name->size, (int)x->name->size,
1509 render_more(out, "\n");
1511 /* Render the tag */
1512 if (!tag || !(tag->flags & ELEMENT_TAG_SPECIFIED))
1515 if (tag->class == ASN1_UNIV &&
1519 render_opcode(out, "_tag(%s, %s, %s),\n",
1520 asn1_classes[tag->class],
1521 asn1_methods[tag->method | e->method],
1522 asn1_universal_tags[tag->tag]);
1524 render_opcode(out, "_tagn(%s, %s, %2u),\n",
1525 asn1_classes[tag->class],
1526 asn1_methods[tag->method | e->method],
1531 /* Deal with compound types */
1532 switch (e->compound) {
1534 render_element(out, e->type->type->element, tag);
1536 render_opcode(out, "ASN1_OP_%sACT,\n",
1537 skippable ? "MAYBE_" : "");
1542 /* Render out-of-line for multiple use or
1544 render_opcode(out, "_jump_target(%u),", e->entry_index);
1545 if (e->type_def && e->type_def->name)
1546 render_more(out, "\t\t// --> %*.*s",
1547 (int)e->type_def->name->size,
1548 (int)e->type_def->name->size,
1549 e->type_def->name->value);
1550 render_more(out, "\n");
1551 if (!(e->flags & ELEMENT_RENDERED)) {
1552 e->flags |= ELEMENT_RENDERED;
1554 render_list_p = &e->render_next;
1558 /* Render inline for single use */
1560 for (ec = e->children; ec; ec = ec->next)
1561 render_element(out, ec, NULL);
1563 render_opcode(out, "ASN1_OP_END_SEQ%s,\n", act);
1570 /* Render out-of-line for multiple use or
1572 render_opcode(out, "_jump_target(%u),", e->entry_index);
1573 if (e->type_def && e->type_def->name)
1574 render_more(out, "\t\t// --> %*.*s",
1575 (int)e->type_def->name->size,
1576 (int)e->type_def->name->size,
1577 e->type_def->name->value);
1578 render_more(out, "\n");
1579 if (!(e->flags & ELEMENT_RENDERED)) {
1580 e->flags |= ELEMENT_RENDERED;
1582 render_list_p = &e->render_next;
1586 /* Render inline for single use */
1589 render_element(out, e->children, NULL);
1591 if (e->compound == SEQUENCE_OF)
1592 render_opcode(out, "ASN1_OP_END_SEQ_OF%s,\n", act);
1594 render_opcode(out, "ASN1_OP_END_SET_OF%s,\n", act);
1595 render_opcode(out, "_jump_target(%u),\n", entry);
1600 /* I can't think of a nice way to do SET support without having
1601 * a stack of bitmasks to make sure no element is repeated.
1602 * The bitmask has also to be checked that no non-optional
1603 * elements are left out whilst not preventing optional
1604 * elements from being left out.
1606 fprintf(stderr, "The ASN.1 SET type is not currently supported.\n");
1610 for (ec = e->children; ec; ec = ec->next)
1611 render_element(out, ec, ec);
1613 render_opcode(out, "ASN1_OP_COND_FAIL,\n");
1615 render_opcode(out, "ASN1_OP_ACT,\n");
1623 render_opcode(out, "_action(ACT_%s),\n", e->action->name);