#!/usr/bin/python import sys, getopt, time def printDiff(pos, byte1, byte2): bits1 = '{0:08b}'.format(byte1) bits2 = '{0:08b}'.format(byte2) print "@%04Xh" % pos print "1:%02Xh, %sb" % (byte1, bits1) print "2:%02Xh, %sb" % (byte2, bits2) def compareFiles(fname1, fname2, offset = 0x0): f1 = open(fname1, "rb") f1.seek(offset) f2 = open(fname2, "rb") f2.seek(offset) while True: c1 = f1.read(1) c2 = f2.read(1) if not (c1 and c2): break b1 = ord(c1) b2 = ord(c2) if b1 != b2: pos = f1.tell() - offset printDiff(pos, b1, b2) pos = f1.tell() - offset print "bytes read: %02d" % pos f1.close() f2.close() def main(argv): file1 = '' file2 = '' offset = 0x0 watchmode = False opts, args = getopt.getopt(argv,"h1:2:",["file1=","file2=","offset=","watch"]) if len(opts) < 2: opts.append(("-h", "")) #print opts for opt, arg in opts: if opt == "-h": print 'bincmp.py [--offset ] [--watch] -1 -2 ' sys.exit(1) elif opt in ("-1", "--file1"): file1 = arg elif opt in ("-2", "--file2"): file2 = arg elif opt == "--offset": offset = int(arg, 16) elif opt == "--watch": watchmode = True delay = 5 print "1:", file1, " 2:", file2 while True: compareFiles(file1, file2, offset) if not watchmode: break print "------" time.sleep(delay) if __name__ == "__main__": main(sys.argv[1:])