-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_compare.py
162 lines (121 loc) · 3.86 KB
/
bit_compare.py
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
# Wesley Taylor 2014
# Program to parse xilinx bitfile header & do md5 checksum on the remainder
# Comparison done on the checksum generated
# To utilise. Run in idle. ignore reponse in console. type compare("bitfile1", "bitfile2")
import sys
import hashlib
def parse_bit(file):
"""A function to parse xilinx bit files, and seperate the bitstream from the header"""
length = ord(next(file)) << 8 | ord(next(file))
for i in range(length):
next(file)
length = ord(next(file)) << 8 | ord(next(file))
fields = {}
for i in "abcde":
assert next(file) == i
length = ord(next(file)) << 8 | ord(next(file))
field = []
for j in range(length):
field.append(next(file))
fields[i] = "".join(field)
return fields, list(file)
def bit_report(filename, report):
"""Add bit file hash to a report"""
file = open(filename).read()
file = iter(file)
file = parse_bit(file)
report.heading("Bit File Report", 0)
report.heading("Bit File Header", 1)
report.preformatted(filename)
table = [["Key", "Value"]]
for i in "abcde":
table.append([str(i), str(file[0][i])])
report.tabulate(table)
report.heading("Bit Stream", 1)
hash = hashlib.md5()
hash.update("".join(file[1]))
hash = "".join(["%x"%ord(i) for i in hash.digest()])
report.tabulate([
["length (bytes):", "sha1_hash"],
[str(len(file[1])), hash],
])
def bit_compare_report(filename1, filename2, report):
"""Add bit file hash to a report"""
report.heading("Bit File Comparison", 0)
file = open(filename1).read()
file = iter(file)
file = parse_bit(file)
report.heading("Original Header", 1)
report.preformatted(filename1)
table = [["Key", "Value"]]
for i in "abcde":
table.append([str(i), str(file[0][i])])
report.tabulate(table)
report.heading("Original Bitstream", 1)
original_hash = hashlib.md5()
original_hash.update("".join(file[1]))
original_hash = "".join(["%x"%ord(i) for i in original_hash.digest()])
report.tabulate([
["length (bytes):", "sha1_hash"],
[str(len(file[1])), original_hash],
])
file = open(filename2).read()
file = iter(file)
file = parse_bit(file)
report.heading("Rebuilt Header", 1)
report.preformatted(filename2)
table = [["Key", "Value"]]
for i in "abcde":
table.append([str(i), str(file[0][i])])
report.tabulate(table)
report.heading("Rebuilt Bitstream", 1)
hash = hashlib.md5()
hash.update("".join(file[1]))
hash = "".join(["%x"%ord(i) for i in hash.digest()])
report.tabulate([
["length (bytes):", "sha1_hash"],
[str(len(file[1])), hash],
])
report.heading("Bitstream Comparison", 1)
if hash == original_hash:
report.paragraph("Rebuild successful.")
else:
report.paragraph("Rebuild **Fail** file did not match.")
def compare(file1, file2):
"""Compare only the bitstreams of 2 bit files, but print the headers"""
original = file1
original = open(original, "rb").read()
original = iter(original)
original = parse_bit(original)
rebuild = file2
rebuild = open(rebuild, "rb").read()
rebuild = iter(rebuild)
rebuild = parse_bit(rebuild)
print "original header"
print "===============\n"
for i in "abcde":
print i, original[0][i]
print "\nrebuild header"
print "===============\n"
for i in "abcde":
print i, rebuild[0][i]
print "\noriginal bitstream"
print "==================\n"
print "length (bytes):", len(original[1])
a = hashlib.md5()
a.update("".join(original[1]))
print "".join(["%x"%ord(i) for i in a.digest()])
print "\nrebuild bitstream"
print "==================\n"
print "length (bytes):", len(rebuild[1])
b = hashlib.md5()
b.update("".join(rebuild[1]))
print "".join(["%x"%ord(i) for i in b.digest()])
if original[1] == rebuild[1]:
print "\nbitstreams match"
return True
else:
print "\nbitstreams differ"
return False
if __name__ == "__main__":
compare(sys.argv[1], sys.argv[2])