blob: 1e541c9983bfeb04e7b245ba00a6588c6a06c802 [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,
vlme7e9bd92006-09-17 11:02:53 +000038 FILE *file, const char *name, ssize_t suggested_bufsize, int first_pdu);
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
vlmcd83dde2006-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) */
vlm6d44a542005-11-08 03:06:16 +000048
49/* Input data format selector */
50static enum input_format {
51 INP_BER, /* -iber: BER input */
vlm337167e2005-11-26 11:25:14 +000052 INP_XER, /* -ixer: XER input */
53 INP_PER /* -iper: Unaligned PER input */
vlm6d44a542005-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 */
vlm6ed22c62006-08-25 04:56:21 +000059 OUT_DER, /* -oder: DER (BER) output */
60 OUT_PER, /* -oper: Unaligned PER output */
vlm6d44a542005-11-08 03:06:16 +000061 OUT_TEXT, /* -otext: semi-structured text */
62 OUT_NULL /* -onull: No pretty-printing */
63} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000064
vlmfdf05da2006-09-24 19:47:07 +000065#ifdef JUNKTEST /* Enable -J <probability> */
66#define JUNKOPT "J:"
67static double opt_jprob; /* Junk bit probability */
68static int junk_failures;
69static void junk_bytes_with_probability(uint8_t *, size_t, double prob);
70#else
71#define JUNKOPT
72#endif
73
vlm4d2ca122005-12-07 05:46:03 +000074/* Debug output function */
75static inline void
76DEBUG(const char *fmt, ...) {
77 va_list ap;
78 if(!opt_debug) return;
79 fprintf(stderr, "AD: ");
80 va_start(ap, fmt);
81 vfprintf(stderr, fmt, ap);
82 va_end(ap);
83 fprintf(stderr, "\n");
84}
vlm0e329f22004-10-28 13:22:54 +000085
86int
vlme0fb1e82006-09-15 18:33:25 +000087main(int ac, char *av[]) {
vlmb51ebb22006-08-25 02:35:08 +000088 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
vlm0e329f22004-10-28 13:22:54 +000089 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
90 int number_of_iterations = 1;
91 int num;
92 int ch;
93
vlm337167e2005-11-26 11:25:14 +000094 /* Figure out if Unaligned PER needs to be default */
95 if(pduType->uper_decoder)
96 iform = INP_PER;
97
vlm0e329f22004-10-28 13:22:54 +000098 /*
99 * Pocess the command-line argments.
100 */
vlmfdf05da2006-09-24 19:47:07 +0000101 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
vlm0e329f22004-10-28 13:22:54 +0000102 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +0000103 case 'i':
104 if(optarg[0] == 'b') { iform = INP_BER; break; }
105 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +0000106 if(pduType->uper_decoder
107 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000108 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000109 optarg);
110 exit(EX_UNAVAILABLE);
111 case 'o':
112 if(optarg[0] == 'd') { oform = OUT_DER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000113 if(pduType->uper_encoder
114 && optarg[0] == 'p') { oform = OUT_PER; break; }
vlm6d44a542005-11-08 03:06:16 +0000115 if(optarg[0] == 'x') { oform = OUT_XER; break; }
116 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
117 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
vlm6ed22c62006-08-25 04:56:21 +0000118 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000119 optarg);
120 exit(EX_UNAVAILABLE);
vlme7e9bd92006-09-17 11:02:53 +0000121 case '1':
122 opt_onepdu = 1;
123 break;
vlm0e329f22004-10-28 13:22:54 +0000124 case 'b':
125 suggested_bufsize = atoi(optarg);
126 if(suggested_bufsize < 1
127 || suggested_bufsize > 16 * 1024 * 1024) {
128 fprintf(stderr,
129 "-b %s: Improper buffer size (1..16M)\n",
130 optarg);
131 exit(EX_UNAVAILABLE);
132 }
133 break;
134 case 'c':
135 opt_check = 1;
136 break;
137 case 'd':
138 opt_debug++; /* Double -dd means ASN.1 debug */
139 break;
140 case 'n':
141 number_of_iterations = atoi(optarg);
142 if(number_of_iterations < 1) {
143 fprintf(stderr,
144 "-n %s: Improper iterations count\n", optarg);
145 exit(EX_UNAVAILABLE);
146 }
147 break;
vlme7e9bd92006-09-17 11:02:53 +0000148 case 'p':
vlmcd83dde2006-09-18 20:05:34 +0000149 if(strcmp(optarg, "er-padded") == 0) {
150 opt_ippad = 1;
151 break;
152 }
vlme7e9bd92006-09-17 11:02:53 +0000153#ifdef ASN_PDU_COLLECTION
vlmcd83dde2006-09-18 20:05:34 +0000154 if(strcmp(optarg, "list") == 0) {
vlme7e9bd92006-09-17 11:02:53 +0000155 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
vlmcd83dde2006-09-18 20:05:34 +0000156 fprintf(stderr, "Available PDU types:\n");
157 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
158 exit(0);
159 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
160 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
vlme7e9bd92006-09-17 11:02:53 +0000161 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
162 if(*pdu) { pduType = *pdu; break; }
vlmcd83dde2006-09-18 20:05:34 +0000163 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
vlme7e9bd92006-09-17 11:02:53 +0000164 }
165#endif /* ASN_PDU_COLLECTION */
vlmcd83dde2006-09-18 20:05:34 +0000166 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
vlme7e9bd92006-09-17 11:02:53 +0000167 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +0000168 case 's':
169 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000170 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000171 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000172 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000173 optarg);
174 exit(EX_UNAVAILABLE);
175 }
176 break;
vlmfdf05da2006-09-24 19:47:07 +0000177#ifdef JUNKTEST
178 case 'J':
179 opt_jprob = strtod(optarg, 0);
180 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
181 fprintf(stderr,
182 "-J %s: Probability range 0..1 expected \n",
183 optarg);
184 exit(EX_UNAVAILABLE);
185 }
186 break;
187#endif /* JUNKTEST */
vlm0e329f22004-10-28 13:22:54 +0000188 case 'h':
189 default:
vlm337167e2005-11-26 11:25:14 +0000190 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
191 fprintf(stderr, "Where options are:\n");
192 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000193 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000194 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000195 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000196 " -iber Input is in BER (Basic Encoding Rules)%s\n",
197 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000198 fprintf(stderr,
vlm6ed22c62006-08-25 04:56:21 +0000199 " -ixer Input is in XER (XML Encoding Rules)\n");
200 if(pduType->uper_encoder)
201 fprintf(stderr,
202 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
203 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000204 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000205 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000206 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000207 " -onull Verify (decode) input, but do not output\n");
vlmcd83dde2006-09-18 20:05:34 +0000208 if(pduType->uper_decoder)
209 fprintf(stderr,
210 " -per-padded Assume PER PDUs are byte-padded (-iper)\n");
vlm337167e2005-11-26 11:25:14 +0000211#ifdef ASN_PDU_COLLECTION
212 fprintf(stderr,
213 " -p <PDU> Specify PDU type to decode\n"
214 " -p list List available PDUs\n");
215#endif /* ASN_PDU_COLLECTION */
216 fprintf(stderr,
vlm659f7102006-09-18 22:30:55 +0000217 " -1 Decode only the first PDU in file\n"
vlm0e329f22004-10-28 13:22:54 +0000218 " -b <size> Set the i/o buffer size (default is %ld)\n"
219 " -c Check ASN.1 constraints after decoding\n"
220 " -d Enable debugging (-dd is even better)\n"
221 " -n <num> Process files <num> times\n"
vlm4d2ca122005-12-07 05:46:03 +0000222 " -s <size> Set the stack usage limit (default is %d)\n"
vlmfdf05da2006-09-24 19:47:07 +0000223#ifdef JUNKTEST
224 " -J <prob> Set random junk test bit garbaging probability\n"
225#endif
vlm4d2ca122005-12-07 05:46:03 +0000226 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000227 exit(EX_USAGE);
228 }
229
230 ac -= optind;
231 av += optind;
232
233 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000234 fprintf(stderr, "%s: No input files specified. "
235 "Try '-h' for more information\n",
236 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000237 exit(EX_USAGE);
238 }
239
240 setvbuf(stdout, 0, _IOLBF, 0);
241
242 for(num = 0; num < number_of_iterations; num++) {
243 int ac_i;
244 /*
245 * Process all files in turn.
246 */
247 for(ac_i = 0; ac_i < ac; ac_i++) {
vlme7e9bd92006-09-17 11:02:53 +0000248 asn_enc_rval_t erv;
249 void *structure; /* Decoded structure */
250 FILE *file = argument_to_file(av, ac_i);
251 char *name = argument_to_name(av, ac_i);
252 int first_pdu;
vlm0e329f22004-10-28 13:22:54 +0000253
vlme7e9bd92006-09-17 11:02:53 +0000254 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
vlm0e329f22004-10-28 13:22:54 +0000255 /*
256 * Decode the encoded structure from file.
257 */
vlme3470e72005-03-10 13:39:03 +0000258 structure = data_decode_from_file(pduType,
vlme7e9bd92006-09-17 11:02:53 +0000259 file, name, suggested_bufsize, first_pdu);
vlm0e329f22004-10-28 13:22:54 +0000260 if(!structure) {
vlme7e9bd92006-09-17 11:02:53 +0000261 if(errno) {
262 /* Error message is already printed */
263 exit(EX_DATAERR);
264 } else {
265 /* EOF */
266 break;
267 }
vlm0e329f22004-10-28 13:22:54 +0000268 }
269
vlm0e329f22004-10-28 13:22:54 +0000270 /* Check ASN.1 constraints */
271 if(opt_check) {
272 char errbuf[128];
273 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000274 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000275 errbuf, &errlen)) {
276 fprintf(stderr, "%s: ASN.1 constraint "
vlme0fb1e82006-09-15 18:33:25 +0000277 "check failed: %s\n", name, errbuf);
vlm0e329f22004-10-28 13:22:54 +0000278 exit(EX_DATAERR);
279 }
280 }
281
vlm6d44a542005-11-08 03:06:16 +0000282 switch(oform) {
283 case OUT_NULL:
vlmfdf05da2006-09-24 19:47:07 +0000284#ifdef JUNKTEST
285 if(opt_jprob == 0.0)
286#endif
vlme0fb1e82006-09-15 18:33:25 +0000287 fprintf(stderr, "%s: decoded successfully\n", name);
vlm6d44a542005-11-08 03:06:16 +0000288 break;
289 case OUT_TEXT: /* -otext */
290 asn_fprint(stdout, pduType, structure);
291 break;
292 case OUT_XER: /* -oxer */
293 if(xer_fprint(stdout, pduType, structure)) {
vlmd4f903d2006-09-18 20:26:24 +0000294 fprintf(stderr,
295 "%s: Cannot convert %s into XML\n",
296 name, pduType->name);
vlm6d44a542005-11-08 03:06:16 +0000297 exit(EX_UNAVAILABLE);
298 }
299 break;
300 case OUT_DER:
301 erv = der_encode(pduType, structure, write_out, stdout);
302 if(erv.encoded < 0) {
vlmd4f903d2006-09-18 20:26:24 +0000303 fprintf(stderr,
304 "%s: Cannot convert %s into DER\n",
305 name, pduType->name);
vlm6d44a542005-11-08 03:06:16 +0000306 exit(EX_UNAVAILABLE);
307 }
vlme7e9bd92006-09-17 11:02:53 +0000308 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
vlm6d44a542005-11-08 03:06:16 +0000309 break;
vlm6ed22c62006-08-25 04:56:21 +0000310 case OUT_PER:
311 erv = uper_encode(pduType, structure, write_out, stdout);
312 if(erv.encoded < 0) {
vlmd4f903d2006-09-18 20:26:24 +0000313 fprintf(stderr,
314 "%s: Cannot convert %s into Unaligned PER\n",
315 name, pduType->name);
vlm6ed22c62006-08-25 04:56:21 +0000316 exit(EX_UNAVAILABLE);
317 }
vlme7e9bd92006-09-17 11:02:53 +0000318 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
vlm6ed22c62006-08-25 04:56:21 +0000319 break;
vlm6d44a542005-11-08 03:06:16 +0000320 }
321
vlmbe78c802006-03-17 02:11:12 +0000322 ASN_STRUCT_FREE(*pduType, structure);
vlme7e9bd92006-09-17 11:02:53 +0000323 }
324
325 if(file && file != stdin)
326 fclose(file);
vlm0e329f22004-10-28 13:22:54 +0000327 }
328 }
329
vlmfdf05da2006-09-24 19:47:07 +0000330#ifdef JUNKTEST
331 if(opt_jprob > 0.0) {
332 fprintf(stderr, "Junked %f OK (%d/%d)\n",
333 opt_jprob, junk_failures, number_of_iterations);
334 }
335#endif /* JUNKTEST */
336
vlm0e329f22004-10-28 13:22:54 +0000337 return 0;
338}
339
vlmf5aff922006-09-13 04:02:00 +0000340static struct dynamic_buffer {
vlmcd83dde2006-09-18 20:05:34 +0000341 uint8_t *data; /* Pointer to the data bytes */
vlmf5aff922006-09-13 04:02:00 +0000342 size_t offset; /* Offset from the start */
343 size_t length; /* Length of meaningful contents */
vlmcd83dde2006-09-18 20:05:34 +0000344 size_t unbits; /* Unused bits in the last byte */
vlmf5aff922006-09-13 04:02:00 +0000345 size_t allocated; /* Allocated memory for data */
346 int nreallocs; /* Number of data reallocations */
347 off_t bytes_shifted; /* Number of bytes ever shifted */
348} DynamicBuffer;
vlm0e329f22004-10-28 13:22:54 +0000349
vlmcd83dde2006-09-18 20:05:34 +0000350static void
351buffer_dump() {
352 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
353 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
354 if(!opt_debug) return;
355 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
356 DynamicBuffer.data,
357 (long)DynamicBuffer.offset,
358 (long)DynamicBuffer.length,
359 (long)DynamicBuffer.unbits,
360 (long)DynamicBuffer.allocated,
361 (long)DynamicBuffer.bytes_shifted);
362 for(; p < e; p++) {
363 fprintf(stderr, " %c%c%c%c%c%c%c%c",
364 ((*p >> 7) & 1) ? '1' : '0',
365 ((*p >> 6) & 1) ? '1' : '0',
366 ((*p >> 5) & 1) ? '1' : '0',
367 ((*p >> 4) & 1) ? '1' : '0',
368 ((*p >> 3) & 1) ? '1' : '0',
369 ((*p >> 2) & 1) ? '1' : '0',
370 ((*p >> 1) & 1) ? '1' : '0',
371 ((*p >> 0) & 1) ? '1' : '0');
372 }
373 if(DynamicBuffer.unbits) {
vlmfdf05da2006-09-24 19:47:07 +0000374 unsigned int shift;
vlmcd83dde2006-09-18 20:05:34 +0000375 fprintf(stderr, " ");
376 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
377 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
vlmfdf05da2006-09-24 19:47:07 +0000378 fprintf(stderr, " %ld:%ld\n",
379 (long)DynamicBuffer.length - 1,
380 (long)8 - DynamicBuffer.unbits);
vlmcd83dde2006-09-18 20:05:34 +0000381 } else {
382 fprintf(stderr, " %d\n", DynamicBuffer.length);
383 }
384}
385
386/*
387 * Move the buffer content left N bits, possibly joining it with
388 * preceeding content.
389 */
390static void
391buffer_shift_left(size_t offset, int bits) {
392 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
393 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
394 + DynamicBuffer.length - 1;
395
396 if(!bits) return;
397
398 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
399 bits, (long)offset,
400 (long)DynamicBuffer.offset,
401 (long)DynamicBuffer.unbits,
402 (long)DynamicBuffer.length);
403
404 if(offset) {
405 int right;
406 right = ptr[0] >> (8 - bits);
407
408 DEBUG("oleft: %c%c%c%c%c%c%c%c",
409 ((ptr[-1] >> 7) & 1) ? '1' : '0',
410 ((ptr[-1] >> 6) & 1) ? '1' : '0',
411 ((ptr[-1] >> 5) & 1) ? '1' : '0',
412 ((ptr[-1] >> 4) & 1) ? '1' : '0',
413 ((ptr[-1] >> 3) & 1) ? '1' : '0',
414 ((ptr[-1] >> 2) & 1) ? '1' : '0',
415 ((ptr[-1] >> 1) & 1) ? '1' : '0',
416 ((ptr[-1] >> 0) & 1) ? '1' : '0');
417
418 DEBUG("oriht: %c%c%c%c%c%c%c%c",
419 ((ptr[0] >> 7) & 1) ? '1' : '0',
420 ((ptr[0] >> 6) & 1) ? '1' : '0',
421 ((ptr[0] >> 5) & 1) ? '1' : '0',
422 ((ptr[0] >> 4) & 1) ? '1' : '0',
423 ((ptr[0] >> 3) & 1) ? '1' : '0',
424 ((ptr[0] >> 2) & 1) ? '1' : '0',
425 ((ptr[0] >> 1) & 1) ? '1' : '0',
426 ((ptr[0] >> 0) & 1) ? '1' : '0');
427
428 DEBUG("mriht: %c%c%c%c%c%c%c%c",
429 ((right >> 7) & 1) ? '1' : '0',
430 ((right >> 6) & 1) ? '1' : '0',
431 ((right >> 5) & 1) ? '1' : '0',
432 ((right >> 4) & 1) ? '1' : '0',
433 ((right >> 3) & 1) ? '1' : '0',
434 ((right >> 2) & 1) ? '1' : '0',
435 ((right >> 1) & 1) ? '1' : '0',
436 ((right >> 0) & 1) ? '1' : '0');
437
438 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
439
440 DEBUG("after: %c%c%c%c%c%c%c%c",
441 ((ptr[-1] >> 7) & 1) ? '1' : '0',
442 ((ptr[-1] >> 6) & 1) ? '1' : '0',
443 ((ptr[-1] >> 5) & 1) ? '1' : '0',
444 ((ptr[-1] >> 4) & 1) ? '1' : '0',
445 ((ptr[-1] >> 3) & 1) ? '1' : '0',
446 ((ptr[-1] >> 2) & 1) ? '1' : '0',
447 ((ptr[-1] >> 1) & 1) ? '1' : '0',
448 ((ptr[-1] >> 0) & 1) ? '1' : '0');
449 }
450
451 buffer_dump();
452
453 for(; ptr < end; ptr++) {
454 int right = ptr[1] >> (8 - bits);
455 *ptr = (*ptr << bits) | right;
456 }
457 *ptr <<= bits;
458
459 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
460 if(DynamicBuffer.unbits == 0) {
461 DynamicBuffer.unbits += bits;
462 } else {
463 DynamicBuffer.unbits += bits;
464 if(DynamicBuffer.unbits > 7) {
465 DynamicBuffer.unbits -= 8;
466 DynamicBuffer.length--;
467 DynamicBuffer.bytes_shifted++;
468 }
469 }
470 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
471
472 buffer_dump();
473
474 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
475 (long)DynamicBuffer.offset,
476 (long)DynamicBuffer.unbits,
477 (long)DynamicBuffer.length);
478
479
480}
481
vlm0e329f22004-10-28 13:22:54 +0000482/*
vlm6d44a542005-11-08 03:06:16 +0000483 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000484 */
vlmcd83dde2006-09-18 20:05:34 +0000485static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
vlm0e329f22004-10-28 13:22:54 +0000486
vlmcd83dde2006-09-18 20:05:34 +0000487 if(bytes == 0) return;
488
489 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
490 (long)bytes,
vlmf5aff922006-09-13 04:02:00 +0000491 (long)DynamicBuffer.offset,
492 (long)DynamicBuffer.length,
vlmcd83dde2006-09-18 20:05:34 +0000493 (long)DynamicBuffer.unbits,
vlmf5aff922006-09-13 04:02:00 +0000494 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000495
vlmf5aff922006-09-13 04:02:00 +0000496 if(DynamicBuffer.allocated
vlmcd83dde2006-09-18 20:05:34 +0000497 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
vlmcb923822006-09-13 04:14:17 +0000498 DEBUG("\tNo buffer reallocation is necessary");
vlmcd83dde2006-09-18 20:05:34 +0000499 } else if(bytes <= DynamicBuffer.offset) {
vlmf5aff922006-09-13 04:02:00 +0000500 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
vlm0e329f22004-10-28 13:22:54 +0000501
502 /* Shift the buffer contents */
vlmf5aff922006-09-13 04:02:00 +0000503 memmove(DynamicBuffer.data,
504 DynamicBuffer.data + DynamicBuffer.offset,
505 DynamicBuffer.length);
506 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
507 DynamicBuffer.offset = 0;
vlm0e329f22004-10-28 13:22:54 +0000508 } else {
vlmcd83dde2006-09-18 20:05:34 +0000509 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
vlmf5aff922006-09-13 04:02:00 +0000510 void *p = MALLOC(newsize);
vlm4d2ca122005-12-07 05:46:03 +0000511 if(!p) {
vlmf5aff922006-09-13 04:02:00 +0000512 perror("malloc()");
vlm0e329f22004-10-28 13:22:54 +0000513 exit(EX_OSERR);
514 }
vlm84444f92006-09-17 08:23:43 +0000515 memcpy(p,
516 DynamicBuffer.data + DynamicBuffer.offset,
517 DynamicBuffer.length);
vlmf5aff922006-09-13 04:02:00 +0000518 FREEMEM(DynamicBuffer.data);
vlmfdf05da2006-09-24 19:47:07 +0000519 DynamicBuffer.data = (uint8_t *)p;
vlmf5aff922006-09-13 04:02:00 +0000520 DynamicBuffer.offset = 0;
521 DynamicBuffer.allocated = newsize;
522 DynamicBuffer.nreallocs++;
vlmcb923822006-09-13 04:14:17 +0000523 DEBUG("\tBuffer reallocated to %ld (%d time)",
vlmf5aff922006-09-13 04:02:00 +0000524 newsize, DynamicBuffer.nreallocs);
vlm0e329f22004-10-28 13:22:54 +0000525 }
vlm4d2ca122005-12-07 05:46:03 +0000526
vlmcd83dde2006-09-18 20:05:34 +0000527 memcpy(DynamicBuffer.data
528 + DynamicBuffer.offset + DynamicBuffer.length,
529 data2add, bytes);
530 DynamicBuffer.length += bytes;
531 if(DynamicBuffer.unbits) {
532 int bits = DynamicBuffer.unbits;
533 DynamicBuffer.unbits = 0;
534 buffer_shift_left(DynamicBuffer.length - bytes, bits);
535 }
536
537 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
538 (long)bytes,
539 (long)DynamicBuffer.offset,
540 (long)DynamicBuffer.length,
541 (long)DynamicBuffer.unbits,
542 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000543}
544
vlme0fb1e82006-09-15 18:33:25 +0000545static void *
vlme7e9bd92006-09-17 11:02:53 +0000546data_decode_from_file(asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) {
vlmcd83dde2006-09-18 20:05:34 +0000547 static uint8_t *fbuf;
vlm0e329f22004-10-28 13:22:54 +0000548 static ssize_t fbuf_size;
549 static asn_codec_ctx_t s_codec_ctx;
550 asn_codec_ctx_t *opt_codec_ctx = 0;
551 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000552 asn_dec_rval_t rval;
vlme7e9bd92006-09-17 11:02:53 +0000553 size_t old_offset;
554 size_t new_offset;
vlmcd83dde2006-09-18 20:05:34 +0000555 int tolerate_eof;
vlm0e329f22004-10-28 13:22:54 +0000556 size_t rd;
vlme0fb1e82006-09-15 18:33:25 +0000557
558 if(!file) {
vlme7e9bd92006-09-17 11:02:53 +0000559 fprintf(stderr, "%s: %s\n", name, strerror(errno));
560 errno = EINVAL;
vlme0fb1e82006-09-15 18:33:25 +0000561 return 0;
562 }
vlm0e329f22004-10-28 13:22:54 +0000563
564 if(opt_stack) {
565 s_codec_ctx.max_stack_size = opt_stack;
566 opt_codec_ctx = &s_codec_ctx;
567 }
568
vlme7e9bd92006-09-17 11:02:53 +0000569 DEBUG("Processing %s", name);
vlm0e329f22004-10-28 13:22:54 +0000570
571 /* prepare the file buffer */
572 if(fbuf_size != suggested_bufsize) {
vlmfdf05da2006-09-24 19:47:07 +0000573 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000574 if(!fbuf) {
575 perror("realloc()");
576 exit(EX_OSERR);
577 }
578 fbuf_size = suggested_bufsize;
579 }
580
vlme7e9bd92006-09-17 11:02:53 +0000581 if(on_first_pdu) {
582 DynamicBuffer.offset = 0;
583 DynamicBuffer.length = 0;
vlmcd83dde2006-09-18 20:05:34 +0000584 DynamicBuffer.unbits = 0;
vlme7e9bd92006-09-17 11:02:53 +0000585 DynamicBuffer.allocated = 0;
586 DynamicBuffer.bytes_shifted = 0;
587 DynamicBuffer.nreallocs = 0;
588 }
589
590 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
vlm0e329f22004-10-28 13:22:54 +0000591
vlm6d44a542005-11-08 03:06:16 +0000592 /* Pretend immediate EOF */
593 rval.code = RC_WMORE;
594 rval.consumed = 0;
595
vlmcd83dde2006-09-18 20:05:34 +0000596 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
597 (rd = fread(fbuf, 1, fbuf_size, file))
598 || feof(file) == 0
599 || (tolerate_eof && DynamicBuffer.length)
600 ;) {
vlmfdf05da2006-09-24 19:47:07 +0000601 int ecbits = 0; /* Extra consumed bits in case of PER */
602 uint8_t *i_bptr;
603 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000604
605 /*
606 * Copy the data over, or use the original buffer.
607 */
vlmf5aff922006-09-13 04:02:00 +0000608 if(DynamicBuffer.allocated) {
vlmcd83dde2006-09-18 20:05:34 +0000609 /* Append new data into the existing dynamic buffer */
vlmf5aff922006-09-13 04:02:00 +0000610 add_bytes_to_buffer(fbuf, rd);
611 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
vlmcb923822006-09-13 04:14:17 +0000612 i_size = DynamicBuffer.length;
vlm0e329f22004-10-28 13:22:54 +0000613 } else {
vlm6d44a542005-11-08 03:06:16 +0000614 i_bptr = fbuf;
615 i_size = rd;
616 }
vlm0e329f22004-10-28 13:22:54 +0000617
vlme7e9bd92006-09-17 11:02:53 +0000618 DEBUG("Decoding %ld bytes", (long)i_size);
619
vlmfdf05da2006-09-24 19:47:07 +0000620#ifdef JUNKTEST
621 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
622#endif
623
vlm6d44a542005-11-08 03:06:16 +0000624 switch(iform) {
625 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000626 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000627 (void **)&structure, i_bptr, i_size);
628 break;
629 case INP_XER:
630 rval = xer_decode(opt_codec_ctx, pduType,
631 (void **)&structure, i_bptr, i_size);
632 break;
vlm337167e2005-11-26 11:25:14 +0000633 case INP_PER:
vlm4d2ca122005-12-07 05:46:03 +0000634 rval = uper_decode(opt_codec_ctx, pduType,
vlmcd83dde2006-09-18 20:05:34 +0000635 (void **)&structure, i_bptr, i_size, 0,
636 DynamicBuffer.unbits);
vlmfdf05da2006-09-24 19:47:07 +0000637 /* PER requires returns number of bits, but a catch! */
638 switch(rval.code) {
639 case RC_OK:
640 /* Check if input is byte-padded at the end */
641 if(opt_ippad && (rval.consumed % 8)) {
642 rval.consumed /= 8;
643 rval.consumed++;
644 ecbits = 0;
645 break;
646 }
647 /* Fall through */
648 case RC_FAIL:
649 ecbits = rval.consumed % 8; /* Extra bits */
650 rval.consumed /= 8; /* Convert into bytes! */
651 break;
652 case RC_WMORE:
653 /* PER does not support restartability */
654 ASN_STRUCT_FREE(*pduType, structure);
655 structure = 0;
656 rval.consumed = 0;
657 /* Continue accumulating data */
658 break;
vlmcd83dde2006-09-18 20:05:34 +0000659 }
vlm337167e2005-11-26 11:25:14 +0000660 break;
vlm6d44a542005-11-08 03:06:16 +0000661 }
vlmcd83dde2006-09-18 20:05:34 +0000662 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
vlmf5aff922006-09-13 04:02:00 +0000663 (long)DynamicBuffer.length,
vlmb4ee56f2006-09-17 11:22:00 +0000664 (long)rval.consumed, ecbits, (long)i_size,
vlm4d2ca122005-12-07 05:46:03 +0000665 rval.code);
vlm0e329f22004-10-28 13:22:54 +0000666
vlmf5aff922006-09-13 04:02:00 +0000667 if(DynamicBuffer.allocated == 0) {
vlm0e329f22004-10-28 13:22:54 +0000668 /*
vlme7e9bd92006-09-17 11:02:53 +0000669 * Flush remainder into the intermediate buffer.
vlm0e329f22004-10-28 13:22:54 +0000670 */
671 if(rval.code != RC_FAIL && rval.consumed < rd) {
vlmf5aff922006-09-13 04:02:00 +0000672 add_bytes_to_buffer(fbuf + rval.consumed,
vlmcd83dde2006-09-18 20:05:34 +0000673 rd - rval.consumed);
674 buffer_shift_left(0, ecbits);
675 DynamicBuffer.bytes_shifted = rval.consumed;
vlm4d2ca122005-12-07 05:46:03 +0000676 rval.consumed = 0;
vlmcd83dde2006-09-18 20:05:34 +0000677 ecbits = 0;
vlm0e329f22004-10-28 13:22:54 +0000678 }
679 }
680
vlme7e9bd92006-09-17 11:02:53 +0000681 /*
682 * Adjust position inside the source buffer.
683 */
684 if(DynamicBuffer.allocated) {
685 DynamicBuffer.offset += rval.consumed;
686 DynamicBuffer.length -= rval.consumed;
687 } else {
688 DynamicBuffer.bytes_shifted += rval.consumed;
689 }
690
vlm0e329f22004-10-28 13:22:54 +0000691 switch(rval.code) {
692 case RC_OK:
vlmcd83dde2006-09-18 20:05:34 +0000693 if(ecbits) buffer_shift_left(0, ecbits);
694 DEBUG("RC_OK, finishing up with %ld+%d",
695 (long)rval.consumed, ecbits);
vlm0e329f22004-10-28 13:22:54 +0000696 return structure;
697 case RC_WMORE:
vlmcd83dde2006-09-18 20:05:34 +0000698 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
699 " with %ld..%ld-%ld..%ld",
700 (long)rd,
vlmcb923822006-09-13 04:14:17 +0000701 (long)rval.consumed,
702 (long)DynamicBuffer.offset,
703 (long)DynamicBuffer.length,
vlmcd83dde2006-09-18 20:05:34 +0000704 (long)DynamicBuffer.unbits,
vlmcb923822006-09-13 04:14:17 +0000705 (long)DynamicBuffer.allocated);
vlmcd83dde2006-09-18 20:05:34 +0000706 if(!rd) tolerate_eof--;
vlm0e329f22004-10-28 13:22:54 +0000707 continue;
708 case RC_FAIL:
709 break;
710 }
711 break;
712 }
713
vlmfdf05da2006-09-24 19:47:07 +0000714 DEBUG("Clean up partially decoded structure");
vlmbe78c802006-03-17 02:11:12 +0000715 ASN_STRUCT_FREE(*pduType, structure);
vlm0e329f22004-10-28 13:22:54 +0000716
vlme7e9bd92006-09-17 11:02:53 +0000717 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
718
719 /*
720 * Print a message and return failure only if not EOF,
721 * unless this is our first PDU (empty file).
722 */
vlmde70d692006-09-18 21:19:32 +0000723 if(on_first_pdu
724 || DynamicBuffer.length
725 || new_offset - old_offset > ((iform == INP_XER)?sizeof("\r\n")-1:0)
726 ) {
vlmfdf05da2006-09-24 19:47:07 +0000727
728#ifdef JUNKTEST
729 /*
730 * Nothing's wrong with being unable to decode junk.
731 * Simulate EOF.
732 */
vlm81ea8df2006-09-28 08:03:15 +0000733 if(opt_jprob != 0.0) {
734 junk_failures++;
735 errno = 0;
736 return 0;
737 }
vlmfdf05da2006-09-24 19:47:07 +0000738#endif
739
vlmcd83dde2006-09-18 20:05:34 +0000740 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
741 on_first_pdu, (long)new_offset, (long)old_offset,
742 (long)DynamicBuffer.length);
vlme7e9bd92006-09-17 11:02:53 +0000743 fprintf(stderr, "%s: "
744 "Decode failed past byte %ld: %s\n",
745 name, (long)new_offset,
746 (rval.code == RC_WMORE)
747 ? "Unexpected end of input"
748 : "Input processing error");
vlm4450c352006-09-18 21:46:50 +0000749#ifndef ENOMSG
750#define ENOMSG EINVAL
751#endif
752#ifndef EBADMSG
753#define EBADMSG EINVAL
754#endif
vlme7e9bd92006-09-17 11:02:53 +0000755 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
756 } else {
vlmcd83dde2006-09-18 20:05:34 +0000757 /* Got EOF after a few successful PDUs */
vlme7e9bd92006-09-17 11:02:53 +0000758 errno = 0;
759 }
vlm0e329f22004-10-28 13:22:54 +0000760
761 return 0;
762}
763
vlmf5aff922006-09-13 04:02:00 +0000764/* Dump the buffer out to the specified FILE */
765static int write_out(const void *buffer, size_t size, void *key) {
766 FILE *fp = (FILE *)key;
767 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
768}
vlme0fb1e82006-09-15 18:33:25 +0000769
770static int argument_is_stdin(char *av[], int idx) {
771 if(strcmp(av[idx], "-")) {
772 return 0; /* Certainly not <stdin> */
773 } else {
774 /* This might be <stdin>, unless `./program -- -` */
775 if(strcmp(av[-1], "--"))
776 return 1;
777 else
778 return 0;
779 }
780}
781
782static FILE *argument_to_file(char *av[], int idx) {
783 return argument_is_stdin(av, idx)
784 ? stdin
785 : fopen(av[idx], "r");
786}
787
788static char *argument_to_name(char *av[], int idx) {
789 return argument_is_stdin(av, idx)
790 ? "standard input"
791 : av[idx];
792}
vlmfdf05da2006-09-24 19:47:07 +0000793
794#ifdef JUNKTEST
795/*
796 * Fill bytes with some garbage with specified probability (more or less).
797 */
798static void
799junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
800 static int junkmode;
801 uint8_t *ptr;
802 uint8_t *end;
803 if(opt_jprob <= 0.0) return;
804 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
805 int byte = *ptr;
806 if(junkmode++ & 1) {
807 if((((double)random() / RAND_MAX) < prob))
808 byte = random() & 0xff;
809 } else {
810#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
811 byte ^= BPROB(0x80);
812 byte ^= BPROB(0x40);
813 byte ^= BPROB(0x20);
814 byte ^= BPROB(0x10);
815 byte ^= BPROB(0x08);
816 byte ^= BPROB(0x04);
817 byte ^= BPROB(0x02);
818 byte ^= BPROB(0x01);
819 }
820 if(byte != *ptr) {
821 DEBUG("Junk buf[%d] %02x -> %02x",
822 ptr - buf, *ptr, byte);
823 *ptr = byte;
824 }
825 }
826}
827#endif /* JUNKTEST */
828