autoformatted (no semantic change)

This commit is contained in:
Lev Walkin 2016-01-23 08:50:28 -08:00
parent 438fe8796f
commit 41a1da63e5
4 changed files with 1308 additions and 1238 deletions

View File

@ -1,5 +1,5 @@
/*-
* Copyright (c) 2003-2014
* Copyright (c) 2003-2016
* Lev Walkin <vlm@lionet.info>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,8 +32,7 @@
#include "sys-common.h"
#undef COPYRIGHT
#define COPYRIGHT \
"Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>\n"
#define COPYRIGHT "Copyright (c) 2003-2016 Lev Walkin <vlm@lionet.info>\n"
#include <asn1parser.h> /* Parse the ASN.1 file and build a tree */
#include <asn1fix.h> /* Fix the ASN.1 tree */
@ -56,7 +55,7 @@ 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 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 */
@ -70,8 +69,7 @@ main(int ac, char **av) {
/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "EFf:g:hLPp:RS:vW:X")) != -1)
switch(ch) {
while((ch = getopt(ac, av, "EFf:g:hLPp:RS:vW:X")) != -1) switch(ch) {
case 'E':
print_arg__print_out = 1;
break;
@ -141,8 +139,7 @@ main(int ac, char **av) {
asn1c__add_pdu_type(pduname);
asn1_compiler_flags |= A1C_PDU_TYPE;
} else {
fprintf(stderr, "-pdu=%s"
": expected -pdu={all|auto|Type}\n",
fprintf(stderr, "-pdu=%s: expected -pdu={all|auto|Type}\n",
pduname);
exit(EX_USAGE);
}
@ -203,8 +200,7 @@ main(int ac, char **av) {
exit(EX_USAGE);
}
if(asn1_printer_flags) {
fprintf(stderr, "Error: "
"-print-... arguments require -E\n");
fprintf(stderr, "Error: -print-... arguments require -E\n");
exit(EX_USAGE);
}
}
@ -217,7 +213,8 @@ main(int ac, char **av) {
av += optind;
} else {
char *bin_name = a1c_basename(av[0]);
fprintf(stderr, "%s: No input files specified. "
fprintf(stderr,
"%s: No input files specified. "
"Try '%s -h' for more information\n",
bin_name, bin_name);
exit(1);
@ -250,8 +247,7 @@ main(int ac, char **av) {
"WARNING: skeletons are neither in "
"\"%s\" nor in \"%s\"!\n",
DATADIR, skeletons_dir);
if(warnings_as_errors)
exit(EX_OSFILE);
if(warnings_as_errors) exit(EX_OSFILE);
}
}
}
@ -290,8 +286,7 @@ main(int ac, char **av) {
* 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);
if(asn1print(asn, asn1_printer_flags)) exit(EX_SOFTWARE);
return 0;
}
@ -299,8 +294,7 @@ main(int ac, char **av) {
* Read in the files from skeletons/standard-modules
*/
if(importStandardModules(asn, skeletons_dir)) {
if(warnings_as_errors)
exit(EX_DATAERR);
if(warnings_as_errors) exit(EX_DATAERR);
}
/*
@ -312,8 +306,7 @@ main(int ac, char **av) {
NULL /* default fprintf(stderr) */);
switch(ret) {
case 1:
if(!warnings_as_errors)
/* Fall through */
if(!warnings_as_errors) /* Fall through */
case 0:
break; /* All clear */
case -1:
@ -324,8 +317,7 @@ main(int ac, char **av) {
* 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);
if(asn1print(asn, asn1_printer_flags)) exit(EX_SOFTWARE);
return 0;
}
@ -333,8 +325,8 @@ main(int ac, char **av) {
* 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)) {
if(asn1_compile(asn, skeletons_dir, asn1_compiler_flags, ac + optind,
optind - 1, av - optind)) {
exit(EX_SOFTWARE);
}
@ -370,8 +362,7 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
/*
* Figure out the standard-modules directory.
*/
target_dir_len = strlen(skeletons_dir)
+ sizeof("/standard-modules") - 1;
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",
@ -388,8 +379,7 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
dir = opendir(target_dir);
if(!dir) {
#endif
fprintf(stderr,
"WARNING: Cannot find standard modules in %s\n",
fprintf(stderr, "WARNING: Cannot find standard modules in %s\n",
target_dir);
return -1;
}
@ -402,8 +392,7 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
filename = dp->d_name;
#endif
len = strlen(filename);
if(len <= 5 || strcmp(filename + len - 5, ".asn1"))
continue;
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 */
@ -412,7 +401,8 @@ importStandardModules(asn1p_t *asn, const char *skeletons_dir) {
new_asn = asn1p_parse_file(filename, A1P_NOFLAGS);
if(new_asn == NULL) {
fprintf(stderr, "WARNING: Cannot parse standard module \"%s\"\n", filename);
fprintf(stderr, "WARNING: Cannot parse standard module \"%s\"\n",
filename);
ret = -1;
continue;
}
@ -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
);
a1c_basename(av0), DATADIR);
/* clang-format on */
exit(EX_USAGE);
}

View File

@ -34,10 +34,9 @@
#include <ber_tlv_length.c>
#undef COPYRIGHT
#define COPYRIGHT \
"Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>\n"
#define COPYRIGHT "Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>\n"
static void usage(const char *av0, int);/* Print the Usage screen and exit */
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);
@ -51,8 +50,7 @@ main(int ac, char **av) {
/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "nhv")) != -1)
switch(ch) {
while((ch = getopt(ac, av, "nhv")) != -1) switch(ch) {
case 'n':
no_validation++;
break;
@ -82,8 +80,7 @@ main(int ac, char **av) {
* All syntax trees from all files will be bundled together.
*/
for(i = 0; i < ac; i++) {
if(process(av[i]))
exit(EX_DATAERR);
if(process(av[i])) exit(EX_DATAERR);
}
return 0;
@ -101,7 +98,8 @@ usage(const char *av0, int copyright_only) {
fprintf(stderr,
"Usage: %s [-n] [-] [file ...]\n"
"Options:\n"
" -n Disable XML input validation\n", av0);
" -n Disable XML input validation\n",
av0);
exit(EX_USAGE);
}
@ -132,9 +130,8 @@ process(const char *fname) {
size_t len = strlen(buf);
if(!len) continue;
if(collector_offset || buf[len-1] != '\n') {
if((collector_size - collector_offset) <= len
|| !collector) {
if(collector_offset || buf[len - 1] != '\n') {
if((collector_size - collector_offset) <= len || !collector) {
collector_size <<= 1;
collector = REALLOC(collector, collector_size);
if(!collector) {
@ -145,10 +142,10 @@ process(const char *fname) {
memcpy(collector + collector_offset, buf, len + 1);
collector_offset += len;
}
if(buf[len-1] != '\n') continue;
if(buf[len - 1] != '\n') continue;
if(collector_offset) {
assert(collector[collector_offset-1] == '\n');
assert(collector[collector_offset - 1] == '\n');
process_line(fname, collector, ++lineno);
collector_offset = 0;
} else {
@ -156,8 +153,7 @@ process(const char *fname) {
}
}
if(fp != stdin)
fclose(fp);
if(fp != stdin) fclose(fp);
return 0;
}
@ -180,7 +176,8 @@ process_line(const char *fname, char *line, int lineno) {
(void)fname;
/* Skip the whitespace */
for(; *line == ' ' || *line == '\t'; line++);
for(; *line == ' ' || *line == '\t'; line++)
;
/* Find a tag opening angle bracket */
op = line;
@ -188,23 +185,21 @@ process_line(const char *fname, char *line, int lineno) {
case '<': /* That's what we want! A tag opening */
break;
case '-': /* This is a comment (dash-dash) */
if(op[1] == *op)
case '\r':
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);
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);
fprintf(stderr, "%s: Invalid charset (%d) at line %d\n", fname,
*(const unsigned char *)line, lineno);
exit(EX_DATAERR);
}
}
@ -217,9 +212,8 @@ process_line(const char *fname, char *line, int lineno) {
/* 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);
fprintf(stderr, "%s: Multiple tags per line at line %d\n", fname,
lineno);
exit(EX_DATAERR);
}
/* End-of-content octets */
@ -231,14 +225,23 @@ process_line(const char *fname, char *line, int lineno) {
}
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;
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",
"%s: Expected \"C\"/\"P\"/\"I\" as the XML tag name (%c) at "
"line %d\n",
fname, op[1], lineno);
exit(EX_DATAERR);
}
@ -247,7 +250,8 @@ process_line(const char *fname, char *line, int lineno) {
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);
"Re-run `unber` with -p option to disable pretty-printing.\n",
fname, lineno);
exit(EX_DATAERR);
}
@ -255,9 +259,8 @@ process_line(const char *fname, char *line, int lineno) {
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);
fprintf(stderr, "%s: Mandatory attribute %s is not found at line %d\n",
fname, (!tcl_pos) ? "T" : "V", lineno);
exit(EX_DATAERR);
}
errno = 0;
@ -268,11 +271,12 @@ process_line(const char *fname, char *line, int lineno) {
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);
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,13 +304,14 @@ process_line(const char *fname, char *line, int lineno) {
}
break;
}
/* clang-format on */
unsigned long tag_value_UL;
errno = 0;
if(!*tcl_pos
|| ((tag_value_UL = strtoul(tcl_pos, 0, 10)) > UINT_MAX)
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);
fprintf(stderr, "%s: Invalid tag value (%c) at line %d\n", fname,
*tcl_pos, lineno);
exit(EX_DATAERR);
} else {
tag_value = tag_value_UL;
@ -319,12 +324,12 @@ process_line(const char *fname, char *line, int lineno) {
buf[ret] = 0x80;
ret += 1;
} else {
ret += der_tlv_length_serialize(tlv_len,
buf + ret, sizeof(buf) - ret);
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 "
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);
@ -348,13 +353,14 @@ process_line(const char *fname, char *line, int lineno) {
}
cl++;
if(*cl != 'x') {
fprintf(stderr, "%s: Expected \"&#xNN;\" at line %d\n",
fname, lineno);
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,12 +377,12 @@ process_line(const char *fname, char *line, int lineno) {
fname, lineno, clv);
exit(EX_DATAERR);
}
/* clang-format on */
}
cl++;
if(*cl != ';') {
fprintf(stderr,
"%s: Expected \"&#xNN;\" at line %d\n",
fname, lineno);
fprintf(stderr, "%s: Expected \"&#xNN;\" at line %d\n", fname,
lineno);
exit(EX_DATAERR);
}
fputc(v, stdout);
@ -393,4 +399,3 @@ process_line(const char *fname, char *line, int lineno) {
return 0;
}

View File

@ -69,4 +69,3 @@
#endif
#define snprintf _snprintf
#endif /* _WIN32 */

View File

@ -41,8 +41,7 @@
#include <asn_codecs_prim.c>
#undef COPYRIGHT
#define COPYRIGHT \
"Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>\n"
#define COPYRIGHT "Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>\n"
static void usage(const char *av0); /* Print the Usage screen and exit */
static int process(const char *fname); /* Perform the BER decoding */
@ -62,8 +61,7 @@ main(int ac, char **av) {
/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "1hi:mps:t:v")) != -1)
switch(ch) {
while((ch = getopt(ac, av, "1hi:mps:t:v")) != -1) switch(ch) {
case '1':
single_type_decoding = 1;
break;
@ -73,7 +71,7 @@ main(int ac, char **av) {
memset(indent_bytes, ' ', i);
indent_bytes[i] = '\0';
} else {
fprintf(stderr, "-i %s: Invalid indent value\n",optarg);
fprintf(stderr, "-i %s: Invalid indent value\n", optarg);
exit(EX_USAGE);
}
break;
@ -86,14 +84,12 @@ main(int ac, char **av) {
case 's':
skip_bytes = atoi(optarg);
if(skip_bytes < 0) {
fprintf(stderr, "-s %s: positive value expected\n",
optarg);
fprintf(stderr, "-s %s: positive value expected\n", optarg);
exit(EX_USAGE);
}
break;
case 't':
if(decode_tlv_from_string(optarg))
exit(EX_DATAERR);
if(decode_tlv_from_string(optarg)) exit(EX_DATAERR);
exit(0);
case 'v':
fprintf(stderr, "ASN.1 BER Decoder, v" VERSION "\n" COPYRIGHT);
@ -122,8 +118,7 @@ main(int ac, char **av) {
* All syntax trees from all files will be bundled together.
*/
for(i = 0; i < ac; i++) {
if(process(av[i]))
exit(EX_DATAERR);
if(process(av[i])) exit(EX_DATAERR);
}
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,6 +153,7 @@ usage(const char *av0) {
" [F] Indicates that the value was reformatted (pretty-printed)\n"
"See the manual page for details\n"
, av0);
/* clang-format on */
exit(EX_USAGE);
}
@ -165,8 +162,13 @@ typedef enum pd_code {
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);
/*
@ -194,8 +196,8 @@ process(const char *fname) {
*/
for(; offset < skip_bytes; offset++) {
if(fgetc(fp) == -1) {
fprintf(stderr,
"%s: input source (%" PRIdASN " bytes) "
fprintf(stderr, "%s: input source (%" PRIdASN
" bytes) "
"has less data than \"-s %d\" switch "
"wants to skip\n",
fname, offset, skip_bytes);
@ -208,15 +210,12 @@ process(const char *fname) {
* Fetch out BER-encoded data until EOF or error.
*/
do {
pdc = process_deeper(fname, fp, &offset,
0, -1, &frame_size, 0, 0);
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;
if(pdc == PD_FAILED) return -1;
return 0;
}
@ -224,7 +223,9 @@ process(const char *fname) {
* 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) {
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;
@ -238,13 +239,13 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
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 ". "
" at %" PRIdASN
". "
"Broken or maliciously constructed file\n",
fname, (long)tblen, (long)limit, *offset);
return PD_FAILED;
@ -272,7 +273,8 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
t_len = ber_fetch_tag(tagbuf, tblen, &tlv_tag);
switch(t_len) {
case -1:
fprintf(stderr, "%s: Fatal error decoding tag"
fprintf(stderr,
"%s: Fatal error decoding tag"
" at %" PRIdASN "+%ld\n",
fname, *offset, (long)tblen);
return PD_FAILED;
@ -285,8 +287,8 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
* Decode the TLV length.
*/
constr = BER_TLV_CONSTRUCTED(tagbuf);
l_len = ber_fetch_length(constr,
tagbuf + t_len, tblen - t_len, &tlv_len);
l_len =
ber_fetch_length(constr, tagbuf + t_len, tblen - t_len, &tlv_len);
switch(l_len) {
case -1:
fprintf(stderr,
@ -303,8 +305,8 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
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);
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 */
@ -327,8 +329,7 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
if(expect_eoc && !tagbuf[0] && !tagbuf[1]) {
/* End of content octets */
print_TL(1, *offset - 2, level - 1, 1, 2, 0, -1,
effective_size);
print_TL(1, *offset - 2, level - 1, 1, 2, 0, -1, effective_size);
return PD_FINISHED;
}
@ -342,8 +343,8 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
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);
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);
@ -354,15 +355,12 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
local_esize += dec;
if(tlv_len == -1) {
tblen = 0;
if(pdc == PD_FINISHED
&& limit < 0 && !expect_eoc)
return pdc;
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(print_V(fname, fp, tlv_tag, tlv_len)) return PD_FAILED;
if(limit != -1) {
assert(limit >= tlv_len);
@ -374,22 +372,22 @@ process_deeper(const char *fname, FILE *fp, asn1c_integer_t *offset, int level,
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;
/* Report success for a single top level TLV */
if(level == 0 && limit == -1 && !expect_eoc)
break;
if(level == 0 && limit == -1 && !expect_eoc) break;
} /* for(;;) */
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("</P>\n");
return;
@ -401,8 +399,7 @@ print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, b
printf(constr ? ((tlv_len == -1) ? "I" : "C") : "P");
/* Print out the offset of this boundary, even if closing tag */
if(!minimalistic)
printf(" O=\"%" PRIdASN "\"", offset);
if(!minimalistic) printf(" O=\"%" PRIdASN "\"", offset);
printf(" T=\"");
ber_tlv_tag_fwrite(tlv_tag, stdout);
@ -417,8 +414,7 @@ print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, b
printf(" V=\"%ld\"", (long)tlv_len);
}
if(!minimalistic
&& BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL) {
if(!minimalistic && 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);
@ -426,8 +422,7 @@ print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, b
}
if(fin) {
if(constr && !minimalistic)
printf(" L=\"%ld\"", (long)effective_size);
if(constr && !minimalistic) printf(" L=\"%ld\"", (long)effective_size);
printf(">\n");
}
}
@ -436,7 +431,8 @@ print_TL(int fin, asn1c_integer_t offset, int level, int constr, ssize_t tlen, b
* 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) {
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;
@ -445,8 +441,7 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
ssize_t i;
/* Figure out what type is it */
if(BER_TAG_CLASS(tlv_tag) == ASN_TAG_CLASS_UNIVERSAL
&& pretty_printing) {
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);
}
@ -474,7 +469,7 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
break;
case ASN_BASIC_OBJECT_IDENTIFIER:
case ASN_BASIC_RELATIVE_OID:
if(tlv_len > 0 && tlv_len < 128*1024 /* VERY long 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);
@ -494,18 +489,14 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
break; /* Not directly compatible with UTF-8 */
default:
/* Conditionally compatible with UTF-8 */
if((
(etype & ASN_STRING_MASK)
||
(etype == ASN_BASIC_OCTET_STRING)
||
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)) {
&& pretty_printing))
&& (tlv_len > 0 && tlv_len < 128 * 1024)) {
vbuf = MALLOC(tlv_len + 1);
/* Not checking is intentional */
}
@ -522,8 +513,7 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
for(i = 0; i < tlv_len; i++) {
int ch = fgetc(fp);
if(ch == -1) {
fprintf(stderr,
"%s: Unexpected end of file (V)\n", fname);
fprintf(stderr, "%s: Unexpected end of file (V)\n", fname);
if(vbuf) FREEMEM(vbuf);
if(arcs) FREEMEM(arcs);
return -1;
@ -538,29 +528,36 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
case ASN_STRING_UTF8String:
switch(ch) {
default:
if(((etype == ASN_STRING_UTF8String)
|| !(ch & 0x80))
&& (ch >= 0x20)
) {
if(((etype == ASN_STRING_UTF8String) || !(ch & 0x80))
&& (ch >= 0x20)) {
printf("%c", ch);
break;
}
/* Fall through */
case 0x3c: case 0x3e: case 0x26:
case 0x3c:
case 0x3e:
case 0x26:
printf("&#x%02x;", ch);
}
break;
case ASN_BASIC_BOOLEAN:
switch(ch) {
case 0: printf("<false/>"); break;
case 0xff: printf("<true/>"); break;
default: printf("<true value=\"&#x%02x\"/>", ch);
case 0:
printf("<false/>");
break;
case 0xff:
printf("<true/>");
break;
default:
printf("<true value=\"&#x%02x\"/>", ch);
}
break;
case ASN_BASIC_INTEGER:
case ASN_BASIC_ENUMERATED:
if(i) collector = collector * 256 + ch;
else collector = (int)(signed char)ch;
if(i)
collector = collector * 256 + ch;
else
collector = (int)(signed char)ch;
break;
default:
if(vbuf) {
@ -585,8 +582,8 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
oid.buf = vbuf;
oid.size = tlv_len;
arcno = OBJECT_IDENTIFIER_get_arcs(&oid, arcs,
sizeof(*arcs), tlv_len + 1);
arcno = OBJECT_IDENTIFIER_get_arcs(&oid, arcs, sizeof(*arcs),
tlv_len + 1);
if(arcno >= 0) {
assert(arcno <= (tlv_len + 1));
printf(" F>");
@ -607,8 +604,7 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
oid.buf = vbuf;
oid.size = tlv_len;
arcno = RELATIVE_OID_get_arcs(&oid, arcs,
sizeof(*arcs), tlv_len);
arcno = RELATIVE_OID_get_arcs(&oid, arcs, sizeof(*arcs), tlv_len);
if(arcno >= 0) {
assert(arcno <= (tlv_len + 1));
printf(" F>");
@ -621,7 +617,8 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
}
}
break;
default: break;
default:
break;
}
/*
@ -638,8 +635,13 @@ print_V(const char *fname, FILE *fp, ber_tlv_tag_t tlv_tag, ber_tlv_len_t tlv_le
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;
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 */
@ -682,6 +684,7 @@ decode_tlv_from_string(const char *datastring) {
assert(data);
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,7 +704,11 @@ decode_tlv_from_string(const char *datastring) {
fprintf(stderr, "%s^ <- here\n", (char *)data);
return -1;
}
if(half) dp++; else (*dp) <<= 4;
/* clang-format on */
if(half)
dp++;
else
(*dp) <<= 4;
half = !half;
}
@ -709,8 +716,7 @@ decode_tlv_from_string(const char *datastring) {
dsize = dp - data;
printf("BER: ");
for(dp = data; dp < data + dsize; dp++)
printf("%02X", *dp);
for(dp = data; dp < data + dsize; dp++) printf("%02X", *dp);
printf("\n");
len = ber_fetch_tag(data, dsize, &tlv_tag);
@ -741,12 +747,11 @@ decode_tlv_from_string(const char *datastring) {
}
if(dsize > (size_t)len) {
len = ber_fetch_length(BER_TLV_CONSTRUCTED(data),
data + len, dsize - len, &tlv_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");
fprintf(stderr, "LEN: Fatal error decoding length\n");
return -1;
case 0:
fprintf(stderr, "LEN: More data expected\n");
@ -765,14 +770,85 @@ decode_tlv_from_string(const char *datastring) {
/*
* 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;
}