diff options
author | Simon Josefsson <simon@josefsson.org> | 2009-03-04 17:17:46 +0100 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2009-03-04 17:17:46 +0100 |
commit | 45e34b66d1c30727816cca7594b44ea212e47ad4 (patch) | |
tree | c954d417f3b8face79292904dddfe4e08876d93a /src | |
parent | 7610ca58af3ccc0431e89d6ffd6279f6b10f283b (diff) | |
download | libtasn1-45e34b66d1c30727816cca7594b44ea212e47ad4.tar.gz libtasn1-45e34b66d1c30727816cca7594b44ea212e47ad4.tar.bz2 libtasn1-45e34b66d1c30727816cca7594b44ea212e47ad4.zip |
Indent code. Reproduce using 'make indent' with GNU indent 2.2.10.
Diffstat (limited to 'src')
-rw-r--r-- | src/asn1Coding.c | 535 | ||||
-rw-r--r-- | src/asn1Decoding.c | 389 | ||||
-rw-r--r-- | src/asn1Parser.c | 260 |
3 files changed, 628 insertions, 556 deletions
diff --git a/src/asn1Coding.c b/src/asn1Coding.c index 22ea515..b084cb3 100644 --- a/src/asn1Coding.c +++ b/src/asn1Coding.c @@ -23,7 +23,7 @@ /*****************************************************/ /* File: asn1Coding.c */ /* Description: program to generate a DER coding */ -/* of an ASN1 definition. */ +/* of an ASN1 definition. */ /*****************************************************/ #include <config.h> @@ -49,24 +49,27 @@ static const char help_man[] = " -o, --output FILE output file\n" " -h, --help display this help and exit\n" " -v, --version output version information and exit.\n" - "\n" - "Report bugs to <" PACKAGE_BUGREPORT ">."; + "\n" "Report bugs to <" PACKAGE_BUGREPORT ">."; #define ASSIGNMENT_SUCCESS 1 #define ASSIGNMENT_ERROR 2 #define ASSIGNMENT_EOF 3 static int -readAssignment(FILE *file,char *varName, char *value){ +readAssignment (FILE * file, char *varName, char *value) +{ int ret; - ret=fscanf(file,"%s",varName); - if(ret==EOF) return ASSIGNMENT_EOF; - if(!strcmp(varName,"''")) varName[0]=0; + ret = fscanf (file, "%s", varName); + if (ret == EOF) + return ASSIGNMENT_EOF; + if (!strcmp (varName, "''")) + varName[0] = 0; - ret=fscanf(file,"%s",value); - if(ret==EOF) return ASSIGNMENT_ERROR; + ret = fscanf (file, "%s", value); + if (ret == EOF) + return ASSIGNMENT_ERROR; return ASSIGNMENT_SUCCESS; } @@ -74,34 +77,35 @@ readAssignment(FILE *file,char *varName, char *value){ static void -createFileName(char *inputFileName, char **outputFileName) +createFileName (char *inputFileName, char **outputFileName) { - char *char_p,*slash_p,*dot_p; - - /* searching the last '/' and '.' in inputFileAssignmentName */ - char_p=inputFileName; - slash_p=inputFileName; - while((char_p=strchr(char_p,'/'))){ - char_p++; - slash_p=char_p; - } - - char_p=slash_p; - dot_p=inputFileName+strlen(inputFileName); - - while((char_p=strchr(char_p,'.'))){ - dot_p=char_p; - char_p++; - } - - /* outputFileName= inputFileName + .out */ - *outputFileName=(char *)malloc(dot_p-inputFileName+1+ - strlen(".out")); - memcpy(*outputFileName,inputFileName, - dot_p-inputFileName); - (*outputFileName)[dot_p-inputFileName]=0; - strcat(*outputFileName,".out"); - return; + char *char_p, *slash_p, *dot_p; + + /* searching the last '/' and '.' in inputFileAssignmentName */ + char_p = inputFileName; + slash_p = inputFileName; + while ((char_p = strchr (char_p, '/'))) + { + char_p++; + slash_p = char_p; + } + + char_p = slash_p; + dot_p = inputFileName + strlen (inputFileName); + + while ((char_p = strchr (char_p, '.'))) + { + dot_p = char_p; + char_p++; + } + + /* outputFileName= inputFileName + .out */ + *outputFileName = (char *) malloc (dot_p - inputFileName + 1 + + strlen (".out")); + memcpy (*outputFileName, inputFileName, dot_p - inputFileName); + (*outputFileName)[dot_p - inputFileName] = 0; + strcat (*outputFileName, ".out"); + return; } @@ -110,224 +114,251 @@ createFileName(char *inputFileName, char **outputFileName) /* Description: */ /********************************************************/ int -main(int argc,char *argv[]) +main (int argc, char *argv[]) { - static struct option long_options[] = - { - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'v'}, - {"check", no_argument, 0, 'c'}, - {"output", required_argument, 0, 'o'}, + static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"check", no_argument, 0, 'c'}, + {"output", required_argument, 0, 'o'}, {0, 0, 0, 0} }; - int option_index=0; - int option_result; - char *outputFileName=NULL; - char *inputFileAsnName=NULL; - char *inputFileAssignmentName=NULL; - int checkSyntaxOnly=0; - ASN1_TYPE definitions=ASN1_TYPE_EMPTY; - ASN1_TYPE structure=ASN1_TYPE_EMPTY; - char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; - int asn1_result=ASN1_SUCCESS; - FILE *outputFile; - FILE *inputFile; - char varName[1024]; - char value[1024]; - unsigned char *der = NULL; - int der_len; - int k; - - set_program_name (argv[0]); - - opterr=0; /* disable error messages from getopt */ - - while(1){ - - option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index); - - if(option_result == -1) break; - - switch(option_result){ - case 'h': /* HELP */ - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - exit(0); - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - if(outputFileName) free(outputFileName); - exit(0); - break; - case 'c': /* CHECK SYNTAX */ - checkSyntaxOnly = 1; - break; - case 'o': /* OUTPUT */ - outputFileName=(char *)malloc(strlen(optarg)+1); - strcpy(outputFileName,optarg); - break; - case '?': /* UNKNOW OPTION */ - fprintf(stderr,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv[optind-1]); - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - exit(1); - break; - default: - fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result); - } - } - - if(optind == argc){ - fprintf(stderr,"asn1Coding: input file with ASN1 definitions missing.\n"); - fprintf(stderr," input file with assignments missing.\n\n"); - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - exit(1); - } - - if(optind == argc-1){ - fprintf(stderr,"asn1Coding: input file with assignments missing.\n\n"); - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - exit(1); - } - - inputFileAsnName=(char *)malloc(strlen(argv[optind])+1); - strcpy(inputFileAsnName,argv[optind]); - - inputFileAssignmentName=(char *)malloc(strlen(argv[optind+1])+1); - strcpy(inputFileAssignmentName,argv[optind+1]); - - asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription); - - switch(asn1_result){ - case ASN1_SUCCESS: - printf("Parse: done.\n"); - break; - case ASN1_FILE_NOT_FOUND: - printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - printf("asn1Coding: %s\n",errorDescription); - break; - default: - printf("libtasn1 ERROR: %s\n",asn1_strerror(asn1_result)); - } - - if(asn1_result != ASN1_SUCCESS){ - free(inputFileAsnName); - free(inputFileAssignmentName); - exit(1); - } - - - inputFile=fopen(inputFileAssignmentName,"r"); - - if(inputFile==NULL){ - printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName); - free(inputFileAsnName); - free(inputFileAssignmentName); - exit(1); - } - - - printf("\n"); - - while(readAssignment(inputFile,varName,value) == ASSIGNMENT_SUCCESS){ - printf("var=%s, value=%s\n",varName,value); - if(structure==ASN1_TYPE_EMPTY){ - asn1_result=asn1_create_element(definitions,value,&structure); - } - else - asn1_result=asn1_write_value(structure,varName,value,0); - - if(asn1_result != ASN1_SUCCESS){ - printf("libtasn1 ERROR: %s\n",asn1_strerror(asn1_result)); - - asn1_delete_structure(&definitions); - asn1_delete_structure(&structure); - - free(inputFileAsnName); - free(inputFileAssignmentName); - - fclose(inputFile); - exit(1); - } - } - fclose(inputFile); - - printf("\n"); - asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE); - - der_len=0; - asn1_result=asn1_der_coding(structure,"",der,&der_len, - errorDescription); - if (asn1_result==ASN1_MEM_ERROR) - { - der = malloc (der_len); - asn1_result=asn1_der_coding(structure,"",der,&der_len, + int option_index = 0; + int option_result; + char *outputFileName = NULL; + char *inputFileAsnName = NULL; + char *inputFileAssignmentName = NULL; + int checkSyntaxOnly = 0; + ASN1_TYPE definitions = ASN1_TYPE_EMPTY; + ASN1_TYPE structure = ASN1_TYPE_EMPTY; + char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; + int asn1_result = ASN1_SUCCESS; + FILE *outputFile; + FILE *inputFile; + char varName[1024]; + char value[1024]; + unsigned char *der = NULL; + int der_len; + int k; + + set_program_name (argv[0]); + + opterr = 0; /* disable error messages from getopt */ + + while (1) + { + + option_result = + getopt_long (argc, argv, "hvco:", long_options, &option_index); + + if (option_result == -1) + break; + + switch (option_result) + { + case 'h': /* HELP */ + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + exit (0); + break; + case 'v': /* VERSION */ + version_etc (stdout, program_name, PACKAGE, VERSION, + "Fabio Fiorina", NULL); + if (outputFileName) + free (outputFileName); + exit (0); + break; + case 'c': /* CHECK SYNTAX */ + checkSyntaxOnly = 1; + break; + case 'o': /* OUTPUT */ + outputFileName = (char *) malloc (strlen (optarg) + 1); + strcpy (outputFileName, optarg); + break; + case '?': /* UNKNOW OPTION */ + fprintf (stderr, + "asn1Coding: option '%s' not recognized or without argument.\n\n", + argv[optind - 1]); + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + exit (1); + break; + default: + fprintf (stderr, + "asn1Coding: ?? getopt returned character code Ox%x ??\n", + option_result); + } + } + + if (optind == argc) + { + fprintf (stderr, + "asn1Coding: input file with ASN1 definitions missing.\n"); + fprintf (stderr, + " input file with assignments missing.\n\n"); + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + exit (1); + } + + if (optind == argc - 1) + { + fprintf (stderr, + "asn1Coding: input file with assignments missing.\n\n"); + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + exit (1); + } + + inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1); + strcpy (inputFileAsnName, argv[optind]); + + inputFileAssignmentName = (char *) malloc (strlen (argv[optind + 1]) + 1); + strcpy (inputFileAssignmentName, argv[optind + 1]); + + asn1_result = + asn1_parser2tree (inputFileAsnName, &definitions, errorDescription); + + switch (asn1_result) + { + case ASN1_SUCCESS: + printf ("Parse: done.\n"); + break; + case ASN1_FILE_NOT_FOUND: + printf ("asn1Coding: FILE %s NOT FOUND\n", inputFileAsnName); + break; + case ASN1_SYNTAX_ERROR: + case ASN1_IDENTIFIER_NOT_FOUND: + case ASN1_NAME_TOO_LONG: + printf ("asn1Coding: %s\n", errorDescription); + break; + default: + printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result)); + } + + if (asn1_result != ASN1_SUCCESS) + { + free (inputFileAsnName); + free (inputFileAssignmentName); + exit (1); + } + + + inputFile = fopen (inputFileAssignmentName, "r"); + + if (inputFile == NULL) + { + printf ("asn1Coding: file '%s' not found\n", inputFileAssignmentName); + free (inputFileAsnName); + free (inputFileAssignmentName); + exit (1); + } + + + printf ("\n"); + + while (readAssignment (inputFile, varName, value) == ASSIGNMENT_SUCCESS) + { + printf ("var=%s, value=%s\n", varName, value); + if (structure == ASN1_TYPE_EMPTY) + { + asn1_result = asn1_create_element (definitions, value, &structure); + } + else + asn1_result = asn1_write_value (structure, varName, value, 0); + + if (asn1_result != ASN1_SUCCESS) + { + printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result)); + + asn1_delete_structure (&definitions); + asn1_delete_structure (&structure); + + free (inputFileAsnName); + free (inputFileAssignmentName); + + fclose (inputFile); + exit (1); + } + } + fclose (inputFile); + + printf ("\n"); + asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE); + + der_len = 0; + asn1_result = asn1_der_coding (structure, "", der, &der_len, errorDescription); - } - printf("\nCoding: %s\n\n",asn1_strerror(asn1_result)); - if(asn1_result!=ASN1_SUCCESS){ - printf("asn1Coding: %s\n",errorDescription); - - if (der) - free (der); - - asn1_delete_structure(&definitions); - asn1_delete_structure(&structure); - - free(inputFileAsnName); - free(inputFileAssignmentName); - - exit(1); - } - - /* Print the 'Certificate1' DER encoding */ - printf("-----------------\nNumber of bytes=%i\n",der_len); - for(k=0;k<der_len;k++) printf("%02x ",der[k]); - printf("\n-----------------\n"); - - asn1_delete_structure(&definitions); - asn1_delete_structure(&structure); - - - if(outputFileName==NULL) - createFileName(inputFileAssignmentName,&outputFileName); - - printf("\nOutputFile=%s\n",outputFileName); - - outputFile=fopen(outputFileName,"w"); - - if(outputFile==NULL){ - printf("asn1Coding: output file '%s' not available\n",outputFileName); - if (der) - free (der); - free(inputFileAsnName); - free(inputFileAssignmentName); - free(outputFileName); - exit(1); - } - - for(k=0;k<der_len;k++) - fprintf(outputFile,"%c",der[k]); - fclose(outputFile); - printf("\nWriting: done.\n"); - - if (der) - free (der); - - free(inputFileAsnName); - free(inputFileAssignmentName); - free(outputFileName); - - exit(0); + if (asn1_result == ASN1_MEM_ERROR) + { + der = malloc (der_len); + asn1_result = asn1_der_coding (structure, "", der, &der_len, + errorDescription); + } + printf ("\nCoding: %s\n\n", asn1_strerror (asn1_result)); + if (asn1_result != ASN1_SUCCESS) + { + printf ("asn1Coding: %s\n", errorDescription); + + if (der) + free (der); + + asn1_delete_structure (&definitions); + asn1_delete_structure (&structure); + + free (inputFileAsnName); + free (inputFileAssignmentName); + + exit (1); + } + + /* Print the 'Certificate1' DER encoding */ + printf ("-----------------\nNumber of bytes=%i\n", der_len); + for (k = 0; k < der_len; k++) + printf ("%02x ", der[k]); + printf ("\n-----------------\n"); + + asn1_delete_structure (&definitions); + asn1_delete_structure (&structure); + + + if (outputFileName == NULL) + createFileName (inputFileAssignmentName, &outputFileName); + + printf ("\nOutputFile=%s\n", outputFileName); + + outputFile = fopen (outputFileName, "w"); + + if (outputFile == NULL) + { + printf ("asn1Coding: output file '%s' not available\n", outputFileName); + if (der) + free (der); + free (inputFileAsnName); + free (inputFileAssignmentName); + free (outputFileName); + exit (1); + } + + for (k = 0; k < der_len; k++) + fprintf (outputFile, "%c", der[k]); + fclose (outputFile); + printf ("\nWriting: done.\n"); + + if (der) + free (der); + + free (inputFileAsnName); + free (inputFileAssignmentName); + free (outputFileName); + + exit (0); } diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c index e38b78f..8aebfc1 100644 --- a/src/asn1Decoding.c +++ b/src/asn1Decoding.c @@ -48,213 +48,230 @@ static const char help_man[] = " -c, --check checks the syntax only\n" " -h, --help display this help and exit\n" " -v, --version output version information and exit.\n" - "\n" - "Report bugs to <" PACKAGE_BUGREPORT ">."; + "\n" "Report bugs to <" PACKAGE_BUGREPORT ">."; /********************************************************/ /* Function : main */ /* Description: */ /********************************************************/ int -main(int argc,char *argv[]) +main (int argc, char *argv[]) { - static struct option long_options[] = - { - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'v'}, - {"check", no_argument, 0, 'c'}, + static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"check", no_argument, 0, 'c'}, {0, 0, 0, 0} }; - int option_index = 0; - int option_result; - char *inputFileAsnName=NULL; - char *inputFileDerName=NULL; - char *typeName=NULL; - int checkSyntaxOnly=0; - ASN1_TYPE definitions=ASN1_TYPE_EMPTY; - ASN1_TYPE structure=ASN1_TYPE_EMPTY; - char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; - int asn1_result=ASN1_SUCCESS; - unsigned char *der; - int der_len=0; - /* FILE *outputFile; */ - - set_program_name (argv[0]); - - opterr=0; /* disable error messages from getopt */ - - while(1){ - - option_result=getopt_long(argc,argv,"hvc",long_options,&option_index); - - if(option_result == -1) break; - - switch(option_result){ - case 'h': /* HELP */ - printf("%s\n",help_man); - exit(0); - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - exit(0); - break; - case 'c': /* CHECK SYNTAX */ - checkSyntaxOnly = 1; - break; - case '?': /* UNKNOW OPTION */ - fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]); - printf("%s\n",help_man); - - exit(1); - break; - default: - fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result); - } - } - - if(optind == argc){ - fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n"); - fprintf(stderr," input file with DER coding missing.\n"); - fprintf(stderr," ASN1 type name missing.\n\n"); - printf("%s\n",help_man); - - exit(1); - } - - if(optind == argc-1){ - fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n"); - fprintf(stderr," ASN1 type name missing.\n\n"); - printf("%s\n",help_man); - - exit(1); - } - - if(optind == argc-2){ - fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n"); - printf("%s\n",help_man); - - exit(1); - } - - inputFileAsnName=(char *)malloc(strlen(argv[optind])+1); - strcpy(inputFileAsnName,argv[optind]); - - inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1); - strcpy(inputFileDerName,argv[optind+1]); - - typeName=(char *)malloc(strlen(argv[optind+2])+1); - strcpy(typeName,argv[optind+2]); - - asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription); - - switch(asn1_result){ - case ASN1_SUCCESS: - printf("Parse: done.\n"); - break; - case ASN1_FILE_NOT_FOUND: - printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - printf("asn1Decoding: %s\n",errorDescription); - break; - default: - printf("libtasn1 ERROR: %s\n",asn1_strerror(asn1_result)); - } - - if(asn1_result != ASN1_SUCCESS){ - free(inputFileAsnName); - free(inputFileDerName); - free(typeName); - exit(1); - } - - - { - size_t tmplen; - der = (unsigned char*) read_binary_file (inputFileDerName, &tmplen); - der_len = tmplen; - } - - if(der==NULL){ - printf("asn1Decoding: could not read '%s'\n",inputFileDerName); - asn1_delete_structure(&definitions); - - free(inputFileAsnName); - free(inputFileDerName); - free(typeName); - exit(1); - } + int option_index = 0; + int option_result; + char *inputFileAsnName = NULL; + char *inputFileDerName = NULL; + char *typeName = NULL; + int checkSyntaxOnly = 0; + ASN1_TYPE definitions = ASN1_TYPE_EMPTY; + ASN1_TYPE structure = ASN1_TYPE_EMPTY; + char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; + int asn1_result = ASN1_SUCCESS; + unsigned char *der; + int der_len = 0; + /* FILE *outputFile; */ + + set_program_name (argv[0]); + + opterr = 0; /* disable error messages from getopt */ + + while (1) + { + + option_result = + getopt_long (argc, argv, "hvc", long_options, &option_index); + + if (option_result == -1) + break; + + switch (option_result) + { + case 'h': /* HELP */ + printf ("%s\n", help_man); + exit (0); + break; + case 'v': /* VERSION */ + version_etc (stdout, program_name, PACKAGE, VERSION, + "Fabio Fiorina", NULL); + exit (0); + break; + case 'c': /* CHECK SYNTAX */ + checkSyntaxOnly = 1; + break; + case '?': /* UNKNOW OPTION */ + fprintf (stderr, + "asn1Decoding: option '%s' not recognized or without argument.\n\n", + argv[optind - 1]); + printf ("%s\n", help_man); + + exit (1); + break; + default: + fprintf (stderr, + "asn1Decoding: ?? getopt returned character code Ox%x ??\n", + option_result); + } + } + + if (optind == argc) + { + fprintf (stderr, + "asn1Decoding: input file with ASN1 definitions missing.\n"); + fprintf (stderr, " input file with DER coding missing.\n"); + fprintf (stderr, " ASN1 type name missing.\n\n"); + printf ("%s\n", help_man); + + exit (1); + } + + if (optind == argc - 1) + { + fprintf (stderr, + "asn1Decoding: input file with DER coding missing.\n\n"); + fprintf (stderr, " ASN1 type name missing.\n\n"); + printf ("%s\n", help_man); + + exit (1); + } + + if (optind == argc - 2) + { + fprintf (stderr, "asn1Decoding: ASN1 type name missing.\n\n"); + printf ("%s\n", help_man); + + exit (1); + } + + inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1); + strcpy (inputFileAsnName, argv[optind]); + + inputFileDerName = (char *) malloc (strlen (argv[optind + 1]) + 1); + strcpy (inputFileDerName, argv[optind + 1]); + + typeName = (char *) malloc (strlen (argv[optind + 2]) + 1); + strcpy (typeName, argv[optind + 2]); + + asn1_result = + asn1_parser2tree (inputFileAsnName, &definitions, errorDescription); + + switch (asn1_result) + { + case ASN1_SUCCESS: + printf ("Parse: done.\n"); + break; + case ASN1_FILE_NOT_FOUND: + printf ("asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName); + break; + case ASN1_SYNTAX_ERROR: + case ASN1_IDENTIFIER_NOT_FOUND: + case ASN1_NAME_TOO_LONG: + printf ("asn1Decoding: %s\n", errorDescription); + break; + default: + printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result)); + } + + if (asn1_result != ASN1_SUCCESS) + { + free (inputFileAsnName); + free (inputFileDerName); + free (typeName); + exit (1); + } - /*****************************************/ - /* ONLY FOR TEST */ - /*****************************************/ - /* - der_len=0; - outputFile=fopen("data.p12","w"); - while(fscanf(inputFile,"%c",der+der_len) != EOF){ - if((der_len>=0x11) && (der_len<=(0xe70))) - fprintf(outputFile,"%c",der[der_len]); - der_len++; - } - fclose(outputFile); - fclose(inputFile); - */ - - asn1_result=asn1_create_element(definitions,typeName,&structure); + { + size_t tmplen; + der = (unsigned char *) read_binary_file (inputFileDerName, &tmplen); + der_len = tmplen; + } - /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */ + if (der == NULL) + { + printf ("asn1Decoding: could not read '%s'\n", inputFileDerName); + asn1_delete_structure (&definitions); + free (inputFileAsnName); + free (inputFileDerName); + free (typeName); + exit (1); + } - if(asn1_result != ASN1_SUCCESS){ - printf("Structure creation: %s\n",asn1_strerror(asn1_result)); - asn1_delete_structure(&definitions); - free(inputFileAsnName); - free(inputFileDerName); - free(typeName); - free(der); - exit(1); - } + /*****************************************/ + /* ONLY FOR TEST */ + /*****************************************/ + /* + der_len=0; + outputFile=fopen("data.p12","w"); + while(fscanf(inputFile,"%c",der+der_len) != EOF){ + if((der_len>=0x11) && (der_len<=(0xe70))) + fprintf(outputFile,"%c",der[der_len]); + der_len++; + } + fclose(outputFile); + fclose(inputFile); + */ - asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription); - printf("\nDecoding: %s\n",asn1_strerror(asn1_result)); - if(asn1_result != ASN1_SUCCESS) - printf("asn1Decoding: %s\n",errorDescription); + asn1_result = asn1_create_element (definitions, typeName, &structure); - printf("\nDECODING RESULT:\n"); - asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE); + /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */ - /*****************************************/ - /* ONLY FOR TEST */ - /*****************************************/ - /* - der_len=10000; - option_index=0; - asn1_result=asn1_read_value(structure,"?2.content",der,&der_len); - outputFile=fopen("encryptedData.p12","w"); - while(der_len>0){ - fprintf(outputFile,"%c",der[option_index]); - der_len--; - option_index++; - } - fclose(outputFile); - */ - asn1_delete_structure(&definitions); - asn1_delete_structure(&structure); + if (asn1_result != ASN1_SUCCESS) + { + printf ("Structure creation: %s\n", asn1_strerror (asn1_result)); + asn1_delete_structure (&definitions); - free(der); + free (inputFileAsnName); + free (inputFileDerName); + free (typeName); + free (der); + exit (1); + } - free(inputFileAsnName); - free(inputFileDerName); - free(typeName); + asn1_result = + asn1_der_decoding (&structure, der, der_len, errorDescription); + printf ("\nDecoding: %s\n", asn1_strerror (asn1_result)); + if (asn1_result != ASN1_SUCCESS) + printf ("asn1Decoding: %s\n", errorDescription); - if(asn1_result != ASN1_SUCCESS) - exit(1); + printf ("\nDECODING RESULT:\n"); + asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE); - exit(0); + /*****************************************/ + /* ONLY FOR TEST */ + /*****************************************/ + /* + der_len=10000; + option_index=0; + asn1_result=asn1_read_value(structure,"?2.content",der,&der_len); + outputFile=fopen("encryptedData.p12","w"); + while(der_len>0){ + fprintf(outputFile,"%c",der[option_index]); + der_len--; + option_index++; + } + fclose(outputFile); + */ + + asn1_delete_structure (&definitions); + asn1_delete_structure (&structure); + + free (der); + + free (inputFileAsnName); + free (inputFileDerName); + free (typeName); + + if (asn1_result != ASN1_SUCCESS) + exit (1); + + exit (0); } diff --git a/src/asn1Parser.c b/src/asn1Parser.c index 61923c3..7e4d18e 100644 --- a/src/asn1Parser.c +++ b/src/asn1Parser.c @@ -50,132 +50,156 @@ static const char help_man[] = " -n, --name NAME array name\n" " -h, --help display this help and exit\n" " -v, --version output version information and exit.\n" - "\n" - "Report bugs to <" PACKAGE_BUGREPORT ">."; + "\n" "Report bugs to <" PACKAGE_BUGREPORT ">."; /********************************************************/ /* Function : main */ /* Description: */ /********************************************************/ int -main(int argc,char *argv[]) +main (int argc, char *argv[]) { - static struct option long_options[] = - { - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'v'}, - {"check", no_argument, 0, 'c'}, - {"output", required_argument, 0, 'o'}, - {"name", required_argument, 0, 'n'}, + static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"check", no_argument, 0, 'c'}, + {"output", required_argument, 0, 'o'}, + {"name", required_argument, 0, 'n'}, {0, 0, 0, 0} }; int option_index = 0; - int option_result; - char *outputFileName=NULL; - char *inputFileName=NULL; - char *vectorName=NULL; - int checkSyntaxOnly=0; - ASN1_TYPE pointer=ASN1_TYPE_EMPTY; - char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; - int parse_result=ASN1_SUCCESS; - - set_program_name (argv[0]); - - opterr=0; /* disable error messages from getopt */ - - while(1){ - - option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index); - - if(option_result == -1) break; - - switch(option_result){ - case 0: - printf("option %s",long_options[option_index].name); - if(optarg) printf(" with arg %s",optarg); - printf("\n"); - break; - case 'h': /* HELP */ - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - if(vectorName) free(vectorName); - exit(0); - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - if(outputFileName) free(outputFileName); - if(vectorName) free(vectorName); - exit(0); - break; - case 'c': /* CHECK SYNTAX */ - checkSyntaxOnly = 1; - break; - case 'o': /* OUTPUT */ - outputFileName=(char *)malloc(strlen(optarg)+1); - strcpy(outputFileName,optarg); - break; - case 'n': /* VECTOR NAME */ - vectorName=(char *)malloc(strlen(optarg)+1); - strcpy(vectorName,optarg); - break; - case '?': /* UNKNOW OPTION */ - fprintf(stderr,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv[optind-1]); - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - if(vectorName) free(vectorName); - exit(1); - break; - default: - fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result); - } - - } - - if(optind == argc){ - fprintf(stderr,"asn1Parser: input file name missing.\n\n"); - printf("%s\n",help_man); - - if(outputFileName) free(outputFileName); - if(vectorName) free(vectorName); - exit(1); - } - else{ - inputFileName=(char *)malloc(strlen(argv[optind])+1); - strcpy(inputFileName,argv[optind]); - } - - if(checkSyntaxOnly == 1){ - parse_result=asn1_parser2tree(inputFileName,&pointer,errorDescription); - asn1_delete_structure(&pointer); - } - else /* C VECTOR CREATION */ - parse_result=asn1_parser2array(inputFileName, - outputFileName,vectorName,errorDescription); - - switch(parse_result){ - case ASN1_SUCCESS: - printf("Done.\n"); - break; - case ASN1_FILE_NOT_FOUND: - printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - printf("asn1Parser: %s\n",errorDescription); - break; - default: - printf("libtasn1 ERROR: %s\n",asn1_strerror(parse_result)); - } - - - free(inputFileName); - if(outputFileName) free(outputFileName); - if(vectorName) free(vectorName); - - if(parse_result != ASN1_SUCCESS) exit(1); - exit(0); + int option_result; + char *outputFileName = NULL; + char *inputFileName = NULL; + char *vectorName = NULL; + int checkSyntaxOnly = 0; + ASN1_TYPE pointer = ASN1_TYPE_EMPTY; + char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; + int parse_result = ASN1_SUCCESS; + + set_program_name (argv[0]); + + opterr = 0; /* disable error messages from getopt */ + + while (1) + { + + option_result = + getopt_long (argc, argv, "hvco:n:", long_options, &option_index); + + if (option_result == -1) + break; + + switch (option_result) + { + case 0: + printf ("option %s", long_options[option_index].name); + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + case 'h': /* HELP */ + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + if (vectorName) + free (vectorName); + exit (0); + break; + case 'v': /* VERSION */ + version_etc (stdout, program_name, PACKAGE, VERSION, + "Fabio Fiorina", NULL); + if (outputFileName) + free (outputFileName); + if (vectorName) + free (vectorName); + exit (0); + break; + case 'c': /* CHECK SYNTAX */ + checkSyntaxOnly = 1; + break; + case 'o': /* OUTPUT */ + outputFileName = (char *) malloc (strlen (optarg) + 1); + strcpy (outputFileName, optarg); + break; + case 'n': /* VECTOR NAME */ + vectorName = (char *) malloc (strlen (optarg) + 1); + strcpy (vectorName, optarg); + break; + case '?': /* UNKNOW OPTION */ + fprintf (stderr, + "asn1Parser: option '%s' not recognized or without argument.\n\n", + argv[optind - 1]); + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + if (vectorName) + free (vectorName); + exit (1); + break; + default: + fprintf (stderr, + "asn1Parser: ?? getopt returned character code Ox%x ??\n", + option_result); + } + + } + + if (optind == argc) + { + fprintf (stderr, "asn1Parser: input file name missing.\n\n"); + printf ("%s\n", help_man); + + if (outputFileName) + free (outputFileName); + if (vectorName) + free (vectorName); + exit (1); + } + else + { + inputFileName = (char *) malloc (strlen (argv[optind]) + 1); + strcpy (inputFileName, argv[optind]); + } + + if (checkSyntaxOnly == 1) + { + parse_result = + asn1_parser2tree (inputFileName, &pointer, errorDescription); + asn1_delete_structure (&pointer); + } + else /* C VECTOR CREATION */ + parse_result = asn1_parser2array (inputFileName, + outputFileName, vectorName, + errorDescription); + + switch (parse_result) + { + case ASN1_SUCCESS: + printf ("Done.\n"); + break; + case ASN1_FILE_NOT_FOUND: + printf ("asn1Parser: FILE %s NOT FOUND\n", inputFileName); + break; + case ASN1_SYNTAX_ERROR: + case ASN1_IDENTIFIER_NOT_FOUND: + case ASN1_NAME_TOO_LONG: + printf ("asn1Parser: %s\n", errorDescription); + break; + default: + printf ("libtasn1 ERROR: %s\n", asn1_strerror (parse_result)); + } + + + free (inputFileName); + if (outputFileName) + free (outputFileName); + if (vectorName) + free (vectorName); + + if (parse_result != ASN1_SUCCESS) + exit (1); + exit (0); } |