]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - contrib/logrewrap.pl
first pass
[PuTTY.git] / contrib / logrewrap.pl
1 #!/usr/bin/perl
2
3 # Process a PuTTY SSH packet log that has gone through inappropriate
4 # line wrapping, and try to make it legible again.
5 #
6 # Motivation: people often include PuTTY packet logs in email
7 # messages, and if they're not careful, the sending MUA 'helpfully'
8 # wraps the lines at 72 characters, corrupting all the hex dumps into
9 # total unreadability.
10 #
11 # But as long as it's only the ASCII part of the dump at the end of
12 # the line that gets wrapped, and the hex part is untouched, this is a
13 # mechanically recoverable kind of corruption, because the ASCII is
14 # redundant and can be reconstructed from the hex. Better still, you
15 # can spot lines in which this has happened (because the ASCII at the
16 # end of the line is a truncated version of what we think it should
17 # say), and use that as a cue to remove the following line.
18
19 use strict;
20 use warnings;
21
22 while (<>) {
23     if (/^  ([0-9a-f]{8})  ((?:[0-9a-f]{2} ){0,15}(?:[0-9a-f]{2}))/) {
24         my $addr = $1;
25         my $hex = $2;
26         my $ascii = "";
27         for (my $i = 0; $i < length($2); $i += 3) {
28             my $byte = hex(substr($hex, $i, 2));
29             my $char = ($byte >= 32 && $byte < 127 ? chr($byte) : ".");
30             $ascii .= $char;
31         }
32         $hex = substr($hex . (" " x 48), 0, 47);
33         my $old_line = $_;
34         chomp($old_line);
35         my $new_line = "  $addr  $hex  $ascii";
36         if ($old_line ne $new_line and
37             $old_line eq substr($new_line, 0, length($old_line))) {
38             print "$new_line\n";
39             <>; # eat the subsequent wrapped line
40             next;
41         }
42     }
43     print $_;
44 }