blob: 178264e259b4009c8020c152e8fe5f18a614188b [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 */
70static inline void
71DEBUG(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
109#define OP_OFFSET(fname) (unsigned)(&(((asn_TYPE_operation_t *)0)->fname))
110typedef struct {
111 const char *name;
112 enum asn_transfer_syntax syntax;
113 unsigned codec_offset;
114 const char *full_name;
115} syntax_selector;
116
117static syntax_selector input_encodings[] = {
118 {"ber", ATS_BER, OP_OFFSET(ber_decoder),
119 "Input is in BER (Basic Encoding Rules) or DER"},
120 {"oer", ATS_BASIC_OER, OP_OFFSET(oer_decoder),
121 "Input is in OER (Octet Encoding Rules)"},
122 {"per", ATS_UNALIGNED_BASIC_PER, OP_OFFSET(uper_decoder),
123 "Input is in Unaligned PER (Packed Encoding Rules)"},
124 {"xer", ATS_BASIC_XER, OP_OFFSET(xer_decoder),
125 "Input is in XER (XML Encoding Rules)"},
126 {0, ATS_INVALID, 0, 0}};
127
128static syntax_selector output_encodings[] = {
129 {"der", ATS_DER, OP_OFFSET(der_encoder),
130 "Output as DER (Distinguished Encoding Rules)"},
131 {"oer", ATS_CANONICAL_OER, OP_OFFSET(oer_encoder),
132 "Output as Canonical OER (Octet Encoding Rules)"},
133 {"per", ATS_UNALIGNED_CANONICAL_PER, OP_OFFSET(uper_encoder),
134 "Output as Unaligned PER (Packed Encoding Rules)"},
135 {"xer", ATS_BASIC_XER, OP_OFFSET(xer_encoder),
136 "Output as XER (XML Encoding Rules)"},
137 {"text", ATS_NONSTANDARD_PLAINTEXT, OP_OFFSET(print_struct),
138 "Output as plain semi-structured text"},
139 {"null", ATS_INVALID, OP_OFFSET(print_struct),
140 "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
154 + element
155 ->codec_offset)) {
156 return element;
157 }
158 }
159 }
160 return NULL;
161}
162
Lev Walkin22b54552004-10-28 13:22:54 +0000163int
Lev Walkinc744a022006-09-15 18:33:25 +0000164main(int ac, char *av[]) {
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700165 FILE *binary_out;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700166 static asn_TYPE_descriptor_t *pduType = PDU_Type_Ptr;
Lev Walkinfb35e082017-08-04 03:06:51 -0700167 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
168 int number_of_iterations = 1;
169 int num;
170 int ch;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700171 const syntax_selector *sel;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700172 enum asn_transfer_syntax isyntax = ATS_INVALID;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700173 enum asn_transfer_syntax osyntax = ATS_BASIC_XER;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700174
175#ifndef PDU
176 if(!pduType) {
177 fprintf(stderr, "No -DPDU defined during compilation.\n");
178#ifdef NO_ASN_PDU
179 exit(0);
180#else
181 exit(EX_SOFTWARE);
182#endif
183 }
184#endif
Lev Walkin22b54552004-10-28 13:22:54 +0000185
Lev Walkinfb35e082017-08-04 03:06:51 -0700186 /* Figure out if specialty decoder needs to be default */
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700187 if(ats_by_name("oer", pduType, input_encodings))
Lev Walkin6d46bc32017-09-12 21:34:00 -0700188 isyntax = ATS_BASIC_OER;
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700189 if(ats_by_name("per", pduType, input_encodings))
Lev Walkin6d46bc32017-09-12 21:34:00 -0700190 isyntax = ATS_UNALIGNED_BASIC_PER;
Lev Walkin59b176e2005-11-26 11:25:14 +0000191
Lev Walkinfb35e082017-08-04 03:06:51 -0700192 /*
193 * Pocess the command-line argments.
194 */
195 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
196 switch(ch) {
197 case 'i':
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700198 sel = ats_by_name(optarg, pduType, input_encodings);
199 if(sel) {
200 isyntax = sel->syntax;
201 } else {
202 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
203 optarg);
204 exit(EX_UNAVAILABLE);
205 }
206 break;
Lev Walkinfb35e082017-08-04 03:06:51 -0700207 case 'o':
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700208 sel = ats_by_name(optarg, pduType, output_encodings);
209 if(sel) {
210 osyntax = sel->syntax;
211 } else {
212 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
213 optarg);
214 exit(EX_UNAVAILABLE);
215 }
216 break;
Lev Walkinfb35e082017-08-04 03:06:51 -0700217 case '1':
218 opt_onepdu = 1;
219 break;
220 case 'b':
221 suggested_bufsize = atoi(optarg);
222 if(suggested_bufsize < 1
223 || suggested_bufsize > 16 * 1024 * 1024) {
224 fprintf(stderr,
225 "-b %s: Improper buffer size (1..16M)\n",
226 optarg);
227 exit(EX_UNAVAILABLE);
228 }
229 break;
230 case 'c':
231 opt_check = 1;
232 break;
233 case 'd':
234 opt_debug++; /* Double -dd means ASN.1 debug */
235 break;
236 case 'n':
237 number_of_iterations = atoi(optarg);
238 if(number_of_iterations < 1) {
239 fprintf(stderr,
240 "-n %s: Improper iterations count\n", optarg);
241 exit(EX_UNAVAILABLE);
242 }
243 break;
244 case 'p':
Lev Walkin0f77cea2017-09-13 00:28:20 -0700245 if(strcmp(optarg, "er-nopad") == 0) {
246 opt_nopad = 1;
247 break;
248 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700249#ifdef ASN_PDU_COLLECTION
250 if(strcmp(optarg, "list") == 0) {
251 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
252 fprintf(stderr, "Available PDU types:\n");
253 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
254 exit(0);
255 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
256 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
257 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
258 if(*pdu) { pduType = *pdu; break; }
259 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkin7e5ea1d2017-08-06 00:59:28 -0700260 exit(EX_USAGE);
261 }
262#else /* Without -pdu=auto there's just a single type */
263 if(strcmp(optarg, "list") == 0) {
264 fprintf(stderr, "Available PDU types:\n");
265 printf("%s\n", pduType->name);
266 exit(0);
267 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
268 if(strcmp(optarg, pduType->name) == 0) {
269 break;
270 }
271 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
272 exit(EX_USAGE);
Lev Walkinfb35e082017-08-04 03:06:51 -0700273 }
274#endif /* ASN_PDU_COLLECTION */
275 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
276 exit(EX_UNAVAILABLE);
277 case 's':
278 opt_stack = atoi(optarg);
279 if(opt_stack < 0) {
280 fprintf(stderr,
281 "-s %s: Non-negative value expected\n",
282 optarg);
283 exit(EX_UNAVAILABLE);
284 }
285 break;
286#ifdef JUNKTEST
287 case 'J':
288 opt_jprob = strtod(optarg, 0);
289 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
290 fprintf(stderr,
291 "-J %s: Probability range 0..1 expected \n",
292 optarg);
293 exit(EX_UNAVAILABLE);
294 }
295 break;
296#endif /* JUNKTEST */
297 case 'h':
298 default:
299#ifdef ASN_CONVERTER_TITLE
300#define _AXS(x) #x
301#define _ASX(x) _AXS(x)
302 fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE));
Lev Walkin8a4a06c2007-06-29 12:44:01 +0000303#endif
Lev Walkinae0c3c22017-08-26 17:20:32 -0700304 fprintf(stderr, "Usage: %s [options] <datafile> ...\n", av[0]);
Lev Walkinfb35e082017-08-04 03:06:51 -0700305 fprintf(stderr, "Where options are:\n");
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700306 for(sel = input_encodings; sel->name; sel++) {
307 if(ats_by_name(sel->name, pduType, sel)) {
308 fprintf(stderr, " -i%s %s%s\n", sel->name,
309 sel->full_name,
310 (sel->syntax == isyntax) ? " (DEFAULT)" : "");
311 }
312 }
313 for(sel = output_encodings; sel->name; sel++) {
314 if(ats_by_name(sel->name, pduType, sel)) {
315 fprintf(stderr, " -o%s%s %s%s\n", sel->name,
316 strlen(sel->name) > 3 ? "" : " ",
317 sel->full_name,
318 (sel->syntax == osyntax) ? " (DEFAULT)" : "");
319 }
320 }
Lev Walkin0f77cea2017-09-13 00:28:20 -0700321 if(pduType->op->uper_decoder) {
322 fprintf(stderr,
323 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
324 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700325#ifdef ASN_PDU_COLLECTION
326 fprintf(stderr,
327 " -p <PDU> Specify PDU type to decode\n"
328 " -p list List available PDUs\n");
329#endif /* ASN_PDU_COLLECTION */
330 fprintf(stderr,
331 " -1 Decode only the first PDU in file\n"
332 " -b <size> Set the i/o buffer size (default is %ld)\n"
333 " -c Check ASN.1 constraints after decoding\n"
334 " -d Enable debugging (-dd is even better)\n"
335 " -n <num> Process files <num> times\n"
336 " -s <size> Set the stack usage limit (default is %d)\n"
337#ifdef JUNKTEST
338 " -J <prob> Set random junk test bit garbaging probability\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000339#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700340 , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX);
341 exit(EX_USAGE);
342 }
Lev Walkin22b54552004-10-28 13:22:54 +0000343
Lev Walkinfb35e082017-08-04 03:06:51 -0700344 ac -= optind;
345 av += optind;
Lev Walkin22b54552004-10-28 13:22:54 +0000346
Lev Walkinfb35e082017-08-04 03:06:51 -0700347 if(ac < 1) {
348 fprintf(stderr, "%s: No input files specified. "
349 "Try '-h' for more information\n",
350 av[-optind]);
351 exit(EX_USAGE);
352 }
Lev Walkin22b54552004-10-28 13:22:54 +0000353
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700354 if(isatty(1)) {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700355 const int is_text_output = osyntax == ATS_NONSTANDARD_PLAINTEXT
356 || osyntax == ATS_BASIC_XER
357 || osyntax == ATS_CANONICAL_XER;
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700358 if(is_text_output) {
359 binary_out = stdout;
360 } else {
361 fprintf(stderr, "(Suppressing binary output to a terminal.)\n");
362 binary_out = fopen("/dev/null", "wb");
363 if(!binary_out) {
364 fprintf(stderr, "Can't open /dev/null: %s\n", strerror(errno));
365 exit(EX_OSERR);
366 }
367 }
368 } else {
369 binary_out = stdout;
370 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700371 setvbuf(stdout, 0, _IOLBF, 0);
Lev Walkin22b54552004-10-28 13:22:54 +0000372
Lev Walkinfb35e082017-08-04 03:06:51 -0700373 for(num = 0; num < number_of_iterations; num++) {
374 int ac_i;
375 /*
376 * Process all files in turn.
377 */
378 for(ac_i = 0; ac_i < ac; ac_i++) {
379 asn_enc_rval_t erv;
380 void *structure; /* Decoded structure */
381 FILE *file = argument_to_file(av, ac_i);
382 char *name = argument_to_name(av, ac_i);
383 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000384
Lev Walkin6d46bc32017-09-12 21:34:00 -0700385 for(first_pdu = 1; file && (first_pdu || !opt_onepdu); first_pdu = 0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700386 /*
387 * Decode the encoded structure from file.
388 */
Lev Walkin6d46bc32017-09-12 21:34:00 -0700389 structure = data_decode_from_file(isyntax, pduType, file, name,
Lev Walkinfb35e082017-08-04 03:06:51 -0700390 suggested_bufsize, first_pdu);
391 if(!structure) {
392 if(errno) {
393 /* Error message is already printed */
394 exit(EX_DATAERR);
395 } else {
396 /* EOF */
397 break;
398 }
399 }
Lev Walkin22b54552004-10-28 13:22:54 +0000400
Lev Walkinfb35e082017-08-04 03:06:51 -0700401 /* Check ASN.1 constraints */
402 if(opt_check) {
403 char errbuf[128];
404 size_t errlen = sizeof(errbuf);
405 if(asn_check_constraints(pduType, structure, errbuf, &errlen)) {
406 fprintf(stderr,
407 "%s: ASN.1 constraint "
408 "check failed: %s\n",
409 name, errbuf);
410 exit(EX_DATAERR);
411 }
412 }
Lev Walkin22b54552004-10-28 13:22:54 +0000413
Lev Walkin6d46bc32017-09-12 21:34:00 -0700414 if(osyntax == ATS_INVALID) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700415#ifdef JUNKTEST
Lev Walkin6d46bc32017-09-12 21:34:00 -0700416 if(opt_jprob == 0.0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700417 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700418 }
Lev Walkin69033802017-08-25 12:15:58 -0700419#else
Lev Walkin6d46bc32017-09-12 21:34:00 -0700420 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkin69033802017-08-25 12:15:58 -0700421#endif
Lev Walkin6d46bc32017-09-12 21:34:00 -0700422 } else {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700423 erv = asn_encode(NULL, osyntax, pduType, structure, write_out,
424 binary_out);
Lev Walkin6d46bc32017-09-12 21:34:00 -0700425
426 if(erv.encoded == -1) {
427 fprintf(stderr, "%s: Cannot convert %s into %s\n", name,
428 pduType->name, ats_simple_name(osyntax));
Lev Walkinfb35e082017-08-04 03:06:51 -0700429 exit(EX_UNAVAILABLE);
430 }
Lev Walkin6d46bc32017-09-12 21:34:00 -0700431 DEBUG("Encoded in %zd bytes of %s", erv.encoded,
432 ats_simple_name(osyntax));
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700433 }
Lev Walkind1bfea62005-11-08 03:06:16 +0000434
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700435 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinfb35e082017-08-04 03:06:51 -0700436 }
Lev Walkinbc691772006-09-17 11:02:53 +0000437
Lev Walkinfb35e082017-08-04 03:06:51 -0700438 if(file && file != stdin) {
439 fclose(file);
440 }
441 }
442 }
Lev Walkin22b54552004-10-28 13:22:54 +0000443
Lev Walkinfb35e082017-08-04 03:06:51 -0700444#ifdef JUNKTEST
445 if(opt_jprob > 0.0) {
446 fprintf(stderr, "Junked %f OK (%d/%d)\n",
447 opt_jprob, junk_failures, number_of_iterations);
448 }
449#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000450
Lev Walkinfb35e082017-08-04 03:06:51 -0700451 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000452}
453
Lev Walkin419f6752006-09-13 04:02:00 +0000454static struct dynamic_buffer {
Lev Walkinfb35e082017-08-04 03:06:51 -0700455 uint8_t *data; /* Pointer to the data bytes */
456 size_t offset; /* Offset from the start */
457 size_t length; /* Length of meaningful contents */
458 size_t unbits; /* Unused bits in the last byte */
459 size_t allocated; /* Allocated memory for data */
460 int nreallocs; /* Number of data reallocations */
461 off_t bytes_shifted; /* Number of bytes ever shifted */
Lev Walkin419f6752006-09-13 04:02:00 +0000462} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000463
Lev Walkin5a621d62006-09-18 20:05:34 +0000464static void
465buffer_dump() {
Lev Walkinfb35e082017-08-04 03:06:51 -0700466 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
467 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
468 if(!opt_debug) return;
469 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
470 DynamicBuffer.data,
471 (long)DynamicBuffer.offset,
472 (long)DynamicBuffer.length,
473 (long)DynamicBuffer.unbits,
474 (long)DynamicBuffer.allocated,
475 (long)DynamicBuffer.bytes_shifted);
476 for(; p < e; p++) {
477 fprintf(stderr, " %c%c%c%c%c%c%c%c",
478 ((*p >> 7) & 1) ? '1' : '0',
479 ((*p >> 6) & 1) ? '1' : '0',
480 ((*p >> 5) & 1) ? '1' : '0',
481 ((*p >> 4) & 1) ? '1' : '0',
482 ((*p >> 3) & 1) ? '1' : '0',
483 ((*p >> 2) & 1) ? '1' : '0',
484 ((*p >> 1) & 1) ? '1' : '0',
485 ((*p >> 0) & 1) ? '1' : '0');
486 }
487 if(DynamicBuffer.unbits) {
488 unsigned int shift;
489 fprintf(stderr, " ");
490 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
491 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
492 fprintf(stderr, " %ld:%ld\n",
493 (long)DynamicBuffer.length - 1,
494 (long)8 - DynamicBuffer.unbits);
495 } else {
496 fprintf(stderr, " %ld\n", (long)DynamicBuffer.length);
497 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000498}
499
500/*
501 * Move the buffer content left N bits, possibly joining it with
502 * preceeding content.
503 */
504static void
505buffer_shift_left(size_t offset, int bits) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700506 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
507 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
508 + DynamicBuffer.length - 1;
509
510 if(!bits) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000511
Lev Walkinfb35e082017-08-04 03:06:51 -0700512 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
513 bits, (long)offset,
514 (long)DynamicBuffer.offset,
515 (long)DynamicBuffer.unbits,
516 (long)DynamicBuffer.length);
Lev Walkin5a621d62006-09-18 20:05:34 +0000517
Lev Walkinfb35e082017-08-04 03:06:51 -0700518 if(offset) {
519 int right;
520 right = ptr[0] >> (8 - bits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000521
Lev Walkinfb35e082017-08-04 03:06:51 -0700522 DEBUG("oleft: %c%c%c%c%c%c%c%c",
523 ((ptr[-1] >> 7) & 1) ? '1' : '0',
524 ((ptr[-1] >> 6) & 1) ? '1' : '0',
525 ((ptr[-1] >> 5) & 1) ? '1' : '0',
526 ((ptr[-1] >> 4) & 1) ? '1' : '0',
527 ((ptr[-1] >> 3) & 1) ? '1' : '0',
528 ((ptr[-1] >> 2) & 1) ? '1' : '0',
529 ((ptr[-1] >> 1) & 1) ? '1' : '0',
530 ((ptr[-1] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000531
Lev Walkinfb35e082017-08-04 03:06:51 -0700532 DEBUG("oriht: %c%c%c%c%c%c%c%c",
533 ((ptr[0] >> 7) & 1) ? '1' : '0',
534 ((ptr[0] >> 6) & 1) ? '1' : '0',
535 ((ptr[0] >> 5) & 1) ? '1' : '0',
536 ((ptr[0] >> 4) & 1) ? '1' : '0',
537 ((ptr[0] >> 3) & 1) ? '1' : '0',
538 ((ptr[0] >> 2) & 1) ? '1' : '0',
539 ((ptr[0] >> 1) & 1) ? '1' : '0',
540 ((ptr[0] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000541
Lev Walkinfb35e082017-08-04 03:06:51 -0700542 DEBUG("mriht: %c%c%c%c%c%c%c%c",
543 ((right >> 7) & 1) ? '1' : '0',
544 ((right >> 6) & 1) ? '1' : '0',
545 ((right >> 5) & 1) ? '1' : '0',
546 ((right >> 4) & 1) ? '1' : '0',
547 ((right >> 3) & 1) ? '1' : '0',
548 ((right >> 2) & 1) ? '1' : '0',
549 ((right >> 1) & 1) ? '1' : '0',
550 ((right >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000551
Lev Walkinfb35e082017-08-04 03:06:51 -0700552 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
Lev Walkin5a621d62006-09-18 20:05:34 +0000553
Lev Walkinfb35e082017-08-04 03:06:51 -0700554 DEBUG("after: %c%c%c%c%c%c%c%c",
555 ((ptr[-1] >> 7) & 1) ? '1' : '0',
556 ((ptr[-1] >> 6) & 1) ? '1' : '0',
557 ((ptr[-1] >> 5) & 1) ? '1' : '0',
558 ((ptr[-1] >> 4) & 1) ? '1' : '0',
559 ((ptr[-1] >> 3) & 1) ? '1' : '0',
560 ((ptr[-1] >> 2) & 1) ? '1' : '0',
561 ((ptr[-1] >> 1) & 1) ? '1' : '0',
562 ((ptr[-1] >> 0) & 1) ? '1' : '0');
563 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000564
Lev Walkinfb35e082017-08-04 03:06:51 -0700565 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000566
Lev Walkinfb35e082017-08-04 03:06:51 -0700567 for(; ptr < end; ptr++) {
568 int right = ptr[1] >> (8 - bits);
569 *ptr = (*ptr << bits) | right;
570 }
571 *ptr <<= bits;
Lev Walkin5a621d62006-09-18 20:05:34 +0000572
Lev Walkinfb35e082017-08-04 03:06:51 -0700573 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
574 if(DynamicBuffer.unbits == 0) {
575 DynamicBuffer.unbits += bits;
576 } else {
577 DynamicBuffer.unbits += bits;
578 if(DynamicBuffer.unbits > 7) {
579 DynamicBuffer.unbits -= 8;
580 DynamicBuffer.length--;
581 DynamicBuffer.bytes_shifted++;
582 }
583 }
584 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000585
Lev Walkinfb35e082017-08-04 03:06:51 -0700586 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000587
Lev Walkinfb35e082017-08-04 03:06:51 -0700588 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
589 (long)DynamicBuffer.offset,
590 (long)DynamicBuffer.unbits,
591 (long)DynamicBuffer.length);
592
Lev Walkin5a621d62006-09-18 20:05:34 +0000593
594}
595
Lev Walkin22b54552004-10-28 13:22:54 +0000596/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000597 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000598 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000599static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000600
Lev Walkinfb35e082017-08-04 03:06:51 -0700601 if(bytes == 0) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000602
Lev Walkinfb35e082017-08-04 03:06:51 -0700603 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
604 (long)bytes,
605 (long)DynamicBuffer.offset,
606 (long)DynamicBuffer.length,
607 (long)DynamicBuffer.unbits,
608 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000609
Lev Walkinfb35e082017-08-04 03:06:51 -0700610 if(DynamicBuffer.allocated
611 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
612 DEBUG("\tNo buffer reallocation is necessary");
613 } else if(bytes <= DynamicBuffer.offset) {
614 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000615
Lev Walkinfb35e082017-08-04 03:06:51 -0700616 /* Shift the buffer contents */
617 memmove(DynamicBuffer.data,
618 DynamicBuffer.data + DynamicBuffer.offset,
619 DynamicBuffer.length);
620 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
621 DynamicBuffer.offset = 0;
622 } else {
623 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
624 void *p = MALLOC(newsize);
625 if(!p) {
626 perror("malloc()");
627 exit(EX_OSERR);
628 }
629 memcpy(p,
630 DynamicBuffer.data + DynamicBuffer.offset,
631 DynamicBuffer.length);
632 FREEMEM(DynamicBuffer.data);
633 DynamicBuffer.data = (uint8_t *)p;
634 DynamicBuffer.offset = 0;
635 DynamicBuffer.allocated = newsize;
636 DynamicBuffer.nreallocs++;
637 DEBUG("\tBuffer reallocated to %ld (%d time)",
638 newsize, DynamicBuffer.nreallocs);
639 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000640
Lev Walkinfb35e082017-08-04 03:06:51 -0700641 memcpy(DynamicBuffer.data
642 + DynamicBuffer.offset + DynamicBuffer.length,
643 data2add, bytes);
644 DynamicBuffer.length += bytes;
645 if(DynamicBuffer.unbits) {
646 int bits = DynamicBuffer.unbits;
647 DynamicBuffer.unbits = 0;
648 buffer_shift_left(DynamicBuffer.length - bytes, bits);
649 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000650
Lev Walkinfb35e082017-08-04 03:06:51 -0700651 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
652 (long)bytes,
653 (long)DynamicBuffer.offset,
654 (long)DynamicBuffer.length,
655 (long)DynamicBuffer.unbits,
656 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000657}
658
Lev Walkine8bbe932017-09-12 23:06:52 -0700659static int
Lev Walkin0f77cea2017-09-13 00:28:20 -0700660is_syntax_PER(enum asn_transfer_syntax syntax) {
Lev Walkine8bbe932017-09-12 23:06:52 -0700661 return (syntax != ATS_UNALIGNED_BASIC_PER
662 && syntax != ATS_UNALIGNED_CANONICAL_PER);
663}
664
Lev Walkin0f77cea2017-09-13 00:28:20 -0700665static int
666restartability_supported(enum asn_transfer_syntax syntax) {
667 return is_syntax_PER(syntax);
668}
669
Lev Walkinc744a022006-09-15 18:33:25 +0000670static void *
Lev Walkin6d46bc32017-09-12 21:34:00 -0700671data_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 -0700672 static uint8_t *fbuf;
673 static ssize_t fbuf_size;
674 static asn_codec_ctx_t s_codec_ctx;
675 asn_codec_ctx_t *opt_codec_ctx = 0;
676 void *structure = 0;
677 asn_dec_rval_t rval;
678 size_t old_offset;
679 size_t new_offset;
680 int tolerate_eof;
681 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000682
Lev Walkinfb35e082017-08-04 03:06:51 -0700683 if(!file) {
684 fprintf(stderr, "%s: %s\n", name, strerror(errno));
685 errno = EINVAL;
686 return 0;
687 }
Lev Walkin22b54552004-10-28 13:22:54 +0000688
Lev Walkinfb35e082017-08-04 03:06:51 -0700689 if(opt_stack) {
690 s_codec_ctx.max_stack_size = opt_stack;
691 opt_codec_ctx = &s_codec_ctx;
692 }
Lev Walkin22b54552004-10-28 13:22:54 +0000693
Lev Walkinfb35e082017-08-04 03:06:51 -0700694 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000695
Lev Walkinfb35e082017-08-04 03:06:51 -0700696 /* prepare the file buffer */
697 if(fbuf_size != suggested_bufsize) {
698 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
699 if(!fbuf) {
700 perror("realloc()");
701 exit(EX_OSERR);
702 }
703 fbuf_size = suggested_bufsize;
704 }
Lev Walkin22b54552004-10-28 13:22:54 +0000705
Lev Walkinfb35e082017-08-04 03:06:51 -0700706 if(on_first_pdu) {
707 DynamicBuffer.offset = 0;
708 DynamicBuffer.length = 0;
709 DynamicBuffer.unbits = 0;
710 DynamicBuffer.allocated = 0;
711 DynamicBuffer.bytes_shifted = 0;
712 DynamicBuffer.nreallocs = 0;
713 }
Lev Walkinbc691772006-09-17 11:02:53 +0000714
Lev Walkinfb35e082017-08-04 03:06:51 -0700715 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000716
Lev Walkinfb35e082017-08-04 03:06:51 -0700717 /* Pretend immediate EOF */
718 rval.code = RC_WMORE;
719 rval.consumed = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000720
Lev Walkinfb35e082017-08-04 03:06:51 -0700721 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
722 (rd = fread(fbuf, 1, fbuf_size, file))
723 || feof(file) == 0
724 || (tolerate_eof && DynamicBuffer.length)
725 ;) {
726 int ecbits = 0; /* Extra consumed bits in case of PER */
727 uint8_t *i_bptr;
728 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000729
Lev Walkinfb35e082017-08-04 03:06:51 -0700730 /*
731 * Copy the data over, or use the original buffer.
732 */
733 if(DynamicBuffer.allocated) {
734 /* Append new data into the existing dynamic buffer */
735 add_bytes_to_buffer(fbuf, rd);
736 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
737 i_size = DynamicBuffer.length;
738 } else {
739 i_bptr = fbuf;
740 i_size = rd;
741 }
Lev Walkin22b54552004-10-28 13:22:54 +0000742
Lev Walkinfb35e082017-08-04 03:06:51 -0700743 DEBUG("Decoding %ld bytes", (long)i_size);
Lev Walkinbc691772006-09-17 11:02:53 +0000744
Lev Walkinfb35e082017-08-04 03:06:51 -0700745#ifdef JUNKTEST
746 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
Lev Walkin1f12da42006-09-24 19:47:07 +0000747#endif
748
Lev Walkin0f77cea2017-09-13 00:28:20 -0700749 if(is_syntax_PER(isyntax) && opt_nopad) {
750#ifdef ASN_DISABLE_PER_SUPPORT
751 rval.code = RC_FAIL;
752 rval.consumed = 0;
753#else
754 rval = uper_decode(opt_codec_ctx, pduType, (void **)&structure,
755 i_bptr, i_size, 0, DynamicBuffer.unbits);
756 /* uper_decode() returns bits! */
757 ecbits = rval.consumed % 8; /* Bits consumed from the last byte */
758 rval.consumed >>= 3; /* Convert bits into bytes. */
759#endif
760 /* Non-padded PER decoder */
761 } else {
762 rval = asn_decode(opt_codec_ctx, isyntax, pduType,
763 (void **)&structure, i_bptr, i_size);
764 }
Lev Walkine8bbe932017-09-12 23:06:52 -0700765 if(rval.code == RC_WMORE && !restartability_supported(isyntax)) {
766 /* PER does not support restartability */
767 ASN_STRUCT_FREE(*pduType, structure);
768 structure = 0;
Lev Walkin69033802017-08-25 12:15:58 -0700769 rval.consumed = 0;
Lev Walkine8bbe932017-09-12 23:06:52 -0700770 /* Continue accumulating data */
Lev Walkinfb35e082017-08-04 03:06:51 -0700771 }
Lev Walkine8bbe932017-09-12 23:06:52 -0700772
Lev Walkinfb35e082017-08-04 03:06:51 -0700773 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
774 (long)DynamicBuffer.length,
775 (long)rval.consumed, ecbits, (long)i_size,
776 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000777
Lev Walkinfb35e082017-08-04 03:06:51 -0700778 if(DynamicBuffer.allocated == 0) {
779 /*
780 * Flush remainder into the intermediate buffer.
781 */
782 if(rval.code != RC_FAIL && rval.consumed < rd) {
783 add_bytes_to_buffer(fbuf + rval.consumed,
784 rd - rval.consumed);
785 buffer_shift_left(0, ecbits);
786 DynamicBuffer.bytes_shifted = rval.consumed;
787 rval.consumed = 0;
788 ecbits = 0;
789 }
790 }
Lev Walkin22b54552004-10-28 13:22:54 +0000791
Lev Walkinfb35e082017-08-04 03:06:51 -0700792 /*
793 * Adjust position inside the source buffer.
794 */
795 if(DynamicBuffer.allocated) {
796 DynamicBuffer.offset += rval.consumed;
797 DynamicBuffer.length -= rval.consumed;
798 } else {
799 DynamicBuffer.bytes_shifted += rval.consumed;
800 }
Lev Walkinbc691772006-09-17 11:02:53 +0000801
Lev Walkinfb35e082017-08-04 03:06:51 -0700802 switch(rval.code) {
803 case RC_OK:
804 if(ecbits) buffer_shift_left(0, ecbits);
805 DEBUG("RC_OK, finishing up with %ld+%d",
806 (long)rval.consumed, ecbits);
807 return structure;
808 case RC_WMORE:
809 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
810 " with %ld..%ld-%ld..%ld",
811 (long)rd,
812 (long)rval.consumed,
813 (long)DynamicBuffer.offset,
814 (long)DynamicBuffer.length,
815 (long)DynamicBuffer.unbits,
816 (long)DynamicBuffer.allocated);
817 if(!rd) tolerate_eof--;
818 continue;
819 case RC_FAIL:
820 break;
821 }
822 break;
823 }
Lev Walkin22b54552004-10-28 13:22:54 +0000824
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700825 DEBUG("Clean up partially decoded %s", pduType->name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700826 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000827
Lev Walkinfb35e082017-08-04 03:06:51 -0700828 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkinbc691772006-09-17 11:02:53 +0000829
Lev Walkinfb35e082017-08-04 03:06:51 -0700830 /*
831 * Print a message and return failure only if not EOF,
832 * unless this is our first PDU (empty file).
833 */
834 if(on_first_pdu
835 || DynamicBuffer.length
Lev Walkin6d46bc32017-09-12 21:34:00 -0700836 || new_offset - old_offset > ((isyntax == ATS_BASIC_XER)?sizeof("\r\n")-1:0)
Lev Walkinfb35e082017-08-04 03:06:51 -0700837 ) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000838
Lev Walkinfb35e082017-08-04 03:06:51 -0700839#ifdef JUNKTEST
840 /*
841 * Nothing's wrong with being unable to decode junk.
842 * Simulate EOF.
843 */
844 if(opt_jprob != 0.0) {
845 junk_failures++;
846 errno = 0;
847 return 0;
848 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000849#endif
850
Lev Walkinfb35e082017-08-04 03:06:51 -0700851 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
852 on_first_pdu, (long)new_offset, (long)old_offset,
853 (long)DynamicBuffer.length);
854 fprintf(stderr, "%s: "
855 "Decode failed past byte %ld: %s\n",
856 name, (long)new_offset,
857 (rval.code == RC_WMORE)
858 ? "Unexpected end of input"
859 : "Input processing error");
860#ifndef ENOMSG
861#define ENOMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000862#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700863#ifndef EBADMSG
864#define EBADMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000865#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700866 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
867 } else {
868 /* Got EOF after a few successful PDUs */
869 errno = 0;
870 }
Lev Walkin22b54552004-10-28 13:22:54 +0000871
Lev Walkinfb35e082017-08-04 03:06:51 -0700872 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000873}
874
Lev Walkin419f6752006-09-13 04:02:00 +0000875/* Dump the buffer out to the specified FILE */
876static int write_out(const void *buffer, size_t size, void *key) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700877 FILE *fp = (FILE *)key;
878 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
Lev Walkin419f6752006-09-13 04:02:00 +0000879}
Lev Walkinc744a022006-09-15 18:33:25 +0000880
881static int argument_is_stdin(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700882 if(strcmp(av[idx], "-")) {
883 return 0; /* Certainly not <stdin> */
884 } else {
885 /* This might be <stdin>, unless `./program -- -` */
886 if(strcmp(av[-1], "--"))
887 return 1;
888 else
889 return 0;
890 }
Lev Walkinc744a022006-09-15 18:33:25 +0000891}
892
893static FILE *argument_to_file(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700894 return argument_is_stdin(av, idx) ? stdin : fopen(av[idx], "rb");
Lev Walkinc744a022006-09-15 18:33:25 +0000895}
896
897static char *argument_to_name(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700898 return argument_is_stdin(av, idx) ? "standard input" : av[idx];
Lev Walkinc744a022006-09-15 18:33:25 +0000899}
Lev Walkin1f12da42006-09-24 19:47:07 +0000900
Lev Walkinfb35e082017-08-04 03:06:51 -0700901#ifdef JUNKTEST
Lev Walkin1f12da42006-09-24 19:47:07 +0000902/*
903 * Fill bytes with some garbage with specified probability (more or less).
904 */
905static void
906junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700907 static int junkmode;
908 uint8_t *ptr;
909 uint8_t *end;
910 if(opt_jprob <= 0.0) return;
911 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
912 int byte = *ptr;
913 if(junkmode++ & 1) {
914 if((((double)random() / RAND_MAX) < prob))
915 byte = random() & 0xff;
916 } else {
917#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
918 byte ^= BPROB(0x80);
919 byte ^= BPROB(0x40);
920 byte ^= BPROB(0x20);
921 byte ^= BPROB(0x10);
922 byte ^= BPROB(0x08);
923 byte ^= BPROB(0x04);
924 byte ^= BPROB(0x02);
925 byte ^= BPROB(0x01);
926 }
927 if(byte != *ptr) {
928 DEBUG("Junk buf[%d] %02x -> %02x", ptr - buf, *ptr, byte);
929 *ptr = byte;
930 }
931 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000932}
Lev Walkinfb35e082017-08-04 03:06:51 -0700933#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000934