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