blob: aedaaa98806191b43a3b1d186ff80ebf90ad5ca3 [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 Walkind19216f2017-08-04 03:02:57 -07003 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
Lev Walkin8a4a06c2007-06-29 12:44:01 +00004 * 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 */
Lev Walkinfb35e082017-08-04 03:06:51 -070010#ifdef HAVE_CONFIG_H
Lev Walkin22b54552004-10-28 13:22:54 +000011#include <config.h>
12#endif
13#include <stdio.h>
14#include <sys/types.h>
Lev Walkinfb35e082017-08-04 03:06:51 -070015#include <stdlib.h> /* for atoi(3) */
16#include <unistd.h> /* for getopt(3) */
17#include <string.h> /* for strerror(3) */
18#include <sysexits.h> /* for EX_* exit codes */
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 Walkinfb35e082017-08-04 03:06:51 -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" */
Lev Walkin6d46bc32017-09-12 21:34:00 -070025
26#ifndef NO_ASN_PDU
27#ifndef PDU
28#error Define -DPDU to compile this sample converter.
29#endif
30#ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
31extern asn_TYPE_descriptor_t *asn_pdu_collection[];
32#endif
Lev Walkinfb35e082017-08-04 03:06:51 -070033#define ASN_DEF_PDU(t) asn_DEF_ ## t
34#define DEF_PDU_Type(t) ASN_DEF_PDU(t)
35#define PDU_Type DEF_PDU_Type(PDU)
Lev Walkind0625682006-08-25 02:35:08 +000036
Lev Walkinfb35e082017-08-04 03:06:51 -070037extern asn_TYPE_descriptor_t PDU_Type; /* ASN.1 type to be decoded */
Lev Walkin6d46bc32017-09-12 21:34:00 -070038#define PDU_Type_Ptr (&PDU_Type)
39#else /* NO_ASN_PDU */
40#define PDU_Type_Ptr NULL
41#endif /* NO_ASN_PDU */
Lev Walkin22b54552004-10-28 13:22:54 +000042
43/*
Lev Walkin00949562005-03-10 13:39:03 +000044 * Open file and parse its contens.
Lev Walkin22b54552004-10-28 13:22:54 +000045 */
Lev Walkin6d46bc32017-09-12 21:34:00 -070046static void *data_decode_from_file(enum asn_transfer_syntax,
47 asn_TYPE_descriptor_t *asnTypeOfPDU,
48 FILE *file, const char *name,
49 ssize_t suggested_bufsize, int first_pdu);
Lev Walkind1bfea62005-11-08 03:06:16 +000050static int write_out(const void *buffer, size_t size, void *key);
Lev Walkinc744a022006-09-15 18:33:25 +000051static FILE *argument_to_file(char *av[], int idx);
52static char *argument_to_name(char *av[], int idx);
Lev Walkin22b54552004-10-28 13:22:54 +000053
Lev Walkin0f77cea2017-09-13 00:28:20 -070054 int opt_debug; /* -d (or -dd) */
55static int opt_check; /* -c (constraints checking) */
56static int opt_stack; /* -s (maximum stack size) */
57static int opt_nopad; /* -per-nopad (PER input is not padded between msgs) */
58static int opt_onepdu; /* -1 (decode single PDU) */
Lev Walkind1bfea62005-11-08 03:06:16 +000059
Lev Walkinfb35e082017-08-04 03:06:51 -070060#ifdef JUNKTEST /* Enable -J <probability> */
61#define JUNKOPT "J:"
62static double opt_jprob; /* Junk bit probability */
Lev Walkin1f12da42006-09-24 19:47:07 +000063static int junk_failures;
64static void junk_bytes_with_probability(uint8_t *, size_t, double prob);
65#else
Lev Walkinfb35e082017-08-04 03:06:51 -070066#define JUNKOPT
Lev Walkin1f12da42006-09-24 19:47:07 +000067#endif
68
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000069/* Debug output function */
Lev Walkineff98a52017-09-13 22:24:35 +000070static void
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000071DEBUG(const char *fmt, ...) {
Lev Walkinfb35e082017-08-04 03:06:51 -070072 va_list ap;
73 if(!opt_debug) return;
74 fprintf(stderr, "AD: ");
75 va_start(ap, fmt);
76 vfprintf(stderr, fmt, ap);
77 va_end(ap);
78 fprintf(stderr, "\n");
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000079}
Lev Walkin22b54552004-10-28 13:22:54 +000080
Lev Walkin6d46bc32017-09-12 21:34:00 -070081static const char *
82ats_simple_name(enum asn_transfer_syntax syntax) {
83 switch(syntax) {
84 case ATS_INVALID:
85 return "/dev/null";
86 case ATS_NONSTANDARD_PLAINTEXT:
87 return "plaintext";
88 case ATS_BER:
89 return "BER";
90 case ATS_DER:
91 return "DER";
92 case ATS_CER:
93 return "CER";
94 case ATS_BASIC_OER:
95 case ATS_CANONICAL_OER:
96 return "OER";
97 case ATS_BASIC_XER:
98 case ATS_CANONICAL_XER:
99 return "XER";
100 case ATS_UNALIGNED_BASIC_PER:
101 case ATS_UNALIGNED_CANONICAL_PER:
102 return "PER";
103 default:
104 return "<?>";
105 }
106}
107
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700108
Lev Walkind5142d42017-09-15 23:33:42 -0700109#define HAS_CODEC(fname) ((&(((asn_TYPE_operation_t *)0)->fname)) != 0)
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700110typedef struct {
111 const char *name;
112 enum asn_transfer_syntax syntax;
Lev Walkind5142d42017-09-15 23:33:42 -0700113 int has_codec;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700114 const char *full_name;
115} syntax_selector;
116
117static syntax_selector input_encodings[] = {
Lev Walkind5142d42017-09-15 23:33:42 -0700118 {"ber", ATS_BER, HAS_CODEC(ber_decoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700119 "Input is in BER (Basic Encoding Rules) or DER"},
Lev Walkind5142d42017-09-15 23:33:42 -0700120 {"oer", ATS_BASIC_OER, HAS_CODEC(oer_decoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700121 "Input is in OER (Octet Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700122 {"per", ATS_UNALIGNED_BASIC_PER, HAS_CODEC(uper_decoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700123 "Input is in Unaligned PER (Packed Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700124 {"xer", ATS_BASIC_XER, HAS_CODEC(xer_decoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700125 "Input is in XER (XML Encoding Rules)"},
126 {0, ATS_INVALID, 0, 0}};
127
128static syntax_selector output_encodings[] = {
Lev Walkind5142d42017-09-15 23:33:42 -0700129 {"der", ATS_DER, HAS_CODEC(der_encoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700130 "Output as DER (Distinguished Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700131 {"oer", ATS_CANONICAL_OER, HAS_CODEC(oer_encoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700132 "Output as Canonical OER (Octet Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700133 {"per", ATS_UNALIGNED_CANONICAL_PER, HAS_CODEC(uper_encoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700134 "Output as Unaligned PER (Packed Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700135 {"xer", ATS_BASIC_XER, HAS_CODEC(xer_encoder),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700136 "Output as XER (XML Encoding Rules)"},
Lev Walkind5142d42017-09-15 23:33:42 -0700137 {"text", ATS_NONSTANDARD_PLAINTEXT, HAS_CODEC(print_struct),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700138 "Output as plain semi-structured text"},
Lev Walkind5142d42017-09-15 23:33:42 -0700139 {"null", ATS_INVALID, HAS_CODEC(print_struct),
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700140 "Verify (decode) input, but do not output"},
141 {0, ATS_INVALID, 0, 0}};
142
143/*
144 * Select ASN.1 Transfer Enocoding Syntax by command line name.
145 */
146static const syntax_selector *
147ats_by_name(const char *name, const asn_TYPE_descriptor_t *td,
148 const syntax_selector *first_element) {
149 const syntax_selector *element;
150 for(element = first_element; element->name; element++) {
151 if(strcmp(element->name, name) == 0) {
152 if(td && td->op
153 && *(const void *const *)(const void *)((const char *)td->op
Lev Walkind5142d42017-09-15 23:33:42 -0700154 + element->has_codec)) {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700155 return element;
156 }
157 }
158 }
159 return NULL;
160}
161
Lev Walkin22b54552004-10-28 13:22:54 +0000162int
Lev Walkinc744a022006-09-15 18:33:25 +0000163main(int ac, char *av[]) {
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700164 FILE *binary_out;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700165 static asn_TYPE_descriptor_t *pduType = PDU_Type_Ptr;
Lev Walkinfb35e082017-08-04 03:06:51 -0700166 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
167 int number_of_iterations = 1;
168 int num;
169 int ch;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700170 const syntax_selector *sel;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700171 enum asn_transfer_syntax isyntax = ATS_INVALID;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700172 enum asn_transfer_syntax osyntax = ATS_BASIC_XER;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700173
174#ifndef PDU
175 if(!pduType) {
176 fprintf(stderr, "No -DPDU defined during compilation.\n");
177#ifdef NO_ASN_PDU
178 exit(0);
179#else
180 exit(EX_SOFTWARE);
181#endif
182 }
183#endif
Lev Walkin22b54552004-10-28 13:22:54 +0000184
Lev Walkinfb35e082017-08-04 03:06:51 -0700185 /* Figure out if specialty decoder needs to be default */
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700186 if(ats_by_name("oer", pduType, input_encodings))
Lev Walkin6d46bc32017-09-12 21:34:00 -0700187 isyntax = ATS_BASIC_OER;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700188 if(ats_by_name("per", pduType, input_encodings))
Lev Walkin6d46bc32017-09-12 21:34:00 -0700189 isyntax = ATS_UNALIGNED_BASIC_PER;
Lev Walkin59b176e2005-11-26 11:25:14 +0000190
Lev Walkinfb35e082017-08-04 03:06:51 -0700191 /*
192 * Pocess the command-line argments.
193 */
194 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
195 switch(ch) {
196 case 'i':
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700197 sel = ats_by_name(optarg, pduType, input_encodings);
198 if(sel) {
199 isyntax = sel->syntax;
200 } else {
201 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
202 optarg);
203 exit(EX_UNAVAILABLE);
204 }
205 break;
Lev Walkinfb35e082017-08-04 03:06:51 -0700206 case 'o':
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700207 sel = ats_by_name(optarg, pduType, output_encodings);
208 if(sel) {
209 osyntax = sel->syntax;
210 } else {
211 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
212 optarg);
213 exit(EX_UNAVAILABLE);
214 }
215 break;
Lev Walkinfb35e082017-08-04 03:06:51 -0700216 case '1':
217 opt_onepdu = 1;
218 break;
219 case 'b':
220 suggested_bufsize = atoi(optarg);
221 if(suggested_bufsize < 1
222 || suggested_bufsize > 16 * 1024 * 1024) {
223 fprintf(stderr,
224 "-b %s: Improper buffer size (1..16M)\n",
225 optarg);
226 exit(EX_UNAVAILABLE);
227 }
228 break;
229 case 'c':
230 opt_check = 1;
231 break;
232 case 'd':
233 opt_debug++; /* Double -dd means ASN.1 debug */
234 break;
235 case 'n':
236 number_of_iterations = atoi(optarg);
237 if(number_of_iterations < 1) {
238 fprintf(stderr,
239 "-n %s: Improper iterations count\n", optarg);
240 exit(EX_UNAVAILABLE);
241 }
242 break;
243 case 'p':
Lev Walkin0f77cea2017-09-13 00:28:20 -0700244 if(strcmp(optarg, "er-nopad") == 0) {
245 opt_nopad = 1;
246 break;
247 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700248#ifdef ASN_PDU_COLLECTION
249 if(strcmp(optarg, "list") == 0) {
250 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
251 fprintf(stderr, "Available PDU types:\n");
252 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
253 exit(0);
254 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
255 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
256 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
257 if(*pdu) { pduType = *pdu; break; }
258 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkin7e5ea1d2017-08-06 00:59:28 -0700259 exit(EX_USAGE);
260 }
261#else /* Without -pdu=auto there's just a single type */
262 if(strcmp(optarg, "list") == 0) {
263 fprintf(stderr, "Available PDU types:\n");
264 printf("%s\n", pduType->name);
265 exit(0);
266 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
267 if(strcmp(optarg, pduType->name) == 0) {
268 break;
269 }
270 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
271 exit(EX_USAGE);
Lev Walkinfb35e082017-08-04 03:06:51 -0700272 }
273#endif /* ASN_PDU_COLLECTION */
274 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
275 exit(EX_UNAVAILABLE);
276 case 's':
277 opt_stack = atoi(optarg);
278 if(opt_stack < 0) {
279 fprintf(stderr,
280 "-s %s: Non-negative value expected\n",
281 optarg);
282 exit(EX_UNAVAILABLE);
283 }
284 break;
285#ifdef JUNKTEST
286 case 'J':
287 opt_jprob = strtod(optarg, 0);
288 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
289 fprintf(stderr,
290 "-J %s: Probability range 0..1 expected \n",
291 optarg);
292 exit(EX_UNAVAILABLE);
293 }
294 break;
295#endif /* JUNKTEST */
296 case 'h':
297 default:
298#ifdef ASN_CONVERTER_TITLE
299#define _AXS(x) #x
300#define _ASX(x) _AXS(x)
301 fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE));
Lev Walkin8a4a06c2007-06-29 12:44:01 +0000302#endif
Lev Walkinae0c3c22017-08-26 17:20:32 -0700303 fprintf(stderr, "Usage: %s [options] <datafile> ...\n", av[0]);
Lev Walkinfb35e082017-08-04 03:06:51 -0700304 fprintf(stderr, "Where options are:\n");
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700305 for(sel = input_encodings; sel->name; sel++) {
306 if(ats_by_name(sel->name, pduType, sel)) {
307 fprintf(stderr, " -i%s %s%s\n", sel->name,
308 sel->full_name,
309 (sel->syntax == isyntax) ? " (DEFAULT)" : "");
310 }
311 }
312 for(sel = output_encodings; sel->name; sel++) {
313 if(ats_by_name(sel->name, pduType, sel)) {
314 fprintf(stderr, " -o%s%s %s%s\n", sel->name,
315 strlen(sel->name) > 3 ? "" : " ",
316 sel->full_name,
317 (sel->syntax == osyntax) ? " (DEFAULT)" : "");
318 }
319 }
Lev Walkin0f77cea2017-09-13 00:28:20 -0700320 if(pduType->op->uper_decoder) {
321 fprintf(stderr,
322 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
323 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700324#ifdef ASN_PDU_COLLECTION
325 fprintf(stderr,
326 " -p <PDU> Specify PDU type to decode\n"
327 " -p list List available PDUs\n");
328#endif /* ASN_PDU_COLLECTION */
329 fprintf(stderr,
330 " -1 Decode only the first PDU in file\n"
331 " -b <size> Set the i/o buffer size (default is %ld)\n"
332 " -c Check ASN.1 constraints after decoding\n"
333 " -d Enable debugging (-dd is even better)\n"
334 " -n <num> Process files <num> times\n"
335 " -s <size> Set the stack usage limit (default is %d)\n"
336#ifdef JUNKTEST
337 " -J <prob> Set random junk test bit garbaging probability\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000338#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700339 , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX);
340 exit(EX_USAGE);
341 }
Lev Walkin22b54552004-10-28 13:22:54 +0000342
Lev Walkinfb35e082017-08-04 03:06:51 -0700343 ac -= optind;
344 av += optind;
Lev Walkin22b54552004-10-28 13:22:54 +0000345
Lev Walkinfb35e082017-08-04 03:06:51 -0700346 if(ac < 1) {
347 fprintf(stderr, "%s: No input files specified. "
348 "Try '-h' for more information\n",
349 av[-optind]);
350 exit(EX_USAGE);
351 }
Lev Walkin22b54552004-10-28 13:22:54 +0000352
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700353 if(isatty(1)) {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700354 const int is_text_output = osyntax == ATS_NONSTANDARD_PLAINTEXT
355 || osyntax == ATS_BASIC_XER
356 || osyntax == ATS_CANONICAL_XER;
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700357 if(is_text_output) {
358 binary_out = stdout;
359 } else {
360 fprintf(stderr, "(Suppressing binary output to a terminal.)\n");
361 binary_out = fopen("/dev/null", "wb");
362 if(!binary_out) {
363 fprintf(stderr, "Can't open /dev/null: %s\n", strerror(errno));
364 exit(EX_OSERR);
365 }
366 }
367 } else {
368 binary_out = stdout;
369 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700370 setvbuf(stdout, 0, _IOLBF, 0);
Lev Walkin22b54552004-10-28 13:22:54 +0000371
Lev Walkinfb35e082017-08-04 03:06:51 -0700372 for(num = 0; num < number_of_iterations; num++) {
373 int ac_i;
374 /*
375 * Process all files in turn.
376 */
377 for(ac_i = 0; ac_i < ac; ac_i++) {
378 asn_enc_rval_t erv;
379 void *structure; /* Decoded structure */
380 FILE *file = argument_to_file(av, ac_i);
381 char *name = argument_to_name(av, ac_i);
382 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000383
Lev Walkin6d46bc32017-09-12 21:34:00 -0700384 for(first_pdu = 1; file && (first_pdu || !opt_onepdu); first_pdu = 0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700385 /*
386 * Decode the encoded structure from file.
387 */
Lev Walkin6d46bc32017-09-12 21:34:00 -0700388 structure = data_decode_from_file(isyntax, pduType, file, name,
Lev Walkinfb35e082017-08-04 03:06:51 -0700389 suggested_bufsize, first_pdu);
390 if(!structure) {
391 if(errno) {
392 /* Error message is already printed */
393 exit(EX_DATAERR);
394 } else {
395 /* EOF */
396 break;
397 }
398 }
Lev Walkin22b54552004-10-28 13:22:54 +0000399
Lev Walkinfb35e082017-08-04 03:06:51 -0700400 /* Check ASN.1 constraints */
401 if(opt_check) {
402 char errbuf[128];
403 size_t errlen = sizeof(errbuf);
404 if(asn_check_constraints(pduType, structure, errbuf, &errlen)) {
405 fprintf(stderr,
406 "%s: ASN.1 constraint "
407 "check failed: %s\n",
408 name, errbuf);
409 exit(EX_DATAERR);
410 }
411 }
Lev Walkin22b54552004-10-28 13:22:54 +0000412
Lev Walkin6d46bc32017-09-12 21:34:00 -0700413 if(osyntax == ATS_INVALID) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700414#ifdef JUNKTEST
Lev Walkin6d46bc32017-09-12 21:34:00 -0700415 if(opt_jprob == 0.0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700416 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700417 }
Lev Walkin69033802017-08-25 12:15:58 -0700418#else
Lev Walkin6d46bc32017-09-12 21:34:00 -0700419 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkin69033802017-08-25 12:15:58 -0700420#endif
Lev Walkin6d46bc32017-09-12 21:34:00 -0700421 } else {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700422 erv = asn_encode(NULL, osyntax, pduType, structure, write_out,
423 binary_out);
Lev Walkin6d46bc32017-09-12 21:34:00 -0700424
425 if(erv.encoded == -1) {
426 fprintf(stderr, "%s: Cannot convert %s into %s\n", name,
427 pduType->name, ats_simple_name(osyntax));
Lev Walkinfb35e082017-08-04 03:06:51 -0700428 exit(EX_UNAVAILABLE);
429 }
Lev Walkin6d46bc32017-09-12 21:34:00 -0700430 DEBUG("Encoded in %zd bytes of %s", erv.encoded,
431 ats_simple_name(osyntax));
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700432 }
Lev Walkind1bfea62005-11-08 03:06:16 +0000433
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700434 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinfb35e082017-08-04 03:06:51 -0700435 }
Lev Walkinbc691772006-09-17 11:02:53 +0000436
Lev Walkinfb35e082017-08-04 03:06:51 -0700437 if(file && file != stdin) {
438 fclose(file);
439 }
440 }
441 }
Lev Walkin22b54552004-10-28 13:22:54 +0000442
Lev Walkinfb35e082017-08-04 03:06:51 -0700443#ifdef JUNKTEST
444 if(opt_jprob > 0.0) {
445 fprintf(stderr, "Junked %f OK (%d/%d)\n",
446 opt_jprob, junk_failures, number_of_iterations);
447 }
448#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000449
Lev Walkinfb35e082017-08-04 03:06:51 -0700450 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000451}
452
Lev Walkin419f6752006-09-13 04:02:00 +0000453static struct dynamic_buffer {
Lev Walkinfb35e082017-08-04 03:06:51 -0700454 uint8_t *data; /* Pointer to the data bytes */
455 size_t offset; /* Offset from the start */
456 size_t length; /* Length of meaningful contents */
457 size_t unbits; /* Unused bits in the last byte */
458 size_t allocated; /* Allocated memory for data */
459 int nreallocs; /* Number of data reallocations */
460 off_t bytes_shifted; /* Number of bytes ever shifted */
Lev Walkin419f6752006-09-13 04:02:00 +0000461} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000462
Lev Walkin5a621d62006-09-18 20:05:34 +0000463static void
464buffer_dump() {
Lev Walkinfb35e082017-08-04 03:06:51 -0700465 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
466 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
467 if(!opt_debug) return;
468 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
469 DynamicBuffer.data,
470 (long)DynamicBuffer.offset,
471 (long)DynamicBuffer.length,
472 (long)DynamicBuffer.unbits,
473 (long)DynamicBuffer.allocated,
474 (long)DynamicBuffer.bytes_shifted);
475 for(; p < e; p++) {
476 fprintf(stderr, " %c%c%c%c%c%c%c%c",
477 ((*p >> 7) & 1) ? '1' : '0',
478 ((*p >> 6) & 1) ? '1' : '0',
479 ((*p >> 5) & 1) ? '1' : '0',
480 ((*p >> 4) & 1) ? '1' : '0',
481 ((*p >> 3) & 1) ? '1' : '0',
482 ((*p >> 2) & 1) ? '1' : '0',
483 ((*p >> 1) & 1) ? '1' : '0',
484 ((*p >> 0) & 1) ? '1' : '0');
485 }
486 if(DynamicBuffer.unbits) {
487 unsigned int shift;
488 fprintf(stderr, " ");
489 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
490 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
491 fprintf(stderr, " %ld:%ld\n",
492 (long)DynamicBuffer.length - 1,
493 (long)8 - DynamicBuffer.unbits);
494 } else {
495 fprintf(stderr, " %ld\n", (long)DynamicBuffer.length);
496 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000497}
498
499/*
500 * Move the buffer content left N bits, possibly joining it with
501 * preceeding content.
502 */
503static void
504buffer_shift_left(size_t offset, int bits) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700505 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
506 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
507 + DynamicBuffer.length - 1;
508
509 if(!bits) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000510
Lev Walkinfb35e082017-08-04 03:06:51 -0700511 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
512 bits, (long)offset,
513 (long)DynamicBuffer.offset,
514 (long)DynamicBuffer.unbits,
515 (long)DynamicBuffer.length);
Lev Walkin5a621d62006-09-18 20:05:34 +0000516
Lev Walkinfb35e082017-08-04 03:06:51 -0700517 if(offset) {
518 int right;
519 right = ptr[0] >> (8 - bits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000520
Lev Walkinfb35e082017-08-04 03:06:51 -0700521 DEBUG("oleft: %c%c%c%c%c%c%c%c",
522 ((ptr[-1] >> 7) & 1) ? '1' : '0',
523 ((ptr[-1] >> 6) & 1) ? '1' : '0',
524 ((ptr[-1] >> 5) & 1) ? '1' : '0',
525 ((ptr[-1] >> 4) & 1) ? '1' : '0',
526 ((ptr[-1] >> 3) & 1) ? '1' : '0',
527 ((ptr[-1] >> 2) & 1) ? '1' : '0',
528 ((ptr[-1] >> 1) & 1) ? '1' : '0',
529 ((ptr[-1] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000530
Lev Walkinfb35e082017-08-04 03:06:51 -0700531 DEBUG("oriht: %c%c%c%c%c%c%c%c",
532 ((ptr[0] >> 7) & 1) ? '1' : '0',
533 ((ptr[0] >> 6) & 1) ? '1' : '0',
534 ((ptr[0] >> 5) & 1) ? '1' : '0',
535 ((ptr[0] >> 4) & 1) ? '1' : '0',
536 ((ptr[0] >> 3) & 1) ? '1' : '0',
537 ((ptr[0] >> 2) & 1) ? '1' : '0',
538 ((ptr[0] >> 1) & 1) ? '1' : '0',
539 ((ptr[0] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000540
Lev Walkinfb35e082017-08-04 03:06:51 -0700541 DEBUG("mriht: %c%c%c%c%c%c%c%c",
542 ((right >> 7) & 1) ? '1' : '0',
543 ((right >> 6) & 1) ? '1' : '0',
544 ((right >> 5) & 1) ? '1' : '0',
545 ((right >> 4) & 1) ? '1' : '0',
546 ((right >> 3) & 1) ? '1' : '0',
547 ((right >> 2) & 1) ? '1' : '0',
548 ((right >> 1) & 1) ? '1' : '0',
549 ((right >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000550
Lev Walkinfb35e082017-08-04 03:06:51 -0700551 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
Lev Walkin5a621d62006-09-18 20:05:34 +0000552
Lev Walkinfb35e082017-08-04 03:06:51 -0700553 DEBUG("after: %c%c%c%c%c%c%c%c",
554 ((ptr[-1] >> 7) & 1) ? '1' : '0',
555 ((ptr[-1] >> 6) & 1) ? '1' : '0',
556 ((ptr[-1] >> 5) & 1) ? '1' : '0',
557 ((ptr[-1] >> 4) & 1) ? '1' : '0',
558 ((ptr[-1] >> 3) & 1) ? '1' : '0',
559 ((ptr[-1] >> 2) & 1) ? '1' : '0',
560 ((ptr[-1] >> 1) & 1) ? '1' : '0',
561 ((ptr[-1] >> 0) & 1) ? '1' : '0');
562 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000563
Lev Walkinfb35e082017-08-04 03:06:51 -0700564 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000565
Lev Walkinfb35e082017-08-04 03:06:51 -0700566 for(; ptr < end; ptr++) {
567 int right = ptr[1] >> (8 - bits);
568 *ptr = (*ptr << bits) | right;
569 }
570 *ptr <<= bits;
Lev Walkin5a621d62006-09-18 20:05:34 +0000571
Lev Walkinfb35e082017-08-04 03:06:51 -0700572 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
573 if(DynamicBuffer.unbits == 0) {
574 DynamicBuffer.unbits += bits;
575 } else {
576 DynamicBuffer.unbits += bits;
577 if(DynamicBuffer.unbits > 7) {
578 DynamicBuffer.unbits -= 8;
579 DynamicBuffer.length--;
580 DynamicBuffer.bytes_shifted++;
581 }
582 }
583 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000584
Lev Walkinfb35e082017-08-04 03:06:51 -0700585 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000586
Lev Walkinfb35e082017-08-04 03:06:51 -0700587 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
588 (long)DynamicBuffer.offset,
589 (long)DynamicBuffer.unbits,
590 (long)DynamicBuffer.length);
591
Lev Walkin5a621d62006-09-18 20:05:34 +0000592
593}
594
Lev Walkin22b54552004-10-28 13:22:54 +0000595/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000596 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000597 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000598static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000599
Lev Walkinfb35e082017-08-04 03:06:51 -0700600 if(bytes == 0) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000601
Lev Walkinfb35e082017-08-04 03:06:51 -0700602 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
603 (long)bytes,
604 (long)DynamicBuffer.offset,
605 (long)DynamicBuffer.length,
606 (long)DynamicBuffer.unbits,
607 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000608
Lev Walkinfb35e082017-08-04 03:06:51 -0700609 if(DynamicBuffer.allocated
610 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
611 DEBUG("\tNo buffer reallocation is necessary");
612 } else if(bytes <= DynamicBuffer.offset) {
613 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000614
Lev Walkinfb35e082017-08-04 03:06:51 -0700615 /* Shift the buffer contents */
616 memmove(DynamicBuffer.data,
617 DynamicBuffer.data + DynamicBuffer.offset,
618 DynamicBuffer.length);
619 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
620 DynamicBuffer.offset = 0;
621 } else {
622 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
623 void *p = MALLOC(newsize);
624 if(!p) {
625 perror("malloc()");
626 exit(EX_OSERR);
627 }
628 memcpy(p,
629 DynamicBuffer.data + DynamicBuffer.offset,
630 DynamicBuffer.length);
631 FREEMEM(DynamicBuffer.data);
632 DynamicBuffer.data = (uint8_t *)p;
633 DynamicBuffer.offset = 0;
634 DynamicBuffer.allocated = newsize;
635 DynamicBuffer.nreallocs++;
636 DEBUG("\tBuffer reallocated to %ld (%d time)",
637 newsize, DynamicBuffer.nreallocs);
638 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000639
Lev Walkinfb35e082017-08-04 03:06:51 -0700640 memcpy(DynamicBuffer.data
641 + DynamicBuffer.offset + DynamicBuffer.length,
642 data2add, bytes);
643 DynamicBuffer.length += bytes;
644 if(DynamicBuffer.unbits) {
645 int bits = DynamicBuffer.unbits;
646 DynamicBuffer.unbits = 0;
647 buffer_shift_left(DynamicBuffer.length - bytes, bits);
648 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000649
Lev Walkinfb35e082017-08-04 03:06:51 -0700650 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
651 (long)bytes,
652 (long)DynamicBuffer.offset,
653 (long)DynamicBuffer.length,
654 (long)DynamicBuffer.unbits,
655 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000656}
657
Lev Walkine8bbe932017-09-12 23:06:52 -0700658static int
Lev Walkin0f77cea2017-09-13 00:28:20 -0700659is_syntax_PER(enum asn_transfer_syntax syntax) {
Lev Walkine8bbe932017-09-12 23:06:52 -0700660 return (syntax != ATS_UNALIGNED_BASIC_PER
661 && syntax != ATS_UNALIGNED_CANONICAL_PER);
662}
663
Lev Walkin0f77cea2017-09-13 00:28:20 -0700664static int
665restartability_supported(enum asn_transfer_syntax syntax) {
666 return is_syntax_PER(syntax);
667}
668
Lev Walkinc744a022006-09-15 18:33:25 +0000669static void *
Lev Walkin6d46bc32017-09-12 21:34:00 -0700670data_decode_from_file(enum asn_transfer_syntax isyntax, asn_TYPE_descriptor_t *pduType, FILE *file, const char *name, ssize_t suggested_bufsize, int on_first_pdu) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700671 static uint8_t *fbuf;
672 static ssize_t fbuf_size;
673 static asn_codec_ctx_t s_codec_ctx;
674 asn_codec_ctx_t *opt_codec_ctx = 0;
675 void *structure = 0;
676 asn_dec_rval_t rval;
677 size_t old_offset;
678 size_t new_offset;
679 int tolerate_eof;
680 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000681
Lev Walkinfb35e082017-08-04 03:06:51 -0700682 if(!file) {
683 fprintf(stderr, "%s: %s\n", name, strerror(errno));
684 errno = EINVAL;
685 return 0;
686 }
Lev Walkin22b54552004-10-28 13:22:54 +0000687
Lev Walkinfb35e082017-08-04 03:06:51 -0700688 if(opt_stack) {
689 s_codec_ctx.max_stack_size = opt_stack;
690 opt_codec_ctx = &s_codec_ctx;
691 }
Lev Walkin22b54552004-10-28 13:22:54 +0000692
Lev Walkinfb35e082017-08-04 03:06:51 -0700693 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000694
Lev Walkinfb35e082017-08-04 03:06:51 -0700695 /* prepare the file buffer */
696 if(fbuf_size != suggested_bufsize) {
697 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
698 if(!fbuf) {
699 perror("realloc()");
700 exit(EX_OSERR);
701 }
702 fbuf_size = suggested_bufsize;
703 }
Lev Walkin22b54552004-10-28 13:22:54 +0000704
Lev Walkinfb35e082017-08-04 03:06:51 -0700705 if(on_first_pdu) {
706 DynamicBuffer.offset = 0;
707 DynamicBuffer.length = 0;
708 DynamicBuffer.unbits = 0;
709 DynamicBuffer.allocated = 0;
710 DynamicBuffer.bytes_shifted = 0;
711 DynamicBuffer.nreallocs = 0;
712 }
Lev Walkinbc691772006-09-17 11:02:53 +0000713
Lev Walkinfb35e082017-08-04 03:06:51 -0700714 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000715
Lev Walkinfb35e082017-08-04 03:06:51 -0700716 /* Pretend immediate EOF */
717 rval.code = RC_WMORE;
718 rval.consumed = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000719
Lev Walkinfb35e082017-08-04 03:06:51 -0700720 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
721 (rd = fread(fbuf, 1, fbuf_size, file))
722 || feof(file) == 0
723 || (tolerate_eof && DynamicBuffer.length)
724 ;) {
725 int ecbits = 0; /* Extra consumed bits in case of PER */
726 uint8_t *i_bptr;
727 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000728
Lev Walkinfb35e082017-08-04 03:06:51 -0700729 /*
730 * Copy the data over, or use the original buffer.
731 */
732 if(DynamicBuffer.allocated) {
733 /* Append new data into the existing dynamic buffer */
734 add_bytes_to_buffer(fbuf, rd);
735 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
736 i_size = DynamicBuffer.length;
737 } else {
738 i_bptr = fbuf;
739 i_size = rd;
740 }
Lev Walkin22b54552004-10-28 13:22:54 +0000741
Lev Walkinfb35e082017-08-04 03:06:51 -0700742 DEBUG("Decoding %ld bytes", (long)i_size);
Lev Walkinbc691772006-09-17 11:02:53 +0000743
Lev Walkinfb35e082017-08-04 03:06:51 -0700744#ifdef JUNKTEST
745 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
Lev Walkin1f12da42006-09-24 19:47:07 +0000746#endif
747
Lev Walkin0f77cea2017-09-13 00:28:20 -0700748 if(is_syntax_PER(isyntax) && opt_nopad) {
749#ifdef ASN_DISABLE_PER_SUPPORT
750 rval.code = RC_FAIL;
751 rval.consumed = 0;
752#else
753 rval = uper_decode(opt_codec_ctx, pduType, (void **)&structure,
754 i_bptr, i_size, 0, DynamicBuffer.unbits);
755 /* uper_decode() returns bits! */
756 ecbits = rval.consumed % 8; /* Bits consumed from the last byte */
757 rval.consumed >>= 3; /* Convert bits into bytes. */
758#endif
759 /* Non-padded PER decoder */
760 } else {
761 rval = asn_decode(opt_codec_ctx, isyntax, pduType,
762 (void **)&structure, i_bptr, i_size);
763 }
Lev Walkine8bbe932017-09-12 23:06:52 -0700764 if(rval.code == RC_WMORE && !restartability_supported(isyntax)) {
765 /* PER does not support restartability */
766 ASN_STRUCT_FREE(*pduType, structure);
767 structure = 0;
Lev Walkin69033802017-08-25 12:15:58 -0700768 rval.consumed = 0;
Lev Walkine8bbe932017-09-12 23:06:52 -0700769 /* Continue accumulating data */
Lev Walkinfb35e082017-08-04 03:06:51 -0700770 }
Lev Walkine8bbe932017-09-12 23:06:52 -0700771
Lev Walkinfb35e082017-08-04 03:06:51 -0700772 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
773 (long)DynamicBuffer.length,
774 (long)rval.consumed, ecbits, (long)i_size,
775 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000776
Lev Walkinfb35e082017-08-04 03:06:51 -0700777 if(DynamicBuffer.allocated == 0) {
778 /*
779 * Flush remainder into the intermediate buffer.
780 */
781 if(rval.code != RC_FAIL && rval.consumed < rd) {
782 add_bytes_to_buffer(fbuf + rval.consumed,
783 rd - rval.consumed);
784 buffer_shift_left(0, ecbits);
785 DynamicBuffer.bytes_shifted = rval.consumed;
786 rval.consumed = 0;
787 ecbits = 0;
788 }
789 }
Lev Walkin22b54552004-10-28 13:22:54 +0000790
Lev Walkinfb35e082017-08-04 03:06:51 -0700791 /*
792 * Adjust position inside the source buffer.
793 */
794 if(DynamicBuffer.allocated) {
795 DynamicBuffer.offset += rval.consumed;
796 DynamicBuffer.length -= rval.consumed;
797 } else {
798 DynamicBuffer.bytes_shifted += rval.consumed;
799 }
Lev Walkinbc691772006-09-17 11:02:53 +0000800
Lev Walkinfb35e082017-08-04 03:06:51 -0700801 switch(rval.code) {
802 case RC_OK:
803 if(ecbits) buffer_shift_left(0, ecbits);
804 DEBUG("RC_OK, finishing up with %ld+%d",
805 (long)rval.consumed, ecbits);
806 return structure;
807 case RC_WMORE:
808 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
809 " with %ld..%ld-%ld..%ld",
810 (long)rd,
811 (long)rval.consumed,
812 (long)DynamicBuffer.offset,
813 (long)DynamicBuffer.length,
814 (long)DynamicBuffer.unbits,
815 (long)DynamicBuffer.allocated);
816 if(!rd) tolerate_eof--;
817 continue;
818 case RC_FAIL:
819 break;
820 }
821 break;
822 }
Lev Walkin22b54552004-10-28 13:22:54 +0000823
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700824 DEBUG("Clean up partially decoded %s", pduType->name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700825 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000826
Lev Walkinfb35e082017-08-04 03:06:51 -0700827 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkinbc691772006-09-17 11:02:53 +0000828
Lev Walkinfb35e082017-08-04 03:06:51 -0700829 /*
830 * Print a message and return failure only if not EOF,
831 * unless this is our first PDU (empty file).
832 */
833 if(on_first_pdu
834 || DynamicBuffer.length
Lev Walkin6d46bc32017-09-12 21:34:00 -0700835 || new_offset - old_offset > ((isyntax == ATS_BASIC_XER)?sizeof("\r\n")-1:0)
Lev Walkinfb35e082017-08-04 03:06:51 -0700836 ) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000837
Lev Walkinfb35e082017-08-04 03:06:51 -0700838#ifdef JUNKTEST
839 /*
840 * Nothing's wrong with being unable to decode junk.
841 * Simulate EOF.
842 */
843 if(opt_jprob != 0.0) {
844 junk_failures++;
845 errno = 0;
846 return 0;
847 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000848#endif
849
Lev Walkinfb35e082017-08-04 03:06:51 -0700850 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
851 on_first_pdu, (long)new_offset, (long)old_offset,
852 (long)DynamicBuffer.length);
853 fprintf(stderr, "%s: "
854 "Decode failed past byte %ld: %s\n",
855 name, (long)new_offset,
856 (rval.code == RC_WMORE)
857 ? "Unexpected end of input"
858 : "Input processing error");
859#ifndef ENOMSG
860#define ENOMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000861#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700862#ifndef EBADMSG
863#define EBADMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000864#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700865 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
866 } else {
867 /* Got EOF after a few successful PDUs */
868 errno = 0;
869 }
Lev Walkin22b54552004-10-28 13:22:54 +0000870
Lev Walkinfb35e082017-08-04 03:06:51 -0700871 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000872}
873
Lev Walkin419f6752006-09-13 04:02:00 +0000874/* Dump the buffer out to the specified FILE */
875static int write_out(const void *buffer, size_t size, void *key) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700876 FILE *fp = (FILE *)key;
877 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
Lev Walkin419f6752006-09-13 04:02:00 +0000878}
Lev Walkinc744a022006-09-15 18:33:25 +0000879
880static int argument_is_stdin(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700881 if(strcmp(av[idx], "-")) {
882 return 0; /* Certainly not <stdin> */
883 } else {
884 /* This might be <stdin>, unless `./program -- -` */
885 if(strcmp(av[-1], "--"))
886 return 1;
887 else
888 return 0;
889 }
Lev Walkinc744a022006-09-15 18:33:25 +0000890}
891
892static FILE *argument_to_file(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700893 return argument_is_stdin(av, idx) ? stdin : fopen(av[idx], "rb");
Lev Walkinc744a022006-09-15 18:33:25 +0000894}
895
896static char *argument_to_name(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700897 return argument_is_stdin(av, idx) ? "standard input" : av[idx];
Lev Walkinc744a022006-09-15 18:33:25 +0000898}
Lev Walkin1f12da42006-09-24 19:47:07 +0000899
Lev Walkinfb35e082017-08-04 03:06:51 -0700900#ifdef JUNKTEST
Lev Walkin1f12da42006-09-24 19:47:07 +0000901/*
902 * Fill bytes with some garbage with specified probability (more or less).
903 */
904static void
905junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700906 static int junkmode;
907 uint8_t *ptr;
908 uint8_t *end;
909 if(opt_jprob <= 0.0) return;
910 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
911 int byte = *ptr;
912 if(junkmode++ & 1) {
913 if((((double)random() / RAND_MAX) < prob))
914 byte = random() & 0xff;
915 } else {
916#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
917 byte ^= BPROB(0x80);
918 byte ^= BPROB(0x40);
919 byte ^= BPROB(0x20);
920 byte ^= BPROB(0x10);
921 byte ^= BPROB(0x08);
922 byte ^= BPROB(0x04);
923 byte ^= BPROB(0x02);
924 byte ^= BPROB(0x01);
925 }
926 if(byte != *ptr) {
927 DEBUG("Junk buf[%d] %02x -> %02x", ptr - buf, *ptr, byte);
928 *ptr = byte;
929 }
930 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000931}
Lev Walkinfb35e082017-08-04 03:06:51 -0700932#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000933