-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendian_fix.py
executable file
·42 lines (32 loc) · 927 Bytes
/
endian_fix.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
#!/usr/bin/env python
#
import struct
import sys
from binascii import hexlify
def int_to_bytes(value, length):
result = []
for i in range(0, length):
result.append(value >> (i * 8) & 0xff)
#result.reverse()
return bytearray(result)
def bytes_to_int(bts, length):
return struct.unpack("<I", bytes(bts))[0]
def bytes_to_int_r(bts, length):
bts_ba = bytearray(bts)
bts_ba.reverse()
bts = bytes(bts_ba)
return struct.unpack("<I", bts)[0]
infile = sys.argv[1]
outfile = sys.argv[2]
# loop over all memory
with open(infile, 'r+b') as data_in:
with open(outfile, 'w+b') as data_out:
for addr in range(0, 0x40000, 4):
h_addr = hex(addr)
buf = data_in.read(4)
if len(buf) == 0:
break
reversed_buf_i = bytes_to_int_r(buf, 4)
norm_reversed = int_to_bytes(reversed_buf_i, 4)
data_out.write(norm_reversed)
print("Done")