Fix release.
[libtasn1.git] / src / asn1Coding.c
blobdce006c85d095d7d8e7992f2667a93d4661a7a78
1 /*
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
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: asn1Coding.c */
26 /* Description: program to generate a DER coding */
27 /* of an ASN1 definition. */
28 /*****************************************************/
30 #include <config.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <getopt.h>
38 #include <libtasn1.h>
40 #include <progname.h>
41 #include <version-etc.h>
43 static const char help_man[] =
44 "Usage: asn1Coding [OPTION] DEFINITIONS ASSIGNMENTS\n"
45 "asn1Coding generates a DER encoding of ASN.1 DEFINITIONS file\n"
46 "and ASSIGNMENTS file with value assignments.\n"
47 "\n"
48 "Mandatory arguments to long options are mandatory for short options too.\n"
49 " -c, --check checks the syntax only\n"
50 " -o, --output FILE output file\n"
51 " -h, --help display this help and exit\n"
52 " -v, --version output version information and exit.\n"
53 "\n"
54 "Report bugs to <" PACKAGE_BUGREPORT ">.";
56 #define ASSIGNMENT_SUCCESS 1
57 #define ASSIGNMENT_ERROR 2
58 #define ASSIGNMENT_EOF 3
60 int readAssignment(FILE *file,char *varName, char *value){
62 int ret;
64 ret=fscanf(file,"%s",varName);
65 if(ret==EOF) return ASSIGNMENT_EOF;
66 if(!strcmp(varName,"''")) varName[0]=0;
68 ret=fscanf(file,"%s",value);
69 if(ret==EOF) return ASSIGNMENT_ERROR;
71 return ASSIGNMENT_SUCCESS;
76 void createFileName(char *inputFileName, char **outputFileName)
78 char *char_p,*slash_p,*dot_p;
80 /* searching the last '/' and '.' in inputFileAssignmentName */
81 char_p=inputFileName;
82 slash_p=inputFileName;
83 while((char_p=strchr(char_p,'/'))){
84 char_p++;
85 slash_p=char_p;
88 char_p=slash_p;
89 dot_p=inputFileName+strlen(inputFileName);
91 while((char_p=strchr(char_p,'.'))){
92 dot_p=char_p;
93 char_p++;
96 /* outputFileName= inputFileName + .out */
97 *outputFileName=(char *)malloc(dot_p-inputFileName+1+
98 strlen(".out"));
99 memcpy(*outputFileName,inputFileName,
100 dot_p-inputFileName);
101 (*outputFileName)[dot_p-inputFileName]=0;
102 strcat(*outputFileName,".out");
103 return;
107 /********************************************************/
108 /* Function : main */
109 /* Description: */
110 /********************************************************/
112 main(int argc,char *argv[])
114 static struct option long_options[] =
116 {"help", no_argument, 0, 'h'},
117 {"version", no_argument, 0, 'v'},
118 {"check", no_argument, 0, 'c'},
119 {"output", required_argument, 0, 'o'},
120 {0, 0, 0, 0}
122 int option_index=0;
123 int option_result;
124 char *outputFileName=NULL;
125 char *inputFileAsnName=NULL;
126 char *inputFileAssignmentName=NULL;
127 int checkSyntaxOnly=0;
128 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
129 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
130 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
131 int asn1_result=ASN1_SUCCESS;
132 FILE *outputFile;
133 FILE *inputFile;
134 char varName[1024];
135 char value[1024];
136 unsigned char *der = NULL;
137 int der_len;
138 int k;
140 set_program_name (argv[0]);
142 opterr=0; /* disable error messages from getopt */
144 while(1){
146 option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
148 if(option_result == -1) break;
150 switch(option_result){
151 case 'h': /* HELP */
152 printf("%s\n",help_man);
154 if(outputFileName) free(outputFileName);
155 exit(0);
156 break;
157 case 'v': /* VERSION */
158 version_etc (stdout, program_name, PACKAGE, VERSION,
159 "Fabio Fiorina", NULL);
160 if(outputFileName) free(outputFileName);
161 exit(0);
162 break;
163 case 'c': /* CHECK SYNTAX */
164 checkSyntaxOnly = 1;
165 break;
166 case 'o': /* OUTPUT */
167 outputFileName=(char *)malloc(strlen(optarg)+1);
168 strcpy(outputFileName,optarg);
169 break;
170 case '?': /* UNKNOW OPTION */
171 fprintf(stderr,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
172 printf("%s\n",help_man);
174 if(outputFileName) free(outputFileName);
175 exit(1);
176 break;
177 default:
178 fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result);
182 if(optind == argc){
183 fprintf(stderr,"asn1Coding: input file with ASN1 definitions missing.\n");
184 fprintf(stderr," input file with assignments missing.\n\n");
185 printf("%s\n",help_man);
187 if(outputFileName) free(outputFileName);
188 exit(1);
191 if(optind == argc-1){
192 fprintf(stderr,"asn1Coding: input file with assignments missing.\n\n");
193 printf("%s\n",help_man);
195 if(outputFileName) free(outputFileName);
196 exit(1);
199 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
200 strcpy(inputFileAsnName,argv[optind]);
202 inputFileAssignmentName=(char *)malloc(strlen(argv[optind+1])+1);
203 strcpy(inputFileAssignmentName,argv[optind+1]);
205 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
207 switch(asn1_result){
208 case ASN1_SUCCESS:
209 printf("Parse: done.\n");
210 break;
211 case ASN1_FILE_NOT_FOUND:
212 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName);
213 break;
214 case ASN1_SYNTAX_ERROR:
215 case ASN1_IDENTIFIER_NOT_FOUND:
216 case ASN1_NAME_TOO_LONG:
217 printf("asn1Coding: %s\n",errorDescription);
218 break;
219 default:
220 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
223 if(asn1_result != ASN1_SUCCESS){
224 free(inputFileAsnName);
225 free(inputFileAssignmentName);
226 exit(1);
230 inputFile=fopen(inputFileAssignmentName,"r");
232 if(inputFile==NULL){
233 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName);
234 free(inputFileAsnName);
235 free(inputFileAssignmentName);
236 exit(1);
240 printf("\n");
242 while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){
243 printf("var=%s, value=%s\n",varName,value);
244 if(structure==ASN1_TYPE_EMPTY){
245 asn1_result=asn1_create_element(definitions,value,&structure);
247 else
248 asn1_result=asn1_write_value(structure,varName,value,0);
250 if(asn1_result != ASN1_SUCCESS){
251 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
253 asn1_delete_structure(&definitions);
254 asn1_delete_structure(&structure);
256 free(inputFileAsnName);
257 free(inputFileAssignmentName);
259 fclose(inputFile);
260 exit(1);
263 fclose(inputFile);
265 printf("\n");
266 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
268 der_len=0;
269 asn1_result=asn1_der_coding(structure,"",der,&der_len,
270 errorDescription);
271 if (asn1_result==ASN1_MEM_ERROR)
273 der = malloc (der_len);
274 asn1_result=asn1_der_coding(structure,"",der,&der_len,
275 errorDescription);
277 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result));
278 if(asn1_result!=ASN1_SUCCESS){
279 printf("asn1Coding: %s\n",errorDescription);
281 if (der)
282 free (der);
284 asn1_delete_structure(&definitions);
285 asn1_delete_structure(&structure);
287 free(inputFileAsnName);
288 free(inputFileAssignmentName);
290 exit(1);
293 /* Print the 'Certificate1' DER encoding */
294 printf("-----------------\nNumber of bytes=%i\n",der_len);
295 for(k=0;k<der_len;k++) printf("%02x ",der[k]);
296 printf("\n-----------------\n");
298 asn1_delete_structure(&definitions);
299 asn1_delete_structure(&structure);
302 if(outputFileName==NULL)
303 createFileName(inputFileAssignmentName,&outputFileName);
305 printf("\nOutputFile=%s\n",outputFileName);
307 outputFile=fopen(outputFileName,"w");
309 if(outputFile==NULL){
310 printf("asn1Coding: output file '%s' not available\n",outputFileName);
311 if (der)
312 free (der);
313 free(inputFileAsnName);
314 free(inputFileAssignmentName);
315 free(outputFileName);
316 exit(1);
319 for(k=0;k<der_len;k++)
320 fprintf(outputFile,"%c",der[k]);
321 fclose(outputFile);
322 printf("\nWriting: done.\n");
324 if (der)
325 free (der);
327 free(inputFileAsnName);
328 free(inputFileAssignmentName);
329 free(outputFileName);
331 exit(0);