1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Simplified ASN.1 notation parser
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
18 #include <linux/asn1_ber_bytecode.h>
24 DIRECTIVE_APPLICATION
,
36 DIRECTIVE_CONSTRAINED
,
40 DIRECTIVE_DEFINITIONS
,
43 DIRECTIVE_ENCODING_CONTROL
,
49 DIRECTIVE_EXTENSIBILITY
,
53 DIRECTIVE_GeneralString
,
54 DIRECTIVE_GeneralizedTime
,
55 DIRECTIVE_GraphicString
,
63 DIRECTIVE_INSTRUCTIONS
,
65 DIRECTIVE_INTERSECTION
,
66 DIRECTIVE_ISO646String
,
69 DIRECTIVE_MINUS_INFINITY
,
71 DIRECTIVE_NumericString
,
76 DIRECTIVE_ObjectDescriptor
,
79 DIRECTIVE_PLUS_INFINITY
,
82 DIRECTIVE_PrintableString
,
84 DIRECTIVE_RELATIVE_OID
,
93 DIRECTIVE_TeletexString
,
99 DIRECTIVE_UniversalString
,
100 DIRECTIVE_VideotexString
,
101 DIRECTIVE_VisibleString
,
104 TOKEN_ASSIGNMENT
= NR__DIRECTIVES
,
118 static const unsigned char token_to_tag
[NR__TOKENS
] = {
120 [DIRECTIVE_BOOLEAN
] = ASN1_BOOL
,
121 [DIRECTIVE_INTEGER
] = ASN1_INT
,
122 [DIRECTIVE_BIT
] = ASN1_BTS
,
123 [DIRECTIVE_OCTET
] = ASN1_OTS
,
124 [DIRECTIVE_NULL
] = ASN1_NULL
,
125 [DIRECTIVE_OBJECT
] = ASN1_OID
,
126 [DIRECTIVE_ObjectDescriptor
] = ASN1_ODE
,
127 [DIRECTIVE_EXTERNAL
] = ASN1_EXT
,
128 [DIRECTIVE_REAL
] = ASN1_REAL
,
129 [DIRECTIVE_ENUMERATED
] = ASN1_ENUM
,
130 [DIRECTIVE_EMBEDDED
] = 0,
131 [DIRECTIVE_UTF8String
] = ASN1_UTF8STR
,
132 [DIRECTIVE_RELATIVE_OID
] = ASN1_RELOID
,
135 [DIRECTIVE_SEQUENCE
] = ASN1_SEQ
,
136 [DIRECTIVE_SET
] = ASN1_SET
,
137 [DIRECTIVE_NumericString
] = ASN1_NUMSTR
,
138 [DIRECTIVE_PrintableString
] = ASN1_PRNSTR
,
139 [DIRECTIVE_T61String
] = ASN1_TEXSTR
,
140 [DIRECTIVE_TeletexString
] = ASN1_TEXSTR
,
141 [DIRECTIVE_VideotexString
] = ASN1_VIDSTR
,
142 [DIRECTIVE_IA5String
] = ASN1_IA5STR
,
143 [DIRECTIVE_UTCTime
] = ASN1_UNITIM
,
144 [DIRECTIVE_GeneralizedTime
] = ASN1_GENTIM
,
145 [DIRECTIVE_GraphicString
] = ASN1_GRASTR
,
146 [DIRECTIVE_VisibleString
] = ASN1_VISSTR
,
147 [DIRECTIVE_GeneralString
] = ASN1_GENSTR
,
148 [DIRECTIVE_UniversalString
] = ASN1_UNITIM
,
149 [DIRECTIVE_CHARACTER
] = ASN1_CHRSTR
,
150 [DIRECTIVE_BMPString
] = ASN1_BMPSTR
,
153 static const char asn1_classes
[4][5] = {
154 [ASN1_UNIV
] = "UNIV",
155 [ASN1_APPL
] = "APPL",
156 [ASN1_CONT
] = "CONT",
160 static const char asn1_methods
[2][5] = {
161 [ASN1_UNIV
] = "PRIM",
165 static const char *const asn1_universal_tags
[32] = {
200 static const char *filename
;
201 static const char *grammar_name
;
202 static const char *outputname
;
203 static const char *headername
;
205 static const char *const directives
[NR__DIRECTIVES
] = {
206 #define _(X) [DIRECTIVE_##X] = #X
229 [DIRECTIVE_ENCODING_CONTROL
] = "ENCODING-CONTROL",
255 [DIRECTIVE_MINUS_INFINITY
] = "MINUS-INFINITY",
256 [DIRECTIVE_NULL
] = "NULL",
265 [DIRECTIVE_PLUS_INFINITY
] = "PLUS-INFINITY",
270 [DIRECTIVE_RELATIVE_OID
] = "RELATIVE-OID",
297 static struct action
*action_list
;
298 static unsigned nr_actions
;
302 enum token_type token_type
: 8;
304 struct action
*action
;
309 static struct token
*token_list
;
310 static unsigned nr_tokens
;
311 static bool verbose_opt
;
312 static bool debug_opt
;
314 #define verbose(fmt, ...) do { if (verbose_opt) printf(fmt, ## __VA_ARGS__); } while (0)
315 #define debug(fmt, ...) do { if (debug_opt) printf(fmt, ## __VA_ARGS__); } while (0)
317 static int directive_compare(const void *_key
, const void *_pdir
)
319 const struct token
*token
= _key
;
320 const char *const *pdir
= _pdir
, *dir
= *pdir
;
325 clen
= (dlen
< token
->size
) ? dlen
: token
->size
;
327 //debug("cmp(%s,%s) = ", token->content, dir);
329 val
= memcmp(token
->content
, dir
, clen
);
331 //debug("%d [cmp]\n", val);
335 if (dlen
== token
->size
) {
339 //debug("%d\n", (int)dlen - (int)token->size);
340 return dlen
- token
->size
; /* shorter -> negative */
344 * Tokenise an ASN.1 grammar
346 static void tokenise(char *buffer
, char *end
)
348 struct token
*tokens
;
349 char *line
, *nl
, *start
, *p
, *q
;
350 unsigned tix
, lineno
;
352 /* Assume we're going to have half as many tokens as we have
355 token_list
= tokens
= calloc((end
- buffer
) / 2, sizeof(struct token
));
363 while (buffer
< end
) {
364 /* First of all, break out a line */
367 nl
= memchr(line
, '\n', end
- buffer
);
375 /* Remove "--" comments */
378 while ((p
= memchr(p
, '-', nl
- p
))) {
380 /* Found a comment; see if there's a terminator */
382 while ((q
= memchr(q
, '-', nl
- q
))) {
384 /* There is - excise the comment */
386 memmove(p
, q
, nl
- q
);
401 /* Skip white space */
402 while (p
< nl
&& isspace(*p
))
407 tokens
[tix
].line
= lineno
;
410 /* Handle string tokens */
414 /* Can be a directive, type name or element
415 * name. Find the end of the name.
418 while (q
< nl
&& (isalnum(*q
) || *q
== '-' || *q
== '_'))
420 tokens
[tix
].size
= q
- p
;
423 tokens
[tix
].content
= malloc(tokens
[tix
].size
+ 1);
424 if (!tokens
[tix
].content
) {
428 memcpy(tokens
[tix
].content
, start
, tokens
[tix
].size
);
429 tokens
[tix
].content
[tokens
[tix
].size
] = 0;
431 /* If it begins with a lowercase letter then
432 * it's an element name
434 if (islower(tokens
[tix
].content
[0])) {
435 tokens
[tix
++].token_type
= TOKEN_ELEMENT_NAME
;
439 /* Otherwise we need to search the directive
442 dir
= bsearch(&tokens
[tix
], directives
,
443 sizeof(directives
) / sizeof(directives
[1]),
444 sizeof(directives
[1]),
447 tokens
[tix
++].token_type
= dir
- directives
;
451 tokens
[tix
++].token_type
= TOKEN_TYPE_NAME
;
457 /* Find the end of the number */
459 while (q
< nl
&& (isdigit(*q
)))
461 tokens
[tix
].size
= q
- p
;
463 tokens
[tix
].content
= malloc(tokens
[tix
].size
+ 1);
464 if (!tokens
[tix
].content
) {
468 memcpy(tokens
[tix
].content
, start
, tokens
[tix
].size
);
469 tokens
[tix
].content
[tokens
[tix
].size
] = 0;
470 tokens
[tix
++].token_type
= TOKEN_NUMBER
;
475 if (memcmp(p
, "::=", 3) == 0) {
477 tokens
[tix
].size
= 3;
478 tokens
[tix
].content
= "::=";
479 tokens
[tix
++].token_type
= TOKEN_ASSIGNMENT
;
485 if (memcmp(p
, "({", 2) == 0) {
487 tokens
[tix
].size
= 2;
488 tokens
[tix
].content
= "({";
489 tokens
[tix
++].token_type
= TOKEN_OPEN_ACTION
;
492 if (memcmp(p
, "})", 2) == 0) {
494 tokens
[tix
].size
= 2;
495 tokens
[tix
].content
= "})";
496 tokens
[tix
++].token_type
= TOKEN_CLOSE_ACTION
;
502 tokens
[tix
].size
= 1;
506 tokens
[tix
].content
= "{";
507 tokens
[tix
++].token_type
= TOKEN_OPEN_CURLY
;
511 tokens
[tix
].content
= "}";
512 tokens
[tix
++].token_type
= TOKEN_CLOSE_CURLY
;
516 tokens
[tix
].content
= "[";
517 tokens
[tix
++].token_type
= TOKEN_OPEN_SQUARE
;
521 tokens
[tix
].content
= "]";
522 tokens
[tix
++].token_type
= TOKEN_CLOSE_SQUARE
;
526 tokens
[tix
].content
= ",";
527 tokens
[tix
++].token_type
= TOKEN_COMMA
;
534 fprintf(stderr
, "%s:%u: Unknown character in grammar: '%c'\n",
535 filename
, lineno
, *p
);
541 verbose("Extracted %u tokens\n", nr_tokens
);
546 for (n
= 0; n
< nr_tokens
; n
++)
547 debug("Token %3u: '%s'\n", n
, token_list
[n
].content
);
552 static void build_type_list(void);
553 static void parse(void);
554 static void dump_elements(void);
555 static void render(FILE *out
, FILE *hdr
);
560 int main(int argc
, char **argv
)
566 char *kbuild_verbose
;
569 kbuild_verbose
= getenv("KBUILD_VERBOSE");
571 verbose_opt
= atoi(kbuild_verbose
);
574 if (strcmp(argv
[1], "-v") == 0)
576 else if (strcmp(argv
[1], "-d") == 0)
580 memmove(&argv
[1], &argv
[2], (argc
- 2) * sizeof(char *));
585 fprintf(stderr
, "Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n",
591 outputname
= argv
[2];
592 headername
= argv
[3];
594 fd
= open(filename
, O_RDONLY
);
600 if (fstat(fd
, &st
) < 0) {
605 if (!(buffer
= malloc(st
.st_size
+ 1))) {
610 if ((readlen
= read(fd
, buffer
, st
.st_size
)) < 0) {
620 if (readlen
!= st
.st_size
) {
621 fprintf(stderr
, "%s: Short read\n", filename
);
625 p
= strrchr(argv
[1], '/');
626 p
= p
? p
+ 1 : argv
[1];
627 grammar_name
= strdup(p
);
632 p
= strchr(grammar_name
, '.');
637 tokenise(buffer
, buffer
+ readlen
);
642 out
= fopen(outputname
, "w");
648 hdr
= fopen(headername
, "w");
656 if (fclose(out
) < 0) {
661 if (fclose(hdr
) < 0) {
682 struct type
*type_def
;
685 struct action
*action
;
686 struct element
*children
;
687 struct element
*next
;
688 struct element
*render_next
;
689 struct element
*list_next
;
691 enum compound compound
: 8;
692 enum asn1_class
class : 8;
693 enum asn1_method method
: 8;
695 unsigned entry_index
;
697 #define ELEMENT_IMPLICIT 0x0001
698 #define ELEMENT_EXPLICIT 0x0002
699 #define ELEMENT_TAG_SPECIFIED 0x0004
700 #define ELEMENT_RENDERED 0x0008
701 #define ELEMENT_SKIPPABLE 0x0010
702 #define ELEMENT_CONDITIONAL 0x0020
708 struct element
*element
;
711 #define TYPE_STOP_MARKER 0x0001
712 #define TYPE_BEGIN 0x0002
715 static struct type
*type_list
;
716 static struct type
**type_index
;
717 static unsigned nr_types
;
719 static int type_index_compare(const void *_a
, const void *_b
)
721 const struct type
*const *a
= _a
, *const *b
= _b
;
723 if ((*a
)->name
->size
!= (*b
)->name
->size
)
724 return (*a
)->name
->size
- (*b
)->name
->size
;
726 return memcmp((*a
)->name
->content
, (*b
)->name
->content
,
730 static int type_finder(const void *_key
, const void *_ti
)
732 const struct token
*token
= _key
;
733 const struct type
*const *ti
= _ti
;
734 const struct type
*type
= *ti
;
736 if (token
->size
!= type
->name
->size
)
737 return token
->size
- type
->name
->size
;
739 return memcmp(token
->content
, type
->name
->content
,
744 * Build up a list of types and a sorted index to that list.
746 static void build_type_list(void)
752 for (n
= 0; n
< nr_tokens
- 1; n
++)
753 if (token_list
[n
+ 0].token_type
== TOKEN_TYPE_NAME
&&
754 token_list
[n
+ 1].token_type
== TOKEN_ASSIGNMENT
)
758 fprintf(stderr
, "%s: No defined types\n", filename
);
763 types
= type_list
= calloc(nr
+ 1, sizeof(type_list
[0]));
768 type_index
= calloc(nr
, sizeof(type_index
[0]));
775 types
[t
].flags
|= TYPE_BEGIN
;
776 for (n
= 0; n
< nr_tokens
- 1; n
++) {
777 if (token_list
[n
+ 0].token_type
== TOKEN_TYPE_NAME
&&
778 token_list
[n
+ 1].token_type
== TOKEN_ASSIGNMENT
) {
779 types
[t
].name
= &token_list
[n
];
780 type_index
[t
] = &types
[t
];
784 types
[t
].name
= &token_list
[n
+ 1];
785 types
[t
].flags
|= TYPE_STOP_MARKER
;
787 qsort(type_index
, nr
, sizeof(type_index
[0]), type_index_compare
);
789 verbose("Extracted %u types\n", nr_types
);
791 for (n
= 0; n
< nr_types
; n
++) {
792 struct type
*type
= type_index
[n
];
793 debug("- %*.*s\n", type
->name
->content
);
798 static struct element
*parse_type(struct token
**_cursor
, struct token
*stop
,
802 * Parse the token stream
804 static void parse(void)
806 struct token
*cursor
;
809 /* Parse one type definition statement at a time */
814 if (cursor
[0].token_type
!= TOKEN_TYPE_NAME
||
815 cursor
[1].token_type
!= TOKEN_ASSIGNMENT
)
819 type
->element
= parse_type(&cursor
, type
[1].name
, NULL
);
820 type
->element
->type_def
= type
;
822 if (cursor
!= type
[1].name
) {
823 fprintf(stderr
, "%s:%d: Parse error at token '%s'\n",
824 filename
, cursor
->line
, cursor
->content
);
828 } while (type
++, !(type
->flags
& TYPE_STOP_MARKER
));
830 verbose("Extracted %u actions\n", nr_actions
);
833 static struct element
*element_list
;
835 static struct element
*alloc_elem(struct token
*type
)
837 struct element
*e
= calloc(1, sizeof(*e
));
842 e
->list_next
= element_list
;
847 static struct element
*parse_compound(struct token
**_cursor
, struct token
*end
,
851 * Parse one type definition statement
853 static struct element
*parse_type(struct token
**_cursor
, struct token
*end
,
856 struct element
*top
, *element
;
857 struct action
*action
, **ppaction
;
858 struct token
*cursor
= *_cursor
;
861 int labelled
= 0, implicit
= 0;
863 top
= element
= alloc_elem(cursor
);
864 element
->class = ASN1_UNIV
;
865 element
->method
= ASN1_PRIM
;
866 element
->tag
= token_to_tag
[cursor
->token_type
];
867 element
->name
= name
;
869 /* Extract the tag value if one given */
870 if (cursor
->token_type
== TOKEN_OPEN_SQUARE
) {
874 switch (cursor
->token_type
) {
875 case DIRECTIVE_UNIVERSAL
:
876 element
->class = ASN1_UNIV
;
879 case DIRECTIVE_APPLICATION
:
880 element
->class = ASN1_APPL
;
884 element
->class = ASN1_CONT
;
886 case DIRECTIVE_PRIVATE
:
887 element
->class = ASN1_PRIV
;
891 fprintf(stderr
, "%s:%d: Unrecognised tag class token '%s'\n",
892 filename
, cursor
->line
, cursor
->content
);
898 if (cursor
->token_type
!= TOKEN_NUMBER
) {
899 fprintf(stderr
, "%s:%d: Missing tag number '%s'\n",
900 filename
, cursor
->line
, cursor
->content
);
904 element
->tag
&= ~0x1f;
905 element
->tag
|= strtoul(cursor
->content
, &p
, 10);
906 element
->flags
|= ELEMENT_TAG_SPECIFIED
;
907 if (p
- cursor
->content
!= cursor
->size
)
913 if (cursor
->token_type
!= TOKEN_CLOSE_SQUARE
) {
914 fprintf(stderr
, "%s:%d: Missing closing square bracket '%s'\n",
915 filename
, cursor
->line
, cursor
->content
);
924 /* Handle implicit and explicit markers */
925 if (cursor
->token_type
== DIRECTIVE_IMPLICIT
) {
926 element
->flags
|= ELEMENT_IMPLICIT
;
931 } else if (cursor
->token_type
== DIRECTIVE_EXPLICIT
) {
932 element
->flags
|= ELEMENT_EXPLICIT
;
940 element
->method
|= ASN1_CONS
;
941 element
->compound
= implicit
? TAG_OVERRIDE
: SEQUENCE
;
942 element
->children
= alloc_elem(cursor
);
943 element
= element
->children
;
944 element
->class = ASN1_UNIV
;
945 element
->method
= ASN1_PRIM
;
946 element
->tag
= token_to_tag
[cursor
->token_type
];
947 element
->name
= name
;
950 /* Extract the type we're expecting here */
951 element
->type
= cursor
;
952 switch (cursor
->token_type
) {
954 element
->compound
= ANY
;
959 case DIRECTIVE_BOOLEAN
:
960 case DIRECTIVE_ENUMERATED
:
961 case DIRECTIVE_INTEGER
:
962 element
->compound
= NOT_COMPOUND
;
966 case DIRECTIVE_EXTERNAL
:
967 element
->method
= ASN1_CONS
;
969 case DIRECTIVE_BMPString
:
970 case DIRECTIVE_GeneralString
:
971 case DIRECTIVE_GraphicString
:
972 case DIRECTIVE_IA5String
:
973 case DIRECTIVE_ISO646String
:
974 case DIRECTIVE_NumericString
:
975 case DIRECTIVE_PrintableString
:
976 case DIRECTIVE_T61String
:
977 case DIRECTIVE_TeletexString
:
978 case DIRECTIVE_UniversalString
:
979 case DIRECTIVE_UTF8String
:
980 case DIRECTIVE_VideotexString
:
981 case DIRECTIVE_VisibleString
:
982 case DIRECTIVE_ObjectDescriptor
:
983 case DIRECTIVE_GeneralizedTime
:
984 case DIRECTIVE_UTCTime
:
985 element
->compound
= NOT_COMPOUND
;
990 case DIRECTIVE_OCTET
:
991 element
->compound
= NOT_COMPOUND
;
995 if (cursor
->token_type
!= DIRECTIVE_STRING
)
1000 case DIRECTIVE_OBJECT
:
1001 element
->compound
= NOT_COMPOUND
;
1005 if (cursor
->token_type
!= DIRECTIVE_IDENTIFIER
)
1010 case TOKEN_TYPE_NAME
:
1011 element
->compound
= TYPE_REF
;
1012 ref
= bsearch(cursor
, type_index
, nr_types
, sizeof(type_index
[0]),
1015 fprintf(stderr
, "%s:%d: Type '%s' undefined\n",
1016 filename
, cursor
->line
, cursor
->content
);
1019 cursor
->type
= *ref
;
1020 (*ref
)->ref_count
++;
1024 case DIRECTIVE_CHOICE
:
1025 element
->compound
= CHOICE
;
1027 element
->children
= parse_compound(&cursor
, end
, 1);
1030 case DIRECTIVE_SEQUENCE
:
1031 element
->compound
= SEQUENCE
;
1032 element
->method
= ASN1_CONS
;
1036 if (cursor
->token_type
== DIRECTIVE_OF
) {
1037 element
->compound
= SEQUENCE_OF
;
1041 element
->children
= parse_type(&cursor
, end
, NULL
);
1043 element
->children
= parse_compound(&cursor
, end
, 0);
1048 element
->compound
= SET
;
1049 element
->method
= ASN1_CONS
;
1053 if (cursor
->token_type
== DIRECTIVE_OF
) {
1054 element
->compound
= SET_OF
;
1058 element
->children
= parse_type(&cursor
, end
, NULL
);
1060 element
->children
= parse_compound(&cursor
, end
, 1);
1065 fprintf(stderr
, "%s:%d: Token '%s' does not introduce a type\n",
1066 filename
, cursor
->line
, cursor
->content
);
1070 /* Handle elements that are optional */
1071 if (cursor
< end
&& (cursor
->token_type
== DIRECTIVE_OPTIONAL
||
1072 cursor
->token_type
== DIRECTIVE_DEFAULT
)
1075 top
->flags
|= ELEMENT_SKIPPABLE
;
1078 if (cursor
< end
&& cursor
->token_type
== TOKEN_OPEN_ACTION
) {
1082 if (cursor
->token_type
!= TOKEN_ELEMENT_NAME
) {
1083 fprintf(stderr
, "%s:%d: Token '%s' is not an action function name\n",
1084 filename
, cursor
->line
, cursor
->content
);
1088 action
= malloc(sizeof(struct action
));
1094 action
->name
= cursor
->content
;
1096 for (ppaction
= &action_list
;
1098 ppaction
= &(*ppaction
)->next
1100 int cmp
= strcmp(action
->name
, (*ppaction
)->name
);
1107 action
->next
= *ppaction
;
1113 action
->next
= NULL
;
1118 element
->action
= action
;
1119 cursor
->action
= action
;
1123 if (cursor
->token_type
!= TOKEN_CLOSE_ACTION
) {
1124 fprintf(stderr
, "%s:%d: Missing close action, got '%s'\n",
1125 filename
, cursor
->line
, cursor
->content
);
1135 fprintf(stderr
, "%s:%d: Unexpected token '%s'\n",
1136 filename
, cursor
->line
, cursor
->content
);
1140 fprintf(stderr
, "%s: Unexpectedly hit EOF\n", filename
);
1145 * Parse a compound type list
1147 static struct element
*parse_compound(struct token
**_cursor
, struct token
*end
,
1150 struct element
*children
, **child_p
= &children
, *element
;
1151 struct token
*cursor
= *_cursor
, *name
;
1153 if (cursor
->token_type
!= TOKEN_OPEN_CURLY
) {
1154 fprintf(stderr
, "%s:%d: Expected compound to start with brace not '%s'\n",
1155 filename
, cursor
->line
, cursor
->content
);
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
, cursor
->content
);
1206 fprintf(stderr
, "%s: Unexpectedly hit EOF\n", filename
);
1210 static void dump_element(const struct element
*e
, int level
)
1212 const struct element
*c
;
1213 const struct type
*t
= e
->type_def
;
1214 const char *name
= e
->name
? e
->name
->content
: ".";
1215 const char *tname
= t
&& t
->name
? t
->name
->content
: ".";
1218 if (e
->class == 0 && e
->method
== 0 && e
->tag
== 0)
1219 strcpy(tag
, "<...>");
1220 else if (e
->class == ASN1_UNIV
)
1221 sprintf(tag
, "%s %s %s",
1222 asn1_classes
[e
->class],
1223 asn1_methods
[e
->method
],
1224 asn1_universal_tags
[e
->tag
]);
1226 sprintf(tag
, "%s %s %u",
1227 asn1_classes
[e
->class],
1228 asn1_methods
[e
->method
],
1231 printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %s %s \e[35m%s\e[m\n",
1232 e
->flags
& ELEMENT_IMPLICIT
? 'I' : '-',
1233 e
->flags
& ELEMENT_EXPLICIT
? 'E' : '-',
1234 e
->flags
& ELEMENT_TAG_SPECIFIED
? 'T' : '-',
1235 e
->flags
& ELEMENT_SKIPPABLE
? 'S' : '-',
1236 e
->flags
& ELEMENT_CONDITIONAL
? 'C' : '-',
1237 "-tTqQcaro"[e
->compound
],
1242 e
->action
? e
->action
->name
: "");
1243 if (e
->compound
== TYPE_REF
)
1244 dump_element(e
->type
->type
->element
, level
+ 3);
1246 for (c
= e
->children
; c
; c
= c
->next
)
1247 dump_element(c
, level
+ 3);
1250 static void dump_elements(void)
1253 dump_element(type_list
[0].element
, 0);
1256 static void render_element(FILE *out
, struct element
*e
, struct element
*tag
);
1257 static void render_out_of_line_list(FILE *out
);
1259 static int nr_entries
;
1260 static int render_depth
= 1;
1261 static struct element
*render_list
, **render_list_p
= &render_list
;
1263 __attribute__((format(printf
, 2, 3)))
1264 static void render_opcode(FILE *out
, const char *fmt
, ...)
1269 fprintf(out
, "\t[%4d] =%*s", nr_entries
, render_depth
, "");
1271 vfprintf(out
, fmt
, va
);
1277 __attribute__((format(printf
, 2, 3)))
1278 static void render_more(FILE *out
, const char *fmt
, ...)
1284 vfprintf(out
, fmt
, va
);
1290 * Render the grammar into a state machine definition.
1292 static void render(FILE *out
, FILE *hdr
)
1295 struct action
*action
;
1299 fprintf(hdr
, "/*\n");
1300 fprintf(hdr
, " * Automatically generated by asn1_compiler. Do not edit\n");
1301 fprintf(hdr
, " *\n");
1302 fprintf(hdr
, " * ASN.1 parser for %s\n", grammar_name
);
1303 fprintf(hdr
, " */\n");
1304 fprintf(hdr
, "#include <linux/asn1_decoder.h>\n");
1306 fprintf(hdr
, "extern const struct asn1_decoder %s_decoder;\n", grammar_name
);
1312 fprintf(out
, "/*\n");
1313 fprintf(out
, " * Automatically generated by asn1_compiler. Do not edit\n");
1314 fprintf(out
, " *\n");
1315 fprintf(out
, " * ASN.1 parser for %s\n", grammar_name
);
1316 fprintf(out
, " */\n");
1317 fprintf(out
, "#include <linux/asn1_ber_bytecode.h>\n");
1318 fprintf(out
, "#include \"%s.asn1.h\"\n", grammar_name
);
1325 /* Tabulate the action functions we might have to call */
1328 for (action
= action_list
; action
; action
= action
->next
) {
1329 action
->index
= index
++;
1331 "extern int %s(void *, size_t, unsigned char,"
1332 " const void *, size_t);\n",
1337 fprintf(out
, "enum %s_actions {\n", grammar_name
);
1338 for (action
= action_list
; action
; action
= action
->next
)
1339 fprintf(out
, "\tACT_%s = %u,\n",
1340 action
->name
, action
->index
);
1341 fprintf(out
, "\tNR__%s_actions = %u\n", grammar_name
, nr_actions
);
1342 fprintf(out
, "};\n");
1345 fprintf(out
, "static const asn1_action_t %s_action_table[NR__%s_actions] = {\n",
1346 grammar_name
, grammar_name
);
1347 for (action
= action_list
; action
; action
= action
->next
)
1348 fprintf(out
, "\t[%4u] = %s,\n", action
->index
, action
->name
);
1349 fprintf(out
, "};\n");
1356 /* We do two passes - the first one calculates all the offsets */
1357 verbose("Pass 1\n");
1359 root
= &type_list
[0];
1360 render_element(NULL
, root
->element
, NULL
);
1361 render_opcode(NULL
, "ASN1_OP_COMPLETE,\n");
1362 render_out_of_line_list(NULL
);
1364 for (e
= element_list
; e
; e
= e
->list_next
)
1365 e
->flags
&= ~ELEMENT_RENDERED
;
1367 /* And then we actually render */
1368 verbose("Pass 2\n");
1370 fprintf(out
, "static const unsigned char %s_machine[] = {\n",
1374 root
= &type_list
[0];
1375 render_element(out
, root
->element
, NULL
);
1376 render_opcode(out
, "ASN1_OP_COMPLETE,\n");
1377 render_out_of_line_list(out
);
1379 fprintf(out
, "};\n");
1382 fprintf(out
, "const struct asn1_decoder %s_decoder = {\n", grammar_name
);
1383 fprintf(out
, "\t.machine = %s_machine,\n", grammar_name
);
1384 fprintf(out
, "\t.machlen = sizeof(%s_machine),\n", grammar_name
);
1385 fprintf(out
, "\t.actions = %s_action_table,\n", grammar_name
);
1386 fprintf(out
, "};\n");
1390 * Render the out-of-line elements
1392 static void render_out_of_line_list(FILE *out
)
1394 struct element
*e
, *ce
;
1398 while ((e
= render_list
)) {
1399 render_list
= e
->render_next
;
1401 render_list_p
= &render_list
;
1403 render_more(out
, "\n");
1404 e
->entry_index
= entry
= nr_entries
;
1406 for (ce
= e
->children
; ce
; ce
= ce
->next
)
1407 render_element(out
, ce
, NULL
);
1410 act
= e
->action
? "_ACT" : "";
1411 switch (e
->compound
) {
1413 render_opcode(out
, "ASN1_OP_END_SEQ%s,\n", act
);
1416 render_opcode(out
, "ASN1_OP_END_SEQ_OF%s,\n", act
);
1417 render_opcode(out
, "_jump_target(%u),\n", entry
);
1420 render_opcode(out
, "ASN1_OP_END_SET%s,\n", act
);
1423 render_opcode(out
, "ASN1_OP_END_SET_OF%s,\n", act
);
1424 render_opcode(out
, "_jump_target(%u),\n", entry
);
1430 render_opcode(out
, "_action(ACT_%s),\n",
1432 render_opcode(out
, "ASN1_OP_RETURN,\n");
1437 * Render an element.
1439 static void render_element(FILE *out
, struct element
*e
, struct element
*tag
)
1441 struct element
*ec
, *x
;
1442 const char *cond
, *act
;
1443 int entry
, skippable
= 0, outofline
= 0;
1445 if (e
->flags
& ELEMENT_SKIPPABLE
||
1446 (tag
&& tag
->flags
& ELEMENT_SKIPPABLE
))
1449 if ((e
->type_def
&& e
->type_def
->ref_count
> 1) ||
1453 if (e
->type_def
&& out
) {
1454 render_more(out
, "\t// %s\n", e
->type_def
->name
->content
);
1457 /* Render the operation */
1458 cond
= (e
->flags
& ELEMENT_CONDITIONAL
||
1459 (tag
&& tag
->flags
& ELEMENT_CONDITIONAL
)) ? "COND_" : "";
1460 act
= e
->action
? "_ACT" : "";
1461 switch (e
->compound
) {
1463 render_opcode(out
, "ASN1_OP_%sMATCH_ANY%s%s,",
1464 cond
, act
, skippable
? "_OR_SKIP" : "");
1466 render_more(out
, "\t\t// %s", e
->name
->content
);
1467 render_more(out
, "\n");
1468 goto dont_render_tag
;
1471 render_element(out
, e
->children
, e
);
1478 render_opcode(out
, "ASN1_OP_%sMATCH%s%s,",
1480 outofline
? "_JUMP" : "",
1481 skippable
? "_OR_SKIP" : "");
1485 goto dont_render_tag
;
1488 if (e
->class == ASN1_UNIV
&& e
->method
== ASN1_PRIM
&& e
->tag
== 0)
1489 goto dont_render_tag
;
1491 render_opcode(out
, "ASN1_OP_%sMATCH%s%s,",
1493 skippable
? "_OR_SKIP" : "");
1499 render_more(out
, "\t\t// %s", x
->name
->content
);
1500 render_more(out
, "\n");
1502 /* Render the tag */
1503 if (!tag
|| !(tag
->flags
& ELEMENT_TAG_SPECIFIED
))
1506 if (tag
->class == ASN1_UNIV
&&
1510 render_opcode(out
, "_tag(%s, %s, %s),\n",
1511 asn1_classes
[tag
->class],
1512 asn1_methods
[tag
->method
| e
->method
],
1513 asn1_universal_tags
[tag
->tag
]);
1515 render_opcode(out
, "_tagn(%s, %s, %2u),\n",
1516 asn1_classes
[tag
->class],
1517 asn1_methods
[tag
->method
| e
->method
],
1522 /* Deal with compound types */
1523 switch (e
->compound
) {
1525 render_element(out
, e
->type
->type
->element
, tag
);
1527 render_opcode(out
, "ASN1_OP_%sACT,\n",
1528 skippable
? "MAYBE_" : "");
1533 /* Render out-of-line for multiple use or
1535 render_opcode(out
, "_jump_target(%u),", e
->entry_index
);
1536 if (e
->type_def
&& e
->type_def
->name
)
1537 render_more(out
, "\t\t// --> %s",
1538 e
->type_def
->name
->content
);
1539 render_more(out
, "\n");
1540 if (!(e
->flags
& ELEMENT_RENDERED
)) {
1541 e
->flags
|= ELEMENT_RENDERED
;
1543 render_list_p
= &e
->render_next
;
1547 /* Render inline for single use */
1549 for (ec
= e
->children
; ec
; ec
= ec
->next
)
1550 render_element(out
, ec
, NULL
);
1552 render_opcode(out
, "ASN1_OP_END_SEQ%s,\n", act
);
1559 /* Render out-of-line for multiple use or
1561 render_opcode(out
, "_jump_target(%u),", e
->entry_index
);
1562 if (e
->type_def
&& e
->type_def
->name
)
1563 render_more(out
, "\t\t// --> %s",
1564 e
->type_def
->name
->content
);
1565 render_more(out
, "\n");
1566 if (!(e
->flags
& ELEMENT_RENDERED
)) {
1567 e
->flags
|= ELEMENT_RENDERED
;
1569 render_list_p
= &e
->render_next
;
1573 /* Render inline for single use */
1576 render_element(out
, e
->children
, NULL
);
1578 if (e
->compound
== SEQUENCE_OF
)
1579 render_opcode(out
, "ASN1_OP_END_SEQ_OF%s,\n", act
);
1581 render_opcode(out
, "ASN1_OP_END_SET_OF%s,\n", act
);
1582 render_opcode(out
, "_jump_target(%u),\n", entry
);
1587 /* I can't think of a nice way to do SET support without having
1588 * a stack of bitmasks to make sure no element is repeated.
1589 * The bitmask has also to be checked that no non-optional
1590 * elements are left out whilst not preventing optional
1591 * elements from being left out.
1593 fprintf(stderr
, "The ASN.1 SET type is not currently supported.\n");
1597 for (ec
= e
->children
; ec
; ec
= ec
->next
)
1598 render_element(out
, ec
, ec
);
1600 render_opcode(out
, "ASN1_OP_COND_FAIL,\n");
1602 render_opcode(out
, "ASN1_OP_ACT,\n");
1610 render_opcode(out
, "_action(ACT_%s),\n", e
->action
->name
);