(asn1_write_value): Fix prototype, to avoid warnings.
[libtasn1.git] / src / asn1Coding.c
blob3a2ccfe604a2ae9e588140b3181ad749afcba686
1 /*
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: asn1Coding.c */
24 /* Description: program to generate a DER coding */
25 /* of an ASN1 definition. */
26 /*****************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <libtasn1.h>
31 #include <stdlib.h>
32 #include <config.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 char version_man[] = "asn1Coding (GNU libtasn1) " VERSION;
44 char help_man[] = "asn1Coding generates a DER encoding from a file\n"
45 "with ASN1 definitions and another one with assignments.\n"
46 "\n"
47 "Usage: asn1Coding [options] <file1> <file2>\n"
48 " <file1> file with ASN1 definitions.\n"
49 " <file2> file with assignments.\n"
50 "\n"
51 #ifdef HAVE_GETOPT_LONG
52 "Operation modes:\n"
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"
56 "\n"
57 "Output:\n"
58 " -o <file>, --output <file> output file.\n";
59 #else
60 "Operation modes:\n"
61 " -h shows this message and exit.\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n"
64 "\n"
65 "Output:\n"
66 " -o <file> output file.\n";
67 #endif
70 #define ASSIGNMENT_SUCCESS 1
71 #define ASSIGNMENT_ERROR 2
72 #define ASSIGNMENT_EOF 3
74 int readAssignment(FILE *file,char *varName, char *value){
76 int ret;
78 ret=fscanf(file,"%s",varName);
79 if(ret==EOF) return ASSIGNMENT_EOF;
80 if(!strcmp(varName,"''")) varName[0]=0;
82 ret=fscanf(file,"%s",value);
83 if(ret==EOF) return ASSIGNMENT_ERROR;
85 return ASSIGNMENT_SUCCESS;
90 void createFileName(char *inputFileName, char **outputFileName)
92 char *char_p,*slash_p,*dot_p;
94 /* searching the last '/' and '.' in inputFileAssignmentName */
95 char_p=inputFileName;
96 slash_p=inputFileName;
97 while((char_p=strchr(char_p,'/'))){
98 char_p++;
99 slash_p=char_p;
102 char_p=slash_p;
103 dot_p=inputFileName+strlen(inputFileName);
105 while((char_p=strchr(char_p,'.'))){
106 dot_p=char_p;
107 char_p++;
110 /* outputFileName= inputFileName + .out */
111 *outputFileName=(char *)malloc(dot_p-inputFileName+1+
112 strlen(".out"));
113 memcpy(*outputFileName,inputFileName,
114 dot_p-inputFileName);
115 (*outputFileName)[dot_p-inputFileName]=0;
116 strcat(*outputFileName,".out");
117 return;
121 /********************************************************/
122 /* Function : main */
123 /* Description: */
124 /********************************************************/
126 main(int argc,char *argv[])
129 #ifdef HAVE_GETOPT_LONG
130 static struct option long_options[] =
132 {"help", no_argument, 0, 'h'},
133 {"version", no_argument, 0, 'v'},
134 {"check", no_argument, 0, 'c'},
135 {"output", required_argument, 0, 'o'},
136 {0, 0, 0, 0}
138 int option_index=0;
139 #endif
141 int option_result;
142 char *outputFileName=NULL;
143 char *inputFileAsnName=NULL;
144 char *inputFileAssignmentName=NULL;
145 int checkSyntaxOnly=0;
146 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
147 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
148 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
149 int asn1_result=ASN1_SUCCESS;
150 FILE *outputFile;
151 FILE *inputFile;
152 char varName[1024];
153 char value[1024];
154 unsigned char der[1024];
155 int der_len;
156 int k;
158 opterr=0; /* disable error messages from getopt */
160 printf("\n");
162 while(1){
164 #ifdef HAVE_GETOPT_LONG
165 option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
166 #else
167 option_result=getopt(argc,argv,"hvco:");
168 #endif
170 if(option_result == -1) break;
172 switch(option_result){
173 case 'h': /* HELP */
174 printf("%s\n",help_man);
176 if(outputFileName) free(outputFileName);
177 exit(0);
178 break;
179 case 'v': /* VERSION */
180 printf("%s\n",version_man);
182 if(outputFileName) free(outputFileName);
183 exit(0);
184 break;
185 case 'c': /* CHECK SYNTAX */
186 checkSyntaxOnly = 1;
187 break;
188 case 'o': /* OUTPUT */
189 outputFileName=(char *)malloc(strlen(optarg)+1);
190 strcpy(outputFileName,optarg);
191 break;
192 case '?': /* UNKNOW OPTION */
193 fprintf(stderr,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
194 printf("%s\n",help_man);
196 if(outputFileName) free(outputFileName);
197 exit(1);
198 break;
199 default:
200 fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result);
204 if(optind == argc){
205 fprintf(stderr,"asn1Coding: input file with ASN1 definitions missing.\n");
206 fprintf(stderr," input file with assignments missing.\n\n");
207 printf("%s\n",help_man);
209 if(outputFileName) free(outputFileName);
210 exit(1);
213 if(optind == argc-1){
214 fprintf(stderr,"asn1Coding: input file with assignments missing.\n\n");
215 printf("%s\n",help_man);
217 if(outputFileName) free(outputFileName);
218 exit(1);
221 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
222 strcpy(inputFileAsnName,argv[optind]);
224 inputFileAssignmentName=(char *)malloc(strlen(argv[optind+1])+1);
225 strcpy(inputFileAssignmentName,argv[optind+1]);
227 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
229 switch(asn1_result){
230 case ASN1_SUCCESS:
231 printf("Parse: done.\n");
232 break;
233 case ASN1_FILE_NOT_FOUND:
234 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName);
235 break;
236 case ASN1_SYNTAX_ERROR:
237 case ASN1_IDENTIFIER_NOT_FOUND:
238 case ASN1_NAME_TOO_LONG:
239 printf("asn1Coding: %s\n",errorDescription);
240 break;
241 default:
242 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
245 if(asn1_result != ASN1_SUCCESS){
246 free(inputFileAsnName);
247 free(inputFileAssignmentName);
248 exit(1);
252 inputFile=fopen(inputFileAssignmentName,"r");
254 if(inputFile==NULL){
255 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName);
256 free(inputFileAsnName);
257 free(inputFileAssignmentName);
258 exit(1);
262 printf("\n");
264 while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){
265 printf("var=%s, value=%s\n",varName,value);
266 if(structure==ASN1_TYPE_EMPTY){
267 asn1_result=asn1_create_element(definitions,value,&structure);
269 else
270 asn1_result=asn1_write_value(structure,varName,value,0);
272 if(asn1_result != ASN1_SUCCESS){
273 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
275 asn1_delete_structure(&definitions);
276 asn1_delete_structure(&structure);
278 free(inputFileAsnName);
279 free(inputFileAssignmentName);
281 fclose(inputFile);
282 exit(1);
285 fclose(inputFile);
287 printf("\n");
288 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
290 der_len=1024;
291 asn1_result=asn1_der_coding(structure,"",der,&der_len,
292 errorDescription);
293 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result));
294 if(asn1_result!=ASN1_SUCCESS){
295 printf("asn1Coding: %s\n",errorDescription);
297 asn1_delete_structure(&definitions);
298 asn1_delete_structure(&structure);
300 free(inputFileAsnName);
301 free(inputFileAssignmentName);
303 exit(1);
306 /* Print the 'Certificate1' DER encoding */
307 printf("-----------------\nNumber of bytes=%i\n",der_len);
308 for(k=0;k<der_len;k++) printf("%02x ",der[k]);
309 printf("\n-----------------\n");
311 asn1_delete_structure(&definitions);
312 asn1_delete_structure(&structure);
315 if(outputFileName==NULL)
316 createFileName(inputFileAssignmentName,&outputFileName);
318 printf("\nOutputFile=%s\n",outputFileName);
320 outputFile=fopen(outputFileName,"w");
322 if(outputFile==NULL){
323 printf("asn1Coding: output file '%s' not available\n",outputFileName);
324 free(inputFileAsnName);
325 free(inputFileAssignmentName);
326 free(outputFileName);
327 exit(1);
330 for(k=0;k<der_len;k++)
331 fprintf(outputFile,"%c",der[k]);
332 fclose(outputFile);
333 printf("\nWriting: done.\n");
336 free(inputFileAsnName);
337 free(inputFileAssignmentName);
338 free(outputFileName);
340 exit(0);