-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path94.py
176 lines (146 loc) · 6.42 KB
/
94.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
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
import os
import sys
import json
def process_94_file_psxexe(kyuhuyon_file):
print("PS-X EXE | Reading the contents of a .94 file:", kyuhuyon_file)
with open(kyuhuyon_file, 'rb') as f:
data = f.read()
# Find positions for each data block
magicnum_start = 0x00
execution_start_address_start = 0x10
text_section_start_address_start = 0x18
text_section_size_start = 0x1C
stack_start_address_start = 0x30
region_specific_start = 0x4C
end = 0x800
# Extracting data
magicnum = data[magicnum_start:magicnum_start + 8].decode('ascii').strip('\0')
region_specific = data[region_specific_start:end].replace(b'\x00', b'').replace(b'\xd5', b"'").decode('utf-8').rstrip('\x00')
initial_pc = hex(int.from_bytes(data[0x10:0x14], byteorder='little'))
initial_gp = hex(int.from_bytes(data[0x14:0x18], byteorder='little'))
destination_address_in_ram = hex(int.from_bytes(data[0x18:0x1C], byteorder='little'))
filesize = hex(int.from_bytes(data[0x1C:0x20], byteorder='little'))
memfill_start_address = hex(int.from_bytes(data[0x28:0x2C], byteorder='little'))
memfill_size = hex(int.from_bytes(data[0x2C:0x30], byteorder='little'))
initial_sp_fp_base = hex(int.from_bytes(data[0x30:0x34], byteorder='little'))
initial_sp_fp_offs = hex(int.from_bytes(data[0x34:0x38], byteorder='little'))
# Create a dictionary for the data
output_data = {
"magicnum": magicnum,
"region_specific": region_specific,
"initial_pc": initial_pc,
"initial_gp": initial_gp,
"destination_address_in_ram": destination_address_in_ram,
"filesize": filesize,
"memfill_start_address": memfill_start_address,
"memfill_size": memfill_size,
"initial_sp_fp_base": initial_sp_fp_base,
"initial_sp_fp_offs": initial_sp_fp_offs
}
# Create a folder for the .94 file
folder_name = os.path.splitext(kyuhuyon_file)[0]
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# Export data to JSON file
json_file = os.path.join(folder_name, f'{folder_name}.json')
print(f'Export data to JSON file {folder_name}.json...')
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(output_data, f, ensure_ascii=False, indent=4)
print(".json processing is complete")
def process_94_file_img(kyuhuyon_file):
print("IMG | Reading the contents of a .glu file:", kyuhuyon_file)
with open(kyuhuyon_file, 'rb') as f:
data = f.read()
img_counter = 1
img_data = []
# Find the start of each IMG block
start_index = data.find(b'\x49\x4D\x47\x34')
if start_index == -1:
print("No data found for .img file")
return
while start_index != -1:
print(f"Data processing for .img file {img_counter}...")
end_index = data.find(b'\x56\x41\x47\x70', start_index + 1) # IMG block ends when VAG block begins
if end_index == -1:
end_index = len(data)
img_data.append((os.path.splitext(kyuhuyon_file)[0], data[start_index:end_index]))
start_index = data.find(b'\x49\x4D\x47\x34', end_index) # Find the start of the next IMG block
img_counter += 1
# Create a folder for the .glu file
folder_name = os.path.splitext(kyuhuyon_file)[0]
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# Export to .img
print("Exporting data to .img files...")
for prefix, img_block in img_data:
img_filename = os.path.join(folder_name, f'{prefix}.IMG')
with open(img_filename, 'wb') as f:
f.write(img_block)
print(".img processing is complete")
def process_94_file_vag(kyuhuyon_file):
print("VAG | Reading the contents of a .94 file:", kyuhuyon_file)
with open(kyuhuyon_file, 'rb') as f:
data = f.read()
vag_counter = 1
vag_data = []
vag_names = []
prefix = None
# Find the start of each VAG block
start_index = data.find(b'\x56\x41\x47\x70')
if start_index == -1:
print("No data found for .vag file")
return
# Find .vag file names
while start_index != -1:
# Find the name offset
name_offset = start_index + 0x20
name_length = data.find(b'\x00', name_offset) - name_offset
name = data[name_offset:name_offset + name_length].decode('utf-8').rstrip('\x00')
vag_names.append(name)
start_index = data.find(b'\x56\x41\x47\x70', start_index + 1)
start_index = data.find(b'\x56\x41\x47\x70')
while start_index != -1:
# Find the name offset
name_offset = start_index + 0x20
name_length = data.find(b'\x00', name_offset) - name_offset
name = data[name_offset:name_offset + name_length].decode('utf-8').rstrip('\x00')
vag_names.append(name)
# Find the end of VAG block
end_index = start_index + int.from_bytes(data[start_index + 0xE:start_index + 0x10], byteorder='big') + 0x10
vag_data.append((vag_names[vag_counter - 1], data[start_index:end_index]))
start_index = data.find(b'\x56\x41\x47\x70', end_index)
vag_counter += 1
# Create a folder for the .94 file
folder_name = os.path.splitext(kyuhuyon_file)[0]
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# Export to .vag
print("Exporting data to .vag files...")
for prefix, vag_block in vag_data:
prefix_parts = prefix.split('\\')
if len(prefix_parts) > 1:
subfolder_name = os.path.join(folder_name, prefix_parts[0])
if not os.path.exists(subfolder_name):
os.makedirs(subfolder_name)
vag_filename = os.path.join(subfolder_name, f'{prefix_parts[1]}.vag')
else:
vag_filename = os.path.join(folder_name, f'{prefix}.vag')
with open(vag_filename, 'wb') as f:
f.write(vag_block)
print(".vag processing is complete")
def is_exe_file(file_path):
with open(file_path, 'rb') as f:
first_bytes = f.read(8)
return b'PS-X' in first_bytes
if __name__ == "__main__":
if len(sys.argv) == 3 and sys.argv[1] == 'file':
kyuhuyon_file = sys.argv[2]
if is_exe_file(kyuhuyon_file):
process_94_file_psxexe(kyuhuyon_file)
process_94_file_img(kyuhuyon_file)
process_94_file_vag(kyuhuyon_file)
else:
print("Not a valid 94 file")
else:
print("Usage: python 94.py file <filename>")
print("Example: python glu.py file SLUS_011.94")