diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 30 | ||||
-rw-r--r-- | src/asn1Coding.c | 359 | ||||
-rw-r--r-- | src/asn1Decoding.c | 327 | ||||
-rw-r--r-- | src/asn1Parser.c | 195 | ||||
-rw-r--r-- | src/benchmark.c | 160 | ||||
-rw-r--r-- | src/benchmark.h | 52 |
6 files changed, 0 insertions, 1123 deletions
diff --git a/src/Makefile.am b/src/Makefile.am deleted file mode 100644 index 06439b4..0000000 --- a/src/Makefile.am +++ /dev/null @@ -1,30 +0,0 @@ -## Process this file with automake to produce Makefile.in -# Copyright (C) 2002-2014 Free Software Foundation, Inc. -# -# This file is part of LIBTASN1. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -AM_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS) -AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_builddir)/gl - -LDADD = ../lib/libtasn1.la ../gl/libgnu.la - -bin_PROGRAMS = asn1Parser asn1Coding asn1Decoding - -asn1Parser_SOURCES = asn1Parser.c - -asn1Coding_SOURCES = asn1Coding.c - -asn1Decoding_SOURCES = asn1Decoding.c benchmark.c benchmark.h diff --git a/src/asn1Coding.c b/src/asn1Coding.c deleted file mode 100644 index b516bfe..0000000 --- a/src/asn1Coding.c +++ /dev/null @@ -1,359 +0,0 @@ -/* asn1Coding.c --- program to generate a DER coding of an ASN1 definition. - * Copyright (C) 2002-2014 Free Software Foundation, Inc. - * - * This file is part of LIBTASN1. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -#include <config.h> - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> -#include <getopt.h> - -#include <libtasn1.h> - -#include <progname.h> -#include <version-etc.h> - -/* This feature is available in gcc versions 2.5 and later. */ -#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) -#define ATTR_NO_RETRUN -#else -#define ATTR_NO_RETRUN __attribute__ ((__noreturn__)) -#endif - -ATTR_NO_RETRUN static void -usage (int status) -{ - if (status != EXIT_SUCCESS) - fprintf (stderr, "Try `%s --help' for more information.\n", program_name); - else - { - printf ("\ -Usage: %s [OPTION] DEFINITIONS ASSIGNMENTS\n", program_name); - printf ("\ -Generates a DER encoding of ASN.1 DEFINITIONS file\n\ -and ASSIGNMENTS file with value assignments.\n\ -\n"); - printf ("\ -Mandatory arguments to long options are mandatory for short options too.\n\ - -c, --check checks the syntax only\n\ - -o, --output=FILE output file\n\ - -h, --help display this help and exit\n\ - -v, --version output version information and exit\n"); - emit_bug_reporting_address (); - } - exit (status); -} - -#define ASSIGNMENT_SUCCESS 1 -#define ASSIGNMENT_ERROR 2 -#define ASSIGNMENT_EOF 3 - -static int -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", value); - if (ret == EOF) - return ASSIGNMENT_ERROR; - - return ASSIGNMENT_SUCCESS; -} - -static void -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; -} - -int -main (int argc, char *argv[]) -{ - static const 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_node definitions = NULL; - asn1_node structure = NULL; - 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; - int last_ra; - - 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 */ - free (outputFileName); - usage (EXIT_SUCCESS); - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - 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 */ - free (outputFileName); - fprintf (stderr, - "asn1Coding: option '%s' not recognized or without argument.\n\n", - argv[optind - 1]); - usage (EXIT_FAILURE); - break; - default: - fprintf (stderr, - "asn1Coding: ?? getopt returned character code Ox%x ??\n", - (unsigned)option_result); - } - } - - if (optind == argc || optind == argc - 1) - { - free (outputFileName); - fputs ("asn1Coding: input files missing\n", stderr); - usage (EXIT_FAILURE); - } - - 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: - fputs ("Parse: done.\n", stderr); - break; - case ASN1_FILE_NOT_FOUND: - fprintf (stderr, "asn1Coding: FILE %s NOT FOUND\n", inputFileAsnName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - fprintf (stderr, "asn1Coding: %s\n", errorDescription); - break; - default: - fprintf (stderr, "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) - { - fprintf (stderr, "asn1Coding: file '%s' not found\n", - inputFileAssignmentName); - free (inputFileAsnName); - free (inputFileAssignmentName); - exit (1); - } - - - putc ('\n', stderr); - - while ((last_ra = readAssignment (inputFile, varName, value)) - == ASSIGNMENT_SUCCESS) - { - fprintf (stderr, "var=%s, value=%s\n", varName, value); - if (structure == NULL) - { - asn1_result = asn1_create_element (definitions, value, &structure); - } - else - { - if (strcmp(value, "(NULL)") == 0) - asn1_result = asn1_write_value (structure, varName, NULL, 0); - else - asn1_result = asn1_write_value (structure, varName, value, 0); - } - - if (asn1_result != ASN1_SUCCESS) - { - fprintf (stderr, "libtasn1 ERROR: %s\n", - asn1_strerror (asn1_result)); - - asn1_delete_structure (&definitions); - asn1_delete_structure (&structure); - - free (inputFileAsnName); - free (inputFileAssignmentName); - - fclose (inputFile); - exit (1); - } - } - if (last_ra != ASSIGNMENT_EOF) - { - fprintf (stderr, "asn1Coding: error reading assignment file\n"); - exit (1); - } - fclose (inputFile); - - putc ('\n', stderr); - asn1_print_structure (stderr, 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, - errorDescription); - } - fprintf (stderr, "\nCoding: %s\n\n", asn1_strerror (asn1_result)); - if (asn1_result != ASN1_SUCCESS) - { - fprintf (stderr, "asn1Coding: %s\n", errorDescription); - - free (der); - - asn1_delete_structure (&definitions); - asn1_delete_structure (&structure); - - free (inputFileAsnName); - free (inputFileAssignmentName); - - exit (1); - } - - /* Print the 'Certificate1' DER encoding */ - fprintf (stderr, "-----------------\nNumber of bytes=%i\n", der_len); - for (k = 0; k < der_len; k++) - fprintf (stderr, "%02x ", der[k]); - fputs ("\n-----------------\n", stderr); - - asn1_delete_structure (&definitions); - asn1_delete_structure (&structure); - - if (!checkSyntaxOnly) - { - if (outputFileName == NULL) - createFileName (inputFileAssignmentName, &outputFileName); - - fprintf (stderr, "\nOutputFile=%s\n", outputFileName); - - outputFile = fopen (outputFileName, "w"); - - if (outputFile == NULL) - { - fprintf (stderr, - "asn1Coding: output file '%s' not available\n", - outputFileName); - free (der); - free (inputFileAsnName); - free (inputFileAssignmentName); - free (outputFileName); - exit (1); - } - - for (k = 0; k < der_len; k++) - fprintf (outputFile, "%c", der[k]); - fclose (outputFile); - fputs ("\nWriting: done.\n", stderr); - } - - free (der); - - free (inputFileAsnName); - free (inputFileAssignmentName); - free (outputFileName); - - exit (0); -} diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c deleted file mode 100644 index 65456bf..0000000 --- a/src/asn1Decoding.c +++ /dev/null @@ -1,327 +0,0 @@ -/* asn1Decoding.c --- program to generate an ASN1 type from a DER coding. - * Copyright (C) 2002-2014 Free Software Foundation, Inc. - * - * This file is part of LIBTASN1. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -#include <config.h> - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> -#include <getopt.h> - -#include <libtasn1.h> - -#include <progname.h> -#include <version-etc.h> -#include <read-file.h> -#include "benchmark.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 -#else -#define ATTR_NO_RETRUN __attribute__ ((__noreturn__)) -#endif - -ATTR_NO_RETRUN static void -usage (int status) -{ - if (status != EXIT_SUCCESS) - fprintf (stderr, "Try `%s --help' for more information.\n", program_name); - else - { - printf ("\ -Usage: %s [OPTION] DEFINITIONS ENCODED ASN1TYPE\n", program_name); - printf ("\ -Decodes DER data in ENCODED file, for the ASN1TYPE element\n\ -described in ASN.1 DEFINITIONS file, and print decoded structures.\n\ -\n"); - printf ("\ - -b, --benchmark perform a benchmark on decoding\n\ - -s, --strict use strict DER decoding\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"); - emit_bug_reporting_address (); - } - exit (status); -} - -int -main (int argc, char *argv[]) -{ - static const struct option long_options[] = { - {"help", no_argument, 0, 'h'}, - {"strict", no_argument, 0, 's'}, - {"no-time-strict", no_argument, 0, 't'}, - {"debug", no_argument, 0, 'd'}, - {"benchmark", no_argument, 0, 'b'}, - {"version", no_argument, 0, 'v'}, - {0, 0, 0, 0} - }; - int option_index = 0; - int option_result; - char *inputFileAsnName = NULL; - char *inputFileDerName = NULL; - char *typeName = NULL; - asn1_node definitions = NULL; - char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; - int asn1_result = ASN1_SUCCESS; - unsigned char *der; - int der_len = 0, benchmark = 0; - int flags = 0, debug = 0; - /* FILE *outputFile; */ - - set_program_name (argv[0]); - - opterr = 0; /* disable error messages from getopt */ - - while (1) - { - - option_result = - getopt_long (argc, argv, "hbdsvtc", long_options, &option_index); - - if (option_result == -1) - break; - - switch (option_result) - { - case 'h': /* HELP */ - usage (EXIT_SUCCESS); - break; - case 'b': - benchmark = 1; - break; - case 'd': - debug = 1; - break; - case 's': - case 't': - flags |= ASN1_DECODE_FLAG_STRICT_DER; - if (option_result == 't') - flags |= ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME; - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - exit (0); - break; - case '?': /* UNKNOW OPTION */ - fprintf (stderr, - "asn1Decoding: option '%s' not recognized or without argument.\n\n", - argv[optind - 1]); - usage (EXIT_FAILURE); - break; - default: - fprintf (stderr, - "asn1Decoding: ?? getopt returned character code Ox%x ??\n", - (unsigned)option_result); - } - } - - if (optind == argc || optind == argc - 1 || optind == argc - 2) - { - fprintf (stderr, "asn1Decoding: input files or ASN.1 type " - "name missing\n"); - usage (EXIT_FAILURE); - } - - 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); - } - - asn1_result = - asn1_parser2tree (inputFileAsnName, &definitions, errorDescription); - - switch (asn1_result) - { - case ASN1_SUCCESS: - fprintf (stderr, "Parse: done.\n"); - break; - case ASN1_FILE_NOT_FOUND: - fprintf (stderr, "asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - fprintf (stderr, "asn1Decoding: %s\n", errorDescription); - break; - default: - fprintf (stderr, "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; - } - - /* read_binary_file() returns a buffer with more data than required, - * with this reallocation we ensure that memory accesses outside the - * boundaries are detected */ - if (der != NULL && debug != 0) - der = realloc(der, der_len); - - if (der == NULL) - { - fprintf (stderr, "asn1Decoding: could not read '%s'\n", - inputFileDerName); - asn1_delete_structure (&definitions); - - 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); - */ - - if (decode (definitions, typeName, der, der_len, benchmark, flags) != ASN1_SUCCESS) - { - asn1_delete_structure (&definitions); - free (inputFileAsnName); - free (inputFileDerName); - free (typeName); - free (der); - exit (1); - } - - asn1_delete_structure (&definitions); - - free (der); - - free (inputFileAsnName); - free (inputFileDerName); - free (typeName); - - if (asn1_result != ASN1_SUCCESS) - exit (1); - - exit (0); -} - -static int -simple_decode (asn1_node definitions, const char *typeName, void *der, - int der_len, int benchmark, int flags) -{ - - int asn1_result; - asn1_node structure = NULL; - char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; - - asn1_result = asn1_create_element (definitions, typeName, &structure); - - /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */ - - - if (asn1_result != ASN1_SUCCESS) - { - fprintf (stderr, "Structure creation: %s\n", - asn1_strerror (asn1_result)); - asn1_delete_structure (&structure); - return asn1_result; - } - - if (flags != 0) - asn1_result = - asn1_der_decoding2(&structure, der, &der_len, flags, errorDescription); - else - asn1_result = - asn1_der_decoding (&structure, der, der_len, errorDescription); - - if (!benchmark) - fprintf (stderr, "\nDecoding: %s\n", asn1_strerror (asn1_result)); - if (asn1_result != ASN1_SUCCESS) - { - fprintf (stderr, "asn1Decoding: %s\n", errorDescription); - asn1_delete_structure (&structure); - return asn1_result; - } - - if (!benchmark) - { - fprintf (stderr, "\nDECODING RESULT:\n"); - asn1_print_structure (stdout, structure, "", - ASN1_PRINT_NAME_TYPE_VALUE); - } - asn1_delete_structure (&structure); - return ASN1_SUCCESS; -} - -static int -decode (asn1_node definitions, const char *typeName, void *der, int der_len, - int benchmark, int flags) -{ - struct benchmark_st st; - - if (benchmark == 0) - return simple_decode (definitions, typeName, der, der_len, benchmark, flags); - else - { - start_benchmark (&st); - - do - { - simple_decode (definitions, typeName, der, der_len, benchmark, flags); - st.size++; - } - while (benchmark_must_finish == 0); - - stop_benchmark (&st, "structures"); - fprintf (stdout, "\n"); - - } - return ASN1_SUCCESS; -} diff --git a/src/asn1Parser.c b/src/asn1Parser.c deleted file mode 100644 index 475bfc9..0000000 --- a/src/asn1Parser.c +++ /dev/null @@ -1,195 +0,0 @@ -/* asn1Parser.c -- program to parse a file with ASN1 definitions - * Copyright (C) 2002-2014 Free Software Foundation, Inc. - * - * This file is part of LIBTASN1. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -#include <config.h> - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> -#include <getopt.h> - -#include <libtasn1.h> - -#include <progname.h> -#include <version-etc.h> - -/* This feature is available in gcc versions 2.5 and later. */ -#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) -#define ATTR_NO_RETRUN -#else -#define ATTR_NO_RETRUN __attribute__ ((__noreturn__)) -#endif - -ATTR_NO_RETRUN static void -usage (int status) -{ - if (status != EXIT_SUCCESS) - fprintf (stderr, "Try `%s --help' for more information.\n", program_name); - else - { - printf ("\ -Usage: %s [OPTION] FILE\n", program_name); - printf ("\ -Read FILE with ASN.1 definitions and generate\n\ -a C array that is used with libtasn1 functions.\n\ -\n"); - printf ("\ -Mandatory arguments to long options are mandatory for short options too.\n\ - -c, --check checks the syntax only\n\ - -o, --output=FILE output file\n\ - -n, --name=NAME array name\n\ - -h, --help display this help and exit\n\ - -v, --version output version information and exit\n"); - emit_bug_reporting_address (); - } - exit (status); -} - -int -main (int argc, char *argv[]) -{ - static const 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_node pointer = NULL; - 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: - fprintf (stderr, "option %s", long_options[option_index].name); - if (optarg) - fprintf (stderr, " with arg %s", optarg); - putc ('\n', stderr); - break; - case 'h': /* HELP */ - free (outputFileName); - free (vectorName); - usage (EXIT_SUCCESS); - break; - case 'v': /* VERSION */ - version_etc (stdout, program_name, PACKAGE, VERSION, - "Fabio Fiorina", NULL); - free (outputFileName); - 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]); - free (outputFileName); - free (vectorName); - usage (EXIT_FAILURE); - break; - default: - fprintf (stderr, - "asn1Parser: ?? getopt returned character code Ox%x ??\n", - (unsigned)option_result); - } - - } - - if (optind == argc) - { - free (outputFileName); - free (vectorName); - usage (EXIT_SUCCESS); - } - 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: - fputs ("Done.\n", stderr); - break; - case ASN1_FILE_NOT_FOUND: - fprintf (stderr, "asn1Parser: file %s was not found\n", inputFileName); - break; - case ASN1_SYNTAX_ERROR: - case ASN1_IDENTIFIER_NOT_FOUND: - case ASN1_NAME_TOO_LONG: - fprintf (stderr, "asn1Parser: %s\n", errorDescription); - break; - default: - fprintf (stderr, "libtasn1 ERROR: %s\n", asn1_strerror (parse_result)); - } - - - free (inputFileName); - free (outputFileName); - free (vectorName); - - if (parse_result != ASN1_SUCCESS) - exit (1); - exit (0); -} diff --git a/src/benchmark.c b/src/benchmark.c deleted file mode 100644 index f36115b..0000000 --- a/src/benchmark.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (C) 2011-2014 Free Software Foundation, Inc. - * - * This file is part of GnuTLS. - * - * GnuTLS is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GnuTLS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <config.h> -#include <stdio.h> -#include <string.h> -#include <signal.h> -#include <sys/time.h> -#include <time.h> -#include <unistd.h> -#include "benchmark.h" - -int benchmark_must_finish = 0; - -#if defined _WIN32 -#include <windows.h> -static DWORD WINAPI -alarm_handler (LPVOID lpParameter) -{ - HANDLE wtimer = *((HANDLE *) lpParameter); - WaitForSingleObject (wtimer, INFINITE); - benchmark_must_finish = 1; - return 0; -} -#else -static void -alarm_handler (int signo) -{ - benchmark_must_finish = 1; -} -#endif - -static void -value2human (unsigned long bytes, double secs, double *data, double *speed, - char *metric) -{ - if (bytes > 1000 && bytes < 1000 * 1000) - { - *data = ((double) bytes) / 1000; - *speed = *data / secs; - strcpy (metric, "KB"); - return; - } - else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000) - { - *data = ((double) bytes) / (1000 * 1000); - *speed = *data / secs; - strcpy (metric, "MB"); - return; - } - else if (bytes >= 1000 * 1000 * 1000) - { - *data = ((double) bytes) / (1000 * 1000 * 1000); - *speed = *data / secs; - strcpy (metric, "GB"); - return; - } - else - { - *data = (double) bytes; - *speed = *data / secs; - strcpy (metric, "bytes"); - return; - } -} - -void -start_benchmark (struct benchmark_st *st) -{ - memset (st, 0, sizeof (*st)); -#ifndef _WIN32 - st->old_handler = signal (SIGALRM, alarm_handler); -#endif - gettime (&st->start); - benchmark_must_finish = 0; - -#if defined _WIN32 - st->wtimer = CreateWaitableTimer (NULL, TRUE, NULL); - if (st->wtimer == NULL) - { - fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); - exit (1); - } - st->wthread = CreateThread (NULL, 0, alarm_handler, &st->wtimer, 0, NULL); - if (st->wthread == NULL) - { - fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); - exit (1); - } - st->alarm_timeout.QuadPart = (5) * 10000000; - if (SetWaitableTimer (st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE) - == 0) - { - fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); - exit (1); - } -#else - alarm (5); -#endif - -} - -/* returns the elapsed time */ -double -stop_benchmark (struct benchmark_st *st, const char *metric) -{ - double secs; - unsigned long lsecs; - struct timespec stop; - double dspeed, ddata; - char imetric[16]; - -#if defined _WIN32 - if (st->wtimer != NULL) - CloseHandle (st->wtimer); - if (st->wthread != NULL) - CloseHandle (st->wthread); -#else - signal (SIGALRM, st->old_handler); -#endif - - gettime (&stop); - - lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - - (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000))); - secs = lsecs; - secs /= 1000; - - if (metric == NULL) - { /* assume bytes/sec */ - value2human (st->size, secs, &ddata, &dspeed, imetric); - printf (" Processed %.2f %s in %.2f secs: ", ddata, imetric, secs); - printf ("%.2f %s/sec\n", dspeed, imetric); - } - else - { - ddata = (double) st->size; - dspeed = ddata / secs; - printf (" Processed %.2f %s in %.2f secs: ", ddata, metric, secs); - printf ("%.2f %s/sec\n", dspeed, metric); - } - - return secs; -} diff --git a/src/benchmark.h b/src/benchmark.h deleted file mode 100644 index 1f45c56..0000000 --- a/src/benchmark.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2011-2014 Free Software Foundation, Inc. - * - * This file is part of GnuTLS. - * - * GnuTLS is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GnuTLS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <sys/time.h> -#include <time.h> -#include <signal.h> -#if defined _WIN32 -#include <windows.h> -#endif -#include "timespec.h" /* gnulib gettime */ - -typedef void (*sighandler_t) (int); - -struct benchmark_st -{ - struct timespec start; - unsigned long size; - sighandler_t old_handler; -#if defined _WIN32 - HANDLE wtimer; - HANDLE wthread; - LARGE_INTEGER alarm_timeout; -#endif -}; - -extern int benchmark_must_finish; - -void start_benchmark (struct benchmark_st *st); -double stop_benchmark (struct benchmark_st *st, const char *metric); - -inline static unsigned int -timespec_sub_ms (struct timespec *a, struct timespec *b) -{ - return (a->tv_sec * 1000 + a->tv_nsec / (1000 * 1000) - - (b->tv_sec * 1000 + b->tv_nsec / (1000 * 1000))); -} |