-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatb
executable file
·68 lines (54 loc) · 1.66 KB
/
catb
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
#!/usr/bin/env python
"""
catb: compare files by printing a column of bytes per file side-by-side
author: [email protected], 2013
license: GPL
"""
import sys
NARGS = len(sys.argv)
if NARGS < 2:
print("\n Usage: catb <filename1> <filename2> ... <filenameN>")
print("\n prints a byte column for each file, side-by-side")
sys.exit(0)
def main():
"""
For each file specified, print a stream of bytes in hex formatted
columns, pausing when a difference between the files is found
"""
files = []
for arg_no in [file_no + 1 for file_no in range(NARGS - 1)]:
files.append(open(sys.argv[arg_no], 'rb'))
nbytes = 0
file_bytes = read_bytes(files)
while any(file_bytes):
byte_str = " {0:10d}: ".format(nbytes)
nbytes += 1
byte_str += " ".join([byte_string(b) for b in file_bytes])
if len(set(file_bytes)) == 1:
print(byte_str)
else:
print(byte_str + " <= different")
#raw_input("...Enter to continue") # uncomment to pause upon diff
file_bytes = read_bytes(files)
for bfile in files:
bfile.close()
print("\ntotal nbytes: {0}".format(nbytes))
def byte_string(byte_val):
"""
Return string representation of byte, or N! for length 0 string
"""
if byte_val == "":
return "N!"
else:
return "{0:x}".format(ord(byte_val))
def read_bytes(files):
"""
Return a list of the next byte for each file in a file list
"""
file_bytes = []
for bfile in files:
a_byte = bfile.read(1)
file_bytes.append(a_byte)
return file_bytes
if __name__ == '__main__':
main()