blob: de42326d1932476511c04d5b41510e7459241157 [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.
Lev Walkin8a4a06c2007-06-29 12:44:01 +00003 * Copyright (c) 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
4 * All rights reserved.
Lev Walkin22b54552004-10-28 13:22:54 +00005 *
Lev Walkind0625682006-08-25 02:35:08 +00006 * To compile with your own ASN.1 type, please redefine the PDU as shown:
Lev Walkin22b54552004-10-28 13:22:54 +00007 *
Lev Walkind0625682006-08-25 02:35:08 +00008 * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.c
Lev Walkin22b54552004-10-28 13:22:54 +00009 */
10#ifdef HAVE_CONFIG_H
11#include <config.h>
12#endif
13#include <stdio.h>
14#include <sys/types.h>
Lev Walkin4696c742005-08-22 12:23:54 +000015#include <stdlib.h> /* for atoi(3) */
16#include <unistd.h> /* for getopt(3) */
Lev Walkin22b54552004-10-28 13:22:54 +000017#include <string.h> /* for strerror(3) */
Lev Walkin22b54552004-10-28 13:22:54 +000018#include <sysexits.h> /* for EX_* exit codes */
Lev Walkinfc776432005-03-29 17:21:14 +000019#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 Walkin7c1dc052016-03-14 03:08:15 -070022#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) */
Lev Walkin08b30bb2007-06-26 08:24:50 +000046static int opt_nopad; /* -per-nopad (PER input is not padded) */
Lev Walkin5a621d62006-09-18 20:05:34 +000047static 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 Walkin08b30bb2007-06-26 08:24:50 +0000149 if(strcmp(optarg, "er-nopad") == 0) {
150 opt_nopad = 1;
Lev Walkin5a621d62006-09-18 20:05:34 +0000151 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 Walkin8a4a06c2007-06-29 12:44:01 +0000190#ifdef ASN_CONVERTER_TITLE
191#define _AXS(x) #x
192#define _ASX(x) _AXS(x)
193 fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE));
194#endif
Lev Walkin59b176e2005-11-26 11:25:14 +0000195 fprintf(stderr, "Usage: %s [options] <data.ber> ...\n", av[0]);
196 fprintf(stderr, "Where options are:\n");
197 if(pduType->uper_decoder)
Lev Walkin22b54552004-10-28 13:22:54 +0000198 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000199 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000200 fprintf(stderr,
Lev Walkin857d11b2006-02-22 08:54:49 +0000201 " -iber Input is in BER (Basic Encoding Rules)%s\n",
202 iform == INP_PER ? "" : " (DEFAULT)");
Lev Walkin59b176e2005-11-26 11:25:14 +0000203 fprintf(stderr,
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000204 " -ixer Input is in XER (XML Encoding Rules)\n");
205 if(pduType->uper_encoder)
206 fprintf(stderr,
207 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
208 fprintf(stderr,
Lev Walkind1bfea62005-11-08 03:06:16 +0000209 " -oder Output in DER (Distinguished Encoding Rules)\n"
Lev Walkin857d11b2006-02-22 08:54:49 +0000210 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
Lev Walkind1bfea62005-11-08 03:06:16 +0000211 " -otext Output in plain semi-structured text (dump)\n"
Lev Walkin59b176e2005-11-26 11:25:14 +0000212 " -onull Verify (decode) input, but do not output\n");
Lev Walkin5a621d62006-09-18 20:05:34 +0000213 if(pduType->uper_decoder)
214 fprintf(stderr,
Lev Walkin08b30bb2007-06-26 08:24:50 +0000215 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000216#ifdef ASN_PDU_COLLECTION
217 fprintf(stderr,
218 " -p <PDU> Specify PDU type to decode\n"
219 " -p list List available PDUs\n");
220#endif /* ASN_PDU_COLLECTION */
221 fprintf(stderr,
Lev Walkinc8edcb82006-09-18 22:30:55 +0000222 " -1 Decode only the first PDU in file\n"
Lev Walkin22b54552004-10-28 13:22:54 +0000223 " -b <size> Set the i/o buffer size (default is %ld)\n"
224 " -c Check ASN.1 constraints after decoding\n"
225 " -d Enable debugging (-dd is even better)\n"
226 " -n <num> Process files <num> times\n"
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000227 " -s <size> Set the stack usage limit (default is %d)\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000228#ifdef JUNKTEST
229 " -J <prob> Set random junk test bit garbaging probability\n"
230#endif
Lev Walkin7c1dc052016-03-14 03:08:15 -0700231 , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX);
Lev Walkin22b54552004-10-28 13:22:54 +0000232 exit(EX_USAGE);
233 }
234
235 ac -= optind;
236 av += optind;
237
238 if(ac < 1) {
Lev Walkind1bfea62005-11-08 03:06:16 +0000239 fprintf(stderr, "%s: No input files specified. "
240 "Try '-h' for more information\n",
241 av[-optind]);
Lev Walkin22b54552004-10-28 13:22:54 +0000242 exit(EX_USAGE);
243 }
244
245 setvbuf(stdout, 0, _IOLBF, 0);
246
247 for(num = 0; num < number_of_iterations; num++) {
248 int ac_i;
249 /*
250 * Process all files in turn.
251 */
252 for(ac_i = 0; ac_i < ac; ac_i++) {
Lev Walkinbc691772006-09-17 11:02:53 +0000253 asn_enc_rval_t erv;
254 void *structure; /* Decoded structure */
255 FILE *file = argument_to_file(av, ac_i);
256 char *name = argument_to_name(av, ac_i);
257 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000258
Lev Walkinbc691772006-09-17 11:02:53 +0000259 for(first_pdu = 1; first_pdu || !opt_onepdu; first_pdu = 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000260 /*
261 * Decode the encoded structure from file.
262 */
Lev Walkin00949562005-03-10 13:39:03 +0000263 structure = data_decode_from_file(pduType,
Lev Walkinbc691772006-09-17 11:02:53 +0000264 file, name, suggested_bufsize, first_pdu);
Lev Walkin22b54552004-10-28 13:22:54 +0000265 if(!structure) {
Lev Walkinbc691772006-09-17 11:02:53 +0000266 if(errno) {
267 /* Error message is already printed */
268 exit(EX_DATAERR);
269 } else {
270 /* EOF */
271 break;
272 }
Lev Walkin22b54552004-10-28 13:22:54 +0000273 }
274
Lev Walkin22b54552004-10-28 13:22:54 +0000275 /* Check ASN.1 constraints */
276 if(opt_check) {
277 char errbuf[128];
278 size_t errlen = sizeof(errbuf);
Lev Walkin00949562005-03-10 13:39:03 +0000279 if(asn_check_constraints(pduType, structure,
Lev Walkin22b54552004-10-28 13:22:54 +0000280 errbuf, &errlen)) {
281 fprintf(stderr, "%s: ASN.1 constraint "
Lev Walkinc744a022006-09-15 18:33:25 +0000282 "check failed: %s\n", name, errbuf);
Lev Walkin22b54552004-10-28 13:22:54 +0000283 exit(EX_DATAERR);
284 }
285 }
286
Lev Walkind1bfea62005-11-08 03:06:16 +0000287 switch(oform) {
288 case OUT_NULL:
Lev Walkin1f12da42006-09-24 19:47:07 +0000289#ifdef JUNKTEST
290 if(opt_jprob == 0.0)
291#endif
Lev Walkinc744a022006-09-15 18:33:25 +0000292 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000293 break;
294 case OUT_TEXT: /* -otext */
295 asn_fprint(stdout, pduType, structure);
296 break;
297 case OUT_XER: /* -oxer */
298 if(xer_fprint(stdout, pduType, structure)) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000299 fprintf(stderr,
300 "%s: Cannot convert %s into XML\n",
301 name, pduType->name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000302 exit(EX_UNAVAILABLE);
303 }
304 break;
305 case OUT_DER:
306 erv = der_encode(pduType, structure, write_out, stdout);
307 if(erv.encoded < 0) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000308 fprintf(stderr,
309 "%s: Cannot convert %s into DER\n",
310 name, pduType->name);
Lev Walkind1bfea62005-11-08 03:06:16 +0000311 exit(EX_UNAVAILABLE);
312 }
Lev Walkinbc691772006-09-17 11:02:53 +0000313 DEBUG("Encoded in %ld bytes of DER", (long)erv.encoded);
Lev Walkind1bfea62005-11-08 03:06:16 +0000314 break;
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000315 case OUT_PER:
316 erv = uper_encode(pduType, structure, write_out, stdout);
317 if(erv.encoded < 0) {
Lev Walkinefbba4a2006-09-18 20:26:24 +0000318 fprintf(stderr,
319 "%s: Cannot convert %s into Unaligned PER\n",
320 name, pduType->name);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000321 exit(EX_UNAVAILABLE);
322 }
Lev Walkinbc691772006-09-17 11:02:53 +0000323 DEBUG("Encoded in %ld bits of UPER", (long)erv.encoded);
Lev Walkinbe3a0c52006-08-25 04:56:21 +0000324 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000325 }
326
Lev Walkinadcb5862006-03-17 02:11:12 +0000327 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinbc691772006-09-17 11:02:53 +0000328 }
329
330 if(file && file != stdin)
331 fclose(file);
Lev Walkin22b54552004-10-28 13:22:54 +0000332 }
333 }
334
Lev Walkin1f12da42006-09-24 19:47:07 +0000335#ifdef JUNKTEST
336 if(opt_jprob > 0.0) {
337 fprintf(stderr, "Junked %f OK (%d/%d)\n",
338 opt_jprob, junk_failures, number_of_iterations);
339 }
340#endif /* JUNKTEST */
341
Lev Walkin22b54552004-10-28 13:22:54 +0000342 return 0;
343}
344
Lev Walkin419f6752006-09-13 04:02:00 +0000345static struct dynamic_buffer {
Lev Walkin5a621d62006-09-18 20:05:34 +0000346 uint8_t *data; /* Pointer to the data bytes */
Lev Walkin419f6752006-09-13 04:02:00 +0000347 size_t offset; /* Offset from the start */
348 size_t length; /* Length of meaningful contents */
Lev Walkin5a621d62006-09-18 20:05:34 +0000349 size_t unbits; /* Unused bits in the last byte */
Lev Walkin419f6752006-09-13 04:02:00 +0000350 size_t allocated; /* Allocated memory for data */
351 int nreallocs; /* Number of data reallocations */
352 off_t bytes_shifted; /* Number of bytes ever shifted */
353} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000354
Lev Walkin5a621d62006-09-18 20:05:34 +0000355static void
356buffer_dump() {
357 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
358 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
359 if(!opt_debug) return;
360 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
361 DynamicBuffer.data,
362 (long)DynamicBuffer.offset,
363 (long)DynamicBuffer.length,
364 (long)DynamicBuffer.unbits,
365 (long)DynamicBuffer.allocated,
366 (long)DynamicBuffer.bytes_shifted);
367 for(; p < e; p++) {
368 fprintf(stderr, " %c%c%c%c%c%c%c%c",
369 ((*p >> 7) & 1) ? '1' : '0',
370 ((*p >> 6) & 1) ? '1' : '0',
371 ((*p >> 5) & 1) ? '1' : '0',
372 ((*p >> 4) & 1) ? '1' : '0',
373 ((*p >> 3) & 1) ? '1' : '0',
374 ((*p >> 2) & 1) ? '1' : '0',
375 ((*p >> 1) & 1) ? '1' : '0',
376 ((*p >> 0) & 1) ? '1' : '0');
377 }
378 if(DynamicBuffer.unbits) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000379 unsigned int shift;
Lev Walkin5a621d62006-09-18 20:05:34 +0000380 fprintf(stderr, " ");
381 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
382 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
Lev Walkin1f12da42006-09-24 19:47:07 +0000383 fprintf(stderr, " %ld:%ld\n",
384 (long)DynamicBuffer.length - 1,
385 (long)8 - DynamicBuffer.unbits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000386 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400387 fprintf(stderr, " %ld\n", (long)DynamicBuffer.length);
Lev Walkin5a621d62006-09-18 20:05:34 +0000388 }
389}
390
391/*
392 * Move the buffer content left N bits, possibly joining it with
393 * preceeding content.
394 */
395static void
396buffer_shift_left(size_t offset, int bits) {
397 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
398 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
399 + DynamicBuffer.length - 1;
400
401 if(!bits) return;
402
403 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
404 bits, (long)offset,
405 (long)DynamicBuffer.offset,
406 (long)DynamicBuffer.unbits,
407 (long)DynamicBuffer.length);
408
409 if(offset) {
410 int right;
411 right = ptr[0] >> (8 - bits);
412
413 DEBUG("oleft: %c%c%c%c%c%c%c%c",
414 ((ptr[-1] >> 7) & 1) ? '1' : '0',
415 ((ptr[-1] >> 6) & 1) ? '1' : '0',
416 ((ptr[-1] >> 5) & 1) ? '1' : '0',
417 ((ptr[-1] >> 4) & 1) ? '1' : '0',
418 ((ptr[-1] >> 3) & 1) ? '1' : '0',
419 ((ptr[-1] >> 2) & 1) ? '1' : '0',
420 ((ptr[-1] >> 1) & 1) ? '1' : '0',
421 ((ptr[-1] >> 0) & 1) ? '1' : '0');
422
423 DEBUG("oriht: %c%c%c%c%c%c%c%c",
424 ((ptr[0] >> 7) & 1) ? '1' : '0',
425 ((ptr[0] >> 6) & 1) ? '1' : '0',
426 ((ptr[0] >> 5) & 1) ? '1' : '0',
427 ((ptr[0] >> 4) & 1) ? '1' : '0',
428 ((ptr[0] >> 3) & 1) ? '1' : '0',
429 ((ptr[0] >> 2) & 1) ? '1' : '0',
430 ((ptr[0] >> 1) & 1) ? '1' : '0',
431 ((ptr[0] >> 0) & 1) ? '1' : '0');
432
433 DEBUG("mriht: %c%c%c%c%c%c%c%c",
434 ((right >> 7) & 1) ? '1' : '0',
435 ((right >> 6) & 1) ? '1' : '0',
436 ((right >> 5) & 1) ? '1' : '0',
437 ((right >> 4) & 1) ? '1' : '0',
438 ((right >> 3) & 1) ? '1' : '0',
439 ((right >> 2) & 1) ? '1' : '0',
440 ((right >> 1) & 1) ? '1' : '0',
441 ((right >> 0) & 1) ? '1' : '0');
442
443 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
444
445 DEBUG("after: %c%c%c%c%c%c%c%c",
446 ((ptr[-1] >> 7) & 1) ? '1' : '0',
447 ((ptr[-1] >> 6) & 1) ? '1' : '0',
448 ((ptr[-1] >> 5) & 1) ? '1' : '0',
449 ((ptr[-1] >> 4) & 1) ? '1' : '0',
450 ((ptr[-1] >> 3) & 1) ? '1' : '0',
451 ((ptr[-1] >> 2) & 1) ? '1' : '0',
452 ((ptr[-1] >> 1) & 1) ? '1' : '0',
453 ((ptr[-1] >> 0) & 1) ? '1' : '0');
454 }
455
456 buffer_dump();
457
458 for(; ptr < end; ptr++) {
459 int right = ptr[1] >> (8 - bits);
460 *ptr = (*ptr << bits) | right;
461 }
462 *ptr <<= bits;
463
464 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
465 if(DynamicBuffer.unbits == 0) {
466 DynamicBuffer.unbits += bits;
467 } else {
468 DynamicBuffer.unbits += bits;
469 if(DynamicBuffer.unbits > 7) {
470 DynamicBuffer.unbits -= 8;
471 DynamicBuffer.length--;
472 DynamicBuffer.bytes_shifted++;
473 }
474 }
475 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
476
477 buffer_dump();
478
479 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
480 (long)DynamicBuffer.offset,
481 (long)DynamicBuffer.unbits,
482 (long)DynamicBuffer.length);
483
484
485}
486
Lev Walkin22b54552004-10-28 13:22:54 +0000487/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000488 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000489 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000490static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000491
Lev Walkin5a621d62006-09-18 20:05:34 +0000492 if(bytes == 0) return;
493
494 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
495 (long)bytes,
Lev Walkin419f6752006-09-13 04:02:00 +0000496 (long)DynamicBuffer.offset,
497 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000498 (long)DynamicBuffer.unbits,
Lev Walkin419f6752006-09-13 04:02:00 +0000499 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000500
Lev Walkin419f6752006-09-13 04:02:00 +0000501 if(DynamicBuffer.allocated
Lev Walkin5a621d62006-09-18 20:05:34 +0000502 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
Lev Walkinf9492a92006-09-13 04:14:17 +0000503 DEBUG("\tNo buffer reallocation is necessary");
Lev Walkin5a621d62006-09-18 20:05:34 +0000504 } else if(bytes <= DynamicBuffer.offset) {
Lev Walkin419f6752006-09-13 04:02:00 +0000505 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000506
507 /* Shift the buffer contents */
Lev Walkin419f6752006-09-13 04:02:00 +0000508 memmove(DynamicBuffer.data,
509 DynamicBuffer.data + DynamicBuffer.offset,
510 DynamicBuffer.length);
511 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
512 DynamicBuffer.offset = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000513 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000514 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
Lev Walkin419f6752006-09-13 04:02:00 +0000515 void *p = MALLOC(newsize);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000516 if(!p) {
Lev Walkin419f6752006-09-13 04:02:00 +0000517 perror("malloc()");
Lev Walkin22b54552004-10-28 13:22:54 +0000518 exit(EX_OSERR);
519 }
Lev Walkine1baa4d2006-09-17 08:23:43 +0000520 memcpy(p,
521 DynamicBuffer.data + DynamicBuffer.offset,
522 DynamicBuffer.length);
Lev Walkin419f6752006-09-13 04:02:00 +0000523 FREEMEM(DynamicBuffer.data);
Lev Walkin1f12da42006-09-24 19:47:07 +0000524 DynamicBuffer.data = (uint8_t *)p;
Lev Walkin419f6752006-09-13 04:02:00 +0000525 DynamicBuffer.offset = 0;
526 DynamicBuffer.allocated = newsize;
527 DynamicBuffer.nreallocs++;
Lev Walkinf9492a92006-09-13 04:14:17 +0000528 DEBUG("\tBuffer reallocated to %ld (%d time)",
Lev Walkin419f6752006-09-13 04:02:00 +0000529 newsize, DynamicBuffer.nreallocs);
Lev Walkin22b54552004-10-28 13:22:54 +0000530 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000531
Lev Walkin5a621d62006-09-18 20:05:34 +0000532 memcpy(DynamicBuffer.data
533 + DynamicBuffer.offset + DynamicBuffer.length,
534 data2add, bytes);
535 DynamicBuffer.length += bytes;
536 if(DynamicBuffer.unbits) {
537 int bits = DynamicBuffer.unbits;
538 DynamicBuffer.unbits = 0;
539 buffer_shift_left(DynamicBuffer.length - bytes, bits);
540 }
541
542 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
543 (long)bytes,
544 (long)DynamicBuffer.offset,
545 (long)DynamicBuffer.length,
546 (long)DynamicBuffer.unbits,
547 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000548}
549
Lev Walkinc744a022006-09-15 18:33:25 +0000550static void *
Lev Walkinbc691772006-09-17 11:02:53 +0000551data_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 +0000552 static uint8_t *fbuf;
Lev Walkin22b54552004-10-28 13:22:54 +0000553 static ssize_t fbuf_size;
554 static asn_codec_ctx_t s_codec_ctx;
555 asn_codec_ctx_t *opt_codec_ctx = 0;
556 void *structure = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000557 asn_dec_rval_t rval;
Lev Walkinbc691772006-09-17 11:02:53 +0000558 size_t old_offset;
559 size_t new_offset;
Lev Walkin5a621d62006-09-18 20:05:34 +0000560 int tolerate_eof;
Lev Walkin22b54552004-10-28 13:22:54 +0000561 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000562
563 if(!file) {
Lev Walkinbc691772006-09-17 11:02:53 +0000564 fprintf(stderr, "%s: %s\n", name, strerror(errno));
565 errno = EINVAL;
Lev Walkinc744a022006-09-15 18:33:25 +0000566 return 0;
567 }
Lev Walkin22b54552004-10-28 13:22:54 +0000568
569 if(opt_stack) {
570 s_codec_ctx.max_stack_size = opt_stack;
571 opt_codec_ctx = &s_codec_ctx;
572 }
573
Lev Walkinbc691772006-09-17 11:02:53 +0000574 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000575
576 /* prepare the file buffer */
577 if(fbuf_size != suggested_bufsize) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000578 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
Lev Walkin22b54552004-10-28 13:22:54 +0000579 if(!fbuf) {
580 perror("realloc()");
581 exit(EX_OSERR);
582 }
583 fbuf_size = suggested_bufsize;
584 }
585
Lev Walkinbc691772006-09-17 11:02:53 +0000586 if(on_first_pdu) {
587 DynamicBuffer.offset = 0;
588 DynamicBuffer.length = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000589 DynamicBuffer.unbits = 0;
Lev Walkinbc691772006-09-17 11:02:53 +0000590 DynamicBuffer.allocated = 0;
591 DynamicBuffer.bytes_shifted = 0;
592 DynamicBuffer.nreallocs = 0;
593 }
594
595 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000596
Lev Walkind1bfea62005-11-08 03:06:16 +0000597 /* Pretend immediate EOF */
598 rval.code = RC_WMORE;
599 rval.consumed = 0;
600
Lev Walkin5a621d62006-09-18 20:05:34 +0000601 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
602 (rd = fread(fbuf, 1, fbuf_size, file))
603 || feof(file) == 0
604 || (tolerate_eof && DynamicBuffer.length)
605 ;) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000606 int ecbits = 0; /* Extra consumed bits in case of PER */
607 uint8_t *i_bptr;
608 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000609
610 /*
611 * Copy the data over, or use the original buffer.
612 */
Lev Walkin419f6752006-09-13 04:02:00 +0000613 if(DynamicBuffer.allocated) {
Lev Walkin5a621d62006-09-18 20:05:34 +0000614 /* Append new data into the existing dynamic buffer */
Lev Walkin419f6752006-09-13 04:02:00 +0000615 add_bytes_to_buffer(fbuf, rd);
616 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
Lev Walkinf9492a92006-09-13 04:14:17 +0000617 i_size = DynamicBuffer.length;
Lev Walkin22b54552004-10-28 13:22:54 +0000618 } else {
Lev Walkind1bfea62005-11-08 03:06:16 +0000619 i_bptr = fbuf;
620 i_size = rd;
621 }
Lev Walkin22b54552004-10-28 13:22:54 +0000622
Lev Walkinbc691772006-09-17 11:02:53 +0000623 DEBUG("Decoding %ld bytes", (long)i_size);
624
Lev Walkin1f12da42006-09-24 19:47:07 +0000625#ifdef JUNKTEST
626 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
627#endif
628
Lev Walkind1bfea62005-11-08 03:06:16 +0000629 switch(iform) {
630 case INP_BER:
Lev Walkin00949562005-03-10 13:39:03 +0000631 rval = ber_decode(opt_codec_ctx, pduType,
Lev Walkind1bfea62005-11-08 03:06:16 +0000632 (void **)&structure, i_bptr, i_size);
633 break;
634 case INP_XER:
635 rval = xer_decode(opt_codec_ctx, pduType,
636 (void **)&structure, i_bptr, i_size);
637 break;
Lev Walkin59b176e2005-11-26 11:25:14 +0000638 case INP_PER:
Lev Walkin08b30bb2007-06-26 08:24:50 +0000639 if(opt_nopad)
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000640 rval = uper_decode(opt_codec_ctx, pduType,
Lev Walkin5a621d62006-09-18 20:05:34 +0000641 (void **)&structure, i_bptr, i_size, 0,
642 DynamicBuffer.unbits);
Lev Walkin08b30bb2007-06-26 08:24:50 +0000643 else
644 rval = uper_decode_complete(opt_codec_ctx, pduType,
645 (void **)&structure, i_bptr, i_size);
Lev Walkin1f12da42006-09-24 19:47:07 +0000646 switch(rval.code) {
647 case RC_OK:
Lev Walkin1f12da42006-09-24 19:47:07 +0000648 /* Fall through */
649 case RC_FAIL:
Lev Walkin08b30bb2007-06-26 08:24:50 +0000650 if(opt_nopad) {
651 /* uper_decode() returns bits! */
652 /* Extra bits */
653 ecbits = rval.consumed % 8;
654 /* Convert into bytes! */
655 rval.consumed /= 8;
656 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000657 break;
658 case RC_WMORE:
659 /* PER does not support restartability */
660 ASN_STRUCT_FREE(*pduType, structure);
661 structure = 0;
662 rval.consumed = 0;
663 /* Continue accumulating data */
664 break;
Lev Walkin5a621d62006-09-18 20:05:34 +0000665 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000666 break;
Lev Walkind1bfea62005-11-08 03:06:16 +0000667 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000668 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
Lev Walkin419f6752006-09-13 04:02:00 +0000669 (long)DynamicBuffer.length,
Lev Walkin4a858bb2006-09-17 11:22:00 +0000670 (long)rval.consumed, ecbits, (long)i_size,
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000671 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000672
Lev Walkin419f6752006-09-13 04:02:00 +0000673 if(DynamicBuffer.allocated == 0) {
Lev Walkin22b54552004-10-28 13:22:54 +0000674 /*
Lev Walkinbc691772006-09-17 11:02:53 +0000675 * Flush remainder into the intermediate buffer.
Lev Walkin22b54552004-10-28 13:22:54 +0000676 */
677 if(rval.code != RC_FAIL && rval.consumed < rd) {
Lev Walkin419f6752006-09-13 04:02:00 +0000678 add_bytes_to_buffer(fbuf + rval.consumed,
Lev Walkin5a621d62006-09-18 20:05:34 +0000679 rd - rval.consumed);
680 buffer_shift_left(0, ecbits);
681 DynamicBuffer.bytes_shifted = rval.consumed;
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000682 rval.consumed = 0;
Lev Walkin5a621d62006-09-18 20:05:34 +0000683 ecbits = 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000684 }
685 }
686
Lev Walkinbc691772006-09-17 11:02:53 +0000687 /*
688 * Adjust position inside the source buffer.
689 */
690 if(DynamicBuffer.allocated) {
691 DynamicBuffer.offset += rval.consumed;
692 DynamicBuffer.length -= rval.consumed;
693 } else {
694 DynamicBuffer.bytes_shifted += rval.consumed;
695 }
696
Lev Walkin22b54552004-10-28 13:22:54 +0000697 switch(rval.code) {
698 case RC_OK:
Lev Walkin5a621d62006-09-18 20:05:34 +0000699 if(ecbits) buffer_shift_left(0, ecbits);
700 DEBUG("RC_OK, finishing up with %ld+%d",
701 (long)rval.consumed, ecbits);
Lev Walkin22b54552004-10-28 13:22:54 +0000702 return structure;
703 case RC_WMORE:
Lev Walkin5a621d62006-09-18 20:05:34 +0000704 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
705 " with %ld..%ld-%ld..%ld",
706 (long)rd,
Lev Walkinf9492a92006-09-13 04:14:17 +0000707 (long)rval.consumed,
708 (long)DynamicBuffer.offset,
709 (long)DynamicBuffer.length,
Lev Walkin5a621d62006-09-18 20:05:34 +0000710 (long)DynamicBuffer.unbits,
Lev Walkinf9492a92006-09-13 04:14:17 +0000711 (long)DynamicBuffer.allocated);
Lev Walkin5a621d62006-09-18 20:05:34 +0000712 if(!rd) tolerate_eof--;
Lev Walkin22b54552004-10-28 13:22:54 +0000713 continue;
714 case RC_FAIL:
715 break;
716 }
717 break;
718 }
719
Lev Walkin1f12da42006-09-24 19:47:07 +0000720 DEBUG("Clean up partially decoded structure");
Lev Walkinadcb5862006-03-17 02:11:12 +0000721 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000722
Lev Walkinbc691772006-09-17 11:02:53 +0000723 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
724
725 /*
726 * Print a message and return failure only if not EOF,
727 * unless this is our first PDU (empty file).
728 */
Lev Walkin00918812006-09-18 21:19:32 +0000729 if(on_first_pdu
730 || DynamicBuffer.length
731 || new_offset - old_offset > ((iform == INP_XER)?sizeof("\r\n")-1:0)
732 ) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000733
734#ifdef JUNKTEST
735 /*
736 * Nothing's wrong with being unable to decode junk.
737 * Simulate EOF.
738 */
Lev Walkinbbd6eb32006-09-28 08:03:15 +0000739 if(opt_jprob != 0.0) {
740 junk_failures++;
741 errno = 0;
742 return 0;
743 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000744#endif
745
Lev Walkin5a621d62006-09-18 20:05:34 +0000746 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
747 on_first_pdu, (long)new_offset, (long)old_offset,
748 (long)DynamicBuffer.length);
Lev Walkinbc691772006-09-17 11:02:53 +0000749 fprintf(stderr, "%s: "
750 "Decode failed past byte %ld: %s\n",
751 name, (long)new_offset,
752 (rval.code == RC_WMORE)
753 ? "Unexpected end of input"
754 : "Input processing error");
Lev Walkin4bf96b92006-09-18 21:46:50 +0000755#ifndef ENOMSG
756#define ENOMSG EINVAL
757#endif
758#ifndef EBADMSG
759#define EBADMSG EINVAL
760#endif
Lev Walkinbc691772006-09-17 11:02:53 +0000761 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
762 } else {
Lev Walkin5a621d62006-09-18 20:05:34 +0000763 /* Got EOF after a few successful PDUs */
Lev Walkinbc691772006-09-17 11:02:53 +0000764 errno = 0;
765 }
Lev Walkin22b54552004-10-28 13:22:54 +0000766
767 return 0;
768}
769
Lev Walkin419f6752006-09-13 04:02:00 +0000770/* Dump the buffer out to the specified FILE */
771static int write_out(const void *buffer, size_t size, void *key) {
772 FILE *fp = (FILE *)key;
773 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
774}
Lev Walkinc744a022006-09-15 18:33:25 +0000775
776static int argument_is_stdin(char *av[], int idx) {
777 if(strcmp(av[idx], "-")) {
778 return 0; /* Certainly not <stdin> */
779 } else {
780 /* This might be <stdin>, unless `./program -- -` */
781 if(strcmp(av[-1], "--"))
782 return 1;
783 else
784 return 0;
785 }
786}
787
788static FILE *argument_to_file(char *av[], int idx) {
789 return argument_is_stdin(av, idx)
790 ? stdin
Lev Walkin86d8d772012-10-10 05:55:28 -0700791 : fopen(av[idx], "rb");
Lev Walkinc744a022006-09-15 18:33:25 +0000792}
793
794static char *argument_to_name(char *av[], int idx) {
795 return argument_is_stdin(av, idx)
796 ? "standard input"
797 : av[idx];
798}
Lev Walkin1f12da42006-09-24 19:47:07 +0000799
800#ifdef JUNKTEST
801/*
802 * Fill bytes with some garbage with specified probability (more or less).
803 */
804static void
805junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
806 static int junkmode;
807 uint8_t *ptr;
808 uint8_t *end;
809 if(opt_jprob <= 0.0) return;
810 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
811 int byte = *ptr;
812 if(junkmode++ & 1) {
813 if((((double)random() / RAND_MAX) < prob))
814 byte = random() & 0xff;
815 } else {
816#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
817 byte ^= BPROB(0x80);
818 byte ^= BPROB(0x40);
819 byte ^= BPROB(0x20);
820 byte ^= BPROB(0x10);
821 byte ^= BPROB(0x08);
822 byte ^= BPROB(0x04);
823 byte ^= BPROB(0x02);
824 byte ^= BPROB(0x01);
825 }
826 if(byte != *ptr) {
827 DEBUG("Junk buf[%d] %02x -> %02x",
828 ptr - buf, *ptr, byte);
829 *ptr = byte;
830 }
831 }
832}
833#endif /* JUNKTEST */
834