Skip to content

Instantly share code, notes, and snippets.

@XiGou
Last active July 24, 2023 02:51
Show Gist options
  • Save XiGou/7804222c5bf69d46f976f0cd3ba76a21 to your computer and use it in GitHub Desktop.
Save XiGou/7804222c5bf69d46f976f0cd3ba76a21 to your computer and use it in GitHub Desktop.
compare 2 binary files and print diff ranges
import hashlib
import sys
BLOCK_SIZE = 4096
def get_blocks(file):
while True:
block = file.read(BLOCK_SIZE)
if not block:
break
yield block
def diff(file1, file2):
diffs = []
f1 = open(file1, 'rb')
f2 = open(file2, 'rb')
offset = 0
for block1, block2 in zip(get_blocks(f1), get_blocks(f2)):
if block1 != block2:
diffs.append((offset, offset+len(block1)))
offset += len(block1)
f1.close()
f2.close()
return diffs
if __name__ == '__main__':
file1 = sys.argv[1]
file2 = sys.argv[2]
diffs = diff(file1, file2)
for start, end in diffs:
print(start, end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment