Debian Codegraph @

src/modloaders/pt2_xpk_unpack.c

raw symbols SCIP
1 /* Taken from the OpenMPT project and converted to C.
2 ** OpenMPT shares the same license as the PT2 clone (BSD 3-clause),
3 ** so the licensing is compatible.
4 **
5 ** This is probably not 100% safe code, but it works.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include "../pt2_header.h"
14 #include "../pt2_helpers.h"
15
16 typedef struct XPKFILEHEADER
17 {
18 char XPKF[4];
19 uint32_t SrcLen;
20 char SQSH[4];
21 uint32_t DstLen;
22 char Name[16];
23 uint32_t Reserved;
24 } XPKFILEHEADER;
25
26 typedef struct XPK_BufferBounds
27 {
28 const uint8_t *pSrcBeg;
29 size_t SrcSize;
30 } XPK_BufferBounds;
31
32 static const uint8_t xpk_table[56] =
33 {
34 2,3,4,5,6,7,8,0,3,2,4,5,6,7,8,0,4,3,5,
35 2,6,7,8,0,5,4,6,2,3,7,8,0,6,5,7,2,3,4,
36 8,0,7,6,8,2,3,4,5,0,8,7,6,2,3,4,5,0
37 };
38
39 static inline uint8_t SrcRead(size_t index, XPK_BufferBounds *bufs)
40 {
41 ASSERT(index < bufs->SrcSize);
42 if (index >= bufs->SrcSize)
43 return 0; // this is actually not how to do it, ugh...
44
45 return bufs->pSrcBeg[index];
46 }
47
48 static inline int32_t bfextu(size_t p, int32_t bo, int32_t bc, XPK_BufferBounds *bufs)
49 {
50 p += (uint32_t)bo >> 3;
51
52 uint32_t r = SrcRead(p, bufs); p++;
53 r <<= 8;
54 r |= SrcRead(p, bufs); p++;
55 r <<= 8;
56 r |= SrcRead(p, bufs);
57 r <<= bo & 7;
58 r &= 0x00FFFFFF;
59 r >>= 24 - bc;
60
61 return r;
62 }
63
64 static inline int32_t bfexts(size_t p, int32_t bo, int32_t bc, XPK_BufferBounds *bufs)
65 {
66 p += (uint32_t)bo >> 3;
67
68 uint32_t r = SrcRead(p, bufs); p++;
69 r <<= 8;
70 r |= SrcRead(p, bufs); p++;
71 r <<= 8;
72 r |= SrcRead(p, bufs);
73 r <<= (bo & 7) + 8;
74 r = (int32_t)r >> (32-bc);
75
76 return r;
77 }
78
79 static inline uint8_t XPK_ReadTable(int32_t index)
80 {
81 if (index < 0 || index >= (int32_t)sizeof (xpk_table))
82 return 0; // this is actually not how to do it, ugh...
83
84 return xpk_table[index];
85 }
86
87 static bool XPK_DoUnpack(const uint8_t *src_, uint32_t srcLen, int32_t len, uint8_t **out)
88 {
89 *out = NULL;
90 if (len <= 0)
91 return false;
92
93 int32_t d0, d1, d2, d3, d4, d5, d6, a2, a5, cup1;
94 const uint32_t unpackedLen = MIN((uint32_t)len, MIN(srcLen, UINT32_MAX / 20) * 20);
95
96 uint8_t *unpackedData = (uint8_t *)malloc(unpackedLen);
97 if (unpackedData == NULL)
98 return false;
99
100 uint8_t *PtrEnd = unpackedData + unpackedLen;
101 uint8_t *PtrOut = unpackedData;
102
103 XPK_BufferBounds bufs;
104 bufs.pSrcBeg = src_;
105 bufs.SrcSize = srcLen;
106
107 size_t src = 0;
108 size_t c = src;
109 while (len > 0)
110 {
111 int32_t type = SrcRead(c+0, &bufs);
112 int32_t cp = (SrcRead(c+4, &bufs)<<8) | (SrcRead(c+5, &bufs)); // packed
113 cup1 = (SrcRead(c+6, &bufs)<<8) | (SrcRead(c+7, &bufs)); // unpacked
114
115 c += 8;
116 src = c + 2;
117
118 if (type == 0) // RAW chunk
119 {
120 if (cp < 0 || cp > len)
121 {
122 free(unpackedData);
123 return false;
124 }
125
126 for (int32_t i = 0; i < cp; i++)
127 *PtrOut++ = SrcRead(c + i, &bufs);
128
129 c += cp;
130 len -= cp;
131 continue;
132 }
133
134 if (type != 1)
135 break;
136
137 if (cup1 > len)
138 cup1 = len;
139
140 len -= cup1;
141 cp = (cp + 3) & 0xFFFC;
142 c += cp;
143
144 d0 = d1 = d2 = a2 = 0;
145 d3 = SrcRead(src, &bufs); src++;
146 *PtrOut++ = (uint8_t)d3;
147 cup1--;
148
149 while (cup1 > 0)
150 {
151 if (d1 >= 8) goto l6dc;
152 if (bfextu(src, d0, 1, &bufs)) goto l75a;
153 d0++;
154 d5 = 0;
155 d6 = 8;
156 goto l734;
157
158 l6dc:
159 if (bfextu(src, d0, 1, &bufs)) goto l726;
160 d0++;
161 if (!bfextu(src, d0, 1, &bufs)) goto l75a;
162 d0++;
163 if (bfextu(src, d0, 1, &bufs)) goto l6f6;
164 d6 = 2;
165 goto l708;
166
167 l6f6:
168 d0++;
169 if (!bfextu(src, d0, 1, &bufs)) goto l706;
170 d6 = bfextu(src, d0, 3, &bufs);
171 d0 += 3;
172 goto l70a;
173
174 l706:
175 d6 = 3;
176 l708:
177 d0++;
178 l70a:
179 d6 = XPK_ReadTable((a2*8) + d6 - 17);
180 if (d6 != 8) goto l730;
181 l718:
182 if (d2 >= 20)
183 {
184 d5 = 1;
185 goto l732;
186 }
187 d5 = 0;
188 goto l734;
189
190 l726:
191 d0++;
192 d6 = 8;
193 if (d6 == a2) goto l718;
194 d6 = a2;
195 l730:
196 d5 = 4;
197 l732:
198 d2 += 8;
199 l734:
200 while (d5 >= 0 && cup1 > 0)
201 {
202 d4 = bfexts(src, d0, d6, &bufs);
203 d0 += d6;
204 d3 -= d4;
205 *PtrOut++ = (uint8_t)d3;
206 cup1--;
207 d5--;
208 }
209
210 if (d1 != 31)
211 d1++;
212
213 a2 = d6;
214 l74c:
215 d6 = d2;
216 d6 >>= 3;
217 d2 -= d6;
218 }
219 }
220
221 if (PtrOut > PtrEnd)
222 {
223 free(unpackedData);
224 return false;
225 }
226
227 *out = unpackedData;
228 return true;
229
230 l75a:
231 d0++;
232 if (bfextu(src, d0, 1, &bufs)) goto l766;
233 d4 = 2;
234 goto l79e;
235
236 l766:
237 d0++;
238 if (bfextu(src, d0, 1, &bufs)) goto l772;
239 d4 = 4;
240 goto l79e;
241
242 l772:
243 d0++;
244 if (bfextu(src, d0, 1, &bufs)) goto l77e;
245 d4 = 6;
246 goto l79e;
247
248 l77e:
249 d0++;
250 if (bfextu(src, d0, 1, &bufs)) goto l792;
251 d0++;
252 d6 = bfextu(src, d0, 3, &bufs);
253 d0 += 3;
254 d6 += 8;
255 goto l7a8;
256
257 l792:
258 d0++;
259 d6 = bfextu(src, d0, 5, &bufs);
260 d0 += 5;
261 d4 = 16;
262 goto l7a6;
263
264 l79e:
265 d0++;
266 d6 = bfextu(src, d0, 1, &bufs);
267 d0++;
268 l7a6:
269 d6 += d4;
270 l7a8:
271 if (bfextu(src, d0, 1, &bufs))
272 {
273 d5 = 12;
274 a5 = -0x100;
275 }
276 else
277 {
278 d0++;
279 if (bfextu(src, d0, 1, &bufs))
280 {
281 d5 = 14;
282 a5 = -0x1100;
283 }
284 else
285 {
286 d5 = 8;
287 a5 = 0;
288 }
289 }
290
291 d0++;
292 d4 = bfextu(src, d0, d5, &bufs);
293 d0 += d5;
294 d6 -= 3;
295 if (d6 >= 0)
296 {
297 if (d6 > 0)
298 d1--;
299
300 d1--;
301 if (d1 < 0)
302 d1 = 0;
303 }
304 d6 += 2;
305
306 size_t phist = (size_t)(PtrOut-unpackedData) + a5 - d4 - 1;
307 if (phist >= (size_t)(PtrOut-unpackedData))
308 {
309 free(unpackedData);
310 return false;
311 }
312
313 while (d6 >= 0 && cup1 > 0)
314 {
315 d3 = unpackedData[phist];
316 phist++;
317 *PtrOut++ = (uint8_t)d3;
318 cup1--;
319 d6--;
320 }
321
322 goto l74c;
323 }
324
325 static bool ValidateHeader(XPKFILEHEADER *header)
326 {
327 if (memcmp(header->XPKF, "XPKF", 4) != 0)
328 return false;
329
330 if (memcmp(header->SQSH, "SQSH", 4) != 0)
331 return false;
332
333 if (header->SrcLen == 0 || header->DstLen == 0)
334 return false;
335
336 if (header->SrcLen < sizeof (XPKFILEHEADER)-8)
337 return false;
338
339 return true;
340 }
341
342 bool ReadHeader(FILE *f, XPKFILEHEADER *header)
343 {
344 if (f == NULL || fread(header, sizeof (XPKFILEHEADER), 1, f) != 1)
345 return false;
346
347 header->SrcLen = SWAP32(header->SrcLen);
348 header->DstLen = SWAP32(header->DstLen);
349
350 return true;
351 }
352
353 bool detectXPK(FILE *f)
354 {
355 XPKFILEHEADER header;
356
357 uint32_t OldPos = ftell(f);
358 rewind(f);
359 bool result = ReadHeader(f, &header);
360 fseek(f, OldPos, SEEK_SET);
361
362 if (result == false)
363 return false;
364
365 return ValidateHeader(&header);
366 }
367
368 bool unpackXPK(FILE *f, uint32_t *filesize, uint8_t **out)
369 {
370 XPKFILEHEADER header;
371
372 *filesize = 0;
373 *out = NULL;
374
375 if (f == NULL)
376 return false;
377
378 uint32_t oldPos = ftell(f);
379
380 rewind(f);
381
382 if (!ReadHeader(f, &header))
383 {
384 fseek(f, oldPos, SEEK_SET);
385 return false;
386 }
387
388 if (!ValidateHeader(&header))
389 {
390 fseek(f, oldPos, SEEK_SET);
391 return false;
392 }
393
394 fseek(f, 0, SEEK_END);
395 uint32_t inputfilesize = ftell(f) - sizeof (XPKFILEHEADER);
396 fseek(f, sizeof (XPKFILEHEADER), SEEK_SET);
397
398 uint8_t *packedData = (uint8_t *)malloc(inputfilesize);
399 if (packedData == NULL)
400 {
401 fseek(f, oldPos, SEEK_SET);
402 return false;
403 }
404
405 if (fread(packedData, 1, inputfilesize, f) != inputfilesize)
406 {
407 free(packedData);
408 fseek(f, oldPos, SEEK_SET);
409 return false;
410 }
411
412 uint8_t *unpackedData = NULL;
413 bool result = XPK_DoUnpack(packedData, header.SrcLen - (sizeof (XPKFILEHEADER) - 8), header.DstLen, &unpackedData);
414 free(packedData);
415
416 if (result == true)
417 {
418 *filesize = header.DstLen;
419 *out = unpackedData;
420 return true;
421 }
422 else
423 {
424 return false;
425 }
426 }