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