blob: e5ab8da137d92d4cf732f22ea054baae46e0c8a2 [file] [log] [blame]
Lev Walkin22b54552004-10-28 13:22:54 +00001/*
Lev Walkind0625682006-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.
Lev Walkin22b54552004-10-28 13:22:54 +00004 *
Lev Walkind0625682006-08-25 02:35:08 +00005 * To compile with your own ASN.1 type, please redefine the PDU as shown:
Lev Walkin22b54552004-10-28 13:22:54 +00006 *
Lev Walkind0625682006-08-25 02:35:08 +00007 * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.c
Lev Walkin22b54552004-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>
Lev Walkin4696c742005-08-22 12:23:54 +000014#include <stdlib.h> /* for atoi(3) */
15#include <unistd.h> /* for getopt(3) */
Lev Walkin22b54552004-10-28 13:22:54 +000016#include <string.h> /* for strerror(3) */
Lev Walkin22b54552004-10-28 13:22:54 +000017#include <sysexits.h> /* for EX_* exit codes */
Lev Walkinfc776432005-03-29 17:21:14 +000018#include <assert.h> /* for assert(3) */
19#include <errno.h> /* for errno */
Lev Walkin22b54552004-10-28 13:22:54 +000020
Lev Walkin00949562005-03-10 13:39:03 +000021#include <asn_application.h>
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000022#include <asn_internal.h> /* for _ASN_DEFAULT_STACK_MAX */
Lev Walkin22b54552004-10-28 13:22:54 +000023
Lev Walkind0625682006-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 */
Lev Walkin59b176e2005-11-26 11:25:14 +000030#ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
31extern asn_TYPE_descriptor_t *asn_pdu_collection[];
32#endif
Lev Walkin22b54552004-10-28 13:22:54 +000033
34/*
Lev Walkin00949562005-03-10 13:39:03 +000035 * Open file and parse its contens.
Lev Walkin22b54552004-10-28 13:22:54 +000036 */
Lev Walkin00949562005-03-10 13:39:03 +000037static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU,
38 const char *fname, ssize_t suggested_bufsize);
Lev Walkind1bfea62005-11-08 03:06:16 +000039static int write_out(const void *buffer, size_t size, void *key);
Lev Walkin22b54552004-10-28 13:22:54 +000040
41 int opt_debug; /* -d */
42static int opt_check; /* -c */
Lev Walkin22b54552004-10-28 13:22:54 +000043static int opt_stack; /* -s */
Lev Walkind1bfea62005-11-08 03:06:16 +000044
45/* Input data format selector */
46static enum input_format {
47 INP_BER, /* -iber: BER input */
Lev Walkin59b176e2005-11-26 11:25:14 +000048 INP_XER, /* -ixer: XER input */
49 INP_PER /* -iper: Unaligned PER input */
Lev Walkind1bfea62005-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 */
Lev Walkinbe3a0c52006-08-25 04:56:21 +000055 OUT_DER, /* -oder: DER (BER) output */
56 OUT_PER, /* -oper: Unaligned PER output */
Lev Walkind1bfea62005-11-08 03:06:16 +000057 OUT_TEXT, /* -otext: semi-structured text */
58 OUT_NULL /* -onull: No pretty-printing */
59} oform; /* -o<format> */
Lev Walkin22b54552004-10-28 13:22:54 +000060
Lev Walkin1d9e8dd2005-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}
Lev Walkin22b54552004-10-28 13:22:54 +000072
73int
74main(int ac, char **av) {
Lev Walkind0625682006-08-25 02:35:08 +000075 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
Lev Walkin22b54552004-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
Lev Walkin59b176e2005-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
Lev Walkin22b54552004-10-28 13:22:54 +000085 /*
86 * Pocess the command-line argments.
87 */
Lev Walkin59b176e2005-11-26 11:25:14 +000088 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
Lev Walkin22b54552004-10-28 13:22:54 +000089 switch(ch) {
Lev Walkind1bfea62005-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; }
Lev Walkin59b176e2005-11-26 11:25:14 +000093 if(pduType->uper_decoder
94 && optarg[0] == 'p') { iform = INP_PER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +000095 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +000096 optarg);
97 exit(EX_UNAVAILABLE);
98 case 'o':
99 if(optarg[0] == 'd') { oform = OUT_DER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000100 if(pduType->uper_encoder
101 && optarg[0] == 'p') { oform = OUT_PER; break; }
Lev Walkind1bfea62005-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; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000105 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000106 optarg);
107 exit(EX_UNAVAILABLE);
Lev Walkin59b176e2005-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);
Lev Walkin22b54552004-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000147 case 's':
148 opt_stack = atoi(optarg);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000149 if(opt_stack < 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000150 fprintf(stderr,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000151 "-s %s: Non-negative value expected\n",
Lev Walkin22b54552004-10-28 13:22:54 +0000152 optarg);
153 exit(EX_UNAVAILABLE);
154 }
155 break;
Lev Walkin22b54552004-10-28 13:22:54 +0000156 case 'h':
157 default:
Lev Walkin59b176e2005-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)
Lev Walkin22b54552004-10-28 13:22:54 +0000161 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000162 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000163 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000164 " -iber Input is in BER (Basic Encoding Rules)%s\n",
165 iform == INP_PER ? "" : " (DEFAULT)");
Lev Walkin59b176e2005-11-26 11:25:14 +0000166 fprintf(stderr,
Lev Walkinbe3a0c52006-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,
Lev Walkind1bfea62005-11-08 03:06:16 +0000172 " -oder Output in DER (Distinguished Encoding Rules)\n"
Lev Walkin857d11b2006-02-22 08:54:49 +0000173 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
Lev Walkind1bfea62005-11-08 03:06:16 +0000174 " -otext Output in plain semi-structured text (dump)\n"
Lev Walkin59b176e2005-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,
Lev Walkin22b54552004-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"
Lev Walkin1d9e8dd2005-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);
Lev Walkin22b54552004-10-28 13:22:54 +0000188 exit(EX_USAGE);
189 }
190
191 ac -= optind;
192 av += optind;
193
194 if(ac < 1) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000195 fprintf(stderr, "%s: No input files specified. "
196 "Try '-h' for more information\n",
197 av[-optind]);
Lev Walkin22b54552004-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;
Lev Walkind1bfea62005-11-08 03:06:16 +0000211 asn_enc_rval_t erv;
Lev Walkin22b54552004-10-28 13:22:54 +0000212
213 /*
214 * Decode the encoded structure from file.
215 */
Lev Walkin00949562005-03-10 13:39:03 +0000216 structure = data_decode_from_file(pduType,
217 fname, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000218 if(!structure) {
219 /* Error message is already printed */
220 exit(EX_DATAERR);
221 }
222
Lev Walkin22b54552004-10-28 13:22:54 +0000223 /* Check ASN.1 constraints */
224 if(opt_check) {
225 char errbuf[128];
226 size_t errlen = sizeof(errbuf);
Lev Walkin00949562005-03-10 13:39:03 +0000227 if(asn_check_constraints(pduType, structure,
Lev Walkin22b54552004-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
Lev Walkind1bfea62005-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;
Lev Walkinbe3a0c52006-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;
Lev Walkind1bfea62005-11-08 03:06:16 +0000265 }
266
Lev Walkinadcb5862006-03-17 02:11:12 +0000267 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000268 }
269 }
270
271 return 0;
272}
273
Lev Walkind1bfea62005-11-08 03:06:16 +0000274/* Dump the buffer */
275static int write_out(const void *buffer, size_t size, void *key) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000276 FILE *fp = (FILE *)key;
277 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
Lev Walkind1bfea62005-11-08 03:06:16 +0000278}
Lev Walkin22b54552004-10-28 13:22:54 +0000279
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000280static char *buffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000281static size_t buf_len; /* Length of meaningful contents */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000282static size_t buf_size; /* Allocated memory */
283static size_t buf_offset; /* Offset from the start */
284static off_t buf_shifted; /* Number of bytes ever shifted */
285static int buf_nreallocs; /* Number of reallocations */
Lev Walkin22b54552004-10-28 13:22:54 +0000286
287#define bufptr (buffer + buf_offset)
288#define bufend (buffer + buf_offset + buf_len)
289
290/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000291 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000292 */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000293static void buf_extend(const void *data2add, size_t bySize) {
Lev Walkin22b54552004-10-28 13:22:54 +0000294
295 DEBUG("buf_extend(%ld) { o=%ld l=%ld s=%ld }",
296 (long)bySize, (long)buf_offset, (long)buf_len, (long)buf_size);
297
298 if(buf_size >= (buf_offset + buf_len + bySize)) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000299 /* Nothing to do */
Lev Walkin22b54552004-10-28 13:22:54 +0000300 } else if(bySize <= buf_offset) {
301 DEBUG("\tContents shifted by %ld", (long)buf_offset);
302
303 /* Shift the buffer contents */
304 memmove(buffer, buffer + buf_offset, buf_len);
305 buf_shifted += buf_offset;
306 buf_offset = 0;
307 } else {
308 size_t newsize = (buf_size << 2) + bySize;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000309 void *p = malloc(newsize);
310 if(!p) {
Lev Walkin22b54552004-10-28 13:22:54 +0000311 perror("realloc()");
312 exit(EX_OSERR);
313 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000314 memcpy(p, buffer, buf_len);
315 free(buffer);
316 buffer = (char *)p;
317 buf_size = newsize;
318 buf_offset = 0;
319 DEBUG("\tBuffer reallocated to %ld, %d time",
320 (long)newsize, ++buf_nreallocs);
Lev Walkin22b54552004-10-28 13:22:54 +0000321 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000322
323 memcpy(buffer + buf_offset + buf_len, data2add, bySize);
324 buf_len += bySize;
Lev Walkin22b54552004-10-28 13:22:54 +0000325}
326
Lev Walkin00949562005-03-10 13:39:03 +0000327static void *data_decode_from_file(asn_TYPE_descriptor_t *pduType, const char *fname, ssize_t suggested_bufsize) {
Lev Walkin22b54552004-10-28 13:22:54 +0000328 static char *fbuf;
329 static ssize_t fbuf_size;
330 static asn_codec_ctx_t s_codec_ctx;
331 asn_codec_ctx_t *opt_codec_ctx = 0;
332 void *structure = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000333 asn_dec_rval_t rval;
Lev Walkin22b54552004-10-28 13:22:54 +0000334 size_t rd;
335 FILE *fp;
336
337 if(opt_stack) {
338 s_codec_ctx.max_stack_size = opt_stack;
339 opt_codec_ctx = &s_codec_ctx;
340 }
341
Lev Walkind1bfea62005-11-08 03:06:16 +0000342 if(strcmp(fname, "-")) {
343 DEBUG("Processing file %s", fname);
344 fp = fopen(fname, "r");
345 } else {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000346 DEBUG("Processing %s", "standard input");
Lev Walkind1bfea62005-11-08 03:06:16 +0000347 fname = "stdin";
348 fp = stdin;
349 }
Lev Walkin22b54552004-10-28 13:22:54 +0000350
351 if(!fp) {
352 fprintf(stderr, "%s: %s\n", fname, strerror(errno));
353 return 0;
354 }
355
356 /* prepare the file buffer */
357 if(fbuf_size != suggested_bufsize) {
Lev Walkin814cca72004-12-15 23:23:53 +0000358 fbuf = (char *)realloc(fbuf, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000359 if(!fbuf) {
360 perror("realloc()");
361 exit(EX_OSERR);
362 }
363 fbuf_size = suggested_bufsize;
364 }
365
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000366 buf_nreallocs = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000367 buf_shifted = 0;
368 buf_offset = 0;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000369 buf_size = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000370 buf_len = 0;
371
Lev Walkind1bfea62005-11-08 03:06:16 +0000372 /* Pretend immediate EOF */
373 rval.code = RC_WMORE;
374 rval.consumed = 0;
375
Lev Walkin22b54552004-10-28 13:22:54 +0000376 while((rd = fread(fbuf, 1, fbuf_size, fp)) || !feof(fp)) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000377 char *i_bptr;
378 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000379
380 /*
381 * Copy the data over, or use the original buffer.
382 */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000383 if(buf_size) {
Lev Walkin22b54552004-10-28 13:22:54 +0000384 /* Append the new data into the intermediate buffer */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000385 buf_extend(fbuf, rd);
Lev Walkind1bfea62005-11-08 03:06:16 +0000386 i_bptr = bufptr;
387 i_size = buf_len;
Lev Walkin22b54552004-10-28 13:22:54 +0000388 } else {
Lev Walkind1bfea62005-11-08 03:06:16 +0000389 i_bptr = fbuf;
390 i_size = rd;
391 }
Lev Walkin22b54552004-10-28 13:22:54 +0000392
Lev Walkind1bfea62005-11-08 03:06:16 +0000393 switch(iform) {
394 case INP_BER:
Lev Walkin00949562005-03-10 13:39:03 +0000395 rval = ber_decode(opt_codec_ctx, pduType,
Lev Walkind1bfea62005-11-08 03:06:16 +0000396 (void **)&structure, i_bptr, i_size);
397 break;
398 case INP_XER:
399 rval = xer_decode(opt_codec_ctx, pduType,
400 (void **)&structure, i_bptr, i_size);
401 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000402 case INP_PER:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000403 rval = uper_decode(opt_codec_ctx, pduType,
404 (void **)&structure, i_bptr, i_size);
Lev Walkin59b176e2005-11-26 11:25:14 +0000405 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000406 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000407 DEBUG("decode(%ld) consumed %ld (%ld), code %d",
408 (long)buf_len, (long)rval.consumed, (long)i_size,
409 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000410
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000411 if(buf_size == 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000412 /*
413 * Switch the remainder into the intermediate buffer.
414 */
415 if(rval.code != RC_FAIL && rval.consumed < rd) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000416 buf_extend(fbuf + rval.consumed,
417 rd - rval.consumed);
418 rval.consumed = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000419 }
420 }
421
422 switch(rval.code) {
423 case RC_OK:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000424 DEBUG("RC_OK, finishing up with %ld",
425 (long)rval.consumed);
Lev Walkind1bfea62005-11-08 03:06:16 +0000426 if(fp != stdin) fclose(fp);
Lev Walkin22b54552004-10-28 13:22:54 +0000427 return structure;
428 case RC_WMORE:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000429 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
430 (long)rval.consumed, (long)buf_offset,
431 (long)buf_len, (long)buf_size);
Lev Walkind1bfea62005-11-08 03:06:16 +0000432 /*
433 * Adjust position inside the source buffer.
434 */
435 buf_offset += rval.consumed;
436 buf_len -= rval.consumed;
437 rval.consumed = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000438 continue;
439 case RC_FAIL:
440 break;
441 }
442 break;
443 }
444
445 fclose(fp);
446
447 /* Clean up partially decoded structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000448 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000449
450 fprintf(stderr, "%s: "
Lev Walkind1bfea62005-11-08 03:06:16 +0000451 "Decode failed past byte %ld: %s\n",
452 fname, (long)(buf_shifted + buf_offset + rval.consumed),
453 (rval.code == RC_WMORE)
454 ? "Unexpected end of input"
455 : "Input processing error");
Lev Walkin22b54552004-10-28 13:22:54 +0000456
457 return 0;
458}
459