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) = ", token->content, dir);
333 val
= memcmp(token
->content
, dir
, clen
);
335 //debug("%d [cmp]\n", val);
339 if (dlen
== token
->size
) {
343 //debug("%d\n", (int)dlen - (int)token->size);
344 return dlen
- token
->size
; /* shorter -> negative */
348 * Tokenise an ASN.1 grammar
350 static void tokenise(char *buffer
, char *end
)
352 struct token
*tokens
;
353 char *line
, *nl
, *start
, *p
, *q
;
354 unsigned tix
, lineno
;
356 /* Assume we're going to have half as many tokens as we have
359 token_list
= tokens
= calloc((end
- buffer
) / 2, sizeof(struct token
));
367 while (buffer
< end
) {
368 /* First of all, break out a line */
371 nl
= memchr(line
, '\n', end
- buffer
);
379 /* Remove "--" comments */
382 while ((p
= memchr(p
, '-', nl
- p
))) {
384 /* Found a comment; see if there's a terminator */
386 while ((q
= memchr(q
, '-', nl
- q
))) {
388 /* There is - excise the comment */
390 memmove(p
, q
, nl
- q
);
405 /* Skip white space */
406 while (p
< nl
&& isspace(*p
))
411 tokens
[tix
].line
= lineno
;
414 /* Handle string tokens */
416 const char **dir
, *start
= p
;
418 /* Can be a directive, type name or element
419 * name. Find the end of the name.
422 while (q
< nl
&& (isalnum(*q
) || *q
== '-' || *q
== '_'))
424 tokens
[tix
].size
= q
- p
;
427 tokens
[tix
].content
= malloc(tokens
[tix
].size
+ 1);
428 if (!tokens
[tix
].content
) {
432 memcpy(tokens
[tix
].content
, start
, tokens
[tix
].size
);
433 tokens
[tix
].content
[tokens
[tix
].size
] = 0;
435 /* If it begins with a lowercase letter then
436 * it's an element name
438 if (islower(tokens
[tix
].content
[0])) {
439 tokens
[tix
++].token_type
= TOKEN_ELEMENT_NAME
;
443 /* Otherwise we need to search the directive
446 dir
= bsearch(&tokens
[tix
], directives
,
447 sizeof(directives
) / sizeof(directives
[1]),
448 sizeof(directives
[1]),
451 tokens
[tix
++].token_type
= dir
- directives
;
455 tokens
[tix
++].token_type
= TOKEN_TYPE_NAME
;
461 /* Find the end of the number */
463 while (q
< nl
&& (isdigit(*q
)))
465 tokens
[tix
].size
= q
- p
;
467 tokens
[tix
].content
= malloc(tokens
[tix
].size
+ 1);
468 if (!tokens
[tix
].content
) {
472 memcpy(tokens
[tix
].content
, start
, tokens
[tix
].size
);
473 tokens
[tix
].content
[tokens
[tix
].size
] = 0;
474 tokens
[tix
++].token_type
= TOKEN_NUMBER
;
479 if (memcmp(p
, "::=", 3) == 0) {
481 tokens
[tix
].size
= 3;
482 tokens
[tix
].content
= "::=";
483 tokens
[tix
++].token_type
= TOKEN_ASSIGNMENT
;
489 if (memcmp(p
, "({", 2) == 0) {
491 tokens
[tix
].size
= 2;
492 tokens
[tix
].content
= "({";
493 tokens
[tix
++].token_type
= TOKEN_OPEN_ACTION
;
496 if (memcmp(p
, "})", 2) == 0) {
498 tokens
[tix
].size
= 2;
499 tokens
[tix
].content
= "})";
500 tokens
[tix
++].token_type
= TOKEN_CLOSE_ACTION
;
506 tokens
[tix
].size
= 1;
510 tokens
[tix
].content
= "{";
511 tokens
[tix
++].token_type
= TOKEN_OPEN_CURLY
;
515 tokens
[tix
].content
= "}";
516 tokens
[tix
++].token_type
= TOKEN_CLOSE_CURLY
;
520 tokens
[tix
].content
= "[";
521 tokens
[tix
++].token_type
= TOKEN_OPEN_SQUARE
;
525 tokens
[tix
].content
= "]";
526 tokens
[tix
++].token_type
= TOKEN_CLOSE_SQUARE
;
530 tokens
[tix
].content
= ",";
531 tokens
[tix
++].token_type
= TOKEN_COMMA
;
538 fprintf(stderr
, "%s:%u: Unknown character in grammar: '%c'\n",
539 filename
, lineno
, *p
);
545 verbose("Extracted %u tokens\n", nr_tokens
);
550 for (n
= 0; n
< nr_tokens
; n
++)
551 debug("Token %3u: '%s'\n", n
, token_list
[n
].content
);
556 static void build_type_list(void);
557 static void parse(void);
558 static void dump_elements(void);
559 static void render(FILE *out
, FILE *hdr
);
564 int main(int argc
, char **argv
)
570 char *kbuild_verbose
;
573 kbuild_verbose
= getenv("KBUILD_VERBOSE");
575 verbose_opt
= atoi(kbuild_verbose
);
578 if (strcmp(argv
[1], "-v") == 0)
580 else if (strcmp(argv
[1], "-d") == 0)
584 memmove(&argv
[1], &argv
[2], (argc
- 2) * sizeof(char *));
589 fprintf(stderr
, "Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n",
595 outputname
= argv
[2];
596 headername
= argv
[3];
598 fd
= open(filename
, O_RDONLY
);
604 if (fstat(fd
, &st
) < 0) {
609 if (!(buffer
= malloc(st
.st_size
+ 1))) {
614 if ((readlen
= read(fd
, buffer
, st
.st_size
)) < 0) {
624 if (readlen
!= st
.st_size
) {
625 fprintf(stderr
, "%s: Short read\n", filename
);
629 p
= strrchr(argv
[1], '/');
630 p
= p
? p
+ 1 : argv
[1];
631 grammar_name
= strdup(p
);
636 p
= strchr(grammar_name
, '.');
641 tokenise(buffer
, buffer
+ readlen
);
646 out
= fopen(outputname
, "w");
652 hdr
= fopen(headername
, "w");
660 if (fclose(out
) < 0) {
665 if (fclose(hdr
) < 0) {
686 struct type
*type_def
;
689 struct action
*action
;
690 struct element
*children
;
691 struct element
*next
;
692 struct element
*render_next
;
693 struct element
*list_next
;
695 enum compound compound
: 8;
696 enum asn1_class
class : 8;
697 enum asn1_method method
: 8;
699 unsigned entry_index
;
701 #define ELEMENT_IMPLICIT 0x0001
702 #define ELEMENT_EXPLICIT 0x0002
703 #define ELEMENT_TAG_SPECIFIED 0x0004
704 #define ELEMENT_RENDERED 0x0008
705 #define ELEMENT_SKIPPABLE 0x0010
706 #define ELEMENT_CONDITIONAL 0x0020
712 struct element
*element
;
715 #define TYPE_STOP_MARKER 0x0001
716 #define TYPE_BEGIN 0x0002
719 static struct type
*type_list
;
720 static struct type
**type_index
;
721 static unsigned nr_types
;
723 static int type_index_compare(const void *_a
, const void *_b
)
725 const struct type
*const *a
= _a
, *const *b
= _b
;
727 if ((*a
)->name
->size
!= (*b
)->name
->size
)
728 return (*a
)->name
->size
- (*b
)->name
->size
;
730 return memcmp((*a
)->name
->content
, (*b
)->name
->content
,
734 static int type_finder(const void *_key
, const void *_ti
)
736 const struct token
*token
= _key
;
737 const struct type
*const *ti
= _ti
;
738 const struct type
*type
= *ti
;
740 if (token
->size
!= type
->name
->size
)
741 return token
->size
- type
->name
->size
;
743 return memcmp(token
->content
, type
->name
->content
,
748 * Build up a list of types and a sorted index to that list.
750 static void build_type_list(void)
756 for (n
= 0; n
< nr_tokens
- 1; n
++)
757 if (token_list
[n
+ 0].token_type
== TOKEN_TYPE_NAME
&&
758 token_list
[n
+ 1].token_type
== TOKEN_ASSIGNMENT
)
762 fprintf(stderr
, "%s: No defined types\n", filename
);
767 types
= type_list
= calloc(nr
+ 1, sizeof(type_list
[0]));
772 type_index
= calloc(nr
, sizeof(type_index
[0]));
779 types
[t
].flags
|= TYPE_BEGIN
;
780 for (n
= 0; n
< nr_tokens
- 1; n
++) {
781 if (token_list
[n
+ 0].token_type
== TOKEN_TYPE_NAME
&&
782 token_list
[n
+ 1].token_type
== TOKEN_ASSIGNMENT
) {
783 types
[t
].name
= &token_list
[n
];
784 type_index
[t
] = &types
[t
];
788 types
[t
].name
= &token_list
[n
+ 1];
789 types
[t
].flags
|= TYPE_STOP_MARKER
;
791 qsort(type_index
, nr
, sizeof(type_index
[0]), type_index_compare
);
793 verbose("Extracted %u types\n", nr_types
);
795 for (n
= 0; n
< nr_types
; n
++) {
796 struct type
*type
= type_index
[n
];
797 debug("- %*.*s\n", type
->name
->content
);
802 static struct element
*parse_type(struct token
**_cursor
, struct token
*stop
,
806 * Parse the token stream
808 static void parse(void)
810 struct token
*cursor
;
813 /* Parse one type definition statement at a time */
818 if (cursor
[0].token_type
!= TOKEN_TYPE_NAME
||
819 cursor
[1].token_type
!= TOKEN_ASSIGNMENT
)
823 type
->element
= parse_type(&cursor
, type
[1].name
, NULL
);
824 type
->element
->type_def
= type
;
826 if (cursor
!= type
[1].name
) {
827 fprintf(stderr
, "%s:%d: Parse error at token '%s'\n",
828 filename
, cursor
->line
, cursor
->content
);
832 } while (type
++, !(type
->flags
& TYPE_STOP_MARKER
));
834 verbose("Extracted %u actions\n", nr_actions
);
837 static struct element
*element_list
;
839 static struct element
*alloc_elem(struct token
*type
)
841 struct element
*e
= calloc(1, sizeof(*e
));
846 e
->list_next
= element_list
;
851 static struct element
*parse_compound(struct token
**_cursor
, struct token
*end
,
855 * Parse one type definition statement
857 static struct element
*parse_type(struct token
**_cursor
, struct token
*end
,
860 struct element
*top
, *element
;
861 struct action
*action
, **ppaction
;
862 struct token
*cursor
= *_cursor
;
865 int labelled
= 0, implicit
= 0;
867 top
= element
= alloc_elem(cursor
);
868 element
->class = ASN1_UNIV
;
869 element
->method
= ASN1_PRIM
;
870 element
->tag
= token_to_tag
[cursor
->token_type
];
871 element
->name
= name
;
873 /* Extract the tag value if one given */
874 if (cursor
->token_type
== TOKEN_OPEN_SQUARE
) {
878 switch (cursor
->token_type
) {
879 case DIRECTIVE_UNIVERSAL
:
880 element
->class = ASN1_UNIV
;
883 case DIRECTIVE_APPLICATION
:
884 element
->class = ASN1_APPL
;
888 element
->class = ASN1_CONT
;
890 case DIRECTIVE_PRIVATE
:
891 element
->class = ASN1_PRIV
;
895 fprintf(stderr
, "%s:%d: Unrecognised tag class token '%s'\n",
896 filename
, cursor
->line
, cursor
->content
);
902 if (cursor
->token_type
!= TOKEN_NUMBER
) {
903 fprintf(stderr
, "%s:%d: Missing tag number '%s'\n",
904 filename
, cursor
->line
, cursor
->content
);
908 element
->tag
&= ~0x1f;
909 element
->tag
|= strtoul(cursor
->content
, &p
, 10);
910 element
->flags
|= ELEMENT_TAG_SPECIFIED
;
911 if (p
- cursor
->content
!= cursor
->size
)
917 if (cursor
->token_type
!= TOKEN_CLOSE_SQUARE
) {
918 fprintf(stderr
, "%s:%d: Missing closing square bracket '%s'\n",
919 filename
, cursor
->line
, cursor
->content
);
928 /* Handle implicit and explicit markers */
929 if (cursor
->token_type
== DIRECTIVE_IMPLICIT
) {
930 element
->flags
|= ELEMENT_IMPLICIT
;
935 } else if (cursor
->token_type
== DIRECTIVE_EXPLICIT
) {
936 element
->flags
|= ELEMENT_EXPLICIT
;
944 element
->method
|= ASN1_CONS
;
945 element
->compound
= implicit
? TAG_OVERRIDE
: SEQUENCE
;
946 element
->children
= alloc_elem(cursor
);
947 element
= element
->children
;
948 element
->class = ASN1_UNIV
;
949 element
->method
= ASN1_PRIM
;
950 element
->tag
= token_to_tag
[cursor
->token_type
];
951 element
->name
= name
;
954 /* Extract the type we're expecting here */
955 element
->type
= cursor
;
956 switch (cursor
->token_type
) {
958 element
->compound
= ANY
;
963 case DIRECTIVE_BOOLEAN
:
964 case DIRECTIVE_ENUMERATED
:
965 case DIRECTIVE_INTEGER
:
966 element
->compound
= NOT_COMPOUND
;
970 case DIRECTIVE_EXTERNAL
:
971 element
->method
= ASN1_CONS
;
973 case DIRECTIVE_BMPString
:
974 case DIRECTIVE_GeneralString
:
975 case DIRECTIVE_GraphicString
:
976 case DIRECTIVE_IA5String
:
977 case DIRECTIVE_ISO646String
:
978 case DIRECTIVE_NumericString
:
979 case DIRECTIVE_PrintableString
:
980 case DIRECTIVE_T61String
:
981 case DIRECTIVE_TeletexString
:
982 case DIRECTIVE_UniversalString
:
983 case DIRECTIVE_UTF8String
:
984 case DIRECTIVE_VideotexString
:
985 case DIRECTIVE_VisibleString
:
986 case DIRECTIVE_ObjectDescriptor
:
987 case DIRECTIVE_GeneralizedTime
:
988 case DIRECTIVE_UTCTime
:
989 element
->compound
= NOT_COMPOUND
;
994 case DIRECTIVE_OCTET
:
995 element
->compound
= NOT_COMPOUND
;
999 if (cursor
->token_type
!= DIRECTIVE_STRING
)
1004 case DIRECTIVE_OBJECT
:
1005 element
->compound
= NOT_COMPOUND
;
1009 if (cursor
->token_type
!= DIRECTIVE_IDENTIFIER
)
1014 case TOKEN_TYPE_NAME
:
1015 element
->compound
= TYPE_REF
;
1016 ref
= bsearch(cursor
, type_index
, nr_types
, sizeof(type_index
[0]),
1019 fprintf(stderr
, "%s:%d: Type '%s' undefined\n",
1020 filename
, cursor
->line
, cursor
->content
);
1023 cursor
->type
= *ref
;
1024 (*ref
)->ref_count
++;
1028 case DIRECTIVE_CHOICE
:
1029 element
->compound
= CHOICE
;
1031 element
->children
= parse_compound(&cursor
, end
, 1);
1034 case DIRECTIVE_SEQUENCE
:
1035 element
->compound
= SEQUENCE
;
1036 element
->method
= ASN1_CONS
;
1040 if (cursor
->token_type
== DIRECTIVE_OF
) {
1041 element
->compound
= SEQUENCE_OF
;
1045 element
->children
= parse_type(&cursor
, end
, NULL
);
1047 element
->children
= parse_compound(&cursor
, end
, 0);
1052 element
->compound
= SET
;
1053 element
->method
= ASN1_CONS
;
1057 if (cursor
->token_type
== DIRECTIVE_OF
) {
1058 element
->compound
= SET_OF
;
1062 element
->children
= parse_type(&cursor
, end
, NULL
);
1064 element
->children
= parse_compound(&cursor
, end
, 1);
1069 fprintf(stderr
, "%s:%d: Token '%s' does not introduce a type\n",
1070 filename
, cursor
->line
, cursor
->content
);
1074 /* Handle elements that are optional */
1075 if (cursor
< end
&& (cursor
->token_type
== DIRECTIVE_OPTIONAL
||
1076 cursor
->token_type
== DIRECTIVE_DEFAULT
)
1079 top
->flags
|= ELEMENT_SKIPPABLE
;
1082 if (cursor
< end
&& cursor
->token_type
== TOKEN_OPEN_ACTION
) {
1086 if (cursor
->token_type
!= TOKEN_ELEMENT_NAME
) {
1087 fprintf(stderr
, "%s:%d: Token '%s' is not an action function name\n",
1088 filename
, cursor
->line
, cursor
->content
);
1092 action
= malloc(sizeof(struct action
));
1098 action
->name
= cursor
->content
;
1100 for (ppaction
= &action_list
;
1102 ppaction
= &(*ppaction
)->next
1104 int cmp
= strcmp(action
->name
, (*ppaction
)->name
);
1111 action
->next
= *ppaction
;
1117 action
->next
= NULL
;
1122 element
->action
= action
;
1123 cursor
->action
= action
;
1127 if (cursor
->token_type
!= TOKEN_CLOSE_ACTION
) {
1128 fprintf(stderr
, "%s:%d: Missing close action, got '%s'\n",
1129 filename
, cursor
->line
, cursor
->content
);
1139 fprintf(stderr
, "%s:%d: Unexpected token '%s'\n",
1140 filename
, cursor
->line
, cursor
->content
);
1144 fprintf(stderr
, "%s: Unexpectedly hit EOF\n", filename
);
1149 * Parse a compound type list
1151 static struct element
*parse_compound(struct token
**_cursor
, struct token
*end
,
1154 struct element
*children
, **child_p
= &children
, *element
;
1155 struct token
*cursor
= *_cursor
, *name
;
1157 if (cursor
->token_type
!= TOKEN_OPEN_CURLY
) {
1158 fprintf(stderr
, "%s:%d: Expected compound to start with brace not '%s'\n",
1159 filename
, cursor
->line
, cursor
->content
);
1166 if (cursor
->token_type
== TOKEN_OPEN_CURLY
) {
1167 fprintf(stderr
, "%s:%d: Empty compound\n",
1168 filename
, cursor
->line
);
1174 if (cursor
->token_type
== TOKEN_ELEMENT_NAME
) {
1181 element
= parse_type(&cursor
, end
, name
);
1183 element
->flags
|= ELEMENT_SKIPPABLE
| ELEMENT_CONDITIONAL
;
1186 child_p
= &element
->next
;
1190 if (cursor
->token_type
!= TOKEN_COMMA
)
1197 children
->flags
&= ~ELEMENT_CONDITIONAL
;
1199 if (cursor
->token_type
!= TOKEN_CLOSE_CURLY
) {
1200 fprintf(stderr
, "%s:%d: Expected compound closure, got '%s'\n",
1201 filename
, cursor
->line
, cursor
->content
);
1210 fprintf(stderr
, "%s: Unexpectedly hit EOF\n", filename
);
1214 static void dump_element(const struct element
*e
, int level
)
1216 const struct element
*c
;
1217 const struct type
*t
= e
->type_def
;
1218 const char *name
= e
->name
? e
->name
->content
: ".";
1219 const char *tname
= t
&& t
->name
? t
->name
->content
: ".";
1222 if (e
->class == 0 && e
->method
== 0 && e
->tag
== 0)
1223 strcpy(tag
, "<...>");
1224 else if (e
->class == ASN1_UNIV
)
1225 sprintf(tag
, "%s %s %s",
1226 asn1_classes
[e
->class],
1227 asn1_methods
[e
->method
],
1228 asn1_universal_tags
[e
->tag
]);
1230 sprintf(tag
, "%s %s %u",
1231 asn1_classes
[e
->class],
1232 asn1_methods
[e
->method
],
1235 printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %s %s \e[35m%s\e[m\n",
1236 e
->flags
& ELEMENT_IMPLICIT
? 'I' : '-',
1237 e
->flags
& ELEMENT_EXPLICIT
? 'E' : '-',
1238 e
->flags
& ELEMENT_TAG_SPECIFIED
? 'T' : '-',
1239 e
->flags
& ELEMENT_SKIPPABLE
? 'S' : '-',
1240 e
->flags
& ELEMENT_CONDITIONAL
? 'C' : '-',
1241 "-tTqQcaro"[e
->compound
],
1246 e
->action
? e
->action
->name
: "");
1247 if (e
->compound
== TYPE_REF
)
1248 dump_element(e
->type
->type
->element
, level
+ 3);
1250 for (c
= e
->children
; c
; c
= c
->next
)
1251 dump_element(c
, level
+ 3);
1254 static void dump_elements(void)
1257 dump_element(type_list
[0].element
, 0);
1260 static void render_element(FILE *out
, struct element
*e
, struct element
*tag
);
1261 static void render_out_of_line_list(FILE *out
);
1263 static int nr_entries
;
1264 static int render_depth
= 1;
1265 static struct element
*render_list
, **render_list_p
= &render_list
;
1267 __attribute__((format(printf
, 2, 3)))
1268 static void render_opcode(FILE *out
, const char *fmt
, ...)
1273 fprintf(out
, "\t[%4d] =%*s", nr_entries
, render_depth
, "");
1275 vfprintf(out
, fmt
, va
);
1281 __attribute__((format(printf
, 2, 3)))
1282 static void render_more(FILE *out
, const char *fmt
, ...)
1288 vfprintf(out
, fmt
, va
);
1294 * Render the grammar into a state machine definition.
1296 static void render(FILE *out
, FILE *hdr
)
1299 struct action
*action
;
1303 fprintf(hdr
, "/*\n");
1304 fprintf(hdr
, " * Automatically generated by asn1_compiler. Do not edit\n");
1305 fprintf(hdr
, " *\n");
1306 fprintf(hdr
, " * ASN.1 parser for %s\n", grammar_name
);
1307 fprintf(hdr
, " */\n");
1308 fprintf(hdr
, "#include <linux/asn1_decoder.h>\n");
1310 fprintf(hdr
, "extern const struct asn1_decoder %s_decoder;\n", grammar_name
);
1316 fprintf(out
, "/*\n");
1317 fprintf(out
, " * Automatically generated by asn1_compiler. Do not edit\n");
1318 fprintf(out
, " *\n");
1319 fprintf(out
, " * ASN.1 parser for %s\n", grammar_name
);
1320 fprintf(out
, " */\n");
1321 fprintf(out
, "#include <linux/asn1_ber_bytecode.h>\n");
1322 fprintf(out
, "#include \"%s-asn1.h\"\n", grammar_name
);
1329 /* Tabulate the action functions we might have to call */
1332 for (action
= action_list
; action
; action
= action
->next
) {
1333 action
->index
= index
++;
1335 "extern int %s(void *, size_t, unsigned char,"
1336 " const void *, size_t);\n",
1341 fprintf(out
, "enum %s_actions {\n", grammar_name
);
1342 for (action
= action_list
; action
; action
= action
->next
)
1343 fprintf(out
, "\tACT_%s = %u,\n",
1344 action
->name
, action
->index
);
1345 fprintf(out
, "\tNR__%s_actions = %u\n", grammar_name
, nr_actions
);
1346 fprintf(out
, "};\n");
1349 fprintf(out
, "static const asn1_action_t %s_action_table[NR__%s_actions] = {\n",
1350 grammar_name
, grammar_name
);
1351 for (action
= action_list
; action
; action
= action
->next
)
1352 fprintf(out
, "\t[%4u] = %s,\n", action
->index
, action
->name
);
1353 fprintf(out
, "};\n");
1360 /* We do two passes - the first one calculates all the offsets */
1361 verbose("Pass 1\n");
1363 root
= &type_list
[0];
1364 render_element(NULL
, root
->element
, NULL
);
1365 render_opcode(NULL
, "ASN1_OP_COMPLETE,\n");
1366 render_out_of_line_list(NULL
);
1368 for (e
= element_list
; e
; e
= e
->list_next
)
1369 e
->flags
&= ~ELEMENT_RENDERED
;
1371 /* And then we actually render */
1372 verbose("Pass 2\n");
1374 fprintf(out
, "static const unsigned char %s_machine[] = {\n",
1378 root
= &type_list
[0];
1379 render_element(out
, root
->element
, NULL
);
1380 render_opcode(out
, "ASN1_OP_COMPLETE,\n");
1381 render_out_of_line_list(out
);
1383 fprintf(out
, "};\n");
1386 fprintf(out
, "const struct asn1_decoder %s_decoder = {\n", grammar_name
);
1387 fprintf(out
, "\t.machine = %s_machine,\n", grammar_name
);
1388 fprintf(out
, "\t.machlen = sizeof(%s_machine),\n", grammar_name
);
1389 fprintf(out
, "\t.actions = %s_action_table,\n", grammar_name
);
1390 fprintf(out
, "};\n");
1394 * Render the out-of-line elements
1396 static void render_out_of_line_list(FILE *out
)
1398 struct element
*e
, *ce
;
1402 while ((e
= render_list
)) {
1403 render_list
= e
->render_next
;
1405 render_list_p
= &render_list
;
1407 render_more(out
, "\n");
1408 e
->entry_index
= entry
= nr_entries
;
1410 for (ce
= e
->children
; ce
; ce
= ce
->next
)
1411 render_element(out
, ce
, NULL
);
1414 act
= e
->action
? "_ACT" : "";
1415 switch (e
->compound
) {
1417 render_opcode(out
, "ASN1_OP_END_SEQ%s,\n", act
);
1420 render_opcode(out
, "ASN1_OP_END_SEQ_OF%s,\n", act
);
1421 render_opcode(out
, "_jump_target(%u),\n", entry
);
1424 render_opcode(out
, "ASN1_OP_END_SET%s,\n", act
);
1427 render_opcode(out
, "ASN1_OP_END_SET_OF%s,\n", act
);
1428 render_opcode(out
, "_jump_target(%u),\n", entry
);
1434 render_opcode(out
, "_action(ACT_%s),\n",
1436 render_opcode(out
, "ASN1_OP_RETURN,\n");
1441 * Render an element.
1443 static void render_element(FILE *out
, struct element
*e
, struct element
*tag
)
1445 struct element
*ec
, *x
;
1446 const char *cond
, *act
;
1447 int entry
, skippable
= 0, outofline
= 0;
1449 if (e
->flags
& ELEMENT_SKIPPABLE
||
1450 (tag
&& tag
->flags
& ELEMENT_SKIPPABLE
))
1453 if ((e
->type_def
&& e
->type_def
->ref_count
> 1) ||
1457 if (e
->type_def
&& out
) {
1458 render_more(out
, "\t// %s\n", e
->type_def
->name
->content
);
1461 /* Render the operation */
1462 cond
= (e
->flags
& ELEMENT_CONDITIONAL
||
1463 (tag
&& tag
->flags
& ELEMENT_CONDITIONAL
)) ? "COND_" : "";
1464 act
= e
->action
? "_ACT" : "";
1465 switch (e
->compound
) {
1467 render_opcode(out
, "ASN1_OP_%sMATCH_ANY%s%s,",
1468 cond
, act
, skippable
? "_OR_SKIP" : "");
1470 render_more(out
, "\t\t// %s", e
->name
->content
);
1471 render_more(out
, "\n");
1472 goto dont_render_tag
;
1475 render_element(out
, e
->children
, e
);
1482 render_opcode(out
, "ASN1_OP_%sMATCH%s%s,",
1484 outofline
? "_JUMP" : "",
1485 skippable
? "_OR_SKIP" : "");
1489 goto dont_render_tag
;
1492 if (e
->class == ASN1_UNIV
&& e
->method
== ASN1_PRIM
&& e
->tag
== 0)
1493 goto dont_render_tag
;
1495 render_opcode(out
, "ASN1_OP_%sMATCH%s%s,",
1497 skippable
? "_OR_SKIP" : "");
1503 render_more(out
, "\t\t// %s", x
->name
->content
);
1504 render_more(out
, "\n");
1506 /* Render the tag */
1507 if (!tag
|| !(tag
->flags
& ELEMENT_TAG_SPECIFIED
))
1510 if (tag
->class == ASN1_UNIV
&&
1514 render_opcode(out
, "_tag(%s, %s, %s),\n",
1515 asn1_classes
[tag
->class],
1516 asn1_methods
[tag
->method
| e
->method
],
1517 asn1_universal_tags
[tag
->tag
]);
1519 render_opcode(out
, "_tagn(%s, %s, %2u),\n",
1520 asn1_classes
[tag
->class],
1521 asn1_methods
[tag
->method
| e
->method
],
1526 /* Deal with compound types */
1527 switch (e
->compound
) {
1529 render_element(out
, e
->type
->type
->element
, tag
);
1531 render_opcode(out
, "ASN1_OP_%sACT,\n",
1532 skippable
? "MAYBE_" : "");
1537 /* Render out-of-line for multiple use or
1539 render_opcode(out
, "_jump_target(%u),", e
->entry_index
);
1540 if (e
->type_def
&& e
->type_def
->name
)
1541 render_more(out
, "\t\t// --> %s",
1542 e
->type_def
->name
->content
);
1543 render_more(out
, "\n");
1544 if (!(e
->flags
& ELEMENT_RENDERED
)) {
1545 e
->flags
|= ELEMENT_RENDERED
;
1547 render_list_p
= &e
->render_next
;
1551 /* Render inline for single use */
1553 for (ec
= e
->children
; ec
; ec
= ec
->next
)
1554 render_element(out
, ec
, NULL
);
1556 render_opcode(out
, "ASN1_OP_END_SEQ%s,\n", act
);
1563 /* Render out-of-line for multiple use or
1565 render_opcode(out
, "_jump_target(%u),", e
->entry_index
);
1566 if (e
->type_def
&& e
->type_def
->name
)
1567 render_more(out
, "\t\t// --> %s",
1568 e
->type_def
->name
->content
);
1569 render_more(out
, "\n");
1570 if (!(e
->flags
& ELEMENT_RENDERED
)) {
1571 e
->flags
|= ELEMENT_RENDERED
;
1573 render_list_p
= &e
->render_next
;
1577 /* Render inline for single use */
1580 render_element(out
, e
->children
, NULL
);
1582 if (e
->compound
== SEQUENCE_OF
)
1583 render_opcode(out
, "ASN1_OP_END_SEQ_OF%s,\n", act
);
1585 render_opcode(out
, "ASN1_OP_END_SET_OF%s,\n", act
);
1586 render_opcode(out
, "_jump_target(%u),\n", entry
);
1591 /* I can't think of a nice way to do SET support without having
1592 * a stack of bitmasks to make sure no element is repeated.
1593 * The bitmask has also to be checked that no non-optional
1594 * elements are left out whilst not preventing optional
1595 * elements from being left out.
1597 fprintf(stderr
, "The ASN.1 SET type is not currently supported.\n");
1601 for (ec
= e
->children
; ec
; ec
= ec
->next
)
1602 render_element(out
, ec
, ec
);
1604 render_opcode(out
, "ASN1_OP_COND_FAIL,\n");
1606 render_opcode(out
, "ASN1_OP_ACT,\n");
1614 render_opcode(out
, "_action(ACT_%s),\n", e
->action
->name
);