forked from CountDer3k/OrangeHexCompare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrangeHexCompare_No_Emoji.py
207 lines (184 loc) · 6.09 KB
/
OrangeHexCompare_No_Emoji.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Der3k Burrola
# July 2020
# Orange Hex Compare
import binascii
import os
from tkinter import Tk
from tkinter.filedialog import askopenfilename
class ByteHolder():
byteValue = ''
byteColor = "\033[92m"
byteOffset = -1
byteNextOffset = -1
byteFake = False
#-------------------
RED = "\033[91m"
BLACK = "\033[92m"
def createByteObject(value, offset, isFake):
bt = ByteHolder()
bt.byteValue = value
bt.byteOffset = hex(offset)
bt.byteNextOffset = hex(offset+1)
bt.byteFake = isFake
return bt
def getHexOf(file):
hexString = []
isBlank = False
x = 0
with open(file, 'rb') as f:
while(not isBlank):
# Reads one byte at a time
content = f.read(1)
# Converts to hex then to useable string
pw_bytes = binascii.hexlify(content)
pw_bytes = pw_bytes.decode("utf-8")
if(pw_bytes != ''):
bt = createByteObject(pw_bytes, x, False)
hexString.append(bt)
x = x + 1
else:
isBlank = True
return hexString
# takes a file in & then returns an array of arrays for the length of the file
def getHexOfInArray(file):
hexString = []
hexStringLine = []
isBlank = False
lineAmount = 0
with open(file, 'rb') as f:
while(not isBlank):
#Read one byte at a time
content = f.read(1)
# Converts to hex then to useable string
pw_bytes = binascii.hexlify(content)
pw_bytes = pw_bytes.decode("utf-8")
# Takes 16 bytes
if(lineAmount != 16):
# Add byte to an array
hexStringLine.append(pw_bytes)
lineAmount = lineAmount + 1
else:
# add array to final array
hexString.append(hexStringLine)
# Clear temp values
hexStringLine.clear()
lineAmount = 0
# When the last byte is blank break out of while loop
if(pw_bytes == ''):
isBlank = True
return hexString
def pickFile():
print()
Tk().withdraw()
file = askopenfilename()
return getHexOf(file)
def compareFiles(file1, file2):
info = []
textInfo = ''
count = 0
for i in range(len(file1)):
if(file1[i].byteValue != file2[i].byteValue and not file1[i].byteFake and not file2[i].byteFake):
count = count + 1
file1[i].byteColor = RED
file2[i].byteColor = RED
compareString = 'File 1: ' + file1[i].byteColor + file1[i].byteValue + BLACK + ' vs File 2: ' + file1[i].byteColor + file2[i].byteValue + BLACK
fullString = 'Byte at: ' + file1[i].byteColor + file1[i].byteOffset + BLACK + ' are different! ' + compareString
textInfo = textInfo + 'Byte at: ' + file1[i].byteOffset + ' are different! ' + 'File 1 Value: ' + file1[i].byteValue + ' vs File 2 Value: ' + file2[i].byteValue + '\n'
print(fullString)
info.append(textInfo)
info.append(count)
return info
def saveToString(data, file, i, ending):
hexString = data + file[i].byteColor + file[i].byteValue + "\033[92m" + ending
return hexString
def displayBothFilesInHex(file1, file2):
hexString = ''
hexString1 = ''
hexString2 = ''
largerFile = len(file1) if len(file1) > len(file2) else len(file2)
for i in range (largerFile):
if(i==0):
hexString = str(file1[i].byteOffset) + '\t'
hexString1 = saveToString(hexString1, file1, i, '')
hexString2 = saveToString(hexString2, file2, i, '')
if((i+1)%4==0):
hexString1= hexString1 + ' '
hexString2= hexString2 + ' '
if((i+1)%16==0 or i==len(file1)-1):
if(i==len(file1)-1):
hexString = hexString + (hexString1 + ' | ' + hexString2) + '\n'
else:
hexString = hexString + (hexString1 + ' | ' + hexString2) + '\n' + str(file1[i].byteNextOffset) + '\t'
hexString1 = ''
hexString2 = ''
print('File 1: | File 2:')
print(hexString)
def fixAlignmentof(file1, file2):
#?? Add blank characters at end of file1 if line doesn't align
files = []
# blank characters at end of files if they are different sizes
if(len(file1) != len(file2)):
if(len(file1) > len(file2)):
sizeToAdd = len(file1) - len(file2)
for x in range(sizeToAdd):
bt = createByteObject(' ', x, True)
file2.append(bt)
if(len(file1) < len(file2)):
sizeToAdd = len(file2) - len(file1)
for x in range(sizeToAdd):
bt = createByteObject(' ', x, True)
file1.append(bt)
files.append(file1)
files.append(file2)
return files
def writeDifferencesToText(file1, file2):
text_file = open("Diffrences.txt", "w")
differences = compareFiles(file1, file2)[0]
os.system('clear')
toText = ''
for i in differences:
toText = toText + i
n = text_file.write(toText)
text_file.close()
print('\nSuccessfully exported to Differences.txt')
def main():
file1Hex = pickFile()
file2Hex = pickFile()
files = fixAlignmentof(file1Hex, file2Hex)
file1Hex = files[0]
file2Hex = files[1]
isRunning = True
while(isRunning):
print('_______________________')
print(' Orange HexCompare ')
print('_______________________\n')
print('Select an option from below')
print('[1] Display files side-by-side')
print('[2] Display differences only')
print('[3] Export differences to text file')
print('[4] Exit')
answer = str(input())
os.system('clear')
if(answer == '1'):
print('--------------------------')
print(' File Comparison ')
print('--------------------------\n')
displayBothFilesInHex(file1Hex, file2Hex)
print('\n\n-------------------\n\n')
if(answer == '2'):
print('--------------------------')
print(' Offset Differences ')
print('--------------------------\n')
print('\nNumber of differences: ' + str(compareFiles(file1Hex, file2Hex)[1]))
print('\n\n-------------------\n\n')
if(answer == '3'):
writeDifferencesToText(file1Hex, file2Hex)
print('\n\n-------------------\n\n')
if(answer == '4'):
print('\n\n-------------------\n\n')
print('Good-bye')
isRunning = False
if(answer == 'Easter Egg'):
print('*********************************\n*** ****** ***** **** ***********\n*** ****** ********** ************\n*** ****** ***** **** **** ** *\n*** ****** ***** **** **** ** *\n*** ***** **** ***********\n*** ****** ***** **** *** ***** *\n*** ****** ***** **** **** *\n*** ****** ***** **** ***********\n*** ****** ***** **** ***********\n*** ****** ***** ****************\n*** ****** ***** **** ***********\n*********************************')
if __name__ == "__main__":
main()