blob: bc4aab87c1337827fd26955f50d8d7204072e22c [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
Lev Walkin5a621d62006-09-18 20:05:34 +000043 int opt_debug; /* -d (or -dd) */
44static int opt_check; /* -c (constraints checking) */
45static int opt_stack; /* -s (maximum stack size) */
46static int opt_ippad; /* -per-padded (PER input is byte-padded) */
47static int opt_onepdu; /* -1 (decode single PDU) */
Lev Walkind1bfea62005-11-08 03:06:16 +000048
49/* Input data format selector */
50static enum input_format {
51 INP_BER, /* -iber: BER input */
Lev Walkin59b176e2005-11-26 11:25:14 +000052 INP_XER, /* -ixer: XER input */
53 INP_PER /* -iper: Unaligned PER input */
Lev Walkind1bfea62005-11-08 03:06:16 +000054} iform; /* -i<format> */
55
56/* Output data format selector */
57static enum output_format {
58 OUT_XER, /* -oxer: XER (XML) output */
Lev Walkinbe3a0c52006-08-25 04:56:21 +000059 OUT_DER, /* -oder: DER (BER) output */
60 OUT_PER, /* -oper: Unaligned PER output */
Lev Walkind1bfea62005-11-08 03:06:16 +000061 OUT_TEXT, /* -otext: semi-structured text */
62 OUT_NULL /* -onull: No pretty-printing */
63} oform; /* -o<format> */
Lev Walkin22b54552004-10-28 13:22:54 +000064
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000065/* Debug output function */
66static inline void
67DEBUG(const char *fmt, ...) {
68 va_list ap;
69 if(!opt_debug) return;
70 fprintf(stderr, "AD: ");
71 va_start(ap, fmt);
72 vfprintf(stderr, fmt, ap);
73 va_end(ap);
74 fprintf(stderr, "\n");
75}
Lev Walkin22b54552004-10-28 13:22:54 +000076
77int
Lev Walkinc744a022006-09-15 18:33:25 +000078main(int ac, char *av[]) {
Lev Walkind0625682006-08-25 02:35:08 +000079 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
Lev Walkin22b54552004-10-28 13:22:54 +000080 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
81 int number_of_iterations = 1;
82 int num;
83 int ch;
84
Lev Walkin59b176e2005-11-26 11:25:14 +000085 /* Figure out if Unaligned PER needs to be default */
86 if(pduType->uper_decoder)
87 iform = INP_PER;
88
Lev Walkin22b54552004-10-28 13:22:54 +000089 /*
90 * Pocess the command-line argments.
91 */
Lev Walkin5a621d62006-09-18 20:05:34 +000092 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:")) != -1)
Lev Walkin22b54552004-10-28 13:22:54 +000093 switch(ch) {
Lev Walkind1bfea62005-11-08 03:06:16 +000094 case 'i':
95 if(optarg[0] == 'b') { iform = INP_BER; break; }
96 if(optarg[0] == 'x') { iform = INP_XER; break; }
Lev Walkin59b176e2005-11-26 11:25:14 +000097 if(pduType->uper_decoder
98 && optarg[0] == 'p') { iform = INP_PER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +000099 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000100 optarg);
101 exit(EX_UNAVAILABLE);
102 case 'o':
103 if(optarg[0] == 'd') { oform = OUT_DER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000104 if(pduType->uper_encoder
105 && optarg[0] == 'p') { oform = OUT_PER; break; }
Lev Walkind1bfea62005-11-08 03:06:16 +0000106 if(optarg[0] == 'x') { oform = OUT_XER; break; }
107 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
108 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000109 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000110 optarg);
111 exit(EX_UNAVAILABLE);
Lev Walkinbc691772006-09-17 11:02:53 +0000112 case '1':
113 opt_onepdu = 1;
114 break;
Lev Walkin22b54552004-10-28 13:22:54 +0000115 case 'b':
116 suggested_bufsize = atoi(optarg);
117 if(suggested_bufsize < 1
118 || suggested_bufsize > 16 * 1024 * 1024) {
119 fprintf(stderr,
120 "-b %s: Improper buffer size (1..16M)\n",
121 optarg);
122 exit(EX_UNAVAILABLE);
123 }
124 break;
125 case 'c':
126 opt_check = 1;
127 break;
128 case 'd':
129 opt_debug++; /* Double -dd means ASN.1 debug */
130 break;
131 case 'n':
132 number_of_iterations = atoi(optarg);
133 if(number_of_iterations < 1) {
134 fprintf(stderr,
135 "-n %s: Improper iterations count\n", optarg);
136 exit(EX_UNAVAILABLE);
137 }
138 break;
Lev Walkinbc691772006-09-17 11:02:53 +0000139 case 'p':
Lev Walkin5a621d62006-09-18 20:05:34 +0000140 if(strcmp(optarg, "er-padded") == 0) {
141 opt_ippad = 1;
142 break;
143 }
Lev Walkinbc691772006-09-17 11:02:53 +0000144#ifdef ASN_PDU_COLLECTION
Lev Walkin5a621d62006-09-18 20:05:34 +0000145 if(strcmp(optarg, "list") == 0) {
Lev Walkinbc691772006-09-17 11:02:53 +0000146 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
Lev Walkin5a621d62006-09-18 20:05:34 +0000147 fprintf(stderr, "Available PDU types:\n");
148 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
149 exit(0);
150 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
151 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
Lev Walkinbc691772006-09-17 11:02:53 +0000152 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
153 if(*pdu) { pduType = *pdu; break; }
Lev Walkin5a621d62006-09-18 20:05:34 +0000154 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkinbc691772006-09-17 11:02:53 +0000155 }
156#endif /* ASN_PDU_COLLECTION */
Lev Walkin5a621d62006-09-18 20:05:34 +0000157 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
Lev Walkinbc691772006-09-17 11:02:53 +0000158 exit(EX_UNAVAILABLE);
Lev Walkin22b54552004-10-28 13:22:54 +0000159 case 's':
160 opt_stack = atoi(optarg);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000161 if(opt_stack < 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000162 fprintf(stderr,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000163 "-s %s: Non-negative value expected\n",
Lev Walkin22b54552004-10-28 13:22:54 +0000164 optarg);
165 exit(EX_UNAVAILABLE);
166 }
167 break;
Lev Walkin22b54552004-10-28 13:22:54 +0000168 case 'h':
169 default:
Lev Walkin59b176e2005-11-26 11:25:14 +0000170 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
171 fprintf(stderr, "Where options are:\n");
172 if(pduType->uper_decoder)
Lev Walkin22b54552004-10-28 13:22:54 +0000173 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000174 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000175 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000176 " -iber Input is in BER (Basic Encoding Rules)%s\n",
177 iform == INP_PER ? "" : " (DEFAULT)");
Lev Walkin59b176e2005-11-26 11:25:14 +0000178 fprintf(stderr,
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000179 " -ixer Input is in XER (XML Encoding Rules)\n");
180 if(pduType->uper_encoder)
181 fprintf(stderr,
182 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
183 fprintf(stderr,
Lev Walkind1bfea62005-11-08 03:06:16 +0000184 " -oder Output in DER (Distinguished Encoding Rules)\n"
Lev Walkin857d11b2006-02-22 08:54:49 +0000185 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
Lev Walkind1bfea62005-11-08 03:06:16 +0000186 " -otext Output in plain semi-structured text (dump)\n"
Lev Walkin59b176e2005-11-26 11:25:14 +0000187 " -onull Verify (decode) input, but do not output\n");
Lev Walkin5a621d62006-09-18 20:05:34 +0000188 if(pduType->uper_decoder)
189 fprintf(stderr,
190 " -per-padded Assume PER PDUs are byte-padded (-iper)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000191#ifdef ASN_PDU_COLLECTION
192 fprintf(stderr,
193 " -p <PDU> Specify PDU type to decode\n"
194 " -p list List available PDUs\n");
195#endif /* ASN_PDU_COLLECTION */
196 fprintf(stderr,
Lev Walkin22b54552004-10-28 13:22:54 +0000197 " -b <size> Set the i/o buffer size (default is %ld)\n"
198 " -c Check ASN.1 constraints after decoding\n"
199 " -d Enable debugging (-dd is even better)\n"
200 " -n <num> Process files <num> times\n"
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000201 " -s <size> Set the stack usage limit (default is %d)\n"
202 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
Lev Walkin22b54552004-10-28 13:22:54 +0000203 exit(EX_USAGE);
204 }
205
206 ac -= optind;
207 av += optind;
208
209 if(ac < 1) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000210 fprintf(stderr, "%s: No input files specified. "
211 "Try '-h' for more information\n",
212 av[-optind]);
Lev Walkin22b54552004-10-28 13:22:54 +0000213 exit(EX_USAGE);
214 }
215
216 setvbuf(stdout, 0, _IOLBF, 0);
217
218 for(num = 0; num < number_of_iterations; num++) {
219 int ac_i;
220 /*
221 * Process all files in turn.
222 */
223 for(ac_i = 0; ac_i < ac; ac_i++) {
Lev Walkinbc691772006-09-17 11:02:53 +0000224 asn_enc_rval_t erv;
225 void *structure; /* Decoded structure */
226 FILE *file = argument_to_file(av, ac_i);
227 char *name = argument_to_name(av, ac_i);
228 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000229
Lev Walkinbc691772006-09-17 11:02:53 +0000230 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000231 /*
232 * Decode the encoded structure from file.
233 */
Lev Walkin00949562005-03-10 13:39:03 +0000234 structure = data_decode_from_file(pduType,
Lev Walkinbc691772006-09-17 11:02:53 +0000235 file, name, suggested_bufsize, first_pdu);
Lev Walkin22b54552004-10-28 13:22:54 +0000236 if(!structure) {
Lev Walkinbc691772006-09-17 11:02:53 +0000237 if(errno) {
238 /* Error message is already printed */
239 exit(EX_DATAERR);
240 } else {
241 /* EOF */
242 break;
243 }
Lev Walkin22b54552004-10-28 13:22:54 +0000244 }
245
Lev Walkin22b54552004-10-28 13:22:54 +0000246 /* Check ASN.1 constraints */
247 if(opt_check) {
248 char errbuf[128];
249 size_t errlen = sizeof(errbuf);
Lev Walkin00949562005-03-10 13:39:03 +0000250 if(asn_check_constraints(pduType, structure,
Lev Walkin22b54552004-10-28 13:22:54 +0000251 errbuf, &errlen)) {
252 fprintf(stderr, "%s: ASN.1 constraint "
Lev Walkinc744a022006-09-15 18:33:25 +0000253 "check failed: %s\n", name, errbuf);
Lev Walkin22b54552004-10-28 13:22:54 +0000254 exit(EX_DATAERR);
255 }
256 }
257
Lev Walkind1bfea62005-11-08 03:06:16 +0000258 switch(oform) {
259 case OUT_NULL:
Lev Walkinc744a022006-09-15 18:33:25 +0000260 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000261 break;
262 case OUT_TEXT: /* -otext */
263 asn_fprint(stdout, pduType, structure);
264 break;
265 case OUT_XER: /* -oxer */
266 if(xer_fprint(stdout, pduType, structure)) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000267 fprintf(stderr,
268 "%s: Cannot convert %s into XML\n",
269 name, pduType->name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000270 exit(EX_UNAVAILABLE);
271 }
272 break;
273 case OUT_DER:
274 erv = der_encode(pduType, structure, write_out, stdout);
275 if(erv.encoded < 0) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000276 fprintf(stderr,
277 "%s: Cannot convert %s into DER\n",
278 name, pduType->name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000279 exit(EX_UNAVAILABLE);
280 }
Lev Walkinbc691772006-09-17 11:02:53 +0000281 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
Lev Walkind1bfea62005-11-08 03:06:16 +0000282 break;
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000283 case OUT_PER:
284 erv = uper_encode(pduType, structure, write_out, stdout);
285 if(erv.encoded < 0) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000286 fprintf(stderr,
287 "%s: Cannot convert %s into Unaligned PER\n",
288 name, pduType->name);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000289 exit(EX_UNAVAILABLE);
290 }
Lev Walkinbc691772006-09-17 11:02:53 +0000291 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000292 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000293 }
294
Lev Walkinadcb5862006-03-17 02:11:12 +0000295 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinbc691772006-09-17 11:02:53 +0000296 }
297
298 if(file && file != stdin)
299 fclose(file);
Lev Walkin22b54552004-10-28 13:22:54 +0000300 }
301 }
302
303 return 0;
304}
305
Lev Walkin419f6752006-09-13 04:02:00 +0000306static struct dynamic_buffer {
Lev Walkin5a621d62006-09-18 20:05:34 +0000307 uint8_t *data; /* Pointer to the data bytes */
Lev Walkin419f6752006-09-13 04:02:00 +0000308 size_t offset; /* Offset from the start */
309 size_t length; /* Length of meaningful contents */
Lev Walkin5a621d62006-09-18 20:05:34 +0000310 size_t unbits; /* Unused bits in the last byte */
Lev Walkin419f6752006-09-13 04:02:00 +0000311 size_t allocated; /* Allocated memory for data */
312 int nreallocs; /* Number of data reallocations */
313 off_t bytes_shifted; /* Number of bytes ever shifted */
314} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000315
Lev Walkin5a621d62006-09-18 20:05:34 +0000316static void
317buffer_dump() {
318 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
319 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
320 if(!opt_debug) return;
321 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
322 DynamicBuffer.data,
323 (long)DynamicBuffer.offset,
324 (long)DynamicBuffer.length,
325 (long)DynamicBuffer.unbits,
326 (long)DynamicBuffer.allocated,
327 (long)DynamicBuffer.bytes_shifted);
328 for(; p < e; p++) {
329 fprintf(stderr, " %c%c%c%c%c%c%c%c",
330 ((*p >> 7) & 1) ? '1' : '0',
331 ((*p >> 6) & 1) ? '1' : '0',
332 ((*p >> 5) & 1) ? '1' : '0',
333 ((*p >> 4) & 1) ? '1' : '0',
334 ((*p >> 3) & 1) ? '1' : '0',
335 ((*p >> 2) & 1) ? '1' : '0',
336 ((*p >> 1) & 1) ? '1' : '0',
337 ((*p >> 0) & 1) ? '1' : '0');
338 }
339 if(DynamicBuffer.unbits) {
340 int shift;
341 fprintf(stderr, " ");
342 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
343 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
344 fprintf(stderr, " %d:%d\n",
345 DynamicBuffer.length - 1, 8 - DynamicBuffer.unbits);
346 } else {
347 fprintf(stderr, " %d\n", DynamicBuffer.length);
348 }
349}
350
351/*
352 * Move the buffer content left N bits, possibly joining it with
353 * preceeding content.
354 */
355static void
356buffer_shift_left(size_t offset, int bits) {
357 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
358 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
359 + DynamicBuffer.length - 1;
360
361 if(!bits) return;
362
363 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
364 bits, (long)offset,
365 (long)DynamicBuffer.offset,
366 (long)DynamicBuffer.unbits,
367 (long)DynamicBuffer.length);
368
369 if(offset) {
370 int right;
371 right = ptr[0] >> (8 - bits);
372
373 DEBUG("oleft: %c%c%c%c%c%c%c%c",
374 ((ptr[-1] >> 7) & 1) ? '1' : '0',
375 ((ptr[-1] >> 6) & 1) ? '1' : '0',
376 ((ptr[-1] >> 5) & 1) ? '1' : '0',
377 ((ptr[-1] >> 4) & 1) ? '1' : '0',
378 ((ptr[-1] >> 3) & 1) ? '1' : '0',
379 ((ptr[-1] >> 2) & 1) ? '1' : '0',
380 ((ptr[-1] >> 1) & 1) ? '1' : '0',
381 ((ptr[-1] >> 0) & 1) ? '1' : '0');
382
383 DEBUG("oriht: %c%c%c%c%c%c%c%c",
384 ((ptr[0] >> 7) & 1) ? '1' : '0',
385 ((ptr[0] >> 6) & 1) ? '1' : '0',
386 ((ptr[0] >> 5) & 1) ? '1' : '0',
387 ((ptr[0] >> 4) & 1) ? '1' : '0',
388 ((ptr[0] >> 3) & 1) ? '1' : '0',
389 ((ptr[0] >> 2) & 1) ? '1' : '0',
390 ((ptr[0] >> 1) & 1) ? '1' : '0',
391 ((ptr[0] >> 0) & 1) ? '1' : '0');
392
393 DEBUG("mriht: %c%c%c%c%c%c%c%c",
394 ((right >> 7) & 1) ? '1' : '0',
395 ((right >> 6) & 1) ? '1' : '0',
396 ((right >> 5) & 1) ? '1' : '0',
397 ((right >> 4) & 1) ? '1' : '0',
398 ((right >> 3) & 1) ? '1' : '0',
399 ((right >> 2) & 1) ? '1' : '0',
400 ((right >> 1) & 1) ? '1' : '0',
401 ((right >> 0) & 1) ? '1' : '0');
402
403 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
404
405 DEBUG("after: %c%c%c%c%c%c%c%c",
406 ((ptr[-1] >> 7) & 1) ? '1' : '0',
407 ((ptr[-1] >> 6) & 1) ? '1' : '0',
408 ((ptr[-1] >> 5) & 1) ? '1' : '0',
409 ((ptr[-1] >> 4) & 1) ? '1' : '0',
410 ((ptr[-1] >> 3) & 1) ? '1' : '0',
411 ((ptr[-1] >> 2) & 1) ? '1' : '0',
412 ((ptr[-1] >> 1) & 1) ? '1' : '0',
413 ((ptr[-1] >> 0) & 1) ? '1' : '0');
414 }
415
416 buffer_dump();
417
418 for(; ptr < end; ptr++) {
419 int right = ptr[1] >> (8 - bits);
420 *ptr = (*ptr << bits) | right;
421 }
422 *ptr <<= bits;
423
424 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
425 if(DynamicBuffer.unbits == 0) {
426 DynamicBuffer.unbits += bits;
427 } else {
428 DynamicBuffer.unbits += bits;
429 if(DynamicBuffer.unbits > 7) {
430 DynamicBuffer.unbits -= 8;
431 DynamicBuffer.length--;
432 DynamicBuffer.bytes_shifted++;
433 }
434 }
435 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
436
437 buffer_dump();
438
439 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
440 (long)DynamicBuffer.offset,
441 (long)DynamicBuffer.unbits,
442 (long)DynamicBuffer.length);
443
444
445}
446
Lev Walkin22b54552004-10-28 13:22:54 +0000447/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000448 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000449 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000450static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000451
Lev Walkin5a621d62006-09-18 20:05:34 +0000452 if(bytes == 0) return;
453
454 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
455 (long)bytes,
Lev Walkin419f6752006-09-13 04:02:00 +0000456 (long)DynamicBuffer.offset,
457 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000458 (long)DynamicBuffer.unbits,
Lev Walkin419f6752006-09-13 04:02:00 +0000459 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000460
Lev Walkin419f6752006-09-13 04:02:00 +0000461 if(DynamicBuffer.allocated
Lev Walkin5a621d62006-09-18 20:05:34 +0000462 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
Lev Walkinf9492a92006-09-13 04:14:17 +0000463 DEBUG("\tNo buffer reallocation is necessary");
Lev Walkin5a621d62006-09-18 20:05:34 +0000464 } else if(bytes <= DynamicBuffer.offset) {
Lev Walkin419f6752006-09-13 04:02:00 +0000465 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000466
467 /* Shift the buffer contents */
Lev Walkin419f6752006-09-13 04:02:00 +0000468 memmove(DynamicBuffer.data,
469 DynamicBuffer.data + DynamicBuffer.offset,
470 DynamicBuffer.length);
471 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
472 DynamicBuffer.offset = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000473 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000474 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
Lev Walkin419f6752006-09-13 04:02:00 +0000475 void *p = MALLOC(newsize);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000476 if(!p) {
Lev Walkin419f6752006-09-13 04:02:00 +0000477 perror("malloc()");
Lev Walkin22b54552004-10-28 13:22:54 +0000478 exit(EX_OSERR);
479 }
Lev Walkine1baa4d2006-09-17 08:23:43 +0000480 memcpy(p,
481 DynamicBuffer.data + DynamicBuffer.offset,
482 DynamicBuffer.length);
Lev Walkin419f6752006-09-13 04:02:00 +0000483 FREEMEM(DynamicBuffer.data);
484 DynamicBuffer.data = (char *)p;
485 DynamicBuffer.offset = 0;
486 DynamicBuffer.allocated = newsize;
487 DynamicBuffer.nreallocs++;
Lev Walkinf9492a92006-09-13 04:14:17 +0000488 DEBUG("\tBuffer reallocated to %ld (%d time)",
Lev Walkin419f6752006-09-13 04:02:00 +0000489 newsize, DynamicBuffer.nreallocs);
Lev Walkin22b54552004-10-28 13:22:54 +0000490 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000491
Lev Walkin5a621d62006-09-18 20:05:34 +0000492 memcpy(DynamicBuffer.data
493 + DynamicBuffer.offset + DynamicBuffer.length,
494 data2add, bytes);
495 DynamicBuffer.length += bytes;
496 if(DynamicBuffer.unbits) {
497 int bits = DynamicBuffer.unbits;
498 DynamicBuffer.unbits = 0;
499 buffer_shift_left(DynamicBuffer.length - bytes, bits);
500 }
501
502 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
503 (long)bytes,
504 (long)DynamicBuffer.offset,
505 (long)DynamicBuffer.length,
506 (long)DynamicBuffer.unbits,
507 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000508}
509
Lev Walkinc744a022006-09-15 18:33:25 +0000510static void *
Lev Walkinbc691772006-09-17 11:02:53 +0000511data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000512 static uint8_t *fbuf;
Lev Walkin22b54552004-10-28 13:22:54 +0000513 static ssize_t fbuf_size;
514 static asn_codec_ctx_t s_codec_ctx;
515 asn_codec_ctx_t *opt_codec_ctx = 0;
516 void *structure = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000517 asn_dec_rval_t rval;
Lev Walkinbc691772006-09-17 11:02:53 +0000518 size_t old_offset;
519 size_t new_offset;
Lev Walkin5a621d62006-09-18 20:05:34 +0000520 int tolerate_eof;
Lev Walkin22b54552004-10-28 13:22:54 +0000521 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000522
523 if(!file) {
Lev Walkinbc691772006-09-17 11:02:53 +0000524 fprintf(stderr, "%s: %s\n", name, strerror(errno));
525 errno = EINVAL;
Lev Walkinc744a022006-09-15 18:33:25 +0000526 return 0;
527 }
Lev Walkin22b54552004-10-28 13:22:54 +0000528
529 if(opt_stack) {
530 s_codec_ctx.max_stack_size = opt_stack;
531 opt_codec_ctx = &s_codec_ctx;
532 }
533
Lev Walkinbc691772006-09-17 11:02:53 +0000534 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000535
536 /* prepare the file buffer */
537 if(fbuf_size != suggested_bufsize) {
Lev Walkin419f6752006-09-13 04:02:00 +0000538 fbuf = (char *)REALLOC(fbuf, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000539 if(!fbuf) {
540 perror("realloc()");
541 exit(EX_OSERR);
542 }
543 fbuf_size = suggested_bufsize;
544 }
545
Lev Walkinbc691772006-09-17 11:02:53 +0000546 if(on_first_pdu) {
547 DynamicBuffer.offset = 0;
548 DynamicBuffer.length = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000549 DynamicBuffer.unbits = 0;
Lev Walkinbc691772006-09-17 11:02:53 +0000550 DynamicBuffer.allocated = 0;
551 DynamicBuffer.bytes_shifted = 0;
552 DynamicBuffer.nreallocs = 0;
553 }
554
555 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000556
Lev Walkind1bfea62005-11-08 03:06:16 +0000557 /* Pretend immediate EOF */
558 rval.code = RC_WMORE;
559 rval.consumed = 0;
560
Lev Walkin5a621d62006-09-18 20:05:34 +0000561 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
562 (rd = fread(fbuf, 1, fbuf_size, file))
563 || feof(file) == 0
564 || (tolerate_eof && DynamicBuffer.length)
565 ;) {
Lev Walkinbc691772006-09-17 11:02:53 +0000566 int ecbits = 0; /* Extra consumed bits in case of PER */
Lev Walkind1bfea62005-11-08 03:06:16 +0000567 char *i_bptr;
568 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000569
570 /*
571 * Copy the data over, or use the original buffer.
572 */
Lev Walkin419f6752006-09-13 04:02:00 +0000573 if(DynamicBuffer.allocated) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000574 /* Append new data into the existing dynamic buffer */
Lev Walkin419f6752006-09-13 04:02:00 +0000575 add_bytes_to_buffer(fbuf, rd);
576 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
Lev Walkinf9492a92006-09-13 04:14:17 +0000577 i_size = DynamicBuffer.length;
Lev Walkin22b54552004-10-28 13:22:54 +0000578 } else {
Lev Walkind1bfea62005-11-08 03:06:16 +0000579 i_bptr = fbuf;
580 i_size = rd;
581 }
Lev Walkin22b54552004-10-28 13:22:54 +0000582
Lev Walkinbc691772006-09-17 11:02:53 +0000583 DEBUG("Decoding %ld bytes", (long)i_size);
584
Lev Walkind1bfea62005-11-08 03:06:16 +0000585 switch(iform) {
586 case INP_BER:
Lev Walkin00949562005-03-10 13:39:03 +0000587 rval = ber_decode(opt_codec_ctx, pduType,
Lev Walkind1bfea62005-11-08 03:06:16 +0000588 (void **)&structure, i_bptr, i_size);
589 break;
590 case INP_XER:
591 rval = xer_decode(opt_codec_ctx, pduType,
592 (void **)&structure, i_bptr, i_size);
593 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000594 case INP_PER:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000595 rval = uper_decode(opt_codec_ctx, pduType,
Lev Walkin5a621d62006-09-18 20:05:34 +0000596 (void **)&structure, i_bptr, i_size, 0,
597 DynamicBuffer.unbits);
Lev Walkinbc691772006-09-17 11:02:53 +0000598 ecbits = rval.consumed % 8; /* Extra bits */
Lev Walkinbc691772006-09-17 11:02:53 +0000599 rval.consumed /= 8; /* Convert to value in bytes! */
Lev Walkin5a621d62006-09-18 20:05:34 +0000600 /* Check if input is byte-padded at the end */
601 if(opt_ippad && ecbits && rval.code == RC_OK) {
602 rval.consumed++;
603 ecbits = 0;
604 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000606 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000607 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
Lev Walkin419f6752006-09-13 04:02:00 +0000608 (long)DynamicBuffer.length,
Lev Walkin4a858bb2006-09-17 11:22:00 +0000609 (long)rval.consumed, ecbits, (long)i_size,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000610 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000611
Lev Walkin419f6752006-09-13 04:02:00 +0000612 if(DynamicBuffer.allocated == 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000613 /*
Lev Walkinbc691772006-09-17 11:02:53 +0000614 * Flush remainder into the intermediate buffer.
Lev Walkin22b54552004-10-28 13:22:54 +0000615 */
616 if(rval.code != RC_FAIL && rval.consumed < rd) {
Lev Walkin419f6752006-09-13 04:02:00 +0000617 add_bytes_to_buffer(fbuf + rval.consumed,
Lev Walkin5a621d62006-09-18 20:05:34 +0000618 rd - rval.consumed);
619 buffer_shift_left(0, ecbits);
620 DynamicBuffer.bytes_shifted = rval.consumed;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000621 rval.consumed = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000622 ecbits = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000623 }
624 }
625
Lev Walkinbc691772006-09-17 11:02:53 +0000626 /*
627 * Adjust position inside the source buffer.
628 */
629 if(DynamicBuffer.allocated) {
630 DynamicBuffer.offset += rval.consumed;
631 DynamicBuffer.length -= rval.consumed;
632 } else {
633 DynamicBuffer.bytes_shifted += rval.consumed;
634 }
635
Lev Walkin22b54552004-10-28 13:22:54 +0000636 switch(rval.code) {
637 case RC_OK:
Lev Walkin5a621d62006-09-18 20:05:34 +0000638 if(ecbits) buffer_shift_left(0, ecbits);
639 DEBUG("RC_OK, finishing up with %ld+%d",
640 (long)rval.consumed, ecbits);
Lev Walkin22b54552004-10-28 13:22:54 +0000641 return structure;
642 case RC_WMORE:
Lev Walkin5a621d62006-09-18 20:05:34 +0000643 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
644 " with %ld..%ld-%ld..%ld",
645 (long)rd,
Lev Walkinf9492a92006-09-13 04:14:17 +0000646 (long)rval.consumed,
647 (long)DynamicBuffer.offset,
648 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000649 (long)DynamicBuffer.unbits,
Lev Walkinf9492a92006-09-13 04:14:17 +0000650 (long)DynamicBuffer.allocated);
Lev Walkin5a621d62006-09-18 20:05:34 +0000651 if(!rd) tolerate_eof--;
Lev Walkin22b54552004-10-28 13:22:54 +0000652 continue;
653 case RC_FAIL:
654 break;
655 }
656 break;
657 }
658
Lev Walkin22b54552004-10-28 13:22:54 +0000659 /* Clean up partially decoded structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000660 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000661
Lev Walkinbc691772006-09-17 11:02:53 +0000662 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
663
664 /*
665 * Print a message and return failure only if not EOF,
666 * unless this is our first PDU (empty file).
667 */
Lev Walkin00918812006-09-18 21:19:32 +0000668 if(on_first_pdu
669 || DynamicBuffer.length
670 || new_offset - old_offset > ((iform == INP_XER)?sizeof("\r\n")-1:0)
671 ) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000672 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
673 on_first_pdu, (long)new_offset, (long)old_offset,
674 (long)DynamicBuffer.length);
Lev Walkinbc691772006-09-17 11:02:53 +0000675 fprintf(stderr, "%s: "
676 "Decode failed past byte %ld: %s\n",
677 name, (long)new_offset,
678 (rval.code == RC_WMORE)
679 ? "Unexpected end of input"
680 : "Input processing error");
Lev Walkin4bf96b92006-09-18 21:46:50 +0000681#ifndef ENOMSG
682#define ENOMSG EINVAL
683#endif
684#ifndef EBADMSG
685#define EBADMSG EINVAL
686#endif
Lev Walkinbc691772006-09-17 11:02:53 +0000687 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
688 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000689 /* Got EOF after a few successful PDUs */
Lev Walkinbc691772006-09-17 11:02:53 +0000690 errno = 0;
691 }
Lev Walkin22b54552004-10-28 13:22:54 +0000692
693 return 0;
694}
695
Lev Walkin419f6752006-09-13 04:02:00 +0000696/* Dump the buffer out to the specified FILE */
697static int write_out(const void *buffer, size_t size, void *key) {
698 FILE *fp = (FILE *)key;
699 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
700}
Lev Walkinc744a022006-09-15 18:33:25 +0000701
702static int argument_is_stdin(char *av[], int idx) {
703 if(strcmp(av[idx], "-")) {
704 return 0; /* Certainly not <stdin> */
705 } else {
706 /* This might be <stdin>, unless `./program -- -` */
707 if(strcmp(av[-1], "--"))
708 return 1;
709 else
710 return 0;
711 }
712}
713
714static FILE *argument_to_file(char *av[], int idx) {
715 return argument_is_stdin(av, idx)
716 ? stdin
717 : fopen(av[idx], "r");
718}
719
720static char *argument_to_name(char *av[], int idx) {
721 return argument_is_stdin(av, idx)
722 ? "standard input"
723 : av[idx];
724}