sourcecode
hsmyers
<code>
#!/usr/bin/perl
# dump.pl -- create hex dump of input file.
use strict;
use warnings;
use diagnostics;
use Getopt::Std;
use Config;
my $filename = $ARGV[0];
my $output = $ARGV[1];
my $buffer;
my $address = 0;
my $old_fh;
$0 =~ s|.*[/\\]||;
my $usage = <<EOT;
Usage: $0 [-h]
or: $0 file_in [file_out]
EOT
my %OPT = ();
warn($usage), exit(0) if !getopts( 'h', \%OPT ) or $OPT{'h'};
if ($output) {
open( OUT, "> $output" ) || die "Couldn't open $output for output: $!\n";
$old_fh = select(OUT);
}
die "No filename specified\n" unless ($filename);
open( FILE, $filename ) || die "Couldn't open $filename: $!\n";
binmode FILE;
while ( read( FILE, $buffer, 16 ) ) {
printf( "%08x: %s | %-16s\n", $address, hexstr($buffer),
makesafe($buffer) );
$address += 16;
}
close(FILE);
if ($output) {
select($old_fh) if ($output);
close(OUT);
}
sub makesafe {
my @list = unpack( 'C*', $_[0] );
foreach (@list) {
if ( $_ < 32 || $_ > 126 ) {
$_ = 46;
}
}
my $result = pack( 'C*', @list );
return $result;
}
sub hexstr {
my @list = unpack( 'H32', $_[0] );
my $result = sprintf( "%-32s", $list[0] );
my $expanded;
while ( $result =~ /(..)/g ) {
$expanded .= $1 . ' ';
}
return $expanded;
}
=pod
=head1 NAME
dump - output a formated hex dump of a selected file
=head1 SYNOPSIS
B<dump> B<-h>
B<dump> filename
B<dump> file_in file_out
=head1 DESCRIPTION
This utility converts a file into the formated hexidecimal representation of itself.
Output is to either STDOUT or to a specified file.
=head1 EXAMPLES
C:\dump dump.pl
creates:
00000000: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 20 | #!/usr/bin/perl
00000010: 2d 77 0a 23 20 64 75 6d 70 2e 70 6c 20 2d 2d 20 | -w.# dump.pl --
00000020: 63 72 65 61 74 65 20 68 65 78 20 64 75 6d 70 20 | create hex dump
00000030: 6f 66 20 69 6e 70 75 74 20 66 69 6c 65 2e 0a 0a | of input file...
00000040: 75 73 65 20 73 74 72 69 63 74 3b 0a 75 73 65 20 | use strict;.use
00000050: 77 61 72 6e 69 6e 67 73 3b 0a 0a 6d 79 20 24 66 | warnings;..my $f
.
.
.
00000390: 64 65 64 20 2e 3d 20 24 31 2e 27 20 27 3b 0a 09 | ded .= $1.' ';..
000003a0: 7d 0a 09 72 65 74 75 72 6e 20 24 65 78 70 61 6e | }..return $expan
000003b0: 64 65 64 3b 0a 7d | ded;.}
C:\dump dump.pl out.txt
creates: the file out.txt as above...
C:\dump -h
shows: command line usage
=head1 SEE ALSO
coredump, divination, entrails
=head1 BUGS
C:\dump filename > outfile
won't work if this is made a dos batch file...
=head1 AUTHOR
Hugh S. Myers
hsmyers@sdragons.com
=cut
__END__
</code>
Nothing fancy, just a straight forward binary dump of a file in a formatted display to either STDOUT or filename. Particularly handy to compare <em>what you think is in the file</em> with what is actually in the file!<p>hsm
Utility Scripts
hsmyers@sdragons.com