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