| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package DR::Tnt::Dumper; | ||
| 6 | use base qw(Exporter); | ||
| 7 | our @EXPORT = qw(pkt_dump dump); | ||
| 8 | use constant DUMP_LINE_LEN => 16; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | sub pkt_dump($$) { | ||
| 12 | my ($name, $pkt) = @_; | ||
| 13 | |||
| 14 | $name .= sprintf ' (%s octets)', length $pkt; | ||
| 15 | my @lines; | ||
| 16 | |||
| 17 | while (DUMP_LINE_LEN < length $pkt) { | ||
| 18 | push @lines, substr $pkt, 0, DUMP_LINE_LEN, ''; | ||
| 19 | } | ||
| 20 | push @lines => $pkt if length $pkt; | ||
| 21 | |||
| 22 | for (@lines) { | ||
| 23 | |||
| 24 | $_ = [ $_, $_ ]; | ||
| 25 | |||
| 26 | $_->[0] =~ s/(.)/sprintf '%02X ', ord $1/ges; | ||
| 27 | |||
| 28 | while (length($_->[0]) < (3 * DUMP_LINE_LEN)) { | ||
| 29 | $_->[0] .= ' '; | ||
| 30 | } | ||
| 31 | $_->[1] =~ s/(.)/((ord($1) >= 0x20) and (ord($1) <= 0x7F)) ? $1 : '.'/ges; | ||
| 32 | |||
| 33 | $_ = "$_->[0] $_->[1]"; | ||
| 34 | } | ||
| 35 | return join "\n", "request $name", @lines, ""; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub dump($) { | ||
| 39 | my ($o) = @_; | ||
| 40 | local $Data::Dumper::Indent = 1; | ||
| 41 | local $Data::Dumper::Terse = 1; | ||
| 42 | local $Data::Dumper::Useqq = 1; | ||
| 43 | local $Data::Dumper::Deepcopy = 1; | ||
| 44 | local $Data::Dumper::Maxdepth = 0; | ||
| 45 | return Data::Dumper->Dump([$o]); | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 |