blob: 4f5b8ed27bb2c9ad65ff3db65907f439848bcfaf [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 <errno.h> /* for errno */
vlm0e329f22004-10-28 13:22:54 +000019
vlme3470e72005-03-10 13:39:03 +000020#include <asn_application.h>
vlm4d2ca122005-12-07 05:46:03 +000021#include <asn_internal.h> /* for _ASN_DEFAULT_STACK_MAX */
vlm0e329f22004-10-28 13:22:54 +000022
vlmb51ebb22006-08-25 02:35:08 +000023/* Convert "Type" defined by -DPDU into "asn_DEF_Type" */
24#define ASN_DEF_PDU(t) asn_DEF_ ## t
25#define DEF_PDU_Type(t) ASN_DEF_PDU(t)
26#define PDU_Type DEF_PDU_Type(PDU)
27
28extern asn_TYPE_descriptor_t PDU_Type; /* ASN.1 type to be decoded */
vlm337167e2005-11-26 11:25:14 +000029#ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
30extern asn_TYPE_descriptor_t *asn_pdu_collection[];
31#endif
vlm0e329f22004-10-28 13:22:54 +000032
33/*
vlme3470e72005-03-10 13:39:03 +000034 * Open file and parse its contens.
vlm0e329f22004-10-28 13:22:54 +000035 */
vlme3470e72005-03-10 13:39:03 +000036static void *data_decode_from_file(asn_TYPE_descriptor_t *asnTypeOfPDU,
vlme7e9bd92006-09-17 11:02:53 +000037 FILE *file, const char *name, ssize_t suggested_bufsize, int first_pdu);
vlm6d44a542005-11-08 03:06:16 +000038static int write_out(const void *buffer, size_t size, void *key);
vlme0fb1e82006-09-15 18:33:25 +000039static FILE *argument_to_file(char *av[], int idx);
40static char *argument_to_name(char *av[], int idx);
vlm0e329f22004-10-28 13:22:54 +000041
vlmcd83dde2006-09-18 20:05:34 +000042 int opt_debug; /* -d (or -dd) */
43static int opt_check; /* -c (constraints checking) */
44static int opt_stack; /* -s (maximum stack size) */
vlma472cf22007-06-26 08:24:50 +000045static int opt_nopad; /* -per-nopad (PER input is not padded) */
vlmcd83dde2006-09-18 20:05:34 +000046static int opt_onepdu; /* -1 (decode single PDU) */
vlm6d44a542005-11-08 03:06:16 +000047
48/* Input data format selector */
49static enum input_format {
50 INP_BER, /* -iber: BER input */
vlm337167e2005-11-26 11:25:14 +000051 INP_XER, /* -ixer: XER input */
52 INP_PER /* -iper: Unaligned PER input */
vlm6d44a542005-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 */
vlm6ed22c62006-08-25 04:56:21 +000058 OUT_DER, /* -oder: DER (BER) output */
59 OUT_PER, /* -oper: Unaligned PER output */
vlm6d44a542005-11-08 03:06:16 +000060 OUT_TEXT, /* -otext: semi-structured text */
61 OUT_NULL /* -onull: No pretty-printing */
62} oform; /* -o<format> */
vlm0e329f22004-10-28 13:22:54 +000063
vlmfdf05da2006-09-24 19:47:07 +000064#ifdef JUNKTEST /* Enable -J <probability> */
65#define JUNKOPT "J:"
66static double opt_jprob; /* Junk bit probability */
67static int junk_failures;
68static void junk_bytes_with_probability(uint8_t *, size_t, double prob);
69#else
70#define JUNKOPT
71#endif
72
vlm4d2ca122005-12-07 05:46:03 +000073/* Debug output function */
74static inline void
75DEBUG(const char *fmt, ...) {
76 va_list ap;
77 if(!opt_debug) return;
78 fprintf(stderr, "AD: ");
79 va_start(ap, fmt);
80 vfprintf(stderr, fmt, ap);
81 va_end(ap);
82 fprintf(stderr, "\n");
83}
vlm0e329f22004-10-28 13:22:54 +000084
85int
vlme0fb1e82006-09-15 18:33:25 +000086main(int ac, char *av[]) {
vlmb51ebb22006-08-25 02:35:08 +000087 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
vlm0e329f22004-10-28 13:22:54 +000088 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
89 int number_of_iterations = 1;
90 int num;
91 int ch;
92
vlm337167e2005-11-26 11:25:14 +000093 /* Figure out if Unaligned PER needs to be default */
94 if(pduType->uper_decoder)
95 iform = INP_PER;
96
vlm0e329f22004-10-28 13:22:54 +000097 /*
98 * Pocess the command-line argments.
99 */
vlmfdf05da2006-09-24 19:47:07 +0000100 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
vlm0e329f22004-10-28 13:22:54 +0000101 switch(ch) {
vlm6d44a542005-11-08 03:06:16 +0000102 case 'i':
103 if(optarg[0] == 'b') { iform = INP_BER; break; }
104 if(optarg[0] == 'x') { iform = INP_XER; break; }
vlm337167e2005-11-26 11:25:14 +0000105 if(pduType->uper_decoder
106 && optarg[0] == 'p') { iform = INP_PER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000107 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000108 optarg);
109 exit(EX_UNAVAILABLE);
110 case 'o':
111 if(optarg[0] == 'd') { oform = OUT_DER; break; }
vlm6ed22c62006-08-25 04:56:21 +0000112 if(pduType->uper_encoder
113 && optarg[0] == 'p') { oform = OUT_PER; break; }
vlm6d44a542005-11-08 03:06:16 +0000114 if(optarg[0] == 'x') { oform = OUT_XER; break; }
115 if(optarg[0] == 't') { oform = OUT_TEXT; break; }
116 if(optarg[0] == 'n') { oform = OUT_NULL; break; }
vlm6ed22c62006-08-25 04:56:21 +0000117 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
vlm6d44a542005-11-08 03:06:16 +0000118 optarg);
119 exit(EX_UNAVAILABLE);
vlme7e9bd92006-09-17 11:02:53 +0000120 case '1':
121 opt_onepdu = 1;
122 break;
vlm0e329f22004-10-28 13:22:54 +0000123 case 'b':
124 suggested_bufsize = atoi(optarg);
125 if(suggested_bufsize < 1
126 || suggested_bufsize > 16 * 1024 * 1024) {
127 fprintf(stderr,
128 "-b %s: Improper buffer size (1..16M)\n",
129 optarg);
130 exit(EX_UNAVAILABLE);
131 }
132 break;
133 case 'c':
134 opt_check = 1;
135 break;
136 case 'd':
137 opt_debug++; /* Double -dd means ASN.1 debug */
138 break;
139 case 'n':
140 number_of_iterations = atoi(optarg);
141 if(number_of_iterations < 1) {
142 fprintf(stderr,
143 "-n %s: Improper iterations count\n", optarg);
144 exit(EX_UNAVAILABLE);
145 }
146 break;
vlme7e9bd92006-09-17 11:02:53 +0000147 case 'p':
vlma472cf22007-06-26 08:24:50 +0000148 if(strcmp(optarg, "er-nopad") == 0) {
149 opt_nopad = 1;
vlmcd83dde2006-09-18 20:05:34 +0000150 break;
151 }
vlme7e9bd92006-09-17 11:02:53 +0000152#ifdef ASN_PDU_COLLECTION
vlmcd83dde2006-09-18 20:05:34 +0000153 if(strcmp(optarg, "list") == 0) {
vlme7e9bd92006-09-17 11:02:53 +0000154 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
vlmcd83dde2006-09-18 20:05:34 +0000155 fprintf(stderr, "Available PDU types:\n");
156 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
157 exit(0);
158 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
159 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
vlme7e9bd92006-09-17 11:02:53 +0000160 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
161 if(*pdu) { pduType = *pdu; break; }
vlmcd83dde2006-09-18 20:05:34 +0000162 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
vlme7e9bd92006-09-17 11:02:53 +0000163 }
164#endif /* ASN_PDU_COLLECTION */
vlmcd83dde2006-09-18 20:05:34 +0000165 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
vlme7e9bd92006-09-17 11:02:53 +0000166 exit(EX_UNAVAILABLE);
vlm0e329f22004-10-28 13:22:54 +0000167 case 's':
168 opt_stack = atoi(optarg);
vlm4d2ca122005-12-07 05:46:03 +0000169 if(opt_stack < 0) {
vlm0e329f22004-10-28 13:22:54 +0000170 fprintf(stderr,
vlm4d2ca122005-12-07 05:46:03 +0000171 "-s %s: Non-negative value expected\n",
vlm0e329f22004-10-28 13:22:54 +0000172 optarg);
173 exit(EX_UNAVAILABLE);
174 }
175 break;
vlmfdf05da2006-09-24 19:47:07 +0000176#ifdef JUNKTEST
177 case 'J':
178 opt_jprob = strtod(optarg, 0);
179 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
180 fprintf(stderr,
181 "-J %s: Probability range 0..1 expected \n",
182 optarg);
183 exit(EX_UNAVAILABLE);
184 }
185 break;
186#endif /* JUNKTEST */
vlm0e329f22004-10-28 13:22:54 +0000187 case 'h':
188 default:
vlm337167e2005-11-26 11:25:14 +0000189 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
190 fprintf(stderr, "Where options are:\n");
191 if(pduType->uper_decoder)
vlm0e329f22004-10-28 13:22:54 +0000192 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000193 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
vlm337167e2005-11-26 11:25:14 +0000194 fprintf(stderr,
vlm9927d132006-02-22 08:54:49 +0000195 " -iber Input is in BER (Basic Encoding Rules)%s\n",
196 iform == INP_PER ? "" : " (DEFAULT)");
vlm337167e2005-11-26 11:25:14 +0000197 fprintf(stderr,
vlm6ed22c62006-08-25 04:56:21 +0000198 " -ixer Input is in XER (XML Encoding Rules)\n");
199 if(pduType->uper_encoder)
200 fprintf(stderr,
201 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
202 fprintf(stderr,
vlm6d44a542005-11-08 03:06:16 +0000203 " -oder Output in DER (Distinguished Encoding Rules)\n"
vlm9927d132006-02-22 08:54:49 +0000204 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
vlm6d44a542005-11-08 03:06:16 +0000205 " -otext Output in plain semi-structured text (dump)\n"
vlm337167e2005-11-26 11:25:14 +0000206 " -onull Verify (decode) input, but do not output\n");
vlmcd83dde2006-09-18 20:05:34 +0000207 if(pduType->uper_decoder)
208 fprintf(stderr,
vlma472cf22007-06-26 08:24:50 +0000209 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
vlm337167e2005-11-26 11:25:14 +0000210#ifdef ASN_PDU_COLLECTION
211 fprintf(stderr,
212 " -p <PDU> Specify PDU type to decode\n"
213 " -p list List available PDUs\n");
214#endif /* ASN_PDU_COLLECTION */
215 fprintf(stderr,
vlm659f7102006-09-18 22:30:55 +0000216 " -1 Decode only the first PDU in file\n"
vlm0e329f22004-10-28 13:22:54 +0000217 " -b <size> Set the i/o buffer size (default is %ld)\n"
218 " -c Check ASN.1 constraints after decoding\n"
219 " -d Enable debugging (-dd is even better)\n"
220 " -n <num> Process files <num> times\n"
vlm4d2ca122005-12-07 05:46:03 +0000221 " -s <size> Set the stack usage limit (default is %d)\n"
vlmfdf05da2006-09-24 19:47:07 +0000222#ifdef JUNKTEST
223 " -J <prob> Set random junk test bit garbaging probability\n"
224#endif
vlm4d2ca122005-12-07 05:46:03 +0000225 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
vlm0e329f22004-10-28 13:22:54 +0000226 exit(EX_USAGE);
227 }
228
229 ac -= optind;
230 av += optind;
231
232 if(ac < 1) {
vlm6d44a542005-11-08 03:06:16 +0000233 fprintf(stderr, "%s: No input files specified. "
234 "Try '-h' for more information\n",
235 av[-optind]);
vlm0e329f22004-10-28 13:22:54 +0000236 exit(EX_USAGE);
237 }
238
239 setvbuf(stdout, 0, _IOLBF, 0);
240
241 for(num = 0; num < number_of_iterations; num++) {
242 int ac_i;
243 /*
244 * Process all files in turn.
245 */
246 for(ac_i = 0; ac_i < ac; ac_i++) {
vlme7e9bd92006-09-17 11:02:53 +0000247 asn_enc_rval_t erv;
248 void *structure; /* Decoded structure */
249 FILE *file = argument_to_file(av, ac_i);
250 char *name = argument_to_name(av, ac_i);
251 int first_pdu;
vlm0e329f22004-10-28 13:22:54 +0000252
vlme7e9bd92006-09-17 11:02:53 +0000253 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
vlm0e329f22004-10-28 13:22:54 +0000254 /*
255 * Decode the encoded structure from file.
256 */
vlme3470e72005-03-10 13:39:03 +0000257 structure = data_decode_from_file(pduType,
vlme7e9bd92006-09-17 11:02:53 +0000258 file, name, suggested_bufsize, first_pdu);
vlm0e329f22004-10-28 13:22:54 +0000259 if(!structure) {
vlme7e9bd92006-09-17 11:02:53 +0000260 if(errno) {
261 /* Error message is already printed */
262 exit(EX_DATAERR);
263 } else {
264 /* EOF */
265 break;
266 }
vlm0e329f22004-10-28 13:22:54 +0000267 }
268
vlm0e329f22004-10-28 13:22:54 +0000269 /* Check ASN.1 constraints */
270 if(opt_check) {
271 char errbuf[128];
272 size_t errlen = sizeof(errbuf);
vlme3470e72005-03-10 13:39:03 +0000273 if(asn_check_constraints(pduType, structure,
vlm0e329f22004-10-28 13:22:54 +0000274 errbuf, &errlen)) {
275 fprintf(stderr, "%s: ASN.1 constraint "
vlme0fb1e82006-09-15 18:33:25 +0000276 "check failed: %s\n", name, errbuf);
vlm0e329f22004-10-28 13:22:54 +0000277 exit(EX_DATAERR);
278 }
279 }
280
vlm6d44a542005-11-08 03:06:16 +0000281 switch(oform) {
282 case OUT_NULL:
vlmfdf05da2006-09-24 19:47:07 +0000283#ifdef JUNKTEST
284 if(opt_jprob == 0.0)
285#endif
vlme0fb1e82006-09-15 18:33:25 +0000286 fprintf(stderr, "%s: decoded successfully\n", name);
vlm6d44a542005-11-08 03:06:16 +0000287 break;
288 case OUT_TEXT: /* -otext */
289 asn_fprint(stdout, pduType, structure);
290 break;
291 case OUT_XER: /* -oxer */
292 if(xer_fprint(stdout, pduType, structure)) {
vlmd4f903d2006-09-18 20:26:24 +0000293 fprintf(stderr,
294 "%s: Cannot convert %s into XML\n",
295 name, pduType->name);
vlm6d44a542005-11-08 03:06:16 +0000296 exit(EX_UNAVAILABLE);
297 }
298 break;
299 case OUT_DER:
300 erv = der_encode(pduType, structure, write_out, stdout);
301 if(erv.encoded < 0) {
vlmd4f903d2006-09-18 20:26:24 +0000302 fprintf(stderr,
303 "%s: Cannot convert %s into DER\n",
304 name, pduType->name);
vlm6d44a542005-11-08 03:06:16 +0000305 exit(EX_UNAVAILABLE);
306 }
vlme7e9bd92006-09-17 11:02:53 +0000307 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
vlm6d44a542005-11-08 03:06:16 +0000308 break;
vlm6ed22c62006-08-25 04:56:21 +0000309 case OUT_PER:
310 erv = uper_encode(pduType, structure, write_out, stdout);
311 if(erv.encoded < 0) {
vlmd4f903d2006-09-18 20:26:24 +0000312 fprintf(stderr,
313 "%s: Cannot convert %s into Unaligned PER\n",
314 name, pduType->name);
vlm6ed22c62006-08-25 04:56:21 +0000315 exit(EX_UNAVAILABLE);
316 }
vlme7e9bd92006-09-17 11:02:53 +0000317 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
vlm6ed22c62006-08-25 04:56:21 +0000318 break;
vlm6d44a542005-11-08 03:06:16 +0000319 }
320
vlmbe78c802006-03-17 02:11:12 +0000321 ASN_STRUCT_FREE(*pduType, structure);
vlme7e9bd92006-09-17 11:02:53 +0000322 }
323
324 if(file && file != stdin)
325 fclose(file);
vlm0e329f22004-10-28 13:22:54 +0000326 }
327 }
328
vlmfdf05da2006-09-24 19:47:07 +0000329#ifdef JUNKTEST
330 if(opt_jprob > 0.0) {
331 fprintf(stderr, "Junked %f OK (%d/%d)\n",
332 opt_jprob, junk_failures, number_of_iterations);
333 }
334#endif /* JUNKTEST */
335
vlm0e329f22004-10-28 13:22:54 +0000336 return 0;
337}
338
vlmf5aff922006-09-13 04:02:00 +0000339static struct dynamic_buffer {
vlmcd83dde2006-09-18 20:05:34 +0000340 uint8_t *data; /* Pointer to the data bytes */
vlmf5aff922006-09-13 04:02:00 +0000341 size_t offset; /* Offset from the start */
342 size_t length; /* Length of meaningful contents */
vlmcd83dde2006-09-18 20:05:34 +0000343 size_t unbits; /* Unused bits in the last byte */
vlmf5aff922006-09-13 04:02:00 +0000344 size_t allocated; /* Allocated memory for data */
345 int nreallocs; /* Number of data reallocations */
346 off_t bytes_shifted; /* Number of bytes ever shifted */
347} DynamicBuffer;
vlm0e329f22004-10-28 13:22:54 +0000348
vlmcd83dde2006-09-18 20:05:34 +0000349static void
350buffer_dump() {
351 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
352 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
353 if(!opt_debug) return;
354 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
355 DynamicBuffer.data,
356 (long)DynamicBuffer.offset,
357 (long)DynamicBuffer.length,
358 (long)DynamicBuffer.unbits,
359 (long)DynamicBuffer.allocated,
360 (long)DynamicBuffer.bytes_shifted);
361 for(; p < e; p++) {
362 fprintf(stderr, " %c%c%c%c%c%c%c%c",
363 ((*p >> 7) & 1) ? '1' : '0',
364 ((*p >> 6) & 1) ? '1' : '0',
365 ((*p >> 5) & 1) ? '1' : '0',
366 ((*p >> 4) & 1) ? '1' : '0',
367 ((*p >> 3) & 1) ? '1' : '0',
368 ((*p >> 2) & 1) ? '1' : '0',
369 ((*p >> 1) & 1) ? '1' : '0',
370 ((*p >> 0) & 1) ? '1' : '0');
371 }
372 if(DynamicBuffer.unbits) {
vlmfdf05da2006-09-24 19:47:07 +0000373 unsigned int shift;
vlmcd83dde2006-09-18 20:05:34 +0000374 fprintf(stderr, " ");
375 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
376 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
vlmfdf05da2006-09-24 19:47:07 +0000377 fprintf(stderr, " %ld:%ld\n",
378 (long)DynamicBuffer.length - 1,
379 (long)8 - DynamicBuffer.unbits);
vlmcd83dde2006-09-18 20:05:34 +0000380 } else {
381 fprintf(stderr, " %d\n", DynamicBuffer.length);
382 }
383}
384
385/*
386 * Move the buffer content left N bits, possibly joining it with
387 * preceeding content.
388 */
389static void
390buffer_shift_left(size_t offset, int bits) {
391 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
392 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
393 + DynamicBuffer.length - 1;
394
395 if(!bits) return;
396
397 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
398 bits, (long)offset,
399 (long)DynamicBuffer.offset,
400 (long)DynamicBuffer.unbits,
401 (long)DynamicBuffer.length);
402
403 if(offset) {
404 int right;
405 right = ptr[0] >> (8 - bits);
406
407 DEBUG("oleft: %c%c%c%c%c%c%c%c",
408 ((ptr[-1] >> 7) & 1) ? '1' : '0',
409 ((ptr[-1] >> 6) & 1) ? '1' : '0',
410 ((ptr[-1] >> 5) & 1) ? '1' : '0',
411 ((ptr[-1] >> 4) & 1) ? '1' : '0',
412 ((ptr[-1] >> 3) & 1) ? '1' : '0',
413 ((ptr[-1] >> 2) & 1) ? '1' : '0',
414 ((ptr[-1] >> 1) & 1) ? '1' : '0',
415 ((ptr[-1] >> 0) & 1) ? '1' : '0');
416
417 DEBUG("oriht: %c%c%c%c%c%c%c%c",
418 ((ptr[0] >> 7) & 1) ? '1' : '0',
419 ((ptr[0] >> 6) & 1) ? '1' : '0',
420 ((ptr[0] >> 5) & 1) ? '1' : '0',
421 ((ptr[0] >> 4) & 1) ? '1' : '0',
422 ((ptr[0] >> 3) & 1) ? '1' : '0',
423 ((ptr[0] >> 2) & 1) ? '1' : '0',
424 ((ptr[0] >> 1) & 1) ? '1' : '0',
425 ((ptr[0] >> 0) & 1) ? '1' : '0');
426
427 DEBUG("mriht: %c%c%c%c%c%c%c%c",
428 ((right >> 7) & 1) ? '1' : '0',
429 ((right >> 6) & 1) ? '1' : '0',
430 ((right >> 5) & 1) ? '1' : '0',
431 ((right >> 4) & 1) ? '1' : '0',
432 ((right >> 3) & 1) ? '1' : '0',
433 ((right >> 2) & 1) ? '1' : '0',
434 ((right >> 1) & 1) ? '1' : '0',
435 ((right >> 0) & 1) ? '1' : '0');
436
437 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
438
439 DEBUG("after: %c%c%c%c%c%c%c%c",
440 ((ptr[-1] >> 7) & 1) ? '1' : '0',
441 ((ptr[-1] >> 6) & 1) ? '1' : '0',
442 ((ptr[-1] >> 5) & 1) ? '1' : '0',
443 ((ptr[-1] >> 4) & 1) ? '1' : '0',
444 ((ptr[-1] >> 3) & 1) ? '1' : '0',
445 ((ptr[-1] >> 2) & 1) ? '1' : '0',
446 ((ptr[-1] >> 1) & 1) ? '1' : '0',
447 ((ptr[-1] >> 0) & 1) ? '1' : '0');
448 }
449
450 buffer_dump();
451
452 for(; ptr < end; ptr++) {
453 int right = ptr[1] >> (8 - bits);
454 *ptr = (*ptr << bits) | right;
455 }
456 *ptr <<= bits;
457
458 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
459 if(DynamicBuffer.unbits == 0) {
460 DynamicBuffer.unbits += bits;
461 } else {
462 DynamicBuffer.unbits += bits;
463 if(DynamicBuffer.unbits > 7) {
464 DynamicBuffer.unbits -= 8;
465 DynamicBuffer.length--;
466 DynamicBuffer.bytes_shifted++;
467 }
468 }
469 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
470
471 buffer_dump();
472
473 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
474 (long)DynamicBuffer.offset,
475 (long)DynamicBuffer.unbits,
476 (long)DynamicBuffer.length);
477
478
479}
480
vlm0e329f22004-10-28 13:22:54 +0000481/*
vlm6d44a542005-11-08 03:06:16 +0000482 * Ensure that the buffer contains at least this amount of free space.
vlm0e329f22004-10-28 13:22:54 +0000483 */
vlmcd83dde2006-09-18 20:05:34 +0000484static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
vlm0e329f22004-10-28 13:22:54 +0000485
vlmcd83dde2006-09-18 20:05:34 +0000486 if(bytes == 0) return;
487
488 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
489 (long)bytes,
vlmf5aff922006-09-13 04:02:00 +0000490 (long)DynamicBuffer.offset,
491 (long)DynamicBuffer.length,
vlmcd83dde2006-09-18 20:05:34 +0000492 (long)DynamicBuffer.unbits,
vlmf5aff922006-09-13 04:02:00 +0000493 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000494
vlmf5aff922006-09-13 04:02:00 +0000495 if(DynamicBuffer.allocated
vlmcd83dde2006-09-18 20:05:34 +0000496 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
vlmcb923822006-09-13 04:14:17 +0000497 DEBUG("\tNo buffer reallocation is necessary");
vlmcd83dde2006-09-18 20:05:34 +0000498 } else if(bytes <= DynamicBuffer.offset) {
vlmf5aff922006-09-13 04:02:00 +0000499 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
vlm0e329f22004-10-28 13:22:54 +0000500
501 /* Shift the buffer contents */
vlmf5aff922006-09-13 04:02:00 +0000502 memmove(DynamicBuffer.data,
503 DynamicBuffer.data + DynamicBuffer.offset,
504 DynamicBuffer.length);
505 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
506 DynamicBuffer.offset = 0;
vlm0e329f22004-10-28 13:22:54 +0000507 } else {
vlmcd83dde2006-09-18 20:05:34 +0000508 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
vlmf5aff922006-09-13 04:02:00 +0000509 void *p = MALLOC(newsize);
vlm4d2ca122005-12-07 05:46:03 +0000510 if(!p) {
vlmf5aff922006-09-13 04:02:00 +0000511 perror("malloc()");
vlm0e329f22004-10-28 13:22:54 +0000512 exit(EX_OSERR);
513 }
vlm84444f92006-09-17 08:23:43 +0000514 memcpy(p,
515 DynamicBuffer.data + DynamicBuffer.offset,
516 DynamicBuffer.length);
vlmf5aff922006-09-13 04:02:00 +0000517 FREEMEM(DynamicBuffer.data);
vlmfdf05da2006-09-24 19:47:07 +0000518 DynamicBuffer.data = (uint8_t *)p;
vlmf5aff922006-09-13 04:02:00 +0000519 DynamicBuffer.offset = 0;
520 DynamicBuffer.allocated = newsize;
521 DynamicBuffer.nreallocs++;
vlmcb923822006-09-13 04:14:17 +0000522 DEBUG("\tBuffer reallocated to %ld (%d time)",
vlmf5aff922006-09-13 04:02:00 +0000523 newsize, DynamicBuffer.nreallocs);
vlm0e329f22004-10-28 13:22:54 +0000524 }
vlm4d2ca122005-12-07 05:46:03 +0000525
vlmcd83dde2006-09-18 20:05:34 +0000526 memcpy(DynamicBuffer.data
527 + DynamicBuffer.offset + DynamicBuffer.length,
528 data2add, bytes);
529 DynamicBuffer.length += bytes;
530 if(DynamicBuffer.unbits) {
531 int bits = DynamicBuffer.unbits;
532 DynamicBuffer.unbits = 0;
533 buffer_shift_left(DynamicBuffer.length - bytes, bits);
534 }
535
536 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
537 (long)bytes,
538 (long)DynamicBuffer.offset,
539 (long)DynamicBuffer.length,
540 (long)DynamicBuffer.unbits,
541 (long)DynamicBuffer.allocated);
vlm0e329f22004-10-28 13:22:54 +0000542}
543
vlme0fb1e82006-09-15 18:33:25 +0000544static void *
vlme7e9bd92006-09-17 11:02:53 +0000545data_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 +0000546 static uint8_t *fbuf;
vlm0e329f22004-10-28 13:22:54 +0000547 static ssize_t fbuf_size;
548 static asn_codec_ctx_t s_codec_ctx;
549 asn_codec_ctx_t *opt_codec_ctx = 0;
550 void *structure = 0;
vlm6d44a542005-11-08 03:06:16 +0000551 asn_dec_rval_t rval;
vlme7e9bd92006-09-17 11:02:53 +0000552 size_t old_offset;
553 size_t new_offset;
vlmcd83dde2006-09-18 20:05:34 +0000554 int tolerate_eof;
vlm0e329f22004-10-28 13:22:54 +0000555 size_t rd;
vlme0fb1e82006-09-15 18:33:25 +0000556
557 if(!file) {
vlme7e9bd92006-09-17 11:02:53 +0000558 fprintf(stderr, "%s: %s\n", name, strerror(errno));
559 errno = EINVAL;
vlme0fb1e82006-09-15 18:33:25 +0000560 return 0;
561 }
vlm0e329f22004-10-28 13:22:54 +0000562
563 if(opt_stack) {
564 s_codec_ctx.max_stack_size = opt_stack;
565 opt_codec_ctx = &s_codec_ctx;
566 }
567
vlme7e9bd92006-09-17 11:02:53 +0000568 DEBUG("Processing %s", name);
vlm0e329f22004-10-28 13:22:54 +0000569
570 /* prepare the file buffer */
571 if(fbuf_size != suggested_bufsize) {
vlmfdf05da2006-09-24 19:47:07 +0000572 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
vlm0e329f22004-10-28 13:22:54 +0000573 if(!fbuf) {
574 perror("realloc()");
575 exit(EX_OSERR);
576 }
577 fbuf_size = suggested_bufsize;
578 }
579
vlme7e9bd92006-09-17 11:02:53 +0000580 if(on_first_pdu) {
581 DynamicBuffer.offset = 0;
582 DynamicBuffer.length = 0;
vlmcd83dde2006-09-18 20:05:34 +0000583 DynamicBuffer.unbits = 0;
vlme7e9bd92006-09-17 11:02:53 +0000584 DynamicBuffer.allocated = 0;
585 DynamicBuffer.bytes_shifted = 0;
586 DynamicBuffer.nreallocs = 0;
587 }
588
589 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
vlm0e329f22004-10-28 13:22:54 +0000590
vlm6d44a542005-11-08 03:06:16 +0000591 /* Pretend immediate EOF */
592 rval.code = RC_WMORE;
593 rval.consumed = 0;
594
vlmcd83dde2006-09-18 20:05:34 +0000595 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
596 (rd = fread(fbuf, 1, fbuf_size, file))
597 || feof(file) == 0
598 || (tolerate_eof && DynamicBuffer.length)
599 ;) {
vlmfdf05da2006-09-24 19:47:07 +0000600 int ecbits = 0; /* Extra consumed bits in case of PER */
601 uint8_t *i_bptr;
602 size_t i_size;
vlm0e329f22004-10-28 13:22:54 +0000603
604 /*
605 * Copy the data over, or use the original buffer.
606 */
vlmf5aff922006-09-13 04:02:00 +0000607 if(DynamicBuffer.allocated) {
vlmcd83dde2006-09-18 20:05:34 +0000608 /* Append new data into the existing dynamic buffer */
vlmf5aff922006-09-13 04:02:00 +0000609 add_bytes_to_buffer(fbuf, rd);
610 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
vlmcb923822006-09-13 04:14:17 +0000611 i_size = DynamicBuffer.length;
vlm0e329f22004-10-28 13:22:54 +0000612 } else {
vlm6d44a542005-11-08 03:06:16 +0000613 i_bptr = fbuf;
614 i_size = rd;
615 }
vlm0e329f22004-10-28 13:22:54 +0000616
vlme7e9bd92006-09-17 11:02:53 +0000617 DEBUG("Decoding %ld bytes", (long)i_size);
618
vlmfdf05da2006-09-24 19:47:07 +0000619#ifdef JUNKTEST
620 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
621#endif
622
vlm6d44a542005-11-08 03:06:16 +0000623 switch(iform) {
624 case INP_BER:
vlme3470e72005-03-10 13:39:03 +0000625 rval = ber_decode(opt_codec_ctx, pduType,
vlm6d44a542005-11-08 03:06:16 +0000626 (void **)&structure, i_bptr, i_size);
627 break;
628 case INP_XER:
629 rval = xer_decode(opt_codec_ctx, pduType,
630 (void **)&structure, i_bptr, i_size);
631 break;
vlm337167e2005-11-26 11:25:14 +0000632 case INP_PER:
vlma472cf22007-06-26 08:24:50 +0000633 if(opt_nopad)
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);
vlma472cf22007-06-26 08:24:50 +0000637 else
638 rval = uper_decode_complete(opt_codec_ctx, pduType,
639 (void **)&structure, i_bptr, i_size);
vlmfdf05da2006-09-24 19:47:07 +0000640 switch(rval.code) {
641 case RC_OK:
vlmfdf05da2006-09-24 19:47:07 +0000642 /* Fall through */
643 case RC_FAIL:
vlma472cf22007-06-26 08:24:50 +0000644 if(opt_nopad) {
645 /* uper_decode() returns bits! */
646 /* Extra bits */
647 ecbits = rval.consumed % 8;
648 /* Convert into bytes! */
649 rval.consumed /= 8;
650 }
vlmfdf05da2006-09-24 19:47:07 +0000651 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