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