blob: c95b0d0950ead0f5467d6b9b3c4a59a663ae4c25 [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 Walkinfb35e082017-08-04 03:06:51 -070054 int opt_debug; /* -d (or -dd) */
55static int opt_check; /* -c (constraints checking) */
56static int opt_stack; /* -s (maximum stack size) */
Lev Walkinfb35e082017-08-04 03:06:51 -070057static int opt_onepdu; /* -1 (decode single PDU) */
Lev Walkind1bfea62005-11-08 03:06:16 +000058
Lev Walkinfb35e082017-08-04 03:06:51 -070059#ifdef JUNKTEST /* Enable -J <probability> */
60#define JUNKOPT "J:"
61static double opt_jprob; /* Junk bit probability */
Lev Walkin1f12da42006-09-24 19:47:07 +000062static int junk_failures;
63static void junk_bytes_with_probability(uint8_t *, size_t, double prob);
64#else
Lev Walkinfb35e082017-08-04 03:06:51 -070065#define JUNKOPT
Lev Walkin1f12da42006-09-24 19:47:07 +000066#endif
67
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000068/* Debug output function */
69static inline void
70DEBUG(const char *fmt, ...) {
Lev Walkinfb35e082017-08-04 03:06:51 -070071 va_list ap;
72 if(!opt_debug) return;
73 fprintf(stderr, "AD: ");
74 va_start(ap, fmt);
75 vfprintf(stderr, fmt, ap);
76 va_end(ap);
77 fprintf(stderr, "\n");
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000078}
Lev Walkin22b54552004-10-28 13:22:54 +000079
Lev Walkin6d46bc32017-09-12 21:34:00 -070080static const char *
81ats_simple_name(enum asn_transfer_syntax syntax) {
82 switch(syntax) {
83 case ATS_INVALID:
84 return "/dev/null";
85 case ATS_NONSTANDARD_PLAINTEXT:
86 return "plaintext";
87 case ATS_BER:
88 return "BER";
89 case ATS_DER:
90 return "DER";
91 case ATS_CER:
92 return "CER";
93 case ATS_BASIC_OER:
94 case ATS_CANONICAL_OER:
95 return "OER";
96 case ATS_BASIC_XER:
97 case ATS_CANONICAL_XER:
98 return "XER";
99 case ATS_UNALIGNED_BASIC_PER:
100 case ATS_UNALIGNED_CANONICAL_PER:
101 return "PER";
102 default:
103 return "<?>";
104 }
105}
106
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700107
108#define OP_OFFSET(fname) (unsigned)(&(((asn_TYPE_operation_t *)0)->fname))
109typedef struct {
110 const char *name;
111 enum asn_transfer_syntax syntax;
112 unsigned codec_offset;
113 const char *full_name;
114} syntax_selector;
115
116static syntax_selector input_encodings[] = {
117 {"ber", ATS_BER, OP_OFFSET(ber_decoder),
118 "Input is in BER (Basic Encoding Rules) or DER"},
119 {"oer", ATS_BASIC_OER, OP_OFFSET(oer_decoder),
120 "Input is in OER (Octet Encoding Rules)"},
121 {"per", ATS_UNALIGNED_BASIC_PER, OP_OFFSET(uper_decoder),
122 "Input is in Unaligned PER (Packed Encoding Rules)"},
123 {"xer", ATS_BASIC_XER, OP_OFFSET(xer_decoder),
124 "Input is in XER (XML Encoding Rules)"},
125 {0, ATS_INVALID, 0, 0}};
126
127static syntax_selector output_encodings[] = {
128 {"der", ATS_DER, OP_OFFSET(der_encoder),
129 "Output as DER (Distinguished Encoding Rules)"},
130 {"oer", ATS_CANONICAL_OER, OP_OFFSET(oer_encoder),
131 "Output as Canonical OER (Octet Encoding Rules)"},
132 {"per", ATS_UNALIGNED_CANONICAL_PER, OP_OFFSET(uper_encoder),
133 "Output as Unaligned PER (Packed Encoding Rules)"},
134 {"xer", ATS_BASIC_XER, OP_OFFSET(xer_encoder),
135 "Output as XER (XML Encoding Rules)"},
136 {"text", ATS_NONSTANDARD_PLAINTEXT, OP_OFFSET(print_struct),
137 "Output as plain semi-structured text"},
138 {"null", ATS_INVALID, OP_OFFSET(print_struct),
139 "Verify (decode) input, but do not output"},
140 {0, ATS_INVALID, 0, 0}};
141
142/*
143 * Select ASN.1 Transfer Enocoding Syntax by command line name.
144 */
145static const syntax_selector *
146ats_by_name(const char *name, const asn_TYPE_descriptor_t *td,
147 const syntax_selector *first_element) {
148 const syntax_selector *element;
149 for(element = first_element; element->name; element++) {
150 if(strcmp(element->name, name) == 0) {
151 if(td && td->op
152 && *(const void *const *)(const void *)((const char *)td->op
153 + element
154 ->codec_offset)) {
155 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 Walkinfb35e082017-08-04 03:06:51 -0700244#ifdef ASN_PDU_COLLECTION
245 if(strcmp(optarg, "list") == 0) {
246 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
247 fprintf(stderr, "Available PDU types:\n");
248 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
249 exit(0);
250 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
251 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
252 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
253 if(*pdu) { pduType = *pdu; break; }
254 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkin7e5ea1d2017-08-06 00:59:28 -0700255 exit(EX_USAGE);
256 }
257#else /* Without -pdu=auto there's just a single type */
258 if(strcmp(optarg, "list") == 0) {
259 fprintf(stderr, "Available PDU types:\n");
260 printf("%s\n", pduType->name);
261 exit(0);
262 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
263 if(strcmp(optarg, pduType->name) == 0) {
264 break;
265 }
266 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
267 exit(EX_USAGE);
Lev Walkinfb35e082017-08-04 03:06:51 -0700268 }
269#endif /* ASN_PDU_COLLECTION */
270 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
271 exit(EX_UNAVAILABLE);
272 case 's':
273 opt_stack = atoi(optarg);
274 if(opt_stack < 0) {
275 fprintf(stderr,
276 "-s %s: Non-negative value expected\n",
277 optarg);
278 exit(EX_UNAVAILABLE);
279 }
280 break;
281#ifdef JUNKTEST
282 case 'J':
283 opt_jprob = strtod(optarg, 0);
284 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
285 fprintf(stderr,
286 "-J %s: Probability range 0..1 expected \n",
287 optarg);
288 exit(EX_UNAVAILABLE);
289 }
290 break;
291#endif /* JUNKTEST */
292 case 'h':
293 default:
294#ifdef ASN_CONVERTER_TITLE
295#define _AXS(x) #x
296#define _ASX(x) _AXS(x)
297 fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE));
Lev Walkin8a4a06c2007-06-29 12:44:01 +0000298#endif
Lev Walkinae0c3c22017-08-26 17:20:32 -0700299 fprintf(stderr, "Usage: %s [options] <datafile> ...\n", av[0]);
Lev Walkinfb35e082017-08-04 03:06:51 -0700300 fprintf(stderr, "Where options are:\n");
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700301 for(sel = input_encodings; sel->name; sel++) {
302 if(ats_by_name(sel->name, pduType, sel)) {
303 fprintf(stderr, " -i%s %s%s\n", sel->name,
304 sel->full_name,
305 (sel->syntax == isyntax) ? " (DEFAULT)" : "");
306 }
307 }
308 for(sel = output_encodings; sel->name; sel++) {
309 if(ats_by_name(sel->name, pduType, sel)) {
310 fprintf(stderr, " -o%s%s %s%s\n", sel->name,
311 strlen(sel->name) > 3 ? "" : " ",
312 sel->full_name,
313 (sel->syntax == osyntax) ? " (DEFAULT)" : "");
314 }
315 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700316#ifdef ASN_PDU_COLLECTION
317 fprintf(stderr,
318 " -p <PDU> Specify PDU type to decode\n"
319 " -p list List available PDUs\n");
320#endif /* ASN_PDU_COLLECTION */
321 fprintf(stderr,
322 " -1 Decode only the first PDU in file\n"
323 " -b <size> Set the i/o buffer size (default is %ld)\n"
324 " -c Check ASN.1 constraints after decoding\n"
325 " -d Enable debugging (-dd is even better)\n"
326 " -n <num> Process files <num> times\n"
327 " -s <size> Set the stack usage limit (default is %d)\n"
328#ifdef JUNKTEST
329 " -J <prob> Set random junk test bit garbaging probability\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000330#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700331 , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX);
332 exit(EX_USAGE);
333 }
Lev Walkin22b54552004-10-28 13:22:54 +0000334
Lev Walkinfb35e082017-08-04 03:06:51 -0700335 ac -= optind;
336 av += optind;
Lev Walkin22b54552004-10-28 13:22:54 +0000337
Lev Walkinfb35e082017-08-04 03:06:51 -0700338 if(ac < 1) {
339 fprintf(stderr, "%s: No input files specified. "
340 "Try '-h' for more information\n",
341 av[-optind]);
342 exit(EX_USAGE);
343 }
Lev Walkin22b54552004-10-28 13:22:54 +0000344
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700345 if(isatty(1)) {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700346 const int is_text_output = osyntax == ATS_NONSTANDARD_PLAINTEXT
347 || osyntax == ATS_BASIC_XER
348 || osyntax == ATS_CANONICAL_XER;
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700349 if(is_text_output) {
350 binary_out = stdout;
351 } else {
352 fprintf(stderr, "(Suppressing binary output to a terminal.)\n");
353 binary_out = fopen("/dev/null", "wb");
354 if(!binary_out) {
355 fprintf(stderr, "Can't open /dev/null: %s\n", strerror(errno));
356 exit(EX_OSERR);
357 }
358 }
359 } else {
360 binary_out = stdout;
361 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700362 setvbuf(stdout, 0, _IOLBF, 0);
Lev Walkin22b54552004-10-28 13:22:54 +0000363
Lev Walkinfb35e082017-08-04 03:06:51 -0700364 for(num = 0; num < number_of_iterations; num++) {
365 int ac_i;
366 /*
367 * Process all files in turn.
368 */
369 for(ac_i = 0; ac_i < ac; ac_i++) {
370 asn_enc_rval_t erv;
371 void *structure; /* Decoded structure */
372 FILE *file = argument_to_file(av, ac_i);
373 char *name = argument_to_name(av, ac_i);
374 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000375
Lev Walkin6d46bc32017-09-12 21:34:00 -0700376 for(first_pdu = 1; file && (first_pdu || !opt_onepdu); first_pdu = 0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700377 /*
378 * Decode the encoded structure from file.
379 */
Lev Walkin6d46bc32017-09-12 21:34:00 -0700380 structure = data_decode_from_file(isyntax, pduType, file, name,
Lev Walkinfb35e082017-08-04 03:06:51 -0700381 suggested_bufsize, first_pdu);
382 if(!structure) {
383 if(errno) {
384 /* Error message is already printed */
385 exit(EX_DATAERR);
386 } else {
387 /* EOF */
388 break;
389 }
390 }
Lev Walkin22b54552004-10-28 13:22:54 +0000391
Lev Walkinfb35e082017-08-04 03:06:51 -0700392 /* Check ASN.1 constraints */
393 if(opt_check) {
394 char errbuf[128];
395 size_t errlen = sizeof(errbuf);
396 if(asn_check_constraints(pduType, structure, errbuf, &errlen)) {
397 fprintf(stderr,
398 "%s: ASN.1 constraint "
399 "check failed: %s\n",
400 name, errbuf);
401 exit(EX_DATAERR);
402 }
403 }
Lev Walkin22b54552004-10-28 13:22:54 +0000404
Lev Walkin6d46bc32017-09-12 21:34:00 -0700405 if(osyntax == ATS_INVALID) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700406#ifdef JUNKTEST
Lev Walkin6d46bc32017-09-12 21:34:00 -0700407 if(opt_jprob == 0.0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700408 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700409 }
Lev Walkin69033802017-08-25 12:15:58 -0700410#else
Lev Walkin6d46bc32017-09-12 21:34:00 -0700411 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkin69033802017-08-25 12:15:58 -0700412#endif
Lev Walkin6d46bc32017-09-12 21:34:00 -0700413 } else {
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700414 erv = asn_encode(NULL, osyntax, pduType, structure, write_out,
415 binary_out);
Lev Walkin6d46bc32017-09-12 21:34:00 -0700416
417 if(erv.encoded == -1) {
418 fprintf(stderr, "%s: Cannot convert %s into %s\n", name,
419 pduType->name, ats_simple_name(osyntax));
Lev Walkinfb35e082017-08-04 03:06:51 -0700420 exit(EX_UNAVAILABLE);
421 }
Lev Walkin6d46bc32017-09-12 21:34:00 -0700422 DEBUG("Encoded in %zd bytes of %s", erv.encoded,
423 ats_simple_name(osyntax));
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700424 }
Lev Walkind1bfea62005-11-08 03:06:16 +0000425
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700426 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinfb35e082017-08-04 03:06:51 -0700427 }
Lev Walkinbc691772006-09-17 11:02:53 +0000428
Lev Walkinfb35e082017-08-04 03:06:51 -0700429 if(file && file != stdin) {
430 fclose(file);
431 }
432 }
433 }
Lev Walkin22b54552004-10-28 13:22:54 +0000434
Lev Walkinfb35e082017-08-04 03:06:51 -0700435#ifdef JUNKTEST
436 if(opt_jprob > 0.0) {
437 fprintf(stderr, "Junked %f OK (%d/%d)\n",
438 opt_jprob, junk_failures, number_of_iterations);
439 }
440#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000441
Lev Walkinfb35e082017-08-04 03:06:51 -0700442 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000443}
444
Lev Walkin419f6752006-09-13 04:02:00 +0000445static struct dynamic_buffer {
Lev Walkinfb35e082017-08-04 03:06:51 -0700446 uint8_t *data; /* Pointer to the data bytes */
447 size_t offset; /* Offset from the start */
448 size_t length; /* Length of meaningful contents */
449 size_t unbits; /* Unused bits in the last byte */
450 size_t allocated; /* Allocated memory for data */
451 int nreallocs; /* Number of data reallocations */
452 off_t bytes_shifted; /* Number of bytes ever shifted */
Lev Walkin419f6752006-09-13 04:02:00 +0000453} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000454
Lev Walkin5a621d62006-09-18 20:05:34 +0000455static void
456buffer_dump() {
Lev Walkinfb35e082017-08-04 03:06:51 -0700457 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
458 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
459 if(!opt_debug) return;
460 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
461 DynamicBuffer.data,
462 (long)DynamicBuffer.offset,
463 (long)DynamicBuffer.length,
464 (long)DynamicBuffer.unbits,
465 (long)DynamicBuffer.allocated,
466 (long)DynamicBuffer.bytes_shifted);
467 for(; p < e; p++) {
468 fprintf(stderr, " %c%c%c%c%c%c%c%c",
469 ((*p >> 7) & 1) ? '1' : '0',
470 ((*p >> 6) & 1) ? '1' : '0',
471 ((*p >> 5) & 1) ? '1' : '0',
472 ((*p >> 4) & 1) ? '1' : '0',
473 ((*p >> 3) & 1) ? '1' : '0',
474 ((*p >> 2) & 1) ? '1' : '0',
475 ((*p >> 1) & 1) ? '1' : '0',
476 ((*p >> 0) & 1) ? '1' : '0');
477 }
478 if(DynamicBuffer.unbits) {
479 unsigned int shift;
480 fprintf(stderr, " ");
481 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
482 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
483 fprintf(stderr, " %ld:%ld\n",
484 (long)DynamicBuffer.length - 1,
485 (long)8 - DynamicBuffer.unbits);
486 } else {
487 fprintf(stderr, " %ld\n", (long)DynamicBuffer.length);
488 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000489}
490
491/*
492 * Move the buffer content left N bits, possibly joining it with
493 * preceeding content.
494 */
495static void
496buffer_shift_left(size_t offset, int bits) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700497 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
498 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
499 + DynamicBuffer.length - 1;
500
501 if(!bits) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000502
Lev Walkinfb35e082017-08-04 03:06:51 -0700503 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
504 bits, (long)offset,
505 (long)DynamicBuffer.offset,
506 (long)DynamicBuffer.unbits,
507 (long)DynamicBuffer.length);
Lev Walkin5a621d62006-09-18 20:05:34 +0000508
Lev Walkinfb35e082017-08-04 03:06:51 -0700509 if(offset) {
510 int right;
511 right = ptr[0] >> (8 - bits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000512
Lev Walkinfb35e082017-08-04 03:06:51 -0700513 DEBUG("oleft: %c%c%c%c%c%c%c%c",
514 ((ptr[-1] >> 7) & 1) ? '1' : '0',
515 ((ptr[-1] >> 6) & 1) ? '1' : '0',
516 ((ptr[-1] >> 5) & 1) ? '1' : '0',
517 ((ptr[-1] >> 4) & 1) ? '1' : '0',
518 ((ptr[-1] >> 3) & 1) ? '1' : '0',
519 ((ptr[-1] >> 2) & 1) ? '1' : '0',
520 ((ptr[-1] >> 1) & 1) ? '1' : '0',
521 ((ptr[-1] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000522
Lev Walkinfb35e082017-08-04 03:06:51 -0700523 DEBUG("oriht: %c%c%c%c%c%c%c%c",
524 ((ptr[0] >> 7) & 1) ? '1' : '0',
525 ((ptr[0] >> 6) & 1) ? '1' : '0',
526 ((ptr[0] >> 5) & 1) ? '1' : '0',
527 ((ptr[0] >> 4) & 1) ? '1' : '0',
528 ((ptr[0] >> 3) & 1) ? '1' : '0',
529 ((ptr[0] >> 2) & 1) ? '1' : '0',
530 ((ptr[0] >> 1) & 1) ? '1' : '0',
531 ((ptr[0] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000532
Lev Walkinfb35e082017-08-04 03:06:51 -0700533 DEBUG("mriht: %c%c%c%c%c%c%c%c",
534 ((right >> 7) & 1) ? '1' : '0',
535 ((right >> 6) & 1) ? '1' : '0',
536 ((right >> 5) & 1) ? '1' : '0',
537 ((right >> 4) & 1) ? '1' : '0',
538 ((right >> 3) & 1) ? '1' : '0',
539 ((right >> 2) & 1) ? '1' : '0',
540 ((right >> 1) & 1) ? '1' : '0',
541 ((right >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000542
Lev Walkinfb35e082017-08-04 03:06:51 -0700543 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
Lev Walkin5a621d62006-09-18 20:05:34 +0000544
Lev Walkinfb35e082017-08-04 03:06:51 -0700545 DEBUG("after: %c%c%c%c%c%c%c%c",
546 ((ptr[-1] >> 7) & 1) ? '1' : '0',
547 ((ptr[-1] >> 6) & 1) ? '1' : '0',
548 ((ptr[-1] >> 5) & 1) ? '1' : '0',
549 ((ptr[-1] >> 4) & 1) ? '1' : '0',
550 ((ptr[-1] >> 3) & 1) ? '1' : '0',
551 ((ptr[-1] >> 2) & 1) ? '1' : '0',
552 ((ptr[-1] >> 1) & 1) ? '1' : '0',
553 ((ptr[-1] >> 0) & 1) ? '1' : '0');
554 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000555
Lev Walkinfb35e082017-08-04 03:06:51 -0700556 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000557
Lev Walkinfb35e082017-08-04 03:06:51 -0700558 for(; ptr < end; ptr++) {
559 int right = ptr[1] >> (8 - bits);
560 *ptr = (*ptr << bits) | right;
561 }
562 *ptr <<= bits;
Lev Walkin5a621d62006-09-18 20:05:34 +0000563
Lev Walkinfb35e082017-08-04 03:06:51 -0700564 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
565 if(DynamicBuffer.unbits == 0) {
566 DynamicBuffer.unbits += bits;
567 } else {
568 DynamicBuffer.unbits += bits;
569 if(DynamicBuffer.unbits > 7) {
570 DynamicBuffer.unbits -= 8;
571 DynamicBuffer.length--;
572 DynamicBuffer.bytes_shifted++;
573 }
574 }
575 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000576
Lev Walkinfb35e082017-08-04 03:06:51 -0700577 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000578
Lev Walkinfb35e082017-08-04 03:06:51 -0700579 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
580 (long)DynamicBuffer.offset,
581 (long)DynamicBuffer.unbits,
582 (long)DynamicBuffer.length);
583
Lev Walkin5a621d62006-09-18 20:05:34 +0000584
585}
586
Lev Walkin22b54552004-10-28 13:22:54 +0000587/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000588 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000589 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000590static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000591
Lev Walkinfb35e082017-08-04 03:06:51 -0700592 if(bytes == 0) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000593
Lev Walkinfb35e082017-08-04 03:06:51 -0700594 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
595 (long)bytes,
596 (long)DynamicBuffer.offset,
597 (long)DynamicBuffer.length,
598 (long)DynamicBuffer.unbits,
599 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000600
Lev Walkinfb35e082017-08-04 03:06:51 -0700601 if(DynamicBuffer.allocated
602 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
603 DEBUG("\tNo buffer reallocation is necessary");
604 } else if(bytes <= DynamicBuffer.offset) {
605 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000606
Lev Walkinfb35e082017-08-04 03:06:51 -0700607 /* Shift the buffer contents */
608 memmove(DynamicBuffer.data,
609 DynamicBuffer.data + DynamicBuffer.offset,
610 DynamicBuffer.length);
611 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
612 DynamicBuffer.offset = 0;
613 } else {
614 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
615 void *p = MALLOC(newsize);
616 if(!p) {
617 perror("malloc()");
618 exit(EX_OSERR);
619 }
620 memcpy(p,
621 DynamicBuffer.data + DynamicBuffer.offset,
622 DynamicBuffer.length);
623 FREEMEM(DynamicBuffer.data);
624 DynamicBuffer.data = (uint8_t *)p;
625 DynamicBuffer.offset = 0;
626 DynamicBuffer.allocated = newsize;
627 DynamicBuffer.nreallocs++;
628 DEBUG("\tBuffer reallocated to %ld (%d time)",
629 newsize, DynamicBuffer.nreallocs);
630 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000631
Lev Walkinfb35e082017-08-04 03:06:51 -0700632 memcpy(DynamicBuffer.data
633 + DynamicBuffer.offset + DynamicBuffer.length,
634 data2add, bytes);
635 DynamicBuffer.length += bytes;
636 if(DynamicBuffer.unbits) {
637 int bits = DynamicBuffer.unbits;
638 DynamicBuffer.unbits = 0;
639 buffer_shift_left(DynamicBuffer.length - bytes, bits);
640 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000641
Lev Walkinfb35e082017-08-04 03:06:51 -0700642 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
643 (long)bytes,
644 (long)DynamicBuffer.offset,
645 (long)DynamicBuffer.length,
646 (long)DynamicBuffer.unbits,
647 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000648}
649
Lev Walkine8bbe932017-09-12 23:06:52 -0700650static int
651restartability_supported(enum asn_transfer_syntax syntax) {
652 return (syntax != ATS_UNALIGNED_BASIC_PER
653 && syntax != ATS_UNALIGNED_CANONICAL_PER);
654}
655
Lev Walkinc744a022006-09-15 18:33:25 +0000656static void *
Lev Walkin6d46bc32017-09-12 21:34:00 -0700657data_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 -0700658 static uint8_t *fbuf;
659 static ssize_t fbuf_size;
660 static asn_codec_ctx_t s_codec_ctx;
661 asn_codec_ctx_t *opt_codec_ctx = 0;
662 void *structure = 0;
663 asn_dec_rval_t rval;
664 size_t old_offset;
665 size_t new_offset;
666 int tolerate_eof;
667 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000668
Lev Walkinfb35e082017-08-04 03:06:51 -0700669 if(!file) {
670 fprintf(stderr, "%s: %s\n", name, strerror(errno));
671 errno = EINVAL;
672 return 0;
673 }
Lev Walkin22b54552004-10-28 13:22:54 +0000674
Lev Walkinfb35e082017-08-04 03:06:51 -0700675 if(opt_stack) {
676 s_codec_ctx.max_stack_size = opt_stack;
677 opt_codec_ctx = &s_codec_ctx;
678 }
Lev Walkin22b54552004-10-28 13:22:54 +0000679
Lev Walkinfb35e082017-08-04 03:06:51 -0700680 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000681
Lev Walkinfb35e082017-08-04 03:06:51 -0700682 /* prepare the file buffer */
683 if(fbuf_size != suggested_bufsize) {
684 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
685 if(!fbuf) {
686 perror("realloc()");
687 exit(EX_OSERR);
688 }
689 fbuf_size = suggested_bufsize;
690 }
Lev Walkin22b54552004-10-28 13:22:54 +0000691
Lev Walkinfb35e082017-08-04 03:06:51 -0700692 if(on_first_pdu) {
693 DynamicBuffer.offset = 0;
694 DynamicBuffer.length = 0;
695 DynamicBuffer.unbits = 0;
696 DynamicBuffer.allocated = 0;
697 DynamicBuffer.bytes_shifted = 0;
698 DynamicBuffer.nreallocs = 0;
699 }
Lev Walkinbc691772006-09-17 11:02:53 +0000700
Lev Walkinfb35e082017-08-04 03:06:51 -0700701 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000702
Lev Walkinfb35e082017-08-04 03:06:51 -0700703 /* Pretend immediate EOF */
704 rval.code = RC_WMORE;
705 rval.consumed = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000706
Lev Walkinfb35e082017-08-04 03:06:51 -0700707 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
708 (rd = fread(fbuf, 1, fbuf_size, file))
709 || feof(file) == 0
710 || (tolerate_eof && DynamicBuffer.length)
711 ;) {
712 int ecbits = 0; /* Extra consumed bits in case of PER */
713 uint8_t *i_bptr;
714 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000715
Lev Walkinfb35e082017-08-04 03:06:51 -0700716 /*
717 * Copy the data over, or use the original buffer.
718 */
719 if(DynamicBuffer.allocated) {
720 /* Append new data into the existing dynamic buffer */
721 add_bytes_to_buffer(fbuf, rd);
722 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
723 i_size = DynamicBuffer.length;
724 } else {
725 i_bptr = fbuf;
726 i_size = rd;
727 }
Lev Walkin22b54552004-10-28 13:22:54 +0000728
Lev Walkinfb35e082017-08-04 03:06:51 -0700729 DEBUG("Decoding %ld bytes", (long)i_size);
Lev Walkinbc691772006-09-17 11:02:53 +0000730
Lev Walkinfb35e082017-08-04 03:06:51 -0700731#ifdef JUNKTEST
732 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
Lev Walkin1f12da42006-09-24 19:47:07 +0000733#endif
734
Lev Walkine8bbe932017-09-12 23:06:52 -0700735 rval = asn_decode(opt_codec_ctx, isyntax, pduType, (void **)&structure,
736 i_bptr, i_size);
737 if(rval.code == RC_WMORE && !restartability_supported(isyntax)) {
738 /* PER does not support restartability */
739 ASN_STRUCT_FREE(*pduType, structure);
740 structure = 0;
Lev Walkin69033802017-08-25 12:15:58 -0700741 rval.consumed = 0;
Lev Walkine8bbe932017-09-12 23:06:52 -0700742 /* Continue accumulating data */
Lev Walkinfb35e082017-08-04 03:06:51 -0700743 }
Lev Walkine8bbe932017-09-12 23:06:52 -0700744
Lev Walkinfb35e082017-08-04 03:06:51 -0700745 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
746 (long)DynamicBuffer.length,
747 (long)rval.consumed, ecbits, (long)i_size,
748 rval.code);
Lev Walkin22b54552004-10-28 13:22:54 +0000749
Lev Walkinfb35e082017-08-04 03:06:51 -0700750 if(DynamicBuffer.allocated == 0) {
751 /*
752 * Flush remainder into the intermediate buffer.
753 */
754 if(rval.code != RC_FAIL && rval.consumed < rd) {
755 add_bytes_to_buffer(fbuf + rval.consumed,
756 rd - rval.consumed);
757 buffer_shift_left(0, ecbits);
758 DynamicBuffer.bytes_shifted = rval.consumed;
759 rval.consumed = 0;
760 ecbits = 0;
761 }
762 }
Lev Walkin22b54552004-10-28 13:22:54 +0000763
Lev Walkinfb35e082017-08-04 03:06:51 -0700764 /*
765 * Adjust position inside the source buffer.
766 */
767 if(DynamicBuffer.allocated) {
768 DynamicBuffer.offset += rval.consumed;
769 DynamicBuffer.length -= rval.consumed;
770 } else {
771 DynamicBuffer.bytes_shifted += rval.consumed;
772 }
Lev Walkinbc691772006-09-17 11:02:53 +0000773
Lev Walkinfb35e082017-08-04 03:06:51 -0700774 switch(rval.code) {
775 case RC_OK:
776 if(ecbits) buffer_shift_left(0, ecbits);
777 DEBUG("RC_OK, finishing up with %ld+%d",
778 (long)rval.consumed, ecbits);
779 return structure;
780 case RC_WMORE:
781 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
782 " with %ld..%ld-%ld..%ld",
783 (long)rd,
784 (long)rval.consumed,
785 (long)DynamicBuffer.offset,
786 (long)DynamicBuffer.length,
787 (long)DynamicBuffer.unbits,
788 (long)DynamicBuffer.allocated);
789 if(!rd) tolerate_eof--;
790 continue;
791 case RC_FAIL:
792 break;
793 }
794 break;
795 }
Lev Walkin22b54552004-10-28 13:22:54 +0000796
Lev Walkinb48bc2f2017-09-12 22:39:47 -0700797 DEBUG("Clean up partially decoded %s", pduType->name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700798 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkin22b54552004-10-28 13:22:54 +0000799
Lev Walkinfb35e082017-08-04 03:06:51 -0700800 new_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkinbc691772006-09-17 11:02:53 +0000801
Lev Walkinfb35e082017-08-04 03:06:51 -0700802 /*
803 * Print a message and return failure only if not EOF,
804 * unless this is our first PDU (empty file).
805 */
806 if(on_first_pdu
807 || DynamicBuffer.length
Lev Walkin6d46bc32017-09-12 21:34:00 -0700808 || new_offset - old_offset > ((isyntax == ATS_BASIC_XER)?sizeof("\r\n")-1:0)
Lev Walkinfb35e082017-08-04 03:06:51 -0700809 ) {
Lev Walkin1f12da42006-09-24 19:47:07 +0000810
Lev Walkinfb35e082017-08-04 03:06:51 -0700811#ifdef JUNKTEST
812 /*
813 * Nothing's wrong with being unable to decode junk.
814 * Simulate EOF.
815 */
816 if(opt_jprob != 0.0) {
817 junk_failures++;
818 errno = 0;
819 return 0;
820 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000821#endif
822
Lev Walkinfb35e082017-08-04 03:06:51 -0700823 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
824 on_first_pdu, (long)new_offset, (long)old_offset,
825 (long)DynamicBuffer.length);
826 fprintf(stderr, "%s: "
827 "Decode failed past byte %ld: %s\n",
828 name, (long)new_offset,
829 (rval.code == RC_WMORE)
830 ? "Unexpected end of input"
831 : "Input processing error");
832#ifndef ENOMSG
833#define ENOMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000834#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700835#ifndef EBADMSG
836#define EBADMSG EINVAL
Lev Walkin4bf96b92006-09-18 21:46:50 +0000837#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700838 errno = (rval.code == RC_WMORE) ? ENOMSG : EBADMSG;
839 } else {
840 /* Got EOF after a few successful PDUs */
841 errno = 0;
842 }
Lev Walkin22b54552004-10-28 13:22:54 +0000843
Lev Walkinfb35e082017-08-04 03:06:51 -0700844 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000845}
846
Lev Walkin419f6752006-09-13 04:02:00 +0000847/* Dump the buffer out to the specified FILE */
848static int write_out(const void *buffer, size_t size, void *key) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700849 FILE *fp = (FILE *)key;
850 return (fwrite(buffer, 1, size, fp) == size) ? 0 : -1;
Lev Walkin419f6752006-09-13 04:02:00 +0000851}
Lev Walkinc744a022006-09-15 18:33:25 +0000852
853static int argument_is_stdin(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700854 if(strcmp(av[idx], "-")) {
855 return 0; /* Certainly not <stdin> */
856 } else {
857 /* This might be <stdin>, unless `./program -- -` */
858 if(strcmp(av[-1], "--"))
859 return 1;
860 else
861 return 0;
862 }
Lev Walkinc744a022006-09-15 18:33:25 +0000863}
864
865static FILE *argument_to_file(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700866 return argument_is_stdin(av, idx) ? stdin : fopen(av[idx], "rb");
Lev Walkinc744a022006-09-15 18:33:25 +0000867}
868
869static char *argument_to_name(char *av[], int idx) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700870 return argument_is_stdin(av, idx) ? "standard input" : av[idx];
Lev Walkinc744a022006-09-15 18:33:25 +0000871}
Lev Walkin1f12da42006-09-24 19:47:07 +0000872
Lev Walkinfb35e082017-08-04 03:06:51 -0700873#ifdef JUNKTEST
Lev Walkin1f12da42006-09-24 19:47:07 +0000874/*
875 * Fill bytes with some garbage with specified probability (more or less).
876 */
877static void
878junk_bytes_with_probability(uint8_t *buf, size_t size, double prob) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700879 static int junkmode;
880 uint8_t *ptr;
881 uint8_t *end;
882 if(opt_jprob <= 0.0) return;
883 for(ptr = buf, end = ptr + size; ptr < end; ptr++) {
884 int byte = *ptr;
885 if(junkmode++ & 1) {
886 if((((double)random() / RAND_MAX) < prob))
887 byte = random() & 0xff;
888 } else {
889#define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
890 byte ^= BPROB(0x80);
891 byte ^= BPROB(0x40);
892 byte ^= BPROB(0x20);
893 byte ^= BPROB(0x10);
894 byte ^= BPROB(0x08);
895 byte ^= BPROB(0x04);
896 byte ^= BPROB(0x02);
897 byte ^= BPROB(0x01);
898 }
899 if(byte != *ptr) {
900 DEBUG("Junk buf[%d] %02x -> %02x", ptr - buf, *ptr, byte);
901 *ptr = byte;
902 }
903 }
Lev Walkin1f12da42006-09-24 19:47:07 +0000904}
Lev Walkinfb35e082017-08-04 03:06:51 -0700905#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000906