blob: d146f3262e6e42c354e17517d8ba2190ab65bdd1 [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,
vlme0fb1e82006-09-15 18:33:25 +000038 FILE *f, const char *filename, ssize_t suggested_bufsize);
vlm6d44a542005-11-08 03:06:16 +000039static int write_out(const void *buffer, size_t size, void *key);
vlme0fb1e82006-09-15 18:33:25 +000040static FILE *argument_to_file(char *av[], int idx);
41static char *argument_to_name(char *av[], int idx);
vlm0e329f22004-10-28 13:22:54 +000042
43 int opt_debug; /* -d */
44static int opt_check; /* -c */
vlm0e329f22004-10-28 13:22:54 +000045static int opt_stack; /* -s */
vlm6d44a542005-11-08 03:06:16 +000046
47/* Input data format selector */
48static enum input_format {
49 INP_BER, /* -iber: BER input */
vlm337167e2005-11-26 11:25:14 +000050 INP_XER, /* -ixer: XER input */
51 INP_PER /* -iper: Unaligned PER input */
vlm6d44a542005-11-08 03:06:16 +000052} iform; /* -i<format> */
53
54/* Output data format selector */
55static enum output_format {
56 OUT_XER, /* -oxer: XER (XML) output */
vlm6ed22c62006-08-25 04:56:21 +000057 OUT_DER, /* -oder: DER (BER) output */
58 OUT_PER, /* -oper: Unaligned PER output */
vlm6d44a542005-11-08 03:06:16 +000059 OUT_TEXT, /* -otext: semi-structured text */
60 OUT_NULL /* -onull: No pretty-printing */
61} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000062
vlm4d2ca122005-12-07 05:46:03 +000063/* Debug output function */
64static inline void
65DEBUG(const char *fmt, ...) {
66 va_list ap;
67 if(!opt_debug) return;
68 fprintf(stderr, "AD: ");
69 va_start(ap, fmt);
70 vfprintf(stderr, fmt, ap);
71 va_end(ap);
72 fprintf(stderr, "\n");
73}
vlm0e329f22004-10-28 13:22:54 +000074
75int
vlme0fb1e82006-09-15 18:33:25 +000076main(int ac, char *av[]) {
vlmb51ebb22006-08-25 02:35:08 +000077 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
vlm0e329f22004-10-28 13:22:54 +000078 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
79 int number_of_iterations = 1;
80 int num;
81 int ch;
82
vlm337167e2005-11-26 11:25:14 +000083 /* Figure out if Unaligned PER needs to be default */
84 if(pduType->uper_decoder)
85 iform = INP_PER;
86
vlm0e329f22004-10-28 13:22:54 +000087 /*
88 * Pocess the command-line argments.
89 */
vlm337167e2005-11-26 11:25:14 +000090 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
vlm0e329f22004-10-28 13:22:54 +000091 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +000092 case 'i':
93 if(optarg[0] == 'b') { iform = INP_BER; break; }
94 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +000095 if(pduType->uper_decoder
96 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6ed22c62006-08-25 04:56:21 +000097 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +000098 optarg);
99 exit(EX_UNAVAILABLE);
100 case 'o':
101 if(optarg[0] == 'd') { oform = OUT_DER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000102 if(pduType->uper_encoder
103 && optarg[0] == 'p') { oform = OUT_PER; break; }
vlm6d44a542005-11-08 03:06:16 +0000104 if(optarg[0] == 'x') { oform = OUT_XER; break; }
105 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
106 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
vlm6ed22c62006-08-25 04:56:21 +0000107 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000108 optarg);
109 exit(EX_UNAVAILABLE);
vlm337167e2005-11-26 11:25:14 +0000110 case 'p':
111#ifdef ASN_PDU_COLLECTION
112 {
113 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
114 if(optarg[0] < 'A' || optarg[0] > 'Z') {
115 fprintf(stderr, "Available PDU types:\n");
116 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
117 exit(0);
118 }
119 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
120 if(*pdu) { pduType = *pdu; break; }
121 }
122#endif /* ASN_PDU_COLLECTION */
123 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
124 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +0000125 case 'b':
126 suggested_bufsize = atoi(optarg);
127 if(suggested_bufsize < 1
128 || suggested_bufsize > 16 * 1024 * 1024) {
129 fprintf(stderr,
130 "-b %s: Improper buffer size (1..16M)\n",
131 optarg);
132 exit(EX_UNAVAILABLE);
133 }
134 break;
135 case 'c':
136 opt_check = 1;
137 break;
138 case 'd':
139 opt_debug++; /* Double -dd means ASN.1 debug */
140 break;
141 case 'n':
142 number_of_iterations = atoi(optarg);
143 if(number_of_iterations < 1) {
144 fprintf(stderr,
145 "-n %s: Improper iterations count\n", optarg);
146 exit(EX_UNAVAILABLE);
147 }
148 break;
vlm0e329f22004-10-28 13:22:54 +0000149 case 's':
150 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000151 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000152 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000153 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000154 optarg);
155 exit(EX_UNAVAILABLE);
156 }
157 break;
vlm0e329f22004-10-28 13:22:54 +0000158 case 'h':
159 default:
vlm337167e2005-11-26 11:25:14 +0000160 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
161 fprintf(stderr, "Where options are:\n");
162 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000163 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000164 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000165 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000166 " -iber Input is in BER (Basic Encoding Rules)%s\n",
167 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000168 fprintf(stderr,
vlm6ed22c62006-08-25 04:56:21 +0000169 " -ixer Input is in XER (XML Encoding Rules)\n");
170 if(pduType->uper_encoder)
171 fprintf(stderr,
172 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
173 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000174 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000175 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000176 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000177 " -onull Verify (decode) input, but do not output\n");
178#ifdef ASN_PDU_COLLECTION
179 fprintf(stderr,
180 " -p <PDU> Specify PDU type to decode\n"
181 " -p list List available PDUs\n");
182#endif /* ASN_PDU_COLLECTION */
183 fprintf(stderr,
vlm0e329f22004-10-28 13:22:54 +0000184 " -b <size> Set the i/o buffer size (default is %ld)\n"
185 " -c Check ASN.1 constraints after decoding\n"
186 " -d Enable debugging (-dd is even better)\n"
187 " -n <num> Process files <num> times\n"
vlm4d2ca122005-12-07 05:46:03 +0000188 " -s <size> Set the stack usage limit (default is %d)\n"
189 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000190 exit(EX_USAGE);
191 }
192
193 ac -= optind;
194 av += optind;
195
196 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000197 fprintf(stderr, "%s: No input files specified. "
198 "Try '-h' for more information\n",
199 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000200 exit(EX_USAGE);
201 }
202
203 setvbuf(stdout, 0, _IOLBF, 0);
204
205 for(num = 0; num < number_of_iterations; num++) {
206 int ac_i;
207 /*
208 * Process all files in turn.
209 */
210 for(ac_i = 0; ac_i < ac; ac_i++) {
vlm6d44a542005-11-08 03:06:16 +0000211 asn_enc_rval_t erv;
vlme0fb1e82006-09-15 18:33:25 +0000212 void *structure; /* Decoded structure */
213 FILE *file = argument_to_file(av, ac_i);
214 char *name = argument_to_name(av, ac_i);
vlm0e329f22004-10-28 13:22:54 +0000215
216 /*
217 * Decode the encoded structure from file.
218 */
vlme3470e72005-03-10 13:39:03 +0000219 structure = data_decode_from_file(pduType,
vlme0fb1e82006-09-15 18:33:25 +0000220 file, name, suggested_bufsize);
221 if(file && file != stdin) fclose(file);
vlm0e329f22004-10-28 13:22:54 +0000222 if(!structure) {
223 /* Error message is already printed */
224 exit(EX_DATAERR);
225 }
226
vlm0e329f22004-10-28 13:22:54 +0000227 /* Check ASN.1 constraints */
228 if(opt_check) {
229 char errbuf[128];
230 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000231 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000232 errbuf, &errlen)) {
233 fprintf(stderr, "%s: ASN.1 constraint "
vlme0fb1e82006-09-15 18:33:25 +0000234 "check failed: %s\n", name, errbuf);
vlm0e329f22004-10-28 13:22:54 +0000235 exit(EX_DATAERR);
236 }
237 }
238
vlm6d44a542005-11-08 03:06:16 +0000239 switch(oform) {
240 case OUT_NULL:
vlme0fb1e82006-09-15 18:33:25 +0000241 fprintf(stderr, "%s: decoded successfully\n", name);
vlm6d44a542005-11-08 03:06:16 +0000242 break;
243 case OUT_TEXT: /* -otext */
244 asn_fprint(stdout, pduType, structure);
245 break;
246 case OUT_XER: /* -oxer */
247 if(xer_fprint(stdout, pduType, structure)) {
248 fprintf(stderr, "%s: Cannot convert into XML\n",
vlme0fb1e82006-09-15 18:33:25 +0000249 name);
vlm6d44a542005-11-08 03:06:16 +0000250 exit(EX_UNAVAILABLE);
251 }
252 break;
253 case OUT_DER:
254 erv = der_encode(pduType, structure, write_out, stdout);
255 if(erv.encoded < 0) {
256 fprintf(stderr, "%s: Cannot convert into DER\n",
vlme0fb1e82006-09-15 18:33:25 +0000257 name);
vlm6d44a542005-11-08 03:06:16 +0000258 exit(EX_UNAVAILABLE);
259 }
260 break;
vlm6ed22c62006-08-25 04:56:21 +0000261 case OUT_PER:
262 erv = uper_encode(pduType, structure, write_out, stdout);
263 if(erv.encoded < 0) {
264 fprintf(stderr, "%s: Cannot convert into Unaligned PER\n",
vlme0fb1e82006-09-15 18:33:25 +0000265 name);
vlm6ed22c62006-08-25 04:56:21 +0000266 exit(EX_UNAVAILABLE);
267 }
268 break;
vlm6d44a542005-11-08 03:06:16 +0000269 }
270
vlmbe78c802006-03-17 02:11:12 +0000271 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000272 }
273 }
274
275 return 0;
276}
277
vlmf5aff922006-09-13 04:02:00 +0000278static struct dynamic_buffer {
279 char *data; /* Pointer to the data bytes */
280 size_t offset; /* Offset from the start */
281 size_t length; /* Length of meaningful contents */
282 size_t allocated; /* Allocated memory for data */
283 int nreallocs; /* Number of data reallocations */
284 off_t bytes_shifted; /* Number of bytes ever shifted */
285} DynamicBuffer;
vlm0e329f22004-10-28 13:22:54 +0000286
287/*
vlm6d44a542005-11-08 03:06:16 +0000288 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000289 */
vlmf5aff922006-09-13 04:02:00 +0000290static void add_bytes_to_buffer(const void *data2add, size_t bySize) {
vlm0e329f22004-10-28 13:22:54 +0000291
vlmf5aff922006-09-13 04:02:00 +0000292 DEBUG("add_bytes(%ld) { o=%ld l=%ld s=%ld }",
293 (long)bySize,
294 (long)DynamicBuffer.offset,
295 (long)DynamicBuffer.length,
296 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000297
vlmf5aff922006-09-13 04:02:00 +0000298 if(DynamicBuffer.allocated
299 >= (DynamicBuffer.offset + DynamicBuffer.length + bySize)) {
vlmcb923822006-09-13 04:14:17 +0000300 DEBUG("\tNo buffer reallocation is necessary");
vlmf5aff922006-09-13 04:02:00 +0000301 } else if(bySize <= DynamicBuffer.offset) {
302 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
vlm0e329f22004-10-28 13:22:54 +0000303
304 /* Shift the buffer contents */
vlmf5aff922006-09-13 04:02:00 +0000305 memmove(DynamicBuffer.data,
306 DynamicBuffer.data + DynamicBuffer.offset,
307 DynamicBuffer.length);
308 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
309 DynamicBuffer.offset = 0;
vlm0e329f22004-10-28 13:22:54 +0000310 } else {
vlmf5aff922006-09-13 04:02:00 +0000311 size_t newsize = (DynamicBuffer.allocated << 2) + bySize;
312 void *p = MALLOC(newsize);
vlm4d2ca122005-12-07 05:46:03 +0000313 if(!p) {
vlmf5aff922006-09-13 04:02:00 +0000314 perror("malloc()");
vlm0e329f22004-10-28 13:22:54 +0000315 exit(EX_OSERR);
316 }
vlmf5aff922006-09-13 04:02:00 +0000317 memcpy(p, DynamicBuffer.data, DynamicBuffer.length);
318 FREEMEM(DynamicBuffer.data);
319 DynamicBuffer.data = (char *)p;
320 DynamicBuffer.offset = 0;
321 DynamicBuffer.allocated = newsize;
322 DynamicBuffer.nreallocs++;
vlmcb923822006-09-13 04:14:17 +0000323 DEBUG("\tBuffer reallocated to %ld (%d time)",
vlmf5aff922006-09-13 04:02:00 +0000324 newsize, DynamicBuffer.nreallocs);
vlm0e329f22004-10-28 13:22:54 +0000325 }
vlm4d2ca122005-12-07 05:46:03 +0000326
vlmf5aff922006-09-13 04:02:00 +0000327 memcpy(DynamicBuffer.data + DynamicBuffer.offset + DynamicBuffer.length,
328 data2add, bySize);
329 DynamicBuffer.length += bySize;
vlm0e329f22004-10-28 13:22:54 +0000330}
331
vlme0fb1e82006-09-15 18:33:25 +0000332static void *
333data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *filename, ssize_t suggested_bufsize) {
vlm0e329f22004-10-28 13:22:54 +0000334 static char *fbuf;
335 static ssize_t fbuf_size;
336 static asn_codec_ctx_t s_codec_ctx;
337 asn_codec_ctx_t *opt_codec_ctx = 0;
338 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000339 asn_dec_rval_t rval;
vlm0e329f22004-10-28 13:22:54 +0000340 size_t rd;
vlme0fb1e82006-09-15 18:33:25 +0000341
342 if(!file) {
343 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
344 return 0;
345 }
vlm0e329f22004-10-28 13:22:54 +0000346
347 if(opt_stack) {
348 s_codec_ctx.max_stack_size = opt_stack;
349 opt_codec_ctx = &s_codec_ctx;
350 }
351
vlme0fb1e82006-09-15 18:33:25 +0000352 DEBUG("Processing %s", filename);
vlm0e329f22004-10-28 13:22:54 +0000353
354 /* prepare the file buffer */
355 if(fbuf_size != suggested_bufsize) {
vlmf5aff922006-09-13 04:02:00 +0000356 fbuf = (char *)REALLOC(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000357 if(!fbuf) {
358 perror("realloc()");
359 exit(EX_OSERR);
360 }
361 fbuf_size = suggested_bufsize;
362 }
363
vlmf5aff922006-09-13 04:02:00 +0000364 DynamicBuffer.offset = 0;
365 DynamicBuffer.length = 0;
366 DynamicBuffer.allocated = 0;
367 DynamicBuffer.bytes_shifted = 0;
368 DynamicBuffer.nreallocs = 0;
vlm0e329f22004-10-28 13:22:54 +0000369
vlm6d44a542005-11-08 03:06:16 +0000370 /* Pretend immediate EOF */
371 rval.code = RC_WMORE;
372 rval.consumed = 0;
373
vlme0fb1e82006-09-15 18:33:25 +0000374 while((rd = fread(fbuf, 1, fbuf_size, file)) || !feof(file)) {
vlm6d44a542005-11-08 03:06:16 +0000375 char *i_bptr;
376 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000377
378 /*
379 * Copy the data over, or use the original buffer.
380 */
vlmf5aff922006-09-13 04:02:00 +0000381 if(DynamicBuffer.allocated) {
vlm0e329f22004-10-28 13:22:54 +0000382 /* Append the new data into the intermediate buffer */
vlmf5aff922006-09-13 04:02:00 +0000383 add_bytes_to_buffer(fbuf, rd);
384 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
vlmcb923822006-09-13 04:14:17 +0000385 i_size = DynamicBuffer.length;
vlm0e329f22004-10-28 13:22:54 +0000386 } else {
vlm6d44a542005-11-08 03:06:16 +0000387 i_bptr = fbuf;
388 i_size = rd;
389 }
vlm0e329f22004-10-28 13:22:54 +0000390
vlm6d44a542005-11-08 03:06:16 +0000391 switch(iform) {
392 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000393 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000394 (void **)&structure, i_bptr, i_size);
395 break;
396 case INP_XER:
397 rval = xer_decode(opt_codec_ctx, pduType,
398 (void **)&structure, i_bptr, i_size);
399 break;
vlm337167e2005-11-26 11:25:14 +0000400 case INP_PER:
vlm4d2ca122005-12-07 05:46:03 +0000401 rval = uper_decode(opt_codec_ctx, pduType,
402 (void **)&structure, i_bptr, i_size);
vlm337167e2005-11-26 11:25:14 +0000403 break;
vlm6d44a542005-11-08 03:06:16 +0000404 }
vlm4d2ca122005-12-07 05:46:03 +0000405 DEBUG("decode(%ld) consumed %ld (%ld), code %d",
vlmf5aff922006-09-13 04:02:00 +0000406 (long)DynamicBuffer.length,
407 (long)rval.consumed, (long)i_size,
vlm4d2ca122005-12-07 05:46:03 +0000408 rval.code);
vlm0e329f22004-10-28 13:22:54 +0000409
vlmf5aff922006-09-13 04:02:00 +0000410 if(DynamicBuffer.allocated == 0) {
vlm0e329f22004-10-28 13:22:54 +0000411 /*
vlmf5aff922006-09-13 04:02:00 +0000412 * Flush the remainder into the intermediate buffer.
vlm0e329f22004-10-28 13:22:54 +0000413 */
414 if(rval.code != RC_FAIL && rval.consumed < rd) {
vlmf5aff922006-09-13 04:02:00 +0000415 add_bytes_to_buffer(fbuf + rval.consumed,
vlm4d2ca122005-12-07 05:46:03 +0000416 rd - rval.consumed);
417 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000418 }
419 }
420
421 switch(rval.code) {
422 case RC_OK:
vlm4d2ca122005-12-07 05:46:03 +0000423 DEBUG("RC_OK, finishing up with %ld",
424 (long)rval.consumed);
vlm0e329f22004-10-28 13:22:54 +0000425 return structure;
426 case RC_WMORE:
vlm6d44a542005-11-08 03:06:16 +0000427 /*
428 * Adjust position inside the source buffer.
429 */
vlmf5aff922006-09-13 04:02:00 +0000430 if(DynamicBuffer.allocated) {
431 DynamicBuffer.offset += rval.consumed;
432 DynamicBuffer.length -= rval.consumed;
433 }
vlmcb923822006-09-13 04:14:17 +0000434 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
435 (long)rval.consumed,
436 (long)DynamicBuffer.offset,
437 (long)DynamicBuffer.length,
438 (long)DynamicBuffer.allocated);
vlm6d44a542005-11-08 03:06:16 +0000439 rval.consumed = 0;
vlm0e329f22004-10-28 13:22:54 +0000440 continue;
441 case RC_FAIL:
442 break;
443 }
444 break;
445 }
446
vlm0e329f22004-10-28 13:22:54 +0000447 /* Clean up partially decoded structure */
vlmbe78c802006-03-17 02:11:12 +0000448 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000449
450 fprintf(stderr, "%s: "
vlm6d44a542005-11-08 03:06:16 +0000451 "Decode failed past byte %ld: %s\n",
vlme0fb1e82006-09-15 18:33:25 +0000452 filename, (long)(DynamicBuffer.bytes_shifted
vlmf5aff922006-09-13 04:02:00 +0000453 + DynamicBuffer.offset + rval.consumed),
vlm6d44a542005-11-08 03:06:16 +0000454 (rval.code == RC_WMORE)
455 ? "Unexpected end of input"
456 : "Input processing error");
vlm0e329f22004-10-28 13:22:54 +0000457
458 return 0;
459}
460
vlmf5aff922006-09-13 04:02:00 +0000461/* Dump the buffer out to the specified FILE */
462static int write_out(const void *buffer, size_t size, void *key) {
463 FILE *fp = (FILE *)key;
464 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
465}
vlme0fb1e82006-09-15 18:33:25 +0000466
467static int argument_is_stdin(char *av[], int idx) {
468 if(strcmp(av[idx], "-")) {
469 return 0; /* Certainly not <stdin> */
470 } else {
471 /* This might be <stdin>, unless `./program -- -` */
472 if(strcmp(av[-1], "--"))
473 return 1;
474 else
475 return 0;
476 }
477}
478
479static FILE *argument_to_file(char *av[], int idx) {
480 return argument_is_stdin(av, idx)
481 ? stdin
482 : fopen(av[idx], "r");
483}
484
485static char *argument_to_name(char *av[], int idx) {
486 return argument_is_stdin(av, idx)
487 ? "standard input"
488 : av[idx];
489}