-
Notifications
You must be signed in to change notification settings - Fork 14
/
fileformat.cpp
644 lines (547 loc) · 18.2 KB
/
fileformat.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2016 Christoph Oelckers
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
//
//--------------------------------------------------------------------------
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wadext.h"
#include "fileformat.h"
#pragma warning(disable:4996)
//============================================================================
//
//
//
//============================================================================
struct Detector
{
bool(*checker)(const uint8_t *pbuffer, int len);
FileType ret;
};
bool IsMagic(const uint8_t *s, const char *magic, int len = 4)
{
return !memcmp(s, magic, len);
}
//============================================================================
//
// Doom patch detector
//
//============================================================================
bool isPatch(const uint8_t *data, int length)
{
if (length < 13) return false; // minimum length of a valid Doom patch
const patch_t *foo = (const patch_t *)data;
int height = foo->height;
int width = foo->width;
if (height <= 4096 && width <= 4096 && width < length / 4 && abs(foo->leftoffset) < 4096 && abs(foo->topoffset) < 4096)
{
// The dimensions seem like they might be valid for a patch, so
// check the column directory for extra security. At least one
// column must begin exactly at the end of the column directory,
// and none of them must point past the end of the patch.
bool gapAtStart = true;
int x;
for (x = 0; x < width; ++x)
{
uint32_t ofs = foo->columnofs[x];
if (ofs == (uint32_t)width * 4 + 8)
{
gapAtStart = false;
}
else if (ofs >= (uint32_t)length) // Need one byte for an empty column (but there's patches that don't know that!)
{
return false;
}
}
return !gapAtStart;
}
return false;
}
//============================================================================
//
//
//
//============================================================================
bool isPng(const uint8_t *data, int length)
{
const uint32_t *dwdata = (const uint32_t*)data;
if (length < 16) return false;
// Check up to the IHDR so that this rejects Apple's homegrown image format that masquerades as a PNG but isn't one.
return
dwdata[0] == MAKE_ID(137, 'P', 'N', 'G') &&
dwdata[1] == MAKE_ID(13, 10, 26, 10) &&
dwdata[2] == MAKE_ID(0, 0, 0, 13) &&
dwdata[3] == MAKE_ID('I', 'H', 'D', 'R');
}
//============================================================================
//
//
//
//============================================================================
bool isJpg(const uint8_t *data, int length)
{
return (*data == 0xff && IsMagic(data + 6, "JFIF"));
}
//============================================================================
//
//
//
//============================================================================
bool isTGA(const uint8_t *data, int length)
{
const TGAHeader *hdr = (const TGAHeader *)data;
if (length < sizeof(TGAHeader)) return false;
// Not much that can be done here because TGA does not have a proper
// header to be identified with.
if (hdr->has_cm != 0 && hdr->has_cm != 1) return false;
if (hdr->width <= 0 || hdr->height <= 0) return false;
if (hdr->bpp != 8 && hdr->bpp != 15 && hdr->bpp != 16 && hdr->bpp != 24 && hdr->bpp != 32) return false;
if (hdr->img_type <= 0 || hdr->img_type > 11) return false;
if (hdr->img_type >= 4 && hdr->img_type <= 8) return false;
if ((hdr->img_desc & 16) != 0) return false;
return true;
}
//============================================================================
//
//
//
//============================================================================
bool isPCX(const uint8_t *data, int length)
{
const PCXHeader *hdr = (const PCXHeader*)data;
if (length < sizeof(PCXHeader)) return false;
if (hdr->manufacturer != 10 || hdr->encoding != 1) return false;
if (hdr->version != 0 && hdr->version != 2 && hdr->version != 3 && hdr->version != 4 && hdr->version != 5) return false;
if (hdr->bitsPerPixel != 1 && hdr->bitsPerPixel != 8) return false;
if (hdr->bitsPerPixel == 1 && hdr->numColorPlanes != 1 && hdr->numColorPlanes != 4) return false;
if (hdr->bitsPerPixel == 8 && hdr->uint8_tsPerScanLine != ((hdr->xmax - hdr->xmin + 2)&~1)) return false;
for (int i = 0; i < 54; i++)
{
if (hdr->padding[i] != 0) return false;
}
return true;
}
//============================================================================
//
//
//
//============================================================================
bool isDDS(const uint8_t *data, int length)
{
const DDSFileHeader *Header = (const DDSFileHeader*)data;
if (length < sizeof(DDSFileHeader)) return false;
return Header->Magic == ID_DDS &&
(Header->Desc.Size == sizeof(DDSURFACEDESC2) || Header->Desc.Size == ID_DDS) &&
Header->Desc.PixelFormat.Size == sizeof(DDPIXELFORMAT) &&
(Header->Desc.Flags & (DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT)) == (DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT) &&
Header->Desc.Width != 0 &&
Header->Desc.Height != 0;
}
//============================================================================
//
//
//
//============================================================================
bool isImgz(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "IMGZ");
}
//============================================================================
//
//
//
//============================================================================
bool isMid(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "MThd");
}
//============================================================================
//
//
//
//============================================================================
bool isMus(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "MUS\x1a");
}
//============================================================================
//
//
//
//============================================================================
bool isIt(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "IMPM");
}
//============================================================================
//
//
//
//============================================================================
bool isS3m(const uint8_t *pbuffer, int len)
{
return len >= 0x30 && IsMagic(pbuffer+0x3c, "SCRM");
}
//============================================================================
//
//
//
//============================================================================
bool isXt(const uint8_t *pbuffer, int len)
{
return len >= 0x34 && IsMagic(pbuffer + 0x26, "FastTracker", 11);
}
//============================================================================
//
//
//
//============================================================================
bool isMod(const uint8_t *pbuffer, int len)
{
if (len < 1084) return false;
const uint8_t *s = pbuffer + 1080;
return (IsMagic(s, "M.K.") || IsMagic(s, "M!K!") || IsMagic(s, "M&K!") || IsMagic(s, "N.T.") ||
IsMagic(s, "CD81") || IsMagic(s, "OKTA") || IsMagic(s, "16CN") || IsMagic(s, "32CN") ||
((s[0] == 'F') && (s[1] == 'L') && (s[2] == 'T') && (s[3] >= '4') && (s[3] <= '9')) ||
((s[0] >= '2') && (s[0] <= '9') && (s[1] == 'C') && (s[2] == 'H') && (s[3] == 'N')) ||
((s[0] == '1') && (s[1] >= '0') && (s[1] <= '9') && (s[2] == 'C') && (s[3] == 'H')) ||
((s[0] == '2') && (s[1] >= '0') && (s[1] <= '9') && (s[2] == 'C') && (s[3] == 'H')) ||
((s[0] == '3') && (s[1] >= '0') && (s[1] <= '2') && (s[2] == 'C') && (s[3] == 'H')) ||
((s[0] == 'T') && (s[1] == 'D') && (s[2] == 'Z') && (s[3] >= '4') && (s[3] <= '9'))
);
}
//============================================================================
//
//
//
//============================================================================
bool isSpc(const uint8_t *pbuffer, int len)
{
return len >= 27 && IsMagic(pbuffer, "SNES - SPC700 Sound File Data v0.30\x1A\x1A", 27);
}
//============================================================================
//
//
//
//============================================================================
bool isHmi(const uint8_t *pbuffer, int len)
{
return len >= 12 && IsMagic(pbuffer, "HMI-MIDISONG", 12);
}
//============================================================================
//
//
//
//============================================================================
bool isHmp(const uint8_t *pbuffer, int len)
{
return len >= 12 && IsMagic(pbuffer, "HMIMIDIP", 8);
}
//============================================================================
//
//
//
//============================================================================
bool isXmi(const uint8_t *pbuffer, int len)
{
return len >= 12 &&
((IsMagic(pbuffer, "FORM") && IsMagic(pbuffer + 8, "XDIR")) ||
(IsMagic(pbuffer, "FORM") && IsMagic(pbuffer + 8, "XMID")) ||
(IsMagic(pbuffer, "CAT ") && IsMagic(pbuffer + 8, "XMID")));
}
//============================================================================
//
// Doom sound format has virtually no reliable features to identify.
// All we can go by is to check if the little info in the header makes sense.
//
//============================================================================
bool isDoomSound(const uint8_t *data, int len)
{
if (len < 8) return false;
uint32_t dmxlen = GetInt(data + 4);
int32_t freq = GetShort(data + 2) & 0xffff;
return data[0] == 3 && data[1] == 0 /*&& (freq == 0 || freq == 11025 || freq == 22050 || freq == 44100)*/ && dmxlen <= unsigned(len - 8);
}
//============================================================================
//
//
//
//============================================================================
bool isWav(const uint8_t *pbuffer, int len)
{
return len > 16 && IsMagic(pbuffer, "RIFF") && IsMagic(pbuffer + 8, "WAVE");
}
//============================================================================
//
//
//
//============================================================================
bool isAiff(const uint8_t *pbuffer, int len)
{
return len > 12 && IsMagic(pbuffer, "FORM") && IsMagic(pbuffer+8, "AIFF");
}
bool isAifc(const uint8_t *pbuffer, int len)
{
return len > 12 && IsMagic(pbuffer, "FORM") && IsMagic(pbuffer + 8, "AIFC");
}
//============================================================================
//
//
//
//============================================================================
bool isFlac(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "fLaC");
}
//============================================================================
//
//
//
//============================================================================
bool isOgg(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "OggS");
}
//============================================================================
//
// Almost nothing to check here, so this format is checked last of all.
//
//============================================================================
bool isMP3(const uint8_t *data, int len)
{
if (IsMagic(data, "ID3\3")) return true;
return data[0] == 0xff && ((data[1] & 0xfe) == 0xfa/*MPEG-1*/ || (data[1] & 0xfe) == 0xf2/*MPEG-2*/);
}
//============================================================================
//
//
//
//============================================================================
bool isVoc(const uint8_t *pbuffer, int len)
{
return len > 19 && IsMagic(pbuffer, "Creative Voice File", 19);
}
//============================================================================
//
//
//
//============================================================================
bool isFon1(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "FON1") && pbuffer[5] == 0 && pbuffer[7] == 0;
}
//============================================================================
//
//
//
//============================================================================
bool isFon2(const uint8_t *pbuffer, int len)
{
return IsMagic(pbuffer, "FON2") && pbuffer[5] == 0 && pbuffer[6] <= pbuffer[7];
}
//============================================================================
//
//
//
//============================================================================
bool isBmf(const uint8_t *data, int len)
{
return (data[0] == 0xE1 && data[1] == 0xE6 && data[2] == 0xD5 && data[3] == 0x1A);
}
//==========================================================================
//
//
//
//==========================================================================
bool isKVX(const uint8_t *data, int length)
{
// This isn't reliable enough.
return false;
#if 0
const kvxslab_t *slabs[MAXVOXMIPS];
const uint8_t *rawmip;
int mip, maxmipsize;
int i, j, n;
const uint8_t *rawvoxel = data;
int voxelsize = length - 1;
// Oh, KVX, why couldn't you have a proper header? We'll just go through
// and collect each MIP level, doing lots of range checking, and if the
// last one doesn't end exactly 768 uint8_ts before the end of the file,
// we'll reject it.
for (mip = 0, rawmip = rawvoxel, maxmipsize = voxelsize - 768 - 4;
mip < MAXVOXMIPS;
mip++)
{
int numuint8_ts = GetInt(rawmip);
if (numuint8_ts > maxmipsize || numuint8_ts < 24)
{
break;
}
rawmip += 4;
FVoxelMipLevel mipl;
// Load header data.
mipl.SizeX = GetInt(rawmip + 0);
mipl.SizeY = GetInt(rawmip + 4);
mipl.SizeZ = GetInt(rawmip + 8);
if (mipl.SizeX > 255 || mipl.SizeY > 255 || mipl.SizeZ > 255) return false;
// How much space do we have for voxdata?
int offsetsize = (mipl.SizeX + 1) * 4 + mipl.SizeX * (mipl.SizeY + 1) * 2;
int voxdatasize = numuint8_ts - 24 - offsetsize;
if (voxdatasize < 0)
{ // Clearly, not enough.
return false;
}
if (voxdatasize != 0)
{ // This mip level is not empty.
// Allocate slab data space.
mipl.OffsetX = new int[(numuint8_ts - 24 + 3) / 4];
mipl.OffsetXY = (short *)(mipl.OffsetX + mipl.SizeX + 1);
mipl.SlabData = (uint8_t *)(mipl.OffsetXY + mipl.SizeX * (mipl.SizeY + 1));
// Load x offsets.
for (i = 0, n = mipl.SizeX; i <= n; ++i)
{
// The X offsets stored in the KVX file are relative to the start of the
// X offsets array. Make them relative to voxdata instead.
mipl.OffsetX[i] = GetInt(rawmip + 24 + i * 4) - offsetsize;
}
// The first X offset must be 0 (since we subtracted offsetsize), according to the spec:
// NOTE: xoffset[0] = (xsiz+1)*4 + xsiz*(ysiz+1)*2 (ALWAYS)
if (mipl.OffsetX[0] != 0)
{
return false;
}
// And the final X offset must point just past the end of the voxdata.
if (mipl.OffsetX[mipl.SizeX] != voxdatasize)
{
break;
}
// Load xy offsets.
i = 24 + i * 4;
for (j = 0, n *= mipl.SizeY + 1; j < n; ++j)
{
mipl.OffsetXY[j] = GetShort(rawmip + i + j * 2);
}
// Ensure all offsets are within bounds.
for (i = 0; i < (int)mipl.SizeX; ++i)
{
int xoff = mipl.OffsetX[i];
for (j = 0; j < (int)mipl.SizeY; ++j)
{
int yoff = mipl.OffsetXY[(mipl.SizeY + 1) * i + j];
if (unsigned(xoff + yoff) > unsigned(voxdatasize))
{
return false;
}
}
}
// Record slab location for the end.
slabs[mip] = (kvxslab_t *)(rawmip + 24 + offsetsize);
}
// Time for the next mip Level.
rawmip += numuint8_ts;
maxmipsize -= numuint8_ts + 4;
}
// Did we get any mip levels, and if so, does the last one leave just
// enough room for the palette after it?
if (mip == 0 || rawmip != rawvoxel + voxelsize - 768)
{
return false;
}
return true;
#endif
}
// formats are listed in descending order of detection reliability, not grouped by types!
static Detector detectors[] =
{
// format which have an identifier at the beginning of the file
{ isPng,{ FT_PNG, ".PNG" } },
{ isJpg,{ FT_JPG, ".JPG" }},
{ isMid, { FT_MID, ".MID" }},
{ isOgg, { FT_OGGBASE, ".OGG" }},
{ isMus, { FT_MUS, ".MUS" }},
{ isFlac,{ FT_FLAC, ".FLAC" }},
{ isAiff, { FT_AIFF, ".AIF" }},
{ isAifc, { FT_AIFF, ".AIFC" }},
{ isIt,{ FT_AIFF, ".IT" } },
{ isSpc,{ FT_SPC, ".SPC" } },
{ isVoc,{ FT_VOC, ".VOC" }},
{ isWav,{ FT_WAV, ".WAV" } },
{ isImgz,{ FT_IMGZ, ".IMGZ" } },
{ isHmi, { FT_HMI, ".HMI" }},
{ isHmp,{ FT_HMP, ".HMP" } },
{ isXmi,{ FT_XMI, ".XMI" } },
{ isBmf,{ FT_BMF, ".BMF" } },
{ isFon1,{ FT_FON1, ".FON1" } },
{ isFon2,{ FT_FON2, ".FON2" } },
// formats which have an identifier near the beginning of a file
{ isS3m, { FT_S3M, ".S3M" }},
{ isXt, { FT_XM, ".XM" }},
// format which can only be detected by checking the header (MOD has an identifier in the middle, but too far from the start to be considered safe.)
{ isTGA,{ FT_TGA, ".TGA" } },
{ isPCX,{ FT_PCX, ".PCX" } },
{ isDDS,{ FT_DDS, ".DDS" } },
{ isMod,{ FT_MOD, ".MOD" } },
{ isPatch,{ FT_DOOMGFX, ".GFX" } },
// formats which cannot easily be detected reliably
{ isKVX, { FT_KVX, ".KVX" }},
{ isDoomSound, { FT_DOOMSND, ".DMX" }},
{ isMP3,{ FT_MP3BASE, ".MP3" } },
};
FileType resolveUnknown(FileType ft, const char *name, int length)
{
switch (ft.type)
{
case FT_OGGBASE:
case FT_MP3BASE:
// todo: Try to be a bit smarter about deciding whether this is a music or sound lump.
if (!strnicmp(name, "DS", 2)) ft.type |= FG_SND;
else if (!strnicmp(name, "D_", 2)) ft.type |= FG_MUS;
else if (length < 400000) ft.type |= FG_SND;
else ft.type |= FG_MUS;
break;
}
return ft;
}
FileType IdentifyFileType(const char *name, const uint8_t *data, int length)
{
if (length > 4)
{
for (auto &t : detectors)
{
if (t.checker(data, length))
{
return resolveUnknown(t.ret, name, length);
}
}
}
if (length == 0)
{
return{ FT_BINARY, ".LMP" };
}
// Identify pure text files. If any character < 32, except \t, \n, \r is present, a binary file is assumed
for (int i = 0; i < length; i++)
{
if (data[i] < 9 || data[i] == 11 || data[i] == 12 || (data[i] >= 14 && data[i] < 32))
{
return{ FT_BINARY, ".LMP" };
}
}
return{ FT_TEXT, ".TXT" };
}