Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 1 | /* |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 2 | * Generic converter template for a selected ASN.1 type. |
Lev Walkin | d19216f | 2017-08-04 03:02:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. |
Lev Walkin | 8a4a06c | 2007-06-29 12:44:01 +0000 | [diff] [blame] | 4 | * All rights reserved. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 5 | * |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 6 | * To compile with your own ASN.1 type, please redefine the PDU as shown: |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 7 | * |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 8 | * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.c |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 9 | */ |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 10 | #ifdef HAVE_CONFIG_H |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 11 | #include <config.h> |
| 12 | #endif |
| 13 | #include <stdio.h> |
| 14 | #include <sys/types.h> |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 15 | #include <stdlib.h> /* for atoi(3) */ |
| 16 | #include <unistd.h> /* for getopt(3) */ |
| 17 | #include <string.h> /* for strerror(3) */ |
| 18 | #include <sysexits.h> /* for EX_* exit codes */ |
| 19 | #include <errno.h> /* for errno */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 20 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 21 | #include <asn_application.h> |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 22 | #include <asn_internal.h> /* for ASN__DEFAULT_STACK_MAX */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 23 | |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 24 | /* Convert "Type" defined by -DPDU into "asn_DEF_Type" */ |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 25 | |
| 26 | #ifndef NO_ASN_PDU |
| 27 | #ifndef PDU |
| 28 | #error Define -DPDU to compile this sample converter. |
| 29 | #endif |
| 30 | #ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */ |
| 31 | extern asn_TYPE_descriptor_t *asn_pdu_collection[]; |
| 32 | #endif |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 33 | #define ASN_DEF_PDU(t) asn_DEF_ ## t |
| 34 | #define DEF_PDU_Type(t) ASN_DEF_PDU(t) |
| 35 | #define PDU_Type DEF_PDU_Type(PDU) |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 36 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 37 | extern asn_TYPE_descriptor_t PDU_Type; /* ASN.1 type to be decoded */ |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 38 | #define PDU_Type_Ptr (&PDU_Type) |
| 39 | #else /* NO_ASN_PDU */ |
| 40 | #define PDU_Type_Ptr NULL |
| 41 | #endif /* NO_ASN_PDU */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 44 | * Open file and parse its contens. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 45 | */ |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 46 | static void *data_decode_from_file(enum asn_transfer_syntax, |
| 47 | asn_TYPE_descriptor_t *asnTypeOfPDU, |
| 48 | FILE *file, const char *name, |
| 49 | ssize_t suggested_bufsize, int first_pdu); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 50 | static int write_out(const void *buffer, size_t size, void *key); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 51 | static FILE *argument_to_file(char *av[], int idx); |
| 52 | static char *argument_to_name(char *av[], int idx); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 53 | |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 54 | int opt_debug; /* -d (or -dd) */ |
| 55 | static int opt_check; /* -c (constraints checking) */ |
| 56 | static int opt_stack; /* -s (maximum stack size) */ |
| 57 | static int opt_nopad; /* -per-nopad (PER input is not padded between msgs) */ |
| 58 | static int opt_onepdu; /* -1 (decode single PDU) */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 59 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 60 | #ifdef JUNKTEST /* Enable -J <probability> */ |
| 61 | #define JUNKOPT "J:" |
| 62 | static double opt_jprob; /* Junk bit probability */ |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 63 | static int junk_failures; |
| 64 | static void junk_bytes_with_probability(uint8_t *, size_t, double prob); |
| 65 | #else |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 66 | #define JUNKOPT |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 69 | /* Debug output function */ |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 70 | static void |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 71 | DEBUG(const char *fmt, ...) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 72 | va_list ap; |
| 73 | if(!opt_debug) return; |
| 74 | fprintf(stderr, "AD: "); |
| 75 | va_start(ap, fmt); |
| 76 | vfprintf(stderr, fmt, ap); |
| 77 | va_end(ap); |
| 78 | fprintf(stderr, "\n"); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 79 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 80 | |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 81 | static const char * |
| 82 | ats_simple_name(enum asn_transfer_syntax syntax) { |
| 83 | switch(syntax) { |
| 84 | case ATS_INVALID: |
| 85 | return "/dev/null"; |
| 86 | case ATS_NONSTANDARD_PLAINTEXT: |
| 87 | return "plaintext"; |
| 88 | case ATS_BER: |
| 89 | return "BER"; |
| 90 | case ATS_DER: |
| 91 | return "DER"; |
| 92 | case ATS_CER: |
| 93 | return "CER"; |
| 94 | case ATS_BASIC_OER: |
| 95 | case ATS_CANONICAL_OER: |
| 96 | return "OER"; |
| 97 | case ATS_BASIC_XER: |
| 98 | case ATS_CANONICAL_XER: |
| 99 | return "XER"; |
| 100 | case ATS_UNALIGNED_BASIC_PER: |
| 101 | case ATS_UNALIGNED_CANONICAL_PER: |
| 102 | return "PER"; |
| 103 | default: |
| 104 | return "<?>"; |
| 105 | } |
| 106 | } |
| 107 | |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 108 | |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 109 | #define HAS_CODEC(fname) ((&(((asn_TYPE_operation_t *)0)->fname)) != 0) |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 110 | typedef struct { |
| 111 | const char *name; |
| 112 | enum asn_transfer_syntax syntax; |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 113 | int has_codec; |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 114 | const char *full_name; |
| 115 | } syntax_selector; |
| 116 | |
| 117 | static syntax_selector input_encodings[] = { |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 118 | {"ber", ATS_BER, HAS_CODEC(ber_decoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 119 | "Input is in BER (Basic Encoding Rules) or DER"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 120 | {"oer", ATS_BASIC_OER, HAS_CODEC(oer_decoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 121 | "Input is in OER (Octet Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 122 | {"per", ATS_UNALIGNED_BASIC_PER, HAS_CODEC(uper_decoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 123 | "Input is in Unaligned PER (Packed Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 124 | {"xer", ATS_BASIC_XER, HAS_CODEC(xer_decoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 125 | "Input is in XER (XML Encoding Rules)"}, |
| 126 | {0, ATS_INVALID, 0, 0}}; |
| 127 | |
| 128 | static syntax_selector output_encodings[] = { |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 129 | {"der", ATS_DER, HAS_CODEC(der_encoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 130 | "Output as DER (Distinguished Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 131 | {"oer", ATS_CANONICAL_OER, HAS_CODEC(oer_encoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 132 | "Output as Canonical OER (Octet Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 133 | {"per", ATS_UNALIGNED_CANONICAL_PER, HAS_CODEC(uper_encoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 134 | "Output as Unaligned PER (Packed Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 135 | {"xer", ATS_BASIC_XER, HAS_CODEC(xer_encoder), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 136 | "Output as XER (XML Encoding Rules)"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 137 | {"text", ATS_NONSTANDARD_PLAINTEXT, HAS_CODEC(print_struct), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 138 | "Output as plain semi-structured text"}, |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 139 | {"null", ATS_INVALID, HAS_CODEC(print_struct), |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 140 | "Verify (decode) input, but do not output"}, |
| 141 | {0, ATS_INVALID, 0, 0}}; |
| 142 | |
| 143 | /* |
| 144 | * Select ASN.1 Transfer Enocoding Syntax by command line name. |
| 145 | */ |
| 146 | static const syntax_selector * |
| 147 | ats_by_name(const char *name, const asn_TYPE_descriptor_t *td, |
| 148 | const syntax_selector *first_element) { |
| 149 | const syntax_selector *element; |
| 150 | for(element = first_element; element->name; element++) { |
| 151 | if(strcmp(element->name, name) == 0) { |
| 152 | if(td && td->op |
| 153 | && *(const void *const *)(const void *)((const char *)td->op |
Lev Walkin | d5142d4 | 2017-09-15 23:33:42 -0700 | [diff] [blame] | 154 | + element->has_codec)) { |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 155 | return element; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | return NULL; |
| 160 | } |
| 161 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 162 | int |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 163 | main(int ac, char *av[]) { |
Lev Walkin | 7bb9d5b | 2017-08-26 23:31:58 -0700 | [diff] [blame] | 164 | FILE *binary_out; |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 165 | static asn_TYPE_descriptor_t *pduType = PDU_Type_Ptr; |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 166 | ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */ |
| 167 | int number_of_iterations = 1; |
| 168 | int num; |
| 169 | int ch; |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 170 | const syntax_selector *sel; |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 171 | enum asn_transfer_syntax isyntax = ATS_INVALID; |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 172 | enum asn_transfer_syntax osyntax = ATS_BASIC_XER; |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 173 | |
| 174 | #ifndef PDU |
| 175 | if(!pduType) { |
| 176 | fprintf(stderr, "No -DPDU defined during compilation.\n"); |
| 177 | #ifdef NO_ASN_PDU |
| 178 | exit(0); |
| 179 | #else |
| 180 | exit(EX_SOFTWARE); |
| 181 | #endif |
| 182 | } |
| 183 | #endif |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 184 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 185 | /* Figure out if specialty decoder needs to be default */ |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 186 | if(ats_by_name("oer", pduType, input_encodings)) |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 187 | isyntax = ATS_BASIC_OER; |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 188 | if(ats_by_name("per", pduType, input_encodings)) |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 189 | isyntax = ATS_UNALIGNED_BASIC_PER; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 190 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 191 | /* |
| 192 | * Pocess the command-line argments. |
| 193 | */ |
| 194 | while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1) |
| 195 | switch(ch) { |
| 196 | case 'i': |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 197 | sel = ats_by_name(optarg, pduType, input_encodings); |
| 198 | if(sel) { |
| 199 | isyntax = sel->syntax; |
| 200 | } else { |
| 201 | fprintf(stderr, "-i<format>: '%s': improper format selector\n", |
| 202 | optarg); |
| 203 | exit(EX_UNAVAILABLE); |
| 204 | } |
| 205 | break; |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 206 | case 'o': |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 207 | sel = ats_by_name(optarg, pduType, output_encodings); |
| 208 | if(sel) { |
| 209 | osyntax = sel->syntax; |
| 210 | } else { |
| 211 | fprintf(stderr, "-o<format>: '%s': improper format selector\n", |
| 212 | optarg); |
| 213 | exit(EX_UNAVAILABLE); |
| 214 | } |
| 215 | break; |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 216 | case '1': |
| 217 | opt_onepdu = 1; |
| 218 | break; |
| 219 | case 'b': |
| 220 | suggested_bufsize = atoi(optarg); |
| 221 | if(suggested_bufsize < 1 |
| 222 | || suggested_bufsize > 16 * 1024 * 1024) { |
| 223 | fprintf(stderr, |
| 224 | "-b %s: Improper buffer size (1..16M)\n", |
| 225 | optarg); |
| 226 | exit(EX_UNAVAILABLE); |
| 227 | } |
| 228 | break; |
| 229 | case 'c': |
| 230 | opt_check = 1; |
| 231 | break; |
| 232 | case 'd': |
| 233 | opt_debug++; /* Double -dd means ASN.1 debug */ |
| 234 | break; |
| 235 | case 'n': |
| 236 | number_of_iterations = atoi(optarg); |
| 237 | if(number_of_iterations < 1) { |
| 238 | fprintf(stderr, |
| 239 | "-n %s: Improper iterations count\n", optarg); |
| 240 | exit(EX_UNAVAILABLE); |
| 241 | } |
| 242 | break; |
| 243 | case 'p': |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 244 | if(strcmp(optarg, "er-nopad") == 0) { |
| 245 | opt_nopad = 1; |
| 246 | break; |
| 247 | } |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 248 | #ifdef ASN_PDU_COLLECTION |
| 249 | if(strcmp(optarg, "list") == 0) { |
| 250 | asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
| 251 | fprintf(stderr, "Available PDU types:\n"); |
| 252 | for(; *pdu; pdu++) printf("%s\n", (*pdu)->name); |
| 253 | exit(0); |
| 254 | } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') { |
| 255 | asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
| 256 | while(*pdu && strcmp((*pdu)->name, optarg)) pdu++; |
| 257 | if(*pdu) { pduType = *pdu; break; } |
| 258 | fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); |
Lev Walkin | 7e5ea1d | 2017-08-06 00:59:28 -0700 | [diff] [blame] | 259 | exit(EX_USAGE); |
| 260 | } |
| 261 | #else /* Without -pdu=auto there's just a single type */ |
| 262 | if(strcmp(optarg, "list") == 0) { |
| 263 | fprintf(stderr, "Available PDU types:\n"); |
| 264 | printf("%s\n", pduType->name); |
| 265 | exit(0); |
| 266 | } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') { |
| 267 | if(strcmp(optarg, pduType->name) == 0) { |
| 268 | break; |
| 269 | } |
| 270 | fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); |
| 271 | exit(EX_USAGE); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 272 | } |
| 273 | #endif /* ASN_PDU_COLLECTION */ |
| 274 | fprintf(stderr, "-p %s: Unrecognized option\n", optarg); |
| 275 | exit(EX_UNAVAILABLE); |
| 276 | case 's': |
| 277 | opt_stack = atoi(optarg); |
| 278 | if(opt_stack < 0) { |
| 279 | fprintf(stderr, |
| 280 | "-s %s: Non-negative value expected\n", |
| 281 | optarg); |
| 282 | exit(EX_UNAVAILABLE); |
| 283 | } |
| 284 | break; |
| 285 | #ifdef JUNKTEST |
| 286 | case 'J': |
| 287 | opt_jprob = strtod(optarg, 0); |
| 288 | if(opt_jprob <= 0.0 || opt_jprob > 1.0) { |
| 289 | fprintf(stderr, |
| 290 | "-J %s: Probability range 0..1 expected \n", |
| 291 | optarg); |
| 292 | exit(EX_UNAVAILABLE); |
| 293 | } |
| 294 | break; |
| 295 | #endif /* JUNKTEST */ |
| 296 | case 'h': |
| 297 | default: |
| 298 | #ifdef ASN_CONVERTER_TITLE |
| 299 | #define _AXS(x) #x |
| 300 | #define _ASX(x) _AXS(x) |
| 301 | fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE)); |
Lev Walkin | 8a4a06c | 2007-06-29 12:44:01 +0000 | [diff] [blame] | 302 | #endif |
Lev Walkin | ae0c3c2 | 2017-08-26 17:20:32 -0700 | [diff] [blame] | 303 | fprintf(stderr, "Usage: %s [options] <datafile> ...\n", av[0]); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 304 | fprintf(stderr, "Where options are:\n"); |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 305 | for(sel = input_encodings; sel->name; sel++) { |
| 306 | if(ats_by_name(sel->name, pduType, sel)) { |
| 307 | fprintf(stderr, " -i%s %s%s\n", sel->name, |
| 308 | sel->full_name, |
| 309 | (sel->syntax == isyntax) ? " (DEFAULT)" : ""); |
| 310 | } |
| 311 | } |
| 312 | for(sel = output_encodings; sel->name; sel++) { |
| 313 | if(ats_by_name(sel->name, pduType, sel)) { |
| 314 | fprintf(stderr, " -o%s%s %s%s\n", sel->name, |
| 315 | strlen(sel->name) > 3 ? "" : " ", |
| 316 | sel->full_name, |
| 317 | (sel->syntax == osyntax) ? " (DEFAULT)" : ""); |
| 318 | } |
| 319 | } |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 320 | if(pduType->op->uper_decoder) { |
| 321 | fprintf(stderr, |
| 322 | " -per-nopad Assume PER PDUs are not padded (-iper)\n"); |
| 323 | } |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 324 | #ifdef ASN_PDU_COLLECTION |
| 325 | fprintf(stderr, |
| 326 | " -p <PDU> Specify PDU type to decode\n" |
| 327 | " -p list List available PDUs\n"); |
| 328 | #endif /* ASN_PDU_COLLECTION */ |
| 329 | fprintf(stderr, |
| 330 | " -1 Decode only the first PDU in file\n" |
| 331 | " -b <size> Set the i/o buffer size (default is %ld)\n" |
| 332 | " -c Check ASN.1 constraints after decoding\n" |
| 333 | " -d Enable debugging (-dd is even better)\n" |
| 334 | " -n <num> Process files <num> times\n" |
| 335 | " -s <size> Set the stack usage limit (default is %d)\n" |
| 336 | #ifdef JUNKTEST |
| 337 | " -J <prob> Set random junk test bit garbaging probability\n" |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 338 | #endif |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 339 | , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX); |
| 340 | exit(EX_USAGE); |
| 341 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 342 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 343 | ac -= optind; |
| 344 | av += optind; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 345 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 346 | if(ac < 1) { |
| 347 | fprintf(stderr, "%s: No input files specified. " |
| 348 | "Try '-h' for more information\n", |
| 349 | av[-optind]); |
| 350 | exit(EX_USAGE); |
| 351 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 352 | |
Lev Walkin | 7bb9d5b | 2017-08-26 23:31:58 -0700 | [diff] [blame] | 353 | if(isatty(1)) { |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 354 | const int is_text_output = osyntax == ATS_NONSTANDARD_PLAINTEXT |
| 355 | || osyntax == ATS_BASIC_XER |
| 356 | || osyntax == ATS_CANONICAL_XER; |
Lev Walkin | 7bb9d5b | 2017-08-26 23:31:58 -0700 | [diff] [blame] | 357 | if(is_text_output) { |
| 358 | binary_out = stdout; |
| 359 | } else { |
| 360 | fprintf(stderr, "(Suppressing binary output to a terminal.)\n"); |
| 361 | binary_out = fopen("/dev/null", "wb"); |
| 362 | if(!binary_out) { |
| 363 | fprintf(stderr, "Can't open /dev/null: %s\n", strerror(errno)); |
| 364 | exit(EX_OSERR); |
| 365 | } |
| 366 | } |
| 367 | } else { |
| 368 | binary_out = stdout; |
| 369 | } |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 370 | setvbuf(stdout, 0, _IOLBF, 0); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 371 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 372 | for(num = 0; num < number_of_iterations; num++) { |
| 373 | int ac_i; |
| 374 | /* |
| 375 | * Process all files in turn. |
| 376 | */ |
| 377 | for(ac_i = 0; ac_i < ac; ac_i++) { |
| 378 | asn_enc_rval_t erv; |
| 379 | void *structure; /* Decoded structure */ |
| 380 | FILE *file = argument_to_file(av, ac_i); |
| 381 | char *name = argument_to_name(av, ac_i); |
| 382 | int first_pdu; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 383 | |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 384 | for(first_pdu = 1; file && (first_pdu || !opt_onepdu); first_pdu = 0) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 385 | /* |
| 386 | * Decode the encoded structure from file. |
| 387 | */ |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 388 | structure = data_decode_from_file(isyntax, pduType, file, name, |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 389 | suggested_bufsize, first_pdu); |
| 390 | if(!structure) { |
| 391 | if(errno) { |
| 392 | /* Error message is already printed */ |
| 393 | exit(EX_DATAERR); |
| 394 | } else { |
| 395 | /* EOF */ |
| 396 | break; |
| 397 | } |
| 398 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 399 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 400 | /* Check ASN.1 constraints */ |
| 401 | if(opt_check) { |
| 402 | char errbuf[128]; |
| 403 | size_t errlen = sizeof(errbuf); |
| 404 | if(asn_check_constraints(pduType, structure, errbuf, &errlen)) { |
| 405 | fprintf(stderr, |
| 406 | "%s: ASN.1 constraint " |
| 407 | "check failed: %s\n", |
| 408 | name, errbuf); |
| 409 | exit(EX_DATAERR); |
| 410 | } |
| 411 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 412 | |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 413 | if(osyntax == ATS_INVALID) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 414 | #ifdef JUNKTEST |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 415 | if(opt_jprob == 0.0) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 416 | fprintf(stderr, "%s: decoded successfully\n", name); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 417 | } |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 418 | #else |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 419 | fprintf(stderr, "%s: decoded successfully\n", name); |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 420 | #endif |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 421 | } else { |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 422 | erv = asn_encode(NULL, osyntax, pduType, structure, write_out, |
| 423 | binary_out); |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 424 | |
| 425 | if(erv.encoded == -1) { |
| 426 | fprintf(stderr, "%s: Cannot convert %s into %s\n", name, |
| 427 | pduType->name, ats_simple_name(osyntax)); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 428 | exit(EX_UNAVAILABLE); |
| 429 | } |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 430 | DEBUG("Encoded in %zd bytes of %s", erv.encoded, |
| 431 | ats_simple_name(osyntax)); |
Lev Walkin | 7bb9d5b | 2017-08-26 23:31:58 -0700 | [diff] [blame] | 432 | } |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 433 | |
Lev Walkin | 7bb9d5b | 2017-08-26 23:31:58 -0700 | [diff] [blame] | 434 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 435 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 436 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 437 | if(file && file != stdin) { |
| 438 | fclose(file); |
| 439 | } |
| 440 | } |
| 441 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 442 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 443 | #ifdef JUNKTEST |
| 444 | if(opt_jprob > 0.0) { |
| 445 | fprintf(stderr, "Junked %f OK (%d/%d)\n", |
| 446 | opt_jprob, junk_failures, number_of_iterations); |
| 447 | } |
| 448 | #endif /* JUNKTEST */ |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 449 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 450 | return 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 453 | static struct dynamic_buffer { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 454 | uint8_t *data; /* Pointer to the data bytes */ |
| 455 | size_t offset; /* Offset from the start */ |
| 456 | size_t length; /* Length of meaningful contents */ |
| 457 | size_t unbits; /* Unused bits in the last byte */ |
| 458 | size_t allocated; /* Allocated memory for data */ |
| 459 | int nreallocs; /* Number of data reallocations */ |
| 460 | off_t bytes_shifted; /* Number of bytes ever shifted */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 461 | } DynamicBuffer; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 462 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 463 | static void |
| 464 | buffer_dump() { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 465 | uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset; |
| 466 | uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0); |
| 467 | if(!opt_debug) return; |
| 468 | DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }", |
| 469 | DynamicBuffer.data, |
| 470 | (long)DynamicBuffer.offset, |
| 471 | (long)DynamicBuffer.length, |
| 472 | (long)DynamicBuffer.unbits, |
| 473 | (long)DynamicBuffer.allocated, |
| 474 | (long)DynamicBuffer.bytes_shifted); |
| 475 | for(; p < e; p++) { |
| 476 | fprintf(stderr, " %c%c%c%c%c%c%c%c", |
| 477 | ((*p >> 7) & 1) ? '1' : '0', |
| 478 | ((*p >> 6) & 1) ? '1' : '0', |
| 479 | ((*p >> 5) & 1) ? '1' : '0', |
| 480 | ((*p >> 4) & 1) ? '1' : '0', |
| 481 | ((*p >> 3) & 1) ? '1' : '0', |
| 482 | ((*p >> 2) & 1) ? '1' : '0', |
| 483 | ((*p >> 1) & 1) ? '1' : '0', |
| 484 | ((*p >> 0) & 1) ? '1' : '0'); |
| 485 | } |
| 486 | if(DynamicBuffer.unbits) { |
| 487 | unsigned int shift; |
| 488 | fprintf(stderr, " "); |
| 489 | for(shift = 7; shift >= DynamicBuffer.unbits; shift--) |
| 490 | fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0'); |
| 491 | fprintf(stderr, " %ld:%ld\n", |
| 492 | (long)DynamicBuffer.length - 1, |
| 493 | (long)8 - DynamicBuffer.unbits); |
| 494 | } else { |
| 495 | fprintf(stderr, " %ld\n", (long)DynamicBuffer.length); |
| 496 | } |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Move the buffer content left N bits, possibly joining it with |
| 501 | * preceeding content. |
| 502 | */ |
| 503 | static void |
| 504 | buffer_shift_left(size_t offset, int bits) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 505 | uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset; |
| 506 | uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset |
| 507 | + DynamicBuffer.length - 1; |
| 508 | |
| 509 | if(!bits) return; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 510 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 511 | DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)", |
| 512 | bits, (long)offset, |
| 513 | (long)DynamicBuffer.offset, |
| 514 | (long)DynamicBuffer.unbits, |
| 515 | (long)DynamicBuffer.length); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 516 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 517 | if(offset) { |
| 518 | int right; |
| 519 | right = ptr[0] >> (8 - bits); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 520 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 521 | DEBUG("oleft: %c%c%c%c%c%c%c%c", |
| 522 | ((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 523 | ((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 524 | ((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 525 | ((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 526 | ((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 527 | ((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 528 | ((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 529 | ((ptr[-1] >> 0) & 1) ? '1' : '0'); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 530 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 531 | DEBUG("oriht: %c%c%c%c%c%c%c%c", |
| 532 | ((ptr[0] >> 7) & 1) ? '1' : '0', |
| 533 | ((ptr[0] >> 6) & 1) ? '1' : '0', |
| 534 | ((ptr[0] >> 5) & 1) ? '1' : '0', |
| 535 | ((ptr[0] >> 4) & 1) ? '1' : '0', |
| 536 | ((ptr[0] >> 3) & 1) ? '1' : '0', |
| 537 | ((ptr[0] >> 2) & 1) ? '1' : '0', |
| 538 | ((ptr[0] >> 1) & 1) ? '1' : '0', |
| 539 | ((ptr[0] >> 0) & 1) ? '1' : '0'); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 540 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 541 | DEBUG("mriht: %c%c%c%c%c%c%c%c", |
| 542 | ((right >> 7) & 1) ? '1' : '0', |
| 543 | ((right >> 6) & 1) ? '1' : '0', |
| 544 | ((right >> 5) & 1) ? '1' : '0', |
| 545 | ((right >> 4) & 1) ? '1' : '0', |
| 546 | ((right >> 3) & 1) ? '1' : '0', |
| 547 | ((right >> 2) & 1) ? '1' : '0', |
| 548 | ((right >> 1) & 1) ? '1' : '0', |
| 549 | ((right >> 0) & 1) ? '1' : '0'); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 550 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 551 | ptr[-1] = (ptr[-1] & (0xff << bits)) | right; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 552 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 553 | DEBUG("after: %c%c%c%c%c%c%c%c", |
| 554 | ((ptr[-1] >> 7) & 1) ? '1' : '0', |
| 555 | ((ptr[-1] >> 6) & 1) ? '1' : '0', |
| 556 | ((ptr[-1] >> 5) & 1) ? '1' : '0', |
| 557 | ((ptr[-1] >> 4) & 1) ? '1' : '0', |
| 558 | ((ptr[-1] >> 3) & 1) ? '1' : '0', |
| 559 | ((ptr[-1] >> 2) & 1) ? '1' : '0', |
| 560 | ((ptr[-1] >> 1) & 1) ? '1' : '0', |
| 561 | ((ptr[-1] >> 0) & 1) ? '1' : '0'); |
| 562 | } |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 563 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 564 | buffer_dump(); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 565 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 566 | for(; ptr < end; ptr++) { |
| 567 | int right = ptr[1] >> (8 - bits); |
| 568 | *ptr = (*ptr << bits) | right; |
| 569 | } |
| 570 | *ptr <<= bits; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 571 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 572 | DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits); |
| 573 | if(DynamicBuffer.unbits == 0) { |
| 574 | DynamicBuffer.unbits += bits; |
| 575 | } else { |
| 576 | DynamicBuffer.unbits += bits; |
| 577 | if(DynamicBuffer.unbits > 7) { |
| 578 | DynamicBuffer.unbits -= 8; |
| 579 | DynamicBuffer.length--; |
| 580 | DynamicBuffer.bytes_shifted++; |
| 581 | } |
| 582 | } |
| 583 | DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 584 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 585 | buffer_dump(); |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 586 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 587 | DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)", |
| 588 | (long)DynamicBuffer.offset, |
| 589 | (long)DynamicBuffer.unbits, |
| 590 | (long)DynamicBuffer.length); |
| 591 | |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 592 | |
| 593 | } |
| 594 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 595 | /* |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 596 | * Ensure that the buffer contains at least this amount of free space. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 597 | */ |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 598 | static void add_bytes_to_buffer(const void *data2add, size_t bytes) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 599 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 600 | if(bytes == 0) return; |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 601 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 602 | DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 603 | (long)bytes, |
| 604 | (long)DynamicBuffer.offset, |
| 605 | (long)DynamicBuffer.length, |
| 606 | (long)DynamicBuffer.unbits, |
| 607 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 608 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 609 | if(DynamicBuffer.allocated |
| 610 | >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) { |
| 611 | DEBUG("\tNo buffer reallocation is necessary"); |
| 612 | } else if(bytes <= DynamicBuffer.offset) { |
| 613 | DEBUG("\tContents shifted by %ld", DynamicBuffer.offset); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 614 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 615 | /* Shift the buffer contents */ |
| 616 | memmove(DynamicBuffer.data, |
| 617 | DynamicBuffer.data + DynamicBuffer.offset, |
| 618 | DynamicBuffer.length); |
| 619 | DynamicBuffer.bytes_shifted += DynamicBuffer.offset; |
| 620 | DynamicBuffer.offset = 0; |
| 621 | } else { |
| 622 | size_t newsize = (DynamicBuffer.allocated << 2) + bytes; |
| 623 | void *p = MALLOC(newsize); |
| 624 | if(!p) { |
| 625 | perror("malloc()"); |
| 626 | exit(EX_OSERR); |
| 627 | } |
| 628 | memcpy(p, |
| 629 | DynamicBuffer.data + DynamicBuffer.offset, |
| 630 | DynamicBuffer.length); |
| 631 | FREEMEM(DynamicBuffer.data); |
| 632 | DynamicBuffer.data = (uint8_t *)p; |
| 633 | DynamicBuffer.offset = 0; |
| 634 | DynamicBuffer.allocated = newsize; |
| 635 | DynamicBuffer.nreallocs++; |
| 636 | DEBUG("\tBuffer reallocated to %ld (%d time)", |
| 637 | newsize, DynamicBuffer.nreallocs); |
| 638 | } |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 639 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 640 | memcpy(DynamicBuffer.data |
| 641 | + DynamicBuffer.offset + DynamicBuffer.length, |
| 642 | data2add, bytes); |
| 643 | DynamicBuffer.length += bytes; |
| 644 | if(DynamicBuffer.unbits) { |
| 645 | int bits = DynamicBuffer.unbits; |
| 646 | DynamicBuffer.unbits = 0; |
| 647 | buffer_shift_left(DynamicBuffer.length - bytes, bits); |
| 648 | } |
Lev Walkin | 5a621d6 | 2006-09-18 20:05:34 +0000 | [diff] [blame] | 649 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 650 | DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }", |
| 651 | (long)bytes, |
| 652 | (long)DynamicBuffer.offset, |
| 653 | (long)DynamicBuffer.length, |
| 654 | (long)DynamicBuffer.unbits, |
| 655 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Lev Walkin | e8bbe93 | 2017-09-12 23:06:52 -0700 | [diff] [blame] | 658 | static int |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 659 | is_syntax_PER(enum asn_transfer_syntax syntax) { |
Lev Walkin | e8bbe93 | 2017-09-12 23:06:52 -0700 | [diff] [blame] | 660 | return (syntax != ATS_UNALIGNED_BASIC_PER |
| 661 | && syntax != ATS_UNALIGNED_CANONICAL_PER); |
| 662 | } |
| 663 | |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 664 | static int |
| 665 | restartability_supported(enum asn_transfer_syntax syntax) { |
| 666 | return is_syntax_PER(syntax); |
| 667 | } |
| 668 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 669 | static void * |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 670 | data_decode_from_file(enum asn_transfer_syntax isyntax, asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 671 | static uint8_t *fbuf; |
| 672 | static ssize_t fbuf_size; |
| 673 | static asn_codec_ctx_t s_codec_ctx; |
| 674 | asn_codec_ctx_t *opt_codec_ctx = 0; |
| 675 | void *structure = 0; |
| 676 | asn_dec_rval_t rval; |
| 677 | size_t old_offset; |
| 678 | size_t new_offset; |
| 679 | int tolerate_eof; |
| 680 | size_t rd; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 681 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 682 | if(!file) { |
| 683 | fprintf(stderr, "%s: %s\n", name, strerror(errno)); |
| 684 | errno = EINVAL; |
| 685 | return 0; |
| 686 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 687 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 688 | if(opt_stack) { |
| 689 | s_codec_ctx.max_stack_size = opt_stack; |
| 690 | opt_codec_ctx = &s_codec_ctx; |
| 691 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 692 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 693 | DEBUG("Processing %s", name); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 694 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 695 | /* prepare the file buffer */ |
| 696 | if(fbuf_size != suggested_bufsize) { |
| 697 | fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize); |
| 698 | if(!fbuf) { |
| 699 | perror("realloc()"); |
| 700 | exit(EX_OSERR); |
| 701 | } |
| 702 | fbuf_size = suggested_bufsize; |
| 703 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 704 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 705 | if(on_first_pdu) { |
| 706 | DynamicBuffer.offset = 0; |
| 707 | DynamicBuffer.length = 0; |
| 708 | DynamicBuffer.unbits = 0; |
| 709 | DynamicBuffer.allocated = 0; |
| 710 | DynamicBuffer.bytes_shifted = 0; |
| 711 | DynamicBuffer.nreallocs = 0; |
| 712 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 713 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 714 | old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 715 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 716 | /* Pretend immediate EOF */ |
| 717 | rval.code = RC_WMORE; |
| 718 | rval.consumed = 0; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 719 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 720 | for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */ |
| 721 | (rd = fread(fbuf, 1, fbuf_size, file)) |
| 722 | || feof(file) == 0 |
| 723 | || (tolerate_eof && DynamicBuffer.length) |
| 724 | ;) { |
| 725 | int ecbits = 0; /* Extra consumed bits in case of PER */ |
| 726 | uint8_t *i_bptr; |
| 727 | size_t i_size; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 728 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 729 | /* |
| 730 | * Copy the data over, or use the original buffer. |
| 731 | */ |
| 732 | if(DynamicBuffer.allocated) { |
| 733 | /* Append new data into the existing dynamic buffer */ |
| 734 | add_bytes_to_buffer(fbuf, rd); |
| 735 | i_bptr = DynamicBuffer.data + DynamicBuffer.offset; |
| 736 | i_size = DynamicBuffer.length; |
| 737 | } else { |
| 738 | i_bptr = fbuf; |
| 739 | i_size = rd; |
| 740 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 741 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 742 | DEBUG("Decoding %ld bytes", (long)i_size); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 743 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 744 | #ifdef JUNKTEST |
| 745 | junk_bytes_with_probability(i_bptr, i_size, opt_jprob); |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 746 | #endif |
| 747 | |
Lev Walkin | 0f77cea | 2017-09-13 00:28:20 -0700 | [diff] [blame] | 748 | if(is_syntax_PER(isyntax) && opt_nopad) { |
| 749 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 750 | rval.code = RC_FAIL; |
| 751 | rval.consumed = 0; |
| 752 | #else |
| 753 | rval = uper_decode(opt_codec_ctx, pduType, (void **)&structure, |
| 754 | i_bptr, i_size, 0, DynamicBuffer.unbits); |
| 755 | /* uper_decode() returns bits! */ |
| 756 | ecbits = rval.consumed % 8; /* Bits consumed from the last byte */ |
| 757 | rval.consumed >>= 3; /* Convert bits into bytes. */ |
| 758 | #endif |
| 759 | /* Non-padded PER decoder */ |
| 760 | } else { |
| 761 | rval = asn_decode(opt_codec_ctx, isyntax, pduType, |
| 762 | (void **)&structure, i_bptr, i_size); |
| 763 | } |
Lev Walkin | e8bbe93 | 2017-09-12 23:06:52 -0700 | [diff] [blame] | 764 | if(rval.code == RC_WMORE && !restartability_supported(isyntax)) { |
| 765 | /* PER does not support restartability */ |
| 766 | ASN_STRUCT_FREE(*pduType, structure); |
| 767 | structure = 0; |
Lev Walkin | 6903380 | 2017-08-25 12:15:58 -0700 | [diff] [blame] | 768 | rval.consumed = 0; |
Lev Walkin | e8bbe93 | 2017-09-12 23:06:52 -0700 | [diff] [blame] | 769 | /* Continue accumulating data */ |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 770 | } |
Lev Walkin | e8bbe93 | 2017-09-12 23:06:52 -0700 | [diff] [blame] | 771 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 772 | DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d", |
| 773 | (long)DynamicBuffer.length, |
| 774 | (long)rval.consumed, ecbits, (long)i_size, |
| 775 | rval.code); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 776 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 777 | if(DynamicBuffer.allocated == 0) { |
| 778 | /* |
| 779 | * Flush remainder into the intermediate buffer. |
| 780 | */ |
| 781 | if(rval.code != RC_FAIL && rval.consumed < rd) { |
| 782 | add_bytes_to_buffer(fbuf + rval.consumed, |
| 783 | rd - rval.consumed); |
| 784 | buffer_shift_left(0, ecbits); |
| 785 | DynamicBuffer.bytes_shifted = rval.consumed; |
| 786 | rval.consumed = 0; |
| 787 | ecbits = 0; |
| 788 | } |
| 789 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 790 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 791 | /* |
| 792 | * Adjust position inside the source buffer. |
| 793 | */ |
| 794 | if(DynamicBuffer.allocated) { |
| 795 | DynamicBuffer.offset += rval.consumed; |
| 796 | DynamicBuffer.length -= rval.consumed; |
| 797 | } else { |
| 798 | DynamicBuffer.bytes_shifted += rval.consumed; |
| 799 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 800 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 801 | switch(rval.code) { |
| 802 | case RC_OK: |
| 803 | if(ecbits) buffer_shift_left(0, ecbits); |
| 804 | DEBUG("RC_OK, finishing up with %ld+%d", |
| 805 | (long)rval.consumed, ecbits); |
| 806 | return structure; |
| 807 | case RC_WMORE: |
| 808 | DEBUG("RC_WMORE, continuing read=%ld, cons=%ld " |
| 809 | " with %ld..%ld-%ld..%ld", |
| 810 | (long)rd, |
| 811 | (long)rval.consumed, |
| 812 | (long)DynamicBuffer.offset, |
| 813 | (long)DynamicBuffer.length, |
| 814 | (long)DynamicBuffer.unbits, |
| 815 | (long)DynamicBuffer.allocated); |
| 816 | if(!rd) tolerate_eof--; |
| 817 | continue; |
| 818 | case RC_FAIL: |
| 819 | break; |
| 820 | } |
| 821 | break; |
| 822 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 823 | |
Lev Walkin | b48bc2f | 2017-09-12 22:39:47 -0700 | [diff] [blame] | 824 | DEBUG("Clean up partially decoded %s", pduType->name); |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 825 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 826 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 827 | new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 828 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 829 | /* |
| 830 | * Print a message and return failure only if not EOF, |
| 831 | * unless this is our first PDU (empty file). |
| 832 | */ |
| 833 | if(on_first_pdu |
| 834 | || DynamicBuffer.length |
Lev Walkin | 6d46bc3 | 2017-09-12 21:34:00 -0700 | [diff] [blame] | 835 | || new_offset - old_offset > ((isyntax == ATS_BASIC_XER)?sizeof("\r\n")-1:0) |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 836 | ) { |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 837 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 838 | #ifdef JUNKTEST |
| 839 | /* |
| 840 | * Nothing's wrong with being unable to decode junk. |
| 841 | * Simulate EOF. |
| 842 | */ |
| 843 | if(opt_jprob != 0.0) { |
| 844 | junk_failures++; |
| 845 | errno = 0; |
| 846 | return 0; |
| 847 | } |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 848 | #endif |
| 849 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 850 | DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld", |
| 851 | on_first_pdu, (long)new_offset, (long)old_offset, |
| 852 | (long)DynamicBuffer.length); |
| 853 | fprintf(stderr, "%s: " |
| 854 | "Decode failed past byte %ld: %s\n", |
| 855 | name, (long)new_offset, |
| 856 | (rval.code == RC_WMORE) |
| 857 | ? "Unexpected end of input" |
| 858 | : "Input processing error"); |
| 859 | #ifndef ENOMSG |
| 860 | #define ENOMSG EINVAL |
Lev Walkin | 4bf96b9 | 2006-09-18 21:46:50 +0000 | [diff] [blame] | 861 | #endif |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 862 | #ifndef EBADMSG |
| 863 | #define EBADMSG EINVAL |
Lev Walkin | 4bf96b9 | 2006-09-18 21:46:50 +0000 | [diff] [blame] | 864 | #endif |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 865 | errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG; |
| 866 | } else { |
| 867 | /* Got EOF after a few successful PDUs */ |
| 868 | errno = 0; |
| 869 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 870 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 871 | return 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 874 | /* Dump the buffer out to the specified FILE */ |
| 875 | static int write_out(const void *buffer, size_t size, void *key) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 876 | FILE *fp = (FILE *)key; |
| 877 | return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1; |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 878 | } |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 879 | |
| 880 | static int argument_is_stdin(char *av[], int idx) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 881 | if(strcmp(av[idx], "-")) { |
| 882 | return 0; /* Certainly not <stdin> */ |
| 883 | } else { |
| 884 | /* This might be <stdin>, unless `./program -- -` */ |
| 885 | if(strcmp(av[-1], "--")) |
| 886 | return 1; |
| 887 | else |
| 888 | return 0; |
| 889 | } |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static FILE *argument_to_file(char *av[], int idx) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 893 | return argument_is_stdin(av, idx) ? stdin : fopen(av[idx], "rb"); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static char *argument_to_name(char *av[], int idx) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 897 | return argument_is_stdin(av, idx) ? "standard input" : av[idx]; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 898 | } |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 899 | |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 900 | #ifdef JUNKTEST |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 901 | /* |
| 902 | * Fill bytes with some garbage with specified probability (more or less). |
| 903 | */ |
| 904 | static void |
| 905 | junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) { |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 906 | static int junkmode; |
| 907 | uint8_t *ptr; |
| 908 | uint8_t *end; |
| 909 | if(opt_jprob <= 0.0) return; |
| 910 | for(ptr = buf, end = ptr + size; ptr < end; ptr++) { |
| 911 | int byte = *ptr; |
| 912 | if(junkmode++ & 1) { |
| 913 | if((((double)random() / RAND_MAX) < prob)) |
| 914 | byte = random() & 0xff; |
| 915 | } else { |
| 916 | #define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0) |
| 917 | byte ^= BPROB(0x80); |
| 918 | byte ^= BPROB(0x40); |
| 919 | byte ^= BPROB(0x20); |
| 920 | byte ^= BPROB(0x10); |
| 921 | byte ^= BPROB(0x08); |
| 922 | byte ^= BPROB(0x04); |
| 923 | byte ^= BPROB(0x02); |
| 924 | byte ^= BPROB(0x01); |
| 925 | } |
| 926 | if(byte != *ptr) { |
| 927 | DEBUG("Junk buf[%d] %02x -> %02x", ptr - buf, *ptr, byte); |
| 928 | *ptr = byte; |
| 929 | } |
| 930 | } |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 931 | } |
Lev Walkin | fb35e08 | 2017-08-04 03:06:51 -0700 | [diff] [blame] | 932 | #endif /* JUNKTEST */ |
Lev Walkin | 1f12da4 | 2006-09-24 19:47:07 +0000 | [diff] [blame] | 933 | |