-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsllz.c
More file actions
151 lines (125 loc) · 3.38 KB
/
sllz.c
File metadata and controls
151 lines (125 loc) · 3.38 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
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
/*
Simple LZSS used in SEGA 0.1
by Luigi Auriemma
e-mail: aluigi@autistici.org
web: aluigi.org
Used in Yakuza 3 and Binary Domain.
bytes description
4 SLLZ
1 0 for little endian, 1 for big
1 ???
2 0x10
4 uncompressed size
4 compressed size
x compressed data
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ext ".dat"
#define unyakuza_bswap32(n) \
(((n & 0xff000000) >> 24) | \
((n & 0x00ff0000) >> 8) | \
((n & 0x0000ff00) << 8) | \
((n & 0x000000ff) << 24))
int unyakuza(unsigned char *in, int insz, unsigned char *out, int outsz, int check_head) {
typedef struct {
unsigned char sign[4];
unsigned char endian;
unsigned char dummy;
unsigned short zoff; // type or data offset?
unsigned int size;
unsigned int zsize;
} yakuza_t;
yakuza_t *yakuza;
int i,
d,
a,
b,
op;
unsigned char *p = in,
*o = out,
*il = in + insz,
*ol = out + outsz;
if(!in || (insz < 0) || !out || (outsz < 0)) return(-1);
if(check_head) {
if(insz < sizeof(yakuza_t)) return(-2);
yakuza = (yakuza_t *)p;
if(memcmp(yakuza->sign, "SLLZ", 4)) return(-3);
if(yakuza->endian) {
yakuza->zoff = (yakuza->zoff >> 8) | (yakuza->zoff << 8);
yakuza->size = unyakuza_bswap32(yakuza->size);
yakuza->zsize = unyakuza_bswap32(yakuza->zsize);
}
if(yakuza->zoff != 0x10) return(-4);
if(yakuza->size > outsz) return(-5);
if(yakuza->zsize > insz) return(-6);
p += 0x10;
}
b = *p++;
a = 8;
while(o < ol) {
if(p >= il) return(-7);
if(b & 0x80) op = 1;
else op = 0;
b <<= 1;
a--;
if(!a) {
b = *p++;
a = 8;
}
if(op) {
d = ((p[0] >> 4) | (p[1] << 4)) + 1;
for(i = (p[0] & 15) + 3; i > 0; i--) {
if(o >= ol) break;
*o = *(o - d);
o++;
}
p += 2;
} else {
if(o >= ol) break;
*o++ = *p++;
}
}
return(o - out);
}
int main(int argc, char *argv[]){
if(argc < 2) return 0;
FILE *f;
int size;
char output_name[100];
// output name
strcat(output_name, argv[1]);
strcat(output_name, ext);
// open file
f = fopen(argv[1], "rb");
if(f){
// get size
fseek(f, 0, SEEK_END);
int lSize = ftell(f);
//get uncompressed size
fseek(f, 8, SEEK_SET);
fread(&size, sizeof(int), 1, f);
fseek(f, 0, SEEK_SET);
// allocate buffer
char *buffer = malloc(lSize);
fread(buffer, lSize, 1, f);
// decompress
char *unc_buf = malloc(size);
unyakuza(buffer, lSize, unc_buf, size, 1);
// writing to file
FILE *o = fopen(output_name, "wb");
fwrite(unc_buf, 1, size, o);
printf("Done.");
// free memory
fclose(f);
fclose(o);
free(buffer);
free(unc_buf);
return 0;
} else{
printf("Can't get access to file.");
fclose(f);
return 0;
}
}