blob: c8ee34f2d9e865a8d57ffd468461da1855014a78 [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 */
vlm6ed22c62006-08-25 04:56:21 +000055 OUT_DER, /* -oder: DER (BER) output */
56 OUT_PER, /* -oper: Unaligned PER output */
vlm6d44a542005-11-08 03:06:16 +000057 OUT_TEXT, /* -otext: semi-structured text */
58 OUT_NULL /* -onull: No pretty-printing */
59} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000060
vlm4d2ca122005-12-07 05:46:03 +000061/* Debug output function */
62static inline void
63DEBUG(const char *fmt, ...) {
64 va_list ap;
65 if(!opt_debug) return;
66 fprintf(stderr, "AD: ");
67 va_start(ap, fmt);
68 vfprintf(stderr, fmt, ap);
69 va_end(ap);
70 fprintf(stderr, "\n");
71}
vlm0e329f22004-10-28 13:22:54 +000072
73int
74main(int ac, char **av) {
vlmb51ebb22006-08-25 02:35:08 +000075 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
vlm0e329f22004-10-28 13:22:54 +000076 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
77 int number_of_iterations = 1;
78 int num;
79 int ch;
80
vlm337167e2005-11-26 11:25:14 +000081 /* Figure out if Unaligned PER needs to be default */
82 if(pduType->uper_decoder)
83 iform = INP_PER;
84
vlm0e329f22004-10-28 13:22:54 +000085 /*
86 * Pocess the command-line argments.
87 */
vlm337167e2005-11-26 11:25:14 +000088 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
vlm0e329f22004-10-28 13:22:54 +000089 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +000090 case 'i':
91 if(optarg[0] == 'b') { iform = INP_BER; break; }
92 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +000093 if(pduType->uper_decoder
94 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6ed22c62006-08-25 04:56:21 +000095 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +000096 optarg);
97 exit(EX_UNAVAILABLE);
98 case 'o':
99 if(optarg[0] == 'd') { oform = OUT_DER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000100 if(pduType->uper_encoder
101 && optarg[0] == 'p') { oform = OUT_PER; break; }
vlm6d44a542005-11-08 03:06:16 +0000102 if(optarg[0] == 'x') { oform = OUT_XER; break; }
103 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
104 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
vlm6ed22c62006-08-25 04:56:21 +0000105 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000106 optarg);
107 exit(EX_UNAVAILABLE);
vlm337167e2005-11-26 11:25:14 +0000108 case 'p':
109#ifdef ASN_PDU_COLLECTION
110 {
111 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
112 if(optarg[0] < 'A' || optarg[0] > 'Z') {
113 fprintf(stderr, "Available PDU types:\n");
114 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
115 exit(0);
116 }
117 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
118 if(*pdu) { pduType = *pdu; break; }
119 }
120#endif /* ASN_PDU_COLLECTION */
121 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
122 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +0000123 case 'b':
124 suggested_bufsize = atoi(optarg);
125 if(suggested_bufsize < 1
126 || suggested_bufsize > 16 * 1024 * 1024) {
127 fprintf(stderr,
128 "-b %s: Improper buffer size (1..16M)\n",
129 optarg);
130 exit(EX_UNAVAILABLE);
131 }
132 break;
133 case 'c':
134 opt_check = 1;
135 break;
136 case 'd':
137 opt_debug++; /* Double -dd means ASN.1 debug */
138 break;
139 case 'n':
140 number_of_iterations = atoi(optarg);
141 if(number_of_iterations < 1) {
142 fprintf(stderr,
143 "-n %s: Improper iterations count\n", optarg);
144 exit(EX_UNAVAILABLE);
145 }
146 break;
vlm0e329f22004-10-28 13:22:54 +0000147 case 's':
148 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000149 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000150 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000151 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000152 optarg);
153 exit(EX_UNAVAILABLE);
154 }
155 break;
vlm0e329f22004-10-28 13:22:54 +0000156 case 'h':
157 default:
vlm337167e2005-11-26 11:25:14 +0000158 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
159 fprintf(stderr, "Where options are:\n");
160 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000161 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000162 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000163 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000164 " -iber Input is in BER (Basic Encoding Rules)%s\n",
165 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000166 fprintf(stderr,
vlm6ed22c62006-08-25 04:56:21 +0000167 " -ixer Input is in XER (XML Encoding Rules)\n");
168 if(pduType->uper_encoder)
169 fprintf(stderr,
170 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
171 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000172 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000173 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000174 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000175 " -onull Verify (decode) input, but do not output\n");
176#ifdef ASN_PDU_COLLECTION
177 fprintf(stderr,
178 " -p <PDU> Specify PDU type to decode\n"
179 " -p list List available PDUs\n");
180#endif /* ASN_PDU_COLLECTION */
181 fprintf(stderr,
vlm0e329f22004-10-28 13:22:54 +0000182 " -b <size> Set the i/o buffer size (default is %ld)\n"
183 " -c Check ASN.1 constraints after decoding\n"
184 " -d Enable debugging (-dd is even better)\n"
185 " -n <num> Process files <num> times\n"
vlm4d2ca122005-12-07 05:46:03 +0000186 " -s <size> Set the stack usage limit (default is %d)\n"
187 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000188 exit(EX_USAGE);
189 }
190
191 ac -= optind;
192 av += optind;
193
194 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000195 fprintf(stderr, "%s: No input files specified. "
196 "Try '-h' for more information\n",
197 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000198 exit(EX_USAGE);
199 }
200
201 setvbuf(stdout, 0, _IOLBF, 0);
202
203 for(num = 0; num < number_of_iterations; num++) {
204 int ac_i;
205 /*
206 * Process all files in turn.
207 */
208 for(ac_i = 0; ac_i < ac; ac_i++) {
209 char *fname = av[ac_i];
210 void *structure;
vlm6d44a542005-11-08 03:06:16 +0000211 asn_enc_rval_t erv;
vlm0e329f22004-10-28 13:22:54 +0000212
213 /*
214 * Decode the encoded structure from file.
215 */
vlme3470e72005-03-10 13:39:03 +0000216 structure = data_decode_from_file(pduType,
217 fname, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000218 if(!structure) {
219 /* Error message is already printed */
220 exit(EX_DATAERR);
221 }
222
vlm0e329f22004-10-28 13:22:54 +0000223 /* Check ASN.1 constraints */
224 if(opt_check) {
225 char errbuf[128];
226 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000227 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000228 errbuf, &errlen)) {
229 fprintf(stderr, "%s: ASN.1 constraint "
230 "check failed: %s\n", fname, errbuf);
231 exit(EX_DATAERR);
232 }
233 }
234
vlm6d44a542005-11-08 03:06:16 +0000235 switch(oform) {
236 case OUT_NULL:
237 fprintf(stderr, "%s: decoded successfully\n", fname);
238 break;
239 case OUT_TEXT: /* -otext */
240 asn_fprint(stdout, pduType, structure);
241 break;
242 case OUT_XER: /* -oxer */
243 if(xer_fprint(stdout, pduType, structure)) {
244 fprintf(stderr, "%s: Cannot convert into XML\n",
245 fname);
246 exit(EX_UNAVAILABLE);
247 }
248 break;
249 case OUT_DER:
250 erv = der_encode(pduType, structure, write_out, stdout);
251 if(erv.encoded < 0) {
252 fprintf(stderr, "%s: Cannot convert into DER\n",
253 fname);
254 exit(EX_UNAVAILABLE);
255 }
256 break;
vlm6ed22c62006-08-25 04:56:21 +0000257 case OUT_PER:
258 erv = uper_encode(pduType, structure, write_out, stdout);
259 if(erv.encoded < 0) {
260 fprintf(stderr, "%s: Cannot convert into Unaligned PER\n",
261 fname);
262 exit(EX_UNAVAILABLE);
263 }
264 break;
vlm6d44a542005-11-08 03:06:16 +0000265 }
266
vlmbe78c802006-03-17 02:11:12 +0000267 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000268 }
269 }
270
271 return 0;
272}
273
vlmf5aff922006-09-13 04:02:00 +0000274static struct dynamic_buffer {
275 char *data; /* Pointer to the data bytes */
276 size_t offset; /* Offset from the start */
277 size_t length; /* Length of meaningful contents */
278 size_t allocated; /* Allocated memory for data */
279 int nreallocs; /* Number of data reallocations */
280 off_t bytes_shifted; /* Number of bytes ever shifted */
281} DynamicBuffer;
vlm0e329f22004-10-28 13:22:54 +0000282
283/*
vlm6d44a542005-11-08 03:06:16 +0000284 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000285 */
vlmf5aff922006-09-13 04:02:00 +0000286static void add_bytes_to_buffer(const void *data2add, size_t bySize) {
vlm0e329f22004-10-28 13:22:54 +0000287
vlmf5aff922006-09-13 04:02:00 +0000288 DEBUG("add_bytes(%ld) { o=%ld l=%ld s=%ld }",
289 (long)bySize,
290 (long)DynamicBuffer.offset,
291 (long)DynamicBuffer.length,
292 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000293
vlmf5aff922006-09-13 04:02:00 +0000294 if(DynamicBuffer.allocated
295 >= (DynamicBuffer.offset + DynamicBuffer.length + bySize)) {
vlmcb923822006-09-13 04:14:17 +0000296 DEBUG("\tNo buffer reallocation is necessary");
vlmf5aff922006-09-13 04:02:00 +0000297 } else if(bySize <= DynamicBuffer.offset) {
298 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
vlm0e329f22004-10-28 13:22:54 +0000299
300 /* Shift the buffer contents */
vlmf5aff922006-09-13 04:02:00 +0000301 memmove(DynamicBuffer.data,
302 DynamicBuffer.data + DynamicBuffer.offset,
303 DynamicBuffer.length);
304 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
305 DynamicBuffer.offset = 0;
vlm0e329f22004-10-28 13:22:54 +0000306 } else {
vlmf5aff922006-09-13 04:02:00 +0000307 size_t newsize = (DynamicBuffer.allocated << 2) + bySize;
308 void *p = MALLOC(newsize);
vlm4d2ca122005-12-07 05:46:03 +0000309 if(!p) {
vlmf5aff922006-09-13 04:02:00 +0000310 perror("malloc()");
vlm0e329f22004-10-28 13:22:54 +0000311 exit(EX_OSERR);
312 }
vlmf5aff922006-09-13 04:02:00 +0000313 memcpy(p, DynamicBuffer.data, DynamicBuffer.length);
314 FREEMEM(DynamicBuffer.data);
315 DynamicBuffer.data = (char *)p;
316 DynamicBuffer.offset = 0;
317 DynamicBuffer.allocated = newsize;
318 DynamicBuffer.nreallocs++;
vlmcb923822006-09-13 04:14:17 +0000319 DEBUG("\tBuffer reallocated to %ld (%d time)",
vlmf5aff922006-09-13 04:02:00 +0000320 newsize, DynamicBuffer.nreallocs);
vlm0e329f22004-10-28 13:22:54 +0000321 }
vlm4d2ca122005-12-07 05:46:03 +0000322
vlmf5aff922006-09-13 04:02:00 +0000323 memcpy(DynamicBuffer.data + DynamicBuffer.offset + DynamicBuffer.length,
324 data2add, bySize);
325 DynamicBuffer.length += bySize;
vlm0e329f22004-10-28 13:22:54 +0000326}
327
vlme3470e72005-03-10 13:39:03 +0000328static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) {
vlm0e329f22004-10-28 13:22:54 +0000329 static char *fbuf;
330 static ssize_t fbuf_size;
331 static asn_codec_ctx_t s_codec_ctx;
332 asn_codec_ctx_t *opt_codec_ctx = 0;
333 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000334 asn_dec_rval_t rval;
vlm0e329f22004-10-28 13:22:54 +0000335 size_t rd;
336 FILE *fp;
337
338 if(opt_stack) {
339 s_codec_ctx.max_stack_size = opt_stack;
340 opt_codec_ctx = &s_codec_ctx;
341 }
342
vlm6d44a542005-11-08 03:06:16 +0000343 if(strcmp(fname, "-")) {
344 DEBUG("Processing file %s", fname);
345 fp = fopen(fname, "r");
346 } else {
vlm4d2ca122005-12-07 05:46:03 +0000347 DEBUG("Processing %s", "standard input");
vlm6d44a542005-11-08 03:06:16 +0000348 fname = "stdin";
349 fp = stdin;
350 }
vlm0e329f22004-10-28 13:22:54 +0000351
352 if(!fp) {
353 fprintf(stderr, "%s: %s\n", fname, strerror(errno));
354 return 0;
355 }
356
357 /* prepare the file buffer */
358 if(fbuf_size != suggested_bufsize) {
vlmf5aff922006-09-13 04:02:00 +0000359 fbuf = (char *)REALLOC(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000360 if(!fbuf) {
361 perror("realloc()");
362 exit(EX_OSERR);
363 }
364 fbuf_size = suggested_bufsize;
365 }
366
vlmf5aff922006-09-13 04:02:00 +0000367 DynamicBuffer.offset = 0;
368 DynamicBuffer.length = 0;
369 DynamicBuffer.allocated = 0;
370 DynamicBuffer.bytes_shifted = 0;
371 DynamicBuffer.nreallocs = 0;
vlm0e329f22004-10-28 13:22:54 +0000372
vlm6d44a542005-11-08 03:06:16 +0000373 /* Pretend immediate EOF */
374 rval.code = RC_WMORE;
375 rval.consumed = 0;
376
vlm0e329f22004-10-28 13:22:54 +0000377 while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) {
vlm6d44a542005-11-08 03:06:16 +0000378 char *i_bptr;
379 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000380
381 /*
382 * Copy the data over, or use the original buffer.
383 */
vlmf5aff922006-09-13 04:02:00 +0000384 if(DynamicBuffer.allocated) {
vlm0e329f22004-10-28 13:22:54 +0000385 /* Append the new data into the intermediate buffer */
vlmf5aff922006-09-13 04:02:00 +0000386 add_bytes_to_buffer(fbuf, rd);
387 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
vlmcb923822006-09-13 04:14:17 +0000388 i_size = DynamicBuffer.length;
vlm0e329f22004-10-28 13:22:54 +0000389 } else {
vlm6d44a542005-11-08 03:06:16 +0000390 i_bptr = fbuf;
391 i_size = rd;
392 }
vlm0e329f22004-10-28 13:22:54 +0000393
vlm6d44a542005-11-08 03:06:16 +0000394 switch(iform) {
395 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000396 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000397 (void **)&structure, i_bptr, i_size);
398 break;
399 case INP_XER:
400 rval = xer_decode(opt_codec_ctx, pduType,
401 (void **)&structure, i_bptr, i_size);
402 break;
vlm337167e2005-11-26 11:25:14 +0000403 case INP_PER:
vlm4d2ca122005-12-07 05:46:03 +0000404 rval = uper_decode(opt_codec_ctx, pduType,
405 (void **)&structure, i_bptr, i_size);
vlm337167e2005-11-26 11:25:14 +0000406 break;
vlm6d44a542005-11-08 03:06:16 +0000407 }
vlm4d2ca122005-12-07 05:46:03 +0000408 DEBUG("decode(%ld) consumed %ld (%ld), code %d",
vlmf5aff922006-09-13 04:02:00 +0000409 (long)DynamicBuffer.length,
410 (long)rval.consumed, (long)i_size,
vlm4d2ca122005-12-07 05:46:03 +0000411 rval.code);
vlm0e329f22004-10-28 13:22:54 +0000412
vlmf5aff922006-09-13 04:02:00 +0000413 if(DynamicBuffer.allocated == 0) {
vlm0e329f22004-10-28 13:22:54 +0000414 /*
vlmf5aff922006-09-13 04:02:00 +0000415 * Flush the remainder into the intermediate buffer.
vlm0e329f22004-10-28 13:22:54 +0000416 */
417 if(rval.code != RC_FAIL && rval.consumed < rd) {
vlmf5aff922006-09-13 04:02:00 +0000418 add_bytes_to_buffer(fbuf + rval.consumed,
vlm4d2ca122005-12-07 05:46:03 +0000419 rd - rval.consumed);
420 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000421 }
422 }
423
424 switch(rval.code) {
425 case RC_OK:
vlm4d2ca122005-12-07 05:46:03 +0000426 DEBUG("RC_OK, finishing up with %ld",
427 (long)rval.consumed);
vlm6d44a542005-11-08 03:06:16 +0000428 if(fp != stdin) fclose(fp);
vlm0e329f22004-10-28 13:22:54 +0000429 return structure;
430 case RC_WMORE:
vlm6d44a542005-11-08 03:06:16 +0000431 /*
432 * Adjust position inside the source buffer.
433 */
vlmf5aff922006-09-13 04:02:00 +0000434 if(DynamicBuffer.allocated) {
435 DynamicBuffer.offset += rval.consumed;
436 DynamicBuffer.length -= rval.consumed;
437 }
vlmcb923822006-09-13 04:14:17 +0000438 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
439 (long)rval.consumed,
440 (long)DynamicBuffer.offset,
441 (long)DynamicBuffer.length,
442 (long)DynamicBuffer.allocated);
vlm6d44a542005-11-08 03:06:16 +0000443 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000444 continue;
445 case RC_FAIL:
446 break;
447 }
448 break;
449 }
450
451 fclose(fp);
452
453 /* Clean up partially decoded structure */
vlmbe78c802006-03-17 02:11:12 +0000454 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000455
456 fprintf(stderr, "%s: "
vlm6d44a542005-11-08 03:06:16 +0000457 "Decode failed past byte %ld: %s\n",
vlmf5aff922006-09-13 04:02:00 +0000458 fname, (long)(DynamicBuffer.bytes_shifted
459 + DynamicBuffer.offset + rval.consumed),
vlm6d44a542005-11-08 03:06:16 +0000460 (rval.code == RC_WMORE)
461 ? "Unexpected end of input"
462 : "Input processing error");
vlm0e329f22004-10-28 13:22:54 +0000463
464 return 0;
465}
466
vlmf5aff922006-09-13 04:02:00 +0000467/* Dump the buffer out to the specified FILE */
468static int write_out(const void *buffer, size_t size, void *key) {
469 FILE *fp = (FILE *)key;
470 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
471}