forked from HackerN64/HackerSM64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslienc.c
319 lines (289 loc) · 5.5 KB
/
slienc.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Yay0 "slienc" compression tool
// originally decompiled by SimonTime
int main(int argc, const char **argv, const char **envp);
void encode();
void search(unsigned int a1, int a2, int *a3, unsigned int *a4);
int mischarsearch(unsigned char *a1, int a2, unsigned char *a3, int a4);
void initskip(unsigned char *a1, int a2);
void writeshort(short a1);
void writeint4(int a1);
unsigned short skip[256]; // idb
int cp; // weak
int ndp; // weak
FILE *fp; // idb
unsigned char *def;
unsigned short *pol;
int pp; // weak
int insize; // idb
int ncp; // weak
int npp; // weak
unsigned char *bz;
int dp; // idb
unsigned int *cmd;
int main(int argc, const char **argv, const char **envp)
{
char src[999];
char dest[999];
if (argc < 3)
{
fprintf(stderr, "slienc [infile] [outfile]\n");
return 1;
}
strcpy(src, argv[1]);
strcpy(dest, argv[2]);
if ((fp = fopen(src, "rb")) == NULL)
{
fprintf(stderr, "FILE OPEN ERROR![%s]\n", src);
return 1;
}
fseek(fp, 0, SEEK_END);
insize = ftell(fp);
fseek(fp, 0, SEEK_SET);
bz = malloc(insize);
size_t fread_result = fread(bz, 1, insize, fp);
fclose(fp);
for (int i = 0; src[i]; i++)
{
if (src[i] == '.')
{
src[i] = 0;
break;
}
}
if ((fp = fopen(dest, "wb")) == NULL)
{
fprintf(stderr, "FILE CREATE ERROR![%s]\n", dest);
exit(1);
}
encode();
fprintf(fp, "Yay0");
writeint4(insize);
writeint4(4 * cp + 16);
writeint4(2 * pp + 4 * cp + 16);
for (int i = 0; i < cp; i++)
writeint4(cmd[i]);
for (int i = 0; i < pp; i++)
writeshort(pol[i]);
fwrite(def, 1u, dp, fp);
fclose(fp);
return 0;
}
void encode()
{
unsigned int v0; // esi
unsigned int v1; // edi
int v2; // edx
int v3; // ebx
int v4; // edx
char v5; // [esp+Ch] [ebp-18h]
unsigned int v6; // [esp+10h] [ebp-14h]
unsigned int v7; // [esp+14h] [ebp-10h]
int v8; // [esp+18h] [ebp-Ch]
unsigned int a4; // [esp+1Ch] [ebp-8h]
int a3; // [esp+20h] [ebp-4h]
dp = 0;
pp = 0;
cp = 0;
npp = 4096;
ndp = 4096;
ncp = 4096;
cmd = calloc(0x4000u, 1u);
pol = malloc(2 * npp);
def = malloc(4 * ndp);
v0 = 0;
v6 = 1024;
v1 = 2147483648;
while ( insize > v0 )
{
if ( v6 < v0 )
v6 += 1024;
search(v0, insize, &a3, &a4);
if ( a4 <= 2 )
{
cmd[cp] |= v1;
def[dp++] = bz[v0++];
if ( ndp == dp )
{
ndp = dp + 4096;
def = realloc(def, dp + 4096);
}
}
else
{
search(v0 + 1, insize, &v8, &v7);
if ( v7 > a4 + 1 )
{
cmd[cp] |= v1;
def[dp++] = bz[v0++];
if ( ndp == dp )
{
ndp = dp + 4096;
def = realloc(def, dp + 4096);
}
v1 >>= 1;
if ( !v1 )
{
v1 = 2147483648;
v2 = cp++;
if ( cp == ncp )
{
ncp = v2 + 1025;
cmd = realloc(cmd, 4 * (v2 + 1025));
}
cmd[cp] = 0;
}
a4 = v7;
a3 = v8;
}
v3 = v0 - a3 - 1;
a3 = v0 - a3 - 1;
v5 = a4;
if ( a4 > 0x11 )
{
pol[pp++] = v3;
def[dp++] = v5 - 18;
if ( ndp == dp )
{
ndp = dp + 4096;
def = realloc(def, dp + 4096);
}
}
else
{
pol[pp++] = v3 | (((short)a4 - 2) << 12);
}
if ( npp == pp )
{
npp = pp + 4096;
pol = realloc(pol, 2 * (pp + 4096));
}
v0 += a4;
}
v1 >>= 1;
if ( !v1 )
{
v1 = 2147483648;
v4 = cp++;
if ( cp == ncp )
{
ncp = v4 + 1025;
cmd = realloc(cmd, 4 * (v4 + 1025));
}
cmd[cp] = 0;
}
}
if ( v1 != 0x80000000 )
++cp;
//fprintf(stderr, "IN=%d OUT=%d\n", insize, dp + 2 * pp + 4 * cp + 16);
}
void search(unsigned int a1, int a2, int *a3, unsigned int *a4)
{
unsigned int v4; // ebx
unsigned int v5; // esi
unsigned int *v6; // edi
unsigned int v7; // [esp+Ch] [ebp-10h]
int v8; // [esp+14h] [ebp-8h]
unsigned int v9; // [esp+18h] [ebp-4h]
v4 = 3;
v5 = 0;
if ( a1 > 0x1000 )
v5 = a1 - 4096;
v9 = 273;
if ( a2 - a1 <= 0x111 )
v9 = a2 - a1;
if ( v9 > 2 )
{
while ( a1 > v5 )
{
v7 = mischarsearch(&bz[a1], v4, &bz[v5], v4 + a1 - v5);
if ( v7 >= a1 - v5 )
break;
for ( ; v9 > v4; ++v4 )
{
if ( bz[v4 + v5 + v7] != bz[v4 + a1] )
break;
}
if ( v9 == v4 )
{
*a3 = v7 + v5;
goto LABEL_17;
}
v8 = v5 + v7;
++v4;
v5 += v7 + 1;
}
*a3 = v8;
if ( v4 > 3 )
{
--v4;
LABEL_17:
*a4 = v4;
return;
}
v6 = a4;
}
else
{
*a4 = 0;
v6 = a3;
}
*v6 = 0;
}
int mischarsearch(unsigned char *pattern, int patternlen, unsigned char *data, int datalen)
{
int result; // eax
int i; // ebx
int v6; // eax
int j; // ecx
result = datalen;
if ( patternlen <= datalen )
{
initskip(pattern, patternlen);
for ( i = patternlen - 1; ; i += v6 )
{
if ( pattern[patternlen - 1] == data[i] )
{
--i;
j = patternlen - 2;
if ( patternlen - 2 < 0 )
return i + 1;
while ( pattern[j] == data[i] )
{
--i;
if ( --j < 0 )
return i + 1;
}
v6 = patternlen - j;
if ( skip[data[i]] > patternlen - j )
v6 = skip[data[i]];
}
else
{
v6 = skip[data[i]];
}
}
}
return result;
}
void initskip(unsigned char *pattern, int len)
{
for (int i = 0; i < 256; i++)
skip[i] = len;
for (int i = 0; i < len; i++)
skip[pattern[i]] = len - i - 1;
}
void writeshort(short val)
{
fputc((val & 0xff00) >> 8, fp);
fputc((val & 0x00ff) >> 0, fp);
}
void writeint4(int val)
{
fputc((val & 0x00ff000000) >> 24, fp);
fputc((val & 0x0000ff0000) >> 16, fp);
fputc((val & 0x000000ff00) >> 8, fp);
fputc((val & 0x00000000ff) >> 0, fp);
}