blob: 348c3731d2b53e9bf2b158821fdef3b1be6a0295 [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>
vlm0e329f22004-10-28 13:22:54 +000022
23extern asn_TYPE_descriptor_t asn_DEF; /* ASN.1 type to be decoded */
vlm6d44a542005-11-08 03:06:16 +000024static asn_TYPE_descriptor_t *pduType = &asn_DEF;
vlm0e329f22004-10-28 13:22:54 +000025
26/*
vlme3470e72005-03-10 13:39:03 +000027 * Open file and parse its contens.
vlm0e329f22004-10-28 13:22:54 +000028 */
vlme3470e72005-03-10 13:39:03 +000029static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU,
30 const char *fname, ssize_t suggested_bufsize);
vlm6d44a542005-11-08 03:06:16 +000031static int write_out(const void *buffer, size_t size, void *key);
vlm0e329f22004-10-28 13:22:54 +000032
33 int opt_debug; /* -d */
34static int opt_check; /* -c */
vlm0e329f22004-10-28 13:22:54 +000035static int opt_stack; /* -s */
vlm6d44a542005-11-08 03:06:16 +000036
37/* Input data format selector */
38static enum input_format {
39 INP_BER, /* -iber: BER input */
40 INP_XER /* -ixer: XER input */
41} iform; /* -i<format> */
42
43/* Output data format selector */
44static enum output_format {
45 OUT_XER, /* -oxer: XER (XML) output */
46 OUT_DER, /* -oder: DER output */
47 OUT_TEXT, /* -otext: semi-structured text */
48 OUT_NULL /* -onull: No pretty-printing */
49} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000050
51#define DEBUG(fmt, args...) do { \
52 if(!opt_debug) break; \
53 fprintf(stderr, fmt, ##args); \
54 fprintf(stderr, "\n"); \
55} while(0)
56
57int
58main(int ac, char **av) {
59 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
60 int number_of_iterations = 1;
61 int num;
62 int ch;
63
64 /*
65 * Pocess the command-line argments.
66 */
vlm6d44a542005-11-08 03:06:16 +000067 while((ch = getopt(ac, av, "i:o:b:cdn:hs:")) != -1)
vlm0e329f22004-10-28 13:22:54 +000068 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +000069 case 'i':
70 if(optarg[0] == 'b') { iform = INP_BER; break; }
71 if(optarg[0] == 'x') { iform = INP_XER; break; }
72 fprintf(stderr, "-i<format>: '%s': improper format selector",
73 optarg);
74 exit(EX_UNAVAILABLE);
75 case 'o':
76 if(optarg[0] == 'd') { oform = OUT_DER; break; }
77 if(optarg[0] == 'x') { oform = OUT_XER; break; }
78 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
79 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
80 fprintf(stderr, "-o<format>: '%s': improper format selector",
81 optarg);
82 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +000083 case 'b':
84 suggested_bufsize = atoi(optarg);
85 if(suggested_bufsize < 1
86 || suggested_bufsize > 16 * 1024 * 1024) {
87 fprintf(stderr,
88 "-b %s: Improper buffer size (1..16M)\n",
89 optarg);
90 exit(EX_UNAVAILABLE);
91 }
92 break;
93 case 'c':
94 opt_check = 1;
95 break;
96 case 'd':
97 opt_debug++; /* Double -dd means ASN.1 debug */
98 break;
99 case 'n':
100 number_of_iterations = atoi(optarg);
101 if(number_of_iterations < 1) {
102 fprintf(stderr,
103 "-n %s: Improper iterations count\n", optarg);
104 exit(EX_UNAVAILABLE);
105 }
106 break;
vlm0e329f22004-10-28 13:22:54 +0000107 case 's':
108 opt_stack = atoi(optarg);
109 if(opt_stack <= 0) {
110 fprintf(stderr,
111 "-s %s: Value greater than 0 expected\n",
112 optarg);
113 exit(EX_UNAVAILABLE);
114 }
115 break;
vlm0e329f22004-10-28 13:22:54 +0000116 case 'h':
117 default:
118 fprintf(stderr,
119 "Usage: %s [options] <data.ber> ...\n"
120 "Where options are:\n"
vlm6d44a542005-11-08 03:06:16 +0000121 " -iber (I) Input is in BER (Basic Encoding Rules)\n"
122 " -ixer Input is in XER (XML Encoding Rules)\n"
123 " -oder Output in DER (Distinguished Encoding Rules)\n"
124 " -oxer (O) Output in XER (XML Encoding Rules)\n"
125 " -otext Output in plain semi-structured text (dump)\n"
126 " -onull Verify (decode) input, but do not output\n"
vlm0e329f22004-10-28 13:22:54 +0000127 " -b <size> Set the i/o buffer size (default is %ld)\n"
128 " -c Check ASN.1 constraints after decoding\n"
129 " -d Enable debugging (-dd is even better)\n"
130 " -n <num> Process files <num> times\n"
131 " -s <size> Set the stack usage limit\n"
vlm0e329f22004-10-28 13:22:54 +0000132 , av[0], (long)suggested_bufsize);
133 exit(EX_USAGE);
134 }
135
136 ac -= optind;
137 av += optind;
138
139 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000140 fprintf(stderr, "%s: No input files specified. "
141 "Try '-h' for more information\n",
142 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000143 exit(EX_USAGE);
144 }
145
146 setvbuf(stdout, 0, _IOLBF, 0);
147
148 for(num = 0; num < number_of_iterations; num++) {
149 int ac_i;
150 /*
151 * Process all files in turn.
152 */
153 for(ac_i = 0; ac_i < ac; ac_i++) {
154 char *fname = av[ac_i];
155 void *structure;
vlm6d44a542005-11-08 03:06:16 +0000156 asn_enc_rval_t erv;
vlm0e329f22004-10-28 13:22:54 +0000157
158 /*
159 * Decode the encoded structure from file.
160 */
vlme3470e72005-03-10 13:39:03 +0000161 structure = data_decode_from_file(pduType,
162 fname, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000163 if(!structure) {
164 /* Error message is already printed */
165 exit(EX_DATAERR);
166 }
167
vlm0e329f22004-10-28 13:22:54 +0000168 /* Check ASN.1 constraints */
169 if(opt_check) {
170 char errbuf[128];
171 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000172 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000173 errbuf, &errlen)) {
174 fprintf(stderr, "%s: ASN.1 constraint "
175 "check failed: %s\n", fname, errbuf);
176 exit(EX_DATAERR);
177 }
178 }
179
vlm6d44a542005-11-08 03:06:16 +0000180 switch(oform) {
181 case OUT_NULL:
182 fprintf(stderr, "%s: decoded successfully\n", fname);
183 break;
184 case OUT_TEXT: /* -otext */
185 asn_fprint(stdout, pduType, structure);
186 break;
187 case OUT_XER: /* -oxer */
188 if(xer_fprint(stdout, pduType, structure)) {
189 fprintf(stderr, "%s: Cannot convert into XML\n",
190 fname);
191 exit(EX_UNAVAILABLE);
192 }
193 break;
194 case OUT_DER:
195 erv = der_encode(pduType, structure, write_out, stdout);
196 if(erv.encoded < 0) {
197 fprintf(stderr, "%s: Cannot convert into DER\n",
198 fname);
199 exit(EX_UNAVAILABLE);
200 }
201 break;
202 }
203
vlme3470e72005-03-10 13:39:03 +0000204 pduType->free_struct(pduType, structure, 0);
vlm0e329f22004-10-28 13:22:54 +0000205 }
206 }
207
208 return 0;
209}
210
vlm6d44a542005-11-08 03:06:16 +0000211/* Dump the buffer */
212static int write_out(const void *buffer, size_t size, void *key) {
213 return (fwrite(buffer, 1, size, key) == size) ? 0 : -1;
214}
vlm0e329f22004-10-28 13:22:54 +0000215
216static char *buffer;
217static size_t buf_offset; /* Offset from the start */
218static size_t buf_len; /* Length of meaningful contents */
219static size_t buf_size; /* Allocated memory */
220static off_t buf_shifted; /* Number of bytes ever shifted */
221
222#define bufptr (buffer + buf_offset)
223#define bufend (buffer + buf_offset + buf_len)
224
225/*
vlm6d44a542005-11-08 03:06:16 +0000226 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000227 */
228static void buf_extend(size_t bySize) {
229
230 DEBUG("buf_extend(%ld) { o=%ld l=%ld s=%ld }",
231 (long)bySize, (long)buf_offset, (long)buf_len, (long)buf_size);
232
233 if(buf_size >= (buf_offset + buf_len + bySize)) {
234 return; /* Nothing to do */
235 } else if(bySize <= buf_offset) {
236 DEBUG("\tContents shifted by %ld", (long)buf_offset);
237
238 /* Shift the buffer contents */
239 memmove(buffer, buffer + buf_offset, buf_len);
240 buf_shifted += buf_offset;
241 buf_offset = 0;
242 } else {
243 size_t newsize = (buf_size << 2) + bySize;
244 void *p = realloc(buffer, newsize);
245 if(p) {
vlmd3c80792004-12-15 23:23:53 +0000246 buffer = (char *)p;
vlm0e329f22004-10-28 13:22:54 +0000247 buf_size = newsize;
248
249 DEBUG("\tBuffer reallocated to %ld", (long)newsize);
250 } else {
251 perror("realloc()");
252 exit(EX_OSERR);
253 }
254 }
255}
256
vlme3470e72005-03-10 13:39:03 +0000257static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) {
vlm0e329f22004-10-28 13:22:54 +0000258 static char *fbuf;
259 static ssize_t fbuf_size;
260 static asn_codec_ctx_t s_codec_ctx;
261 asn_codec_ctx_t *opt_codec_ctx = 0;
262 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000263 asn_dec_rval_t rval;
vlm0e329f22004-10-28 13:22:54 +0000264 size_t rd;
265 FILE *fp;
266
267 if(opt_stack) {
268 s_codec_ctx.max_stack_size = opt_stack;
269 opt_codec_ctx = &s_codec_ctx;
270 }
271
vlm6d44a542005-11-08 03:06:16 +0000272 if(strcmp(fname, "-")) {
273 DEBUG("Processing file %s", fname);
274 fp = fopen(fname, "r");
275 } else {
276 DEBUG("Processing standard input");
277 fname = "stdin";
278 fp = stdin;
279 }
vlm0e329f22004-10-28 13:22:54 +0000280
281 if(!fp) {
282 fprintf(stderr, "%s: %s\n", fname, strerror(errno));
283 return 0;
284 }
285
286 /* prepare the file buffer */
287 if(fbuf_size != suggested_bufsize) {
vlmd3c80792004-12-15 23:23:53 +0000288 fbuf = (char *)realloc(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000289 if(!fbuf) {
290 perror("realloc()");
291 exit(EX_OSERR);
292 }
293 fbuf_size = suggested_bufsize;
294 }
295
296 buf_shifted = 0;
297 buf_offset = 0;
298 buf_len = 0;
299
vlm6d44a542005-11-08 03:06:16 +0000300 /* Pretend immediate EOF */
301 rval.code = RC_WMORE;
302 rval.consumed = 0;
303
vlm0e329f22004-10-28 13:22:54 +0000304 while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) {
vlm6d44a542005-11-08 03:06:16 +0000305 char *i_bptr;
306 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000307
308 /*
309 * Copy the data over, or use the original buffer.
310 */
311 if(buf_len) {
312 /* Append the new data into the intermediate buffer */
313 buf_extend(rd);
314 memcpy(bufend, fbuf, rd);
315 buf_len += rd;
316
vlm6d44a542005-11-08 03:06:16 +0000317 i_bptr = bufptr;
318 i_size = buf_len;
vlm0e329f22004-10-28 13:22:54 +0000319 } else {
vlm6d44a542005-11-08 03:06:16 +0000320 i_bptr = fbuf;
321 i_size = rd;
322 }
vlm0e329f22004-10-28 13:22:54 +0000323
vlm6d44a542005-11-08 03:06:16 +0000324 switch(iform) {
325 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000326 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000327 (void **)&structure, i_bptr, i_size);
328 break;
329 case INP_XER:
330 rval = xer_decode(opt_codec_ctx, pduType,
331 (void **)&structure, i_bptr, i_size);
332 break;
333 }
334 DEBUG("decode(%ld) consumed %ld, code %d",
335 (long)buf_len, (long)rval.consumed, rval.code);
vlm0e329f22004-10-28 13:22:54 +0000336
vlm6d44a542005-11-08 03:06:16 +0000337 if(buf_len == 0) {
vlm0e329f22004-10-28 13:22:54 +0000338 /*
339 * Switch the remainder into the intermediate buffer.
340 */
341 if(rval.code != RC_FAIL && rval.consumed < rd) {
342 buf_extend(rd - rval.consumed);
343 memcpy(bufend,
344 fbuf + rval.consumed,
345 rd - rval.consumed);
346 buf_len = rd - rval.consumed;
347 }
348 }
349
350 switch(rval.code) {
351 case RC_OK:
352 DEBUG("RC_OK, finishing up");
vlm6d44a542005-11-08 03:06:16 +0000353 if(fp != stdin) fclose(fp);
vlm0e329f22004-10-28 13:22:54 +0000354 return structure;
355 case RC_WMORE:
356 DEBUG("RC_WMORE, continuing...");
vlm6d44a542005-11-08 03:06:16 +0000357 /*
358 * Adjust position inside the source buffer.
359 */
360 buf_offset += rval.consumed;
361 buf_len -= rval.consumed;
362 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000363 continue;
364 case RC_FAIL:
365 break;
366 }
367 break;
368 }
369
370 fclose(fp);
371
372 /* Clean up partially decoded structure */
vlme3470e72005-03-10 13:39:03 +0000373 pduType->free_struct(pduType, structure, 0);
vlm0e329f22004-10-28 13:22:54 +0000374
375 fprintf(stderr, "%s: "
vlm6d44a542005-11-08 03:06:16 +0000376 "Decode failed past byte %ld: %s\n",
377 fname, (long)(buf_shifted + buf_offset + rval.consumed),
378 (rval.code == RC_WMORE)
379 ? "Unexpected end of input"
380 : "Input processing error");
vlm0e329f22004-10-28 13:22:54 +0000381
382 return 0;
383}
384