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