1
0
mirror of https://github.com/fachat/xa65.git synced 2024-11-03 06:05:58 +00:00
xa65/xa/tests/hextool

58 lines
1.2 KiB
Perl
Executable File

#!/usr/bin/perl -s
# This tool either emits hex in a machine or human parseable format, or
# compares two binaries in the form of cmp. It is mostly to deal with
# systems with an unreliable hexdump, that *lack* hexdump (or od), and/or
# emit non-standard output.
# "use bytes"
BEGIN { $^H |= 0x00000008 unless ($] < 5.006); }
if ($output || $noutput) {
# check output mode. If $output, there must be output. If there is
# $noutput there must NOT be output. Feed tee or something to this.
$wasoutput = 0;
while(<>) {
$wasoutput++;
}
exit (($wasoutput && $output) ? 0 :
($wasoutput && $noutput) ? 1 :
(!$wasoutput && !$noutput) ? 0 :
1);
}
if ($cmp) {
# compare mode. both files must fit in memory. to eliminate any
# weirdness about endianness, encoding, etc., we unpack them to
# hex bytes and just compare strings.
undef $/;
open(W, "$cmp") || die("can't open $cmp: $!\n");
$cmp = unpack("H*", <W>);
close(W);
$std = unpack("H*", <>);
if ($cmp ne $std) {
print <<"EOF";
FAILED!
received from stdin
-------------------
$std
expected to equal
-----------------
$cmp
FAILED!
EOF
exit 255;
}
exit 0;
}
# hexdump mode. accept data on stdin, spew a stream of hex bytes.
while(<>) {
print STDOUT unpack("H*", $_);
}