-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (154 loc) · 4.42 KB
/
main.py
File metadata and controls
194 lines (154 loc) · 4.42 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
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
import argparse
import os
from pathlib import Path
from rich import print
from rich.console import Console
from rich.syntax import Syntax
console = Console()
# Builder
class Builder:
def __init__(
self,
path: str
):
self.path = path
def build_base(self):
pass
def create_data_table(self, config):
path = os.path.join(
self.path,
"DATA"
)
data_points = {}
current_addr = config["DATA_AD"]
total_len = 0
total_size = 0
for entity in os.listdir(path = path):
print("-----------------------")
print(f"Found entity -> \"{entity}\"")
# Construct data
# We need to calculate the size, so first
# just read the entity data
with open(
os.path.join(
path,
entity
),
"rb"
) as f:
data = f.read()
# bytes.__len__ is the size of the bytes
size = len(data)
total_size += size
name = entity.encode("ascii")
if len(name) <= 10:
bytes_name = name + b"\0"*(0xA - len(entity.encode("ascii")))
else:
print(f"[bold red]Entity name was too long \"{entity}\"[/bold red]")
quit(1)
entity_data = [
*bytes_name,
*current_addr.to_bytes(4, "big"),
*size.to_bytes(4, "big"),
]
total_len += len(bytearray(entity_data))
if total_len > 0x1C2:
raise Exception("Too many entities, exceeded table capacity")
data_points[entity] = entity_data
print(f"Loaded entity \[\"{entity}\"] - Size: {hex(size)} Addr: {hex(current_addr)}")
current_addr += size
print("-----------------------")
print(f"Table size: {hex(total_len)}")
# Pad
padding = 0x1C2 - total_len
data_points["padding"] = [*[0]*padding]
data_points["_size"] = [total_size]
return data_points
def run(self):
entities: list[str] = []
for entity in os.listdir(path = self.path):
entities.append(entity)
if sorted(entities) != sorted(["CODE", "CONF", "DATA"]):
print("[bold red]Invalid format. Entries not matched, make sure to follow the standard provided[/bold red]")
quit(1)
# The entries match
# Now we read the config
with open(
os.path.join(
self.path,
"CONF"
)
) as file:
config_lines = file.readlines()
# Process config
config = {}
for line in config_lines:
if line:
l = line.split("=")
config[l[0]] = int(l[1], 16)
if not all(k in config for k in ("CODE_AD","DATA_AD")):
print("[bold red]Invalid format. Config incomplete, make sure to follow the standard provided[/bold red]")
quit(1)
magic = ".EXE".encode("ascii")
into_bytes = lambda num: (hex(num >> 8), hex(num & 0xFF))
data_table = self.create_data_table(config)
with open(
os.path.join(
self.path,
"CODE"
),
"rb"
) as f:
code_lines = f.readlines()
final = [
*magic,
*[0]*12,
*config["CODE_AD"].to_bytes(4, "big"),
*len(bytearray(b''.join(code_lines))).to_bytes(4, "big"),
*[0]*8,
*config["DATA_AD"].to_bytes(4, "big"),
*data_table["_size"][0].to_bytes(4, "big"),
*[0]*216,
#*[int(_, 16) for _ in into_bytes(config["CODE_AD"])
]
print(f"""\
CODE_AD: {hex(config["CODE_AD"])}
CODE_SZ: {hex(len(bytearray(b''.join(code_lines))))}
DATA_AD: {hex(config["DATA_AD"])}
DATA_SZ: {hex(data_table["_size"][0])}""")
# Add data table
for ent in data_table:
for _ in data_table[ent]:
if ent[0] != '_':
final.append(_)
# Add more padding
final += [*[0]*0x3E]
# Add CODE binary
for l in code_lines:
final += l
try:
with open("out", "wb+") as f:
f.write(bytearray(final))
except ValueError:
print("[red bold]Failed to convert final to bytearray[/red bold]")
print(final)
quit(1)
except TypeError:
print("[red bold]Failed to convert final to bytearray. Something was of the wrong type[/red bold]")
print(final)
quit(1)
# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("path")
if __name__ == "__main__":
args = parser.parse_args()
# Define our target directory
target = Path(args.path)
if not target.exists():
print("[red bold]The target directory doesn't exist[/red bold]")
exit(1)
# Start the construction
builder = Builder(
path = args.path
)
builder.run()