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