2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * LIBASN1 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * LIBASN1 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /*****************************************************/
23 /* File: asn1Parser.c */
24 /* Description: program to parse a file with ASN1 */
26 /*****************************************************/
42 char version_man
[] = "asn1Parser (GNU libasn1) " VERSION
;
44 char help_man
[] = "asn1Parser reads files with ASN1 definitions and\n"
45 "generates a C array to use with libtasn1 functions.\n"
47 "Usage: asn1Parser [options] file\n"
49 #ifdef HAVE_GETOPT_LONG
51 " -h, --help shows this message and exit\n"
52 " -v, --version shows version information and exit.\n"
53 " -c, --check checks the syntax only.\n"
56 " -o <file>, --output <file> output file\n"
57 " -n <name>, --name <name> array name\n";
60 " -h shows this message and exit\n"
61 " -v shows version information and exit.\n"
62 " -c checks the syntax only.\n"
65 " -o <file> output file\n"
66 " -n <name> array name\n";
69 /********************************************************/
72 /********************************************************/
74 main(int argc
,char *argv
[])
77 #ifdef HAVE_GETOPT_LONG
78 static struct option long_options
[] =
80 {"help", no_argument
, 0, 'h'},
81 {"version", no_argument
, 0, 'v'},
82 {"check", no_argument
, 0, 'c'},
83 {"output", required_argument
, 0, 'o'},
84 {"name", required_argument
, 0, 'n'},
91 char *outputFileName
=NULL
;
92 char *inputFileName
=NULL
;
93 char *vectorName
=NULL
;
94 int checkSyntaxOnly
=0;
95 ASN1_TYPE pointer
=ASN1_TYPE_EMPTY
;
96 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
97 int parse_result
=ASN1_SUCCESS
;
99 opterr
=0; /* disable error messages from getopt */
105 #ifdef HAVE_GETOPT_LONG
106 option_result
=getopt_long(argc
,argv
,"hvco:n:",long_options
,&option_index
);
108 option_result
=getopt(argc
,argv
,"hvco:n:");
111 if(option_result
== -1) break;
113 switch(option_result
){
115 #ifdef HAVE_GETOPT_LONG
116 printf("option %s",long_options
[option_index
].name
);
117 if(optarg
) printf(" with arg %s",optarg
);
122 printf("%s\n",help_man
);
124 if(outputFileName
) free(outputFileName
);
125 if(vectorName
) free(vectorName
);
128 case 'v': /* VERSION */
129 printf("%s\n",version_man
);
131 if(outputFileName
) free(outputFileName
);
132 if(vectorName
) free(vectorName
);
135 case 'c': /* CHECK SYNTAX */
138 case 'o': /* OUTPUT */
139 outputFileName
=(char *)malloc(strlen(optarg
)+1);
140 strcpy(outputFileName
,optarg
);
142 case 'n': /* VECTOR NAME */
143 vectorName
=(char *)malloc(strlen(optarg
)+1);
144 strcpy(vectorName
,optarg
);
146 case '?': /* UNKNOW OPTION */
147 fprintf(stderr
,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
148 printf("%s\n",help_man
);
150 if(outputFileName
) free(outputFileName
);
151 if(vectorName
) free(vectorName
);
155 fprintf(stderr
,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result
);
161 fprintf(stderr
,"asn1Parser: input file name missing.\n\n");
162 printf("%s\n",help_man
);
164 if(outputFileName
) free(outputFileName
);
165 if(vectorName
) free(vectorName
);
169 inputFileName
=(char *)malloc(strlen(argv
[optind
])+1);
170 strcpy(inputFileName
,argv
[optind
]);
173 if(checkSyntaxOnly
== 1){
174 parse_result
=asn1_parser2tree(inputFileName
,&pointer
,errorDescription
);
175 asn1_delete_structure(&pointer
);
177 else /* C VECTOR CREATION */
178 parse_result
=asn1_parser2array(inputFileName
,
179 outputFileName
,vectorName
,errorDescription
);
181 switch(parse_result
){
185 case ASN1_FILE_NOT_FOUND
:
186 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName
);
188 case ASN1_SYNTAX_ERROR
:
189 case ASN1_IDENTIFIER_NOT_FOUND
:
190 case ASN1_NAME_TOO_LONG
:
191 printf("asn1Parser: %s\n",errorDescription
);
194 printf("libasn1 ERROR: %s\n",libtasn1_strerror(parse_result
));
199 if(outputFileName
) free(outputFileName
);
200 if(vectorName
) free(vectorName
);
202 if(parse_result
!= ASN1_SUCCESS
) exit(1);