blob: 3a76b764faa9a8e1219a8494c2dffc4ca69b20b8 [file] [log] [blame]
vlm0e329f22004-10-28 13:22:54 +00001/*
vlmb51ebb22006-08-25 02:35:08 +00002 * Generic converter template for a selected ASN.1 type.
3 * Copyright (c) 2005, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlm0e329f22004-10-28 13:22:54 +00004 *
vlmb51ebb22006-08-25 02:35:08 +00005 * To compile with your own ASN.1 type, please redefine the PDU as shown:
vlm0e329f22004-10-28 13:22:54 +00006 *
vlmb51ebb22006-08-25 02:35:08 +00007 * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.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
vlmb51ebb22006-08-25 02:35:08 +000024/* 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
29extern asn_TYPE_descriptor_t PDU_Type; /* ASN.1 type to be decoded */
vlm337167e2005-11-26 11:25:14 +000030#ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
31extern asn_TYPE_descriptor_t *asn_pdu_collection[];
32#endif
vlm0e329f22004-10-28 13:22:54 +000033
34/*
vlme3470e72005-03-10 13:39:03 +000035 * Open file and parse its contens.
vlm0e329f22004-10-28 13:22:54 +000036 */
vlme3470e72005-03-10 13:39:03 +000037static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU,
38 const char *fname, ssize_t suggested_bufsize);
vlm6d44a542005-11-08 03:06:16 +000039static int write_out(const void *buffer, size_t size, void *key);
vlm0e329f22004-10-28 13:22:54 +000040
41 int opt_debug; /* -d */
42static int opt_check; /* -c */
vlm0e329f22004-10-28 13:22:54 +000043static int opt_stack; /* -s */
vlm6d44a542005-11-08 03:06:16 +000044
45/* Input data format selector */
46static enum input_format {
47 INP_BER, /* -iber: BER input */
vlm337167e2005-11-26 11:25:14 +000048 INP_XER, /* -ixer: XER input */
49 INP_PER /* -iper: Unaligned PER input */
vlm6d44a542005-11-08 03:06:16 +000050} iform; /* -i<format> */
51
52/* Output data format selector */
53static enum output_format {
54 OUT_XER, /* -oxer: XER (XML) output */
55 OUT_DER, /* -oder: DER output */
56 OUT_TEXT, /* -otext: semi-structured text */
57 OUT_NULL /* -onull: No pretty-printing */
58} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000059
vlm4d2ca122005-12-07 05:46:03 +000060/* Debug output function */
61static inline void
62DEBUG(const char *fmt, ...) {
63 va_list ap;
64 if(!opt_debug) return;
65 fprintf(stderr, "AD: ");
66 va_start(ap, fmt);
67 vfprintf(stderr, fmt, ap);
68 va_end(ap);
69 fprintf(stderr, "\n");
70}
vlm0e329f22004-10-28 13:22:54 +000071
72int
73main(int ac, char **av) {
vlmb51ebb22006-08-25 02:35:08 +000074 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
vlm0e329f22004-10-28 13:22:54 +000075 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
76 int number_of_iterations = 1;
77 int num;
78 int ch;
79
vlm337167e2005-11-26 11:25:14 +000080 /* Figure out if Unaligned PER needs to be default */
81 if(pduType->uper_decoder)
82 iform = INP_PER;
83
vlm0e329f22004-10-28 13:22:54 +000084 /*
85 * Pocess the command-line argments.
86 */
vlm337167e2005-11-26 11:25:14 +000087 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
vlm0e329f22004-10-28 13:22:54 +000088 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +000089 case 'i':
90 if(optarg[0] == 'b') { iform = INP_BER; break; }
91 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +000092 if(pduType->uper_decoder
93 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6d44a542005-11-08 03:06:16 +000094 fprintf(stderr, "-i<format>: '%s': improper format selector",
95 optarg);
96 exit(EX_UNAVAILABLE);
97 case 'o':
98 if(optarg[0] == 'd') { oform = OUT_DER; break; }
99 if(optarg[0] == 'x') { oform = OUT_XER; break; }
100 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
101 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
102 fprintf(stderr, "-o<format>: '%s': improper format selector",
103 optarg);
104 exit(EX_UNAVAILABLE);
vlm337167e2005-11-26 11:25:14 +0000105 case 'p':
106#ifdef ASN_PDU_COLLECTION
107 {
108 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
109 if(optarg[0] < 'A' || optarg[0] > 'Z') {
110 fprintf(stderr, "Available PDU types:\n");
111 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
112 exit(0);
113 }
114 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
115 if(*pdu) { pduType = *pdu; break; }
116 }
117#endif /* ASN_PDU_COLLECTION */
118 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
119 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +0000120 case 'b':
121 suggested_bufsize = atoi(optarg);
122 if(suggested_bufsize < 1
123 || suggested_bufsize > 16 * 1024 * 1024) {
124 fprintf(stderr,
125 "-b %s: Improper buffer size (1..16M)\n",
126 optarg);
127 exit(EX_UNAVAILABLE);
128 }
129 break;
130 case 'c':
131 opt_check = 1;
132 break;
133 case 'd':
134 opt_debug++; /* Double -dd means ASN.1 debug */
135 break;
136 case 'n':
137 number_of_iterations = atoi(optarg);
138 if(number_of_iterations < 1) {
139 fprintf(stderr,
140 "-n %s: Improper iterations count\n", optarg);
141 exit(EX_UNAVAILABLE);
142 }
143 break;
vlm0e329f22004-10-28 13:22:54 +0000144 case 's':
145 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000146 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000147 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000148 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000149 optarg);
150 exit(EX_UNAVAILABLE);
151 }
152 break;
vlm0e329f22004-10-28 13:22:54 +0000153 case 'h':
154 default:
vlm337167e2005-11-26 11:25:14 +0000155 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
156 fprintf(stderr, "Where options are:\n");
157 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000158 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000159 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000160 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000161 " -iber Input is in BER (Basic Encoding Rules)%s\n",
162 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000163 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000164 " -ixer Input is in XER (XML Encoding Rules)\n"
165 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000166 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000167 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000168 " -onull Verify (decode) input, but do not output\n");
169#ifdef ASN_PDU_COLLECTION
170 fprintf(stderr,
171 " -p <PDU> Specify PDU type to decode\n"
172 " -p list List available PDUs\n");
173#endif /* ASN_PDU_COLLECTION */
174 fprintf(stderr,
vlm0e329f22004-10-28 13:22:54 +0000175 " -b <size> Set the i/o buffer size (default is %ld)\n"
176 " -c Check ASN.1 constraints after decoding\n"
177 " -d Enable debugging (-dd is even better)\n"
178 " -n <num> Process files <num> times\n"
vlm4d2ca122005-12-07 05:46:03 +0000179 " -s <size> Set the stack usage limit (default is %d)\n"
180 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000181 exit(EX_USAGE);
182 }
183
184 ac -= optind;
185 av += optind;
186
187 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000188 fprintf(stderr, "%s: No input files specified. "
189 "Try '-h' for more information\n",
190 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000191 exit(EX_USAGE);
192 }
193
194 setvbuf(stdout, 0, _IOLBF, 0);
195
196 for(num = 0; num < number_of_iterations; num++) {
197 int ac_i;
198 /*
199 * Process all files in turn.
200 */
201 for(ac_i = 0; ac_i < ac; ac_i++) {
202 char *fname = av[ac_i];
203 void *structure;
vlm6d44a542005-11-08 03:06:16 +0000204 asn_enc_rval_t erv;
vlm0e329f22004-10-28 13:22:54 +0000205
206 /*
207 * Decode the encoded structure from file.
208 */
vlme3470e72005-03-10 13:39:03 +0000209 structure = data_decode_from_file(pduType,
210 fname, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000211 if(!structure) {
212 /* Error message is already printed */
213 exit(EX_DATAERR);
214 }
215
vlm0e329f22004-10-28 13:22:54 +0000216 /* Check ASN.1 constraints */
217 if(opt_check) {
218 char errbuf[128];
219 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000220 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000221 errbuf, &errlen)) {
222 fprintf(stderr, "%s: ASN.1 constraint "
223 "check failed: %s\n", fname, errbuf);
224 exit(EX_DATAERR);
225 }
226 }
227
vlm6d44a542005-11-08 03:06:16 +0000228 switch(oform) {
229 case OUT_NULL:
230 fprintf(stderr, "%s: decoded successfully\n", fname);
231 break;
232 case OUT_TEXT: /* -otext */
233 asn_fprint(stdout, pduType, structure);
234 break;
235 case OUT_XER: /* -oxer */
236 if(xer_fprint(stdout, pduType, structure)) {
237 fprintf(stderr, "%s: Cannot convert into XML\n",
238 fname);
239 exit(EX_UNAVAILABLE);
240 }
241 break;
242 case OUT_DER:
243 erv = der_encode(pduType, structure, write_out, stdout);
244 if(erv.encoded < 0) {
245 fprintf(stderr, "%s: Cannot convert into DER\n",
246 fname);
247 exit(EX_UNAVAILABLE);
248 }
249 break;
250 }
251
vlmbe78c802006-03-17 02:11:12 +0000252 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000253 }
254 }
255
256 return 0;
257}
258
vlm6d44a542005-11-08 03:06:16 +0000259/* Dump the buffer */
260static int write_out(const void *buffer, size_t size, void *key) {
vlm337167e2005-11-26 11:25:14 +0000261 FILE *fp = (FILE *)key;
262 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
vlm6d44a542005-11-08 03:06:16 +0000263}
vlm0e329f22004-10-28 13:22:54 +0000264
vlm4d2ca122005-12-07 05:46:03 +0000265static char *buffer;
vlm0e329f22004-10-28 13:22:54 +0000266static size_t buf_len; /* Length of meaningful contents */
vlm4d2ca122005-12-07 05:46:03 +0000267static size_t buf_size; /* Allocated memory */
268static size_t buf_offset; /* Offset from the start */
269static off_t buf_shifted; /* Number of bytes ever shifted */
270static int buf_nreallocs; /* Number of reallocations */
vlm0e329f22004-10-28 13:22:54 +0000271
272#define bufptr (buffer + buf_offset)
273#define bufend (buffer + buf_offset + buf_len)
274
275/*
vlm6d44a542005-11-08 03:06:16 +0000276 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000277 */
vlm4d2ca122005-12-07 05:46:03 +0000278static void buf_extend(const void *data2add, size_t bySize) {
vlm0e329f22004-10-28 13:22:54 +0000279
280 DEBUG("buf_extend(%ld) { o=%ld l=%ld s=%ld }",
281 (long)bySize, (long)buf_offset, (long)buf_len, (long)buf_size);
282
283 if(buf_size >= (buf_offset + buf_len + bySize)) {
vlm4d2ca122005-12-07 05:46:03 +0000284 /* Nothing to do */
vlm0e329f22004-10-28 13:22:54 +0000285 } else if(bySize <= buf_offset) {
286 DEBUG("\tContents shifted by %ld", (long)buf_offset);
287
288 /* Shift the buffer contents */
289 memmove(buffer, buffer + buf_offset, buf_len);
290 buf_shifted += buf_offset;
291 buf_offset = 0;
292 } else {
293 size_t newsize = (buf_size << 2) + bySize;
vlm4d2ca122005-12-07 05:46:03 +0000294 void *p = malloc(newsize);
295 if(!p) {
vlm0e329f22004-10-28 13:22:54 +0000296 perror("realloc()");
297 exit(EX_OSERR);
298 }
vlm4d2ca122005-12-07 05:46:03 +0000299 memcpy(p, buffer, buf_len);
300 free(buffer);
301 buffer = (char *)p;
302 buf_size = newsize;
303 buf_offset = 0;
304 DEBUG("\tBuffer reallocated to %ld, %d time",
305 (long)newsize, ++buf_nreallocs);
vlm0e329f22004-10-28 13:22:54 +0000306 }
vlm4d2ca122005-12-07 05:46:03 +0000307
308 memcpy(buffer + buf_offset + buf_len, data2add, bySize);
309 buf_len += bySize;
vlm0e329f22004-10-28 13:22:54 +0000310}
311
vlme3470e72005-03-10 13:39:03 +0000312static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) {
vlm0e329f22004-10-28 13:22:54 +0000313 static char *fbuf;
314 static ssize_t fbuf_size;
315 static asn_codec_ctx_t s_codec_ctx;
316 asn_codec_ctx_t *opt_codec_ctx = 0;
317 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000318 asn_dec_rval_t rval;
vlm0e329f22004-10-28 13:22:54 +0000319 size_t rd;
320 FILE *fp;
321
322 if(opt_stack) {
323 s_codec_ctx.max_stack_size = opt_stack;
324 opt_codec_ctx = &s_codec_ctx;
325 }
326
vlm6d44a542005-11-08 03:06:16 +0000327 if(strcmp(fname, "-")) {
328 DEBUG("Processing file %s", fname);
329 fp = fopen(fname, "r");
330 } else {
vlm4d2ca122005-12-07 05:46:03 +0000331 DEBUG("Processing %s", "standard input");
vlm6d44a542005-11-08 03:06:16 +0000332 fname = "stdin";
333 fp = stdin;
334 }
vlm0e329f22004-10-28 13:22:54 +0000335
336 if(!fp) {
337 fprintf(stderr, "%s: %s\n", fname, strerror(errno));
338 return 0;
339 }
340
341 /* prepare the file buffer */
342 if(fbuf_size != suggested_bufsize) {
vlmd3c80792004-12-15 23:23:53 +0000343 fbuf = (char *)realloc(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000344 if(!fbuf) {
345 perror("realloc()");
346 exit(EX_OSERR);
347 }
348 fbuf_size = suggested_bufsize;
349 }
350
vlm4d2ca122005-12-07 05:46:03 +0000351 buf_nreallocs = 0;
vlm0e329f22004-10-28 13:22:54 +0000352 buf_shifted = 0;
353 buf_offset = 0;
vlm4d2ca122005-12-07 05:46:03 +0000354 buf_size = 0;
vlm0e329f22004-10-28 13:22:54 +0000355 buf_len = 0;
356
vlm6d44a542005-11-08 03:06:16 +0000357 /* Pretend immediate EOF */
358 rval.code = RC_WMORE;
359 rval.consumed = 0;
360
vlm0e329f22004-10-28 13:22:54 +0000361 while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) {
vlm6d44a542005-11-08 03:06:16 +0000362 char *i_bptr;
363 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000364
365 /*
366 * Copy the data over, or use the original buffer.
367 */
vlm4d2ca122005-12-07 05:46:03 +0000368 if(buf_size) {
vlm0e329f22004-10-28 13:22:54 +0000369 /* Append the new data into the intermediate buffer */
vlm4d2ca122005-12-07 05:46:03 +0000370 buf_extend(fbuf, rd);
vlm6d44a542005-11-08 03:06:16 +0000371 i_bptr = bufptr;
372 i_size = buf_len;
vlm0e329f22004-10-28 13:22:54 +0000373 } else {
vlm6d44a542005-11-08 03:06:16 +0000374 i_bptr = fbuf;
375 i_size = rd;
376 }
vlm0e329f22004-10-28 13:22:54 +0000377
vlm6d44a542005-11-08 03:06:16 +0000378 switch(iform) {
379 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000380 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000381 (void **)&structure, i_bptr, i_size);
382 break;
383 case INP_XER:
384 rval = xer_decode(opt_codec_ctx, pduType,
385 (void **)&structure, i_bptr, i_size);
386 break;
vlm337167e2005-11-26 11:25:14 +0000387 case INP_PER:
vlm4d2ca122005-12-07 05:46:03 +0000388 rval = uper_decode(opt_codec_ctx, pduType,
389 (void **)&structure, i_bptr, i_size);
vlm337167e2005-11-26 11:25:14 +0000390 break;
vlm6d44a542005-11-08 03:06:16 +0000391 }
vlm4d2ca122005-12-07 05:46:03 +0000392 DEBUG("decode(%ld) consumed %ld (%ld), code %d",
393 (long)buf_len, (long)rval.consumed, (long)i_size,
394 rval.code);
vlm0e329f22004-10-28 13:22:54 +0000395
vlm4d2ca122005-12-07 05:46:03 +0000396 if(buf_size == 0) {
vlm0e329f22004-10-28 13:22:54 +0000397 /*
398 * Switch the remainder into the intermediate buffer.
399 */
400 if(rval.code != RC_FAIL && rval.consumed < rd) {
vlm4d2ca122005-12-07 05:46:03 +0000401 buf_extend(fbuf + rval.consumed,
402 rd - rval.consumed);
403 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000404 }
405 }
406
407 switch(rval.code) {
408 case RC_OK:
vlm4d2ca122005-12-07 05:46:03 +0000409 DEBUG("RC_OK, finishing up with %ld",
410 (long)rval.consumed);
vlm6d44a542005-11-08 03:06:16 +0000411 if(fp != stdin) fclose(fp);
vlm0e329f22004-10-28 13:22:54 +0000412 return structure;
413 case RC_WMORE:
vlm4d2ca122005-12-07 05:46:03 +0000414 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
415 (long)rval.consumed, (long)buf_offset,
416 (long)buf_len, (long)buf_size);
vlm6d44a542005-11-08 03:06:16 +0000417 /*
418 * Adjust position inside the source buffer.
419 */
420 buf_offset += rval.consumed;
421 buf_len -= rval.consumed;
422 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000423 continue;
424 case RC_FAIL:
425 break;
426 }
427 break;
428 }
429
430 fclose(fp);
431
432 /* Clean up partially decoded structure */
vlmbe78c802006-03-17 02:11:12 +0000433 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000434
435 fprintf(stderr, "%s: "
vlm6d44a542005-11-08 03:06:16 +0000436 "Decode failed past byte %ld: %s\n",
437 fname, (long)(buf_shifted + buf_offset + rval.consumed),
438 (rval.code == RC_WMORE)
439 ? "Unexpected end of input"
440 : "Input processing error");
vlm0e329f22004-10-28 13:22:54 +0000441
442 return 0;
443}
444