2 * Copyright (C) 2006 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program 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 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 /*****************************************************/
25 /* File: asn1Parser.c */
26 /* Description: program to parse a file with ASN1 */
28 /*****************************************************/
44 char version_man
[] = "asn1Parser (GNU libtasn1) " VERSION
;
46 char help_man
[] = "asn1Parser reads files with ASN1 definitions and\n"
47 "generates a C array to use with libtasn1 functions.\n"
49 "Usage: asn1Parser [options] file\n"
51 #ifdef HAVE_GETOPT_LONG
53 " -h, --help shows this message and exit\n"
54 " -v, --version shows version information and exit.\n"
55 " -c, --check checks the syntax only.\n"
58 " -o <file>, --output <file> output file\n"
59 " -n <name>, --name <name> array name\n";
62 " -h shows this message and exit\n"
63 " -v shows version information and exit.\n"
64 " -c checks the syntax only.\n"
67 " -o <file> output file\n"
68 " -n <name> array name\n";
71 /********************************************************/
74 /********************************************************/
76 main(int argc
,char *argv
[])
79 #ifdef HAVE_GETOPT_LONG
80 static struct option long_options
[] =
82 {"help", no_argument
, 0, 'h'},
83 {"version", no_argument
, 0, 'v'},
84 {"check", no_argument
, 0, 'c'},
85 {"output", required_argument
, 0, 'o'},
86 {"name", required_argument
, 0, 'n'},
93 char *outputFileName
=NULL
;
94 char *inputFileName
=NULL
;
95 char *vectorName
=NULL
;
96 int checkSyntaxOnly
=0;
97 ASN1_TYPE pointer
=ASN1_TYPE_EMPTY
;
98 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
99 int parse_result
=ASN1_SUCCESS
;
101 opterr
=0; /* disable error messages from getopt */
107 #ifdef HAVE_GETOPT_LONG
108 option_result
=getopt_long(argc
,argv
,"hvco:n:",long_options
,&option_index
);
110 option_result
=getopt(argc
,argv
,"hvco:n:");
113 if(option_result
== -1) break;
115 switch(option_result
){
117 #ifdef HAVE_GETOPT_LONG
118 printf("option %s",long_options
[option_index
].name
);
119 if(optarg
) printf(" with arg %s",optarg
);
124 printf("%s\n",help_man
);
126 if(outputFileName
) free(outputFileName
);
127 if(vectorName
) free(vectorName
);
130 case 'v': /* VERSION */
131 printf("%s\n",version_man
);
133 if(outputFileName
) free(outputFileName
);
134 if(vectorName
) free(vectorName
);
137 case 'c': /* CHECK SYNTAX */
140 case 'o': /* OUTPUT */
141 outputFileName
=(char *)malloc(strlen(optarg
)+1);
142 strcpy(outputFileName
,optarg
);
144 case 'n': /* VECTOR NAME */
145 vectorName
=(char *)malloc(strlen(optarg
)+1);
146 strcpy(vectorName
,optarg
);
148 case '?': /* UNKNOW OPTION */
149 fprintf(stderr
,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
150 printf("%s\n",help_man
);
152 if(outputFileName
) free(outputFileName
);
153 if(vectorName
) free(vectorName
);
157 fprintf(stderr
,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result
);
163 fprintf(stderr
,"asn1Parser: input file name missing.\n\n");
164 printf("%s\n",help_man
);
166 if(outputFileName
) free(outputFileName
);
167 if(vectorName
) free(vectorName
);
171 inputFileName
=(char *)malloc(strlen(argv
[optind
])+1);
172 strcpy(inputFileName
,argv
[optind
]);
175 if(checkSyntaxOnly
== 1){
176 parse_result
=asn1_parser2tree(inputFileName
,&pointer
,errorDescription
);
177 asn1_delete_structure(&pointer
);
179 else /* C VECTOR CREATION */
180 parse_result
=asn1_parser2array(inputFileName
,
181 outputFileName
,vectorName
,errorDescription
);
183 switch(parse_result
){
187 case ASN1_FILE_NOT_FOUND
:
188 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName
);
190 case ASN1_SYNTAX_ERROR
:
191 case ASN1_IDENTIFIER_NOT_FOUND
:
192 case ASN1_NAME_TOO_LONG
:
193 printf("asn1Parser: %s\n",errorDescription
);
196 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result
));
201 if(outputFileName
) free(outputFileName
);
202 if(vectorName
) free(vectorName
);
204 if(parse_result
!= ASN1_SUCCESS
) exit(1);