diff options
Diffstat (limited to 'src/asn1Decoding.c')
-rw-r--r-- | src/asn1Decoding.c | 95 |
1 files changed, 31 insertions, 64 deletions
diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c index b864768..098ab9e 100644 --- a/src/asn1Decoding.c +++ b/src/asn1Decoding.c @@ -1,5 +1,5 @@ /* asn1Decoding.c --- program to generate an ASN1 type from a DER coding. - * Copyright (C) 2002-2014 Free Software Foundation, Inc. + * Copyright (C) 2002-2022 Free Software Foundation, Inc. * * This file is part of LIBTASN1. * @@ -32,16 +32,18 @@ #include "benchmark.h" -#define program_name "asn1Decoding" +#include "progname.h" +#include "read-file.h" +#include "version-etc.h" static int decode (asn1_node definitions, const char *typeName, void *der, int der_len, int benchmark, int strict); /* This feature is available in gcc versions 2.5 and later. */ #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) -#define ATTR_NO_RETRUN +# define ATTR_NO_RETRUN #else -#define ATTR_NO_RETRUN __attribute__ ((__noreturn__)) +# define ATTR_NO_RETRUN __attribute__ ((__noreturn__)) #endif ATTR_NO_RETRUN static void @@ -63,51 +65,11 @@ described in ASN.1 DEFINITIONS file, and print decoded structures.\n\ -t, --no-time-strict use strict DER decoding but not in time fields\n\ -h, --help display this help and exit\n\ -v, --version output version information and exit\n"); - printf ("Report bugs to "PACKAGE_BUGREPORT); + emit_bug_reporting_address (); } exit (status); } -static char *read_binary_file(const char *file, size_t *l) -{ - FILE *fp; - struct stat st; - char *out; - - if (stat(file, &st) == -1) - { - fprintf(stderr, "Error reading file size!\n"); - exit(1); - } - - fp = fopen(file, "rb"); - if (fp == NULL) - { - fprintf(stderr, "Error reading file!\n"); - exit(1); - } - - out = malloc(st.st_size+1); - if (out == NULL) - { - fprintf(stderr, "Memory error!\n"); - exit(1); - } - - *l = fread(out, 1, st.st_size, fp); - if (*l != st.st_size) - { - fprintf(stderr, "Error reading contents (got: %ld, expected %ld)!\n", - (long)*l, (long)st.st_size); - exit(1); - } - - out[*l] = 0; - - fclose(fp); - return out; -} - int main (int argc, char *argv[]) { @@ -133,6 +95,8 @@ main (int argc, char *argv[]) int flags = 0, debug = 0; /* FILE *outputFile; */ + set_program_name (argv[0]); + opterr = 0; /* disable error messages from getopt */ while (1) @@ -162,9 +126,8 @@ main (int argc, char *argv[]) flags |= ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME; break; case 'v': /* VERSION */ - printf(program_name" "PACKAGE" " VERSION"\n"); - printf("Copyright (C) 2017-2019 Free Software Foundation, Inc.\n\n"); - printf("Written by Fabio Fiorina\n"); + version_etc (stdout, program_name, PACKAGE, VERSION, + "Fabio Fiorina", NULL); exit (0); break; case '?': /* UNKNOW OPTION */ @@ -176,7 +139,7 @@ main (int argc, char *argv[]) default: fprintf (stderr, "asn1Decoding: ?? getopt returned character code Ox%x ??\n", - (unsigned)option_result); + (unsigned) option_result); } } @@ -187,17 +150,17 @@ main (int argc, char *argv[]) usage (EXIT_FAILURE); } - inputFileAsnName = strdup(argv[optind]); - inputFileDerName = strdup(argv[optind + 1]); - typeName = strdup(argv[optind + 2]); + inputFileAsnName = strdup (argv[optind]); + inputFileDerName = strdup (argv[optind + 1]); + typeName = strdup (argv[optind + 2]); if (!(inputFileAsnName && inputFileDerName && typeName)) { - fprintf(stderr, "allocation failed\n"); - free(inputFileAsnName); - free(inputFileDerName); - free(typeName); - exit(1); + fprintf (stderr, "allocation failed\n"); + free (inputFileAsnName); + free (inputFileDerName); + free (typeName); + exit (1); } asn1_result = @@ -231,7 +194,7 @@ main (int argc, char *argv[]) { size_t tmplen; - der = (unsigned char *) read_binary_file (inputFileDerName, &tmplen); + der = (unsigned char *) read_file (inputFileDerName, RF_BINARY, &tmplen); der_len = tmplen; } @@ -239,7 +202,7 @@ main (int argc, char *argv[]) * with this reallocation we ensure that memory accesses outside the * boundaries are detected */ if (der != NULL && debug != 0) - der = realloc(der, der_len); + der = realloc (der, der_len); if (der == NULL) { @@ -259,7 +222,8 @@ main (int argc, char *argv[]) /* der_len=0; outputFile=fopen("data.p12","w"); - while(fscanf(inputFile,"%c",der+der_len) != EOF){ + while(fscanf(inputFile,"%c",der+der_len) != EOF) + { if((der_len>=0x11) && (der_len<=(0xe70))) fprintf(outputFile,"%c",der[der_len]); der_len++; @@ -268,7 +232,8 @@ main (int argc, char *argv[]) fclose(inputFile); */ - if (decode (definitions, typeName, der, der_len, benchmark, flags) != ASN1_SUCCESS) + if (decode (definitions, typeName, der, der_len, benchmark, flags) != + ASN1_SUCCESS) { asn1_delete_structure (&definitions); free (inputFileAsnName); @@ -316,7 +281,7 @@ simple_decode (asn1_node definitions, const char *typeName, void *der, if (flags != 0) asn1_result = - asn1_der_decoding2(&structure, der, &der_len, flags, errorDescription); + asn1_der_decoding2 (&structure, der, &der_len, flags, errorDescription); else asn1_result = asn1_der_decoding (&structure, der, der_len, errorDescription); @@ -347,14 +312,16 @@ decode (asn1_node definitions, const char *typeName, void *der, int der_len, struct benchmark_st st; if (benchmark == 0) - return simple_decode (definitions, typeName, der, der_len, benchmark, flags); + return simple_decode (definitions, typeName, der, der_len, benchmark, + flags); else { start_benchmark (&st); do { - simple_decode (definitions, typeName, der, der_len, benchmark, flags); + simple_decode (definitions, typeName, der, der_len, benchmark, + flags); st.size++; } while (benchmark_must_finish == 0); |