-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompress.c
More file actions
201 lines (191 loc) · 6.32 KB
/
decompress.c
File metadata and controls
201 lines (191 loc) · 6.32 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
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define inbufsiz (1024*1024*1024)
#define matchlim 0x2000
enum mode { m_none, m_direct, m_long, m_short0, m_short1, m_short2, m_short3, m_done };
int readin(FILE *infile, uint8_t **ind)
{
uint8_t *indata = NULL;
int size = 0;
do {
indata = realloc(indata, size + inbufsiz);
//Read in a meg...
size += fread(indata, 1, inbufsiz, infile);
//..and check for more
} while (!feof(infile));
*ind = indata;
return size;
}
struct compnode {
enum mode type;
uint16_t offset;
uint16_t size;
uint8_t data;
};
enum mode getControl(uint8_t **indata)
{
static uint8_t control;
static int bits = 0;
if (!bits) {
control = **indata;
(*indata)++;
bits = 8;
}
if (control & 1) {
//Literal
control >>= 1;
bits--;
return m_direct;
}
control >>= 1;
bits--;
if (!bits) {
control = **indata;
(*indata)++;
bits = 8;
}
if (control & 1) {
//Long copy
control >>= 1;
bits--;
return m_long;
}
//Short copy
control >>= 1;
bits--;
if (!bits) {
control = **indata;
(*indata)++;
bits = 8;
}
int len = 0;
len = control & 1 ? 2 : 0;
control >>= 1;
bits--;
if (!bits) {
control = **indata;
(*indata)++;
bits = 8;
}
len += control & 1 ? 1 : 0;
control >>= 1;
bits--;
return m_short0 + len;
}
void decompress(uint8_t *indata, int insize, struct compnode *nodes)
{
for (int i = 0; i < insize; i++) {
switch (nodes->type = getControl(&indata)) {
case m_direct :
nodes->data = *indata++;
nodes++;
break;
case m_short0 :
case m_short1 :
case m_short2 :
case m_short3 :
nodes->offset = *indata++;
nodes++;
break;
case m_long :
nodes->size = *indata & 0x07;
nodes->offset = (*indata++ & 0xF8) >> 3;
nodes->offset |= *indata++ << 5;
//Special detect for the end phrase
if (!nodes->size && !nodes->offset) {
nodes->size--;
}
if (!nodes->size) {
nodes->size = *indata++;
} else {
nodes->size++;
}
nodes++;
break;
}
}
}
int getSize(struct compnode *nodes)
{
int size = 0;
for (; nodes->type != m_long || nodes->offset != 0 || nodes->size != 0; nodes++) {
switch (nodes->type) {
case m_direct :
size++;
break;
case m_short3 :
size += 5;
break;
case m_short2 :
size += 4;
break;
case m_short1 :
size += 3;
break;
case m_short0 :
size += 2;
break;
case m_long :
size += nodes->size + 1;
break;
}
}
return size;
}
int store(uint8_t *data, struct compnode *nodes)
{
int size;
uint8_t *start = data;
for (; nodes->type != m_long || nodes->offset != 0 || nodes->size != 0; nodes++) {
switch (nodes->type) {
case m_direct :
*data++ = nodes->data;
break;
case m_short3 :
size = 5; if (0)
case m_short2 :
size = 4; if (0)
case m_short1 :
size = 3; if (0)
case m_short0 :
size = 2;
for (int i = 0; i < size; i++) {
*data = *(data-(256-nodes->offset));
data++;
}
break;
case m_long :
size = nodes->size + 1;
for (int i = 0; i < size; i++) {
*data = *(data-(0x2000-nodes->offset));
data++;
}
break;
}
}
return data - start;
}
int main(int argc, char **argv)
{
if (argc < 3) {
puts("Usage: decompress infile outfile");
return 1;
}
FILE *infile = fopen(argv[1], "rb");
uint8_t *indata;
int insize = readin(infile, &indata);
fclose(infile);
struct compnode *nodes = malloc(sizeof(*nodes)*insize);
decompress(indata, insize, nodes);
int outsize = 0;
free(indata);
uint8_t *outdata = malloc(getSize(nodes));
outsize = store(outdata, nodes);
free(nodes);
FILE *outfile = fopen(argv[2], "wb");
fwrite(outdata, 1, outsize, outfile);
fclose(outfile);
free(outdata);
return 0;
}