From 41a1da63e5f812899be173dfb1b92d0c1eb981f5 Mon Sep 17 00:00:00 2001 From: Lev Walkin Date: Sat, 23 Jan 2016 08:50:28 -0800 Subject: [PATCH] autoformatted (no semantic change) --- asn1c/asn1c.c | 702 ++++++++++++------------- asn1c/enber.c | 549 +++++++++---------- asn1c/sys-common.h | 43 +- asn1c/unber.c | 1252 +++++++++++++++++++++++--------------------- 4 files changed, 1308 insertions(+), 1238 deletions(-) diff --git a/asn1c/asn1c.c b/asn1c/asn1c.c index 3eec8497..8a6f785e 100644 --- a/asn1c/asn1c.c +++ b/asn1c/asn1c.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003-2014 + * Copyright (c) 2003-2016 * Lev Walkin . All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,314 +31,306 @@ */ #include "sys-common.h" -#undef COPYRIGHT -#define COPYRIGHT \ - "Copyright (c) 2003-2014 Lev Walkin \n" +#undef COPYRIGHT +#define COPYRIGHT "Copyright (c) 2003-2016 Lev Walkin \n" -#include /* Parse the ASN.1 file and build a tree */ -#include /* Fix the ASN.1 tree */ -#include /* Print the ASN.1 tree */ -#include /* Compile the ASN.1 tree */ +#include /* Parse the ASN.1 file and build a tree */ +#include /* Fix the ASN.1 tree */ +#include /* Print the ASN.1 tree */ +#include /* Compile the ASN.1 tree */ -#include /* Portable basename(3) and dirname(3) */ +#include /* Portable basename(3) and dirname(3) */ -#ifdef _WIN32 +#ifdef _WIN32 #include #include #else #include #endif -static void usage(const char *av0); /* Print the Usage screen and exit */ +static void usage(const char *av0); /* Print the Usage screen and exit */ static int importStandardModules(asn1p_t *asn, const char *skeletons_dir); int main(int ac, char **av) { - enum asn1p_flags asn1_parser_flags = A1P_NOFLAGS; - enum asn1f_flags asn1_fixer_flags = A1F_NOFLAGS; - enum asn1c_flags asn1_compiler_flags= A1C_NO_C99; - enum asn1print_flags asn1_printer_flags = APF_NOFLAGS; - int print_arg__print_out = 0; /* Don't compile, just print parsed */ - int print_arg__fix_n_print = 0; /* Fix and print */ - int warnings_as_errors = 0; /* Treat warnings as errors */ - char *skeletons_dir = NULL; /* Directory with supplementary stuff */ - asn1p_t *asn = 0; /* An ASN.1 parsed tree */ - int ret; /* Return value from misc functions */ - int ch; /* Command line character */ - int i; /* Index in some loops */ + enum asn1p_flags asn1_parser_flags = A1P_NOFLAGS; + enum asn1f_flags asn1_fixer_flags = A1F_NOFLAGS; + enum asn1c_flags asn1_compiler_flags = A1C_NO_C99; + enum asn1print_flags asn1_printer_flags = APF_NOFLAGS; + int print_arg__print_out = 0; /* Don't compile, just print parsed */ + int print_arg__fix_n_print = 0; /* Fix and print */ + int warnings_as_errors = 0; /* Treat warnings as errors */ + char *skeletons_dir = NULL; /* Directory with supplementary stuff */ + asn1p_t *asn = 0; /* An ASN.1 parsed tree */ + int ret; /* Return value from misc functions */ + int ch; /* Command line character */ + int i; /* Index in some loops */ - /* - * Process command-line options. - */ - while((ch = getopt(ac, av, "EFf:g:hLPp:RS:vW:X")) != -1) - switch(ch) { - case 'E': - print_arg__print_out = 1; - break; - case 'F': - print_arg__fix_n_print = 1; - break; - case 'f': - if(strcmp(optarg, "all-defs-global") == 0) { - asn1_compiler_flags |= A1C_ALL_DEFS_GLOBAL; - } else if(strcmp(optarg, "bless-SIZE") == 0) { - asn1_fixer_flags |= A1F_EXTENDED_SizeConstraint; - } else if(strcmp(optarg, "compound-names") == 0) { - asn1_compiler_flags |= A1C_COMPOUND_NAMES; - } else if(strcmp(optarg, "indirect-choice") == 0) { - asn1_compiler_flags |= A1C_INDIRECT_CHOICE; - } else if(strncmp(optarg, "known-extern-type=", 18) == 0) { - char *known_type = optarg + 18; - ret = asn1f_make_known_external_type(known_type); - assert(ret == 0 || errno == EEXIST); - } else if(strcmp(optarg, "native-types") == 0) { - fprintf(stderr, "-f%s: Deprecated option\n", optarg); - asn1_compiler_flags &= ~A1C_USE_WIDE_TYPES; - } else if(strcmp(optarg, "wide-types") == 0) { - asn1_compiler_flags |= A1C_USE_WIDE_TYPES; - } else if(strcmp(optarg, "line-refs") == 0) { - asn1_compiler_flags |= A1C_LINE_REFS; - } else if(strcmp(optarg, "no-constraints") == 0) { - asn1_compiler_flags |= A1C_NO_CONSTRAINTS; - } else if(strcmp(optarg, "no-include-deps") == 0) { - asn1_compiler_flags |= A1C_NO_INCLUDE_DEPS; - } else if(strcmp(optarg, "includes-quoted") == 0) { - asn1_compiler_flags |= A1C_INCLUDES_QUOTED; - } else if(strcmp(optarg, "unnamed-unions") == 0) { - asn1_compiler_flags |= A1C_UNNAMED_UNIONS; - } else if(strcmp(optarg, "skeletons-copy") == 0) { - fprintf(stderr, "-f%s: Deprecated option\n", optarg); - asn1_compiler_flags &= ~A1C_LINK_SKELETONS; - } else if(strcmp(optarg, "link-skeletons") == 0) { - asn1_compiler_flags |= A1C_LINK_SKELETONS; - } else { - fprintf(stderr, "-f%s: Invalid argument\n", optarg); - exit(EX_USAGE); - } - break; - case 'g': - if(strcmp(optarg, "en-PER") == 0) { - asn1_compiler_flags |= A1C_GEN_PER; - } else { - fprintf(stderr, "-g%s: Invalid argument\n", optarg); - exit(EX_USAGE); - } - break; - case 'h': - usage(av[0]); - case 'P': - asn1_compiler_flags |= A1C_PRINT_COMPILED; - asn1_compiler_flags &= ~A1C_NO_C99; - break; - case 'p': - if(strncmp(optarg, "du=", 3) == 0) { - char *pduname = optarg + 3; - if(strcmp(pduname, "all") == 0) { - asn1_compiler_flags |= A1C_PDU_ALL; - } else if(strcmp(pduname, "auto") == 0) { - asn1_compiler_flags |= A1C_PDU_AUTO; - } else if(pduname[0] >= 'A' && pduname[0] <= 'Z') { - asn1c__add_pdu_type(pduname); - asn1_compiler_flags |= A1C_PDU_TYPE; - } else { - fprintf(stderr, "-pdu=%s" - ": expected -pdu={all|auto|Type}\n", - pduname); - exit(EX_USAGE); - } - } else if(strcmp(optarg, "rint-class-matrix") == 0) { - asn1_printer_flags |= APF_PRINT_CLASS_MATRIX; - } else if(strcmp(optarg, "rint-constraints") == 0) { - asn1_printer_flags |= APF_PRINT_CONSTRAINTS; - } else if(strcmp(optarg, "rint-lines") == 0) { - asn1_printer_flags |= APF_LINE_COMMENTS; - } else { - fprintf(stderr, "-p%s: Invalid argument\n", optarg); - exit(EX_USAGE); - } - break; - case 'R': - asn1_compiler_flags |= A1C_OMIT_SUPPORT_CODE; - break; - case 'S': - skeletons_dir = optarg; - break; - case 'v': - fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT); - exit(0); - break; - case 'W': - if(strcmp(optarg, "error") == 0) { - warnings_as_errors = 1; - break; - } else if(strcmp(optarg, "debug-lexer") == 0) { - asn1_parser_flags |= A1P_LEXER_DEBUG; - break; - } else if(strcmp(optarg, "debug-fixer") == 0) { - asn1_fixer_flags |= A1F_DEBUG; - break; - } else if(strcmp(optarg, "debug-compiler") == 0) { - asn1_compiler_flags |= A1C_DEBUG; - break; - } else { - fprintf(stderr, "-W%s: Invalid argument\n", optarg); - exit(EX_USAGE); - } - break; - case 'X': - print_arg__print_out = 1; /* Implicit -E */ - print_arg__fix_n_print = 1; /* Implicit -F */ - asn1_printer_flags |= APF_PRINT_XML_DTD; - break; - default: - usage(av[0]); - } + /* + * Process command-line options. + */ + while((ch = getopt(ac, av, "EFf:g:hLPp:RS:vW:X")) != -1) switch(ch) { + case 'E': + print_arg__print_out = 1; + break; + case 'F': + print_arg__fix_n_print = 1; + break; + case 'f': + if(strcmp(optarg, "all-defs-global") == 0) { + asn1_compiler_flags |= A1C_ALL_DEFS_GLOBAL; + } else if(strcmp(optarg, "bless-SIZE") == 0) { + asn1_fixer_flags |= A1F_EXTENDED_SizeConstraint; + } else if(strcmp(optarg, "compound-names") == 0) { + asn1_compiler_flags |= A1C_COMPOUND_NAMES; + } else if(strcmp(optarg, "indirect-choice") == 0) { + asn1_compiler_flags |= A1C_INDIRECT_CHOICE; + } else if(strncmp(optarg, "known-extern-type=", 18) == 0) { + char *known_type = optarg + 18; + ret = asn1f_make_known_external_type(known_type); + assert(ret == 0 || errno == EEXIST); + } else if(strcmp(optarg, "native-types") == 0) { + fprintf(stderr, "-f%s: Deprecated option\n", optarg); + asn1_compiler_flags &= ~A1C_USE_WIDE_TYPES; + } else if(strcmp(optarg, "wide-types") == 0) { + asn1_compiler_flags |= A1C_USE_WIDE_TYPES; + } else if(strcmp(optarg, "line-refs") == 0) { + asn1_compiler_flags |= A1C_LINE_REFS; + } else if(strcmp(optarg, "no-constraints") == 0) { + asn1_compiler_flags |= A1C_NO_CONSTRAINTS; + } else if(strcmp(optarg, "no-include-deps") == 0) { + asn1_compiler_flags |= A1C_NO_INCLUDE_DEPS; + } else if(strcmp(optarg, "includes-quoted") == 0) { + asn1_compiler_flags |= A1C_INCLUDES_QUOTED; + } else if(strcmp(optarg, "unnamed-unions") == 0) { + asn1_compiler_flags |= A1C_UNNAMED_UNIONS; + } else if(strcmp(optarg, "skeletons-copy") == 0) { + fprintf(stderr, "-f%s: Deprecated option\n", optarg); + asn1_compiler_flags &= ~A1C_LINK_SKELETONS; + } else if(strcmp(optarg, "link-skeletons") == 0) { + asn1_compiler_flags |= A1C_LINK_SKELETONS; + } else { + fprintf(stderr, "-f%s: Invalid argument\n", optarg); + exit(EX_USAGE); + } + break; + case 'g': + if(strcmp(optarg, "en-PER") == 0) { + asn1_compiler_flags |= A1C_GEN_PER; + } else { + fprintf(stderr, "-g%s: Invalid argument\n", optarg); + exit(EX_USAGE); + } + break; + case 'h': + usage(av[0]); + case 'P': + asn1_compiler_flags |= A1C_PRINT_COMPILED; + asn1_compiler_flags &= ~A1C_NO_C99; + break; + case 'p': + if(strncmp(optarg, "du=", 3) == 0) { + char *pduname = optarg + 3; + if(strcmp(pduname, "all") == 0) { + asn1_compiler_flags |= A1C_PDU_ALL; + } else if(strcmp(pduname, "auto") == 0) { + asn1_compiler_flags |= A1C_PDU_AUTO; + } else if(pduname[0] >= 'A' && pduname[0] <= 'Z') { + asn1c__add_pdu_type(pduname); + asn1_compiler_flags |= A1C_PDU_TYPE; + } else { + fprintf(stderr, "-pdu=%s: expected -pdu={all|auto|Type}\n", + pduname); + exit(EX_USAGE); + } + } else if(strcmp(optarg, "rint-class-matrix") == 0) { + asn1_printer_flags |= APF_PRINT_CLASS_MATRIX; + } else if(strcmp(optarg, "rint-constraints") == 0) { + asn1_printer_flags |= APF_PRINT_CONSTRAINTS; + } else if(strcmp(optarg, "rint-lines") == 0) { + asn1_printer_flags |= APF_LINE_COMMENTS; + } else { + fprintf(stderr, "-p%s: Invalid argument\n", optarg); + exit(EX_USAGE); + } + break; + case 'R': + asn1_compiler_flags |= A1C_OMIT_SUPPORT_CODE; + break; + case 'S': + skeletons_dir = optarg; + break; + case 'v': + fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT); + exit(0); + break; + case 'W': + if(strcmp(optarg, "error") == 0) { + warnings_as_errors = 1; + break; + } else if(strcmp(optarg, "debug-lexer") == 0) { + asn1_parser_flags |= A1P_LEXER_DEBUG; + break; + } else if(strcmp(optarg, "debug-fixer") == 0) { + asn1_fixer_flags |= A1F_DEBUG; + break; + } else if(strcmp(optarg, "debug-compiler") == 0) { + asn1_compiler_flags |= A1C_DEBUG; + break; + } else { + fprintf(stderr, "-W%s: Invalid argument\n", optarg); + exit(EX_USAGE); + } + break; + case 'X': + print_arg__print_out = 1; /* Implicit -E */ + print_arg__fix_n_print = 1; /* Implicit -F */ + asn1_printer_flags |= APF_PRINT_XML_DTD; + break; + default: + usage(av[0]); + } - /* - * Validate the options combination. - */ - if(!print_arg__print_out) { - if(print_arg__fix_n_print) { - fprintf(stderr, "Error: -F requires -E\n"); - exit(EX_USAGE); - } - if(asn1_printer_flags) { - fprintf(stderr, "Error: " - "-print-... arguments require -E\n"); - exit(EX_USAGE); - } - } + /* + * Validate the options combination. + */ + if(!print_arg__print_out) { + if(print_arg__fix_n_print) { + fprintf(stderr, "Error: -F requires -E\n"); + exit(EX_USAGE); + } + if(asn1_printer_flags) { + fprintf(stderr, "Error: -print-... arguments require -E\n"); + exit(EX_USAGE); + } + } - /* - * Ensure that there are some input files present. - */ - if(ac > optind) { - ac -= optind; - av += optind; - } else { - char *bin_name = a1c_basename(av[0]); - fprintf(stderr, "%s: No input files specified. " - "Try '%s -h' for more information\n", - bin_name, bin_name); - exit(1); - } + /* + * Ensure that there are some input files present. + */ + if(ac > optind) { + ac -= optind; + av += optind; + } else { + char *bin_name = a1c_basename(av[0]); + fprintf(stderr, + "%s: No input files specified. " + "Try '%s -h' for more information\n", + bin_name, bin_name); + exit(1); + } - /* - * Make sure the skeleton directory is out there. - */ - if(skeletons_dir == NULL) { - struct stat sb; - skeletons_dir = DATADIR; - if((av[-optind][0] == '.' || av[-optind][1] == '/') - && stat(skeletons_dir, &sb)) { - /* - * The default skeletons directory does not exist, - * compute it from my file name: - * ./asn1c/asn1c -> ./skeletons - */ - char *p; - size_t len; + /* + * Make sure the skeleton directory is out there. + */ + if(skeletons_dir == NULL) { + struct stat sb; + skeletons_dir = DATADIR; + if((av[-optind][0] == '.' || av[-optind][1] == '/') + && stat(skeletons_dir, &sb)) { + /* + * The default skeletons directory does not exist, + * compute it from my file name: + * ./asn1c/asn1c -> ./skeletons + */ + char *p; + size_t len; - p = a1c_dirname(av[-optind]); + p = a1c_dirname(av[-optind]); - len = strlen(p) + sizeof("/../skeletons"); - skeletons_dir = malloc(len); - assert(skeletons_dir); - snprintf(skeletons_dir, len, "%s/../skeletons", p); - if(stat(skeletons_dir, &sb)) { - fprintf(stderr, - "WARNING: skeletons are neither in " - "\"%s\" nor in \"%s\"!\n", - DATADIR, skeletons_dir); - if(warnings_as_errors) - exit(EX_OSFILE); - } - } - } + len = strlen(p) + sizeof("/../skeletons"); + skeletons_dir = malloc(len); + assert(skeletons_dir); + snprintf(skeletons_dir, len, "%s/../skeletons", p); + if(stat(skeletons_dir, &sb)) { + fprintf(stderr, + "WARNING: skeletons are neither in " + "\"%s\" nor in \"%s\"!\n", + DATADIR, skeletons_dir); + if(warnings_as_errors) exit(EX_OSFILE); + } + } + } - /* - * Iterate over input files and parse each. - * All syntax trees from all files will be bundled together. - */ - for(i = 0; i < ac; i++) { - asn1p_t *new_asn; + /* + * Iterate over input files and parse each. + * All syntax trees from all files will be bundled together. + */ + for(i = 0; i < ac; i++) { + asn1p_t *new_asn; - new_asn = asn1p_parse_file(av[i], asn1_parser_flags); - if(new_asn == NULL) { - fprintf(stderr, "Cannot parse \"%s\"\n", av[i]); - exit(EX_DATAERR); - } + new_asn = asn1p_parse_file(av[i], asn1_parser_flags); + if(new_asn == NULL) { + fprintf(stderr, "Cannot parse \"%s\"\n", av[i]); + exit(EX_DATAERR); + } - /* - * Bundle the parsed tree with existing one. - */ - if(asn) { - asn1p_module_t *mod; - while((mod = TQ_REMOVE(&(new_asn->modules), mod_next))) - TQ_ADD(&(asn->modules), mod, mod_next); - asn1p_delete(new_asn); - } else { - asn = new_asn; - } - } + /* + * Bundle the parsed tree with existing one. + */ + if(asn) { + asn1p_module_t *mod; + while((mod = TQ_REMOVE(&(new_asn->modules), mod_next))) + TQ_ADD(&(asn->modules), mod, mod_next); + asn1p_delete(new_asn); + } else { + asn = new_asn; + } + } - /* These are mostly notes for the human readers */ - assert(asn); - assert(skeletons_dir); + /* These are mostly notes for the human readers */ + assert(asn); + assert(skeletons_dir); - /* - * Dump the parsed ASN.1 tree if -E specified and -F is NOT given. - */ - if(print_arg__print_out && !print_arg__fix_n_print) { - if(asn1print(asn, asn1_printer_flags)) - exit(EX_SOFTWARE); - return 0; - } + /* + * Dump the parsed ASN.1 tree if -E specified and -F is NOT given. + */ + if(print_arg__print_out && !print_arg__fix_n_print) { + if(asn1print(asn, asn1_printer_flags)) exit(EX_SOFTWARE); + return 0; + } - /* - * Read in the files from skeletons/standard-modules - */ - if(importStandardModules(asn, skeletons_dir)) { - if(warnings_as_errors) - exit(EX_DATAERR); - } + /* + * Read in the files from skeletons/standard-modules + */ + if(importStandardModules(asn, skeletons_dir)) { + if(warnings_as_errors) exit(EX_DATAERR); + } - /* - * Process the ASN.1 specification: perform semantic checks, - * expand references, etc, etc. - * This function will emit necessary warnings and error messages. - */ - ret = asn1f_process(asn, asn1_fixer_flags, - NULL /* default fprintf(stderr) */); - switch(ret) { - case 1: - if(!warnings_as_errors) - /* Fall through */ - case 0: - break; /* All clear */ - case -1: - exit(EX_DATAERR); /* Fatal failure */ - } + /* + * Process the ASN.1 specification: perform semantic checks, + * expand references, etc, etc. + * This function will emit necessary warnings and error messages. + */ + ret = asn1f_process(asn, asn1_fixer_flags, + NULL /* default fprintf(stderr) */); + switch(ret) { + case 1: + if(!warnings_as_errors) /* Fall through */ + case 0: + break; /* All clear */ + case -1: + exit(EX_DATAERR); /* Fatal failure */ + } - /* - * Dump the parsed ASN.1 tree if -E specified and -F is given. - */ - if(print_arg__print_out && print_arg__fix_n_print) { - if(asn1print(asn, asn1_printer_flags)) - exit(EX_SOFTWARE); - return 0; - } + /* + * Dump the parsed ASN.1 tree if -E specified and -F is given. + */ + if(print_arg__print_out && print_arg__fix_n_print) { + if(asn1print(asn, asn1_printer_flags)) exit(EX_SOFTWARE); + return 0; + } - /* - * Compile the ASN.1 tree into a set of source files - * of another language. - */ - if(asn1_compile(asn, skeletons_dir, asn1_compiler_flags, - ac + optind, optind - 1, av - optind)) { - exit(EX_SOFTWARE); - } + /* + * Compile the ASN.1 tree into a set of source files + * of another language. + */ + if(asn1_compile(asn, skeletons_dir, asn1_compiler_flags, ac + optind, + optind - 1, av - optind)) { + exit(EX_SOFTWARE); + } - return 0; + return 0; } /* @@ -346,94 +338,92 @@ main(int ac, char **av) { */ static int importStandardModules(asn1p_t *asn, const char *skeletons_dir) { - asn1p_t *new_asn; - asn1p_module_t *mod; - const char *filename; - char *fullname; - char *target_dir; - int target_dir_len; - int len; -#ifdef _WIN32 - intptr_t dir; - struct _finddata_t c_file; - char *pattern; + asn1p_t *new_asn; + asn1p_module_t *mod; + const char *filename; + char *fullname; + char *target_dir; + int target_dir_len; + int len; +#ifdef _WIN32 + intptr_t dir; + struct _finddata_t c_file; + char *pattern; #else - struct dirent *dp; - DIR *dir; + struct dirent *dp; + DIR *dir; #endif - int ret = 0; + int ret = 0; - /* Notes for the human reader */ - assert(asn); - assert(skeletons_dir); + /* Notes for the human reader */ + assert(asn); + assert(skeletons_dir); - /* - * Figure out the standard-modules directory. - */ - target_dir_len = strlen(skeletons_dir) - + sizeof("/standard-modules") - 1; - target_dir = malloc(target_dir_len + 1); - assert(target_dir); - snprintf(target_dir, target_dir_len + 1, "%s/standard-modules", - skeletons_dir); + /* + * Figure out the standard-modules directory. + */ + target_dir_len = strlen(skeletons_dir) + sizeof("/standard-modules") - 1; + target_dir = malloc(target_dir_len + 1); + assert(target_dir); + snprintf(target_dir, target_dir_len + 1, "%s/standard-modules", + skeletons_dir); -#ifdef _WIN32 - len = target_dir_len + sizeof("/*.asn1"); - pattern = malloc(len); - assert(pattern); - snprintf(pattern, len, "%s/*.asn1", target_dir); - dir = _findfirst(pattern, &c_file); - if(dir == -1L) { +#ifdef _WIN32 + len = target_dir_len + sizeof("/*.asn1"); + pattern = malloc(len); + assert(pattern); + snprintf(pattern, len, "%s/*.asn1", target_dir); + dir = _findfirst(pattern, &c_file); + if(dir == -1L) { #else - dir = opendir(target_dir); - if(!dir) { + dir = opendir(target_dir); + if(!dir) { #endif - fprintf(stderr, - "WARNING: Cannot find standard modules in %s\n", - target_dir); - return -1; - } + fprintf(stderr, "WARNING: Cannot find standard modules in %s\n", + target_dir); + return -1; + } -#ifdef _WIN32 - do { - filename = c_file.name; +#ifdef _WIN32 + do { + filename = c_file.name; #else - while((dp = readdir(dir))) { - filename = dp->d_name; + while((dp = readdir(dir))) { + filename = dp->d_name; #endif - len = strlen(filename); - if(len <= 5 || strcmp(filename + len - 5, ".asn1")) - continue; - len = target_dir_len + 1 + len + 1; - fullname = malloc(len); - if(!fullname) continue; /* Just skip it, no big deal */ - snprintf(fullname, len, "%s/%s", target_dir, filename); - filename = fullname; + len = strlen(filename); + if(len <= 5 || strcmp(filename + len - 5, ".asn1")) continue; + len = target_dir_len + 1 + len + 1; + fullname = malloc(len); + if(!fullname) continue; /* Just skip it, no big deal */ + snprintf(fullname, len, "%s/%s", target_dir, filename); + filename = fullname; - new_asn = asn1p_parse_file(filename, A1P_NOFLAGS); - if(new_asn == NULL) { - fprintf(stderr, "WARNING: Cannot parse standard module \"%s\"\n", filename); - ret = -1; - continue; - } + new_asn = asn1p_parse_file(filename, A1P_NOFLAGS); + if(new_asn == NULL) { + fprintf(stderr, "WARNING: Cannot parse standard module \"%s\"\n", + filename); + ret = -1; + continue; + } - /* Import these modules and mark them as "standard" */ - while((mod = TQ_REMOVE(&(new_asn->modules), mod_next))) { - mod->_tags |= MT_STANDARD_MODULE; - TQ_ADD(&(asn->modules), mod, mod_next); - } - asn1p_delete(new_asn); + /* Import these modules and mark them as "standard" */ + while((mod = TQ_REMOVE(&(new_asn->modules), mod_next))) { + mod->_tags |= MT_STANDARD_MODULE; + TQ_ADD(&(asn->modules), mod, mod_next); + } + asn1p_delete(new_asn); -#ifdef _WIN32 - } while(_findnext(dir, &c_file) == 0); - _findclose(dir); +#ifdef _WIN32 + } while(_findnext(dir, &c_file) == 0); + _findclose(dir); #else - free(fullname); - } /* while(readdir()) */ - closedir(dir); + free(fullname); + } /* while(readdir()) */ + closedir(dir); #endif - return ret; + return ret; } /* @@ -441,6 +431,7 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) { */ static void usage(const char *av0) { + /* clang-format off */ fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT "Usage: %s [options] file ...\n" @@ -482,8 +473,7 @@ usage(const char *av0) { " -print-lines Generate \"-- #line\" comments in -E output\n" , - a1c_basename(av0), DATADIR - ); - exit(EX_USAGE); + a1c_basename(av0), DATADIR); + /* clang-format on */ + exit(EX_USAGE); } - diff --git a/asn1c/enber.c b/asn1c/enber.c index 6ba485bf..ec06b5c6 100644 --- a/asn1c/enber.c +++ b/asn1c/enber.c @@ -26,67 +26,64 @@ */ #include "sys-common.h" -#include /* For static string tables */ +#include /* For static string tables */ #include #include #include #include -#undef COPYRIGHT -#define COPYRIGHT \ - "Copyright (c) 2004, 2005 Lev Walkin \n" +#undef COPYRIGHT +#define COPYRIGHT "Copyright (c) 2004, 2005 Lev Walkin \n" -static void usage(const char *av0, int);/* Print the Usage screen and exit */ -static int process(const char *fname); /* Perform the BER decoding */ +static void usage(const char *av0, int); /* Print the Usage screen and exit */ +static int process(const char *fname); /* Perform the BER decoding */ static int process_line(const char *fname, char *line, int lineno); -static int no_validation; /* -n */ +static int no_validation; /* -n */ int main(int ac, char **av) { - int ch; /* Command line character */ - int i; /* Index in some loops */ + int ch; /* Command line character */ + int i; /* Index in some loops */ - /* - * Process command-line options. - */ - while((ch = getopt(ac, av, "nhv")) != -1) - switch(ch) { - case 'n': - no_validation++; - break; - case 'v': - usage(av[0], 1); - break; - case 'h': - default: - usage(av[0], 0); - } + /* + * Process command-line options. + */ + while((ch = getopt(ac, av, "nhv")) != -1) switch(ch) { + case 'n': + no_validation++; + break; + case 'v': + usage(av[0], 1); + break; + case 'h': + default: + usage(av[0], 0); + } - /* - * Ensure that there are some input files present. - */ - if(ac > optind) { - ac -= optind; - av += optind; - } else { - fprintf(stderr, "%s: No input files specified\n", av[0]); - exit(1); - } + /* + * Ensure that there are some input files present. + */ + if(ac > optind) { + ac -= optind; + av += optind; + } else { + fprintf(stderr, "%s: No input files specified\n", av[0]); + exit(1); + } - setvbuf(stdout, 0, _IOLBF, 0); + setvbuf(stdout, 0, _IOLBF, 0); - /* - * Iterate over input files and parse each. - * All syntax trees from all files will be bundled together. - */ - for(i = 0; i < ac; i++) { - if(process(av[i])) - exit(EX_DATAERR); - } + /* + * Iterate over input files and parse each. + * All syntax trees from all files will be bundled together. + */ + for(i = 0; i < ac; i++) { + if(process(av[i])) exit(EX_DATAERR); + } - return 0; + return 0; } /* @@ -94,15 +91,16 @@ main(int ac, char **av) { */ static void usage(const char *av0, int copyright_only) { - fprintf(stderr, - "Convert unber(1)'s output back into BER, " - "v" VERSION "\n" COPYRIGHT); - if(copyright_only) exit(0); - fprintf(stderr, - "Usage: %s [-n] [-] [file ...]\n" - "Options:\n" - " -n Disable XML input validation\n", av0); - exit(EX_USAGE); + fprintf(stderr, + "Convert unber(1)'s output back into BER, " + "v" VERSION "\n" COPYRIGHT); + if(copyright_only) exit(0); + fprintf(stderr, + "Usage: %s [-n] [-] [file ...]\n" + "Options:\n" + " -n Disable XML input validation\n", + av0); + exit(EX_USAGE); } /* @@ -110,169 +108,175 @@ usage(const char *av0, int copyright_only) { */ static int process(const char *fname) { - char buf[8192]; - char *collector = 0; - size_t collector_size = sizeof(buf); - size_t collector_offset = 0; - int lineno = 0; - FILE *fp; + char buf[8192]; + char *collector = 0; + size_t collector_size = sizeof(buf); + size_t collector_offset = 0; + int lineno = 0; + FILE *fp; - if(strcmp(fname, "-")) { - fp = fopen(fname, "r"); - if(!fp) { - perror(fname); - return -1; - } - } else { - fp = stdin; - } + if(strcmp(fname, "-")) { + fp = fopen(fname, "r"); + if(!fp) { + perror(fname); + return -1; + } + } else { + fp = stdin; + } - while(fgets(buf, sizeof(buf), fp) || !feof(fp)) { - size_t len = strlen(buf); + while(fgets(buf, sizeof(buf), fp) || !feof(fp)) { + size_t len = strlen(buf); - if(!len) continue; - if(collector_offset || buf[len-1] != '\n') { - if((collector_size - collector_offset) <= len - || !collector) { - collector_size <<= 1; - collector = REALLOC(collector, collector_size); - if(!collector) { - perror("realloc()"); - exit(EX_OSERR); - } - } - memcpy(collector + collector_offset, buf, len + 1); - collector_offset += len; - } - if(buf[len-1] != '\n') continue; + if(!len) continue; + if(collector_offset || buf[len - 1] != '\n') { + if((collector_size - collector_offset) <= len || !collector) { + collector_size <<= 1; + collector = REALLOC(collector, collector_size); + if(!collector) { + perror("realloc()"); + exit(EX_OSERR); + } + } + memcpy(collector + collector_offset, buf, len + 1); + collector_offset += len; + } + if(buf[len - 1] != '\n') continue; - if(collector_offset) { - assert(collector[collector_offset-1] == '\n'); - process_line(fname, collector, ++lineno); - collector_offset = 0; - } else { - process_line(fname, buf, ++lineno); - } - } + if(collector_offset) { + assert(collector[collector_offset - 1] == '\n'); + process_line(fname, collector, ++lineno); + collector_offset = 0; + } else { + process_line(fname, buf, ++lineno); + } + } - if(fp != stdin) - fclose(fp); + if(fp != stdin) fclose(fp); - return 0; + return 0; } static int process_line(const char *fname, char *line, int lineno) { - char buf[32]; - char *op; /* '<' */ - char *cl; /* '>' */ - char *tcl_pos; /* tag class (T=") position */ - char *tl_pos; /* tag length (TL=") position */ - char *v_pos; /* value length (V=") position */ - int constr; - ber_tlv_tag_t tag_value; - ber_tlv_tag_t tag_class; - ber_tlv_tag_t tlv_tag; - ber_tlv_len_t tlv_len; - ber_tlv_len_t opt_tl_len; /* optional TL length */ - ssize_t ret; - (void)fname; + char buf[32]; + char *op; /* '<' */ + char *cl; /* '>' */ + char *tcl_pos; /* tag class (T=") position */ + char *tl_pos; /* tag length (TL=") position */ + char *v_pos; /* value length (V=") position */ + int constr; + ber_tlv_tag_t tag_value; + ber_tlv_tag_t tag_class; + ber_tlv_tag_t tlv_tag; + ber_tlv_len_t tlv_len; + ber_tlv_len_t opt_tl_len; /* optional TL length */ + ssize_t ret; + (void)fname; - /* Skip the whitespace */ - for(; *line == ' ' || *line == '\t'; line++); + /* Skip the whitespace */ + for(; *line == ' ' || *line == '\t'; line++) + ; - /* Find a tag opening angle bracket */ - op = line; - switch(*op) { - case '<': /* That's what we want! A tag opening */ - break; - case '-': /* This is a comment (dash-dash) */ - if(op[1] == *op) - case '\r': - case '\n': - case '#': /* This is a comment */ - return 0; - default: - fprintf(stderr, - "%s: Missing '<' after whitespace at line %d\n", - fname, lineno); - exit(EX_DATAERR); - } + /* Find a tag opening angle bracket */ + op = line; + switch(*op) { + case '<': /* That's what we want! A tag opening */ + break; + case '-': /* This is a comment (dash-dash) */ + if(op[1] == *op) case '\r': + case '\n': + case '#': /* This is a comment */ + return 0; + default: + fprintf(stderr, "%s: Missing '<' after whitespace at line %d\n", fname, + lineno); + exit(EX_DATAERR); + } - /* Find a tag closing angle bracket */ - for(; *line && *line != '>'; line++) { - if(*line < ' ') { - fprintf(stderr, "%s: Invalid charset (%d) at line %d\n", - fname, *(const unsigned char *)line, lineno); - exit(EX_DATAERR); - } - } - cl = line; - if(*cl != '>') { - fprintf(stderr, "%s: Missing '>' at line %d\n", fname, lineno); - exit(EX_DATAERR); - } + /* Find a tag closing angle bracket */ + for(; *line && *line != '>'; line++) { + if(*line < ' ') { + fprintf(stderr, "%s: Invalid charset (%d) at line %d\n", fname, + *(const unsigned char *)line, lineno); + exit(EX_DATAERR); + } + } + cl = line; + if(*cl != '>') { + fprintf(stderr, "%s: Missing '>' at line %d\n", fname, lineno); + exit(EX_DATAERR); + } - /* Ignore closing tags */ - if(op[1] == '/') { - if(strchr(cl, '<')) { /* We are not very robust */ - fprintf(stderr, - "%s: Multiple tags per line at line %d\n", - fname, lineno); - exit(EX_DATAERR); - } - /* End-of-content octets */ - if(op[2] == 'I') { - buf[0] = buf[1] = 0x00; - fwrite(buf, 1, 2, stdout); - } - return 0; - } + /* Ignore closing tags */ + if(op[1] == '/') { + if(strchr(cl, '<')) { /* We are not very robust */ + fprintf(stderr, "%s: Multiple tags per line at line %d\n", fname, + lineno); + exit(EX_DATAERR); + } + /* End-of-content octets */ + if(op[2] == 'I') { + buf[0] = buf[1] = 0x00; + fwrite(buf, 1, 2, stdout); + } + return 0; + } - switch(op[1]) { - case '!': return 0; /* A comment */ - case '?': return 0; /* An XML preamble */ - case 'C': constr = 1; break; - case 'P': constr = 0; break; - case 'I': constr = 2; break; - default: - fprintf(stderr, - "%s: Expected \"C\"/\"P\"/\"I\" as the XML tag name (%c) at line %d\n", - fname, op[1], lineno); - exit(EX_DATAERR); - } + switch(op[1]) { + case '!': + return 0; /* A comment */ + case '?': + return 0; /* An XML preamble */ + case 'C': + constr = 1; + break; + case 'P': + constr = 0; + break; + case 'I': + constr = 2; + break; + default: + fprintf(stderr, + "%s: Expected \"C\"/\"P\"/\"I\" as the XML tag name (%c) at " + "line %d\n", + fname, op[1], lineno); + exit(EX_DATAERR); + } - *cl = '\0'; - if(cl[-1] == 'F') { - fprintf(stderr, - "%s: Detected pretty-printing of primitive types at line %d. " - "Re-run `unber` with -p option to disable pretty-printing.\n", fname, lineno); - exit(EX_DATAERR); - } + *cl = '\0'; + if(cl[-1] == 'F') { + fprintf(stderr, + "%s: Detected pretty-printing of primitive types at line %d. " + "Re-run `unber` with -p option to disable pretty-printing.\n", + fname, lineno); + exit(EX_DATAERR); + } - tcl_pos = strstr(op, "T=\"["); - tl_pos = strstr(op, "TL=\""); - v_pos = strstr(op, "V=\""); - if(!tcl_pos || (!v_pos && constr != 2)) { - fprintf(stderr, - "%s: Mandatory attribute %s is not found at line %d\n", - fname, (!tcl_pos)?"T":"V", lineno); - exit(EX_DATAERR); - } - errno = 0; - opt_tl_len = tl_pos ? strtoul(tl_pos + 4, 0, 10) : 0; - if(constr == 2) { - tlv_len = 0; - } else { - tlv_len = strtoul(v_pos + 3, 0, 10); - } - if(errno || (opt_tl_len && opt_tl_len < 2) || tlv_len < 0) { - fprintf(stderr, "%s: Invalid TL or V value at line %d\n", - fname, lineno); - exit(EX_DATAERR); - } + tcl_pos = strstr(op, "T=\"["); + tl_pos = strstr(op, "TL=\""); + v_pos = strstr(op, "V=\""); + if(!tcl_pos || (!v_pos && constr != 2)) { + fprintf(stderr, "%s: Mandatory attribute %s is not found at line %d\n", + fname, (!tcl_pos) ? "T" : "V", lineno); + exit(EX_DATAERR); + } + errno = 0; + opt_tl_len = tl_pos ? strtoul(tl_pos + 4, 0, 10) : 0; + if(constr == 2) { + tlv_len = 0; + } else { + tlv_len = strtoul(v_pos + 3, 0, 10); + } + if(errno || (opt_tl_len && opt_tl_len < 2) || tlv_len < 0) { + fprintf(stderr, "%s: Invalid TL or V value at line %d\n", fname, + lineno); + exit(EX_DATAERR); + } + /* clang-format off */ tcl_pos += 4; switch(*tcl_pos) { case 'U': /* UNIVERSAL */ @@ -300,61 +304,63 @@ process_line(const char *fname, char *line, int lineno) { } break; } - unsigned long tag_value_UL; - errno = 0; - if(!*tcl_pos - || ((tag_value_UL = strtoul(tcl_pos, 0, 10)) > UINT_MAX) - || errno) { - fprintf(stderr, "%s: Invalid tag value (%c) at line %d\n", - fname, *tcl_pos, lineno); - exit(EX_DATAERR); - } else { - tag_value = tag_value_UL; - } - tlv_tag = ((tag_value << 2) | tag_class); + /* clang-format on */ - ret = ber_tlv_tag_serialize(tlv_tag, buf, sizeof(buf)); - assert(ret >= 1 && (size_t)ret < sizeof(buf)); - if(constr == 2) { - buf[ret] = 0x80; - ret += 1; - } else { - ret += der_tlv_length_serialize(tlv_len, - buf + ret, sizeof(buf) - ret); - assert(ret >= 2 && (size_t)ret < sizeof(buf)); - } - if(opt_tl_len && ret != opt_tl_len) { - fprintf(stderr, "%s: Cannot encode TL at line %d " - "in the given number of bytes (%ld!=%ld)\n", - fname, lineno, (long)ret, (long)opt_tl_len); - exit(EX_DATAERR); - } - if(constr) *buf |= 0x20; /* Enable "constructed" bit */ - fwrite(buf, 1, ret, stdout); + unsigned long tag_value_UL; + errno = 0; + if(!*tcl_pos || ((tag_value_UL = strtoul(tcl_pos, 0, 10)) > UINT_MAX) + || errno) { + fprintf(stderr, "%s: Invalid tag value (%c) at line %d\n", fname, + *tcl_pos, lineno); + exit(EX_DATAERR); + } else { + tag_value = tag_value_UL; + } + tlv_tag = ((tag_value << 2) | tag_class); - if(!constr) { - ber_tlv_len_t len; - for(len = 0, cl++; *cl && *cl != '<'; cl++, len++) { - unsigned char v; - int h; - if(*cl != '&') { - fputc(*cl, stdout); - continue; - } - cl++; - if(*cl != '#') { - fputc(*cl, stdout); - continue; - } - cl++; - if(*cl != 'x') { - fprintf(stderr, "%s: Expected \"&#xNN;\" at line %d\n", - fname, lineno); - exit(EX_DATAERR); - } - for(v = 0, h = 0; h < 2; h++) { - unsigned char clv = *++cl; - v <<= 4; + ret = ber_tlv_tag_serialize(tlv_tag, buf, sizeof(buf)); + assert(ret >= 1 && (size_t)ret < sizeof(buf)); + if(constr == 2) { + buf[ret] = 0x80; + ret += 1; + } else { + ret += der_tlv_length_serialize(tlv_len, buf + ret, sizeof(buf) - ret); + assert(ret >= 2 && (size_t)ret < sizeof(buf)); + } + if(opt_tl_len && ret != opt_tl_len) { + fprintf(stderr, + "%s: Cannot encode TL at line %d " + "in the given number of bytes (%ld!=%ld)\n", + fname, lineno, (long)ret, (long)opt_tl_len); + exit(EX_DATAERR); + } + if(constr) *buf |= 0x20; /* Enable "constructed" bit */ + fwrite(buf, 1, ret, stdout); + + if(!constr) { + ber_tlv_len_t len; + for(len = 0, cl++; *cl && *cl != '<'; cl++, len++) { + unsigned char v; + int h; + if(*cl != '&') { + fputc(*cl, stdout); + continue; + } + cl++; + if(*cl != '#') { + fputc(*cl, stdout); + continue; + } + cl++; + if(*cl != 'x') { + fprintf(stderr, "%s: Expected \"&#xNN;\" at line %d\n", fname, + lineno); + exit(EX_DATAERR); + } + for(v = 0, h = 0; h < 2; h++) { + unsigned char clv = *++cl; + v <<= 4; + /* clang-format off */ switch(clv) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -371,26 +377,25 @@ process_line(const char *fname, char *line, int lineno) { fname, lineno, clv); exit(EX_DATAERR); } - } - cl++; - if(*cl != ';') { - fprintf(stderr, - "%s: Expected \"&#xNN;\" at line %d\n", - fname, lineno); - exit(EX_DATAERR); - } - fputc(v, stdout); - } - if(len != tlv_len) { - if(no_validation) fprintf(stderr, "Warning: "); - fprintf(stderr, - "%s: Could not encode value of %ld chars " - "at line %d in %ld bytes\n", - fname, (long)len, lineno, (long)tlv_len); - if(!no_validation) exit(EX_DATAERR); - } - } - - return 0; -} + /* clang-format on */ + } + cl++; + if(*cl != ';') { + fprintf(stderr, "%s: Expected \"&#xNN;\" at line %d\n", fname, + lineno); + exit(EX_DATAERR); + } + fputc(v, stdout); + } + if(len != tlv_len) { + if(no_validation) fprintf(stderr, "Warning: "); + fprintf(stderr, + "%s: Could not encode value of %ld chars " + "at line %d in %ld bytes\n", + fname, (long)len, lineno, (long)tlv_len); + if(!no_validation) exit(EX_DATAERR); + } + } + return 0; +} diff --git a/asn1c/sys-common.h b/asn1c/sys-common.h index 9ab1e6bb..9a9ea96e 100644 --- a/asn1c/sys-common.h +++ b/asn1c/sys-common.h @@ -24,7 +24,7 @@ * * $Id$ */ -#ifdef HAVE_CONFIG_H +#ifdef HAVE_CONFIG_H #include #endif @@ -34,39 +34,38 @@ #include #include -#ifdef HAVE_UNISTD_H +#ifdef HAVE_UNISTD_H #include #endif -#ifdef HAVE_SYS_TYPES_H +#ifdef HAVE_SYS_TYPES_H #include #endif -#ifdef HAVE_SYS_STAT_H -#include /* for stat(2) */ +#ifdef HAVE_SYS_STAT_H +#include /* for stat(2) */ #endif -#ifndef _WIN32 -#include /* for EX_USAGE & others */ -#else /* _WIN32 */ -#ifndef EX_USAGE -#define EX_USAGE 64 +#ifndef _WIN32 +#include /* for EX_USAGE & others */ +#else /* _WIN32 */ +#ifndef EX_USAGE +#define EX_USAGE 64 #endif -#ifndef EX_DATAERR -#define EX_DATAERR 65 +#ifndef EX_DATAERR +#define EX_DATAERR 65 #endif -#ifndef EX_SOFTWARE -#define EX_SOFTWARE 70 +#ifndef EX_SOFTWARE +#define EX_SOFTWARE 70 #endif -#ifndef EX_OSERR -#define EX_OSERR 71 +#ifndef EX_OSERR +#define EX_OSERR 71 #endif -#ifndef EX_OSFILE -#define EX_OSFILE 72 +#ifndef EX_OSFILE +#define EX_OSFILE 72 #endif #if defined HAVE_DECL_ALLOCA && !HAVE_DECL_ALLOCA -#define alloca _alloca +#define alloca _alloca #endif -#define snprintf _snprintf -#endif /* _WIN32 */ - +#define snprintf _snprintf +#endif /* _WIN32 */ diff --git a/asn1c/unber.c b/asn1c/unber.c index 088491f7..43bdff94 100644 --- a/asn1c/unber.c +++ b/asn1c/unber.c @@ -27,9 +27,9 @@ */ #include "sys-common.h" -#define ASN_DISABLE_PER_SUPPORT 1 +#define ASN_DISABLE_PER_SUPPORT 1 -#include /* For static string tables */ +#include /* For static string tables */ #include #include @@ -40,93 +40,88 @@ #include #include -#undef COPYRIGHT -#define COPYRIGHT \ - "Copyright (c) 2004, 2005 Lev Walkin \n" +#undef COPYRIGHT +#define COPYRIGHT "Copyright (c) 2004, 2005 Lev Walkin \n" -static void usage(const char *av0); /* Print the Usage screen and exit */ -static int process(const char *fname); /* Perform the BER decoding */ +static void usage(const char *av0); /* Print the Usage screen and exit */ +static int process(const char *fname); /* Perform the BER decoding */ static int decode_tlv_from_string(const char *datastring); -static int single_type_decoding = 0; /* -1 enables that */ -static int minimalistic = 0; /* -m enables that */ -static int pretty_printing = 1; /* -p disables that */ -static int skip_bytes = 0; /* -s controls that */ -static char indent_bytes[16] = " "; /* -i controls that */ +static int single_type_decoding = 0; /* -1 enables that */ +static int minimalistic = 0; /* -m enables that */ +static int pretty_printing = 1; /* -p disables that */ +static int skip_bytes = 0; /* -s controls that */ +static char indent_bytes[16] = " "; /* -i controls that */ int main(int ac, char **av) { - int ch; /* Command line character */ - int i; /* Index in some loops */ + int ch; /* Command line character */ + int i; /* Index in some loops */ - /* - * Process command-line options. - */ - while((ch = getopt(ac, av, "1hi:mps:t:v")) != -1) - switch(ch) { - case '1': - single_type_decoding = 1; - break; - case 'i': - i = atoi(optarg); - if(i >= 0 && i < (int)sizeof(indent_bytes)) { - memset(indent_bytes, ' ', i); - indent_bytes[i] = '\0'; - } else { - fprintf(stderr, "-i %s: Invalid indent value\n",optarg); - exit(EX_USAGE); - } - break; - case 'm': - minimalistic = 1; - break; - case 'p': - pretty_printing = 0; - break; - case 's': - skip_bytes = atoi(optarg); - if(skip_bytes < 0) { - fprintf(stderr, "-s %s: positive value expected\n", - optarg); - exit(EX_USAGE); - } - break; - case 't': - if(decode_tlv_from_string(optarg)) - exit(EX_DATAERR); - exit(0); - case 'v': - fprintf(stderr, "ASN.1 BER Decoder, v" VERSION "\n" COPYRIGHT); - exit(0); - break; - case 'h': - default: - usage(av[0]); - } + /* + * Process command-line options. + */ + while((ch = getopt(ac, av, "1hi:mps:t:v")) != -1) switch(ch) { + case '1': + single_type_decoding = 1; + break; + case 'i': + i = atoi(optarg); + if(i >= 0 && i < (int)sizeof(indent_bytes)) { + memset(indent_bytes, ' ', i); + indent_bytes[i] = '\0'; + } else { + fprintf(stderr, "-i %s: Invalid indent value\n", optarg); + exit(EX_USAGE); + } + break; + case 'm': + minimalistic = 1; + break; + case 'p': + pretty_printing = 0; + break; + case 's': + skip_bytes = atoi(optarg); + if(skip_bytes < 0) { + fprintf(stderr, "-s %s: positive value expected\n", optarg); + exit(EX_USAGE); + } + break; + case 't': + if(decode_tlv_from_string(optarg)) exit(EX_DATAERR); + exit(0); + case 'v': + fprintf(stderr, "ASN.1 BER Decoder, v" VERSION "\n" COPYRIGHT); + exit(0); + break; + case 'h': + default: + usage(av[0]); + } - /* - * Ensure that there are some input files present. - */ - if(ac > optind) { - ac -= optind; - av += optind; - } else { - fprintf(stderr, "%s: No input files specified\n", av[0]); - exit(1); - } + /* + * Ensure that there are some input files present. + */ + if(ac > optind) { + ac -= optind; + av += optind; + } else { + fprintf(stderr, "%s: No input files specified\n", av[0]); + exit(1); + } - setvbuf(stdout, 0, _IOLBF, 0); + setvbuf(stdout, 0, _IOLBF, 0); - /* - * Iterate over input files and parse each. - * All syntax trees from all files will be bundled together. - */ - for(i = 0; i < ac; i++) { - if(process(av[i])) - exit(EX_DATAERR); - } + /* + * Iterate over input files and parse each. + * All syntax trees from all files will be bundled together. + */ + for(i = 0; i < ac; i++) { + if(process(av[i])) exit(EX_DATAERR); + } - return 0; + return 0; } /* @@ -134,6 +129,7 @@ main(int ac, char **av) { */ static void usage(const char *av0) { + /* clang-format off */ fprintf(stderr, "ASN.1 BER Decoder, v" VERSION "\n" COPYRIGHT "Usage: %s [options] [-] [file ...]\n" @@ -157,16 +153,22 @@ usage(const char *av0) { " [F] Indicates that the value was reformatted (pretty-printed)\n" "See the manual page for details\n" , av0); - exit(EX_USAGE); + /* clang-format on */ + exit(EX_USAGE); } typedef enum pd_code { - PD_FAILED = -1, - PD_FINISHED = 0, - PD_EOF = 1, + PD_FAILED = -1, + PD_FINISHED = 0, + PD_EOF = 1, } pd_code_e; -static pd_code_e process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level, ssize_t limit, ber_tlv_len_t *frame_size, ber_tlv_len_t effective_size, int expect_eoc); -static void print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, ber_tlv_tag_t, ber_tlv_len_t, ber_tlv_len_t effective_frame_size); +static pd_code_e process_deeper(const char *fname, FILE *fp, + asn1c_integer_t *offset, int level, + ssize_t limit, ber_tlv_len_t *frame_size, + ber_tlv_len_t effective_size, int expect_eoc); +static void print_TL(int fin, asn1c_integer_t offset, int level, int constr, + ssize_t tlen, ber_tlv_tag_t, ber_tlv_len_t, + ber_tlv_len_t effective_frame_size); static int print_V(const char *fname, FILE *fp, ber_tlv_tag_t, ber_tlv_len_t); /* @@ -174,514 +176,515 @@ static int print_V(const char *fname, FILE *fp, ber_tlv_tag_t, ber_tlv_len_t); */ static int process(const char *fname) { - FILE *fp; - pd_code_e pdc; - asn1c_integer_t offset = 0; /* Stream decoding position */ - ber_tlv_len_t frame_size = 0; /* Single frame size */ + FILE *fp; + pd_code_e pdc; + asn1c_integer_t offset = 0; /* Stream decoding position */ + ber_tlv_len_t frame_size = 0; /* Single frame size */ - if(strcmp(fname, "-")) { - fp = fopen(fname, "rb"); - if(!fp) { - perror(fname); - return -1; - } - } else { - fp = stdin; - } + if(strcmp(fname, "-")) { + fp = fopen(fname, "rb"); + if(!fp) { + perror(fname); + return -1; + } + } else { + fp = stdin; + } - /* - * Skip the requested amount of bytes. - */ - for(; offset < skip_bytes; offset++) { - if(fgetc(fp) == -1) { - fprintf(stderr, - "%s: input source (%" PRIdASN " bytes) " - "has less data than \"-s %d\" switch " - "wants to skip\n", - fname, offset, skip_bytes); - if(fp != stdin) fclose(fp); - return -1; - } - } + /* + * Skip the requested amount of bytes. + */ + for(; offset < skip_bytes; offset++) { + if(fgetc(fp) == -1) { + fprintf(stderr, "%s: input source (%" PRIdASN + " bytes) " + "has less data than \"-s %d\" switch " + "wants to skip\n", + fname, offset, skip_bytes); + if(fp != stdin) fclose(fp); + return -1; + } + } - /* - * Fetch out BER-encoded data until EOF or error. - */ - do { - pdc = process_deeper(fname, fp, &offset, - 0, -1, &frame_size, 0, 0); - } while(pdc == PD_FINISHED && !single_type_decoding); + /* + * Fetch out BER-encoded data until EOF or error. + */ + do { + pdc = process_deeper(fname, fp, &offset, 0, -1, &frame_size, 0, 0); + } while(pdc == PD_FINISHED && !single_type_decoding); - if(fp != stdin) - fclose(fp); + if(fp != stdin) fclose(fp); - if(pdc == PD_FAILED) - return -1; - return 0; + if(pdc == PD_FAILED) return -1; + return 0; } /* * Process the TLV recursively. */ static pd_code_e -process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level, ssize_t limit, ber_tlv_len_t *frame_size, ber_tlv_len_t effective_size, int expect_eoc) { - unsigned char tagbuf[32]; - ssize_t tblen = 0; - pd_code_e pdc = PD_FINISHED; - ber_tlv_tag_t tlv_tag; - ber_tlv_len_t tlv_len; - ssize_t t_len; - ssize_t l_len; +process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level, + ssize_t limit, ber_tlv_len_t *frame_size, + ber_tlv_len_t effective_size, int expect_eoc) { + unsigned char tagbuf[32]; + ssize_t tblen = 0; + pd_code_e pdc = PD_FINISHED; + ber_tlv_tag_t tlv_tag; + ber_tlv_len_t tlv_len; + ssize_t t_len; + ssize_t l_len; - for(;;) { - ber_tlv_len_t local_esize = 0; - int constr; - int ch; + for(;;) { + ber_tlv_len_t local_esize = 0; + int constr; + int ch; - if(limit == 0) - return PD_FINISHED; + if(limit == 0) return PD_FINISHED; - if(limit >= 0 && tblen >= limit) { - fprintf(stderr, - "%s: Too long TL sequence (%ld >= %ld)" - " at %" PRIdASN ". " - "Broken or maliciously constructed file\n", - fname, (long)tblen, (long)limit, *offset); - return PD_FAILED; - } + if(limit >= 0 && tblen >= limit) { + fprintf(stderr, + "%s: Too long TL sequence (%ld >= %ld)" + " at %" PRIdASN + ". " + "Broken or maliciously constructed file\n", + fname, (long)tblen, (long)limit, *offset); + return PD_FAILED; + } - /* Get the next byte from the input stream */ - ch = fgetc(fp); - if(ch == -1) { - if(limit > 0 || expect_eoc) { - fprintf(stderr, - "%s: Unexpected end of file (TL)" - " at %" PRIdASN "\n", - fname, *offset); - return PD_FAILED; - } else { - return PD_EOF; - } - } + /* Get the next byte from the input stream */ + ch = fgetc(fp); + if(ch == -1) { + if(limit > 0 || expect_eoc) { + fprintf(stderr, + "%s: Unexpected end of file (TL)" + " at %" PRIdASN "\n", + fname, *offset); + return PD_FAILED; + } else { + return PD_EOF; + } + } - tagbuf[tblen++] = ch; + tagbuf[tblen++] = ch; - /* - * Decode the TLV tag. - */ - t_len = ber_fetch_tag(tagbuf, tblen, &tlv_tag); - switch(t_len) { - case -1: - fprintf(stderr, "%s: Fatal error decoding tag" - " at %" PRIdASN "+%ld\n", - fname, *offset, (long)tblen); - return PD_FAILED; - case 0: - /* More data expected */ - continue; - } + /* + * Decode the TLV tag. + */ + t_len = ber_fetch_tag(tagbuf, tblen, &tlv_tag); + switch(t_len) { + case -1: + fprintf(stderr, + "%s: Fatal error decoding tag" + " at %" PRIdASN "+%ld\n", + fname, *offset, (long)tblen); + return PD_FAILED; + case 0: + /* More data expected */ + continue; + } - /* - * Decode the TLV length. - */ - constr = BER_TLV_CONSTRUCTED(tagbuf); - l_len = ber_fetch_length(constr, - tagbuf + t_len, tblen - t_len, &tlv_len); - switch(l_len) { - case -1: - fprintf(stderr, - "%s: Fatal error decoding value length" - " at %" PRIdASN "\n", - fname, *offset + t_len); - return PD_FAILED; - case 0: - /* More data expected */ - continue; - } + /* + * Decode the TLV length. + */ + constr = BER_TLV_CONSTRUCTED(tagbuf); + l_len = + ber_fetch_length(constr, tagbuf + t_len, tblen - t_len, &tlv_len); + switch(l_len) { + case -1: + fprintf(stderr, + "%s: Fatal error decoding value length" + " at %" PRIdASN "\n", + fname, *offset + t_len); + return PD_FAILED; + case 0: + /* More data expected */ + continue; + } - /* Make sure the T & L decoders took exactly the whole buffer */ - assert((t_len + l_len) == tblen); + /* Make sure the T & L decoders took exactly the whole buffer */ + assert((t_len + l_len) == tblen); - if(!expect_eoc || tagbuf[0] || tagbuf[1]) - print_TL(0, *offset, level, constr, tblen, - tlv_tag, tlv_len, effective_size); + if(!expect_eoc || tagbuf[0] || tagbuf[1]) + print_TL(0, *offset, level, constr, tblen, tlv_tag, tlv_len, + effective_size); - if(limit != -1) { - /* If limit is set, account for the TL sequence */ - limit -= (t_len + l_len); - assert(limit >= 0); + if(limit != -1) { + /* If limit is set, account for the TL sequence */ + limit -= (t_len + l_len); + assert(limit >= 0); - if(tlv_len > limit) { - fprintf(stderr, - "%s: Structure advertizes length (%ld) " - "greater than of a parent container (%ld)\n", - fname, (long)tlv_len, (long)limit); - return PD_FAILED; - } - } + if(tlv_len > limit) { + fprintf(stderr, + "%s: Structure advertizes length (%ld) " + "greater than of a parent container (%ld)\n", + fname, (long)tlv_len, (long)limit); + return PD_FAILED; + } + } - *offset += t_len + l_len; - *frame_size += t_len + l_len; - effective_size += t_len + l_len; - local_esize += t_len + l_len; + *offset += t_len + l_len; + *frame_size += t_len + l_len; + effective_size += t_len + l_len; + local_esize += t_len + l_len; - if(expect_eoc && !tagbuf[0] && !tagbuf[1]) { - /* End of content octets */ - print_TL(1, *offset - 2, level - 1, 1, 2, 0, -1, - effective_size); - return PD_FINISHED; - } + if(expect_eoc && !tagbuf[0] && !tagbuf[1]) { + /* End of content octets */ + print_TL(1, *offset - 2, level - 1, 1, 2, 0, -1, effective_size); + return PD_FINISHED; + } - if(constr) { - ber_tlv_len_t dec = 0; - /* - * This is a constructed type. Process recursively. - */ - printf(">\n"); /* Close the opening tag */ - if(tlv_len != -1 && limit != -1) { - assert(limit >= tlv_len); - } - pdc = process_deeper(fname, fp, offset, level + 1, - tlv_len == -1 ? limit : tlv_len, - &dec, t_len + l_len, tlv_len == -1); - if(pdc == PD_FAILED) return pdc; - if(limit != -1) { - assert(limit >= dec); - limit -= dec; - } - *frame_size += dec; - effective_size += dec; - local_esize += dec; - if(tlv_len == -1) { - tblen = 0; - if(pdc == PD_FINISHED - && limit < 0 && !expect_eoc) - return pdc; - continue; - } - } else { - assert(tlv_len >= 0); - if(print_V(fname, fp, tlv_tag, tlv_len)) - return PD_FAILED; + if(constr) { + ber_tlv_len_t dec = 0; + /* + * This is a constructed type. Process recursively. + */ + printf(">\n"); /* Close the opening tag */ + if(tlv_len != -1 && limit != -1) { + assert(limit >= tlv_len); + } + pdc = process_deeper(fname, fp, offset, level + 1, + tlv_len == -1 ? limit : tlv_len, &dec, + t_len + l_len, tlv_len == -1); + if(pdc == PD_FAILED) return pdc; + if(limit != -1) { + assert(limit >= dec); + limit -= dec; + } + *frame_size += dec; + effective_size += dec; + local_esize += dec; + if(tlv_len == -1) { + tblen = 0; + if(pdc == PD_FINISHED && limit < 0 && !expect_eoc) return pdc; + continue; + } + } else { + assert(tlv_len >= 0); + if(print_V(fname, fp, tlv_tag, tlv_len)) return PD_FAILED; - if(limit != -1) { - assert(limit >= tlv_len); - limit -= tlv_len; - } - *offset += tlv_len; - *frame_size += tlv_len; - effective_size += tlv_len; - local_esize += tlv_len; - } + if(limit != -1) { + assert(limit >= tlv_len); + limit -= tlv_len; + } + *offset += tlv_len; + *frame_size += tlv_len; + effective_size += tlv_len; + local_esize += tlv_len; + } - print_TL(1, *offset, level, constr, tblen, - tlv_tag, tlv_len, local_esize); + print_TL(1, *offset, level, constr, tblen, tlv_tag, tlv_len, + local_esize); - tblen = 0; + tblen = 0; - /* Report success for a single top level TLV */ - if(level == 0 && limit == -1 && !expect_eoc) - break; - } /* for(;;) */ + /* Report success for a single top level TLV */ + if(level == 0 && limit == -1 && !expect_eoc) break; + } /* for(;;) */ - return pdc; + return pdc; } static void -print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_len, ber_tlv_len_t effective_size) { +print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, + ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_len, + ber_tlv_len_t effective_size) { + if(fin && !constr) { + printf("

\n"); + return; + } - if(fin && !constr) { - printf("

\n"); - return; - } + while(level-- > 0) fputs(indent_bytes, stdout); /* Print indent */ + printf(fin ? " 0) fputs(indent_bytes, stdout); /* Print indent */ - printf(fin ? "\n"); - } + if(fin) { + if(constr && !minimalistic) printf(" L=\"%ld\"", (long)effective_size); + printf(">\n"); + } } /* * Print the value in binary form, or reformat for pretty-printing. */ static int -print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_len) { - asn1c_integer_t *arcs = 0; /* Object identifier arcs */ - unsigned char *vbuf = 0; - asn1p_expr_type_e etype = 0; - asn1c_integer_t collector = 0; - int special_format = 0; - ssize_t i; +print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, + ber_tlv_len_t tlv_len) { + asn1c_integer_t *arcs = 0; /* Object identifier arcs */ + unsigned char *vbuf = 0; + asn1p_expr_type_e etype = 0; + asn1c_integer_t collector = 0; + int special_format = 0; + ssize_t i; - /* Figure out what type is it */ - if(BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL - && pretty_printing) { - ber_tlv_tag_t tvalue = BER_TAG_VALUE(tlv_tag); - etype = ASN_UNIVERSAL_TAG2TYPE(tvalue); - } + /* Figure out what type is it */ + if(BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL && pretty_printing) { + ber_tlv_tag_t tvalue = BER_TAG_VALUE(tlv_tag); + etype = ASN_UNIVERSAL_TAG2TYPE(tvalue); + } - /* - * Determine how to print the value, either in its native binary form, - * encoded with &xNN characters, or using pretty-printing. - * The basic string types (including "useful types", like UTCTime) - * are excempt from this determination logic, because their alphabets - * are subsets of the XML's native UTF-8 encoding. - */ - switch(etype) { - case ASN_BASIC_BOOLEAN: - if(tlv_len == 1) - special_format = 1; - else - etype = 0; - break; - case ASN_BASIC_INTEGER: - case ASN_BASIC_ENUMERATED: - if((size_t)tlv_len <= sizeof(collector)) - special_format = 1; - else - etype = 0; - break; - case ASN_BASIC_OBJECT_IDENTIFIER: - case ASN_BASIC_RELATIVE_OID: - if(tlv_len > 0 && tlv_len < 128*1024 /* VERY long OID! */) { - arcs = MALLOC(sizeof(*arcs) * (tlv_len + 1)); - if(arcs) { - vbuf = MALLOC(tlv_len + 1); - /* Not checking is intentional */ - } - } - case ASN_BASIC_UTCTime: - case ASN_BASIC_GeneralizedTime: - case ASN_STRING_NumericString: - case ASN_STRING_PrintableString: - case ASN_STRING_VisibleString: - case ASN_STRING_IA5String: - case ASN_STRING_UTF8String: - break; /* Directly compatible with UTF-8 */ - case ASN_STRING_BMPString: - case ASN_STRING_UniversalString: - break; /* Not directly compatible with UTF-8 */ - default: - /* Conditionally compatible with UTF-8 */ - if(( - (etype & ASN_STRING_MASK) - || - (etype == ASN_BASIC_OCTET_STRING) - || - /* - * AUTOMATIC TAGS or IMPLICIT TAGS in effect, - * Treat this primitive type as OCTET_STRING. - */ - (BER_TAG_CLASS(tlv_tag) != ASN_TAG_CLASS_UNIVERSAL - && pretty_printing) - ) && (tlv_len > 0 && tlv_len < 128 * 1024)) { - vbuf = MALLOC(tlv_len + 1); - /* Not checking is intentional */ - } - break; - } + /* + * Determine how to print the value, either in its native binary form, + * encoded with &xNN characters, or using pretty-printing. + * The basic string types (including "useful types", like UTCTime) + * are excempt from this determination logic, because their alphabets + * are subsets of the XML's native UTF-8 encoding. + */ + switch(etype) { + case ASN_BASIC_BOOLEAN: + if(tlv_len == 1) + special_format = 1; + else + etype = 0; + break; + case ASN_BASIC_INTEGER: + case ASN_BASIC_ENUMERATED: + if((size_t)tlv_len <= sizeof(collector)) + special_format = 1; + else + etype = 0; + break; + case ASN_BASIC_OBJECT_IDENTIFIER: + case ASN_BASIC_RELATIVE_OID: + if(tlv_len > 0 && tlv_len < 128 * 1024 /* VERY long OID! */) { + arcs = MALLOC(sizeof(*arcs) * (tlv_len + 1)); + if(arcs) { + vbuf = MALLOC(tlv_len + 1); + /* Not checking is intentional */ + } + } + case ASN_BASIC_UTCTime: + case ASN_BASIC_GeneralizedTime: + case ASN_STRING_NumericString: + case ASN_STRING_PrintableString: + case ASN_STRING_VisibleString: + case ASN_STRING_IA5String: + case ASN_STRING_UTF8String: + break; /* Directly compatible with UTF-8 */ + case ASN_STRING_BMPString: + case ASN_STRING_UniversalString: + break; /* Not directly compatible with UTF-8 */ + default: + /* Conditionally compatible with UTF-8 */ + if(((etype & ASN_STRING_MASK) || (etype == ASN_BASIC_OCTET_STRING) || + /* + * AUTOMATIC TAGS or IMPLICIT TAGS in effect, + * Treat this primitive type as OCTET_STRING. + */ + (BER_TAG_CLASS(tlv_tag) != ASN_TAG_CLASS_UNIVERSAL + && pretty_printing)) + && (tlv_len > 0 && tlv_len < 128 * 1024)) { + vbuf = MALLOC(tlv_len + 1); + /* Not checking is intentional */ + } + break; + } - /* If collection vbuf is present, defer printing the F flag. */ - if(!vbuf) printf(special_format ? " F>" : ">"); + /* If collection vbuf is present, defer printing the F flag. */ + if(!vbuf) printf(special_format ? " F>" : ">"); - /* - * Print the value in binary or text form, - * or collect the bytes into vbuf. - */ - for(i = 0; i < tlv_len; i++) { - int ch = fgetc(fp); - if(ch == -1) { - fprintf(stderr, - "%s: Unexpected end of file (V)\n", fname); - if(vbuf) FREEMEM(vbuf); - if(arcs) FREEMEM(arcs); - return -1; - } - switch(etype) { - case ASN_BASIC_UTCTime: - case ASN_BASIC_GeneralizedTime: - case ASN_STRING_NumericString: - case ASN_STRING_PrintableString: - case ASN_STRING_VisibleString: - case ASN_STRING_IA5String: - case ASN_STRING_UTF8String: - switch(ch) { - default: - if(((etype == ASN_STRING_UTF8String) - || !(ch & 0x80)) - && (ch >= 0x20) - ) { - printf("%c", ch); - break; - } - /* Fall through */ - case 0x3c: case 0x3e: case 0x26: - printf("&#x%02x;", ch); - } - break; - case ASN_BASIC_BOOLEAN: - switch(ch) { - case 0: printf(""); break; - case 0xff: printf(""); break; - default: printf("", ch); - } - break; - case ASN_BASIC_INTEGER: - case ASN_BASIC_ENUMERATED: - if(i) collector = collector * 256 + ch; - else collector = (int)(signed char)ch; - break; - default: - if(vbuf) { - vbuf[i] = ch; - } else { - printf("&#x%02x;", ch); - } - } - } + /* + * Print the value in binary or text form, + * or collect the bytes into vbuf. + */ + for(i = 0; i < tlv_len; i++) { + int ch = fgetc(fp); + if(ch == -1) { + fprintf(stderr, "%s: Unexpected end of file (V)\n", fname); + if(vbuf) FREEMEM(vbuf); + if(arcs) FREEMEM(arcs); + return -1; + } + switch(etype) { + case ASN_BASIC_UTCTime: + case ASN_BASIC_GeneralizedTime: + case ASN_STRING_NumericString: + case ASN_STRING_PrintableString: + case ASN_STRING_VisibleString: + case ASN_STRING_IA5String: + case ASN_STRING_UTF8String: + switch(ch) { + default: + if(((etype == ASN_STRING_UTF8String) || !(ch & 0x80)) + && (ch >= 0x20)) { + printf("%c", ch); + break; + } + /* Fall through */ + case 0x3c: + case 0x3e: + case 0x26: + printf("&#x%02x;", ch); + } + break; + case ASN_BASIC_BOOLEAN: + switch(ch) { + case 0: + printf(""); + break; + case 0xff: + printf(""); + break; + default: + printf("", ch); + } + break; + case ASN_BASIC_INTEGER: + case ASN_BASIC_ENUMERATED: + if(i) + collector = collector * 256 + ch; + else + collector = (int)(signed char)ch; + break; + default: + if(vbuf) { + vbuf[i] = ch; + } else { + printf("&#x%02x;", ch); + } + } + } - /* Do post-processing */ - switch(etype) { - case ASN_BASIC_INTEGER: - case ASN_BASIC_ENUMERATED: - printf("%" PRIdASN, collector); - break; - case ASN_BASIC_OBJECT_IDENTIFIER: - if(vbuf) { - OBJECT_IDENTIFIER_t oid; - int arcno; + /* Do post-processing */ + switch(etype) { + case ASN_BASIC_INTEGER: + case ASN_BASIC_ENUMERATED: + printf("%" PRIdASN, collector); + break; + case ASN_BASIC_OBJECT_IDENTIFIER: + if(vbuf) { + OBJECT_IDENTIFIER_t oid; + int arcno; - oid.buf = vbuf; - oid.size = tlv_len; + oid.buf = vbuf; + oid.size = tlv_len; - arcno = OBJECT_IDENTIFIER_get_arcs(&oid, arcs, - sizeof(*arcs), tlv_len + 1); - if(arcno >= 0) { - assert(arcno <= (tlv_len + 1)); - printf(" F>"); - for(i = 0; i < arcno; i++) { - if(i) printf("."); - printf("%" PRIuASN, arcs[i]); - } - FREEMEM(vbuf); - vbuf = 0; - } - } - break; - case ASN_BASIC_RELATIVE_OID: - if(vbuf) { - RELATIVE_OID_t oid; - int arcno; + arcno = OBJECT_IDENTIFIER_get_arcs(&oid, arcs, sizeof(*arcs), + tlv_len + 1); + if(arcno >= 0) { + assert(arcno <= (tlv_len + 1)); + printf(" F>"); + for(i = 0; i < arcno; i++) { + if(i) printf("."); + printf("%" PRIuASN, arcs[i]); + } + FREEMEM(vbuf); + vbuf = 0; + } + } + break; + case ASN_BASIC_RELATIVE_OID: + if(vbuf) { + RELATIVE_OID_t oid; + int arcno; - oid.buf = vbuf; - oid.size = tlv_len; - - arcno = RELATIVE_OID_get_arcs(&oid, arcs, - sizeof(*arcs), tlv_len); - if(arcno >= 0) { - assert(arcno <= (tlv_len + 1)); - printf(" F>"); - for(i = 0; i < arcno; i++) { - if(i) printf("."); - printf("%" PRIuASN, arcs[i]); - } - FREEMEM(vbuf); - vbuf = 0; - } - } - break; - default: break; - } + oid.buf = vbuf; + oid.size = tlv_len; - /* - * If the buffer was not consumed, print it out. - * It might be an OCTET STRING or other primitive type, - * which might actually be printable, but we need to figure it out. - */ - if(vbuf) { - int binary; + arcno = RELATIVE_OID_get_arcs(&oid, arcs, sizeof(*arcs), tlv_len); + if(arcno >= 0) { + assert(arcno <= (tlv_len + 1)); + printf(" F>"); + for(i = 0; i < arcno; i++) { + if(i) printf("."); + printf("%" PRIuASN, arcs[i]); + } + FREEMEM(vbuf); + vbuf = 0; + } + } + break; + default: + break; + } - /* - * Check whether the data could be represented as text - */ - binary = -1 * (tlv_len >> 3); /* Threshold is 12.5% binary */ - for(i = 0; i < tlv_len; i++) { - switch(vbuf[i]) { - case 0x1b: binary = 1; break; - case 0x09: case 0x0a: case 0x0d: continue; - default: - if(vbuf[i] < 0x20 || vbuf[i] >= 0x7f) - if(++binary > 0) /* Way too many */ - break; - continue; - } - break; - } - printf(">"); - for(i = 0; i < tlv_len; i++) { - if(binary > 0 || vbuf[i] < 0x20 || vbuf[i] >= 0x7f - || vbuf[i] == 0x26 /* '&' */ - || vbuf[i] == 0x3c /* '<' */ - || vbuf[i] == 0x3e /* '>' */ - ) - printf("&#x%02x;", vbuf[i]); - else - printf("%c", vbuf[i]); - } - FREEMEM(vbuf); - } + /* + * If the buffer was not consumed, print it out. + * It might be an OCTET STRING or other primitive type, + * which might actually be printable, but we need to figure it out. + */ + if(vbuf) { + int binary; - if(arcs) FREEMEM(arcs); - return 0; + /* + * Check whether the data could be represented as text + */ + binary = -1 * (tlv_len >> 3); /* Threshold is 12.5% binary */ + for(i = 0; i < tlv_len; i++) { + switch(vbuf[i]) { + case 0x1b: + binary = 1; + break; + case 0x09: + case 0x0a: + case 0x0d: + continue; + default: + if(vbuf[i] < 0x20 || vbuf[i] >= 0x7f) + if(++binary > 0) /* Way too many */ + break; + continue; + } + break; + } + printf(">"); + for(i = 0; i < tlv_len; i++) { + if(binary > 0 || vbuf[i] < 0x20 || vbuf[i] >= 0x7f + || vbuf[i] == 0x26 /* '&' */ + || vbuf[i] == 0x3c /* '<' */ + || vbuf[i] == 0x3e /* '>' */ + ) + printf("&#x%02x;", vbuf[i]); + else + printf("%c", vbuf[i]); + } + FREEMEM(vbuf); + } + + if(arcs) FREEMEM(arcs); + return 0; } static int decode_tlv_from_string(const char *datastring) { - unsigned char *data, *dp; - size_t dsize; /* Data size */ - ssize_t len; - ber_tlv_tag_t tlv_tag; - ber_tlv_len_t tlv_len; - const char *p; - int half; + unsigned char *data, *dp; + size_t dsize; /* Data size */ + ssize_t len; + ber_tlv_tag_t tlv_tag; + ber_tlv_len_t tlv_len; + const char *p; + int half; - dsize = strlen(datastring) + 1; - dp = data = CALLOC(1, dsize); - assert(data); + dsize = strlen(datastring) + 1; + dp = data = CALLOC(1, dsize); + assert(data); - for(half = 0, p = datastring; *p; p++) { + for(half = 0, p = datastring; *p; p++) { + /* clang-format off */ switch(*p) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -701,78 +704,151 @@ decode_tlv_from_string(const char *datastring) { fprintf(stderr, "%s^ <- here\n", (char *)data); return -1; } - if(half) dp++; else (*dp) <<= 4; - half = !half; - } + /* clang-format on */ + if(half) + dp++; + else + (*dp) <<= 4; + half = !half; + } - assert((size_t)(dp - data) <= dsize); - dsize = dp - data; + assert((size_t)(dp - data) <= dsize); + dsize = dp - data; - printf("BER: "); - for(dp = data; dp < data + dsize; dp++) - printf("%02X", *dp); - printf("\n"); + printf("BER: "); + for(dp = data; dp < data + dsize; dp++) printf("%02X", *dp); + printf("\n"); - len = ber_fetch_tag(data, dsize, &tlv_tag); - switch(len) { - case -1: - fprintf(stderr, "TAG: Fatal error decoding tag\n"); - return -1; - case 0: - fprintf(stderr, "TAG: More data expected\n"); - return -1; - default: - printf("TAG: "); - ber_tlv_tag_fwrite(tlv_tag, stdout); - if(BER_TLV_CONSTRUCTED(data)) { - printf(" (constructed)"); - } else if(dsize >= 2 && data[0] == 0 && data[1] == 0) { - printf(" (end-of-content)"); - } else { - printf(" (primitive)"); - } - if(BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL) { - const char *str; - ber_tlv_tag_t tvalue = BER_TAG_VALUE(tlv_tag); - str = ASN_UNIVERSAL_TAG2STR(tvalue); - if(str) printf(" \"%s\"", str); - } - printf("\n"); - } + len = ber_fetch_tag(data, dsize, &tlv_tag); + switch(len) { + case -1: + fprintf(stderr, "TAG: Fatal error decoding tag\n"); + return -1; + case 0: + fprintf(stderr, "TAG: More data expected\n"); + return -1; + default: + printf("TAG: "); + ber_tlv_tag_fwrite(tlv_tag, stdout); + if(BER_TLV_CONSTRUCTED(data)) { + printf(" (constructed)"); + } else if(dsize >= 2 && data[0] == 0 && data[1] == 0) { + printf(" (end-of-content)"); + } else { + printf(" (primitive)"); + } + if(BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL) { + const char *str; + ber_tlv_tag_t tvalue = BER_TAG_VALUE(tlv_tag); + str = ASN_UNIVERSAL_TAG2STR(tvalue); + if(str) printf(" \"%s\"", str); + } + printf("\n"); + } - if(dsize > (size_t)len) { - len = ber_fetch_length(BER_TLV_CONSTRUCTED(data), - data + len, dsize - len, &tlv_len); - switch(len) { - case -1: - fprintf(stderr, - "LEN: Fatal error decoding length\n"); - return -1; - case 0: - fprintf(stderr, "LEN: More data expected\n"); - return -1; - default: - if(tlv_len == (ber_tlv_len_t)-1) - printf("LEN: Indefinite length encoding\n"); - else - printf("LEN: %ld bytes\n", (long)tlv_len); - } - } + if(dsize > (size_t)len) { + len = ber_fetch_length(BER_TLV_CONSTRUCTED(data), data + len, + dsize - len, &tlv_len); + switch(len) { + case -1: + fprintf(stderr, "LEN: Fatal error decoding length\n"); + return -1; + case 0: + fprintf(stderr, "LEN: More data expected\n"); + return -1; + default: + if(tlv_len == (ber_tlv_len_t)-1) + printf("LEN: Indefinite length encoding\n"); + else + printf("LEN: %ld bytes\n", (long)tlv_len); + } + } - return 0; + return 0; } /* * Dummy functions. */ -asn_dec_rval_t ber_check_tags(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx, const void *ptr, size_t size, int tag_mode, int last_tag_form, ber_tlv_len_t *last_length, int *opt_tlv_form) { asn_dec_rval_t rv = { 0, 0 }; (void)opt_codec_ctx; (void)td; (void)opt_ctx; (void)ptr; (void)size; (void)tag_mode; (void)last_tag_form; (void)last_length; (void)opt_tlv_form; return rv; } +asn_dec_rval_t +ber_check_tags(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, + asn_struct_ctx_t *opt_ctx, const void *ptr, size_t size, + int tag_mode, int last_tag_form, ber_tlv_len_t *last_length, + int *opt_tlv_form) { + asn_dec_rval_t rv = {0, 0}; + (void)opt_codec_ctx; + (void)td; + (void)opt_ctx; + (void)ptr; + (void)size; + (void)tag_mode; + (void)last_tag_form; + (void)last_length; + (void)opt_tlv_form; + return rv; +} -ssize_t der_write_tags(asn_TYPE_descriptor_t *td, size_t slen, int tag_mode, int last_tag_form, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) { (void)td; (void)slen; (void)tag_mode; (void)last_tag_form; (void)tag; (void)cb; (void)app_key; return -1; } +ssize_t +der_write_tags(asn_TYPE_descriptor_t *td, size_t slen, int tag_mode, + int last_tag_form, ber_tlv_tag_t tag, + asn_app_consume_bytes_f *cb, void *app_key) { + (void)td; + (void)slen; + (void)tag_mode; + (void)last_tag_form; + (void)tag; + (void)cb; + (void)app_key; + return -1; +} -asn_dec_rval_t xer_decode_general(asn_codec_ctx_t *opt_codec_ctx, asn_struct_ctx_t *ctx, void *struct_key, const char *xml_tag, const void *buf_ptr, size_t size, int (*otd)(void *struct_key, const void *chunk_buf, size_t chunk_size), ssize_t (*br)(void *struct_key, const void *chunk_buf, size_t chunk_size, int have_more)) { asn_dec_rval_t rv = { 0, 0 }; (void)opt_codec_ctx; (void)ctx; (void)struct_key; (void)xml_tag; (void)buf_ptr; (void)size; (void)otd; (void)br; return rv; } +asn_dec_rval_t +xer_decode_general(asn_codec_ctx_t *opt_codec_ctx, asn_struct_ctx_t *ctx, + void *struct_key, const char *xml_tag, const void *buf_ptr, + size_t size, + int (*otd)(void *struct_key, const void *chunk_buf, + size_t chunk_size), + ssize_t (*br)(void *struct_key, const void *chunk_buf, + size_t chunk_size, int have_more)) { + asn_dec_rval_t rv = {0, 0}; + (void)opt_codec_ctx; + (void)ctx; + (void)struct_key; + (void)xml_tag; + (void)buf_ptr; + (void)size; + (void)otd; + (void)br; + return rv; +} -asn_dec_rval_t OCTET_STRING_decode_uper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv = { 0, 0 }; (void)ctx; (void)td; (void)cts; (void)sptr; (void)pd; return rv; } +asn_dec_rval_t +OCTET_STRING_decode_uper(asn_codec_ctx_t *ctx, asn_TYPE_descriptor_t *td, + asn_per_constraints_t *cts, void **sptr, + asn_per_data_t *pd) { + asn_dec_rval_t rv = {0, 0}; + (void)ctx; + (void)td; + (void)cts; + (void)sptr; + (void)pd; + return rv; +} -asn_enc_rval_t OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, void *sptr, asn_per_outp_t *po) { asn_enc_rval_t er = { 0, 0, 0 }; (void)td; (void)cts; (void)sptr; (void)po; return er; } +asn_enc_rval_t +OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *cts, + void *sptr, asn_per_outp_t *po) { + asn_enc_rval_t er = {0, 0, 0}; + (void)td; + (void)cts; + (void)sptr; + (void)po; + return er; +} -size_t xer_whitespace_span(const void *chunk_buf, size_t chunk_size) { (void)chunk_buf; (void)chunk_size; return 0; } +size_t +xer_whitespace_span(const void *chunk_buf, size_t chunk_size) { + (void)chunk_buf; + (void)chunk_size; + return 0; +}