-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompress.c
164 lines (122 loc) · 3.34 KB
/
decompress.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
// This tool decompresses files in the format used by 'Streets of Rage 2'.
// ...This thing is really messy.
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static unsigned char *aaa_out_buffer;
static const unsigned char *orig_in_buffer;
static const unsigned char *in_buffer;
static unsigned char *out_buffer;
static void DecompressBlock(void)
{
const unsigned char *in_buffer_end = in_buffer + (in_buffer[0] | (in_buffer[1] << 8));
// printf("End = %p\n", in_buffer_end - in_buffer);
in_buffer += 2;
for (;;)
{
// printf("Offset %p\n", in_buffer - orig_in_buffer);
if (in_buffer == in_buffer_end)
return;
unsigned char command = *in_buffer++;
if (command & 0x80)
{
// Dictionary-match
unsigned int length = ((command & 0x60) >> 5) + 4;
const size_t offset = ((command & 0x1F) << 8) | *in_buffer++;
const unsigned char *dictionary = out_buffer - offset;
for (;;)
{
printf("Doing dictionary-match of 0x%X bytes from offset 0x%zX to offset 0x%zX\n", length, offset, out_buffer - aaa_out_buffer);
for (unsigned int i = 0; i < length; ++i)
*out_buffer++ = *dictionary++;
if ((*in_buffer & 0xE0) != 0x60)
break;
// printf("Offset %p\n", in_buffer - orig_in_buffer);
if (in_buffer == in_buffer_end)
return;
length = *in_buffer++ & 0x1F;
}
}
else if (command & 0x40)
{
// Byte-fill
unsigned int length = command & 0x2F;
if (command & 0x10)
length = (length << 8) | *in_buffer++;
length += 4;
unsigned char byte = *in_buffer++;
printf("Doing byte-fill of 0x%X for 0x%X bytes\n", byte, length);
for (unsigned int i = 0; i < length; ++i)
*out_buffer++ = byte;
}
else
{
// Uncompressed copy
unsigned int length = command & 0x1F;
if (command & 0x20)
length = (length << 8) | *in_buffer++;
printf("Doing uncompressed copy of 0x%X bytes\n", length);
for (unsigned int i = 0; i < length; ++i)
*out_buffer++ = *in_buffer++;
}
}
}
static void Decompress(const unsigned char *_in_buffer, size_t in_size, unsigned char **_out_buffer, size_t *out_size)
{
(void)in_size;
aaa_out_buffer = malloc(1024 * 64);
orig_in_buffer = _in_buffer;
in_buffer = _in_buffer;
out_buffer = aaa_out_buffer;
// for (;;)
// {
DecompressBlock();
// if (*in_buffer++ == 0)
// break;
// }
*_out_buffer = aaa_out_buffer;
*out_size = out_buffer - aaa_out_buffer;
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("\nStreets of Rage 2 decompressor by Clownacy\n\nUsage:\n %s [input file] [output file]\n\n", argv[0]);
}
else
{
const char *in_filename = argv[1];
char *out_filename;
if (argc > 2)
{
out_filename = argv[2];
}
else
{
out_filename = malloc(strlen(in_filename) + 5);
sprintf(out_filename, "%s.unc", in_filename);
}
FILE *in_file = fopen(in_filename, "rb");
if (in_file != NULL)
{
fseek(in_file, 0, SEEK_END);
size_t in_size = ftell(in_file);
rewind(in_file);
unsigned char *in_buffer = malloc(in_size);
fread(in_buffer, 1, in_size, in_file);
fclose(in_file);
unsigned char *out_buffer;
size_t out_size;
Decompress(in_buffer, in_size, &out_buffer, &out_size);
FILE *out_file = fopen(out_filename, "wb");
if (out_file != NULL)
{
fwrite(out_buffer, 1, out_size, out_file);
fclose(out_file);
}
}
}
return 0;
}