This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
resource_win31.cpp
327 lines (306 loc) · 8.83 KB
/
resource_win31.cpp
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
320
321
322
323
324
325
326
327
#include <string.h>
#include "resource_win31.h"
#include "util.h"
static const uint8_t _shuffleTable[256] = {
0xB2, 0x91, 0x49, 0xEE, 0x8C, 0xBC, 0x16, 0x0D, 0x07, 0x87, 0xCD, 0xB6, 0x4C, 0x44, 0x22, 0xB3,
0xAE, 0x96, 0xDF, 0x18, 0x7B, 0x28, 0x17, 0x9A, 0x74, 0x3C, 0x2E, 0x59, 0x69, 0x56, 0x38, 0x82,
0x7F, 0x25, 0x41, 0xC6, 0xE8, 0x8A, 0x86, 0x7A, 0xB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0x36,
0x85, 0xD0, 0x51, 0x6D, 0xC4, 0x3E, 0x92, 0xF1, 0xC7, 0x62, 0x79, 0xA1, 0x9B, 0x68, 0xF5, 0xE2,
0xAB, 0x0C, 0xCC, 0x78, 0xFC, 0x2A, 0xD8, 0x3A, 0xDD, 0x8F, 0x10, 0x29, 0xF4, 0x0A, 0xB8, 0xC3
};
static uint16_t decode(uint8_t *p, int size, uint16_t key) {
for (int i = 0; i < size; ++i) {
const uint8_t dl = 1 + (key >> 8);
const uint8_t al = _shuffleTable[dl];
const uint8_t dh = al ^ (key & 255);
p[i] ^= al;
key = (dh << 8) | dl;
}
return key;
}
struct Bitstream {
File *_f;
int _size;
uint16_t _bits;
int _len;
Bitstream()
: _f(0), _size(0), _bits(0), _len(0) {
}
void reset(File *f, int size) {
_f = f;
_size = size;
_bits = 0;
_len = 0;
}
uint8_t readByte() {
if (_len < 8) {
_bits <<= 8;
assert(_size > 0);
--_size;
_bits |= _f->readByte();
_len += 8;
}
_len -= 8;
return (_bits >> _len) & 255;
}
int readBit() {
if (_len == 0) {
assert(_size > 0);
--_size;
_bits = _f->readByte();
_len = 8;
}
--_len;
return (_bits & (1 << _len)) != 0 ? 1 : 0;
}
};
struct LzHuffman {
enum {
kCharsCount = 314,
kTableSize = kCharsCount * 2 - 1,
kHuffmanRoot = kTableSize - 1,
kMaxFreq = 0x8000
};
Bitstream _stream;
int _child[kTableSize];
int _freq[628];
int _parent[943];
unsigned char _historyBuffer[4096];
LzHuffman() {
memset(_child, 0, sizeof(_child));
memset(_freq, 0, sizeof(_freq));
memset(_parent, 0, sizeof(_parent));
}
void resetHuffTables() {
for (int i = 0; i < kCharsCount; ++i) {
_freq[i] = 1;
_child[i] = kTableSize + i;
_parent[kTableSize + i] = i;
}
for (int i = 0, j = kCharsCount; j <= kHuffmanRoot; i += 2, ++j) {
_freq[j] = _freq[i] + _freq[i + 1];
_child[j] = i;
_parent[i] = _parent[i + 1] = j;
}
_freq[kTableSize] = 0xFFFF;
_parent[kHuffmanRoot] = 0;
}
int getHuffCode() {
static const int base[] = { 0, 1, 4, 12, 24, 48 };
static const int count[] = { 0, 2, 5, 9, 12, 15 };
static const int length[] = { 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5 };
int index = _stream.readByte();
const int len = length[index >> 4];
const int code = base[len] + (index - count[len] * 16) / (1 << (5 - len));
for (int i = 0; i <= len; ++i) {
index = (index << 1) | _stream.readBit();
}
return (index & 63) | (code << 6);
}
int decodeChar() {
int i = _child[kHuffmanRoot];
while (i < kTableSize) {
i += _stream.readBit();
i = _child[i];
}
i -= kTableSize;
update(i);
return i;
}
void update(int num) {
if (_freq[kHuffmanRoot] == kMaxFreq) {
for (int j = 0, i = 0; j < kTableSize; ++j) {
if (_child[j] >= kTableSize) {
_freq[i] = (_freq[j] + 1) >> 1;
_child[i] = _child[j];
++i;
}
}
for (int j = 0, i = kCharsCount; i < kTableSize; j += 2, ++i) {
const int f = _freq[i] = _freq[j] + _freq[j + 1];
int index = i - 1;
while (_freq[index] > f) {
--index;
}
++index;
const int copySize = (i - index) * sizeof(int);
memmove(_freq + index + 1, _freq + index, copySize);
_freq[index] = f;
memmove(_child + index + 1, _child + index, copySize);
_child[index] = j;
}
for (int i = 0; i < kTableSize; ++i) {
const int j = _child[i];
if (j >= kTableSize) {
_parent[j] = i;
} else {
_parent[j] = _parent[j + 1] = i;
}
}
}
int p = _parent[kTableSize + num];
do {
++_freq[p];
const int i = _freq[p];
int index = p + 1;
if (_freq[index] < i) {
while (_freq[++index] < i) {}
--index;
_freq[p] = _freq[index];
_freq[index] = i;
const int k = _child[p];
_parent[k] = index;
if (k < kTableSize) {
_parent[k + 1] = index;
}
const int j = _child[index];
_child[index] = k;
_parent[j] = p;
if (j < kTableSize) {
_parent[j + 1] = p;
}
_child[p] = j;
p = index;
}
p = _parent[p];
} while (p != 0);
}
bool decode(uint8_t *out, int uncompressedSize) {
resetHuffTables();
memset(_historyBuffer, ' ', sizeof(_historyBuffer));
int offset = 4078;
int currentSize = 0;
while (currentSize < uncompressedSize) {
int chr = decodeChar();
if (chr < 256) {
*out++ = chr & 255;
_historyBuffer[offset++] = chr;
offset &= 0xFFF;
++currentSize;
} else {
const int baseOffset = (offset - getHuffCode() - 1) & 0xFFF;
const int size = chr - 253;
for (int i = 0; i < size; ++i) {
chr = _historyBuffer[(baseOffset + i) & 0xFFF];
*out++ = chr & 255;
_historyBuffer[offset++] = chr;
offset &= 0xFFF;
++currentSize;
}
}
}
return currentSize == uncompressedSize;
}
bool decompressEntry(File &f, const Win31BankEntry *e, uint8_t *out) {
f.seek(e->offset);
_stream.reset(&f, e->packedSize);
return decode(out, e->size);
}
};
const char *ResourceWin31::FILENAME = "BANK";
ResourceWin31::ResourceWin31(const char *dataPath)
: _dataPath(dataPath), _entries(0), _entriesCount(0) {
_f.open(FILENAME, dataPath);
_textBuf = 0;
memset(_stringsTable, 0, sizeof(_stringsTable));
}
ResourceWin31::~ResourceWin31() {
free(_entries);
free(_textBuf);
}
bool ResourceWin31::readEntries() {
uint8_t buf[32];
const int count = _f.read(buf, sizeof(buf));
if (count == 32 && memcmp(buf, "NL\00\00", 4) == 0) {
_entriesCount = READ_LE_UINT16(buf + 4);
debug(DBG_RESOURCE, "Read %d entries in win31 '%s'", _entriesCount, FILENAME);
_entries = (Win31BankEntry *)calloc(_entriesCount, sizeof(Win31BankEntry));
if (_entries) {
uint16_t key = READ_LE_UINT16(buf + 0x14);
for (int i = 0; i < _entriesCount; ++i) {
_f.read(buf, sizeof(buf));
key = decode(buf, sizeof(buf), key);
Win31BankEntry *e = &_entries[i];
memcpy(e->name, buf, 16);
const uint16_t flags = READ_LE_UINT16(buf + 16);
e->type = buf[19];
e->size = READ_LE_UINT32(buf + 20);
e->offset = READ_LE_UINT32(buf + 24);
e->packedSize = READ_LE_UINT32(buf + 28);
debug(DBG_RESOURCE, "Res #%03d '%s' type %d size %d (%d) offset 0x%x", i, e->name, e->type, e->size, e->packedSize, e->offset);
assert(e->size == 0 || flags == 0x80);
}
readStrings();
}
}
return _entries != 0;
}
uint8_t *ResourceWin31::loadFile(int num, uint8_t *dst, uint32_t *size) {
if (num > 0 && num < _entriesCount) {
Win31BankEntry *e = &_entries[num];
*size = e->size;
if (!dst) {
dst = (uint8_t *)malloc(e->size);
if (!dst) {
warning("Unable to allocate %d bytes", e->size);
return 0;
}
}
// check for unpacked data
char name[32];
snprintf(name, sizeof(name), "%03d_%s", num, e->name);
File f;
if (f.open(name, _dataPath) && f.size() == e->size) {
f.read(dst, e->size);
return dst;
}
LzHuffman lzHuf;
if (lzHuf.decompressEntry(_f, e, dst)) {
return dst;
}
}
warning("Unable to load resource #%d", num);
return 0;
}
void ResourceWin31::readStrings() {
uint32_t len, offset = 0;
_textBuf = loadFile(148, 0, &len);
while (1) {
const uint32_t sep = READ_LE_UINT32(_textBuf + offset); offset += 4;
const uint16_t num = sep >> 16;
if (num == 0xFFFF) {
break;
}
if (num < ARRAYSIZE(_stringsTable) && _stringsTable[num] == 0) {
_stringsTable[num] = (const char *)_textBuf + offset;
}
while (offset < len && _textBuf[offset++] != 0);
// strings are not always '\0' terminated
if (_textBuf[offset + 1] != 0) {
--offset;
}
}
}
const char *ResourceWin31::getString(int num) const {
return _stringsTable[num];
}
const char *ResourceWin31::getMusicName(int num) const {
switch (num) {
case 7:
return "y.mid";
case 138:
return "X.mid";
}
return 0;
}