-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzea.py
executable file
·349 lines (273 loc) · 9.51 KB
/
zea.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#! /usr/bin/env python3
from __future__ import annotations
import argparse
from dataclasses import dataclass
import struct
LOG_VERBOSITY = 0
def log_print(*args, **kwargs):
if LOG_VERBOSITY > 0:
print(*args, **kwargs)
@dataclass
class FileHeader:
magic: bytes
decompressed_size: int
compressed_info_start: int
data_start: int
@staticmethod
def read(input) -> FileHeader:
return FileHeader(*struct.unpack_from(">4sIII", input))
def write(self, output):
struct.pack_into(
">4sIII",
output,
self.magic,
self.decompressed_size,
self.compressed_info_start,
self.data_start,
)
MIO_OFF_ADJ = 1
MIO_LEN_ADJ = 3
YAY_OFF_ADJ = 1
YAY_LEN_ADJ = 2
YAY_BIG_LEN_ADJ = 18
YAZ_OFF_ADJ = 1
YAZ_LEN_ADJ = 2
YAZ_BIG_LEN_ADJ = 18
def decompress_mio0(input: bytes, output: bytearray) -> int:
header = FileHeader.read(input)
if header.magic != b"MIO0":
raise BaseException(f"Wrong magic: {header.magic!r} is not 'MIO0'")
layout_off = layout_start = 0x10
info_off = info_start = header.compressed_info_start
data_off = data_start = header.data_start
layout_bit_index = 8
bytes_written = 0
while bytes_written < header.decompressed_size:
layout_bit_index -= 1
log_print(f"{layout_off} {layout_bit_index}, ", end="")
if input[layout_off] & (1 << layout_bit_index):
log_print(f"APPEND {input[data_off]:X}")
output.append(input[data_off])
data_off += 1
bytes_written += 1
else:
length = ((input[info_off] & 0xF0) >> 4) + MIO_LEN_ADJ
offset = ((input[info_off] & 0xF) << 8) + input[info_off + 1] + MIO_OFF_ADJ
log_print(
f"DECOMPRESS {length}, {offset}, {output[ - offset : - offset + length]}"
)
num = 0
while num < length:
output.append(output[bytes_written - offset + num])
num += 1
info_off += 2
bytes_written += length
if layout_bit_index == 0:
layout_bit_index = 8
layout_off += 1
# Consider adding checks here for offsets
if len(output) != header.decompressed_size:
# raise BaseException(
print(
f"Decompression failed: produced size {len(output)} is not equal to header-specified size {header.decompressed_size}"
)
return len(output)
def decompress_yay0(input: bytes, output: bytearray) -> int:
header = FileHeader.read(input)
if header.magic != b"YAY0":
raise BaseException(f"Wrong magic: {header.magic!r} is not 'YAY0'")
layout_off = layout_start = 0x10
info_off = info_start = header.compressed_info_start
data_off = data_start = header.data_start
layout_bit_index = 8
bytes_written = 0
while bytes_written < header.decompressed_size:
layout_bit_index -= 1
log_print(f"{layout_off} {layout_bit_index}, ", end="")
if input[layout_off] & (1 << layout_bit_index):
log_print(f"APPEND {chr(input[data_off])}")
output.append(input[data_off])
data_off += 1
bytes_written += 1
else:
length = (input[info_off] & 0xF0) >> 4
offset = ((input[info_off] & 0xF) << 8) + input[info_off + 1] + YAY_OFF_ADJ
if length == 0:
length = input[data_off] + YAY_BIG_LEN_ADJ
data_off += 1
log_print("BIG ", end="")
else:
length += YAY_LEN_ADJ
info_off += 2
log_print(
f"DECOMPRESS {length}, {offset}: {-offset} to {-offset + length}: {output[ - offset : - offset + length]}"
)
num = 0
while num < length:
output.append(output[bytes_written - offset + num])
num += 1
bytes_written += length
if layout_bit_index == 0:
layout_bit_index = 8
layout_off += 1
# Consider adding checks here for offsets
if len(output) != header.decompressed_size:
raise BaseException(
f"Decompression failed: produced size {len(output)} is not equal to header-specified size {header.decompressed_size}"
)
return len(output)
def decompress_yaz0(input: bytes, output: bytearray) -> int:
header = FileHeader.read(input)
if header.magic != b"YAZ0":
raise BaseException(f"Wrong magic: {header.magic!r} is not 'YAZ0'")
layout_off = layout_start = 0x10
data_off = layout_off + 1
layout_bit_index = 8
bytes_written = 0
while bytes_written < header.decompressed_size:
layout_bit_index -= 1
log_print(f"{layout_off} {layout_bit_index}, ", end="")
if input[layout_off] & (1 << layout_bit_index):
log_print(f"APPEND {chr(input[data_off])}")
output.append(input[data_off])
data_off += 1
bytes_written += 1
else:
length = (input[data_off] & 0xF0) >> 4
offset = ((input[data_off] & 0xF) << 8) + input[data_off + 1] + YAZ_OFF_ADJ
if length == 0:
length = input[data_off + 2] + YAZ_BIG_LEN_ADJ
data_off += 1
log_print("BIG ", end="")
else:
length += YAZ_LEN_ADJ
data_off += 2
log_print(
f"DECOMPRESS {length}, {offset}: {-offset} to {-offset + length}: {output[ - offset : - offset + length]}"
)
num = 0
while num < length:
output.append(output[bytes_written - offset + num])
num += 1
bytes_written += length
if layout_bit_index == 0:
layout_bit_index = 8
layout_off = data_off
data_off += 1
if len(output) != header.decompressed_size:
raise BaseException(
f"Decompression failed: produced size {len(output)} is not equal to header-specified size {header.decompressed_size}"
)
return len(output)
def compress_mio0(input: bytes, output: bytearray) -> int:
return 0
def compress_yay0(input: bytes, output: bytearray) -> int:
return 0
def compress_yaz0(input: bytes, output: bytearray) -> int:
return 0
decompress_funcs = {
"mio0": decompress_mio0,
"yay0": decompress_yay0,
"yaz0": decompress_yaz0,
}
compress_funcs = {
"mio0": compress_mio0,
"yay0": compress_yay0,
"yaz0": compress_yaz0,
}
test_mio0_decompressed = b"An itty bitty Hello Kitty ate the Yellow Jello"
test_mio0_compressed = (
b"MIO0"
+ bytes([0, 0, 0, 0x2E, 0, 0, 0, 0x14, 0, 0, 0, 0x1C])
+ bytes([0xFF, 0xBF, 0xBF, 0xEE])
+ bytes([0x20, 0x05, 0x20, 0x11, 0x10, 0x13, 0x10, 0x1A])
+ b"An itty bHello Kate the Yw J"
)
test_yay0_decompressed = b"An itty bitty Hello Kitty ate the Yellow Jello An itty bitty Hello Kitty ate the Yellow Jello"
test_yay0_compressed = (
b"YAY0"
+ bytes([0, 0, 0, 0x5D, 0, 0, 0, 0x18, 0, 0, 0, 0x22])
+ bytes([0xFF, 0xBF, 0xBF, 0xEE, 0, 0, 0, 0])
+ bytes([0x30, 0x05, 0x30, 0x11, 0x20, 0x13, 0x30, 0x1A, 0, 0x2E])
+ b"An itty bHello Kate the Yw J\x1C"
)
test_yaz0_decompressed = b"An itty bitty Hello Kitty ate the Yellow Jello An itty bitty Hello Kitty ate the Yellow Jello"
test_yaz0_compressed = (
b"YAZ0"
+ bytes([0, 0, 0, 0x5D, 0, 0, 0, 0, 0, 0, 0, 0])
+ b"\xFF"
+ b"An itty " #
+ b"\xBF"
+ b"b"
+ b"\x30\x05"
+ b"Hello " #
+ b"\xBF"
+ b"K"
+ b"\x30\x11"
+ b"ate th" #
+ b"\xEE"
+ b"e Y"
+ b"\x20\x13"
+ b"w J"
+ b"\x30\x1A"
+ b"\0"
+ b"\x00\x2E"
+ b"\x1C" #
)
tests = {
"mio0": {
"decompressed": test_mio0_decompressed,
"compressed": test_mio0_compressed,
},
"yay0": {
"decompressed": test_yay0_decompressed,
"compressed": test_yay0_compressed,
},
"yaz0": {
"decompressed": test_yaz0_decompressed,
"compressed": test_yaz0_compressed,
},
}
def test_decompression(algorithm: str):
input = tests[algorithm]["compressed"]
output = bytearray()
log_print(input)
decompress_funcs[algorithm](input, output)
log_print(output)
correct_output = tests[algorithm]["decompressed"]
if output == correct_output:
print(f"{algorithm}: decompression: SUCCESS")
else:
print(f"{algorithm}: decompression: FAILURE")
raise
def main() -> None:
# test_decompression("mio0")
# test_decompression("yay0")
# test_decompression("yaz0")
# return
description = "Compress or decompress a file with a given algorithm."
epilog = ""
parser = argparse.ArgumentParser(
description=description,
epilog=epilog,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("mode", choices=["decompress", "compress"], help="")
parser.add_argument("input", help="File to read")
parser.add_argument("output", help="File to write")
parser.add_argument(
"-a", "--algorithm", choices=["mio0", "yay0", "yaz0"], help="Algorithm to use"
)
args = parser.parse_args()
algorithm = args.algorithm
with open(args.input, "rb") as input_file:
with open(args.output, "wb") as output_file:
input = input_file.read()
output = bytearray()
if args.mode == "decompress":
decompress_funcs[algorithm](input, output)
else:
...
output_file.write(output)
if __name__ == "__main__":
main()