2 * Generic converter template for a selected ASN.1 type.
3 * Copyright (c) 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
6 * To compile with your own ASN.1 type, please redefine the PDU as shown:
8 * cc -DPDU=MyCustomType -o myDecoder.o -c converter-sample.c
14 #include <sys/types.h>
15 #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 */
21 #include <asn_application.h>
22 #include <asn_internal.h> /* for _ASN_DEFAULT_STACK_MAX */
24 /* Convert "Type" defined by -DPDU into "asn_DEF_Type" */
25 #define ASN_DEF_PDU(t) asn_DEF_ ## t
26 #define DEF_PDU_Type(t) ASN_DEF_PDU(t)
27 #define PDU_Type DEF_PDU_Type(PDU)
29 extern asn_TYPE_descriptor_t PDU_Type
; /* ASN.1 type to be decoded */
30 #ifdef ASN_PDU_COLLECTION /* Generated by asn1c: -pdu=... */
31 extern asn_TYPE_descriptor_t
*asn_pdu_collection
[];
35 * Open file and parse its contens.
37 static void *data_decode_from_file(asn_TYPE_descriptor_t
*asnTypeOfPDU
,
38 FILE *file
, const char *name
, ssize_t suggested_bufsize
, int first_pdu
);
39 static int write_out(const void *buffer
, size_t size
, void *key
);
40 static FILE *argument_to_file(char *av
[], int idx
);
41 static char *argument_to_name(char *av
[], int idx
);
43 int opt_debug
; /* -d (or -dd) */
44 static int opt_check
; /* -c (constraints checking) */
45 static int opt_stack
; /* -s (maximum stack size) */
46 static int opt_nopad
; /* -per-nopad (PER input is not padded) */
47 static int opt_onepdu
; /* -1 (decode single PDU) */
49 /* Input data format selector */
50 static enum input_format
{
51 INP_BER
, /* -iber: BER input */
52 INP_XER
, /* -ixer: XER input */
53 INP_PER
/* -iper: Unaligned PER input */
54 } iform
; /* -i<format> */
56 /* Output data format selector */
57 static enum output_format
{
58 OUT_XER
, /* -oxer: XER (XML) output */
59 OUT_DER
, /* -oder: DER (BER) output */
60 OUT_PER
, /* -oper: Unaligned PER output */
61 OUT_TEXT
, /* -otext: semi-structured text */
62 OUT_NULL
/* -onull: No pretty-printing */
63 } oform
; /* -o<format> */
65 #ifdef JUNKTEST /* Enable -J <probability> */
67 static double opt_jprob
; /* Junk bit probability */
68 static int junk_failures
;
69 static void junk_bytes_with_probability(uint8_t *, size_t, double prob
);
74 /* Debug output function */
76 DEBUG(const char *fmt
, ...) {
78 if(!opt_debug
) return;
79 fprintf(stderr
, "AD: ");
81 vfprintf(stderr
, fmt
, ap
);
83 fprintf(stderr
, "\n");
87 main(int ac
, char *av
[]) {
88 static asn_TYPE_descriptor_t
*pduType
= &PDU_Type
;
89 ssize_t suggested_bufsize
= 8192; /* close or equal to stdio buffer */
90 int number_of_iterations
= 1;
94 /* Figure out if Unaligned PER needs to be default */
95 if(pduType
->uper_decoder
)
99 * Pocess the command-line argments.
101 while((ch
= getopt(ac
, av
, "i:o:1b:cdn:p:hs:" JUNKOPT
)) != -1)
104 if(optarg
[0] == 'b') { iform
= INP_BER
; break; }
105 if(optarg
[0] == 'x') { iform
= INP_XER
; break; }
106 if(pduType
->uper_decoder
107 && optarg
[0] == 'p') { iform
= INP_PER
; break; }
108 fprintf(stderr
, "-i<format>: '%s': improper format selector\n",
110 exit(EX_UNAVAILABLE
);
112 if(optarg
[0] == 'd') { oform
= OUT_DER
; break; }
113 if(pduType
->uper_encoder
114 && optarg
[0] == 'p') { oform
= OUT_PER
; break; }
115 if(optarg
[0] == 'x') { oform
= OUT_XER
; break; }
116 if(optarg
[0] == 't') { oform
= OUT_TEXT
; break; }
117 if(optarg
[0] == 'n') { oform
= OUT_NULL
; break; }
118 fprintf(stderr
, "-o<format>: '%s': improper format selector\n",
120 exit(EX_UNAVAILABLE
);
125 suggested_bufsize
= atoi(optarg
);
126 if(suggested_bufsize
< 1
127 || suggested_bufsize
> 16 * 1024 * 1024) {
129 "-b %s: Improper buffer size (1..16M)\n",
131 exit(EX_UNAVAILABLE
);
138 opt_debug
++; /* Double -dd means ASN.1 debug */
141 number_of_iterations
= atoi(optarg
);
142 if(number_of_iterations
< 1) {
144 "-n %s: Improper iterations count\n", optarg
);
145 exit(EX_UNAVAILABLE
);
149 if(strcmp(optarg
, "er-nopad") == 0) {
153 #ifdef ASN_PDU_COLLECTION
154 if(strcmp(optarg
, "list") == 0) {
155 asn_TYPE_descriptor_t
**pdu
= asn_pdu_collection
;
156 fprintf(stderr
, "Available PDU types:\n");
157 for(; *pdu
; pdu
++) printf("%s\n", (*pdu
)->name
);
159 } else if(optarg
[0] >= 'A' && optarg
[0] <= 'Z') {
160 asn_TYPE_descriptor_t
**pdu
= asn_pdu_collection
;
161 while(*pdu
&& strcmp((*pdu
)->name
, optarg
)) pdu
++;
162 if(*pdu
) { pduType
= *pdu
; break; }
163 fprintf(stderr
, "-p %s: Unrecognized PDU\n", optarg
);
165 #endif /* ASN_PDU_COLLECTION */
166 fprintf(stderr
, "-p %s: Unrecognized option\n", optarg
);
167 exit(EX_UNAVAILABLE
);
169 opt_stack
= atoi(optarg
);
172 "-s %s: Non-negative value expected\n",
174 exit(EX_UNAVAILABLE
);
179 opt_jprob
= strtod(optarg
, 0);
180 if(opt_jprob
<= 0.0 || opt_jprob
> 1.0) {
182 "-J %s: Probability range 0..1 expected \n",
184 exit(EX_UNAVAILABLE
);
187 #endif /* JUNKTEST */
190 #ifdef ASN_CONVERTER_TITLE
192 #define _ASX(x) _AXS(x)
193 fprintf(stderr
, "%s\n", _ASX(ASN_CONVERTER_TITLE
));
195 fprintf(stderr
, "Usage: %s [options] <data.ber> ...\n", av
[0]);
196 fprintf(stderr
, "Where options are:\n");
197 if(pduType
->uper_decoder
)
199 " -iper Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)\n");
201 " -iber Input is in BER (Basic Encoding Rules)%s\n",
202 iform
== INP_PER
? "" : " (DEFAULT)");
204 " -ixer Input is in XER (XML Encoding Rules)\n");
205 if(pduType
->uper_encoder
)
207 " -oper Output in Unaligned PER (Packed Encoding Rules)\n");
209 " -oder Output in DER (Distinguished Encoding Rules)\n"
210 " -oxer Output in XER (XML Encoding Rules) (DEFAULT)\n"
211 " -otext Output in plain semi-structured text (dump)\n"
212 " -onull Verify (decode) input, but do not output\n");
213 if(pduType
->uper_decoder
)
215 " -per-nopad Assume PER PDUs are not padded (-iper)\n");
216 #ifdef ASN_PDU_COLLECTION
218 " -p <PDU> Specify PDU type to decode\n"
219 " -p list List available PDUs\n");
220 #endif /* ASN_PDU_COLLECTION */
222 " -1 Decode only the first PDU in file\n"
223 " -b <size> Set the i/o buffer size (default is %ld)\n"
224 " -c Check ASN.1 constraints after decoding\n"
225 " -d Enable debugging (-dd is even better)\n"
226 " -n <num> Process files <num> times\n"
227 " -s <size> Set the stack usage limit (default is %d)\n"
229 " -J <prob> Set random junk test bit garbaging probability\n"
231 , (long)suggested_bufsize
, _ASN_DEFAULT_STACK_MAX
);
239 fprintf(stderr
, "%s: No input files specified. "
240 "Try '-h' for more information\n",
245 setvbuf(stdout
, 0, _IOLBF
, 0);
247 for(num
= 0; num
< number_of_iterations
; num
++) {
250 * Process all files in turn.
252 for(ac_i
= 0; ac_i
< ac
; ac_i
++) {
254 void *structure
; /* Decoded structure */
255 FILE *file
= argument_to_file(av
, ac_i
);
256 char *name
= argument_to_name(av
, ac_i
);
259 for(first_pdu
= 1; first_pdu
|| !opt_onepdu
; first_pdu
= 0) {
261 * Decode the encoded structure from file.
263 structure
= data_decode_from_file(pduType
,
264 file
, name
, suggested_bufsize
, first_pdu
);
267 /* Error message is already printed */
275 /* Check ASN.1 constraints */
278 size_t errlen
= sizeof(errbuf
);
279 if(asn_check_constraints(pduType
, structure
,
281 fprintf(stderr
, "%s: ASN.1 constraint "
282 "check failed: %s\n", name
, errbuf
);
292 fprintf(stderr
, "%s: decoded successfully\n", name
);
294 case OUT_TEXT
: /* -otext */
295 asn_fprint(stdout
, pduType
, structure
);
297 case OUT_XER
: /* -oxer */
298 if(xer_fprint(stdout
, pduType
, structure
)) {
300 "%s: Cannot convert %s into XML\n",
301 name
, pduType
->name
);
302 exit(EX_UNAVAILABLE
);
306 erv
= der_encode(pduType
, structure
, write_out
, stdout
);
307 if(erv
.encoded
< 0) {
309 "%s: Cannot convert %s into DER\n",
310 name
, pduType
->name
);
311 exit(EX_UNAVAILABLE
);
313 DEBUG("Encoded in %ld bytes of DER", (long)erv
.encoded
);
316 erv
= uper_encode(pduType
, structure
, write_out
, stdout
);
317 if(erv
.encoded
< 0) {
319 "%s: Cannot convert %s into Unaligned PER\n",
320 name
, pduType
->name
);
321 exit(EX_UNAVAILABLE
);
323 DEBUG("Encoded in %ld bits of UPER", (long)erv
.encoded
);
327 ASN_STRUCT_FREE(*pduType
, structure
);
330 if(file
&& file
!= stdin
)
336 if(opt_jprob
> 0.0) {
337 fprintf(stderr
, "Junked %f OK (%d/%d)\n",
338 opt_jprob
, junk_failures
, number_of_iterations
);
340 #endif /* JUNKTEST */
345 static struct dynamic_buffer
{
346 uint8_t *data
; /* Pointer to the data bytes */
347 size_t offset
; /* Offset from the start */
348 size_t length
; /* Length of meaningful contents */
349 size_t unbits
; /* Unused bits in the last byte */
350 size_t allocated
; /* Allocated memory for data */
351 int nreallocs
; /* Number of data reallocations */
352 off_t bytes_shifted
; /* Number of bytes ever shifted */
357 uint8_t *p
= DynamicBuffer
.data
+ DynamicBuffer
.offset
;
358 uint8_t *e
= p
+ DynamicBuffer
.length
- (DynamicBuffer
.unbits
? 1 : 0);
359 if(!opt_debug
) return;
360 DEBUG("Buffer: { d=%p, o=%ld, l=%ld, u=%ld, a=%ld, s=%ld }",
362 (long)DynamicBuffer
.offset
,
363 (long)DynamicBuffer
.length
,
364 (long)DynamicBuffer
.unbits
,
365 (long)DynamicBuffer
.allocated
,
366 (long)DynamicBuffer
.bytes_shifted
);
368 fprintf(stderr
, " %c%c%c%c%c%c%c%c",
369 ((*p
>> 7) & 1) ? '1' : '0',
370 ((*p
>> 6) & 1) ? '1' : '0',
371 ((*p
>> 5) & 1) ? '1' : '0',
372 ((*p
>> 4) & 1) ? '1' : '0',
373 ((*p
>> 3) & 1) ? '1' : '0',
374 ((*p
>> 2) & 1) ? '1' : '0',
375 ((*p
>> 1) & 1) ? '1' : '0',
376 ((*p
>> 0) & 1) ? '1' : '0');
378 if(DynamicBuffer
.unbits
) {
380 fprintf(stderr
, " ");
381 for(shift
= 7; shift
>= DynamicBuffer
.unbits
; shift
--)
382 fprintf(stderr
, "%c", ((*p
>> shift
) & 1) ? '1' : '0');
383 fprintf(stderr
, " %ld:%ld\n",
384 (long)DynamicBuffer
.length
- 1,
385 (long)8 - DynamicBuffer
.unbits
);
387 fprintf(stderr
, " %ld\n", (long)DynamicBuffer
.length
);
392 * Move the buffer content left N bits, possibly joining it with
393 * preceeding content.
396 buffer_shift_left(size_t offset
, int bits
) {
397 uint8_t *ptr
= DynamicBuffer
.data
+ DynamicBuffer
.offset
+ offset
;
398 uint8_t *end
= DynamicBuffer
.data
+ DynamicBuffer
.offset
399 + DynamicBuffer
.length
- 1;
403 DEBUG("Shifting left %d bits off %ld (o=%ld, u=%ld, l=%ld)",
405 (long)DynamicBuffer
.offset
,
406 (long)DynamicBuffer
.unbits
,
407 (long)DynamicBuffer
.length
);
411 right
= ptr
[0] >> (8 - bits
);
413 DEBUG("oleft: %c%c%c%c%c%c%c%c",
414 ((ptr
[-1] >> 7) & 1) ? '1' : '0',
415 ((ptr
[-1] >> 6) & 1) ? '1' : '0',
416 ((ptr
[-1] >> 5) & 1) ? '1' : '0',
417 ((ptr
[-1] >> 4) & 1) ? '1' : '0',
418 ((ptr
[-1] >> 3) & 1) ? '1' : '0',
419 ((ptr
[-1] >> 2) & 1) ? '1' : '0',
420 ((ptr
[-1] >> 1) & 1) ? '1' : '0',
421 ((ptr
[-1] >> 0) & 1) ? '1' : '0');
423 DEBUG("oriht: %c%c%c%c%c%c%c%c",
424 ((ptr
[0] >> 7) & 1) ? '1' : '0',
425 ((ptr
[0] >> 6) & 1) ? '1' : '0',
426 ((ptr
[0] >> 5) & 1) ? '1' : '0',
427 ((ptr
[0] >> 4) & 1) ? '1' : '0',
428 ((ptr
[0] >> 3) & 1) ? '1' : '0',
429 ((ptr
[0] >> 2) & 1) ? '1' : '0',
430 ((ptr
[0] >> 1) & 1) ? '1' : '0',
431 ((ptr
[0] >> 0) & 1) ? '1' : '0');
433 DEBUG("mriht: %c%c%c%c%c%c%c%c",
434 ((right
>> 7) & 1) ? '1' : '0',
435 ((right
>> 6) & 1) ? '1' : '0',
436 ((right
>> 5) & 1) ? '1' : '0',
437 ((right
>> 4) & 1) ? '1' : '0',
438 ((right
>> 3) & 1) ? '1' : '0',
439 ((right
>> 2) & 1) ? '1' : '0',
440 ((right
>> 1) & 1) ? '1' : '0',
441 ((right
>> 0) & 1) ? '1' : '0');
443 ptr
[-1] = (ptr
[-1] & (0xff << bits
)) | right
;
445 DEBUG("after: %c%c%c%c%c%c%c%c",
446 ((ptr
[-1] >> 7) & 1) ? '1' : '0',
447 ((ptr
[-1] >> 6) & 1) ? '1' : '0',
448 ((ptr
[-1] >> 5) & 1) ? '1' : '0',
449 ((ptr
[-1] >> 4) & 1) ? '1' : '0',
450 ((ptr
[-1] >> 3) & 1) ? '1' : '0',
451 ((ptr
[-1] >> 2) & 1) ? '1' : '0',
452 ((ptr
[-1] >> 1) & 1) ? '1' : '0',
453 ((ptr
[-1] >> 0) & 1) ? '1' : '0');
458 for(; ptr
< end
; ptr
++) {
459 int right
= ptr
[1] >> (8 - bits
);
460 *ptr
= (*ptr
<< bits
) | right
;
464 DEBUG("Unbits [%d=>", (int)DynamicBuffer
.unbits
);
465 if(DynamicBuffer
.unbits
== 0) {
466 DynamicBuffer
.unbits
+= bits
;
468 DynamicBuffer
.unbits
+= bits
;
469 if(DynamicBuffer
.unbits
> 7) {
470 DynamicBuffer
.unbits
-= 8;
471 DynamicBuffer
.length
--;
472 DynamicBuffer
.bytes_shifted
++;
475 DEBUG("Unbits =>%d]", (int)DynamicBuffer
.unbits
);
479 DEBUG("Shifted. Now (o=%ld, u=%ld l=%ld)",
480 (long)DynamicBuffer
.offset
,
481 (long)DynamicBuffer
.unbits
,
482 (long)DynamicBuffer
.length
);
488 * Ensure that the buffer contains at least this amount of free space.
490 static void add_bytes_to_buffer(const void *data2add
, size_t bytes
) {
492 if(bytes
== 0) return;
494 DEBUG("=> add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
496 (long)DynamicBuffer
.offset
,
497 (long)DynamicBuffer
.length
,
498 (long)DynamicBuffer
.unbits
,
499 (long)DynamicBuffer
.allocated
);
501 if(DynamicBuffer
.allocated
502 >= (DynamicBuffer
.offset
+ DynamicBuffer
.length
+ bytes
)) {
503 DEBUG("\tNo buffer reallocation is necessary");
504 } else if(bytes
<= DynamicBuffer
.offset
) {
505 DEBUG("\tContents shifted by %ld", DynamicBuffer
.offset
);
507 /* Shift the buffer contents */
508 memmove(DynamicBuffer
.data
,
509 DynamicBuffer
.data
+ DynamicBuffer
.offset
,
510 DynamicBuffer
.length
);
511 DynamicBuffer
.bytes_shifted
+= DynamicBuffer
.offset
;
512 DynamicBuffer
.offset
= 0;
514 size_t newsize
= (DynamicBuffer
.allocated
<< 2) + bytes
;
515 void *p
= MALLOC(newsize
);
521 DynamicBuffer
.data
+ DynamicBuffer
.offset
,
522 DynamicBuffer
.length
);
523 FREEMEM(DynamicBuffer
.data
);
524 DynamicBuffer
.data
= (uint8_t *)p
;
525 DynamicBuffer
.offset
= 0;
526 DynamicBuffer
.allocated
= newsize
;
527 DynamicBuffer
.nreallocs
++;
528 DEBUG("\tBuffer reallocated to %ld (%d time)",
529 newsize
, DynamicBuffer
.nreallocs
);
532 memcpy(DynamicBuffer
.data
533 + DynamicBuffer
.offset
+ DynamicBuffer
.length
,
535 DynamicBuffer
.length
+= bytes
;
536 if(DynamicBuffer
.unbits
) {
537 int bits
= DynamicBuffer
.unbits
;
538 DynamicBuffer
.unbits
= 0;
539 buffer_shift_left(DynamicBuffer
.length
- bytes
, bits
);
542 DEBUG("<= add_bytes(%ld) { o=%ld l=%ld u=%ld, s=%ld }",
544 (long)DynamicBuffer
.offset
,
545 (long)DynamicBuffer
.length
,
546 (long)DynamicBuffer
.unbits
,
547 (long)DynamicBuffer
.allocated
);
551 data_decode_from_file(asn_TYPE_descriptor_t
*pduType
, FILE *file
, const char *name
, ssize_t suggested_bufsize
, int on_first_pdu
) {
552 static uint8_t *fbuf
;
553 static ssize_t fbuf_size
;
554 static asn_codec_ctx_t s_codec_ctx
;
555 asn_codec_ctx_t
*opt_codec_ctx
= 0;
564 fprintf(stderr
, "%s: %s\n", name
, strerror(errno
));
570 s_codec_ctx
.max_stack_size
= opt_stack
;
571 opt_codec_ctx
= &s_codec_ctx
;
574 DEBUG("Processing %s", name
);
576 /* prepare the file buffer */
577 if(fbuf_size
!= suggested_bufsize
) {
578 fbuf
= (uint8_t *)REALLOC(fbuf
, suggested_bufsize
);
583 fbuf_size
= suggested_bufsize
;
587 DynamicBuffer
.offset
= 0;
588 DynamicBuffer
.length
= 0;
589 DynamicBuffer
.unbits
= 0;
590 DynamicBuffer
.allocated
= 0;
591 DynamicBuffer
.bytes_shifted
= 0;
592 DynamicBuffer
.nreallocs
= 0;
595 old_offset
= DynamicBuffer
.bytes_shifted
+ DynamicBuffer
.offset
;
597 /* Pretend immediate EOF */
598 rval
.code
= RC_WMORE
;
601 for(tolerate_eof
= 1; /* Allow EOF first time buffer is non-empty */
602 (rd
= fread(fbuf
, 1, fbuf_size
, file
))
604 || (tolerate_eof
&& DynamicBuffer
.length
)
606 int ecbits
= 0; /* Extra consumed bits in case of PER */
611 * Copy the data over, or use the original buffer.
613 if(DynamicBuffer
.allocated
) {
614 /* Append new data into the existing dynamic buffer */
615 add_bytes_to_buffer(fbuf
, rd
);
616 i_bptr
= DynamicBuffer
.data
+ DynamicBuffer
.offset
;
617 i_size
= DynamicBuffer
.length
;
623 DEBUG("Decoding %ld bytes", (long)i_size
);
626 junk_bytes_with_probability(i_bptr
, i_size
, opt_jprob
);
631 rval
= ber_decode(opt_codec_ctx
, pduType
,
632 (void **)&structure
, i_bptr
, i_size
);
635 rval
= xer_decode(opt_codec_ctx
, pduType
,
636 (void **)&structure
, i_bptr
, i_size
);
640 rval
= uper_decode(opt_codec_ctx
, pduType
,
641 (void **)&structure
, i_bptr
, i_size
, 0,
642 DynamicBuffer
.unbits
);
644 rval
= uper_decode_complete(opt_codec_ctx
, pduType
,
645 (void **)&structure
, i_bptr
, i_size
);
651 /* uper_decode() returns bits! */
653 ecbits
= rval
.consumed
% 8;
654 /* Convert into bytes! */
659 /* PER does not support restartability */
660 ASN_STRUCT_FREE(*pduType
, structure
);
663 /* Continue accumulating data */
668 DEBUG("decode(%ld) consumed %ld+%db (%ld), code %d",
669 (long)DynamicBuffer
.length
,
670 (long)rval
.consumed
, ecbits
, (long)i_size
,
673 if(DynamicBuffer
.allocated
== 0) {
675 * Flush remainder into the intermediate buffer.
677 if(rval
.code
!= RC_FAIL
&& rval
.consumed
< rd
) {
678 add_bytes_to_buffer(fbuf
+ rval
.consumed
,
680 buffer_shift_left(0, ecbits
);
681 DynamicBuffer
.bytes_shifted
= rval
.consumed
;
688 * Adjust position inside the source buffer.
690 if(DynamicBuffer
.allocated
) {
691 DynamicBuffer
.offset
+= rval
.consumed
;
692 DynamicBuffer
.length
-= rval
.consumed
;
694 DynamicBuffer
.bytes_shifted
+= rval
.consumed
;
699 if(ecbits
) buffer_shift_left(0, ecbits
);
700 DEBUG("RC_OK, finishing up with %ld+%d",
701 (long)rval
.consumed
, ecbits
);
704 DEBUG("RC_WMORE, continuing read=%ld, cons=%ld "
705 " with %ld..%ld-%ld..%ld",
708 (long)DynamicBuffer
.offset
,
709 (long)DynamicBuffer
.length
,
710 (long)DynamicBuffer
.unbits
,
711 (long)DynamicBuffer
.allocated
);
712 if(!rd
) tolerate_eof
--;
720 DEBUG("Clean up partially decoded structure");
721 ASN_STRUCT_FREE(*pduType
, structure
);
723 new_offset
= DynamicBuffer
.bytes_shifted
+ DynamicBuffer
.offset
;
726 * Print a message and return failure only if not EOF,
727 * unless this is our first PDU (empty file).
730 || DynamicBuffer
.length
731 || new_offset
- old_offset
> ((iform
== INP_XER
)?sizeof("\r\n")-1:0)
736 * Nothing's wrong with being unable to decode junk.
739 if(opt_jprob
!= 0.0) {
746 DEBUG("ofp %d, no=%ld, oo=%ld, dbl=%ld",
747 on_first_pdu
, (long)new_offset
, (long)old_offset
,
748 (long)DynamicBuffer
.length
);
749 fprintf(stderr
, "%s: "
750 "Decode failed past byte %ld: %s\n",
751 name
, (long)new_offset
,
752 (rval
.code
== RC_WMORE
)
753 ? "Unexpected end of input"
754 : "Input processing error");
756 #define ENOMSG EINVAL
759 #define EBADMSG EINVAL
761 errno
= (rval
.code
== RC_WMORE
) ? ENOMSG
: EBADMSG
;
763 /* Got EOF after a few successful PDUs */
770 /* Dump the buffer out to the specified FILE */
771 static int write_out(const void *buffer
, size_t size
, void *key
) {
772 FILE *fp
= (FILE *)key
;
773 return (fwrite(buffer
, 1, size
, fp
) == size
) ? 0 : -1;
776 static int argument_is_stdin(char *av
[], int idx
) {
777 if(strcmp(av
[idx
], "-")) {
778 return 0; /* Certainly not <stdin> */
780 /* This might be <stdin>, unless `./program -- -` */
781 if(strcmp(av
[-1], "--"))
788 static FILE *argument_to_file(char *av
[], int idx
) {
789 return argument_is_stdin(av
, idx
)
791 : fopen(av
[idx
], "r");
794 static char *argument_to_name(char *av
[], int idx
) {
795 return argument_is_stdin(av
, idx
)
802 * Fill bytes with some garbage with specified probability (more or less).
805 junk_bytes_with_probability(uint8_t *buf
, size_t size
, double prob
) {
809 if(opt_jprob
<= 0.0) return;
810 for(ptr
= buf
, end
= ptr
+ size
; ptr
< end
; ptr
++) {
813 if((((double)random() / RAND_MAX
) < prob
))
814 byte
= random() & 0xff;
816 #define BPROB(b) ((((double)random() / RAND_MAX) < prob) ? b : 0)
827 DEBUG("Junk buf[%d] %02x -> %02x",
828 ptr
- buf
, *ptr
, byte
);
833 #endif /* JUNKTEST */