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