blob: 3225b6674cbbc02ac221e41ff925d196d09f920b [file] [log] [blame]
vlm0e329f22004-10-28 13:22:54 +00001/*
vlm6d44a542005-11-08 03:06:16 +00002 * Generic decoder template for a selected ASN.1 type.
vlmcb14b502005-08-16 03:19:59 +00003 * Copyright (c) 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlm0e329f22004-10-28 13:22:54 +00004 *
vlme641d392005-03-10 11:59:44 +00005 * To compile with your own ASN.1 type, please redefine the asn_DEF as shown:
vlm0e329f22004-10-28 13:22:54 +00006 *
vlme641d392005-03-10 11:59:44 +00007 * cc -Dasn_DEF=asn_DEF_MyCustomType -o myDecoder.o -c asn-decoder-template.c
vlm0e329f22004-10-28 13:22:54 +00008 */
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12#include <stdio.h>
13#include <sys/types.h>
vlm066dc102005-08-22 12:23:54 +000014#include <stdlib.h> /* for atoi(3) */
15#include <unistd.h> /* for getopt(3) */
vlm0e329f22004-10-28 13:22:54 +000016#include <string.h> /* for strerror(3) */
vlm0e329f22004-10-28 13:22:54 +000017#include <sysexits.h> /* for EX_* exit codes */
vlm5ec2fe12005-03-29 17:21:14 +000018#include <assert.h> /* for assert(3) */
19#include <errno.h> /* for errno */
vlm0e329f22004-10-28 13:22:54 +000020
vlme3470e72005-03-10 13:39:03 +000021#include <asn_application.h>
vlm4d2ca122005-12-07 05:46:03 +000022#include <asn_internal.h> /* for _ASN_DEFAULT_STACK_MAX */
vlm0e329f22004-10-28 13:22:54 +000023
24extern asn_TYPE_descriptor_t asn_DEF; /* ASN.1 type to be decoded */
vlm337167e2005-11-26 11:25:14 +000025#ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
26extern asn_TYPE_descriptor_t *asn_pdu_collection[];
27#endif
vlm0e329f22004-10-28 13:22:54 +000028
29/*
vlme3470e72005-03-10 13:39:03 +000030 * Open file and parse its contens.
vlm0e329f22004-10-28 13:22:54 +000031 */
vlme3470e72005-03-10 13:39:03 +000032static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU,
33 const char *fname, ssize_t suggested_bufsize);
vlm6d44a542005-11-08 03:06:16 +000034static int write_out(const void *buffer, size_t size, void *key);
vlm0e329f22004-10-28 13:22:54 +000035
36 int opt_debug; /* -d */
37static int opt_check; /* -c */
vlm0e329f22004-10-28 13:22:54 +000038static int opt_stack; /* -s */
vlm6d44a542005-11-08 03:06:16 +000039
40/* Input data format selector */
41static enum input_format {
42 INP_BER, /* -iber: BER input */
vlm337167e2005-11-26 11:25:14 +000043 INP_XER, /* -ixer: XER input */
44 INP_PER /* -iper: Unaligned PER input */
vlm6d44a542005-11-08 03:06:16 +000045} iform; /* -i<format> */
46
47/* Output data format selector */
48static 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> */
vlm0e329f22004-10-28 13:22:54 +000054
vlm4d2ca122005-12-07 05:46:03 +000055/* Debug output function */
56static inline void
57DEBUG(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}
vlm0e329f22004-10-28 13:22:54 +000066
67int
68main(int ac, char **av) {
vlm337167e2005-11-26 11:25:14 +000069 static asn_TYPE_descriptor_t *pduType = &asn_DEF;
vlm0e329f22004-10-28 13:22:54 +000070 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
71 int number_of_iterations = 1;
72 int num;
73 int ch;
74
vlm337167e2005-11-26 11:25:14 +000075 /* Figure out if Unaligned PER needs to be default */
76 if(pduType->uper_decoder)
77 iform = INP_PER;
78
vlm0e329f22004-10-28 13:22:54 +000079 /*
80 * Pocess the command-line argments.
81 */
vlm337167e2005-11-26 11:25:14 +000082 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
vlm0e329f22004-10-28 13:22:54 +000083 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +000084 case 'i':
85 if(optarg[0] == 'b') { iform = INP_BER; break; }
86 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +000087 if(pduType->uper_decoder
88 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6d44a542005-11-08 03:06:16 +000089 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);
vlm337167e2005-11-26 11:25:14 +0000100 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);
vlm0e329f22004-10-28 13:22:54 +0000115 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;
vlm0e329f22004-10-28 13:22:54 +0000139 case 's':
140 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000141 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000142 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000143 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000144 optarg);
145 exit(EX_UNAVAILABLE);
146 }
147 break;
vlm0e329f22004-10-28 13:22:54 +0000148 case 'h':
149 default:
vlm337167e2005-11-26 11:25:14 +0000150 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
151 fprintf(stderr, "Where options are:\n");
152 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000153 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000154 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000155 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000156 " -iber Input is in BER (Basic Encoding Rules)%s\n",
157 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000158 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000159 " -ixer Input is in XER (XML Encoding Rules)\n"
160 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000161 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000162 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000163 " -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,
vlm0e329f22004-10-28 13:22:54 +0000170 " -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"
vlm4d2ca122005-12-07 05:46:03 +0000174 " -s <size> Set the stack usage limit (default is %d)\n"
175 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000176 exit(EX_USAGE);
177 }
178
179 ac -= optind;
180 av += optind;
181
182 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000183 fprintf(stderr, "%s: No input files specified. "
184 "Try '-h' for more information\n",
185 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000186 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;
vlm6d44a542005-11-08 03:06:16 +0000199 asn_enc_rval_t erv;
vlm0e329f22004-10-28 13:22:54 +0000200
201 /*
202 * Decode the encoded structure from file.
203 */
vlme3470e72005-03-10 13:39:03 +0000204 structure = data_decode_from_file(pduType,
205 fname, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000206 if(!structure) {
207 /* Error message is already printed */
208 exit(EX_DATAERR);
209 }
210
vlm0e329f22004-10-28 13:22:54 +0000211 /* Check ASN.1 constraints */
212 if(opt_check) {
213 char errbuf[128];
214 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000215 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000216 errbuf, &errlen)) {
217 fprintf(stderr, "%s: ASN.1 constraint "
218 "check failed: %s\n", fname, errbuf);
219 exit(EX_DATAERR);
220 }
221 }
222
vlm6d44a542005-11-08 03:06:16 +0000223 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
vlme3470e72005-03-10 13:39:03 +0000247 pduType->free_struct(pduType, structure, 0);
vlm0e329f22004-10-28 13:22:54 +0000248 }
249 }
250
251 return 0;
252}
253
vlm6d44a542005-11-08 03:06:16 +0000254/* Dump the buffer */
255static int write_out(const void *buffer, size_t size, void *key) {
vlm337167e2005-11-26 11:25:14 +0000256 FILE *fp = (FILE *)key;
257 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
vlm6d44a542005-11-08 03:06:16 +0000258}
vlm0e329f22004-10-28 13:22:54 +0000259
vlm4d2ca122005-12-07 05:46:03 +0000260static char *buffer;
vlm0e329f22004-10-28 13:22:54 +0000261static size_t buf_len; /* Length of meaningful contents */
vlm4d2ca122005-12-07 05:46:03 +0000262static size_t buf_size; /* Allocated memory */
263static size_t buf_offset; /* Offset from the start */
264static off_t buf_shifted; /* Number of bytes ever shifted */
265static int buf_nreallocs; /* Number of reallocations */
vlm0e329f22004-10-28 13:22:54 +0000266
267#define bufptr (buffer + buf_offset)
268#define bufend (buffer + buf_offset + buf_len)
269
270/*
vlm6d44a542005-11-08 03:06:16 +0000271 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000272 */
vlm4d2ca122005-12-07 05:46:03 +0000273static void buf_extend(const void *data2add, size_t bySize) {
vlm0e329f22004-10-28 13:22:54 +0000274
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)) {
vlm4d2ca122005-12-07 05:46:03 +0000279 /* Nothing to do */
vlm0e329f22004-10-28 13:22:54 +0000280 } 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;
vlm4d2ca122005-12-07 05:46:03 +0000289 void *p = malloc(newsize);
290 if(!p) {
vlm0e329f22004-10-28 13:22:54 +0000291 perror("realloc()");
292 exit(EX_OSERR);
293 }
vlm4d2ca122005-12-07 05:46:03 +0000294 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);
vlm0e329f22004-10-28 13:22:54 +0000301 }
vlm4d2ca122005-12-07 05:46:03 +0000302
303 memcpy(buffer + buf_offset + buf_len, data2add, bySize);
304 buf_len += bySize;
vlm0e329f22004-10-28 13:22:54 +0000305}
306
vlme3470e72005-03-10 13:39:03 +0000307static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) {
vlm0e329f22004-10-28 13:22:54 +0000308 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;
vlm6d44a542005-11-08 03:06:16 +0000313 asn_dec_rval_t rval;
vlm0e329f22004-10-28 13:22:54 +0000314 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
vlm6d44a542005-11-08 03:06:16 +0000322 if(strcmp(fname, "-")) {
323 DEBUG("Processing file %s", fname);
324 fp = fopen(fname, "r");
325 } else {
vlm4d2ca122005-12-07 05:46:03 +0000326 DEBUG("Processing %s", "standard input");
vlm6d44a542005-11-08 03:06:16 +0000327 fname = "stdin";
328 fp = stdin;
329 }
vlm0e329f22004-10-28 13:22:54 +0000330
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) {
vlmd3c80792004-12-15 23:23:53 +0000338 fbuf = (char *)realloc(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000339 if(!fbuf) {
340 perror("realloc()");
341 exit(EX_OSERR);
342 }
343 fbuf_size = suggested_bufsize;
344 }
345
vlm4d2ca122005-12-07 05:46:03 +0000346 buf_nreallocs = 0;
vlm0e329f22004-10-28 13:22:54 +0000347 buf_shifted = 0;
348 buf_offset = 0;
vlm4d2ca122005-12-07 05:46:03 +0000349 buf_size = 0;
vlm0e329f22004-10-28 13:22:54 +0000350 buf_len = 0;
351
vlm6d44a542005-11-08 03:06:16 +0000352 /* Pretend immediate EOF */
353 rval.code = RC_WMORE;
354 rval.consumed = 0;
355
vlm0e329f22004-10-28 13:22:54 +0000356 while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) {
vlm6d44a542005-11-08 03:06:16 +0000357 char *i_bptr;
358 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000359
360 /*
361 * Copy the data over, or use the original buffer.
362 */
vlm4d2ca122005-12-07 05:46:03 +0000363 if(buf_size) {
vlm0e329f22004-10-28 13:22:54 +0000364 /* Append the new data into the intermediate buffer */
vlm4d2ca122005-12-07 05:46:03 +0000365 buf_extend(fbuf, rd);
vlm6d44a542005-11-08 03:06:16 +0000366 i_bptr = bufptr;
367 i_size = buf_len;
vlm0e329f22004-10-28 13:22:54 +0000368 } else {
vlm6d44a542005-11-08 03:06:16 +0000369 i_bptr = fbuf;
370 i_size = rd;
371 }
vlm0e329f22004-10-28 13:22:54 +0000372
vlm6d44a542005-11-08 03:06:16 +0000373 switch(iform) {
374 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000375 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000376 (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;
vlm337167e2005-11-26 11:25:14 +0000382 case INP_PER:
vlm4d2ca122005-12-07 05:46:03 +0000383 rval = uper_decode(opt_codec_ctx, pduType,
384 (void **)&structure, i_bptr, i_size);
vlm337167e2005-11-26 11:25:14 +0000385 break;
vlm6d44a542005-11-08 03:06:16 +0000386 }
vlm4d2ca122005-12-07 05:46:03 +0000387 DEBUG("decode(%ld) consumed %ld (%ld), code %d",
388 (long)buf_len, (long)rval.consumed, (long)i_size,
389 rval.code);
vlm0e329f22004-10-28 13:22:54 +0000390
vlm4d2ca122005-12-07 05:46:03 +0000391 if(buf_size == 0) {
vlm0e329f22004-10-28 13:22:54 +0000392 /*
393 * Switch the remainder into the intermediate buffer.
394 */
395 if(rval.code != RC_FAIL && rval.consumed < rd) {
vlm4d2ca122005-12-07 05:46:03 +0000396 buf_extend(fbuf + rval.consumed,
397 rd - rval.consumed);
398 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000399 }
400 }
401
402 switch(rval.code) {
403 case RC_OK:
vlm4d2ca122005-12-07 05:46:03 +0000404 DEBUG("RC_OK, finishing up with %ld",
405 (long)rval.consumed);
vlm6d44a542005-11-08 03:06:16 +0000406 if(fp != stdin) fclose(fp);
vlm0e329f22004-10-28 13:22:54 +0000407 return structure;
408 case RC_WMORE:
vlm4d2ca122005-12-07 05:46:03 +0000409 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
410 (long)rval.consumed, (long)buf_offset,
411 (long)buf_len, (long)buf_size);
vlm6d44a542005-11-08 03:06:16 +0000412 /*
413 * Adjust position inside the source buffer.
414 */
415 buf_offset += rval.consumed;
416 buf_len -= rval.consumed;
417 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000418 continue;
419 case RC_FAIL:
420 break;
421 }
422 break;
423 }
424
425 fclose(fp);
426
427 /* Clean up partially decoded structure */
vlme3470e72005-03-10 13:39:03 +0000428 pduType->free_struct(pduType, structure, 0);
vlm0e329f22004-10-28 13:22:54 +0000429
430 fprintf(stderr, "%s: "
vlm6d44a542005-11-08 03:06:16 +0000431 "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");
vlm0e329f22004-10-28 13:22:54 +0000436
437 return 0;
438}
439