Data::Hexdumper - Make binary data human-readable


Data-Hexdumper documentation Contained in the Data-Hexdumper distribution.

Index


Code Index:

NAME

Top

Data::Hexdumper - Make binary data human-readable

SYNOPSIS

Top

    use Data::Hexdumper qw(hexdump);
    $results = hexdump(
        data           => $data, # what to dump
        number_format  => 'S',   # display as unsigned 'shorts'
        start_position => 100,   # start at this offset ...
        end_position   => 148    # ... and end at this offset
    );
    print $results;

DESCRIPTION

Top

Data::Hexdumper provides a simple way to format arbitary binary data into a nice human-readable format, somewhat similar to the Unix 'hexdump' utility.

It gives the programmer a considerable degree of flexibility in how the data is formatted, with sensible defaults. It is envisaged that it will primarily be of use for those wrestling alligators in the swamp of binary file formats, which is why it was written in the first place.

SUBROUTINES

Top

The following subroutines are exported by default, although this is deprecated and will be removed in some future version. Please pretend that you need to ask the module to export them to you.

If you do assume that the module will always export them, then you may also assume that your code will break at some point after 1 Aug 2012.

hexdump

Does everything. Takes a hash of parameters, one of which is mandatory, the rest having sensible defaults if not specified. Available parameters are:

data

A scalar containing the binary data we're interested in. This is mandatory.

start_position

An integer telling us where in data to start dumping. Defaults to the beginning of data.

end_position

An integer telling us where in data to stop dumping. Defaults to the end of data.

number_format

A string specifying how to format the data. It can be any of the following, which you will notice have the same meanings as they do to perl's pack function:

C - unsigned char
S - unsigned 16-bit, native endianness
v or S< - unsigned 16-bit, little-endian
n or S> - unsigned 16-bit, big-endian
L - unsigned 32-bit, native endianness
V or L< - unsigned 32-bit, little-endian
N or L> - unsigned 32-bit, big-endian
Q - unsigned 64-bit, native endianness
Q< - unsigned 64-bit, little-endian
Q> - unsigned 64-bit, big-endian

It defaults to 'C'. Note that 64-bit formats are *always* available, even if your perl is only 32-bit. Similarly, using < and > on the S and L formats always works, even if you're using a pre 5.10.0 perl. That's because this code doesn't use pack().

suppress_warnings

Make this true if you want to suppress any warnings - such as that your data may have been padded with NULLs if it didn't exactly fit into an integer number of words, or if you do something that is deprecated.

space_as_space

Make this true if you want spaces (ASCII character 0x20) to be printed as spaces Otherwise, spaces will be printed as full stops / periods (ASCII 0x2E).

Alternatively, you can supply the parameters as a scalar chunk of data followed by an optional hashref of the other options:

    $results = hexdump($string);

    $results = hexdump(
        $string,
        { start_position => 100, end_position   => 148 }
    );

SEE ALSO

Top

Data::Dumper

Data::HexDump if your needs are simple

perldoc -f unpack

perldoc -f pack

BUGS/LIMITATIONS

Top

There is no support for syntax like 'S!' like what pack() has, so it's not possible to tell it to use your environment's native word-lengths.

It formats the data for an 80 column screen, perhaps this should be a frobbable parameter.

Formatting may break if the end position has an address greater than 65535.

FEEDBACK

Top

I welcome constructive criticism and bug reports. Please report bugs either by email or via RT:http://rt.cpan.org/Public/Dist/Display.html?Name=Data-Hexdumper

The best bug reports contain a test file that fails with the code that is currently in CVS, and will pass once it has been fixed. The CVS repository is on Sourceforge and can be viewed in a web browser here:http://drhyde.cvs.sourceforge.net/drhyde/perlmodules/Data-Hexdumper/

AUTHOR, COPYRIGHT and LICENCE

Top

CONSPIRACY

Top

This module is also free-as-in-mason software.

THANKS TO ...

Top

MHX, for reporting a bug when dumping a single byte of data

Stefan Siegl, for reporting a bug when dumping an ASCII 0


Data-Hexdumper documentation Contained in the Data-Hexdumper distribution.
# $Id: Hexdumper.pm,v 1.6 2009/03/03 20:18:06 drhyde Exp $
package Data::Hexdumper;

$VERSION = "2.01";

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(hexdump);

use strict;
use warnings;

use constant BIGENDIAN    => (unpack("h*", pack("s", 1)) =~ /01/);
use constant LITTLEENDIAN => (unpack("h*", pack("s", 1)) =~ /^1/);

# this is a magic number
use constant CHUNKSIZE => 16;

# static data, tells us the length of each type of word
my %num_bytes=(
    'C'  => 1, # unsigned char
    'S'  => 2, # unsigned 16-bit
    'L'  => 4, # unsigned 32-bit
    'L<' => 4, # unsigned 32-bit, little-endian
    'L>' => 4, # unsigned 32-bit, big-endian
    'V'  => 4, # unsigned 32-bit, little-endian
    'N'  => 4, # unsigned 32-bit, big-endian
    'S<' => 2, # unsigned 16-bit, little-endian
    'S>' => 2, # unsigned 16-bit, big-endian
    'v'  => 2, # unsigned 16-bit, little-endian
    'n'  => 2, # unsigned 16-bit, big-endian
    'Q'  => 8, # unsigned 64-bit
    'Q<' => 8, # unsigned 64-bit, little-endian
    'Q>' => 8, # unsigned 64-bit, big-endian
);

sub hexdump {
    my @params = @_;
    # first let's see if we need to massage the data into canonical form ...
    if($#params == 0) {                 # one param: hexdump($string)
        @params = (data => $params[0]);
    } elsif($#params == 1 && ref($params[1])) { # two: hexdump($foo, {...})
        @params = (
            data => $params[0],
            %{$params[1]}
        )
    }

    my %params=@params;
    my($data, $number_format, $start_position, $end_position)=
        @params{qw(data number_format start_position end_position)};

    my $addr = $start_position ||= 0;
    $number_format ||= 'C';
    $end_position ||= length($data)-1;
    my $num_bytes = $num_bytes{$number_format};
    if($number_format eq 'V') { $number_format = 'L<'; }
    if($number_format eq 'N') { $number_format = 'L>'; }
    if($number_format eq 'v') { $number_format = 'S<'; }
    if($number_format eq 'n') { $number_format = 'S>'; }

    # sanity-check the parameters

    die("No data given to hexdump.") unless length($data);
    die("start_position must be numeric.") if($start_position=~/\D/);
    die("number_format $number_format not recognised.") unless $num_bytes;
    die("end_position must be numeric.") if($end_position=~/\D/);
    die("end_position must not be before start_position.")
        if($end_position < $start_position);

    # extract the required range and pad end with NULLs if necessary

    $data=substr($data, $start_position, 1+$end_position-$start_position);
    if(length($data)/$num_bytes != int(length($data)/$num_bytes)) {
        warn "Data::Hexdumper: data doesn't exactly fit into an integer number ".
                 "of '$number_format' words,\nso has been padded ".
                 "with NULLs at the end.\n"
            unless($params{suppress_warnings});
        $data .= pack('C', 0) x ($num_bytes - length($data) + int(length($data)/$num_bytes)*$num_bytes);
    }

    my $output=''; # where we put the formatted results

    while(length($data)) {
        # Get a chunk
        my $chunk = substr($data, 0, CHUNKSIZE);
        $data = ($chunk eq $data) ? '' : substr($data, CHUNKSIZE);
        
        $output.=sprintf('  0x%04X : ', $addr);

        # have to keep chunk for printing, so make a copy we
        # can 'eat' $num_bytes at a time.
        my $line=$chunk;

        my $lengthOfLine=0;         # used for formatting in inner loop

        while(length($line)) {
            # grab a $num_bytes element, and remove from line
            my $thisElement=substr($line,0,$num_bytes);
            if(length($line)>$num_bytes) {
                $line=substr($line,$num_bytes);
            } else { $line=''; }
            my $thisData = _format_word($number_format, $thisElement);
            $lengthOfLine+=length($thisData);
            $output.=$thisData;
        }
        # replace any non-printable character with .
        if($params{space_as_space}) {
            $chunk=~s/[^a-z0-9\\|,.<>;:'\@[{\]}#`!"\$%^&*()_+=~?\/ -]/./gi;
                }
                else {
            $chunk=~s/[^a-z0-9\\|,.<>;:'\@[{\]}#`!"\$%^&*()_+=~?\/-]/./gi;
                }
        # Yes, this 48 *is* a magic number.
        $output.=' ' x (48-$lengthOfLine) .": $chunk\n";
        $addr += CHUNKSIZE;
    }
    $output;
}

sub _format_word {
    my($format, $data) = @_;

    # big endian
    my @bytes = map { ord($_) } split(//, $data);
    # make little endian if necessary
    @bytes = reverse(@bytes)
        if($format =~ /</ || ($format !~ />/ && LITTLEENDIAN));
    return join('', map { sprintf('%02X', $_) } @bytes).' ';
}

1;