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