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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* ContainerXPK.cpp
* ----------------
* Purpose: Handling of XPK compressed modules
* Notes : (currently none)
* Authors: Olivier Lapicque
* OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "../common/FileReader.h"
#include "Container.h"
#include "Sndfile.h"
#include <stdexcept>
OPENMPT_NAMESPACE_BEGIN
#if !defined(MPT_WITH_ANCIENT)
#ifdef MPT_ALL_LOGGING
#define MMCMP_LOG
#endif
struct XPKFILEHEADER
{
char XPKF[4];
uint32be SrcLen;
char SQSH[4];
uint32be DstLen;
char Name[16];
uint32be Reserved;
};
MPT_BINARY_STRUCT(XPKFILEHEADER, 36)
struct XPK_error : public std::range_error
{
XPK_error() : std::range_error("invalid XPK data") { }
};
struct XPK_BufferBounds
{
const uint8 *pSrcBeg;
std::size_t SrcSize;
inline uint8 SrcRead(std::size_t index)
{
if(index >= SrcSize) throw XPK_error();
return pSrcBeg[index];
}
};
static int32 bfextu(std::size_t p, int32 bo, int32 bc, XPK_BufferBounds &bufs)
{
uint32 r;
p += bo / 8;
r = bufs.SrcRead(p); p++;
r <<= 8;
r |= bufs.SrcRead(p); p++;
r <<= 8;
r |= bufs.SrcRead(p);
r <<= bo % 8;
r &= 0xffffff;
r >>= 24 - bc;
return r;
}
static int32 bfexts(std::size_t p, int32 bo, int32 bc, XPK_BufferBounds &bufs)
{
uint32 r;
p += bo / 8;
r = bufs.SrcRead(p); p++;
r <<= 8;
r |= bufs.SrcRead(p); p++;
r <<= 8;
r |= bufs.SrcRead(p);
r <<= (bo % 8) + 8;
return mpt::rshift_signed(static_cast<int32>(r), 32 - bc);
}
static uint8 XPK_ReadTable(int32 index)
{
static constexpr uint8 xpk_table[] = {
2,3,4,5,6,7,8,0,3,2,4,5,6,7,8,0,4,3,5,2,6,7,8,0,5,4,6,2,3,7,8,0,6,5,7,2,3,4,8,0,7,6,8,2,3,4,5,0,8,7,6,2,3,4,5,0
};
if(index < 0) throw XPK_error();
if(static_cast<std::size_t>(index) >= std::size(xpk_table)) throw XPK_error();
return xpk_table[index];
}
static bool XPK_DoUnpack(const uint8 *src_, uint32 srcLen, std::vector<char> &unpackedData, int32 len)
{
if(len <= 0) return false;
int32 d0,d1,d2,d3,d4,d5,d6,a2,a5;
int32 cp, cup1, type;
std::size_t c;
std::size_t src;
std::size_t phist = 0;
unpackedData.reserve(std::min(static_cast<uint32>(len), std::min(srcLen, uint32_max / 20u) * 20u));
XPK_BufferBounds bufs;
bufs.pSrcBeg = src_;
bufs.SrcSize = srcLen;
src = 0;
c = src;
while(len > 0)
{
type = bufs.SrcRead(c+0);
cp = (bufs.SrcRead(c+4)<<8) | (bufs.SrcRead(c+5)); // packed
cup1 = (bufs.SrcRead(c+6)<<8) | (bufs.SrcRead(c+7)); // unpacked
//Log(" packed=%6d unpacked=%6d bytes left=%d dst=%08X(%d)\n", cp, cup1, len, dst, dst);
c += 8;
src = c+2;
if (type == 0)
{
// RAW chunk
if(cp < 0 || cp > len) throw XPK_error();
for(int32 i = 0; i < cp; ++i)
{
unpackedData.push_back(bufs.SrcRead(c + i));
}
c+=cp;
len -= cp;
continue;
}
if (type != 1)
{
#ifdef MMCMP_LOG
MPT_LOG_GLOBAL(LogDebug, "XPK", MPT_UFORMAT("Invalid XPK type! ({} bytes left)")(len));
#endif
break;
}
LimitMax(cup1, len);
len -= cup1;
cp = (cp + 3) & 0xfffc;
c += cp;
d0 = d1 = d2 = a2 = 0;
d3 = bufs.SrcRead(src); src++;
unpackedData.push_back(static_cast<char>(d3));
cup1--;
while (cup1 > 0)
{
if (d1 >= 8) goto l6dc;
if (bfextu(src,d0,1,bufs)) goto l75a;
d0 += 1;
d5 = 0;
d6 = 8;
goto l734;
l6dc:
if (bfextu(src,d0,1,bufs)) goto l726;
d0 += 1;
if (! bfextu(src,d0,1,bufs)) goto l75a;
d0 += 1;
if (bfextu(src,d0,1,bufs)) goto l6f6;
d6 = 2;
goto l708;
l6f6:
d0 += 1;
if (!bfextu(src,d0,1,bufs)) goto l706;
d6 = bfextu(src,d0,3,bufs);
d0 += 3;
goto l70a;
l706:
d6 = 3;
l708:
d0 += 1;
l70a:
d6 = XPK_ReadTable((8*a2) + d6 -17);
if (d6 != 8) goto l730;
l718:
if (d2 >= 20)
{
d5 = 1;
goto l732;
}
d5 = 0;
goto l734;
l726:
d0 += 1;
d6 = 8;
if (d6 == a2) goto l718;
d6 = a2;
l730:
d5 = 4;
l732:
d2 += 8;
l734:
while ((d5 >= 0) && (cup1 > 0))
{
d4 = bfexts(src,d0,d6,bufs);
d0 += d6;
d3 -= d4;
unpackedData.push_back(static_cast<char>(d3));
cup1--;
d5--;
}
if (d1 != 31) d1++;
a2 = d6;
l74c:
d6 = d2;
d6 >>= 3;
d2 -= d6;
}
}
return !unpackedData.empty();
l75a:
d0 += 1;
if (bfextu(src,d0,1,bufs)) goto l766;
d4 = 2;
goto l79e;
l766:
d0 += 1;
if (bfextu(src,d0,1,bufs)) goto l772;
d4 = 4;
goto l79e;
l772:
d0 += 1;
if (bfextu(src,d0,1,bufs)) goto l77e;
d4 = 6;
goto l79e;
l77e:
d0 += 1;
if (bfextu(src,d0,1,bufs)) goto l792;
d0 += 1;
d6 = bfextu(src,d0,3,bufs);
d0 += 3;
d6 += 8;
goto l7a8;
l792:
d0 += 1;
d6 = bfextu(src,d0,5,bufs);
d0 += 5;
d4 = 16;
goto l7a6;
l79e:
d0 += 1;
d6 = bfextu(src,d0,1,bufs);
d0 += 1;
l7a6:
d6 += d4;
l7a8:
if(bfextu(src, d0, 1, bufs))
{
d5 = 12;
a5 = -0x100;
} else
{
d0 += 1;
if(bfextu(src, d0, 1, bufs))
{
d5 = 14;
a5 = -0x1100;
} else
{
d5 = 8;
a5 = 0;
}
}
d0 += 1;
d4 = bfextu(src,d0,d5,bufs);
d0 += d5;
d6 -= 3;
if (d6 >= 0)
{
if (d6 > 0) d1 -= 1;
d1 -= 1;
if (d1 < 0) d1 = 0;
}
d6 += 2;
phist = unpackedData.size() + a5 - d4 - 1;
if(phist >= unpackedData.size())
throw XPK_error();
while ((d6 >= 0) && (cup1 > 0))
{
d3 = unpackedData[phist];
phist++;
unpackedData.push_back(static_cast<char>(d3));
cup1--;
d6--;
}
goto l74c;
}
static bool ValidateHeader(const XPKFILEHEADER &header)
{
if(std::memcmp(header.XPKF, "XPKF", 4) != 0)
{
return false;
}
if(std::memcmp(header.SQSH, "SQSH", 4) != 0)
{
return false;
}
if(header.SrcLen == 0)
{
return false;
}
if(header.DstLen == 0)
{
return false;
}
static_assert(sizeof(XPKFILEHEADER) >= 8);
if(header.SrcLen < (sizeof(XPKFILEHEADER) - 8))
{
return false;
}
return true;
}
static bool ValidateHeaderFileSize(const XPKFILEHEADER &header, uint64 filesize)
{
if(filesize < header.SrcLen - 8)
{
return false;
}
return true;
}
CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderXPK(MemoryFileReader file, const uint64 *pfilesize)
{
XPKFILEHEADER header;
if(!file.ReadStruct(header))
{
return ProbeWantMoreData;
}
if(!ValidateHeader(header))
{
return ProbeFailure;
}
if(pfilesize)
{
if(!ValidateHeaderFileSize(header, *pfilesize))
{
return ProbeFailure;
}
}
return ProbeSuccess;
}
bool UnpackXPK(std::vector<ContainerItem> &containerItems, FileReader &file, ContainerLoadingFlags loadFlags)
{
file.Rewind();
containerItems.clear();
XPKFILEHEADER header;
if(!file.ReadStruct(header))
{
return false;
}
if(!ValidateHeader(header))
{
return false;
}
if(loadFlags == ContainerOnlyVerifyHeader)
{
return true;
}
if(!file.CanRead(header.SrcLen - (sizeof(XPKFILEHEADER) - 8)))
{
return false;
}
containerItems.emplace_back();
containerItems.back().data_cache = std::make_unique<std::vector<char> >();
std::vector<char> & unpackedData = *(containerItems.back().data_cache);
#ifdef MMCMP_LOG
MPT_LOG_GLOBAL(LogDebug, "XPK", MPT_UFORMAT("XPK detected (SrcLen={} DstLen={}) filesize={}")(static_cast<uint32>(header.SrcLen), static_cast<uint32>(header.DstLen), file.GetLength()));
#endif
bool result = false;
try
{
result = XPK_DoUnpack(file.GetRawData<uint8>().data(), header.SrcLen - (sizeof(XPKFILEHEADER) - 8), unpackedData, header.DstLen);
} catch(mpt::out_of_memory e)
{
mpt::delete_out_of_memory(e);
return false;
} catch(const XPK_error &)
{
return false;
}
if(result)
{
containerItems.back().file = FileReader(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(unpackedData)));
}
return result;
}
#endif // !MPT_WITH_ANCIENT
OPENMPT_NAMESPACE_END
|