blob: bbf276cfeb0ffacf93b009462fe9d4f61f4699e4 [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) */
57static int opt_nopad; /* -per-nopad (PER input is not padded) */
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 Walkin22b54552004-10-28 13:22:54 +0000108int
Lev Walkinc744a022006-09-15 18:33:25 +0000109main(int ac, char *av[]) {
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700110 FILE *binary_out;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700111 static asn_TYPE_descriptor_t *pduType = PDU_Type_Ptr;
Lev Walkinfb35e082017-08-04 03:06:51 -0700112 ssize_t suggested_bufsize = 8192; /* close or equal to stdio buffer */
113 int number_of_iterations = 1;
114 int num;
115 int ch;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700116 enum asn_transfer_syntax isyntax = ATS_INVALID;
117 enum asn_transfer_syntax osyntax = ATS_INVALID;
118
119#ifndef PDU
120 if(!pduType) {
121 fprintf(stderr, "No -DPDU defined during compilation.\n");
122#ifdef NO_ASN_PDU
123 exit(0);
124#else
125 exit(EX_SOFTWARE);
126#endif
127 }
128#endif
Lev Walkin22b54552004-10-28 13:22:54 +0000129
Lev Walkinfb35e082017-08-04 03:06:51 -0700130 /* Figure out if specialty decoder needs to be default */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800131 if(pduType->op->oer_decoder)
Lev Walkin6d46bc32017-09-12 21:34:00 -0700132 isyntax = ATS_BASIC_OER;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800133 else if(pduType->op->uper_decoder)
Lev Walkin6d46bc32017-09-12 21:34:00 -0700134 isyntax = ATS_UNALIGNED_BASIC_PER;
Lev Walkin59b176e2005-11-26 11:25:14 +0000135
Lev Walkinfb35e082017-08-04 03:06:51 -0700136 /*
137 * Pocess the command-line argments.
138 */
139 while((ch = getopt(ac, av, "i:o:1b:cdn:p:hs:" JUNKOPT)) != -1)
140 switch(ch) {
141 case 'i':
Lev Walkin6d46bc32017-09-12 21:34:00 -0700142 if(optarg[0] == 'b') { isyntax = ATS_BER; break; }
143 if(optarg[0] == 'x') { isyntax = ATS_BASIC_XER; break; }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800144 if(pduType->op->oer_decoder
Lev Walkin6d46bc32017-09-12 21:34:00 -0700145 && optarg[0] == 'o') { isyntax = ATS_BASIC_OER; break; }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800146 if(pduType->op->uper_decoder
Lev Walkin6d46bc32017-09-12 21:34:00 -0700147 && optarg[0] == 'p') { isyntax = ATS_UNALIGNED_BASIC_PER; break; }
Lev Walkinfb35e082017-08-04 03:06:51 -0700148 fprintf(stderr, "-i<format>: '%s': improper format selector\n",
149 optarg);
150 exit(EX_UNAVAILABLE);
151 case 'o':
Lev Walkin6d46bc32017-09-12 21:34:00 -0700152 if(optarg[0] == 'd') { osyntax = ATS_DER; break; }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800153 if(pduType->op->oer_encoder
Lev Walkin6d46bc32017-09-12 21:34:00 -0700154 && optarg[0] == 'o') { osyntax = ATS_CANONICAL_OER; break; }
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800155 if(pduType->op->uper_encoder
Lev Walkin6d46bc32017-09-12 21:34:00 -0700156 && optarg[0] == 'p') { osyntax = ATS_UNALIGNED_CANONICAL_PER; break; }
157 if(optarg[0] == 'x') { osyntax = ATS_BASIC_XER; break; }
158 if(optarg[0] == 't') { osyntax = ATS_NONSTANDARD_PLAINTEXT; break; }
159 if(optarg[0] == 'n') { osyntax = ATS_INVALID; break; }
Lev Walkinfb35e082017-08-04 03:06:51 -0700160 fprintf(stderr, "-o<format>: '%s': improper format selector\n",
161 optarg);
162 exit(EX_UNAVAILABLE);
163 case '1':
164 opt_onepdu = 1;
165 break;
166 case 'b':
167 suggested_bufsize = atoi(optarg);
168 if(suggested_bufsize < 1
169 || suggested_bufsize > 16 * 1024 * 1024) {
170 fprintf(stderr,
171 "-b %s: Improper buffer size (1..16M)\n",
172 optarg);
173 exit(EX_UNAVAILABLE);
174 }
175 break;
176 case 'c':
177 opt_check = 1;
178 break;
179 case 'd':
180 opt_debug++; /* Double -dd means ASN.1 debug */
181 break;
182 case 'n':
183 number_of_iterations = atoi(optarg);
184 if(number_of_iterations < 1) {
185 fprintf(stderr,
186 "-n %s: Improper iterations count\n", optarg);
187 exit(EX_UNAVAILABLE);
188 }
189 break;
190 case 'p':
191 if(strcmp(optarg, "er-nopad") == 0) {
192 opt_nopad = 1;
193 break;
194 }
195#ifdef ASN_PDU_COLLECTION
196 if(strcmp(optarg, "list") == 0) {
197 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
198 fprintf(stderr, "Available PDU types:\n");
199 for(; *pdu; pdu++) printf("%s\n", (*pdu)->name);
200 exit(0);
201 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
202 asn_TYPE_descriptor_t **pdu = asn_pdu_collection;
203 while(*pdu && strcmp((*pdu)->name, optarg)) pdu++;
204 if(*pdu) { pduType = *pdu; break; }
205 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
Lev Walkin7e5ea1d2017-08-06 00:59:28 -0700206 exit(EX_USAGE);
207 }
208#else /* Without -pdu=auto there's just a single type */
209 if(strcmp(optarg, "list") == 0) {
210 fprintf(stderr, "Available PDU types:\n");
211 printf("%s\n", pduType->name);
212 exit(0);
213 } else if(optarg[0] >= 'A' && optarg[0] <= 'Z') {
214 if(strcmp(optarg, pduType->name) == 0) {
215 break;
216 }
217 fprintf(stderr, "-p %s: Unrecognized PDU\n", optarg);
218 exit(EX_USAGE);
Lev Walkinfb35e082017-08-04 03:06:51 -0700219 }
220#endif /* ASN_PDU_COLLECTION */
221 fprintf(stderr, "-p %s: Unrecognized option\n", optarg);
222 exit(EX_UNAVAILABLE);
223 case 's':
224 opt_stack = atoi(optarg);
225 if(opt_stack < 0) {
226 fprintf(stderr,
227 "-s %s: Non-negative value expected\n",
228 optarg);
229 exit(EX_UNAVAILABLE);
230 }
231 break;
232#ifdef JUNKTEST
233 case 'J':
234 opt_jprob = strtod(optarg, 0);
235 if(opt_jprob <= 0.0 || opt_jprob > 1.0) {
236 fprintf(stderr,
237 "-J %s: Probability range 0..1 expected \n",
238 optarg);
239 exit(EX_UNAVAILABLE);
240 }
241 break;
242#endif /* JUNKTEST */
243 case 'h':
244 default:
245#ifdef ASN_CONVERTER_TITLE
246#define _AXS(x) #x
247#define _ASX(x) _AXS(x)
248 fprintf(stderr, "%s\n", _ASX(ASN_CONVERTER_TITLE));
Lev Walkin8a4a06c2007-06-29 12:44:01 +0000249#endif
Lev Walkinae0c3c22017-08-26 17:20:32 -0700250 fprintf(stderr, "Usage: %s [options] <datafile> ...\n", av[0]);
Lev Walkinfb35e082017-08-04 03:06:51 -0700251 fprintf(stderr, "Where options are:\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800252 if(pduType->op->oer_decoder)
Lev Walkinfb35e082017-08-04 03:06:51 -0700253 fprintf(stderr,
254 " -ioer Input is in OER (Octet Encoding Rules)%s\n",
Lev Walkin6d46bc32017-09-12 21:34:00 -0700255 isyntax == ATS_BASIC_OER ? " (DEFAULT)" : "");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800256 if(pduType->op->uper_decoder)
Lev Walkinfb35e082017-08-04 03:06:51 -0700257 fprintf(stderr,
258 " -iper Input is in Unaligned PER (Packed Encoding Rules)%s\n",
Lev Walkin6d46bc32017-09-12 21:34:00 -0700259 isyntax == ATS_UNALIGNED_BASIC_PER ? " (DEFAULT)" : "");
Lev Walkinfb35e082017-08-04 03:06:51 -0700260 fprintf(stderr,
261 " -iber Input is in BER (Basic Encoding Rules)%s\n",
Lev Walkin6d46bc32017-09-12 21:34:00 -0700262 isyntax == ATS_BER ? " (DEFAULT)" : "");
Lev Walkinfb35e082017-08-04 03:06:51 -0700263 fprintf(stderr,
264 " -ixer Input is in XER (XML Encoding Rules)\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800265 if(pduType->op->oer_encoder)
Lev Walkinfb35e082017-08-04 03:06:51 -0700266 fprintf(stderr,
267 " -ooer Output in Canonical OER (Octet Encoding Rules)\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800268 if(pduType->op->uper_encoder)
Lev Walkinfb35e082017-08-04 03:06:51 -0700269 fprintf(stderr,
270 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
271 fprintf(stderr,
272 " -oder Output in DER (Distinguished Encoding Rules)\n"
273 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
274 " -otext Output in plain semi-structured text (dump)\n"
275 " -onull Verify (decode) input, but do not output\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800276 if(pduType->op->uper_decoder)
Lev Walkinfb35e082017-08-04 03:06:51 -0700277 fprintf(stderr,
278 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
279#ifdef ASN_PDU_COLLECTION
280 fprintf(stderr,
281 " -p <PDU> Specify PDU type to decode\n"
282 " -p list List available PDUs\n");
283#endif /* ASN_PDU_COLLECTION */
284 fprintf(stderr,
285 " -1 Decode only the first PDU in file\n"
286 " -b <size> Set the i/o buffer size (default is %ld)\n"
287 " -c Check ASN.1 constraints after decoding\n"
288 " -d Enable debugging (-dd is even better)\n"
289 " -n <num> Process files <num> times\n"
290 " -s <size> Set the stack usage limit (default is %d)\n"
291#ifdef JUNKTEST
292 " -J <prob> Set random junk test bit garbaging probability\n"
Lev Walkin1f12da42006-09-24 19:47:07 +0000293#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700294 , (long)suggested_bufsize, ASN__DEFAULT_STACK_MAX);
295 exit(EX_USAGE);
296 }
Lev Walkin22b54552004-10-28 13:22:54 +0000297
Lev Walkinfb35e082017-08-04 03:06:51 -0700298 ac -= optind;
299 av += optind;
Lev Walkin22b54552004-10-28 13:22:54 +0000300
Lev Walkinfb35e082017-08-04 03:06:51 -0700301 if(ac < 1) {
302 fprintf(stderr, "%s: No input files specified. "
303 "Try '-h' for more information\n",
304 av[-optind]);
305 exit(EX_USAGE);
306 }
Lev Walkin22b54552004-10-28 13:22:54 +0000307
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700308 if(isatty(1)) {
Lev Walkin6d46bc32017-09-12 21:34:00 -0700309 const int is_text_output = osyntax == ATS_NONSTANDARD_PLAINTEXT || osyntax == ATS_CANONICAL_XER || osyntax == ATS_BASIC_XER;
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700310 if(is_text_output) {
311 binary_out = stdout;
312 } else {
313 fprintf(stderr, "(Suppressing binary output to a terminal.)\n");
314 binary_out = fopen("/dev/null", "wb");
315 if(!binary_out) {
316 fprintf(stderr, "Can't open /dev/null: %s\n", strerror(errno));
317 exit(EX_OSERR);
318 }
319 }
320 } else {
321 binary_out = stdout;
322 }
Lev Walkinfb35e082017-08-04 03:06:51 -0700323 setvbuf(stdout, 0, _IOLBF, 0);
Lev Walkin22b54552004-10-28 13:22:54 +0000324
Lev Walkinfb35e082017-08-04 03:06:51 -0700325 for(num = 0; num < number_of_iterations; num++) {
326 int ac_i;
327 /*
328 * Process all files in turn.
329 */
330 for(ac_i = 0; ac_i < ac; ac_i++) {
331 asn_enc_rval_t erv;
332 void *structure; /* Decoded structure */
333 FILE *file = argument_to_file(av, ac_i);
334 char *name = argument_to_name(av, ac_i);
335 int first_pdu;
Lev Walkin22b54552004-10-28 13:22:54 +0000336
Lev Walkin6d46bc32017-09-12 21:34:00 -0700337 for(first_pdu = 1; file && (first_pdu || !opt_onepdu); first_pdu = 0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700338 /*
339 * Decode the encoded structure from file.
340 */
Lev Walkin6d46bc32017-09-12 21:34:00 -0700341 structure = data_decode_from_file(isyntax, pduType, file, name,
Lev Walkinfb35e082017-08-04 03:06:51 -0700342 suggested_bufsize, first_pdu);
343 if(!structure) {
344 if(errno) {
345 /* Error message is already printed */
346 exit(EX_DATAERR);
347 } else {
348 /* EOF */
349 break;
350 }
351 }
Lev Walkin22b54552004-10-28 13:22:54 +0000352
Lev Walkinfb35e082017-08-04 03:06:51 -0700353 /* Check ASN.1 constraints */
354 if(opt_check) {
355 char errbuf[128];
356 size_t errlen = sizeof(errbuf);
357 if(asn_check_constraints(pduType, structure, errbuf, &errlen)) {
358 fprintf(stderr,
359 "%s: ASN.1 constraint "
360 "check failed: %s\n",
361 name, errbuf);
362 exit(EX_DATAERR);
363 }
364 }
Lev Walkin22b54552004-10-28 13:22:54 +0000365
Lev Walkin6d46bc32017-09-12 21:34:00 -0700366 if(osyntax == ATS_INVALID) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700367#ifdef JUNKTEST
Lev Walkin6d46bc32017-09-12 21:34:00 -0700368 if(opt_jprob == 0.0) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700369 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkinfb35e082017-08-04 03:06:51 -0700370 }
Lev Walkin69033802017-08-25 12:15:58 -0700371#else
Lev Walkin6d46bc32017-09-12 21:34:00 -0700372 fprintf(stderr, "%s: decoded successfully\n", name);
Lev Walkin69033802017-08-25 12:15:58 -0700373#endif
Lev Walkin6d46bc32017-09-12 21:34:00 -0700374 } else {
375 erv = asn_encode(NULL, osyntax, pduType, structure, write_out, binary_out);
376
377 if(erv.encoded == -1) {
378 fprintf(stderr, "%s: Cannot convert %s into %s\n", name,
379 pduType->name, ats_simple_name(osyntax));
Lev Walkinfb35e082017-08-04 03:06:51 -0700380 exit(EX_UNAVAILABLE);
381 }
Lev Walkin6d46bc32017-09-12 21:34:00 -0700382 DEBUG("Encoded in %zd bytes of %s", erv.encoded,
383 ats_simple_name(osyntax));
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700384 }
Lev Walkind1bfea62005-11-08 03:06:16 +0000385
Lev Walkin7bb9d5b2017-08-26 23:31:58 -0700386 ASN_STRUCT_FREE(*pduType, structure);
Lev Walkinfb35e082017-08-04 03:06:51 -0700387 }
Lev Walkinbc691772006-09-17 11:02:53 +0000388
Lev Walkinfb35e082017-08-04 03:06:51 -0700389 if(file && file != stdin) {
390 fclose(file);
391 }
392 }
393 }
Lev Walkin22b54552004-10-28 13:22:54 +0000394
Lev Walkinfb35e082017-08-04 03:06:51 -0700395#ifdef JUNKTEST
396 if(opt_jprob > 0.0) {
397 fprintf(stderr, "Junked %f OK (%d/%d)\n",
398 opt_jprob, junk_failures, number_of_iterations);
399 }
400#endif /* JUNKTEST */
Lev Walkin1f12da42006-09-24 19:47:07 +0000401
Lev Walkinfb35e082017-08-04 03:06:51 -0700402 return 0;
Lev Walkin22b54552004-10-28 13:22:54 +0000403}
404
Lev Walkin419f6752006-09-13 04:02:00 +0000405static struct dynamic_buffer {
Lev Walkinfb35e082017-08-04 03:06:51 -0700406 uint8_t *data; /* Pointer to the data bytes */
407 size_t offset; /* Offset from the start */
408 size_t length; /* Length of meaningful contents */
409 size_t unbits; /* Unused bits in the last byte */
410 size_t allocated; /* Allocated memory for data */
411 int nreallocs; /* Number of data reallocations */
412 off_t bytes_shifted; /* Number of bytes ever shifted */
Lev Walkin419f6752006-09-13 04:02:00 +0000413} DynamicBuffer;
Lev Walkin22b54552004-10-28 13:22:54 +0000414
Lev Walkin5a621d62006-09-18 20:05:34 +0000415static void
416buffer_dump() {
Lev Walkinfb35e082017-08-04 03:06:51 -0700417 uint8_t *p = DynamicBuffer.data + DynamicBuffer.offset;
418 uint8_t *e = p + DynamicBuffer.length - (DynamicBuffer.unbits ? 1 : 0);
419 if(!opt_debug) return;
420 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
421 DynamicBuffer.data,
422 (long)DynamicBuffer.offset,
423 (long)DynamicBuffer.length,
424 (long)DynamicBuffer.unbits,
425 (long)DynamicBuffer.allocated,
426 (long)DynamicBuffer.bytes_shifted);
427 for(; p < e; p++) {
428 fprintf(stderr, " %c%c%c%c%c%c%c%c",
429 ((*p >> 7) & 1) ? '1' : '0',
430 ((*p >> 6) & 1) ? '1' : '0',
431 ((*p >> 5) & 1) ? '1' : '0',
432 ((*p >> 4) & 1) ? '1' : '0',
433 ((*p >> 3) & 1) ? '1' : '0',
434 ((*p >> 2) & 1) ? '1' : '0',
435 ((*p >> 1) & 1) ? '1' : '0',
436 ((*p >> 0) & 1) ? '1' : '0');
437 }
438 if(DynamicBuffer.unbits) {
439 unsigned int shift;
440 fprintf(stderr, " ");
441 for(shift = 7; shift >= DynamicBuffer.unbits; shift--)
442 fprintf(stderr, "%c", ((*p >> shift) & 1) ? '1' : '0');
443 fprintf(stderr, " %ld:%ld\n",
444 (long)DynamicBuffer.length - 1,
445 (long)8 - DynamicBuffer.unbits);
446 } else {
447 fprintf(stderr, " %ld\n", (long)DynamicBuffer.length);
448 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000449}
450
451/*
452 * Move the buffer content left N bits, possibly joining it with
453 * preceeding content.
454 */
455static void
456buffer_shift_left(size_t offset, int bits) {
Lev Walkinfb35e082017-08-04 03:06:51 -0700457 uint8_t *ptr = DynamicBuffer.data + DynamicBuffer.offset + offset;
458 uint8_t *end = DynamicBuffer.data + DynamicBuffer.offset
459 + DynamicBuffer.length - 1;
460
461 if(!bits) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000462
Lev Walkinfb35e082017-08-04 03:06:51 -0700463 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
464 bits, (long)offset,
465 (long)DynamicBuffer.offset,
466 (long)DynamicBuffer.unbits,
467 (long)DynamicBuffer.length);
Lev Walkin5a621d62006-09-18 20:05:34 +0000468
Lev Walkinfb35e082017-08-04 03:06:51 -0700469 if(offset) {
470 int right;
471 right = ptr[0] >> (8 - bits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000472
Lev Walkinfb35e082017-08-04 03:06:51 -0700473 DEBUG("oleft: %c%c%c%c%c%c%c%c",
474 ((ptr[-1] >> 7) & 1) ? '1' : '0',
475 ((ptr[-1] >> 6) & 1) ? '1' : '0',
476 ((ptr[-1] >> 5) & 1) ? '1' : '0',
477 ((ptr[-1] >> 4) & 1) ? '1' : '0',
478 ((ptr[-1] >> 3) & 1) ? '1' : '0',
479 ((ptr[-1] >> 2) & 1) ? '1' : '0',
480 ((ptr[-1] >> 1) & 1) ? '1' : '0',
481 ((ptr[-1] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000482
Lev Walkinfb35e082017-08-04 03:06:51 -0700483 DEBUG("oriht: %c%c%c%c%c%c%c%c",
484 ((ptr[0] >> 7) & 1) ? '1' : '0',
485 ((ptr[0] >> 6) & 1) ? '1' : '0',
486 ((ptr[0] >> 5) & 1) ? '1' : '0',
487 ((ptr[0] >> 4) & 1) ? '1' : '0',
488 ((ptr[0] >> 3) & 1) ? '1' : '0',
489 ((ptr[0] >> 2) & 1) ? '1' : '0',
490 ((ptr[0] >> 1) & 1) ? '1' : '0',
491 ((ptr[0] >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000492
Lev Walkinfb35e082017-08-04 03:06:51 -0700493 DEBUG("mriht: %c%c%c%c%c%c%c%c",
494 ((right >> 7) & 1) ? '1' : '0',
495 ((right >> 6) & 1) ? '1' : '0',
496 ((right >> 5) & 1) ? '1' : '0',
497 ((right >> 4) & 1) ? '1' : '0',
498 ((right >> 3) & 1) ? '1' : '0',
499 ((right >> 2) & 1) ? '1' : '0',
500 ((right >> 1) & 1) ? '1' : '0',
501 ((right >> 0) & 1) ? '1' : '0');
Lev Walkin5a621d62006-09-18 20:05:34 +0000502
Lev Walkinfb35e082017-08-04 03:06:51 -0700503 ptr[-1] = (ptr[-1] & (0xff << bits)) | right;
Lev Walkin5a621d62006-09-18 20:05:34 +0000504
Lev Walkinfb35e082017-08-04 03:06:51 -0700505 DEBUG("after: %c%c%c%c%c%c%c%c",
506 ((ptr[-1] >> 7) & 1) ? '1' : '0',
507 ((ptr[-1] >> 6) & 1) ? '1' : '0',
508 ((ptr[-1] >> 5) & 1) ? '1' : '0',
509 ((ptr[-1] >> 4) & 1) ? '1' : '0',
510 ((ptr[-1] >> 3) & 1) ? '1' : '0',
511 ((ptr[-1] >> 2) & 1) ? '1' : '0',
512 ((ptr[-1] >> 1) & 1) ? '1' : '0',
513 ((ptr[-1] >> 0) & 1) ? '1' : '0');
514 }
Lev Walkin5a621d62006-09-18 20:05:34 +0000515
Lev Walkinfb35e082017-08-04 03:06:51 -0700516 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000517
Lev Walkinfb35e082017-08-04 03:06:51 -0700518 for(; ptr < end; ptr++) {
519 int right = ptr[1] >> (8 - bits);
520 *ptr = (*ptr << bits) | right;
521 }
522 *ptr <<= bits;
Lev Walkin5a621d62006-09-18 20:05:34 +0000523
Lev Walkinfb35e082017-08-04 03:06:51 -0700524 DEBUG("Unbits [%d=>", (int)DynamicBuffer.unbits);
525 if(DynamicBuffer.unbits == 0) {
526 DynamicBuffer.unbits += bits;
527 } else {
528 DynamicBuffer.unbits += bits;
529 if(DynamicBuffer.unbits > 7) {
530 DynamicBuffer.unbits -= 8;
531 DynamicBuffer.length--;
532 DynamicBuffer.bytes_shifted++;
533 }
534 }
535 DEBUG("Unbits =>%d]", (int)DynamicBuffer.unbits);
Lev Walkin5a621d62006-09-18 20:05:34 +0000536
Lev Walkinfb35e082017-08-04 03:06:51 -0700537 buffer_dump();
Lev Walkin5a621d62006-09-18 20:05:34 +0000538
Lev Walkinfb35e082017-08-04 03:06:51 -0700539 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
540 (long)DynamicBuffer.offset,
541 (long)DynamicBuffer.unbits,
542 (long)DynamicBuffer.length);
543
Lev Walkin5a621d62006-09-18 20:05:34 +0000544
545}
546
Lev Walkin22b54552004-10-28 13:22:54 +0000547/*
Lev Walkind1bfea62005-11-08 03:06:16 +0000548 * Ensure that the buffer contains at least this amount of free space.
Lev Walkin22b54552004-10-28 13:22:54 +0000549 */
Lev Walkin5a621d62006-09-18 20:05:34 +0000550static void add_bytes_to_buffer(const void *data2add, size_t bytes) {
Lev Walkin22b54552004-10-28 13:22:54 +0000551
Lev Walkinfb35e082017-08-04 03:06:51 -0700552 if(bytes == 0) return;
Lev Walkin5a621d62006-09-18 20:05:34 +0000553
Lev Walkinfb35e082017-08-04 03:06:51 -0700554 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
555 (long)bytes,
556 (long)DynamicBuffer.offset,
557 (long)DynamicBuffer.length,
558 (long)DynamicBuffer.unbits,
559 (long)DynamicBuffer.allocated);
Lev Walkin22b54552004-10-28 13:22:54 +0000560
Lev Walkinfb35e082017-08-04 03:06:51 -0700561 if(DynamicBuffer.allocated
562 >= (DynamicBuffer.offset + DynamicBuffer.length + bytes)) {
563 DEBUG("\tNo buffer reallocation is necessary");
564 } else if(bytes <= DynamicBuffer.offset) {
565 DEBUG("\tContents shifted by %ld", DynamicBuffer.offset);
Lev Walkin22b54552004-10-28 13:22:54 +0000566
Lev Walkinfb35e082017-08-04 03:06:51 -0700567 /* Shift the buffer contents */
568 memmove(DynamicBuffer.data,
569 DynamicBuffer.data + DynamicBuffer.offset,
570 DynamicBuffer.length);
571 DynamicBuffer.bytes_shifted += DynamicBuffer.offset;
572 DynamicBuffer.offset = 0;
573 } else {
574 size_t newsize = (DynamicBuffer.allocated << 2) + bytes;
575 void *p = MALLOC(newsize);
576 if(!p) {
577 perror("malloc()");
578 exit(EX_OSERR);
579 }
580 memcpy(p,
581 DynamicBuffer.data + DynamicBuffer.offset,
582 DynamicBuffer.length);
583 FREEMEM(DynamicBuffer.data);
584 DynamicBuffer.data = (uint8_t *)p;
585 DynamicBuffer.offset = 0;
586 DynamicBuffer.allocated = newsize;
587 DynamicBuffer.nreallocs++;
588 DEBUG("\tBuffer reallocated to %ld (%d time)",
589 newsize, DynamicBuffer.nreallocs);
590 }
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000591
Lev Walkinfb35e082017-08-04 03:06:51 -0700592 memcpy(DynamicBuffer.data
593 + DynamicBuffer.offset + DynamicBuffer.length,
594 data2add, bytes);
595 DynamicBuffer.length += bytes;
596 if(DynamicBuffer.unbits) {
597 int bits = DynamicBuffer.unbits;
598 DynamicBuffer.unbits = 0;
599 buffer_shift_left(DynamicBuffer.length - bytes, bits);
600 }
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}
609
Lev Walkinc744a022006-09-15 18:33:25 +0000610static void *
Lev Walkin6d46bc32017-09-12 21:34:00 -0700611data_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 -0700612 static uint8_t *fbuf;
613 static ssize_t fbuf_size;
614 static asn_codec_ctx_t s_codec_ctx;
615 asn_codec_ctx_t *opt_codec_ctx = 0;
616 void *structure = 0;
617 asn_dec_rval_t rval;
618 size_t old_offset;
619 size_t new_offset;
620 int tolerate_eof;
621 size_t rd;
Lev Walkinc744a022006-09-15 18:33:25 +0000622
Lev Walkinfb35e082017-08-04 03:06:51 -0700623 if(!file) {
624 fprintf(stderr, "%s: %s\n", name, strerror(errno));
625 errno = EINVAL;
626 return 0;
627 }
Lev Walkin22b54552004-10-28 13:22:54 +0000628
Lev Walkinfb35e082017-08-04 03:06:51 -0700629 if(opt_stack) {
630 s_codec_ctx.max_stack_size = opt_stack;
631 opt_codec_ctx = &s_codec_ctx;
632 }
Lev Walkin22b54552004-10-28 13:22:54 +0000633
Lev Walkinfb35e082017-08-04 03:06:51 -0700634 DEBUG("Processing %s", name);
Lev Walkin22b54552004-10-28 13:22:54 +0000635
Lev Walkinfb35e082017-08-04 03:06:51 -0700636 /* prepare the file buffer */
637 if(fbuf_size != suggested_bufsize) {
638 fbuf = (uint8_t *)REALLOC(fbuf, suggested_bufsize);
639 if(!fbuf) {
640 perror("realloc()");
641 exit(EX_OSERR);
642 }
643 fbuf_size = suggested_bufsize;
644 }
Lev Walkin22b54552004-10-28 13:22:54 +0000645
Lev Walkinfb35e082017-08-04 03:06:51 -0700646 if(on_first_pdu) {
647 DynamicBuffer.offset = 0;
648 DynamicBuffer.length = 0;
649 DynamicBuffer.unbits = 0;
650 DynamicBuffer.allocated = 0;
651 DynamicBuffer.bytes_shifted = 0;
652 DynamicBuffer.nreallocs = 0;
653 }
Lev Walkinbc691772006-09-17 11:02:53 +0000654
Lev Walkinfb35e082017-08-04 03:06:51 -0700655 old_offset = DynamicBuffer.bytes_shifted + DynamicBuffer.offset;
Lev Walkin22b54552004-10-28 13:22:54 +0000656
Lev Walkinfb35e082017-08-04 03:06:51 -0700657 /* Pretend immediate EOF */
658 rval.code = RC_WMORE;
659 rval.consumed = 0;
Lev Walkind1bfea62005-11-08 03:06:16 +0000660
Lev Walkinfb35e082017-08-04 03:06:51 -0700661 for(tolerate_eof = 1; /* Allow EOF first time buffer is non-empty */
662 (rd = fread(fbuf, 1, fbuf_size, file))
663 || feof(file) == 0
664 || (tolerate_eof && DynamicBuffer.length)
665 ;) {
666 int ecbits = 0; /* Extra consumed bits in case of PER */
667 uint8_t *i_bptr;
668 size_t i_size;
Lev Walkin22b54552004-10-28 13:22:54 +0000669
Lev Walkinfb35e082017-08-04 03:06:51 -0700670 /*
671 * Copy the data over, or use the original buffer.
672 */
673 if(DynamicBuffer.allocated) {
674 /* Append new data into the existing dynamic buffer */
675 add_bytes_to_buffer(fbuf, rd);
676 i_bptr = DynamicBuffer.data + DynamicBuffer.offset;
677 i_size = DynamicBuffer.length;
678 } else {
679 i_bptr = fbuf;
680 i_size = rd;
681 }
Lev Walkin22b54552004-10-28 13:22:54 +0000682
Lev Walkinfb35e082017-08-04 03:06:51 -0700683 DEBUG("Decoding %ld bytes", (long)i_size);
Lev Walkinbc691772006-09-17 11:02:53 +0000684
Lev Walkinfb35e082017-08-04 03:06:51 -0700685#ifdef JUNKTEST
686 junk_bytes_with_probability(i_bptr, i_size, opt_jprob);
Lev Walkin1f12da42006-09-24 19:47:07 +0000687#endif
688
Lev Walkin6d46bc32017-09-12 21:34:00 -0700689 switch(isyntax) {
690 case ATS_BER:
Lev Walkinfb35e082017-08-04 03:06:51 -0700691 rval = ber_decode(opt_codec_ctx, pduType,
692 (void **)&structure, i_bptr, i_size);
693 break;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700694 case ATS_BASIC_OER:
Lev Walkin69033802017-08-25 12:15:58 -0700695#ifdef ASN_DISABLE_OER_SUPPORT
696 rval.code = RC_FAIL;
697 rval.consumed = 0;
698#else
Lev Walkinfb35e082017-08-04 03:06:51 -0700699 rval = oer_decode(opt_codec_ctx, pduType,
700 (void **)&structure, i_bptr, i_size);
Lev Walkin69033802017-08-25 12:15:58 -0700701#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700702 break;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700703 case ATS_BASIC_XER:
Lev Walkinfb35e082017-08-04 03:06:51 -0700704 rval = xer_decode(opt_codec_ctx, pduType,
705 (void **)&structure, i_bptr, i_size);
706 break;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700707 case ATS_UNALIGNED_BASIC_PER:
Lev Walkin69033802017-08-25 12:15:58 -0700708#ifdef ASN_DISABLE_PER_SUPPORT
709 rval.code = RC_FAIL;
710 rval.consumed = 0;
711#else
Lev Walkinfb35e082017-08-04 03:06:51 -0700712 if(opt_nopad)
713 rval = uper_decode(opt_codec_ctx, pduType,
714 (void **)&structure, i_bptr, i_size, 0,
715 DynamicBuffer.unbits);
716 else
717 rval = uper_decode_complete(opt_codec_ctx, pduType,
718 (void **)&structure, i_bptr, i_size);
Lev Walkin69033802017-08-25 12:15:58 -0700719#endif
Lev Walkinfb35e082017-08-04 03:06:51 -0700720 switch(rval.code) {
721 case RC_OK:
722 /* Fall through */
723 case RC_FAIL:
724 if(opt_nopad) {
725 /* uper_decode() returns bits! */
726 /* Extra bits */
727 ecbits = rval.consumed % 8;
728 /* Convert into bytes! */
729 rval.consumed /= 8;
730 }
731 break;
732 case RC_WMORE:
733 /* PER does not support restartability */
734 ASN_STRUCT_FREE(*pduType, structure);
735 structure = 0;
736 rval.consumed = 0;
737 /* Continue accumulating data */
738 break;
739 }
740 break;
Lev Walkin6d46bc32017-09-12 21:34:00 -0700741 default:
742 rval.consumed = 0;
743 /* Fall through */
Lev Walkinfb35e082017-08-04 03:06:51 -0700744 }
745 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 Walkinfb35e082017-08-04 03:06:51 -0700797 DEBUG("Clean up partially decoded structure");
798 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