-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinToMem_CLI.py
More file actions
80 lines (70 loc) · 1.85 KB
/
BinToMem_CLI.py
File metadata and controls
80 lines (70 loc) · 1.85 KB
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
import sys
import os
def bin_to_mem(infile, outfile):
binfile = open(infile, 'rb')
binfile_content = binfile.read(os.path.getsize(infile))
datafile = open(outfile, 'w')
for b in binfile_content:
array = []
array.append(b)
datafile.write(bytearray(array).hex() + '\n')
binfile.close()
datafile.close()
def bin_to_mem64(infile, outfile):
binfile = open(infile, 'rb')
binfile_content = binfile.read(os.path.getsize(infile))
datafile = open(outfile, 'w')
index = 0
b0 = 0
b1 = 0
b2 = 0
b3 = 0
b4 = 0
b5 = 0
b6 = 0
b7 = 0
for b in binfile_content:
if index == 0:
b0 = b
index = index + 1
elif index == 1:
b1 = b
index = index + 1
elif index == 2:
b2 = b
index = index + 1
elif index == 3:
b3 = b
index = index + 1
elif index == 4:
b4 = b
index = index + 1
elif index == 5:
b5 = b
index = index + 1
elif index == 6:
b6 = b
index = index + 1
elif index == 7:
b7 = b
index = 0
array = []
array.append(b7)
array.append(b6)
array.append(b5)
array.append(b4)
array.append(b3)
array.append(b2)
array.append(b1)
array.append(b0)
datafile.write(bytearray(array).hex() + '\n')
binfile.close()
datafile.close()
if __name__ == '__main__':
if len(sys.argv) == 4:
if sys.argv[3] == "64":
bin_to_mem64(sys.argv[1], sys.argv[2])
else :
bin_to_mem(sys.argv[1], sys.argv[2])
else:
print('Usage: %s binfile datafile' % sys.argv[0], sys.argv[1], sys.argv[2])