-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunyakuza.py
More file actions
54 lines (43 loc) · 1.55 KB
/
unyakuza.py
File metadata and controls
54 lines (43 loc) · 1.55 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
# original:
# https://aluigi.altervista.org/papers/quickbms-src-0.12.0.zip
# src/included/unyakuza.c
# ignored Line 34 to 69.
def unyakuza(input_data: bytes, src_size: int, dest_size: int) -> bytes:
decomp_buff = [0] * dest_size
src_idx = 0
dest_idx = 0
flag_bit_idx = 8
#bit 7 6 5 4 3 2 1 0
#index 0 1 2 3 4 5 6 7
flag = input_data[src_idx]
src_idx += 1
# print('{0:#X}'.format(len(decomp_buff)))
while (dest_idx < dest_size):
if (src_idx >= src_size):
return input_data
is_comp = ((flag & 0x80) != 0)
flag = flag << 1
flag_bit_idx -= 1
if (flag_bit_idx < 1):
flag = input_data[src_idx]
src_idx += 1
flag_bit_idx = 8
if (is_comp):
if (src_idx < 0x20):
#print('src_idx: {0:#X}'.format(src_idx))
pass
ref_offset = ((input_data[src_idx] >> 4) | (input_data[src_idx+1] << 4)) + 1
length = (input_data[src_idx] & 0x0F) + 3
for j in range(length):
if (dest_idx >= dest_size):
break
decomp_buff[dest_idx] = decomp_buff[dest_idx - ref_offset]
dest_idx += 1
src_idx += 2
else:
if (dest_idx >= dest_size):
break
decomp_buff[dest_idx] = input_data[src_idx]
dest_idx += 1
src_idx += 1
return bytearray(decomp_buff)