Fix copyright.
[libtasn1.git] / lib / ASN1.y
blob4e93f90dfd684ac1d462416964cb92659175ff5b
1 /*
2 * Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3 * Copyright (C) 2001, 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * The LIBTASN1 library is free software; you can redistribute it
8 * and/or modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: x509_ASN.y */
26 /* Description: input file for 'bison' program. */
27 /* The output file is a parser (in C language) for */
28 /* ASN.1 syntax */
29 /*****************************************************/
33 #include <int.h>
34 #include <errors.h>
35 #include <parser_aux.h>
36 #include <structure.h>
39 static FILE *file_asn1; /* Pointer to file to parse */
40 static asn1_retCode result_parse; /* result of the parser
41 algorithm */
42 static node_asn *p_tree; /* pointer to the root of the
43 structure created by the
44 parser*/
45 static unsigned long lineNumber; /* line number describing the
46 parser position inside the
47 file */
48 static char lastToken[MAX_NAME_SIZE+1]; /* last token find in the file
49 to parse before the 'parse
50 error' */
51 extern char _asn1_identifierMissing[];
52 static const char *fileName; /* file to parse */
54 int _asn1_yyerror (char *);
55 int _asn1_yylex(void);
59 /* Prefix symbols and functions with _asn1_ */
60 %name-prefix="_asn1_yy"
62 %union {
63 unsigned int constant;
64 char str[MAX_NAME_SIZE+1];
65 node_asn* node;
69 %token ASSIG "::="
70 %token <str> NUM
71 %token <str> IDENTIFIER
72 %token OPTIONAL
73 %token INTEGER
74 %token SIZE
75 %token OCTET
76 %token STRING
77 %token SEQUENCE
78 %token BIT
79 %token UNIVERSAL
80 %token PRIVATE
81 %token APPLICATION
82 %token OPTIONAL
83 %token DEFAULT
84 %token CHOICE
85 %token OF
86 %token OBJECT
87 %token STR_IDENTIFIER
88 %token BOOLEAN
89 %token TRUE
90 %token FALSE
91 %token TOKEN_NULL
92 %token ANY
93 %token DEFINED
94 %token BY
95 %token SET
96 %token EXPLICIT
97 %token IMPLICIT
98 %token DEFINITIONS
99 %token TAGS
100 %token BEGIN
101 %token END
102 %token UTCTime
103 %token GeneralizedTime
104 %token GeneralString
105 %token FROM
106 %token IMPORTS
107 %token ENUMERATED
109 %type <node> octet_string_def constant constant_list type_assig_right
110 %type <node> integer_def type_assig type_assig_list sequence_def type_def
111 %type <node> bit_string_def default size_def choise_def object_def
112 %type <node> boolean_def any_def size_def2 obj_constant obj_constant_list
113 %type <node> constant_def type_constant type_constant_list definitions
114 %type <node> definitions_id Time bit_element bit_element_list set_def
115 %type <node> tag_type tag type_assig_right_tag generalstring_def
116 %type <node> type_assig_right_tag_default enumerated_def
117 %type <str> pos_num neg_num pos_neg_num pos_neg_identifier pos_neg_list
118 %type <str> num_identifier
119 %type <constant> class explicit_implicit
124 definitions: definitions_id
125 DEFINITIONS explicit_implicit TAGS "::=" BEGIN /* imports_def */
126 type_constant_list END
127 {$$=_asn1_add_node(TYPE_DEFINITIONS|$3);
128 _asn1_set_name($$,_asn1_get_name($1));
129 _asn1_set_name($1,"");
130 _asn1_set_right($1,$7);
131 _asn1_set_down($$,$1);
133 p_tree=$$;
137 pos_num : NUM {strcpy($$,$1);}
138 | '+' NUM {strcpy($$,$2);}
141 neg_num : '-' NUM {strcpy($$,"-");
142 strcat($$,$2);}
145 pos_neg_num : pos_num {strcpy($$,$1);}
146 | neg_num {strcpy($$,$1);}
149 num_identifier : NUM {strcpy($$,$1);}
150 | IDENTIFIER {strcpy($$,$1);}
153 pos_neg_identifier : pos_neg_num {strcpy($$,$1);}
154 | IDENTIFIER {strcpy($$,$1);}
157 constant: '(' pos_neg_num ')' {$$=_asn1_add_node(TYPE_CONSTANT);
158 _asn1_set_value($$,$2,strlen($2)+1);}
159 | IDENTIFIER'('pos_neg_num')' {$$=_asn1_add_node(TYPE_CONSTANT);
160 _asn1_set_name($$,$1);
161 _asn1_set_value($$,$3,strlen($3)+1);}
164 constant_list: constant {$$=$1;}
165 | constant_list ',' constant {$$=$1;
166 _asn1_set_right(_asn1_get_last_right($1),$3);}
169 obj_constant: num_identifier {$$=_asn1_add_node(TYPE_CONSTANT);
170 _asn1_set_value($$,$1,strlen($1)+1);}
171 | IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
172 _asn1_set_name($$,$1);
173 _asn1_set_value($$,$3,strlen($3)+1);}
176 obj_constant_list: obj_constant {$$=$1;}
177 | obj_constant_list obj_constant {$$=$1;
178 _asn1_set_right(_asn1_get_last_right($1),$2);}
181 class : UNIVERSAL {$$=CONST_UNIVERSAL;}
182 | PRIVATE {$$=CONST_PRIVATE;}
183 | APPLICATION {$$=CONST_APPLICATION;}
186 tag_type : '[' NUM ']' {$$=_asn1_add_node(TYPE_TAG);
187 _asn1_set_value($$,$2,strlen($2)+1);}
188 | '[' class NUM ']' {$$=_asn1_add_node(TYPE_TAG | $2);
189 _asn1_set_value($$,$3,strlen($3)+1);}
192 tag : tag_type {$$=$1;}
193 | tag_type EXPLICIT {$$=_asn1_mod_type($1,CONST_EXPLICIT);}
194 | tag_type IMPLICIT {$$=_asn1_mod_type($1,CONST_IMPLICIT);}
197 default : DEFAULT pos_neg_identifier {$$=_asn1_add_node(TYPE_DEFAULT);
198 _asn1_set_value($$,$2,strlen($2)+1);}
199 | DEFAULT TRUE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_TRUE);}
200 | DEFAULT FALSE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_FALSE);}
204 pos_neg_list: pos_neg_num
205 | pos_neg_list '|' pos_neg_num
209 integer_def: INTEGER {$$=_asn1_add_node(TYPE_INTEGER);}
210 | INTEGER'{'constant_list'}' {$$=_asn1_add_node(TYPE_INTEGER|CONST_LIST);
211 _asn1_set_down($$,$3);}
212 | INTEGER'(' pos_neg_list ')' {$$=_asn1_add_node(TYPE_INTEGER);}
213 | INTEGER'('num_identifier'.''.'num_identifier')'
214 {$$=_asn1_add_node(TYPE_INTEGER|CONST_MIN_MAX);
215 _asn1_set_down($$,_asn1_add_node(TYPE_SIZE));
216 _asn1_set_value(_asn1_get_down($$),$6,strlen($6)+1);
217 _asn1_set_name(_asn1_get_down($$),$3);}
220 boolean_def: BOOLEAN {$$=_asn1_add_node(TYPE_BOOLEAN);}
223 Time: UTCTime {$$=_asn1_add_node(TYPE_TIME|CONST_UTC);}
224 | GeneralizedTime {$$=_asn1_add_node(TYPE_TIME|CONST_GENERALIZED);}
227 size_def2: SIZE'('num_identifier')' {$$=_asn1_add_node(TYPE_SIZE|CONST_1_PARAM);
228 _asn1_set_value($$,$3,strlen($3)+1);}
229 | SIZE'('num_identifier'.''.'num_identifier')'
230 {$$=_asn1_add_node(TYPE_SIZE|CONST_MIN_MAX);
231 _asn1_set_value($$,$3,strlen($3)+1);
232 _asn1_set_name($$,$6);}
235 size_def: size_def2 {$$=$1;}
236 | '(' size_def2 ')' {$$=$2;}
239 generalstring_def: GeneralString {$$=_asn1_add_node(TYPE_GENERALSTRING);}
240 | GeneralString size_def {$$=_asn1_add_node(TYPE_GENERALSTRING|CONST_SIZE);
241 _asn1_set_down($$,$2);}
244 octet_string_def : OCTET STRING {$$=_asn1_add_node(TYPE_OCTET_STRING);}
245 | OCTET STRING size_def {$$=_asn1_add_node(TYPE_OCTET_STRING|CONST_SIZE);
246 _asn1_set_down($$,$3);}
249 bit_element : IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
250 _asn1_set_name($$,$1);
251 _asn1_set_value($$,$3,strlen($3)+1);}
254 bit_element_list : bit_element {$$=$1;}
255 | bit_element_list ',' bit_element {$$=$1;
256 _asn1_set_right(_asn1_get_last_right($1),$3);}
259 bit_string_def : BIT STRING {$$=_asn1_add_node(TYPE_BIT_STRING);}
260 | BIT STRING'{'bit_element_list'}'
261 {$$=_asn1_add_node(TYPE_BIT_STRING|CONST_LIST);
262 _asn1_set_down($$,$4);}
265 enumerated_def : ENUMERATED'{'bit_element_list'}'
266 {$$=_asn1_add_node(TYPE_ENUMERATED|CONST_LIST);
267 _asn1_set_down($$,$3);}
271 object_def : OBJECT STR_IDENTIFIER {$$=_asn1_add_node(TYPE_OBJECT_ID);}
274 type_assig_right: IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
275 _asn1_set_value($$,$1,strlen($1)+1);}
276 | IDENTIFIER size_def {$$=_asn1_add_node(TYPE_IDENTIFIER|CONST_SIZE);
277 _asn1_set_value($$,$1,strlen($1)+1);
278 _asn1_set_down($$,$2);}
279 | integer_def {$$=$1;}
280 | enumerated_def {$$=$1;}
281 | boolean_def {$$=$1;}
282 | Time
283 | octet_string_def {$$=$1;}
284 | bit_string_def {$$=$1;}
285 | generalstring_def {$$=$1;}
286 | sequence_def {$$=$1;}
287 | object_def {$$=$1;}
288 | choise_def {$$=$1;}
289 | any_def {$$=$1;}
290 | set_def {$$=$1;}
291 | TOKEN_NULL {$$=_asn1_add_node(TYPE_NULL);}
294 type_assig_right_tag : type_assig_right {$$=$1;}
295 | tag type_assig_right {$$=_asn1_mod_type($2,CONST_TAG);
296 _asn1_set_right($1,_asn1_get_down($$));
297 _asn1_set_down($$,$1);}
300 type_assig_right_tag_default : type_assig_right_tag {$$=$1;}
301 | type_assig_right_tag default {$$=_asn1_mod_type($1,CONST_DEFAULT);
302 _asn1_set_right($2,_asn1_get_down($$));
303 _asn1_set_down($$,$2);}
304 | type_assig_right_tag OPTIONAL {$$=_asn1_mod_type($1,CONST_OPTION);}
307 type_assig : IDENTIFIER type_assig_right_tag_default {$$=_asn1_set_name($2,$1);}
310 type_assig_list : type_assig {$$=$1;}
311 | type_assig_list','type_assig {$$=$1;
312 _asn1_set_right(_asn1_get_last_right($1),$3);}
315 sequence_def : SEQUENCE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SEQUENCE);
316 _asn1_set_down($$,$3);}
317 | SEQUENCE OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF);
318 _asn1_set_down($$,$3);}
319 | SEQUENCE size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF|CONST_SIZE);
320 _asn1_set_right($2,$4);
321 _asn1_set_down($$,$2);}
324 set_def : SET'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SET);
325 _asn1_set_down($$,$3);}
326 | SET OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF);
327 _asn1_set_down($$,$3);}
328 | SET size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF|CONST_SIZE);
329 _asn1_set_right($2,$4);
330 _asn1_set_down($$,$2);}
333 choise_def : CHOICE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_CHOICE);
334 _asn1_set_down($$,$3);}
337 any_def : ANY {$$=_asn1_add_node(TYPE_ANY);}
338 | ANY DEFINED BY IDENTIFIER {$$=_asn1_add_node(TYPE_ANY|CONST_DEFINED_BY);
339 _asn1_set_down($$,_asn1_add_node(TYPE_CONSTANT));
340 _asn1_set_name(_asn1_get_down($$),$4);}
343 type_def : IDENTIFIER "::=" type_assig_right_tag {$$=_asn1_set_name($3,$1);}
346 constant_def : IDENTIFIER OBJECT STR_IDENTIFIER "::=" '{'obj_constant_list'}'
347 {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN);
348 _asn1_set_name($$,$1);
349 _asn1_set_down($$,$6);}
350 | IDENTIFIER IDENTIFIER "::=" '{' obj_constant_list '}'
351 {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN|CONST_1_PARAM);
352 _asn1_set_name($$,$1);
353 _asn1_set_value($$,$2,strlen($2)+1);
354 _asn1_set_down($$,$5);}
355 | IDENTIFIER INTEGER "::=" pos_neg_num
356 {$$=_asn1_add_node(TYPE_INTEGER|CONST_ASSIGN);
357 _asn1_set_name($$,$1);
358 _asn1_set_value($$,$4,strlen($4)+1);}
361 type_constant: type_def {$$=$1;}
362 | constant_def {$$=$1;}
365 type_constant_list : type_constant {$$=$1;}
366 | type_constant_list type_constant {$$=$1;
367 _asn1_set_right(_asn1_get_last_right($1),$2);}
370 definitions_id : IDENTIFIER '{' obj_constant_list '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
371 _asn1_set_down($$,$3);
372 _asn1_set_name($$,$1);}
373 | IDENTIFIER '{' '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
374 _asn1_set_name($$,$1);}
378 identifier_list : IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
379 _asn1_set_name($$,$1);}
380 | identifier_list IDENTIFIER
381 {$$=$1;
382 _asn1_set_right(_asn1_get_last_right($$),_asn1_add_node(TYPE_IDENTIFIER));
383 _asn1_set_name(_asn1_get_last_right($$),$2);}
387 imports_def : empty {$$=NULL;}
388 | IMPORTS identifier_list FROM IDENTIFIER obj_constant_list
389 {$$=_asn1_add_node(TYPE_IMPORTS);
390 _asn1_set_down($$,_asn1_add_node(TYPE_OBJECT_ID));
391 _asn1_set_name(_asn1_get_down($$),$4);
392 _asn1_set_down(_asn1_get_down($$),$5);
393 _asn1_set_right($$,$2);}
397 explicit_implicit : EXPLICIT {$$=CONST_EXPLICIT;}
398 | IMPLICIT {$$=CONST_IMPLICIT;}
406 const char *key_word[]={"::=","OPTIONAL","INTEGER","SIZE","OCTET","STRING"
407 ,"SEQUENCE","BIT","UNIVERSAL","PRIVATE","OPTIONAL"
408 ,"DEFAULT","CHOICE","OF","OBJECT","IDENTIFIER"
409 ,"BOOLEAN","TRUE","FALSE","APPLICATION","ANY","DEFINED"
410 ,"SET","BY","EXPLICIT","IMPLICIT","DEFINITIONS","TAGS"
411 ,"BEGIN","END","UTCTime","GeneralizedTime"
412 ,"GeneralString","FROM","IMPORTS","NULL","ENUMERATED"};
413 const int key_word_token[]={ASSIG,OPTIONAL,INTEGER,SIZE,OCTET,STRING
414 ,SEQUENCE,BIT,UNIVERSAL,PRIVATE,OPTIONAL
415 ,DEFAULT,CHOICE,OF,OBJECT,STR_IDENTIFIER
416 ,BOOLEAN,TRUE,FALSE,APPLICATION,ANY,DEFINED
417 ,SET,BY,EXPLICIT,IMPLICIT,DEFINITIONS,TAGS
418 ,BEGIN,END,UTCTime,GeneralizedTime
419 ,GeneralString,FROM,IMPORTS,TOKEN_NULL,ENUMERATED};
421 /*************************************************************/
422 /* Function: _asn1_yylex */
423 /* Description: looks for tokens in file_asn1 pointer file. */
424 /* Return: int */
425 /* Token identifier or ASCII code or 0(zero: End Of File) */
426 /*************************************************************/
428 _asn1_yylex()
430 int c,counter=0,k,lastc;
431 char string[MAX_NAME_SIZE+1]; /* will contain the next token */
433 while(1)
435 while((c=fgetc(file_asn1))==' ' || c=='\t' || c=='\n')
436 if(c=='\n') lineNumber++;
438 if(c==EOF){
439 strcpy(lastToken,"End Of File");
440 return 0;
443 if(c=='(' || c==')' || c=='[' || c==']' ||
444 c=='{' || c=='}' || c==',' || c=='.' ||
445 c=='+' || c=='|'){
446 lastToken[0]=c;lastToken[1]=0;
447 return c;
449 if(c=='-'){ /* Maybe the first '-' of a comment */
450 if((c=fgetc(file_asn1))!='-'){
451 ungetc(c,file_asn1);
452 lastToken[0]='-';lastToken[1]=0;
453 return '-';
455 else{ /* Comments */
456 lastc=0;
457 counter=0;
458 /* A comment finishes at the next double hypen or the end of line */
459 while((c=fgetc(file_asn1))!=EOF && c!='\n' &&
460 (lastc!='-' || (lastc=='-' && c!='-')))
461 lastc=c;
462 if(c==EOF){
463 strcpy(lastToken,"End Of File");
464 return 0;
466 else{
467 if(c=='\n') lineNumber++;
468 continue; /* next char, please! (repeat the search) */
472 string[counter++]=c;
473 /* Till the end of the token */
474 while(!((c=fgetc(file_asn1))==EOF || c==' '|| c=='\t' || c=='\n' ||
475 c=='(' || c==')' || c=='[' || c==']' ||
476 c=='{' || c=='}' || c==',' || c=='.'))
478 if(counter>=MAX_NAME_SIZE){
479 result_parse=ASN1_NAME_TOO_LONG;
480 return 0;
482 string[counter++]=c;
484 ungetc(c,file_asn1);
485 string[counter]=0;
486 strcpy(lastToken,string);
488 /* Is STRING a number? */
489 for(k=0;k<counter;k++)
490 if(!isdigit(string[k])) break;
491 if(k>=counter)
493 strcpy(yylval.str,string);
494 return NUM; /* return the number */
497 /* Is STRING a keyword? */
498 for(k=0;k<(sizeof(key_word)/sizeof(char*));k++)
499 if(!strcmp(string,key_word[k])) return key_word_token[k];
501 /* STRING is an IDENTIFIER */
502 strcpy(yylval.str,string);
503 return IDENTIFIER;
508 /*************************************************************/
509 /* Function: _asn1_create_errorDescription */
510 /* Description: creates a string with the description of the*/
511 /* error. */
512 /* Parameters: */
513 /* error : error to describe. */
514 /* errorDescription: string that will contain the */
515 /* description. */
516 /*************************************************************/
517 void
518 _asn1_create_errorDescription(int error,char *errorDescription)
520 switch(error){
521 case ASN1_SUCCESS: case ASN1_FILE_NOT_FOUND:
522 if (errorDescription!=NULL) errorDescription[0]=0;
523 break;
524 case ASN1_SYNTAX_ERROR:
525 if (errorDescription!=NULL) {
526 strcpy(errorDescription,fileName);
527 strcat(errorDescription,":");
528 _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
529 strcat(errorDescription,": parse error near '");
530 strcat(errorDescription,lastToken);
531 strcat(errorDescription,"'");
533 break;
534 case ASN1_NAME_TOO_LONG:
535 if (errorDescription!=NULL) {
536 strcpy(errorDescription,fileName);
537 strcat(errorDescription,":");
538 _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
539 strcat(errorDescription,": name too long (more than ");
540 _asn1_ltostr(MAX_NAME_SIZE,errorDescription+strlen(errorDescription));
541 strcat(errorDescription," characters)");
543 break;
544 case ASN1_IDENTIFIER_NOT_FOUND:
545 if (errorDescription!=NULL) {
546 strcpy(errorDescription,fileName);
547 strcat(errorDescription,":");
548 strcat(errorDescription,": identifier '");
549 strcat(errorDescription,_asn1_identifierMissing);
550 strcat(errorDescription,"' not found");
552 break;
553 default:
554 if (errorDescription!=NULL) errorDescription[0]=0;
555 break;
562 * asn1_parser2tree - function used to start the parse algorithm.
563 * @file_name: specify the path and the name of file that contains
564 * ASN.1 declarations.
565 * @definitions: return the pointer to the structure created from
566 * "file_name" ASN.1 declarations.
567 * @errorDescription: return the error description or an empty
568 * string if success.
570 * Creates the structures needed to manage the definitions included
571 * in *FILE_NAME file.
573 * Returns:
575 * ASN1_SUCCESS: The file has a correct syntax and every identifier
576 * is known.
578 * ASN1_ELEMENT_NOT_EMPTY: *POINTER not ASN1_TYPE_EMPTY.
580 * ASN1_FILE_NOT_FOUND: An error occured while opening FILE_NAME.
582 * ASN1_SYNTAX_ERROR: The syntax is not correct.
584 * ASN1_IDENTIFIER_NOT_FOUND: In the file there is an identifier that
585 * is not defined.
587 * ASN1_NAME_TOO_LONG: In the file there is an identifier whith more
588 * than MAX_NAME_SIZE characters.
590 asn1_retCode
591 asn1_parser2tree(const char *file_name, ASN1_TYPE *definitions,
592 char *errorDescription){
594 p_tree=ASN1_TYPE_EMPTY;
596 if(*definitions != ASN1_TYPE_EMPTY)
597 return ASN1_ELEMENT_NOT_EMPTY;
599 *definitions=ASN1_TYPE_EMPTY;
601 fileName = file_name;
603 /* open the file to parse */
604 file_asn1=fopen(file_name,"r");
606 if(file_asn1==NULL){
607 result_parse=ASN1_FILE_NOT_FOUND;
609 else{
610 result_parse=ASN1_SUCCESS;
612 lineNumber=1;
613 yyparse();
615 fclose(file_asn1);
617 if(result_parse==ASN1_SUCCESS){ /* syntax OK */
618 /* set IMPLICIT or EXPLICIT property */
619 _asn1_set_default_tag(p_tree);
620 /* set CONST_SET and CONST_NOT_USED */
621 _asn1_type_set_config(p_tree);
622 /* check the identifier definitions */
623 result_parse=_asn1_check_identifier(p_tree);
624 if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
625 /* Delete the list and keep the ASN1 structure */
626 _asn1_delete_list();
627 /* Convert into DER coding the value assign to INTEGER constants */
628 _asn1_change_integer_value(p_tree);
629 /* Expand the IDs of OBJECT IDENTIFIER constants */
630 _asn1_expand_object_id(p_tree);
632 *definitions=p_tree;
634 else /* some identifiers not defined */
635 /* Delete the list and the ASN1 structure */
636 _asn1_delete_list_and_nodes();
638 else /* syntax error */
639 /* Delete the list and the ASN1 structure */
640 _asn1_delete_list_and_nodes();
643 if (errorDescription!=NULL)
644 _asn1_create_errorDescription(result_parse,errorDescription);
646 return result_parse;
651 * asn1_parser2array - function that generates a C structure from an ASN1 file
652 * @inputFileName: specify the path and the name of file that
653 * contains ASN.1 declarations.
654 * @outputFileName: specify the path and the name of file that will
655 * contain the C vector definition.
656 * @vectorName: specify the name of the C vector.
657 * @errorDescription : return the error description or an empty
658 * string if success.
660 * Creates a file containing a C vector to use to manage the
661 * definitions included in *INPUTFILENAME file. If *INPUTFILENAME is
662 * "/aa/bb/xx.yy" and OUTPUTFILENAME is NULL, the file created is
663 * "/aa/bb/xx_asn1_tab.c". If VECTORNAME is NULL the vector name
664 * will be "xx_asn1_tab".
666 * Returns:
668 * ASN1_SUCCESS: The file has a correct syntax and every identifier
669 * is known.
671 * ASN1_FILE_NOT_FOUND: An error occured while opening FILE_NAME.
673 * ASN1_SYNTAX_ERROR: The syntax is not correct.
675 * ASN1_IDENTIFIER_NOT_FOUND: In the file there is an identifier that
676 * is not defined.
678 * ASN1_NAME_TOO_LONG: In the file there is an identifier whith more
679 * than MAX_NAME_SIZE characters.
681 int asn1_parser2array(const char *inputFileName,const char *outputFileName,
682 const char *vectorName,char *errorDescription){
683 char *file_out_name=NULL;
684 char *vector_name=NULL;
685 const char *char_p,*slash_p,*dot_p;
687 p_tree=NULL;
689 fileName = inputFileName;
691 /* open the file to parse */
692 file_asn1=fopen(inputFileName,"r");
694 if(file_asn1==NULL)
695 result_parse=ASN1_FILE_NOT_FOUND;
696 else{
697 result_parse=ASN1_SUCCESS;
699 lineNumber=1;
700 yyparse();
702 fclose(file_asn1);
704 if(result_parse==ASN1_SUCCESS){ /* syntax OK */
705 /* set IMPLICIT or EXPLICIT property */
706 _asn1_set_default_tag(p_tree);
707 /* set CONST_SET and CONST_NOT_USED */
708 _asn1_type_set_config(p_tree);
709 /* check the identifier definitions */
710 result_parse=_asn1_check_identifier(p_tree);
712 if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
714 /* searching the last '/' and '.' in inputFileName */
715 char_p=inputFileName;
716 slash_p=inputFileName;
717 while((char_p=strchr(char_p,'/'))){
718 char_p++;
719 slash_p=char_p;
722 char_p=slash_p;
723 dot_p=inputFileName+strlen(inputFileName);
725 while((char_p=strchr(char_p,'.'))){
726 dot_p=char_p;
727 char_p++;
730 if(outputFileName == NULL){
731 /* file_out_name = inputFileName + _asn1_tab.c */
732 file_out_name=(char *)malloc(dot_p-inputFileName+1+
733 strlen("_asn1_tab.c"));
734 memcpy(file_out_name,inputFileName,dot_p-inputFileName);
735 file_out_name[dot_p-inputFileName]=0;
736 strcat(file_out_name,"_asn1_tab.c");
738 else{
739 /* file_out_name = inputFileName */
740 file_out_name=(char *)malloc(strlen(outputFileName)+1);
741 strcpy(file_out_name,outputFileName);
744 if(vectorName == NULL){
745 /* vector_name = file name + _asn1_tab */
746 vector_name=(char *)malloc(dot_p-slash_p+1+
747 strlen("_asn1_tab"));
748 memcpy(vector_name,slash_p,dot_p-slash_p);
749 vector_name[dot_p-slash_p]=0;
750 strcat(vector_name,"_asn1_tab");
752 else{
753 /* vector_name = vectorName */
754 vector_name=(char *)malloc(strlen(vectorName)+1);
755 strcpy(vector_name,vectorName);
758 /* Save structure in a file */
759 _asn1_create_static_structure(p_tree,
760 file_out_name,vector_name);
762 free(file_out_name);
763 free(vector_name);
764 } /* result == OK */
765 } /* result == OK */
767 /* Delete the list and the ASN1 structure */
768 _asn1_delete_list_and_nodes();
769 } /* inputFile exist */
771 if (errorDescription!=NULL)
772 _asn1_create_errorDescription(result_parse,errorDescription);
774 return result_parse;
778 /*************************************************************/
779 /* Function: _asn1_yyerror */
780 /* Description: function called when there are syntax errors*/
781 /* Parameters: */
782 /* char *s : error description */
783 /* Return: int */
784 /* */
785 /*************************************************************/
786 int _asn1_yyerror (char *s)
788 /* Sends the error description to the std_out */
790 #ifdef LIBTASN1_DEBUG_PARSER
791 _libtasn1_log("_asn1_yyerror:%s:%d: %s (Last Token:'%s')\n",fileName,
792 lineNumber,s,lastToken);
793 #endif
795 if(result_parse!=ASN1_NAME_TOO_LONG)
796 result_parse=ASN1_SYNTAX_ERROR;
798 return 0;