Merge defines.h into int.h.
[libtasn1.git] / src / asn1Coding.c
blobb0b789463ef0a668ed3ef98d394e3d21f327f39b
1 /*
2 * Copyright (C) 2006, 2007 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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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, see <http://www.gnu.org/licenses/>.
23 /*****************************************************/
24 /* File: asn1Coding.c */
25 /* Description: program to generate a DER coding */
26 /* of an ASN1 definition. */
27 /*****************************************************/
29 #include <config.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <getopt.h>
37 #include <libtasn1.h>
39 #include <progname.h>
40 #include <version-etc.h>
42 static const char help_man[] =
43 "Usage: asn1Coding [OPTION] DEFINITIONS ASSIGNMENTS\n"
44 "asn1Coding generates a DER encoding of ASN.1 DEFINITIONS file\n"
45 "and ASSIGNMENTS file with value assignments.\n"
46 "\n"
47 "Mandatory arguments to long options are mandatory for short options too.\n"
48 " -c, --check checks the syntax only\n"
49 " -o, --output FILE output file\n"
50 " -h, --help display this help and exit\n"
51 " -v, --version output version information and exit.\n"
52 "\n"
53 "Report bugs to <" PACKAGE_BUGREPORT ">.";
55 #define ASSIGNMENT_SUCCESS 1
56 #define ASSIGNMENT_ERROR 2
57 #define ASSIGNMENT_EOF 3
59 int readAssignment(FILE *file,char *varName, char *value){
61 int ret;
63 ret=fscanf(file,"%s",varName);
64 if(ret==EOF) return ASSIGNMENT_EOF;
65 if(!strcmp(varName,"''")) varName[0]=0;
67 ret=fscanf(file,"%s",value);
68 if(ret==EOF) return ASSIGNMENT_ERROR;
70 return ASSIGNMENT_SUCCESS;
75 void createFileName(char *inputFileName, char **outputFileName)
77 char *char_p,*slash_p,*dot_p;
79 /* searching the last '/' and '.' in inputFileAssignmentName */
80 char_p=inputFileName;
81 slash_p=inputFileName;
82 while((char_p=strchr(char_p,'/'))){
83 char_p++;
84 slash_p=char_p;
87 char_p=slash_p;
88 dot_p=inputFileName+strlen(inputFileName);
90 while((char_p=strchr(char_p,'.'))){
91 dot_p=char_p;
92 char_p++;
95 /* outputFileName= inputFileName + .out */
96 *outputFileName=(char *)malloc(dot_p-inputFileName+1+
97 strlen(".out"));
98 memcpy(*outputFileName,inputFileName,
99 dot_p-inputFileName);
100 (*outputFileName)[dot_p-inputFileName]=0;
101 strcat(*outputFileName,".out");
102 return;
106 /********************************************************/
107 /* Function : main */
108 /* Description: */
109 /********************************************************/
111 main(int argc,char *argv[])
113 static struct option long_options[] =
115 {"help", no_argument, 0, 'h'},
116 {"version", no_argument, 0, 'v'},
117 {"check", no_argument, 0, 'c'},
118 {"output", required_argument, 0, 'o'},
119 {0, 0, 0, 0}
121 int option_index=0;
122 int option_result;
123 char *outputFileName=NULL;
124 char *inputFileAsnName=NULL;
125 char *inputFileAssignmentName=NULL;
126 int checkSyntaxOnly=0;
127 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
128 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
129 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
130 int asn1_result=ASN1_SUCCESS;
131 FILE *outputFile;
132 FILE *inputFile;
133 char varName[1024];
134 char value[1024];
135 unsigned char *der = NULL;
136 int der_len;
137 int k;
139 set_program_name (argv[0]);
141 opterr=0; /* disable error messages from getopt */
143 while(1){
145 option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
147 if(option_result == -1) break;
149 switch(option_result){
150 case 'h': /* HELP */
151 printf("%s\n",help_man);
153 if(outputFileName) free(outputFileName);
154 exit(0);
155 break;
156 case 'v': /* VERSION */
157 version_etc (stdout, program_name, PACKAGE, VERSION,
158 "Fabio Fiorina", NULL);
159 if(outputFileName) free(outputFileName);
160 exit(0);
161 break;
162 case 'c': /* CHECK SYNTAX */
163 checkSyntaxOnly = 1;
164 break;
165 case 'o': /* OUTPUT */
166 outputFileName=(char *)malloc(strlen(optarg)+1);
167 strcpy(outputFileName,optarg);
168 break;
169 case '?': /* UNKNOW OPTION */
170 fprintf(stderr,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
171 printf("%s\n",help_man);
173 if(outputFileName) free(outputFileName);
174 exit(1);
175 break;
176 default:
177 fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result);
181 if(optind == argc){
182 fprintf(stderr,"asn1Coding: input file with ASN1 definitions missing.\n");
183 fprintf(stderr," input file with assignments missing.\n\n");
184 printf("%s\n",help_man);
186 if(outputFileName) free(outputFileName);
187 exit(1);
190 if(optind == argc-1){
191 fprintf(stderr,"asn1Coding: input file with assignments missing.\n\n");
192 printf("%s\n",help_man);
194 if(outputFileName) free(outputFileName);
195 exit(1);
198 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
199 strcpy(inputFileAsnName,argv[optind]);
201 inputFileAssignmentName=(char *)malloc(strlen(argv[optind+1])+1);
202 strcpy(inputFileAssignmentName,argv[optind+1]);
204 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
206 switch(asn1_result){
207 case ASN1_SUCCESS:
208 printf("Parse: done.\n");
209 break;
210 case ASN1_FILE_NOT_FOUND:
211 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName);
212 break;
213 case ASN1_SYNTAX_ERROR:
214 case ASN1_IDENTIFIER_NOT_FOUND:
215 case ASN1_NAME_TOO_LONG:
216 printf("asn1Coding: %s\n",errorDescription);
217 break;
218 default:
219 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
222 if(asn1_result != ASN1_SUCCESS){
223 free(inputFileAsnName);
224 free(inputFileAssignmentName);
225 exit(1);
229 inputFile=fopen(inputFileAssignmentName,"r");
231 if(inputFile==NULL){
232 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName);
233 free(inputFileAsnName);
234 free(inputFileAssignmentName);
235 exit(1);
239 printf("\n");
241 while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){
242 printf("var=%s, value=%s\n",varName,value);
243 if(structure==ASN1_TYPE_EMPTY){
244 asn1_result=asn1_create_element(definitions,value,&structure);
246 else
247 asn1_result=asn1_write_value(structure,varName,value,0);
249 if(asn1_result != ASN1_SUCCESS){
250 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
252 asn1_delete_structure(&definitions);
253 asn1_delete_structure(&structure);
255 free(inputFileAsnName);
256 free(inputFileAssignmentName);
258 fclose(inputFile);
259 exit(1);
262 fclose(inputFile);
264 printf("\n");
265 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
267 der_len=0;
268 asn1_result=asn1_der_coding(structure,"",der,&der_len,
269 errorDescription);
270 if (asn1_result==ASN1_MEM_ERROR)
272 der = malloc (der_len);
273 asn1_result=asn1_der_coding(structure,"",der,&der_len,
274 errorDescription);
276 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result));
277 if(asn1_result!=ASN1_SUCCESS){
278 printf("asn1Coding: %s\n",errorDescription);
280 if (der)
281 free (der);
283 asn1_delete_structure(&definitions);
284 asn1_delete_structure(&structure);
286 free(inputFileAsnName);
287 free(inputFileAssignmentName);
289 exit(1);
292 /* Print the 'Certificate1' DER encoding */
293 printf("-----------------\nNumber of bytes=%i\n",der_len);
294 for(k=0;k<der_len;k++) printf("%02x ",der[k]);
295 printf("\n-----------------\n");
297 asn1_delete_structure(&definitions);
298 asn1_delete_structure(&structure);
301 if(outputFileName==NULL)
302 createFileName(inputFileAssignmentName,&outputFileName);
304 printf("\nOutputFile=%s\n",outputFileName);
306 outputFile=fopen(outputFileName,"w");
308 if(outputFile==NULL){
309 printf("asn1Coding: output file '%s' not available\n",outputFileName);
310 if (der)
311 free (der);
312 free(inputFileAsnName);
313 free(inputFileAssignmentName);
314 free(outputFileName);
315 exit(1);
318 for(k=0;k<der_len;k++)
319 fprintf(outputFile,"%c",der[k]);
320 fclose(outputFile);
321 printf("\nWriting: done.\n");
323 if (der)
324 free (der);
326 free(inputFileAsnName);
327 free(inputFileAssignmentName);
328 free(outputFileName);
330 exit(0);