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 <assert.h> /* for assert(3) */ |
| 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 | |
| 43 | int opt_debug; /* -d */ |
| 44 | static int opt_check; /* -c */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 45 | static int opt_stack; /* -s */ |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 46 | static int opt_onepdu; /* -1 */ |
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 | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 64 | /* Debug output function */ |
| 65 | static inline void |
| 66 | DEBUG(const char *fmt, ...) { |
| 67 | va_list ap; |
| 68 | if(!opt_debug) return; |
| 69 | fprintf(stderr, "AD: "); |
| 70 | va_start(ap, fmt); |
| 71 | vfprintf(stderr, fmt, ap); |
| 72 | va_end(ap); |
| 73 | fprintf(stderr, "\n"); |
| 74 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 75 | |
| 76 | int |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 77 | main(int ac, char *av[]) { |
Lev Walkin | d062568 | 2006-08-25 02:35:08 +0000 | [diff] [blame] | 78 | static asn_TYPE_descriptor_t *pduType = &PDU_Type; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 79 | ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */ |
| 80 | int number_of_iterations = 1; |
| 81 | int num; |
| 82 | int ch; |
| 83 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 84 | /* Figure out if Unaligned PER needs to be default */ |
| 85 | if(pduType->uper_decoder) |
| 86 | iform = INP_PER; |
| 87 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Pocess the command-line argments. |
| 90 | */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 91 | while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1) |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 92 | switch(ch) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 93 | case 'i': |
| 94 | if(optarg[0] == 'b') { iform = INP_BER; break; } |
| 95 | if(optarg[0] == 'x') { iform = INP_XER; break; } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 96 | if(pduType->uper_decoder |
| 97 | && optarg[0] == 'p') { iform = INP_PER; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 98 | fprintf(stderr, "-i<format>: '%s': improper format selector\n", |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 99 | optarg); |
| 100 | exit(EX_UNAVAILABLE); |
| 101 | case 'o': |
| 102 | if(optarg[0] == 'd') { oform = OUT_DER; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 103 | if(pduType->uper_encoder |
| 104 | && optarg[0] == 'p') { oform = OUT_PER; break; } |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 105 | if(optarg[0] == 'x') { oform = OUT_XER; break; } |
| 106 | if(optarg[0] == 't') { oform = OUT_TEXT; break; } |
| 107 | if(optarg[0] == 'n') { oform = OUT_NULL; break; } |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 108 | fprintf(stderr, "-o<format>: '%s': improper format selector\n", |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 109 | optarg); |
| 110 | exit(EX_UNAVAILABLE); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 111 | case '1': |
| 112 | opt_onepdu = 1; |
| 113 | break; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 114 | case 'b': |
| 115 | suggested_bufsize = atoi(optarg); |
| 116 | if(suggested_bufsize < 1 |
| 117 | || suggested_bufsize > 16 * 1024 * 1024) { |
| 118 | fprintf(stderr, |
| 119 | "-b %s: Improper buffer size (1..16M)\n", |
| 120 | optarg); |
| 121 | exit(EX_UNAVAILABLE); |
| 122 | } |
| 123 | break; |
| 124 | case 'c': |
| 125 | opt_check = 1; |
| 126 | break; |
| 127 | case 'd': |
| 128 | opt_debug++; /* Double -dd means ASN.1 debug */ |
| 129 | break; |
| 130 | case 'n': |
| 131 | number_of_iterations = atoi(optarg); |
| 132 | if(number_of_iterations < 1) { |
| 133 | fprintf(stderr, |
| 134 | "-n %s: Improper iterations count\n", optarg); |
| 135 | exit(EX_UNAVAILABLE); |
| 136 | } |
| 137 | break; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 138 | case 'p': |
| 139 | #ifdef ASN_PDU_COLLECTION |
| 140 | { |
| 141 | asn_TYPE_descriptor_t **pdu = asn_pdu_collection; |
| 142 | if(optarg[0] < 'A' || optarg[0] > 'Z') { |
| 143 | fprintf(stderr, "Available PDU types:\n"); |
| 144 | for(; *pdu; pdu++) printf("%s\n", (*pdu)->name); |
| 145 | exit(0); |
| 146 | } |
| 147 | while(*pdu && strcmp((*pdu)->name, optarg)) pdu++; |
| 148 | if(*pdu) { pduType = *pdu; break; } |
| 149 | } |
| 150 | #endif /* ASN_PDU_COLLECTION */ |
| 151 | fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg); |
| 152 | exit(EX_UNAVAILABLE); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 153 | case 's': |
| 154 | opt_stack = atoi(optarg); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 155 | if(opt_stack < 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 156 | fprintf(stderr, |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 157 | "-s %s: Non-negative value expected\n", |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 158 | optarg); |
| 159 | exit(EX_UNAVAILABLE); |
| 160 | } |
| 161 | break; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 162 | case 'h': |
| 163 | default: |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 164 | fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]); |
| 165 | fprintf(stderr, "Where options are:\n"); |
| 166 | if(pduType->uper_decoder) |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 167 | fprintf(stderr, |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 168 | " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 169 | fprintf(stderr, |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 170 | " -iber Input is in BER (Basic Encoding Rules)%s\n", |
| 171 | iform == INP_PER ? "" : " (DEFAULT)"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 172 | fprintf(stderr, |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 173 | " -ixer Input is in XER (XML Encoding Rules)\n"); |
| 174 | if(pduType->uper_encoder) |
| 175 | fprintf(stderr, |
| 176 | " -oper Output in Unaligned PER (Packed Encoding Rules)\n"); |
| 177 | fprintf(stderr, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 178 | " -oder Output in DER (Distinguished Encoding Rules)\n" |
Lev Walkin | 857d11b | 2006-02-22 08:54:49 +0000 | [diff] [blame] | 179 | " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n" |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 180 | " -otext Output in plain semi-structured text (dump)\n" |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 181 | " -onull Verify (decode) input, but do not output\n"); |
| 182 | #ifdef ASN_PDU_COLLECTION |
| 183 | fprintf(stderr, |
| 184 | " -p <PDU> Specify PDU type to decode\n" |
| 185 | " -p list List available PDUs\n"); |
| 186 | #endif /* ASN_PDU_COLLECTION */ |
| 187 | fprintf(stderr, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 188 | " -b <size> Set the i/o buffer size (default is %ld)\n" |
| 189 | " -c Check ASN.1 constraints after decoding\n" |
| 190 | " -d Enable debugging (-dd is even better)\n" |
| 191 | " -n <num> Process files <num> times\n" |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 192 | " -s <size> Set the stack usage limit (default is %d)\n" |
| 193 | , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 194 | exit(EX_USAGE); |
| 195 | } |
| 196 | |
| 197 | ac -= optind; |
| 198 | av += optind; |
| 199 | |
| 200 | if(ac < 1) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 201 | fprintf(stderr, "%s: No input files specified. " |
| 202 | "Try '-h' for more information\n", |
| 203 | av[-optind]); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 204 | exit(EX_USAGE); |
| 205 | } |
| 206 | |
| 207 | setvbuf(stdout, 0, _IOLBF, 0); |
| 208 | |
| 209 | for(num = 0; num < number_of_iterations; num++) { |
| 210 | int ac_i; |
| 211 | /* |
| 212 | * Process all files in turn. |
| 213 | */ |
| 214 | for(ac_i = 0; ac_i < ac; ac_i++) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 215 | asn_enc_rval_t erv; |
| 216 | void *structure; /* Decoded structure */ |
| 217 | FILE *file = argument_to_file(av, ac_i); |
| 218 | char *name = argument_to_name(av, ac_i); |
| 219 | int first_pdu; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 220 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 221 | for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 222 | /* |
| 223 | * Decode the encoded structure from file. |
| 224 | */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 225 | structure = data_decode_from_file(pduType, |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 226 | file, name, suggested_bufsize, first_pdu); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 227 | if(!structure) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 228 | if(errno) { |
| 229 | /* Error message is already printed */ |
| 230 | exit(EX_DATAERR); |
| 231 | } else { |
| 232 | /* EOF */ |
| 233 | break; |
| 234 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 237 | /* Check ASN.1 constraints */ |
| 238 | if(opt_check) { |
| 239 | char errbuf[128]; |
| 240 | size_t errlen = sizeof(errbuf); |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 241 | if(asn_check_constraints(pduType, structure, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 242 | errbuf, &errlen)) { |
| 243 | fprintf(stderr, "%s: ASN.1 constraint " |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 244 | "check failed: %s\n", name, errbuf); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 245 | exit(EX_DATAERR); |
| 246 | } |
| 247 | } |
| 248 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 249 | switch(oform) { |
| 250 | case OUT_NULL: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 251 | fprintf(stderr, "%s: decoded successfully\n", name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 252 | break; |
| 253 | case OUT_TEXT: /* -otext */ |
| 254 | asn_fprint(stdout, pduType, structure); |
| 255 | break; |
| 256 | case OUT_XER: /* -oxer */ |
| 257 | if(xer_fprint(stdout, pduType, structure)) { |
| 258 | fprintf(stderr, "%s: Cannot convert into XML\n", |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 259 | name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 260 | exit(EX_UNAVAILABLE); |
| 261 | } |
| 262 | break; |
| 263 | case OUT_DER: |
| 264 | erv = der_encode(pduType, structure, write_out, stdout); |
| 265 | if(erv.encoded < 0) { |
| 266 | fprintf(stderr, "%s: Cannot convert into DER\n", |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 267 | name); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 268 | exit(EX_UNAVAILABLE); |
| 269 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 270 | DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 271 | break; |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 272 | case OUT_PER: |
| 273 | erv = uper_encode(pduType, structure, write_out, stdout); |
| 274 | if(erv.encoded < 0) { |
| 275 | fprintf(stderr, "%s: Cannot convert into Unaligned PER\n", |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 276 | name); |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 277 | exit(EX_UNAVAILABLE); |
| 278 | } |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 279 | DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded); |
Lev Walkin | be3a0c5 | 2006-08-25 04:56:21 +0000 | [diff] [blame] | 280 | break; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 283 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if(file && file != stdin) |
| 287 | fclose(file); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 294 | static struct dynamic_buffer { |
| 295 | char *data; /* Pointer to the data bytes */ |
| 296 | size_t offset; /* Offset from the start */ |
| 297 | size_t length; /* Length of meaningful contents */ |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 298 | size_t unbit; /* Unused bits in the last byte */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 299 | size_t allocated; /* Allocated memory for data */ |
| 300 | int nreallocs; /* Number of data reallocations */ |
| 301 | off_t bytes_shifted; /* Number of bytes ever shifted */ |
| 302 | } DynamicBuffer; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 303 | |
| 304 | /* |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 305 | * Ensure that the buffer contains at least this amount of free space. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 306 | */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 307 | static void add_bytes_to_buffer(const void *data2add, size_t bySize) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 308 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 309 | DEBUG("add_bytes(%ld) { o=%ld l=%ld s=%ld }", |
| 310 | (long)bySize, |
| 311 | (long)DynamicBuffer.offset, |
| 312 | (long)DynamicBuffer.length, |
| 313 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 314 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 315 | if(DynamicBuffer.allocated |
| 316 | >= (DynamicBuffer.offset + DynamicBuffer.length + bySize)) { |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 317 | DEBUG("\tNo buffer reallocation is necessary"); |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 318 | } else if(bySize <= DynamicBuffer.offset) { |
| 319 | DEBUG("\tContents shifted by %ld", DynamicBuffer.offset); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 320 | |
| 321 | /* Shift the buffer contents */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 322 | memmove(DynamicBuffer.data, |
| 323 | DynamicBuffer.data + DynamicBuffer.offset, |
| 324 | DynamicBuffer.length); |
| 325 | DynamicBuffer.bytes_shifted += DynamicBuffer.offset; |
| 326 | DynamicBuffer.offset = 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 327 | } else { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 328 | size_t newsize = (DynamicBuffer.allocated << 2) + bySize; |
| 329 | void *p = MALLOC(newsize); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 330 | if(!p) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 331 | perror("malloc()"); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 332 | exit(EX_OSERR); |
| 333 | } |
Lev Walkin | e1baa4d | 2006-09-17 08:23:43 +0000 | [diff] [blame] | 334 | memcpy(p, |
| 335 | DynamicBuffer.data + DynamicBuffer.offset, |
| 336 | DynamicBuffer.length); |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 337 | FREEMEM(DynamicBuffer.data); |
| 338 | DynamicBuffer.data = (char *)p; |
| 339 | DynamicBuffer.offset = 0; |
| 340 | DynamicBuffer.allocated = newsize; |
| 341 | DynamicBuffer.nreallocs++; |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 342 | DEBUG("\tBuffer reallocated to %ld (%d time)", |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 343 | newsize, DynamicBuffer.nreallocs); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 344 | } |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 345 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 346 | memcpy(DynamicBuffer.data + DynamicBuffer.offset + DynamicBuffer.length, |
| 347 | data2add, bySize); |
| 348 | DynamicBuffer.length += bySize; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 351 | static void * |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 352 | data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 353 | static char *fbuf; |
| 354 | static ssize_t fbuf_size; |
| 355 | static asn_codec_ctx_t s_codec_ctx; |
| 356 | asn_codec_ctx_t *opt_codec_ctx = 0; |
| 357 | void *structure = 0; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 358 | asn_dec_rval_t rval; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 359 | size_t old_offset; |
| 360 | size_t new_offset; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 361 | size_t rd; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 362 | |
| 363 | if(!file) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 364 | fprintf(stderr, "%s: %s\n", name, strerror(errno)); |
| 365 | errno = EINVAL; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 368 | |
| 369 | if(opt_stack) { |
| 370 | s_codec_ctx.max_stack_size = opt_stack; |
| 371 | opt_codec_ctx = &s_codec_ctx; |
| 372 | } |
| 373 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 374 | DEBUG("Processing %s", name); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 375 | |
| 376 | /* prepare the file buffer */ |
| 377 | if(fbuf_size != suggested_bufsize) { |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 378 | fbuf = (char *)REALLOC(fbuf, suggested_bufsize); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 379 | if(!fbuf) { |
| 380 | perror("realloc()"); |
| 381 | exit(EX_OSERR); |
| 382 | } |
| 383 | fbuf_size = suggested_bufsize; |
| 384 | } |
| 385 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 386 | if(on_first_pdu) { |
| 387 | DynamicBuffer.offset = 0; |
| 388 | DynamicBuffer.length = 0; |
| 389 | DynamicBuffer.unbit = 0; |
| 390 | DynamicBuffer.allocated = 0; |
| 391 | DynamicBuffer.bytes_shifted = 0; |
| 392 | DynamicBuffer.nreallocs = 0; |
| 393 | } |
| 394 | |
| 395 | old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 396 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 397 | /* Pretend immediate EOF */ |
| 398 | rval.code = RC_WMORE; |
| 399 | rval.consumed = 0; |
| 400 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 401 | while((rd = fread(fbuf, 1, fbuf_size, file)) || !feof(file)) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 402 | int ecbits = 0; /* Extra consumed bits in case of PER */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 403 | char *i_bptr; |
| 404 | size_t i_size; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Copy the data over, or use the original buffer. |
| 408 | */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 409 | if(DynamicBuffer.allocated) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 410 | /* Append the new data into the intermediate buffer */ |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 411 | add_bytes_to_buffer(fbuf, rd); |
| 412 | i_bptr = DynamicBuffer.data + DynamicBuffer.offset; |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 413 | i_size = DynamicBuffer.length; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 414 | } else { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 415 | i_bptr = fbuf; |
| 416 | i_size = rd; |
| 417 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 418 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 419 | DEBUG("Decoding %ld bytes", (long)i_size); |
| 420 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 421 | switch(iform) { |
| 422 | case INP_BER: |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 423 | rval = ber_decode(opt_codec_ctx, pduType, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 424 | (void **)&structure, i_bptr, i_size); |
| 425 | break; |
| 426 | case INP_XER: |
| 427 | rval = xer_decode(opt_codec_ctx, pduType, |
| 428 | (void **)&structure, i_bptr, i_size); |
| 429 | break; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 430 | case INP_PER: |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 431 | rval = uper_decode(opt_codec_ctx, pduType, |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 432 | (void **)&structure, i_bptr, i_size, 0); |
| 433 | ecbits = rval.consumed % 8; /* Extra bits */ |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 434 | rval.consumed /= 8; /* Convert to value in bytes! */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 435 | break; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 436 | } |
Lev Walkin | 4a858bb | 2006-09-17 11:22:00 +0000 | [diff] [blame^] | 437 | DEBUG("decode(%ld) consumed %ld+%d (%ld), code %d", |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 438 | (long)DynamicBuffer.length, |
Lev Walkin | 4a858bb | 2006-09-17 11:22:00 +0000 | [diff] [blame^] | 439 | (long)rval.consumed, ecbits, (long)i_size, |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 440 | rval.code); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 441 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 442 | if(DynamicBuffer.allocated == 0) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 443 | /* |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 444 | * Flush remainder into the intermediate buffer. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 445 | */ |
| 446 | if(rval.code != RC_FAIL && rval.consumed < rd) { |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 447 | DEBUG("Saving %d bytes", rd - rval.consumed); |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 448 | add_bytes_to_buffer(fbuf + rval.consumed, |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 449 | rd - rval.consumed); |
| 450 | rval.consumed = 0; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Adjust position inside the source buffer. |
| 456 | */ |
| 457 | if(DynamicBuffer.allocated) { |
| 458 | DynamicBuffer.offset += rval.consumed; |
| 459 | DynamicBuffer.length -= rval.consumed; |
| 460 | } else { |
| 461 | DynamicBuffer.bytes_shifted += rval.consumed; |
| 462 | } |
| 463 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 464 | switch(rval.code) { |
| 465 | case RC_OK: |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 466 | DEBUG("RC_OK, finishing up with %ld", |
| 467 | (long)rval.consumed); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 468 | return structure; |
| 469 | case RC_WMORE: |
Lev Walkin | f9492a9 | 2006-09-13 04:14:17 +0000 | [diff] [blame] | 470 | DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld", |
| 471 | (long)rval.consumed, |
| 472 | (long)DynamicBuffer.offset, |
| 473 | (long)DynamicBuffer.length, |
| 474 | (long)DynamicBuffer.allocated); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 475 | continue; |
| 476 | case RC_FAIL: |
| 477 | break; |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 482 | /* Clean up partially decoded structure */ |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 483 | ASN_STRUCT_FREE(*pduType, structure); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 484 | |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 485 | new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset; |
| 486 | |
| 487 | /* |
| 488 | * Print a message and return failure only if not EOF, |
| 489 | * unless this is our first PDU (empty file). |
| 490 | */ |
| 491 | if(on_first_pdu || new_offset != old_offset) { |
| 492 | fprintf(stderr, "%s: " |
| 493 | "Decode failed past byte %ld: %s\n", |
| 494 | name, (long)new_offset, |
| 495 | (rval.code == RC_WMORE) |
| 496 | ? "Unexpected end of input" |
| 497 | : "Input processing error"); |
| 498 | errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG; |
| 499 | } else { |
| 500 | /* Got EOF after a few successful PDU */ |
| 501 | errno = 0; |
| 502 | } |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Lev Walkin | 419f675 | 2006-09-13 04:02:00 +0000 | [diff] [blame] | 507 | /* Dump the buffer out to the specified FILE */ |
| 508 | static int write_out(const void *buffer, size_t size, void *key) { |
| 509 | FILE *fp = (FILE *)key; |
| 510 | return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1; |
| 511 | } |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 512 | |
| 513 | static int argument_is_stdin(char *av[], int idx) { |
| 514 | if(strcmp(av[idx], "-")) { |
| 515 | return 0; /* Certainly not <stdin> */ |
| 516 | } else { |
| 517 | /* This might be <stdin>, unless `./program -- -` */ |
| 518 | if(strcmp(av[-1], "--")) |
| 519 | return 1; |
| 520 | else |
| 521 | return 0; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static FILE *argument_to_file(char *av[], int idx) { |
| 526 | return argument_is_stdin(av, idx) |
| 527 | ? stdin |
| 528 | : fopen(av[idx], "r"); |
| 529 | } |
| 530 | |
| 531 | static char *argument_to_name(char *av[], int idx) { |
| 532 | return argument_is_stdin(av, idx) |
| 533 | ? "standard input" |
| 534 | : av[idx]; |
| 535 | } |