-
Notifications
You must be signed in to change notification settings - Fork 2
/
colorbindiff.pl
307 lines (282 loc) · 8.81 KB
/
colorbindiff.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/perl
#########################################################################
#
# COLORBINDIFF.PL : A side-by-side visual diff for binary files.
# Consult usage subroutine below for help.
#
# Copyright (C) 2021 Jerome Lelasseux [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
#########################################################################
use warnings;
use strict;
use Term::ANSIColor qw(colorstrip colored);
use Getopt::Long qw(GetOptions);
use File::Temp qw(tempfile);
use constant BLANK => "..";
use constant BUFSIZE => 64 * 1024; # 64kB
sub usage
{
print "USAGE: $0 [OPTIONS] FILE1 FILE2\n";
print "Show a side-by-side binary comparison of FILE1 and FILE2. Show byte modifications but also additions and deletions, whatever the number of changed bytes. Rely on the 'diff' external command such as found on Linux or Cygwin. The algorithm is not suited for large and very different files.\n";
print "Author: Jerome Lelasseux \@2021\n";
print "OPTIONS: \n";
print " --cols=N : display N columns of bytes.diff Default is 16.\n";
print " --no-color : don't colorize output. Needed if you view the output in an editor.\n";
print " --no-marker : don't use the change markers (+ for addition, - for deletion, * for modified).\n";
print " --no-ascii : don't show the ascii columns.\n";
print " --only-changes : only display lines with changes.\n";
print " --no-header : don't print the header line.\n";
exit;
}
# Command line arguments
my $maxCols=16;
my $noColor=0;
my $noMarker=0;
my $noAscii=0;
my $noCommon=0;
my $noHeader=0;
GetOptions(
'cols=i' => \$maxCols,
'no-ascii' => \$noAscii,
'no-color' => \$noColor,
'no-marker' => \$noMarker,
'only-changes' => \$noCommon,
'no-header' => \$noHeader
) or usage();
usage() unless ($#ARGV == 1);
my ($file1, $file2) = (@ARGV);
# Convert input files into hex lists
my $fileHex1 = createHexListFile($file1);
my $fileHex2 = createHexListFile($file2);
# Print a header line
if (! $noHeader)
{
my $asciiSpace = $noAscii ? "" : " " x $maxCols;
my $s = $noMarker ? " " : " ";
my $header="OFFSET ${s}00${s}01${s}02${s}03${s}04${s}05${s}06${s}07${s}08${s}09${s}0A${s}0B${s}0C${s}0D${s}0E${s}0F $asciiSpace OFFSET ${s}00${s}01${s}02${s}03${s}04${s}05${s}06${s}07${s}08${s}09${s}0A${s}0B${s}0C${s}0D${s}0E${s}0F\n";
if (!$noColor)
{
$header = colored($header, 'magenta');
}
print $header;
}
# Process diff -y output to get an easy-to-read side-by-side view
my $colIndex=0;
my $oldPtr=0;
my $newPtr=0;
my $oldLineBuffer = sprintf("0x%04X ", 0);
my $newLineBuffer = sprintf("0x%04X ", 0);
my $oldCharBuffer;
my $newCharBuffer;
my $isDeleting=0;
my $isAdding=0;
my $isUnchangedLine=1;
open(my $fh, '-|', qq(diff -y $fileHex1 $fileHex2)) or die $!;
while (<$fh>)
{
# Parse line by line the output of the 'diff -y' on the 2 hex list files.
# We expect:
# "xx | yy" for a modified byte
# " > yy" for an added byte
# "xx <" for a deleted byte
# "xx xx" for identicial bytes
my ($oldByte, $newByte);
my ($oldChar, $newChar);
if (/\|/)
{
# changed
if ($isDeleting || $isAdding)
{
printLine($colIndex);
}
$isAdding=0;
$isDeleting=0;
$isUnchangedLine=0;
/([a-fA-F0-9]+)([^a-fA-F0-9]+)([a-fA-F0-9]+)/;
$oldByte=formatByte($1, 3);
$oldChar=toPrintableChar($1,3);
$newByte=formatByte($3, 3);
$newChar=toPrintableChar($3,3);
$oldPtr++;
$newPtr++;
} elsif (/</)
{
# deleted in new
if ($isAdding)
{
printLine($colIndex);
}
$isAdding=0;
$isDeleting=1;
$isUnchangedLine=0;
/([a-fA-F0-9]+)/;
$oldByte=formatByte($1, 2);
$oldChar=toPrintableChar($1, 2);
$newByte=formatByte(BLANK, 2);
$newChar=colorize(".", 2);
$oldPtr++;
} elsif (/>/)
{
# added in new
if ($isDeleting)
{
printLine($colIndex);
}
$isAdding=1;
$isDeleting=0;
$isUnchangedLine=0;
/([a-fA-F0-9]+)/;
$oldByte=formatByte(BLANK, 1);
$oldChar=colorize(".", 1);
$newByte=formatByte($1, 1);
$newChar=toPrintableChar($1, 1);
$newPtr++;
}
else
{
# unchanged
if ($isDeleting || $isAdding)
{
printLine($colIndex);
}
$isDeleting=0;
$isAdding=0;
/([a-fA-F0-9]+)([^a-fA-F0-9]+)([a-fA-F0-9]+)/;
$oldByte=formatByte($1, 0);
$oldChar=toPrintableChar($1, 0);
$newByte=formatByte($3, 0);
$newChar=toPrintableChar($3, 0);
$oldPtr++;
$newPtr++;
}
# Append the bytes to the old and new buffers
$oldLineBuffer .= $oldByte;
$oldCharBuffer .= $oldChar;
$newLineBuffer .= $newByte;
$newCharBuffer .= $newChar;
$colIndex++;
if ($colIndex == $maxCols)
{
printLine();
}
}
printLine($colIndex); # Possible remaining line
#================================================================
# subroutines
#================================================================
# $1 a string representing a data byte
# $2 0=unchanged, 1=added, 2=deleted, 3=changed
# return the formatted string (color/maker)
sub formatByte
{
my ($byte, $type) = @_;
my $res;
if (!$noMarker)
{
if ($type == 0 || $byte eq BLANK) { $res = " " . $byte; } # unchanged or blank
elsif ($type == 1) { $res = " +" . $byte; } # added
elsif ($type == 2) { $res = " -" . $byte; } # deleted
elsif ($type == 3) { $res = " *" . $byte; } # changed
else { die "Error"; }
} else
{
$res = " " . $byte;
}
$res = colorize($res, $type);
return $res;
}
# $1 a string
# $2 0=unchanged, 1=added, 2=deleted, 3=changed
# return the colorized string according to $2
sub colorize
{
my ($res, $type) = @_;
if (!$noColor)
{
if ($type == 0) { } # unchanged
elsif ($type == 1) { $res = colored($res, 'bright_green'); } # added
elsif ($type == 2) { $res = colored($res, 'bright_red'); } # deleted
elsif ($type == 3) { $res = colored($res, 'bright_cyan'); } # changed
else { die "Error"; }
}
return $res;
}
# Print the buffered line
sub printLine
{
if (length($oldLineBuffer) <=10)
{
return; # no data to display
}
if (!$isUnchangedLine)
{
# Colorize and add a marker to the address of each line if some bytes are changed/added/deleted
my $prefix = substr($oldLineBuffer, 0, 6) . ($noMarker ? " " : "*");
$prefix = colored($prefix, 'bright_magenta') unless $noColor;
$oldLineBuffer =~ s/^......./$prefix/;
$prefix = substr($newLineBuffer, 0, 6) . ($noMarker ? " " : "*");
$prefix = colored($prefix, 'bright_magenta') unless $noColor;
$newLineBuffer =~ s/^......./$prefix/;
}
my $oldCBuf = $noAscii ? "" : $oldCharBuffer;
my $newCBuf = $noAscii ? "" : $newCharBuffer;
my $spacerChars = $noAscii ? "" : (" " x ($maxCols - $colIndex));
my $spacerData = ($noMarker ? " " : " ") x ($maxCols - $colIndex);
if (!($noCommon && $isUnchangedLine))
{
print "${oldLineBuffer}${spacerData} ${oldCBuf}${spacerChars} ${newLineBuffer}${spacerData} ${newCBuf}\n";
}
# reset buffers and counters
$oldLineBuffer = sprintf("0x%04X ", $oldPtr);
$newLineBuffer = sprintf("0x%04X ", $newPtr);
$oldCharBuffer = "";
$newCharBuffer = "";
$colIndex=0;
$isUnchangedLine=1;
}
# Convert a hex byte string into a printable char, or '.'.
# $1 = hex str such as A0
# $2 0=unchanged, 1=added, 2=deleted, 3=changed
# return the corresponding char, possibly colorized
sub toPrintableChar
{
my ($hexByte, $type) = @_;
my $char = chr(hex($hexByte));
$char = ($char =~ /[[:print:]]/) ? $char : ".";
return colorize($char, $type);
}
# Convert file $1 into a text file with 1 hex byte per line.
# $1=input file name
# return the output file name
sub createHexListFile
{
my ($inFileName) = @_;
my $buffer;
my $in_fh;
open($in_fh, "<:raw", $inFileName) || die "$0: cannot open $inFileName for reading: $!";
my ($out_fh, $filename) = tempfile();
while (my $nbReadBytes = read($in_fh, $buffer, BUFSIZE))
{
my @hexBytes = unpack("H2" x $nbReadBytes, $buffer);
foreach my $hexByte (@hexBytes)
{
print $out_fh "$hexByte\n" || die "couldn't write to $out_fh: $!";
}
}
close($in_fh);
return $filename;
}