Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 1 | /* |
Lev Walkin | 1ce7207 | 2005-08-16 03:19:59 +0000 | [diff] [blame] | 2 | * Generic BER decoder template for any defined ASN.1 type. |
| 3 | * Copyright (c) 2005 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 4 | * |
Lev Walkin | c490ff8 | 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: |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 6 | * |
Lev Walkin | c490ff8 | 2005-03-10 11:59:44 +0000 | [diff] [blame] | 7 | * cc -Dasn_DEF=asn_DEF_MyCustomType -o myDecoder.o -c asn-decoder-template.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 | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 22 | |
| 23 | extern asn_TYPE_descriptor_t asn_DEF; /* ASN.1 type to be decoded */ |
| 24 | |
| 25 | /* |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 26 | * Open file and parse its contens. |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 27 | */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 28 | static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU, |
| 29 | const char *fname, ssize_t suggested_bufsize); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 30 | |
| 31 | int opt_debug; /* -d */ |
| 32 | static int opt_check; /* -c */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 33 | static int opt_stack; /* -s */ |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 34 | static enum output_method { |
| 35 | OUT_NONE, /* No pretty-printing */ |
| 36 | OUT_PRINT, /* -p flag */ |
Lev Walkin | fdb2592 | 2005-07-21 09:32:49 +0000 | [diff] [blame] | 37 | OUT_XML /* -x flag */ |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 38 | } opt_ometh; /* -p or -x */ |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 39 | |
| 40 | #define DEBUG(fmt, args...) do { \ |
| 41 | if(!opt_debug) break; \ |
| 42 | fprintf(stderr, fmt, ##args); \ |
| 43 | fprintf(stderr, "\n"); \ |
| 44 | } while(0) |
| 45 | |
| 46 | int |
| 47 | main(int ac, char **av) { |
| 48 | ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 49 | asn_TYPE_descriptor_t *pduType = &asn_DEF; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 50 | int number_of_iterations = 1; |
| 51 | int num; |
| 52 | int ch; |
| 53 | |
| 54 | /* |
| 55 | * Pocess the command-line argments. |
| 56 | */ |
| 57 | while((ch = getopt(ac, av, "b:cdn:hps:x")) != -1) |
| 58 | switch(ch) { |
| 59 | case 'b': |
| 60 | suggested_bufsize = atoi(optarg); |
| 61 | if(suggested_bufsize < 1 |
| 62 | || suggested_bufsize > 16 * 1024 * 1024) { |
| 63 | fprintf(stderr, |
| 64 | "-b %s: Improper buffer size (1..16M)\n", |
| 65 | optarg); |
| 66 | exit(EX_UNAVAILABLE); |
| 67 | } |
| 68 | break; |
| 69 | case 'c': |
| 70 | opt_check = 1; |
| 71 | break; |
| 72 | case 'd': |
| 73 | opt_debug++; /* Double -dd means ASN.1 debug */ |
| 74 | break; |
| 75 | case 'n': |
| 76 | number_of_iterations = atoi(optarg); |
| 77 | if(number_of_iterations < 1) { |
| 78 | fprintf(stderr, |
| 79 | "-n %s: Improper iterations count\n", optarg); |
| 80 | exit(EX_UNAVAILABLE); |
| 81 | } |
| 82 | break; |
| 83 | case 'p': |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 84 | opt_ometh = OUT_PRINT; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 85 | break; |
| 86 | case 's': |
| 87 | opt_stack = atoi(optarg); |
| 88 | if(opt_stack <= 0) { |
| 89 | fprintf(stderr, |
| 90 | "-s %s: Value greater than 0 expected\n", |
| 91 | optarg); |
| 92 | exit(EX_UNAVAILABLE); |
| 93 | } |
| 94 | break; |
| 95 | case 'x': |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 96 | opt_ometh = OUT_XML; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 97 | break; |
| 98 | case 'h': |
| 99 | default: |
| 100 | fprintf(stderr, |
| 101 | "Usage: %s [options] <data.ber> ...\n" |
| 102 | "Where options are:\n" |
| 103 | " -b <size> Set the i/o buffer size (default is %ld)\n" |
| 104 | " -c Check ASN.1 constraints after decoding\n" |
| 105 | " -d Enable debugging (-dd is even better)\n" |
| 106 | " -n <num> Process files <num> times\n" |
| 107 | " -s <size> Set the stack usage limit\n" |
| 108 | " -p Print out the decoded contents\n" |
Lev Walkin | 5ba113b | 2005-04-08 19:28:18 +0000 | [diff] [blame] | 109 | " -x Print out as XML" |
| 110 | #ifdef ASN_DECODER_DEFAULT_OUTPUT_XML |
| 111 | " (default)" |
| 112 | #endif |
| 113 | "\n" |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 114 | , av[0], (long)suggested_bufsize); |
| 115 | exit(EX_USAGE); |
| 116 | } |
| 117 | |
| 118 | ac -= optind; |
| 119 | av += optind; |
| 120 | |
| 121 | if(ac < 1) { |
| 122 | fprintf(stderr, "Error: missing filename\n"); |
| 123 | exit(EX_USAGE); |
| 124 | } |
| 125 | |
Lev Walkin | 5ba113b | 2005-04-08 19:28:18 +0000 | [diff] [blame] | 126 | #ifdef ASN_DECODER_DEFAULT_OUTPUT_XML |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 127 | if(opt_ometh == OUT_NONE) opt_ometh = OUT_XML; |
Lev Walkin | 5ba113b | 2005-04-08 19:28:18 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 130 | setvbuf(stdout, 0, _IOLBF, 0); |
| 131 | |
| 132 | for(num = 0; num < number_of_iterations; num++) { |
| 133 | int ac_i; |
| 134 | /* |
| 135 | * Process all files in turn. |
| 136 | */ |
| 137 | for(ac_i = 0; ac_i < ac; ac_i++) { |
| 138 | char *fname = av[ac_i]; |
| 139 | void *structure; |
| 140 | |
| 141 | /* |
| 142 | * Decode the encoded structure from file. |
| 143 | */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 144 | structure = data_decode_from_file(pduType, |
| 145 | fname, suggested_bufsize); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 146 | if(!structure) { |
| 147 | /* Error message is already printed */ |
| 148 | exit(EX_DATAERR); |
| 149 | } |
| 150 | |
Lev Walkin | 7bee128 | 2005-04-28 02:59:51 +0000 | [diff] [blame] | 151 | switch(opt_ometh) { |
| 152 | case OUT_NONE: |
| 153 | fprintf(stderr, "%s: decoded successfully\n", fname); |
| 154 | break; |
| 155 | case OUT_PRINT: /* -p */ |
| 156 | asn_fprint(stdout, pduType, structure); |
| 157 | break; |
| 158 | case OUT_XML: /* -x */ |
| 159 | if(xer_fprint(stdout, pduType, structure)) { |
| 160 | fprintf(stderr, "%s: Cannot convert into XML\n", |
| 161 | fname); |
| 162 | exit(EX_UNAVAILABLE); |
| 163 | } |
| 164 | break; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* Check ASN.1 constraints */ |
| 168 | if(opt_check) { |
| 169 | char errbuf[128]; |
| 170 | size_t errlen = sizeof(errbuf); |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 171 | if(asn_check_constraints(pduType, structure, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 172 | errbuf, &errlen)) { |
| 173 | fprintf(stderr, "%s: ASN.1 constraint " |
| 174 | "check failed: %s\n", fname, errbuf); |
| 175 | exit(EX_DATAERR); |
| 176 | } |
| 177 | } |
| 178 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 179 | pduType->free_struct(pduType, structure, 0); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static char *buffer; |
| 188 | static size_t buf_offset; /* Offset from the start */ |
| 189 | static size_t buf_len; /* Length of meaningful contents */ |
| 190 | static size_t buf_size; /* Allocated memory */ |
| 191 | static off_t buf_shifted; /* Number of bytes ever shifted */ |
| 192 | |
| 193 | #define bufptr (buffer + buf_offset) |
| 194 | #define bufend (buffer + buf_offset + buf_len) |
| 195 | |
| 196 | /* |
| 197 | * Ensure that the buffer contains at least this amoount of free space. |
| 198 | */ |
| 199 | static void buf_extend(size_t bySize) { |
| 200 | |
| 201 | DEBUG("buf_extend(%ld) { o=%ld l=%ld s=%ld }", |
| 202 | (long)bySize, (long)buf_offset, (long)buf_len, (long)buf_size); |
| 203 | |
| 204 | if(buf_size >= (buf_offset + buf_len + bySize)) { |
| 205 | return; /* Nothing to do */ |
| 206 | } else if(bySize <= buf_offset) { |
| 207 | DEBUG("\tContents shifted by %ld", (long)buf_offset); |
| 208 | |
| 209 | /* Shift the buffer contents */ |
| 210 | memmove(buffer, buffer + buf_offset, buf_len); |
| 211 | buf_shifted += buf_offset; |
| 212 | buf_offset = 0; |
| 213 | } else { |
| 214 | size_t newsize = (buf_size << 2) + bySize; |
| 215 | void *p = realloc(buffer, newsize); |
| 216 | if(p) { |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 217 | buffer = (char *)p; |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 218 | buf_size = newsize; |
| 219 | |
| 220 | DEBUG("\tBuffer reallocated to %ld", (long)newsize); |
| 221 | } else { |
| 222 | perror("realloc()"); |
| 223 | exit(EX_OSERR); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 228 | static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) { |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 229 | static char *fbuf; |
| 230 | static ssize_t fbuf_size; |
| 231 | static asn_codec_ctx_t s_codec_ctx; |
| 232 | asn_codec_ctx_t *opt_codec_ctx = 0; |
| 233 | void *structure = 0; |
| 234 | size_t rd; |
| 235 | FILE *fp; |
| 236 | |
| 237 | if(opt_stack) { |
| 238 | s_codec_ctx.max_stack_size = opt_stack; |
| 239 | opt_codec_ctx = &s_codec_ctx; |
| 240 | } |
| 241 | |
| 242 | DEBUG("Processing file %s", fname); |
| 243 | |
| 244 | fp = fopen(fname, "r"); |
| 245 | |
| 246 | if(!fp) { |
| 247 | fprintf(stderr, "%s: %s\n", fname, strerror(errno)); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* prepare the file buffer */ |
| 252 | if(fbuf_size != suggested_bufsize) { |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 253 | fbuf = (char *)realloc(fbuf, suggested_bufsize); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 254 | if(!fbuf) { |
| 255 | perror("realloc()"); |
| 256 | exit(EX_OSERR); |
| 257 | } |
| 258 | fbuf_size = suggested_bufsize; |
| 259 | } |
| 260 | |
| 261 | buf_shifted = 0; |
| 262 | buf_offset = 0; |
| 263 | buf_len = 0; |
| 264 | |
| 265 | while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) { |
| 266 | asn_dec_rval_t rval; |
| 267 | int using_local_buf; |
| 268 | |
| 269 | /* |
| 270 | * Copy the data over, or use the original buffer. |
| 271 | */ |
| 272 | if(buf_len) { |
| 273 | /* Append the new data into the intermediate buffer */ |
| 274 | buf_extend(rd); |
| 275 | memcpy(bufend, fbuf, rd); |
| 276 | buf_len += rd; |
| 277 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 278 | rval = ber_decode(opt_codec_ctx, pduType, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 279 | (void **)&structure, bufptr, buf_len); |
| 280 | DEBUG("ber_decode(%ld) consumed %ld, code %d", |
| 281 | (long)buf_len, (long)rval.consumed, rval.code); |
| 282 | |
| 283 | /* |
| 284 | * Adjust position inside the source buffer. |
| 285 | */ |
| 286 | assert(rval.consumed <= buf_len); |
| 287 | buf_offset += rval.consumed; |
| 288 | buf_len -= rval.consumed; |
| 289 | } else { |
| 290 | using_local_buf = 1; |
| 291 | |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 292 | /* Feed the chunk of data into a decoder routine */ |
| 293 | rval = ber_decode(opt_codec_ctx, pduType, |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 294 | (void **)&structure, fbuf, rd); |
| 295 | DEBUG("ber_decode(%ld) consumed %ld, code %d", |
| 296 | (long)rd, (long)rval.consumed, rval.code); |
| 297 | |
| 298 | /* |
| 299 | * Switch the remainder into the intermediate buffer. |
| 300 | */ |
| 301 | if(rval.code != RC_FAIL && rval.consumed < rd) { |
| 302 | buf_extend(rd - rval.consumed); |
| 303 | memcpy(bufend, |
| 304 | fbuf + rval.consumed, |
| 305 | rd - rval.consumed); |
| 306 | buf_len = rd - rval.consumed; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | switch(rval.code) { |
| 311 | case RC_OK: |
| 312 | DEBUG("RC_OK, finishing up"); |
| 313 | fclose(fp); |
| 314 | return structure; |
| 315 | case RC_WMORE: |
| 316 | DEBUG("RC_WMORE, continuing..."); |
| 317 | continue; |
| 318 | case RC_FAIL: |
| 319 | break; |
| 320 | } |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | fclose(fp); |
| 325 | |
| 326 | /* Clean up partially decoded structure */ |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 327 | pduType->free_struct(pduType, structure, 0); |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 328 | |
| 329 | fprintf(stderr, "%s: " |
Lev Walkin | 0094956 | 2005-03-10 13:39:03 +0000 | [diff] [blame] | 330 | "Decode failed past %lld byte\n", |
Lev Walkin | 22b5455 | 2004-10-28 13:22:54 +0000 | [diff] [blame] | 331 | fname, (long long)(buf_shifted + buf_offset)); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |