-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathea5split.py
More file actions
executable file
·54 lines (43 loc) · 1.31 KB
/
ea5split.py
File metadata and controls
executable file
·54 lines (43 loc) · 1.31 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
import sys
import struct
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
chunks = []
output = 'FILE1'
chunk_limit = 8192-6
for i in sys.argv[1:]:
if ':' in i:
addr = i.split(':')[0]
fname = i.split(':')[1]
chunks.append((int(addr, 16), fname))
else:
output = i
last_flag = 0xFFFF
file_idx = 0
block_idx = 0
for (addr, fname) in chunks:
with open(fname, 'rb') as fin:
file_idx = file_idx + 1
block_idx = 0
block_addr = addr
data_blocks = list(divide_chunks(fin.read(), chunk_limit))
for block in data_blocks:
block_idx = block_idx + 1
if file_idx == len(chunks) and block_idx == len(data_blocks):
last_flag = 0x0000
print(f'----------------{file_idx}:{block_idx}--')
print(f'filename: {output}')
print(f'flag: {hex(last_flag)}')
print(f'len: {hex(len(block))}')
print(f'addr: {hex(block_addr)}')
byte_data = bytearray(len(block) + 6)
byte_data[:6] = struct.pack('>HHH', last_flag, len(block), block_addr)
byte_data[6:] = block
with open(output, 'wb') as fout:
fout.write(byte_data)
# setup for next segment
block_addr = chunk_limit + block_addr
last_char = chr(ord(output[-1]) + 1)
output = output[:-1] + last_char