blob: d91bb9e7b2de5cb97e0d3bd72a5d806bd066ad33 [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,
Lev Walkinbc691772006-09-17 11:02:53 +000038 FILE *file, const char *name, ssize_t suggested_bufsize, int first_pdu);
Lev Walkind1bfea62005-11-08 03:06:16 +000039static int write_out(const void *buffer, size_t size, void *key);
Lev Walkinc744a022006-09-15 18:33:25 +000040static FILE *argument_to_file(char *av[], int idx);
41static char *argument_to_name(char *av[], int idx);
Lev Walkin22b54552004-10-28 13:22:54 +000042
43 int opt_debug; /* -d */
44static int opt_check; /* -c */
Lev Walkin22b54552004-10-28 13:22:54 +000045static int opt_stack; /* -s */
Lev Walkinbc691772006-09-17 11:02:53 +000046static int opt_onepdu; /* -1 */
Lev Walkind1bfea62005-11-08 03:06:16 +000047
48/* Input data format selector */
49static enum input_format {
50 INP_BER, /* -iber: BER input */
Lev Walkin59b176e2005-11-26 11:25:14 +000051 INP_XER, /* -ixer: XER input */
52 INP_PER /* -iper: Unaligned PER input */
Lev Walkind1bfea62005-11-08 03:06:16 +000053} iform; /* -i<format> */
54
55/* Output data format selector */
56static enum output_format {
57 OUT_XER, /* -oxer: XER (XML) output */
Lev Walkinbe3a0c52006-08-25 04:56:21 +000058 OUT_DER, /* -oder: DER (BER) output */
59 OUT_PER, /* -oper: Unaligned PER output */
Lev Walkind1bfea62005-11-08 03:06:16 +000060 OUT_TEXT, /* -otext: semi-structured text */
61 OUT_NULL /* -onull: No pretty-printing */
62} oform; /* -o<format> */
Lev Walkin22b54552004-10-28 13:22:54 +000063
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000064/* Debug output function */
65static inline void
66DEBUG(const char *fmt, ...) {
67 va_list ap;
68 if(!opt_debug) return;
69 fprintf(stderr, "AD: ");
70 va_start(ap, fmt);
71 vfprintf(stderr, fmt, ap);
72 va_end(ap);
73 fprintf(stderr, "\n");
74}
Lev Walkin22b54552004-10-28 13:22:54 +000075
76int
Lev Walkinc744a022006-09-15 18:33:25 +000077main(int ac, char *av[]) {
Lev Walkind0625682006-08-25 02:35:08 +000078 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
Lev Walkin22b54552004-10-28 13:22:54 +000079 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
80 int number_of_iterations = 1;
81 int num;
82 int ch;
83
Lev Walkin59b176e2005-11-26 11:25:14 +000084 /* Figure out if Unaligned PER needs to be default */
85 if(pduType->uper_decoder)
86 iform = INP_PER;
87
Lev Walkin22b54552004-10-28 13:22:54 +000088 /*
89 * Pocess the command-line argments.
90 */
Lev Walkin59b176e2005-11-26 11:25:14 +000091 while((ch = getopt(ac, av, "i:o:b:cdn:p:hs:")) != -1)
Lev Walkin22b54552004-10-28 13:22:54 +000092 switch(ch) {
Lev Walkind1bfea62005-11-08 03:06:16 +000093 case 'i':
94 if(optarg[0] == 'b') { iform = INP_BER; break; }
95 if(optarg[0] == 'x') { iform = INP_XER; break; }
Lev Walkin59b176e2005-11-26 11:25:14 +000096 if(pduType->uper_decoder
97 && optarg[0] == 'p') { iform = INP_PER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +000098 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +000099 optarg);
100 exit(EX_UNAVAILABLE);
101 case 'o':
102 if(optarg[0] == 'd') { oform = OUT_DER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000103 if(pduType->uper_encoder
104 && optarg[0] == 'p') { oform = OUT_PER; break; }
Lev Walkind1bfea62005-11-08 03:06:16 +0000105 if(optarg[0] == 'x') { oform = OUT_XER; break; }
106 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
107 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000108 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000109 optarg);
110 exit(EX_UNAVAILABLE);
Lev Walkinbc691772006-09-17 11:02:53 +0000111 case '1':
112 opt_onepdu = 1;
113 break;
Lev Walkin22b54552004-10-28 13:22:54 +0000114 case 'b':
115 suggested_bufsize = atoi(optarg);
116 if(suggested_bufsize < 1
117 || suggested_bufsize > 16 * 1024 * 1024) {
118 fprintf(stderr,
119 "-b %s: Improper buffer size (1..16M)\n",
120 optarg);
121 exit(EX_UNAVAILABLE);
122 }
123 break;
124 case 'c':
125 opt_check = 1;
126 break;
127 case 'd':
128 opt_debug++; /* Double -dd means ASN.1 debug */
129 break;
130 case 'n':
131 number_of_iterations = atoi(optarg);
132 if(number_of_iterations < 1) {
133 fprintf(stderr,
134 "-n %s: Improper iterations count\n", optarg);
135 exit(EX_UNAVAILABLE);
136 }
137 break;
Lev Walkinbc691772006-09-17 11:02:53 +0000138 case 'p':
139#ifdef ASN_PDU_COLLECTION
140 {
141 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
142 if(optarg[0] < 'A' || optarg[0] > 'Z') {
143 fprintf(stderr, "Available PDU types:\n");
144 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
145 exit(0);
146 }
147 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
148 if(*pdu) { pduType = *pdu; break; }
149 }
150#endif /* ASN_PDU_COLLECTION */
151 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
152 exit(EX_UNAVAILABLE);
Lev Walkin22b54552004-10-28 13:22:54 +0000153 case 's':
154 opt_stack = atoi(optarg);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000155 if(opt_stack < 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000156 fprintf(stderr,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000157 "-s %s: Non-negative value expected\n",
Lev Walkin22b54552004-10-28 13:22:54 +0000158 optarg);
159 exit(EX_UNAVAILABLE);
160 }
161 break;
Lev Walkin22b54552004-10-28 13:22:54 +0000162 case 'h':
163 default:
Lev Walkin59b176e2005-11-26 11:25:14 +0000164 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
165 fprintf(stderr, "Where options are:\n");
166 if(pduType->uper_decoder)
Lev Walkin22b54552004-10-28 13:22:54 +0000167 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000168 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000169 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000170 " -iber Input is in BER (Basic Encoding Rules)%s\n",
171 iform == INP_PER ? "" : " (DEFAULT)");
Lev Walkin59b176e2005-11-26 11:25:14 +0000172 fprintf(stderr,
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000173 " -ixer Input is in XER (XML Encoding Rules)\n");
174 if(pduType->uper_encoder)
175 fprintf(stderr,
176 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
177 fprintf(stderr,
Lev Walkind1bfea62005-11-08 03:06:16 +0000178 " -oder Output in DER (Distinguished Encoding Rules)\n"
Lev Walkin857d11b2006-02-22 08:54:49 +0000179 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
Lev Walkind1bfea62005-11-08 03:06:16 +0000180 " -otext Output in plain semi-structured text (dump)\n"
Lev Walkin59b176e2005-11-26 11:25:14 +0000181 " -onull Verify (decode) input, but do not output\n");
182#ifdef ASN_PDU_COLLECTION
183 fprintf(stderr,
184 " -p <PDU> Specify PDU type to decode\n"
185 " -p list List available PDUs\n");
186#endif /* ASN_PDU_COLLECTION */
187 fprintf(stderr,
Lev Walkin22b54552004-10-28 13:22:54 +0000188 " -b <size> Set the i/o buffer size (default is %ld)\n"
189 " -c Check ASN.1 constraints after decoding\n"
190 " -d Enable debugging (-dd is even better)\n"
191 " -n <num> Process files <num> times\n"
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000192 " -s <size> Set the stack usage limit (default is %d)\n"
193 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
Lev Walkin22b54552004-10-28 13:22:54 +0000194 exit(EX_USAGE);
195 }
196
197 ac -= optind;
198 av += optind;
199
200 if(ac < 1) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000201 fprintf(stderr, "%s: No input files specified. "
202 "Try '-h' for more information\n",
203 av[-optind]);
Lev Walkin22b54552004-10-28 13:22:54 +0000204 exit(EX_USAGE);
205 }
206
207 setvbuf(stdout, 0, _IOLBF, 0);
208
209 for(num = 0; num < number_of_iterations; num++) {
210 int ac_i;
211 /*
212 * Process all files in turn.
213 */
214 for(ac_i = 0; ac_i < ac; ac_i++) {
Lev Walkinbc691772006-09-17 11:02:53 +0000215 asn_enc_rval_t erv;
216 void *structure; /* Decoded structure */
217 FILE *file = argument_to_file(av, ac_i);
218 char *name = argument_to_name(av, ac_i);
219 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000220
Lev Walkinbc691772006-09-17 11:02:53 +0000221 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000222 /*
223 * Decode the encoded structure from file.
224 */
Lev Walkin00949562005-03-10 13:39:03 +0000225 structure = data_decode_from_file(pduType,
Lev Walkinbc691772006-09-17 11:02:53 +0000226 file, name, suggested_bufsize, first_pdu);
Lev Walkin22b54552004-10-28 13:22:54 +0000227 if(!structure) {
Lev Walkinbc691772006-09-17 11:02:53 +0000228 if(errno) {
229 /* Error message is already printed */
230 exit(EX_DATAERR);
231 } else {
232 /* EOF */
233 break;
234 }
Lev Walkin22b54552004-10-28 13:22:54 +0000235 }
236
Lev Walkin22b54552004-10-28 13:22:54 +0000237 /* Check ASN.1 constraints */
238 if(opt_check) {
239 char errbuf[128];
240 size_t errlen = sizeof(errbuf);
Lev Walkin00949562005-03-10 13:39:03 +0000241 if(asn_check_constraints(pduType, structure,
Lev Walkin22b54552004-10-28 13:22:54 +0000242 errbuf, &errlen)) {
243 fprintf(stderr, "%s: ASN.1 constraint "
Lev Walkinc744a022006-09-15 18:33:25 +0000244 "check failed: %s\n", name, errbuf);
Lev Walkin22b54552004-10-28 13:22:54 +0000245 exit(EX_DATAERR);
246 }
247 }
248
Lev Walkind1bfea62005-11-08 03:06:16 +0000249 switch(oform) {
250 case OUT_NULL:
Lev Walkinc744a022006-09-15 18:33:25 +0000251 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000252 break;
253 case OUT_TEXT: /* -otext */
254 asn_fprint(stdout, pduType, structure);
255 break;
256 case OUT_XER: /* -oxer */
257 if(xer_fprint(stdout, pduType, structure)) {
258 fprintf(stderr, "%s: Cannot convert into XML\n",
Lev Walkinc744a022006-09-15 18:33:25 +0000259 name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000260 exit(EX_UNAVAILABLE);
261 }
262 break;
263 case OUT_DER:
264 erv = der_encode(pduType, structure, write_out, stdout);
265 if(erv.encoded < 0) {
266 fprintf(stderr, "%s: Cannot convert into DER\n",
Lev Walkinc744a022006-09-15 18:33:25 +0000267 name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000268 exit(EX_UNAVAILABLE);
269 }
Lev Walkinbc691772006-09-17 11:02:53 +0000270 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
Lev Walkind1bfea62005-11-08 03:06:16 +0000271 break;
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000272 case OUT_PER:
273 erv = uper_encode(pduType, structure, write_out, stdout);
274 if(erv.encoded < 0) {
275 fprintf(stderr, "%s: Cannot convert into Unaligned PER\n",
Lev Walkinc744a022006-09-15 18:33:25 +0000276 name);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000277 exit(EX_UNAVAILABLE);
278 }
Lev Walkinbc691772006-09-17 11:02:53 +0000279 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000280 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000281 }
282
Lev Walkinadcb5862006-03-17 02:11:12 +0000283 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinbc691772006-09-17 11:02:53 +0000284 }
285
286 if(file && file != stdin)
287 fclose(file);
Lev Walkin22b54552004-10-28 13:22:54 +0000288 }
289 }
290
291 return 0;
292}
293
Lev Walkin419f6752006-09-13 04:02:00 +0000294static struct dynamic_buffer {
295 char *data; /* Pointer to the data bytes */
296 size_t offset; /* Offset from the start */
297 size_t length; /* Length of meaningful contents */
Lev Walkinbc691772006-09-17 11:02:53 +0000298 size_t unbit; /* Unused bits in the last byte */
Lev Walkin419f6752006-09-13 04:02:00 +0000299 size_t allocated; /* Allocated memory for data */
300 int nreallocs; /* Number of data reallocations */
301 off_t bytes_shifted; /* Number of bytes ever shifted */
302} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000303
304/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000305 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000306 */
Lev Walkin419f6752006-09-13 04:02:00 +0000307static void add_bytes_to_buffer(const void *data2add, size_t bySize) {
Lev Walkin22b54552004-10-28 13:22:54 +0000308
Lev Walkin419f6752006-09-13 04:02:00 +0000309 DEBUG("add_bytes(%ld) { o=%ld l=%ld s=%ld }",
310 (long)bySize,
311 (long)DynamicBuffer.offset,
312 (long)DynamicBuffer.length,
313 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000314
Lev Walkin419f6752006-09-13 04:02:00 +0000315 if(DynamicBuffer.allocated
316 >= (DynamicBuffer.offset + DynamicBuffer.length + bySize)) {
Lev Walkinf9492a92006-09-13 04:14:17 +0000317 DEBUG("\tNo buffer reallocation is necessary");
Lev Walkin419f6752006-09-13 04:02:00 +0000318 } else if(bySize <= DynamicBuffer.offset) {
319 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000320
321 /* Shift the buffer contents */
Lev Walkin419f6752006-09-13 04:02:00 +0000322 memmove(DynamicBuffer.data,
323 DynamicBuffer.data + DynamicBuffer.offset,
324 DynamicBuffer.length);
325 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
326 DynamicBuffer.offset = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000327 } else {
Lev Walkin419f6752006-09-13 04:02:00 +0000328 size_t newsize = (DynamicBuffer.allocated << 2) + bySize;
329 void *p = MALLOC(newsize);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000330 if(!p) {
Lev Walkin419f6752006-09-13 04:02:00 +0000331 perror("malloc()");
Lev Walkin22b54552004-10-28 13:22:54 +0000332 exit(EX_OSERR);
333 }
Lev Walkine1baa4d2006-09-17 08:23:43 +0000334 memcpy(p,
335 DynamicBuffer.data + DynamicBuffer.offset,
336 DynamicBuffer.length);
Lev Walkin419f6752006-09-13 04:02:00 +0000337 FREEMEM(DynamicBuffer.data);
338 DynamicBuffer.data = (char *)p;
339 DynamicBuffer.offset = 0;
340 DynamicBuffer.allocated = newsize;
341 DynamicBuffer.nreallocs++;
Lev Walkinf9492a92006-09-13 04:14:17 +0000342 DEBUG("\tBuffer reallocated to %ld (%d time)",
Lev Walkin419f6752006-09-13 04:02:00 +0000343 newsize, DynamicBuffer.nreallocs);
Lev Walkin22b54552004-10-28 13:22:54 +0000344 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000345
Lev Walkin419f6752006-09-13 04:02:00 +0000346 memcpy(DynamicBuffer.data + DynamicBuffer.offset + DynamicBuffer.length,
347 data2add, bySize);
348 DynamicBuffer.length += bySize;
Lev Walkin22b54552004-10-28 13:22:54 +0000349}
350
Lev Walkinc744a022006-09-15 18:33:25 +0000351static void *
Lev Walkinbc691772006-09-17 11:02:53 +0000352data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) {
Lev Walkin22b54552004-10-28 13:22:54 +0000353 static char *fbuf;
354 static ssize_t fbuf_size;
355 static asn_codec_ctx_t s_codec_ctx;
356 asn_codec_ctx_t *opt_codec_ctx = 0;
357 void *structure = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000358 asn_dec_rval_t rval;
Lev Walkinbc691772006-09-17 11:02:53 +0000359 size_t old_offset;
360 size_t new_offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000361 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000362
363 if(!file) {
Lev Walkinbc691772006-09-17 11:02:53 +0000364 fprintf(stderr, "%s: %s\n", name, strerror(errno));
365 errno = EINVAL;
Lev Walkinc744a022006-09-15 18:33:25 +0000366 return 0;
367 }
Lev Walkin22b54552004-10-28 13:22:54 +0000368
369 if(opt_stack) {
370 s_codec_ctx.max_stack_size = opt_stack;
371 opt_codec_ctx = &s_codec_ctx;
372 }
373
Lev Walkinbc691772006-09-17 11:02:53 +0000374 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000375
376 /* prepare the file buffer */
377 if(fbuf_size != suggested_bufsize) {
Lev Walkin419f6752006-09-13 04:02:00 +0000378 fbuf = (char *)REALLOC(fbuf, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000379 if(!fbuf) {
380 perror("realloc()");
381 exit(EX_OSERR);
382 }
383 fbuf_size = suggested_bufsize;
384 }
385
Lev Walkinbc691772006-09-17 11:02:53 +0000386 if(on_first_pdu) {
387 DynamicBuffer.offset = 0;
388 DynamicBuffer.length = 0;
389 DynamicBuffer.unbit = 0;
390 DynamicBuffer.allocated = 0;
391 DynamicBuffer.bytes_shifted = 0;
392 DynamicBuffer.nreallocs = 0;
393 }
394
395 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000396
Lev Walkind1bfea62005-11-08 03:06:16 +0000397 /* Pretend immediate EOF */
398 rval.code = RC_WMORE;
399 rval.consumed = 0;
400
Lev Walkinc744a022006-09-15 18:33:25 +0000401 while((rd = fread(fbuf, 1, fbuf_size, file)) || !feof(file)) {
Lev Walkinbc691772006-09-17 11:02:53 +0000402 int ecbits = 0; /* Extra consumed bits in case of PER */
Lev Walkind1bfea62005-11-08 03:06:16 +0000403 char *i_bptr;
404 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000405
406 /*
407 * Copy the data over, or use the original buffer.
408 */
Lev Walkin419f6752006-09-13 04:02:00 +0000409 if(DynamicBuffer.allocated) {
Lev Walkin22b54552004-10-28 13:22:54 +0000410 /* Append the new data into the intermediate buffer */
Lev Walkin419f6752006-09-13 04:02:00 +0000411 add_bytes_to_buffer(fbuf, rd);
412 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
Lev Walkinf9492a92006-09-13 04:14:17 +0000413 i_size = DynamicBuffer.length;
Lev Walkin22b54552004-10-28 13:22:54 +0000414 } else {
Lev Walkind1bfea62005-11-08 03:06:16 +0000415 i_bptr = fbuf;
416 i_size = rd;
417 }
Lev Walkin22b54552004-10-28 13:22:54 +0000418
Lev Walkinbc691772006-09-17 11:02:53 +0000419 DEBUG("Decoding %ld bytes", (long)i_size);
420
Lev Walkind1bfea62005-11-08 03:06:16 +0000421 switch(iform) {
422 case INP_BER:
Lev Walkin00949562005-03-10 13:39:03 +0000423 rval = ber_decode(opt_codec_ctx, pduType,
Lev Walkind1bfea62005-11-08 03:06:16 +0000424 (void **)&structure, i_bptr, i_size);
425 break;
426 case INP_XER:
427 rval = xer_decode(opt_codec_ctx, pduType,
428 (void **)&structure, i_bptr, i_size);
429 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000430 case INP_PER:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000431 rval = uper_decode(opt_codec_ctx, pduType,
Lev Walkinbc691772006-09-17 11:02:53 +0000432 (void **)&structure, i_bptr, i_size, 0);
433 ecbits = rval.consumed % 8; /* Extra bits */
Lev Walkinbc691772006-09-17 11:02:53 +0000434 rval.consumed /= 8; /* Convert to value in bytes! */
Lev Walkin59b176e2005-11-26 11:25:14 +0000435 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000436 }
Lev Walkin4a858bb2006-09-17 11:22:00 +0000437 DEBUG("decode(%ld) consumed %ld+%d (%ld), code %d",
Lev Walkin419f6752006-09-13 04:02:00 +0000438 (long)DynamicBuffer.length,
Lev Walkin4a858bb2006-09-17 11:22:00 +0000439 (long)rval.consumed, ecbits, (long)i_size,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000440 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000441
Lev Walkin419f6752006-09-13 04:02:00 +0000442 if(DynamicBuffer.allocated == 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000443 /*
Lev Walkinbc691772006-09-17 11:02:53 +0000444 * Flush remainder into the intermediate buffer.
Lev Walkin22b54552004-10-28 13:22:54 +0000445 */
446 if(rval.code != RC_FAIL && rval.consumed < rd) {
Lev Walkinbc691772006-09-17 11:02:53 +0000447 DEBUG("Saving %d bytes", rd - rval.consumed);
Lev Walkin419f6752006-09-13 04:02:00 +0000448 add_bytes_to_buffer(fbuf + rval.consumed,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000449 rd - rval.consumed);
450 rval.consumed = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000451 }
452 }
453
Lev Walkinbc691772006-09-17 11:02:53 +0000454 /*
455 * Adjust position inside the source buffer.
456 */
457 if(DynamicBuffer.allocated) {
458 DynamicBuffer.offset += rval.consumed;
459 DynamicBuffer.length -= rval.consumed;
460 } else {
461 DynamicBuffer.bytes_shifted += rval.consumed;
462 }
463
Lev Walkin22b54552004-10-28 13:22:54 +0000464 switch(rval.code) {
465 case RC_OK:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000466 DEBUG("RC_OK, finishing up with %ld",
467 (long)rval.consumed);
Lev Walkin22b54552004-10-28 13:22:54 +0000468 return structure;
469 case RC_WMORE:
Lev Walkinf9492a92006-09-13 04:14:17 +0000470 DEBUG("RC_WMORE, continuing %ld with %ld..%ld..%ld",
471 (long)rval.consumed,
472 (long)DynamicBuffer.offset,
473 (long)DynamicBuffer.length,
474 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000475 continue;
476 case RC_FAIL:
477 break;
478 }
479 break;
480 }
481
Lev Walkin22b54552004-10-28 13:22:54 +0000482 /* Clean up partially decoded structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000483 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000484
Lev Walkinbc691772006-09-17 11:02:53 +0000485 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
486
487 /*
488 * Print a message and return failure only if not EOF,
489 * unless this is our first PDU (empty file).
490 */
491 if(on_first_pdu || new_offset != old_offset) {
492 fprintf(stderr, "%s: "
493 "Decode failed past byte %ld: %s\n",
494 name, (long)new_offset,
495 (rval.code == RC_WMORE)
496 ? "Unexpected end of input"
497 : "Input processing error");
498 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
499 } else {
500 /* Got EOF after a few successful PDU */
501 errno = 0;
502 }
Lev Walkin22b54552004-10-28 13:22:54 +0000503
504 return 0;
505}
506
Lev Walkin419f6752006-09-13 04:02:00 +0000507/* Dump the buffer out to the specified FILE */
508static int write_out(const void *buffer, size_t size, void *key) {
509 FILE *fp = (FILE *)key;
510 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
511}
Lev Walkinc744a022006-09-15 18:33:25 +0000512
513static int argument_is_stdin(char *av[], int idx) {
514 if(strcmp(av[idx], "-")) {
515 return 0; /* Certainly not <stdin> */
516 } else {
517 /* This might be <stdin>, unless `./program -- -` */
518 if(strcmp(av[-1], "--"))
519 return 1;
520 else
521 return 0;
522 }
523}
524
525static FILE *argument_to_file(char *av[], int idx) {
526 return argument_is_stdin(av, idx)
527 ? stdin
528 : fopen(av[idx], "r");
529}
530
531static char *argument_to_name(char *av[], int idx) {
532 return argument_is_stdin(av, idx)
533 ? "standard input"
534 : av[idx];
535}