forked from az64/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaz0.cs
203 lines (193 loc) · 6.45 KB
/
yaz0.cs
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
using System;
using System.Collections.Generic;
namespace MMRando
{
public partial class ROMFuncs
{
//compression
// reference: https://github.com/aboood40091/libyaz0
private static int FindMatch(byte[] src, byte b, int start, int end)
{
for (int i = start; i < end; i++)
{
if (src[i] == b)
{
return i;
};
}
return -1;
}
private static int[] Yaz0Search(byte[] src, int pos, int end)
{
int[] found = { 0, 1 };
if ((pos + 2) < end)
{
int search = pos - 0x1000;
if (search < 0) { search = 0; };
int cmp_end = pos + 0x111;
if (cmp_end > end) { cmp_end = end; };
while (search < pos)
{
search = FindMatch(src, src[pos], search, pos);
if (search == -1) { break; };
int cmp1 = search + 1;
int cmp2 = pos + 1;
while ((cmp2 < cmp_end) && (src[cmp1] == src[cmp2]))
{
cmp1++;
cmp2++;
};
int len = cmp2 - pos;
if (found[1] < len)
{
found[1] = len;
found[0] = search;
if (found[1] == 0x111)
{
break;
};
};
search++;
};
};
return found;
}
private static int[] Yaz0LA(byte[] src, int pos, int end, ref int[] LAfound, ref bool LAprev)
{
if (LAprev)
{
LAprev = false;
return LAfound;
};
LAprev = false;
int[] found = Yaz0Search(src, pos, end);
if (found[1] > 2)
{
LAfound = Yaz0Search(src, pos + 1, end);
if (LAfound[1] > found[1] + 2)
{
found[1] = 1;
LAprev = true;
};
};
return found;
}
private static byte[] Yaz0Compress(byte[] src)
{
int[] LAfound = { 0, 1 };
bool LAprev = false;
int src_pos = 0;
int src_end = src.Length;
List<byte> dest = new List<byte>();
//header
dest.Add((byte)0x59);
dest.Add((byte)0x61);
dest.Add((byte)0x7A);
dest.Add((byte)0x30);
dest.Add((byte)((src_end & 0xFF000000) >> 24));
dest.Add((byte)((src_end & 0xFF0000) >> 16));
dest.Add((byte)((src_end & 0xFF00) >> 8));
dest.Add((byte)(src_end & 0xFF));
for (int i = 0; i < 8; i++)
{
dest.Add((byte)0);
};
//do compression
int code_byte_pos = 0;
while (src_pos < src_end)
{
code_byte_pos = dest.Count;
dest.Add((byte)0);
for (int i = 0; i < 8; i++)
{
if (src_pos >= src_end) { break; };
int[] found = Yaz0LA(src, src_pos, src_end, ref LAfound, ref LAprev);
if (found[1] > 2)
{
int delta = src_pos - found[0] - 1;
if (found[1] < 0x12)
{
dest.Add((byte)((delta >> 8) | ((found[1] - 2) << 4)));
dest.Add((byte)(delta & 0xFF));
}
else
{
dest.Add((byte)(delta >> 8));
dest.Add((byte)(delta & 0xFF));
dest.Add((byte)((found[1] - 0x12) & 0xFF));
};
src_pos += found[1];
}
else
{
dest[code_byte_pos] |= (byte)(1 << (7 - i));
dest.Add(src[src_pos]);
src_pos++;
};
};
};
while (dest.Count % 16 != 0)
{
dest.Add((byte)0);
};
return dest.ToArray();
}
private static byte[] Yaz0Decompress(byte[] CmpFile)
{
if (Arr_ReadU32(CmpFile, 0) != 0x59617A30)
{
return null;
};
int dest_len = (int)Arr_ReadU32(CmpFile, 4);
byte[] dest = new byte[dest_len];
int src_pos = 16;
int src_len = CmpFile.Length;
int dest_pos = 0;
byte code = 0;
while ((src_pos < src_len) && (dest_pos < dest_len))
{
code = CmpFile[src_pos];
src_pos++;
for (int i = 0; i < 8; i++)
{
if ((src_pos >= src_len) || (dest_pos >= dest_len))
{
break;
};
if ((code & 0x80) > 0)
{
dest[dest_pos] = CmpFile[src_pos];
dest_pos++;
src_pos++;
}
else
{
byte b1 = CmpFile[src_pos];
src_pos++;
byte b2 = CmpFile[src_pos];
src_pos++;
int copy_from = dest_pos - (((b1 & 0xF) << 8) | b2) - 1;
int n = b1 >> 4;
if (n == 0)
{
n = CmpFile[src_pos] + 0x12;
src_pos++;
}
else
{
n += 2;
};
for (int j = 0; j < n; j++)
{
dest[dest_pos] = dest[copy_from];
dest_pos++;
copy_from++;
};
};
code <<= 1;
};
};
return dest;
}
}
}