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"cxx . . $ `<file>/src/pt2_header.h`/
14 #include "../pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/
15
16 typedef struct XPKFILEHEADERcxx . . $ XPKFILEHEADER#
17 {
18 char XPKFcxx . . $ XPKFILEHEADER#XPKF.[4];
19 uint32_tcxx . . $ uint32_t# SrcLencxx . . $ XPKFILEHEADER#SrcLen.;
20 char SQSHcxx . . $ XPKFILEHEADER#SQSH.[4];
21 uint32_tcxx . . $ uint32_t# DstLencxx . . $ XPKFILEHEADER#DstLen.;
22 char Namecxx . . $ XPKFILEHEADER#Name.[16];
23 uint32_tcxx . . $ uint32_t# Reservedcxx . . $ XPKFILEHEADER#Reserved.;
24 } XPKFILEHEADERcxx . . $ XPKFILEHEADER#;
25
26 typedef struct XPK_BufferBoundscxx . . $ XPK_BufferBounds#
27 {
28 const uint8_tcxx . . $ uint8_t# *pSrcBegcxx . . $ XPK_BufferBounds#pSrcBeg.;
29 size_t SrcSizecxx . . $ XPK_BufferBounds#SrcSize.;
30 } XPK_BufferBoundscxx . . $ XPK_BufferBounds#;
31
32 static const uint8_tcxx . . $ uint8_t# xpk_tablecxx . . $ 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_tcxx . . $ uint8_t# SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(size_t index, XPK_BufferBoundscxx . . $ XPK_BufferBounds# *bufs)
40 {
41 ASSERTcxx . . $ `src/pt2_header.h:25:9`!(index < bufs->SrcSize);
42 if (index >= bufs->SrcSizecxx . . $ XPK_BufferBounds#SrcSize.)
43 return 0; // this is actually not how to do it, ugh...
44
45 return bufs->pSrcBegcxx . . $ XPK_BufferBounds#pSrcBeg.[index];
46 }
47
48 static inline int32_tcxx . . $ int32_t# bfextucxx . . $ bfextu(4ae90ef6547c5df2).(size_t p, int32_tcxx . . $ int32_t# bo, int32_tcxx . . $ int32_t# bc, XPK_BufferBoundscxx . . $ XPK_BufferBounds# *bufs)
49 {
50 p += (uint32_tcxx . . $ uint32_t#)bo >> 3;
51
52 uint32_tcxx . . $ uint32_t# r = SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs); p++;
53 r <<= 8;
54 r |= SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs); p++;
55 r <<= 8;
56 r |= SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs);
57 r <<= bo & 7;
58 r &= 0x00FFFFFF;
59 r >>= 24 - bc;
60
61 return r;
62 }
63
64 static inline int32_tcxx . . $ int32_t# bfextscxx . . $ bfexts(4ae90ef6547c5df2).(size_t p, int32_tcxx . . $ int32_t# bo, int32_tcxx . . $ int32_t# bc, XPK_BufferBoundscxx . . $ XPK_BufferBounds# *bufs)
65 {
66 p += (uint32_tcxx . . $ uint32_t#)bo >> 3;
67
68 uint32_tcxx . . $ uint32_t# r = SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs); p++;
69 r <<= 8;
70 r |= SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs); p++;
71 r <<= 8;
72 r |= SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(p, bufs);
73 r <<= (bo & 7) + 8;
74 r = (int32_tcxx . . $ int32_t#)r >> (32-bc);
75
76 return r;
77 }
78
79 static inline uint8_tcxx . . $ uint8_t# XPK_ReadTablecxx . . $ XPK_ReadTable(bc0fba5608a0bfe7).(int32_tcxx . . $ int32_t# index)
80 {
81 if (index < 0 || index >= (int32_tcxx . . $ int32_t#)sizeof (xpk_tablecxx . . $ xpk_table.))
82 return 0; // this is actually not how to do it, ugh...
83
84 return xpk_tablecxx . . $ xpk_table.[index];
85 }
86
87 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! XPK_DoUnpackcxx . . $ XPK_DoUnpack(56c10bf89e817345).(const uint8_tcxx . . $ uint8_t# *src_, uint32_tcxx . . $ uint32_t# srcLen, int32_tcxx . . $ int32_t# len, uint8_tcxx . . $ uint8_t# **out)
88 {
89 *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
90 if (len <= 0)
91 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
92
93 int32_tcxx . . $ int32_t# d0, d1, d2, d3, d4, d5, d6, a2, a5, cup1;
94 const uint32_tcxx . . $ uint32_t# unpackedLen = MINcxx . . $ `src/pt2_helpers.h:34:9`!((uint32_t)len, MINcxx . . $ `src/pt2_helpers.h:34:9`!(srcLen, UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:118:10`! / 20) * 20);
95
96 uint8_tcxx . . $ uint8_t# *unpackedData = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(unpackedLen);
97 if (unpackedData == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
98 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
99
100 uint8_tcxx . . $ uint8_t# *PtrEnd = unpackedData + unpackedLen;
101 uint8_tcxx . . $ uint8_t# *PtrOut = unpackedData;
102
103 XPK_BufferBoundscxx . . $ XPK_BufferBounds# bufs;
104 bufs.pSrcBegcxx . . $ XPK_BufferBounds#pSrcBeg. = src_;
105 bufs.SrcSizecxx . . $ XPK_BufferBounds#SrcSize. = srcLen;
106
107 size_t src = 0;
108 size_t c = src;
109 while (len > 0)
110 {
111 int32_tcxx . . $ int32_t# type = SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(c+0, &bufs);
112 int32_tcxx . . $ int32_t# cp = (SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(c+4, &bufs)<<8) | (SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(c+5, &bufs)); // packed
113 cup1 = (SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(c+6, &bufs)<<8) | (SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(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 freecxx . . $ free(5d22bdacc48458e8).(unpackedData);
123 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
124 }
125
126 for (int32_tcxx . . $ int32_t# i = 0; i < cp; i++)
127 *PtrOut++ = SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(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 = SrcReadcxx . . $ SrcRead(8864dcb5d8d2ce70).(src, &bufs); src++;
146 *PtrOut++ = (uint8_tcxx . . $ uint8_t#)d3;
147 cup1--;
148
149 while (cup1 > 0)
150 {
151 if (d1 >= 8) goto l6dc;
152 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l75a;
153 d0++;
154 d5 = 0;
155 d6 = 8;
156 goto l734;
157
158 l6dc:
159 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l726;
160 d0++;
161 if (!bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l75a;
162 d0++;
163 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l6f6;
164 d6 = 2;
165 goto l708;
166
167 l6f6:
168 d0++;
169 if (!bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l706;
170 d6 = bfextucxx . . $ bfextu(4ae90ef6547c5df2).(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_ReadTablecxx . . $ XPK_ReadTable(bc0fba5608a0bfe7).((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 = bfextscxx . . $ bfexts(4ae90ef6547c5df2).(src, d0, d6, &bufs);
203 d0 += d6;
204 d3 -= d4;
205 *PtrOut++ = (uint8_tcxx . . $ 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 freecxx . . $ free(5d22bdacc48458e8).(unpackedData);
224 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
225 }
226
227 *out = unpackedData;
228 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
229
230 l75a:
231 d0++;
232 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l766;
233 d4 = 2;
234 goto l79e;
235
236 l766:
237 d0++;
238 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l772;
239 d4 = 4;
240 goto l79e;
241
242 l772:
243 d0++;
244 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l77e;
245 d4 = 6;
246 goto l79e;
247
248 l77e:
249 d0++;
250 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs)) goto l792;
251 d0++;
252 d6 = bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 3, &bufs);
253 d0 += 3;
254 d6 += 8;
255 goto l7a8;
256
257 l792:
258 d0++;
259 d6 = bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 5, &bufs);
260 d0 += 5;
261 d4 = 16;
262 goto l7a6;
263
264 l79e:
265 d0++;
266 d6 = bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs);
267 d0++;
268 l7a6:
269 d6 += d4;
270 l7a8:
271 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(src, d0, 1, &bufs))
272 {
273 d5 = 12;
274 a5 = -0x100;
275 }
276 else
277 {
278 d0++;
279 if (bfextucxx . . $ bfextu(4ae90ef6547c5df2).(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 = bfextucxx . . $ bfextu(4ae90ef6547c5df2).(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 freecxx . . $ free(5d22bdacc48458e8).(unpackedData);
310 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
311 }
312
313 while (d6 >= 0 && cup1 > 0)
314 {
315 d3 = unpackedData[phist];
316 phist++;
317 *PtrOut++ = (uint8_tcxx . . $ uint8_t#)d3;
318 cup1--;
319 d6--;
320 }
321
322 goto l74c;
323 }
324
325 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ValidateHeadercxx . . $ ValidateHeader(e7ca0d67b02988c5).(XPKFILEHEADERcxx . . $ XPKFILEHEADER# *header)
326 {
327 if (memcmpcxx . . $ memcmp(36f949851efa3055).(header->XPKFcxx . . $ XPKFILEHEADER#XPKF., "XPKF", 4) != 0)
328 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
329
330 if (memcmpcxx . . $ memcmp(36f949851efa3055).(header->SQSHcxx . . $ XPKFILEHEADER#SQSH., "SQSH", 4) != 0)
331 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
332
333 if (header->SrcLencxx . . $ XPKFILEHEADER#SrcLen. == 0 || header->DstLencxx . . $ XPKFILEHEADER#DstLen. == 0)
334 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
335
336 if (header->SrcLencxx . . $ XPKFILEHEADER#SrcLen. < sizeof (XPKFILEHEADERcxx . . $ XPKFILEHEADER#)-8)
337 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
338
339 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
340 }
341
342 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ReadHeadercxx . . $ ReadHeader(869d75cb79a5a0d4).(FILEcxx . . $ FILE# *f, XPKFILEHEADERcxx . . $ XPKFILEHEADER# *header)
343 {
344 if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || freadcxx . . $ fread(738fc8ac1d711e58).(header, sizeof (XPKFILEHEADERcxx . . $ XPKFILEHEADER#), 1, f) != 1)
345 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
346
347 header->SrcLencxx . . $ XPKFILEHEADER#SrcLen. = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(header->SrcLen);
348 header->DstLencxx . . $ XPKFILEHEADER#DstLen. = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(header->DstLen);
349
350 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
351 }
352
353 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! detectXPKcxx . . $ detectXPK(dc9e5a301483124c).(FILEcxx . . $ FILE# *f)
354 {
355 XPKFILEHEADERcxx . . $ XPKFILEHEADER# header;
356
357 uint32_tcxx . . $ uint32_t# OldPos = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f);
358 rewindcxx . . $ rewind(8d4262d45fc644ab).(f);
359 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! result = ReadHeadercxx . . $ ReadHeader(869d75cb79a5a0d4).(f, &header);
360 fseekcxx . . $ fseek(543fae30d249f6b1).(f, OldPos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
361
362 if (result == falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!)
363 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
364
365 return ValidateHeadercxx . . $ ValidateHeader(e7ca0d67b02988c5).(&header);
366 }
367
368 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! unpackXPKcxx . . $ unpackXPK(6c33868bbccc6760).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# *filesize, uint8_tcxx . . $ uint8_t# **out)
369 {
370 XPKFILEHEADERcxx . . $ XPKFILEHEADER# header;
371
372 *filesize = 0;
373 *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
374
375 if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
376 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
377
378 uint32_tcxx . . $ uint32_t# oldPos = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f);
379
380 rewindcxx . . $ rewind(8d4262d45fc644ab).(f);
381
382 if (!ReadHeadercxx . . $ ReadHeader(869d75cb79a5a0d4).(f, &header))
383 {
384 fseekcxx . . $ fseek(543fae30d249f6b1).(f, oldPos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
385 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
386 }
387
388 if (!ValidateHeadercxx . . $ ValidateHeader(e7ca0d67b02988c5).(&header))
389 {
390 fseekcxx . . $ fseek(543fae30d249f6b1).(f, oldPos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
391 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
392 }
393
394 fseekcxx . . $ fseek(543fae30d249f6b1).(f, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!);
395 uint32_tcxx . . $ uint32_t# inputfilesize = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f) - sizeof (XPKFILEHEADERcxx . . $ XPKFILEHEADER#);
396 fseekcxx . . $ fseek(543fae30d249f6b1).(f, sizeof (XPKFILEHEADERcxx . . $ XPKFILEHEADER#), SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
397
398 uint8_tcxx . . $ uint8_t# *packedData = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(inputfilesize);
399 if (packedData == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
400 {
401 fseekcxx . . $ fseek(543fae30d249f6b1).(f, oldPos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
402 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
403 }
404
405 if (freadcxx . . $ fread(738fc8ac1d711e58).(packedData, 1, inputfilesize, f) != inputfilesize)
406 {
407 freecxx . . $ free(5d22bdacc48458e8).(packedData);
408 fseekcxx . . $ fseek(543fae30d249f6b1).(f, oldPos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
409 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
410 }
411
412 uint8_tcxx . . $ uint8_t# *unpackedData = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
413 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! result = XPK_DoUnpackcxx . . $ XPK_DoUnpack(56c10bf89e817345).(packedData, header.SrcLencxx . . $ XPKFILEHEADER#SrcLen. - (sizeof (XPKFILEHEADERcxx . . $ XPKFILEHEADER#) - 8), header.DstLencxx . . $ XPKFILEHEADER#DstLen., &unpackedData);
414 freecxx . . $ free(5d22bdacc48458e8).(packedData);
415
416 if (result == truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!)
417 {
418 *filesize = header.DstLencxx . . $ XPKFILEHEADER#DstLen.;
419 *out = unpackedData;
420 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
421 }
422 else
423 {
424 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
425 }
426 }