-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
67 lines (60 loc) · 2.61 KB
/
build.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
import os
import struct
import hashlib
def get_files_size_hash():
files = []
for root, dirs, filenames in os.walk("topack"):
for f in filenames:
with open(os.path.join(root, f), "rb") as file:
files.append((os.path.join(f), os.path.getsize(os.path.join(root, f)), hashlib.sha256(file.read()).hexdigest()))
return files
# open outputpack.edata
with open("outputpack.edata", "wb") as outputpack:
# in first 0x10 put header with global pack size as FFFF to be filled later and number of input files
outputpack.write(struct.pack("<II", 0xFFFFFFFF, len(get_files_size_hash())))
# then we will have pointer jungle of 0x40 at next aligned address storing addresses of files inside data pack we will use offset
# so we will write 0x40 bytes of 0x00
outputpack.seek(0x10)
outputpack.write(b"\x00" * 0x40)
# now we will write files
# first we will write file header with filename, size and hash
# then we will write file content at next 0x10 aligned address
# we will store file address in pointer jungle
# we will repeat this for every file
# we will use offset to store file address in pointer jungle
offset = 0x50
alt_offset = 0x50
outputpack.seek(offset)
# write file header at offset
for file in get_files_size_hash():
offset = alt_offset
outputpack.seek(offset)
# build header as string without struct
# filename is 128 char long
# size is 4 bytes long
header = file[0].ljust(128, "\x00").encode("utf-8") + struct.pack("<I", file[1]) + file[2].encode("utf-8")
# write header
outputpack.write(header)
# write file content at next aligned address
outputpack.seek((outputpack.tell() + 0xF) & ~0xF)
with open("topack/" + file[0], "rb") as file_to_pack:
outputpack.write(file_to_pack.read())
# update offset with header size + filesize
alt_offset = outputpack.tell()
print(offset)
# store file address in pointer jungle
outputpack.seek(0x10 + (get_files_size_hash().index(file) * 4))
print(0x10 + (get_files_size_hash().index(file) * 4))
# print filename
print(file[0])
outputpack.write(struct.pack("<I", offset))
# get current file size in var
current_size = alt_offset
# write to 0x00
outputpack.seek(0x00)
# get current file size in var
current_size = alt_offset
# write to 0x00
outputpack.seek(0x00)
# write current file size
outputpack.write(struct.pack("<I", current_size))