-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathz64yaz0.c
274 lines (229 loc) · 7.3 KB
/
z64yaz0.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//version 1.0 (20050707)
//by shevious
//Thanks to thakis for yaz0dec 1.0.
typedef unsigned char u8;
typedef unsigned int u32;
#define MAX_RUNLEN (0xFF + 0x12)
// simple and straight encoding scheme for Yaz0
static u32
simpleEnc(u8 *src, int size, int pos, u32 *pMatchPos) {
int startPos = pos - 0x1000;
u32 numBytes = 1;
u32 matchPos = 0;
int end = size - pos;
// maximum runlength for 3 byte encoding
if (end > MAX_RUNLEN)
end = MAX_RUNLEN;
if (startPos < 0)
startPos = 0;
for (int i = startPos; i < pos; i++) {
int j;
for (j = 0; j < end; j++) {
if (src[i + j] != src[j + pos])
break;
}
if (j > numBytes) {
numBytes = j;
matchPos = i;
}
}
*pMatchPos = matchPos;
if (numBytes == 2)
numBytes = 1;
return numBytes;
}
// a lookahead encoding scheme for ngc Yaz0
static u32
nintendoEnc(u8 *src, int size, int pos, u32 *pMatchPos) {
u32 numBytes = 1;
static u32 numBytes1;
static u32 matchPos;
static int prevFlag = 0;
// if prevFlag is set, it means that the previous position
// was determined by look-ahead try.
// so just use it. this is not the best optimization,
// but nintendo's choice for speed.
if (prevFlag == 1) {
*pMatchPos = matchPos;
prevFlag = 0;
return numBytes1;
}
prevFlag = 0;
numBytes = simpleEnc(src, size, pos, &matchPos);
*pMatchPos = matchPos;
// if this position is RLE encoded, then compare to copying 1 byte and next position(pos+1) encoding
if (numBytes >= 3) {
numBytes1 = simpleEnc(src, size, pos + 1, &matchPos);
// if the next position encoding is +2 longer than current position, choose it.
// this does not guarantee the best optimization, but fairly good optimization with speed.
if (numBytes1 >= numBytes + 2) {
numBytes = 1;
prevFlag = 1;
}
}
return numBytes;
}
static int
encodeYaz0(u8 *src, u8 *dst, int srcSize) {
int srcPos = 0;
int dstPos = 0;
int bufPos = 0;
u8 buf[24]; // 8 codes * 3 bytes maximum
u32 validBitCount = 0; // number of valid bits left in "code" byte
u8 currCodeByte = 0; // a bitfield, set bits meaning copy, unset meaning RLE
while (srcPos < srcSize) {
u32 numBytes;
u32 matchPos;
numBytes = nintendoEnc(src, srcSize, srcPos, &matchPos);
if (numBytes < 3) {
// straight copy
buf[bufPos] = src[srcPos];
bufPos++;
srcPos++;
//set flag for straight copy
currCodeByte |= (0x80 >> validBitCount);
} else {
//RLE part
u32 dist = srcPos - matchPos - 1;
u8 byte1, byte2, byte3;
if (numBytes >= 0x12) { // 3 byte encoding
byte1 = 0 | (dist >> 8);
byte2 = dist & 0xFF;
buf[bufPos++] = byte1;
buf[bufPos++] = byte2;
// maximum runlength for 3 byte encoding
if (numBytes > MAX_RUNLEN)
numBytes = MAX_RUNLEN;
byte3 = numBytes - 0x12;
buf[bufPos++] = byte3;
} else { // 2 byte encoding
byte1 = ((numBytes - 2) << 4) | (dist >> 8);
byte2 = dist & 0xFF;
buf[bufPos++] = byte1;
buf[bufPos++] = byte2;
}
srcPos += numBytes;
}
validBitCount++;
// write eight codes
if (validBitCount == 8) {
dst[dstPos++] = currCodeByte;
for (int j = 0; j < bufPos; j++)
dst[dstPos++] = buf[j];
currCodeByte = 0;
validBitCount = 0;
bufPos = 0;
}
}
if (validBitCount > 0) {
dst[dstPos++] = currCodeByte;
for (int j = 0; j < bufPos; j++)
dst[dstPos++] = buf[j];
currCodeByte = 0;
validBitCount = 0;
bufPos = 0;
}
return dstPos;
}
void
decompress(u8 *src, u8 *dst, int uncompressedSize) {
int srcPlace = 0, dstPlace = 0; // current read/write positions
u32 validBitCount = 0; // number of valid bits left in "code" byte
u8 currCodeByte = 0;
while (dstPlace < uncompressedSize) {
// read new "code" byte if the current one is used up
if (validBitCount == 0) {
currCodeByte = src[srcPlace++];
validBitCount = 8;
}
if ((currCodeByte & 0x80) != 0) {
// straight copy
dst[dstPlace++] = src[srcPlace++];
} else {
// RLE part
u8 byte1 = src[srcPlace++];
u8 byte2 = src[srcPlace++];
u32 dist = ((byte1 & 0xF) << 8) | byte2;
u32 copySource = dstPlace - (dist + 1);
u32 numBytes = byte1 >> 4;
if (numBytes == 0) {
numBytes = src[srcPlace++] + 0x12;
} else {
numBytes += 2;
}
// copy run
for(int i = 0; i < numBytes; ++i) {
dst[dstPlace++] = dst[copySource++];
}
}
// use next bit from "code" byte
currCodeByte <<= 1;
validBitCount--;
}
}
int
main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
FILE *f = fopen(argv[i], "rb");
if (f == NULL) {
perror(argv[1]);
exit(1);
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
u8 *bufi = malloc(size);
fread(bufi, 1, size, f);
fclose(f);
if (size > 0x10
&& bufi[0] == 'Y'
&& bufi[1] == 'a'
&& bufi[2] == 'z'
&& bufi[3] == '0') {
long usize = (bufi[4] << 24)
| (bufi[5] << 16)
| (bufi[6] << 8)
| bufi[7];
u8 *bufo = malloc(usize);
decompress(bufi + 16, bufo, usize);
fwrite(bufo, usize, 1, stdout);
free(bufo);
} else {
// we don't know how big the "compressed" file could get,
// so over-allocate!
// modern systems have more RAM than the largest Yaz0 file, so...
u8 *bufo = malloc(size * 2);
// write 4 bytes yaz0 header
bufo[0] = 'Y';
bufo[1] = 'a';
bufo[2] = 'z';
bufo[3] = '0';
// write 4 bytes uncompressed size
bufo[4] = (size >> 24) & 0xFF;
bufo[5] = (size >> 16) & 0xFF;
bufo[6] = (size >> 8) & 0xFF;
bufo[7] = (size >> 0) & 0xFF;
// write 8 bytes unused dummy
bufo[8] = 0;
bufo[9] = 0;
bufo[10] = 0;
bufo[11] = 0;
bufo[12] = 0;
bufo[13] = 0;
bufo[14] = 0;
bufo[15] = 0;
long csize = encodeYaz0(bufi, bufo + 16, size) + 16;
// pad compressed file to be a multiple of 16 bytes.
long ceilsize = (csize + 15) & ~0xF;
for (long i = csize; i < ceilsize; i++) {
bufo[i] = 0;
}
fwrite(bufo, ceilsize, 1, stdout);
free(bufo);
}
free(bufi);
}
}