blob: 12d1f898e25214fbd8b083a4b495dc3e4e9c7a38 [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 Walkin1f12da42006-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
Lev Walkin1d9e8dd2005-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}
Lev Walkin22b54552004-10-28 13:22:54 +000085
86int
Lev Walkinc744a022006-09-15 18:33:25 +000087main(int ac, char *av[]) {
Lev Walkind0625682006-08-25 02:35:08 +000088 static asn_TYPE_descriptor_t *pduType = &PDU_Type;
Lev Walkin22b54552004-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
Lev Walkin59b176e2005-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
Lev Walkin22b54552004-10-28 13:22:54 +000098 /*
99 * Pocess the command-line argments.
100 */
Lev Walkin1f12da42006-09-24 19:47:07 +0000101 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
Lev Walkin22b54552004-10-28 13:22:54 +0000102 switch(ch) {
Lev Walkind1bfea62005-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; }
Lev Walkin59b176e2005-11-26 11:25:14 +0000106 if(pduType->uper_decoder
107 && optarg[0] == 'p') { iform = INP_PER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000108 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000109 optarg);
110 exit(EX_UNAVAILABLE);
111 case 'o':
112 if(optarg[0] == 'd') { oform = OUT_DER; break; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000113 if(pduType->uper_encoder
114 && optarg[0] == 'p') { oform = OUT_PER; break; }
Lev Walkind1bfea62005-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; }
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000118 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
Lev Walkind1bfea62005-11-08 03:06:16 +0000119 optarg);
120 exit(EX_UNAVAILABLE);
Lev Walkinbc691772006-09-17 11:02:53 +0000121 case '1':
122 opt_onepdu = 1;
123 break;
Lev Walkin22b54552004-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;
Lev Walkinbc691772006-09-17 11:02:53 +0000148 case 'p':
Lev Walkin5a621d62006-09-18 20:05:34 +0000149 if(strcmp(optarg, "er-padded") == 0) {
150 opt_ippad = 1;
151 break;
152 }
Lev Walkinbc691772006-09-17 11:02:53 +0000153#ifdef ASN_PDU_COLLECTION
Lev Walkin5a621d62006-09-18 20:05:34 +0000154 if(strcmp(optarg, "list") == 0) {
Lev Walkinbc691772006-09-17 11:02:53 +0000155 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
Lev Walkin5a621d62006-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;
Lev Walkinbc691772006-09-17 11:02:53 +0000161 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
162 if(*pdu) { pduType = *pdu; break; }
Lev Walkin5a621d62006-09-18 20:05:34 +0000163 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkinbc691772006-09-17 11:02:53 +0000164 }
165#endif /* ASN_PDU_COLLECTION */
Lev Walkin5a621d62006-09-18 20:05:34 +0000166 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
Lev Walkinbc691772006-09-17 11:02:53 +0000167 exit(EX_UNAVAILABLE);
Lev Walkin22b54552004-10-28 13:22:54 +0000168 case 's':
169 opt_stack = atoi(optarg);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000170 if(opt_stack < 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000171 fprintf(stderr,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000172 "-s %s: Non-negative value expected\n",
Lev Walkin22b54552004-10-28 13:22:54 +0000173 optarg);
174 exit(EX_UNAVAILABLE);
175 }
176 break;
Lev Walkin1f12da42006-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 */
Lev Walkin22b54552004-10-28 13:22:54 +0000188 case 'h':
189 default:
Lev Walkin59b176e2005-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)
Lev Walkin22b54552004-10-28 13:22:54 +0000193 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000194 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000195 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000196 " -iber Input is in BER (Basic Encoding Rules)%s\n",
197 iform == INP_PER ? "" : " (DEFAULT)");
Lev Walkin59b176e2005-11-26 11:25:14 +0000198 fprintf(stderr,
Lev Walkinbe3a0c52006-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,
Lev Walkind1bfea62005-11-08 03:06:16 +0000204 " -oder Output in DER (Distinguished Encoding Rules)\n"
Lev Walkin857d11b2006-02-22 08:54:49 +0000205 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
Lev Walkind1bfea62005-11-08 03:06:16 +0000206 " -otext Output in plain semi-structured text (dump)\n"
Lev Walkin59b176e2005-11-26 11:25:14 +0000207 " -onull Verify (decode) input, but do not output\n");
Lev Walkin5a621d62006-09-18 20:05:34 +0000208 if(pduType->uper_decoder)
209 fprintf(stderr,
210 " -per-padded Assume PER PDUs are byte-padded (-iper)\n");
Lev Walkin59b176e2005-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,
Lev Walkinc8edcb82006-09-18 22:30:55 +0000217 " -1 Decode only the first PDU in file\n"
Lev Walkin22b54552004-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"
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000222 " -s <size> Set the stack usage limit (default is %d)\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000223#ifdef JUNKTEST
224 " -J <prob> Set random junk test bit garbaging probability\n"
225#endif
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000226 , (long)suggested_bufsize, _ASN_DEFAULT_STACK_MAX);
Lev Walkin22b54552004-10-28 13:22:54 +0000227 exit(EX_USAGE);
228 }
229
230 ac -= optind;
231 av += optind;
232
233 if(ac < 1) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000234 fprintf(stderr, "%s: No input files specified. "
235 "Try '-h' for more information\n",
236 av[-optind]);
Lev Walkin22b54552004-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++) {
Lev Walkinbc691772006-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000253
Lev Walkinbc691772006-09-17 11:02:53 +0000254 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000255 /*
256 * Decode the encoded structure from file.
257 */
Lev Walkin00949562005-03-10 13:39:03 +0000258 structure = data_decode_from_file(pduType,
Lev Walkinbc691772006-09-17 11:02:53 +0000259 file, name, suggested_bufsize, first_pdu);
Lev Walkin22b54552004-10-28 13:22:54 +0000260 if(!structure) {
Lev Walkinbc691772006-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 }
Lev Walkin22b54552004-10-28 13:22:54 +0000268 }
269
Lev Walkin22b54552004-10-28 13:22:54 +0000270 /* Check ASN.1 constraints */
271 if(opt_check) {
272 char errbuf[128];
273 size_t errlen = sizeof(errbuf);
Lev Walkin00949562005-03-10 13:39:03 +0000274 if(asn_check_constraints(pduType, structure,
Lev Walkin22b54552004-10-28 13:22:54 +0000275 errbuf, &errlen)) {
276 fprintf(stderr, "%s: ASN.1 constraint "
Lev Walkinc744a022006-09-15 18:33:25 +0000277 "check failed: %s\n", name, errbuf);
Lev Walkin22b54552004-10-28 13:22:54 +0000278 exit(EX_DATAERR);
279 }
280 }
281
Lev Walkind1bfea62005-11-08 03:06:16 +0000282 switch(oform) {
283 case OUT_NULL:
Lev Walkin1f12da42006-09-24 19:47:07 +0000284#ifdef JUNKTEST
285 if(opt_jprob == 0.0)
286#endif
Lev Walkinc744a022006-09-15 18:33:25 +0000287 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkind1bfea62005-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)) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000294 fprintf(stderr,
295 "%s: Cannot convert %s into XML\n",
296 name, pduType->name);
Lev Walkind1bfea62005-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) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000303 fprintf(stderr,
304 "%s: Cannot convert %s into DER\n",
305 name, pduType->name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000306 exit(EX_UNAVAILABLE);
307 }
Lev Walkinbc691772006-09-17 11:02:53 +0000308 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
Lev Walkind1bfea62005-11-08 03:06:16 +0000309 break;
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000310 case OUT_PER:
311 erv = uper_encode(pduType, structure, write_out, stdout);
312 if(erv.encoded < 0) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000313 fprintf(stderr,
314 "%s: Cannot convert %s into Unaligned PER\n",
315 name, pduType->name);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000316 exit(EX_UNAVAILABLE);
317 }
Lev Walkinbc691772006-09-17 11:02:53 +0000318 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000319 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000320 }
321
Lev Walkinadcb5862006-03-17 02:11:12 +0000322 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinbc691772006-09-17 11:02:53 +0000323 }
324
325 if(file && file != stdin)
326 fclose(file);
Lev Walkin22b54552004-10-28 13:22:54 +0000327 }
328 }
329
Lev Walkin1f12da42006-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
Lev Walkin22b54552004-10-28 13:22:54 +0000337 return 0;
338}
339
Lev Walkin419f6752006-09-13 04:02:00 +0000340static struct dynamic_buffer {
Lev Walkin5a621d62006-09-18 20:05:34 +0000341 uint8_t *data; /* Pointer to the data bytes */
Lev Walkin419f6752006-09-13 04:02:00 +0000342 size_t offset; /* Offset from the start */
343 size_t length; /* Length of meaningful contents */
Lev Walkin5a621d62006-09-18 20:05:34 +0000344 size_t unbits; /* Unused bits in the last byte */
Lev Walkin419f6752006-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000349
Lev Walkin5a621d62006-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) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000374 unsigned int shift;
Lev Walkin5a621d62006-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');
Lev Walkin1f12da42006-09-24 19:47:07 +0000378 fprintf(stderr, " %ld:%ld\n",
379 (long)DynamicBuffer.length - 1,
380 (long)8 - DynamicBuffer.unbits);
Lev Walkin5a621d62006-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
Lev Walkin22b54552004-10-28 13:22:54 +0000482/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000483 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000484 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000485static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000486
Lev Walkin5a621d62006-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,
Lev Walkin419f6752006-09-13 04:02:00 +0000491 (long)DynamicBuffer.offset,
492 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000493 (long)DynamicBuffer.unbits,
Lev Walkin419f6752006-09-13 04:02:00 +0000494 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000495
Lev Walkin419f6752006-09-13 04:02:00 +0000496 if(DynamicBuffer.allocated
Lev Walkin5a621d62006-09-18 20:05:34 +0000497 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
Lev Walkinf9492a92006-09-13 04:14:17 +0000498 DEBUG("\tNo buffer reallocation is necessary");
Lev Walkin5a621d62006-09-18 20:05:34 +0000499 } else if(bytes <= DynamicBuffer.offset) {
Lev Walkin419f6752006-09-13 04:02:00 +0000500 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000501
502 /* Shift the buffer contents */
Lev Walkin419f6752006-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000508 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000509 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
Lev Walkin419f6752006-09-13 04:02:00 +0000510 void *p = MALLOC(newsize);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000511 if(!p) {
Lev Walkin419f6752006-09-13 04:02:00 +0000512 perror("malloc()");
Lev Walkin22b54552004-10-28 13:22:54 +0000513 exit(EX_OSERR);
514 }
Lev Walkine1baa4d2006-09-17 08:23:43 +0000515 memcpy(p,
516 DynamicBuffer.data + DynamicBuffer.offset,
517 DynamicBuffer.length);
Lev Walkin419f6752006-09-13 04:02:00 +0000518 FREEMEM(DynamicBuffer.data);
Lev Walkin1f12da42006-09-24 19:47:07 +0000519 DynamicBuffer.data = (uint8_t *)p;
Lev Walkin419f6752006-09-13 04:02:00 +0000520 DynamicBuffer.offset = 0;
521 DynamicBuffer.allocated = newsize;
522 DynamicBuffer.nreallocs++;
Lev Walkinf9492a92006-09-13 04:14:17 +0000523 DEBUG("\tBuffer reallocated to %ld (%d time)",
Lev Walkin419f6752006-09-13 04:02:00 +0000524 newsize, DynamicBuffer.nreallocs);
Lev Walkin22b54552004-10-28 13:22:54 +0000525 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000526
Lev Walkin5a621d62006-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);
Lev Walkin22b54552004-10-28 13:22:54 +0000543}
544
Lev Walkinc744a022006-09-15 18:33:25 +0000545static void *
Lev Walkinbc691772006-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) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000547 static uint8_t *fbuf;
Lev Walkin22b54552004-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;
Lev Walkind1bfea62005-11-08 03:06:16 +0000552 asn_dec_rval_t rval;
Lev Walkinbc691772006-09-17 11:02:53 +0000553 size_t old_offset;
554 size_t new_offset;
Lev Walkin5a621d62006-09-18 20:05:34 +0000555 int tolerate_eof;
Lev Walkin22b54552004-10-28 13:22:54 +0000556 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000557
558 if(!file) {
Lev Walkinbc691772006-09-17 11:02:53 +0000559 fprintf(stderr, "%s: %s\n", name, strerror(errno));
560 errno = EINVAL;
Lev Walkinc744a022006-09-15 18:33:25 +0000561 return 0;
562 }
Lev Walkin22b54552004-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
Lev Walkinbc691772006-09-17 11:02:53 +0000569 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000570
571 /* prepare the file buffer */
572 if(fbuf_size != suggested_bufsize) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000573 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000574 if(!fbuf) {
575 perror("realloc()");
576 exit(EX_OSERR);
577 }
578 fbuf_size = suggested_bufsize;
579 }
580
Lev Walkinbc691772006-09-17 11:02:53 +0000581 if(on_first_pdu) {
582 DynamicBuffer.offset = 0;
583 DynamicBuffer.length = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000584 DynamicBuffer.unbits = 0;
Lev Walkinbc691772006-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000591
Lev Walkind1bfea62005-11-08 03:06:16 +0000592 /* Pretend immediate EOF */
593 rval.code = RC_WMORE;
594 rval.consumed = 0;
595
Lev Walkin5a621d62006-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 ;) {
Lev Walkin1f12da42006-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;
Lev Walkin22b54552004-10-28 13:22:54 +0000604
605 /*
606 * Copy the data over, or use the original buffer.
607 */
Lev Walkin419f6752006-09-13 04:02:00 +0000608 if(DynamicBuffer.allocated) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000609 /* Append new data into the existing dynamic buffer */
Lev Walkin419f6752006-09-13 04:02:00 +0000610 add_bytes_to_buffer(fbuf, rd);
611 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
Lev Walkinf9492a92006-09-13 04:14:17 +0000612 i_size = DynamicBuffer.length;
Lev Walkin22b54552004-10-28 13:22:54 +0000613 } else {
Lev Walkind1bfea62005-11-08 03:06:16 +0000614 i_bptr = fbuf;
615 i_size = rd;
616 }
Lev Walkin22b54552004-10-28 13:22:54 +0000617
Lev Walkinbc691772006-09-17 11:02:53 +0000618 DEBUG("Decoding %ld bytes", (long)i_size);
619
Lev Walkin1f12da42006-09-24 19:47:07 +0000620#ifdef JUNKTEST
621 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
622#endif
623
Lev Walkind1bfea62005-11-08 03:06:16 +0000624 switch(iform) {
625 case INP_BER:
Lev Walkin00949562005-03-10 13:39:03 +0000626 rval = ber_decode(opt_codec_ctx, pduType,
Lev Walkind1bfea62005-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;
Lev Walkin59b176e2005-11-26 11:25:14 +0000633 case INP_PER:
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000634 rval = uper_decode(opt_codec_ctx, pduType,
Lev Walkin5a621d62006-09-18 20:05:34 +0000635 (void **)&structure, i_bptr, i_size, 0,
636 DynamicBuffer.unbits);
Lev Walkin1f12da42006-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;
Lev Walkin5a621d62006-09-18 20:05:34 +0000659 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000660 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000661 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000662 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
Lev Walkin419f6752006-09-13 04:02:00 +0000663 (long)DynamicBuffer.length,
Lev Walkin4a858bb2006-09-17 11:22:00 +0000664 (long)rval.consumed, ecbits, (long)i_size,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000665 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000666
Lev Walkin419f6752006-09-13 04:02:00 +0000667 if(DynamicBuffer.allocated == 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000668 /*
Lev Walkinbc691772006-09-17 11:02:53 +0000669 * Flush remainder into the intermediate buffer.
Lev Walkin22b54552004-10-28 13:22:54 +0000670 */
671 if(rval.code != RC_FAIL && rval.consumed < rd) {
Lev Walkin419f6752006-09-13 04:02:00 +0000672 add_bytes_to_buffer(fbuf + rval.consumed,
Lev Walkin5a621d62006-09-18 20:05:34 +0000673 rd - rval.consumed);
674 buffer_shift_left(0, ecbits);
675 DynamicBuffer.bytes_shifted = rval.consumed;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000676 rval.consumed = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000677 ecbits = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000678 }
679 }
680
Lev Walkinbc691772006-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
Lev Walkin22b54552004-10-28 13:22:54 +0000691 switch(rval.code) {
692 case RC_OK:
Lev Walkin5a621d62006-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);
Lev Walkin22b54552004-10-28 13:22:54 +0000696 return structure;
697 case RC_WMORE:
Lev Walkin5a621d62006-09-18 20:05:34 +0000698 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
699 " with %ld..%ld-%ld..%ld",
700 (long)rd,
Lev Walkinf9492a92006-09-13 04:14:17 +0000701 (long)rval.consumed,
702 (long)DynamicBuffer.offset,
703 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000704 (long)DynamicBuffer.unbits,
Lev Walkinf9492a92006-09-13 04:14:17 +0000705 (long)DynamicBuffer.allocated);
Lev Walkin5a621d62006-09-18 20:05:34 +0000706 if(!rd) tolerate_eof--;
Lev Walkin22b54552004-10-28 13:22:54 +0000707 continue;
708 case RC_FAIL:
709 break;
710 }
711 break;
712 }
713
Lev Walkin1f12da42006-09-24 19:47:07 +0000714 DEBUG("Clean up partially decoded structure");
Lev Walkinadcb5862006-03-17 02:11:12 +0000715 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000716
Lev Walkinbc691772006-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 */
Lev Walkin00918812006-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 ) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000727
728#ifdef JUNKTEST
729 /*
730 * Nothing's wrong with being unable to decode junk.
731 * Simulate EOF.
732 */
733 junk_failures++;
734 errno = 0;
735 return 0;
736#endif
737
Lev Walkin5a621d62006-09-18 20:05:34 +0000738 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
739 on_first_pdu, (long)new_offset, (long)old_offset,
740 (long)DynamicBuffer.length);
Lev Walkinbc691772006-09-17 11:02:53 +0000741 fprintf(stderr, "%s: "
742 "Decode failed past byte %ld: %s\n",
743 name, (long)new_offset,
744 (rval.code == RC_WMORE)
745 ? "Unexpected end of input"
746 : "Input processing error");
Lev Walkin4bf96b92006-09-18 21:46:50 +0000747#ifndef ENOMSG
748#define ENOMSG EINVAL
749#endif
750#ifndef EBADMSG
751#define EBADMSG EINVAL
752#endif
Lev Walkinbc691772006-09-17 11:02:53 +0000753 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
754 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000755 /* Got EOF after a few successful PDUs */
Lev Walkinbc691772006-09-17 11:02:53 +0000756 errno = 0;
757 }
Lev Walkin22b54552004-10-28 13:22:54 +0000758
759 return 0;
760}
761
Lev Walkin419f6752006-09-13 04:02:00 +0000762/* Dump the buffer out to the specified FILE */
763static int write_out(const void *buffer, size_t size, void *key) {
764 FILE *fp = (FILE *)key;
765 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
766}
Lev Walkinc744a022006-09-15 18:33:25 +0000767
768static int argument_is_stdin(char *av[], int idx) {
769 if(strcmp(av[idx], "-")) {
770 return 0; /* Certainly not <stdin> */
771 } else {
772 /* This might be <stdin>, unless `./program -- -` */
773 if(strcmp(av[-1], "--"))
774 return 1;
775 else
776 return 0;
777 }
778}
779
780static FILE *argument_to_file(char *av[], int idx) {
781 return argument_is_stdin(av, idx)
782 ? stdin
783 : fopen(av[idx], "r");
784}
785
786static char *argument_to_name(char *av[], int idx) {
787 return argument_is_stdin(av, idx)
788 ? "standard input"
789 : av[idx];
790}
Lev Walkin1f12da42006-09-24 19:47:07 +0000791
792#ifdef JUNKTEST
793/*
794 * Fill bytes with some garbage with specified probability (more or less).
795 */
796static void
797junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
798 static int junkmode;
799 uint8_t *ptr;
800 uint8_t *end;
801 if(opt_jprob <= 0.0) return;
802 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
803 int byte = *ptr;
804 if(junkmode++ & 1) {
805 if((((double)random() / RAND_MAX) < prob))
806 byte = random() & 0xff;
807 } else {
808#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
809 byte ^= BPROB(0x80);
810 byte ^= BPROB(0x40);
811 byte ^= BPROB(0x20);
812 byte ^= BPROB(0x10);
813 byte ^= BPROB(0x08);
814 byte ^= BPROB(0x04);
815 byte ^= BPROB(0x02);
816 byte ^= BPROB(0x01);
817 }
818 if(byte != *ptr) {
819 DEBUG("Junk buf[%d] %02x -> %02x",
820 ptr - buf, *ptr, byte);
821 *ptr = byte;
822 }
823 }
824}
825#endif /* JUNKTEST */
826