zed.0xff.me

good old "fc /b" (byte-by-byte binary files compare) in ruby

Code (save as fc.rb)

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
#!/usr/bin/ruby
# simple binary file compare (c) http://zed.0xff.me
# like good old DOS "fc /b"

if ARGV.size < 2
  puts("[!] gimme at least two filenames")
  exit
end

handles = ARGV.map{ |fname| open(fname) }

while !handles.any?(&:eof)
  bytes = handles.map(&:readbyte)
  if bytes.uniq.size > 1
    @diff = true
    printf "%08x:"+" %02x"*handles.size+"\n", handles[0].pos-1, *bytes
  end
end

unless handles.all?(&:eof)
  @diff = true
  puts
  ARGV.each do |fname|
    printf "[!] %20s is %8d bytes long\n", fname, File.size(fname)
  end
end

puts "[.] all files are identical" unless @diff

Sample output

1
2
3
4
5
6
7
8
9
10
11
12
#fc.rb black.png white.png 
0000003e: 00 ff
0000003f: 00 ff
00000040: 00 ff
00000064: 23 92
00000065: 79 0a
00000066: 01 04
00000067: 05 02
00000068: 7f 83
00000069: 83 a9
0000006a: dc c5
0000006b: 13 01

Post a comment


(lesstile enabled - surround code blocks with ---)