Upstream update.
[shishi.git] / asn1 / lib / ASN1.y
blobc90c8f8ece88c6733a39db3449b43f6e24cc5524
1 /* Copyright (C) 2001, 2002 Fabio Fiorina
3 * This file is part of LIBASN1.
5 * The LIBTASN1 library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /*****************************************************/
22 /* File: x509_ASN.y */
23 /* Description: input file for 'bison' program. */
24 /* The output file is a parser (in C language) for */
25 /* ASN.1 syntax */
26 /*****************************************************/
29 %{
30 #include <int.h>
31 #include <errors.h>
32 #include <parser_aux.h>
33 #include <structure.h>
36 static FILE *file_asn1; /* Pointer to file to parse */
37 static asn1_retCode result_parse; /* result of the parser algorithm */
38 static node_asn *p_tree; /* pointer to the root of the structure
39 created by the parser*/
40 static unsigned long lineNumber; /* line number describing the parser position
41 inside the file */
42 static char lastToken[MAX_NAME_SIZE+1]; /* last token find in the file to
43 parse before the 'parse error' */
44 extern char _asn1_identifierMissing[];
45 static const char *fileName; /* file to parse */
47 int _asn1_yyerror (char *);
48 int _asn1_yylex(void);
52 /* Prefix symbols and functions with _asn1_ */
53 %name-prefix="_asn1_yy"
55 %union {
56 unsigned int constant;
57 char str[MAX_NAME_SIZE+1];
58 node_asn* node;
62 %token ASSIG "::="
63 %token <str> NUM
64 %token <str> IDENTIFIER
65 %token OPTIONAL
66 %token INTEGER
67 %token SIZE
68 %token OCTET
69 %token STRING
70 %token SEQUENCE
71 %token BIT
72 %token UNIVERSAL
73 %token PRIVATE
74 %token APPLICATION
75 %token OPTIONAL
76 %token DEFAULT
77 %token CHOICE
78 %token OF
79 %token OBJECT
80 %token STR_IDENTIFIER
81 %token BOOLEAN
82 %token TRUE
83 %token FALSE
84 %token TOKEN_NULL
85 %token ANY
86 %token DEFINED
87 %token BY
88 %token SET
89 %token EXPLICIT
90 %token IMPLICIT
91 %token DEFINITIONS
92 %token TAGS
93 %token BEGIN
94 %token END
95 %token UTCTime
96 %token GeneralizedTime
97 %token GeneralString
98 %token FROM
99 %token IMPORTS
100 %token ENUMERATED
102 %type <node> octet_string_def constant constant_list type_assig_right
103 %type <node> integer_def type_assig type_assig_list sequence_def type_def
104 %type <node> bit_string_def default size_def choise_def object_def
105 %type <node> boolean_def any_def size_def2 obj_constant obj_constant_list
106 %type <node> constant_def type_constant type_constant_list definitions
107 %type <node> definitions_id Time bit_element bit_element_list set_def
108 %type <node> tag_type tag type_assig_right_tag generalstring_def
109 %type <node> type_assig_right_tag_default enumerated_def
110 %type <str> pos_num neg_num pos_neg_num pos_neg_identifier num_identifier
111 %type <constant> class explicit_implicit
116 definitions: definitions_id
117 DEFINITIONS explicit_implicit TAGS "::=" BEGIN /* imports_def */
118 type_constant_list END
119 {$$=_asn1_add_node(TYPE_DEFINITIONS|$3);
120 _asn1_set_name($$,_asn1_get_name($1));
121 _asn1_set_name($1,"");
122 _asn1_set_right($1,$7);
123 _asn1_set_down($$,$1);
125 p_tree=$$;
129 pos_num : NUM {strcpy($$,$1);}
130 | '+' NUM {strcpy($$,$2);}
133 neg_num : '-' NUM {strcpy($$,"-");
134 strcat($$,$2);}
137 pos_neg_num : pos_num {strcpy($$,$1);}
138 | neg_num {strcpy($$,$1);}
141 num_identifier : NUM {strcpy($$,$1);}
142 | IDENTIFIER {strcpy($$,$1);}
145 pos_neg_identifier : pos_neg_num {strcpy($$,$1);}
146 | IDENTIFIER {strcpy($$,$1);}
149 constant: '(' pos_neg_num ')' {$$=_asn1_add_node(TYPE_CONSTANT);
150 _asn1_set_value($$,$2,strlen($2)+1);}
151 | IDENTIFIER'('pos_neg_num')' {$$=_asn1_add_node(TYPE_CONSTANT);
152 _asn1_set_name($$,$1);
153 _asn1_set_value($$,$3,strlen($3)+1);}
156 constant_list: constant {$$=$1;}
157 | constant_list ',' constant {$$=$1;
158 _asn1_set_right(_asn1_get_last_right($1),$3);}
161 obj_constant: num_identifier {$$=_asn1_add_node(TYPE_CONSTANT);
162 _asn1_set_value($$,$1,strlen($1)+1);}
163 | IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
164 _asn1_set_name($$,$1);
165 _asn1_set_value($$,$3,strlen($3)+1);}
168 obj_constant_list: obj_constant {$$=$1;}
169 | obj_constant_list obj_constant {$$=$1;
170 _asn1_set_right(_asn1_get_last_right($1),$2);}
173 class : UNIVERSAL {$$=CONST_UNIVERSAL;}
174 | PRIVATE {$$=CONST_PRIVATE;}
175 | APPLICATION {$$=CONST_APPLICATION;}
178 tag_type : '[' NUM ']' {$$=_asn1_add_node(TYPE_TAG);
179 _asn1_set_value($$,$2,strlen($2)+1);}
180 | '[' class NUM ']' {$$=_asn1_add_node(TYPE_TAG | $2);
181 _asn1_set_value($$,$3,strlen($3)+1);}
184 tag : tag_type {$$=$1;}
185 | tag_type EXPLICIT {$$=_asn1_mod_type($1,CONST_EXPLICIT);}
186 | tag_type IMPLICIT {$$=_asn1_mod_type($1,CONST_IMPLICIT);}
189 default : DEFAULT pos_neg_identifier {$$=_asn1_add_node(TYPE_DEFAULT);
190 _asn1_set_value($$,$2,strlen($2)+1);}
191 | DEFAULT TRUE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_TRUE);}
192 | DEFAULT FALSE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_FALSE);}
195 integer_def: INTEGER {$$=_asn1_add_node(TYPE_INTEGER);}
196 | INTEGER'{'constant_list'}' {$$=_asn1_add_node(TYPE_INTEGER|CONST_LIST);
197 _asn1_set_down($$,$3);}
198 | integer_def'('num_identifier'.''.'num_identifier')'
199 {$$=_asn1_add_node(TYPE_INTEGER|CONST_MIN_MAX);
200 _asn1_set_down($$,_asn1_add_node(TYPE_SIZE));
201 _asn1_set_value(_asn1_get_down($$),$6,strlen($6)+1);
202 _asn1_set_name(_asn1_get_down($$),$3);}
205 boolean_def: BOOLEAN {$$=_asn1_add_node(TYPE_BOOLEAN);}
208 Time: UTCTime {$$=_asn1_add_node(TYPE_TIME|CONST_UTC);}
209 | GeneralizedTime {$$=_asn1_add_node(TYPE_TIME|CONST_GENERALIZED);}
212 size_def2: SIZE'('num_identifier')' {$$=_asn1_add_node(TYPE_SIZE|CONST_1_PARAM);
213 _asn1_set_value($$,$3,strlen($3)+1);}
214 | SIZE'('num_identifier'.''.'num_identifier')'
215 {$$=_asn1_add_node(TYPE_SIZE|CONST_MIN_MAX);
216 _asn1_set_value($$,$3,strlen($3)+1);
217 _asn1_set_name($$,$6);}
220 size_def: size_def2 {$$=$1;}
221 | '(' size_def2 ')' {$$=$2;}
224 generalstring_def: GeneralString {$$=_asn1_add_node(TYPE_GENERALSTRING);}
225 | GeneralString size_def {$$=_asn1_add_node(TYPE_GENERALSTRING|CONST_SIZE);
226 _asn1_set_down($$,$2);}
229 octet_string_def : OCTET STRING {$$=_asn1_add_node(TYPE_OCTET_STRING);}
230 | OCTET STRING size_def {$$=_asn1_add_node(TYPE_OCTET_STRING|CONST_SIZE);
231 _asn1_set_down($$,$3);}
234 bit_element : IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
235 _asn1_set_name($$,$1);
236 _asn1_set_value($$,$3,strlen($3)+1);}
239 bit_element_list : bit_element {$$=$1;}
240 | bit_element_list ',' bit_element {$$=$1;
241 _asn1_set_right(_asn1_get_last_right($1),$3);}
244 bit_string_def : BIT STRING {$$=_asn1_add_node(TYPE_BIT_STRING);}
245 | BIT STRING'{'bit_element_list'}'
246 {$$=_asn1_add_node(TYPE_BIT_STRING|CONST_LIST);
247 _asn1_set_down($$,$4);}
250 enumerated_def : ENUMERATED'{'bit_element_list'}'
251 {$$=_asn1_add_node(TYPE_ENUMERATED|CONST_LIST);
252 _asn1_set_down($$,$3);}
256 object_def : OBJECT STR_IDENTIFIER {$$=_asn1_add_node(TYPE_OBJECT_ID);}
259 type_assig_right: IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
260 _asn1_set_value($$,$1,strlen($1)+1);}
261 | IDENTIFIER size_def {$$=_asn1_add_node(TYPE_IDENTIFIER|CONST_SIZE);
262 _asn1_set_value($$,$1,strlen($1)+1);
263 _asn1_set_down($$,$2);}
264 | integer_def {$$=$1;}
265 | enumerated_def {$$=$1;}
266 | boolean_def {$$=$1;}
267 | Time
268 | octet_string_def {$$=$1;}
269 | bit_string_def {$$=$1;}
270 | generalstring_def {$$=$1;}
271 | sequence_def {$$=$1;}
272 | object_def {$$=$1;}
273 | choise_def {$$=$1;}
274 | any_def {$$=$1;}
275 | set_def {$$=$1;}
276 | TOKEN_NULL {$$=_asn1_add_node(TYPE_NULL);}
279 type_assig_right_tag : type_assig_right {$$=$1;}
280 | tag type_assig_right {$$=_asn1_mod_type($2,CONST_TAG);
281 _asn1_set_right($1,_asn1_get_down($$));
282 _asn1_set_down($$,$1);}
285 type_assig_right_tag_default : type_assig_right_tag {$$=$1;}
286 | type_assig_right_tag default {$$=_asn1_mod_type($1,CONST_DEFAULT);
287 _asn1_set_right($2,_asn1_get_down($$));
288 _asn1_set_down($$,$2);}
289 | type_assig_right_tag OPTIONAL {$$=_asn1_mod_type($1,CONST_OPTION);}
292 type_assig : IDENTIFIER type_assig_right_tag_default {$$=_asn1_set_name($2,$1);}
295 type_assig_list : type_assig {$$=$1;}
296 | type_assig_list','type_assig {$$=$1;
297 _asn1_set_right(_asn1_get_last_right($1),$3);}
300 sequence_def : SEQUENCE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SEQUENCE);
301 _asn1_set_down($$,$3);}
302 | SEQUENCE OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF);
303 _asn1_set_down($$,$3);}
304 | SEQUENCE size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF|CONST_SIZE);
305 _asn1_set_right($2,$4);
306 _asn1_set_down($$,$2);}
309 set_def : SET'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SET);
310 _asn1_set_down($$,$3);}
311 | SET OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF);
312 _asn1_set_down($$,$3);}
313 | SET size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF|CONST_SIZE);
314 _asn1_set_right($2,$4);
315 _asn1_set_down($$,$2);}
318 choise_def : CHOICE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_CHOICE);
319 _asn1_set_down($$,$3);}
322 any_def : ANY {$$=_asn1_add_node(TYPE_ANY);}
323 | ANY DEFINED BY IDENTIFIER {$$=_asn1_add_node(TYPE_ANY|CONST_DEFINED_BY);
324 _asn1_set_down($$,_asn1_add_node(TYPE_CONSTANT));
325 _asn1_set_name(_asn1_get_down($$),$4);}
328 type_def : IDENTIFIER "::=" type_assig_right_tag {$$=_asn1_set_name($3,$1);}
331 constant_def : IDENTIFIER OBJECT STR_IDENTIFIER "::=" '{'obj_constant_list'}'
332 {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN);
333 _asn1_set_name($$,$1);
334 _asn1_set_down($$,$6);}
335 | IDENTIFIER IDENTIFIER "::=" '{' obj_constant_list '}'
336 {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN|CONST_1_PARAM);
337 _asn1_set_name($$,$1);
338 _asn1_set_value($$,$2,strlen($2)+1);
339 _asn1_set_down($$,$5);}
340 | IDENTIFIER INTEGER "::=" pos_neg_num
341 {$$=_asn1_add_node(TYPE_INTEGER|CONST_ASSIGN);
342 _asn1_set_name($$,$1);
343 _asn1_set_value($$,$4,strlen($4)+1);}
346 type_constant: type_def {$$=$1;}
347 | constant_def {$$=$1;}
350 type_constant_list : type_constant {$$=$1;}
351 | type_constant_list type_constant {$$=$1;
352 _asn1_set_right(_asn1_get_last_right($1),$2);}
355 definitions_id : IDENTIFIER '{' obj_constant_list '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
356 _asn1_set_down($$,$3);
357 _asn1_set_name($$,$1);}
358 | IDENTIFIER '{' '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
359 _asn1_set_name($$,$1);}
363 identifier_list : IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
364 _asn1_set_name($$,$1);}
365 | identifier_list IDENTIFIER
366 {$$=$1;
367 _asn1_set_right(_asn1_get_last_right($$),_asn1_add_node(TYPE_IDENTIFIER));
368 _asn1_set_name(_asn1_get_last_right($$),$2);}
372 imports_def : empty {$$=NULL;}
373 | IMPORTS identifier_list FROM IDENTIFIER obj_constant_list
374 {$$=_asn1_add_node(TYPE_IMPORTS);
375 _asn1_set_down($$,_asn1_add_node(TYPE_OBJECT_ID));
376 _asn1_set_name(_asn1_get_down($$),$4);
377 _asn1_set_down(_asn1_get_down($$),$5);
378 _asn1_set_right($$,$2);}
382 explicit_implicit : EXPLICIT {$$=CONST_EXPLICIT;}
383 | IMPLICIT {$$=CONST_IMPLICIT;}
391 const char *key_word[]={"::=","OPTIONAL","INTEGER","SIZE","OCTET","STRING"
392 ,"SEQUENCE","BIT","UNIVERSAL","PRIVATE","OPTIONAL"
393 ,"DEFAULT","CHOICE","OF","OBJECT","IDENTIFIER"
394 ,"BOOLEAN","TRUE","FALSE","APPLICATION","ANY","DEFINED"
395 ,"SET","BY","EXPLICIT","IMPLICIT","DEFINITIONS","TAGS"
396 ,"BEGIN","END","UTCTime","GeneralizedTime"
397 ,"GeneralString","FROM","IMPORTS","NULL","ENUMERATED"};
398 const int key_word_token[]={ASSIG,OPTIONAL,INTEGER,SIZE,OCTET,STRING
399 ,SEQUENCE,BIT,UNIVERSAL,PRIVATE,OPTIONAL
400 ,DEFAULT,CHOICE,OF,OBJECT,STR_IDENTIFIER
401 ,BOOLEAN,TRUE,FALSE,APPLICATION,ANY,DEFINED
402 ,SET,BY,EXPLICIT,IMPLICIT,DEFINITIONS,TAGS
403 ,BEGIN,END,UTCTime,GeneralizedTime
404 ,GeneralString,FROM,IMPORTS,TOKEN_NULL,ENUMERATED};
406 /*************************************************************/
407 /* Function: _asn1_yylex */
408 /* Description: looks for tokens in file_asn1 pointer file. */
409 /* Return: int */
410 /* Token identifier or ASCII code or 0(zero: End Of File) */
411 /*************************************************************/
412 int
413 _asn1_yylex()
415 int c,counter=0,k;
416 char string[MAX_NAME_SIZE+1]; /* will contain the next token */
418 while(1)
420 while((c=fgetc(file_asn1))==' ' || c=='\t' || c=='\n')
421 if(c=='\n') lineNumber++;
423 if(c==EOF){
424 strcpy(lastToken,"End Of File");
425 return 0;
428 if(c=='(' || c==')' || c=='[' || c==']' ||
429 c=='{' || c=='}' || c==',' || c=='.' ||
430 c=='+'){
431 lastToken[0]=c;lastToken[1]=0;
432 return c;
434 if(c=='-'){ /* Maybe the first '-' of a comment */
435 if((c=fgetc(file_asn1))!='-'){
436 ungetc(c,file_asn1);
437 lastToken[0]='-';lastToken[1]=0;
438 return '-';
440 else{ /* Comments */
441 counter=0;
442 /* A comment finishes at the end of line */
443 while((c=fgetc(file_asn1))!=EOF && c!='\n');
444 if(c==EOF){
445 strcpy(lastToken,"End Of File");
446 return 0;
448 else{
449 lineNumber++;
450 continue; /* next char, please! (repeat the search) */
454 string[counter++]=c;
455 /* Till the end of the token */
456 while(!((c=fgetc(file_asn1))==EOF || c==' '|| c=='\t' || c=='\n' ||
457 c=='(' || c==')' || c=='[' || c==']' ||
458 c=='{' || c=='}' || c==',' || c=='.'))
460 if(counter>=MAX_NAME_SIZE){
461 result_parse=ASN1_NAME_TOO_LONG;
462 return 0;
464 string[counter++]=c;
466 ungetc(c,file_asn1);
467 string[counter]=0;
468 strcpy(lastToken,string);
470 /* Is STRING a number? */
471 for(k=0;k<counter;k++)
472 if(!isdigit(string[k])) break;
473 if(k>=counter)
475 strcpy(yylval.str,string);
476 return NUM; /* return the number */
479 /* Is STRING a keyword? */
480 for(k=0;k<(sizeof(key_word)/sizeof(char*));k++)
481 if(!strcmp(string,key_word[k])) return key_word_token[k];
483 /* STRING is an IDENTIFIER */
484 strcpy(yylval.str,string);
485 return IDENTIFIER;
490 /*************************************************************/
491 /* Function: _asn1_create_errorDescription */
492 /* Description: creates a string with the description of the*/
493 /* error. */
494 /* Parameters: */
495 /* error : error to describe. */
496 /* errorDescription: string that will contain the */
497 /* description. */
498 /*************************************************************/
499 void
500 _asn1_create_errorDescription(int error,char *errorDescription)
502 switch(error){
503 case ASN1_SUCCESS: case ASN1_FILE_NOT_FOUND:
504 if (errorDescription!=NULL) errorDescription[0]=0;
505 break;
506 case ASN1_SYNTAX_ERROR:
507 if (errorDescription!=NULL) {
508 strcpy(errorDescription,fileName);
509 strcat(errorDescription,":");
510 _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
511 strcat(errorDescription,": parse error near '");
512 strcat(errorDescription,lastToken);
513 strcat(errorDescription,"'");
515 break;
516 case ASN1_NAME_TOO_LONG:
517 if (errorDescription!=NULL) {
518 strcpy(errorDescription,fileName);
519 strcat(errorDescription,":");
520 _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
521 strcat(errorDescription,": name too long (more than ");
522 _asn1_ltostr(MAX_NAME_SIZE,errorDescription+strlen(errorDescription));
523 strcat(errorDescription," characters)");
525 break;
526 case ASN1_IDENTIFIER_NOT_FOUND:
527 if (errorDescription!=NULL) {
528 strcpy(errorDescription,fileName);
529 strcat(errorDescription,":");
530 strcat(errorDescription,": identifier '");
531 strcat(errorDescription,_asn1_identifierMissing);
532 strcat(errorDescription,"' not found");
534 break;
535 default:
536 if (errorDescription!=NULL) errorDescription[0]=0;
537 break;
544 * asn1_parser2tree - function used to start the parse algorithm.
545 * @file_name: specify the path and the name of file that contains ASN.1 declarations.
546 * @definitions: return the pointer to the structure created from
547 * "file_name" ASN.1 declarations.
548 * @errorDescription : return the error description or an empty string if success.
549 * Description:
551 * Creates the structures needed to manage the definitions included in *FILE_NAME file.
553 * Returns:
555 * ASN1_SUCCESS\: the file has a correct syntax and every identifier is known.
557 * ASN1_ELEMENT_NOT_EMPTY\: *POINTER not ASN1_TYPE_EMPTY.
559 * ASN1_FILE_NOT_FOUND\: an error occured while opening FILE_NAME.
561 * ASN1_SYNTAX_ERROR\: the syntax is not correct.
563 * ASN1_IDENTIFIER_NOT_FOUND\: in the file there is an identifier that is not defined.
564 * ASN1_NAME_TOO_LONG\: in the file there is an identifier whith more than MAX_NAME_SIZE characters.
566 asn1_retCode
567 asn1_parser2tree(const char *file_name,ASN1_TYPE *definitions,char *errorDescription){
569 p_tree=ASN1_TYPE_EMPTY;
571 if(*definitions != ASN1_TYPE_EMPTY)
572 return ASN1_ELEMENT_NOT_EMPTY;
574 *definitions=ASN1_TYPE_EMPTY;
576 fileName = file_name;
578 /* open the file to parse */
579 file_asn1=fopen(file_name,"r");
581 if(file_asn1==NULL){
582 result_parse=ASN1_FILE_NOT_FOUND;
584 else{
585 result_parse=ASN1_SUCCESS;
587 lineNumber=1;
588 yyparse();
590 fclose(file_asn1);
592 if(result_parse==ASN1_SUCCESS){ /* syntax OK */
593 /* set IMPLICIT or EXPLICIT property */
594 _asn1_set_default_tag(p_tree);
595 /* set CONST_SET and CONST_NOT_USED */
596 _asn1_type_set_config(p_tree);
597 /* check the identifier definitions */
598 result_parse=_asn1_check_identifier(p_tree);
599 if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
600 /* Delete the list and keep the ASN1 structure */
601 _asn1_delete_list();
602 /* Convert into DER coding the value assign to INTEGER constants */
603 _asn1_change_integer_value(p_tree);
604 /* Expand the IDs of OBJECT IDENTIFIER constants */
605 _asn1_expand_object_id(p_tree);
607 *definitions=p_tree;
609 else /* some identifiers not defined */
610 /* Delete the list and the ASN1 structure */
611 _asn1_delete_list_and_nodes();
613 else /* syntax error */
614 /* Delete the list and the ASN1 structure */
615 _asn1_delete_list_and_nodes();
618 if (errorDescription!=NULL)
619 _asn1_create_errorDescription(result_parse,errorDescription);
621 return result_parse;
626 * asn1_parser2array - function that generates a C structure from an ASN1 file
627 * @inputFileName: specify the path and the name of file that contains ASN.1 declarations.
628 * @outputFileName: specify the path and the name of file that will contain the C vector definition.
629 * @vectorName: specify the name of the C vector.
630 * @errorDescription : return the error description or an empty string if success.
631 * Description:
633 * Creates a file containing a C vector to use to manage the definitions included in
634 * *INPUTFILENAME file. If *INPUTFILENAME is "/aa/bb/xx.yy" and OUTPUTFILENAME is NULL, the file created is "/aa/bb/xx_asn1_tab.c".
635 * If VECTORNAME is NULL the vector name will be "xx_asn1_tab".
637 * Returns:
639 * ASN1_SUCCESS\: the file has a correct syntax and every identifier is known.
641 * ASN1_FILE_NOT_FOUND\: an error occured while opening FILE_NAME.
643 * ASN1_SYNTAX_ERROR\: the syntax is not correct.
645 * ASN1_IDENTIFIER_NOT_FOUND\: in the file there is an identifier that is not defined.
646 * ASN1_NAME_TOO_LONG\: in the file there is an identifier whith more than MAX_NAME_SIZE characters.
648 int asn1_parser2array(const char *inputFileName,const char *outputFileName,
649 const char *vectorName,char *errorDescription){
650 char *file_out_name=NULL;
651 char *vector_name=NULL;
652 const char *char_p,*slash_p,*dot_p;
654 p_tree=NULL;
656 fileName = inputFileName;
658 /* open the file to parse */
659 file_asn1=fopen(inputFileName,"r");
661 if(file_asn1==NULL)
662 result_parse=ASN1_FILE_NOT_FOUND;
663 else{
664 result_parse=ASN1_SUCCESS;
666 lineNumber=1;
667 yyparse();
669 fclose(file_asn1);
671 if(result_parse==ASN1_SUCCESS){ /* syntax OK */
672 /* set IMPLICIT or EXPLICIT property */
673 _asn1_set_default_tag(p_tree);
674 /* set CONST_SET and CONST_NOT_USED */
675 _asn1_type_set_config(p_tree);
676 /* check the identifier definitions */
677 result_parse=_asn1_check_identifier(p_tree);
679 if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
681 /* searching the last '/' and '.' in inputFileName */
682 char_p=inputFileName;
683 slash_p=inputFileName;
684 while((char_p=strchr(char_p,'/'))){
685 char_p++;
686 slash_p=char_p;
689 char_p=slash_p;
690 dot_p=inputFileName+strlen(inputFileName);
692 while((char_p=strchr(char_p,'.'))){
693 dot_p=char_p;
694 char_p++;
697 if(outputFileName == NULL){
698 /* file_out_name = inputFileName + _asn1_tab.c */
699 file_out_name=(char *)malloc(dot_p-inputFileName+1+
700 strlen("_asn1_tab.c"));
701 memcpy(file_out_name,inputFileName,dot_p-inputFileName);
702 file_out_name[dot_p-inputFileName]=0;
703 strcat(file_out_name,"_asn1_tab.c");
705 else{
706 /* file_out_name = inputFileName */
707 file_out_name=(char *)malloc(strlen(outputFileName)+1);
708 strcpy(file_out_name,outputFileName);
711 if(vectorName == NULL){
712 /* vector_name = file name + _asn1_tab */
713 vector_name=(char *)malloc(dot_p-slash_p+1+
714 strlen("_asn1_tab"));
715 memcpy(vector_name,slash_p,dot_p-slash_p);
716 vector_name[dot_p-slash_p]=0;
717 strcat(vector_name,"_asn1_tab");
719 else{
720 /* vector_name = vectorName */
721 vector_name=(char *)malloc(strlen(vectorName)+1);
722 strcpy(vector_name,vectorName);
725 /* Save structure in a file */
726 _asn1_create_static_structure(p_tree,
727 file_out_name,vector_name);
729 free(file_out_name);
730 free(vector_name);
731 } /* result == OK */
732 } /* result == OK */
734 /* Delete the list and the ASN1 structure */
735 _asn1_delete_list_and_nodes();
736 } /* inputFile exist */
738 if (errorDescription!=NULL)
739 _asn1_create_errorDescription(result_parse,errorDescription);
741 return result_parse;
745 /*************************************************************/
746 /* Function: _asn1_yyerror */
747 /* Description: function called when there are syntax errors*/
748 /* Parameters: */
749 /* char *s : error description */
750 /* Return: int */
751 /* */
752 /*************************************************************/
753 int _asn1_yyerror (char *s)
755 /* Sends the error description to the std_out */
757 #ifdef LIBTASN1_DEBUG_PARSER
758 _libtasn1_log("_asn1_yyerror:%s:%d: %s (Last Token:'%s')\n",fileName,
759 lineNumber,s,lastToken);
760 #endif
762 if(result_parse!=ASN1_NAME_TOO_LONG)
763 result_parse=ASN1_SYNTAX_ERROR;
765 return 0;