Skip to content

Commit 2bf3d6f

Browse files
committed
Use more constants in GetFileType
1 parent b2700b5 commit 2bf3d6f

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

SabreTools.Serialization/Wrappers/WrapperFactory.cs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
132132
// Normalize the extension
133133
extension = extension.TrimStart('.').Trim();
134134

135-
// TODO: For all modelled types, use the constants instead of hardcoded values here
135+
// TODO: Use constants from Models here
136136
#region AACSMediaKeyBlock
137137

138138
// Block starting with verify media key record
@@ -154,6 +154,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
154154

155155
#endregion
156156

157+
// TODO: Use constants from Models here
157158
#region BDPlusSVM
158159

159160
if (magic.StartsWith([0x42, 0x44, 0x53, 0x56, 0x4D, 0x5F, 0x43, 0x43]))
@@ -166,11 +167,12 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
166167

167168
#region BFPK
168169

169-
if (magic.StartsWith([0x42, 0x46, 0x50, 0x4b]))
170+
if (magic.StartsWith(Models.BFPK.Constants.SignatureBytes))
170171
return WrapperType.BFPK;
171172

172173
#endregion
173174

175+
// TODO: Use constants from Models here
174176
#region BSP
175177

176178
// Shares a first 4 bytes with some .mc files
@@ -185,6 +187,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
185187

186188
#endregion
187189

190+
// TODO: Use constants from Models here
188191
#region BZip2
189192

190193
if (magic.StartsWith([0x42, 0x52, 0x68]))
@@ -197,7 +200,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
197200

198201
#region CFB
199202

200-
if (magic.StartsWith([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1]))
203+
if (magic.StartsWith(Models.CFB.Constants.SignatureBytes))
201204
return WrapperType.CFB;
202205

203206
// Installer package
@@ -222,6 +225,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
222225

223226
#endregion
224227

228+
// TODO: Use constants from Models here
225229
#region CHD
226230

227231
if (magic.StartsWith([0x4D, 0x43, 0x6F, 0x6D, 0x70, 0x72, 0x48, 0x44]))
@@ -239,7 +243,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
239243
#region Executable
240244

241245
// DOS MZ executable file format (and descendants)
242-
if (magic.StartsWith([0x4d, 0x5a]))
246+
if (magic.StartsWith(Models.MSDOS.Constants.SignatureBytes))
243247
return WrapperType.Executable;
244248

245249
// None of the following are supported yet
@@ -279,6 +283,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
279283

280284
#endregion
281285

286+
// TODO: Use constants from Models here
282287
#region GCF
283288

284289
if (magic.StartsWith([0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]))
@@ -289,9 +294,10 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
289294

290295
#endregion
291296

297+
// TODO: Use constants from Models here
292298
#region GZIP
293299

294-
if (magic.StartsWith([0x1f, 0x8b]))
300+
if (magic.StartsWith([Models.GZIP.Constants.ID1, Models.GZIP.Constants.ID2]))
295301
return WrapperType.GZIP;
296302

297303
if (extension.Equals("gz", StringComparison.OrdinalIgnoreCase))
@@ -306,6 +312,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
306312

307313
#endregion
308314

315+
// TODO: Use constants from Models here
309316
#region InstallShieldArchiveV3
310317

311318
if (magic.StartsWith([0x13, 0x5D, 0x65, 0x8C]))
@@ -318,13 +325,14 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
318325

319326
#region InstallShieldCAB
320327

321-
if (magic.StartsWith([0x49, 0x53, 0x63]))
328+
if (magic.StartsWith(Models.InstallShieldCabinet.Constants.SignatureBytes))
322329
return WrapperType.InstallShieldCAB;
323330

324331
// Both InstallShieldCAB and MicrosoftCAB share the same extension
325332

326333
#endregion
327334

335+
// TODO: Use constants from Models here
328336
#region LDSCRYPT
329337

330338
if (magic.StartsWith([0x4C, 0x44, 0x53, 0x43, 0x52, 0x59, 0x50, 0x54]))
@@ -334,20 +342,20 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
334342

335343
#region LZ
336344

337-
if (magic.StartsWith([0x4B, 0x57, 0x41, 0x4A, 0x88, 0xF0, 0x27, 0xD1]))
345+
if (magic.StartsWith(Models.LZ.Constants.KWAJSignatureBytes))
338346
return WrapperType.LZKWAJ;
339347

340-
if (magic.StartsWith([0x53, 0x5A, 0x20, 0x88, 0xF0, 0x27, 0x33, 0xD1]))
348+
if (magic.StartsWith(Models.LZ.Constants.QBasicSignatureBytes))
341349
return WrapperType.LZQBasic;
342350

343-
if (magic.StartsWith([0x53, 0x5A, 0x44, 0x44, 0x88, 0xF0, 0x27, 0x33]))
351+
if (magic.StartsWith(Models.LZ.Constants.SZDDSignatureBytes))
344352
return WrapperType.LZSZDD;
345353

346354
#endregion
347355

348356
#region MicrosoftCAB
349357

350-
if (magic.StartsWith([0x4d, 0x53, 0x43, 0x46]))
358+
if (magic.StartsWith(Models.MicrosoftCabinet.Constants.SignatureBytes))
351359
return WrapperType.MicrosoftCAB;
352360

353361
// Both InstallShieldCAB and MicrosoftCAB share the same extension
@@ -356,10 +364,10 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
356364

357365
#region MoPaQ
358366

359-
if (magic.StartsWith([0x4d, 0x50, 0x51, 0x1a]))
367+
if (magic.StartsWith(Models.MoPaQ.Constants.ArchiveHeaderSignatureBytes))
360368
return WrapperType.MoPaQ;
361369

362-
if (magic.StartsWith([0x4d, 0x50, 0x51, 0x1b]))
370+
if (magic.StartsWith(Models.MoPaQ.Constants.UserDataSignatureBytes))
363371
return WrapperType.MoPaQ;
364372

365373
if (extension.Equals("mpq", StringComparison.OrdinalIgnoreCase))
@@ -373,12 +381,16 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
373381
if (extension.Equals("3ds", StringComparison.OrdinalIgnoreCase))
374382
return WrapperType.N3DS;
375383

384+
else if (extension.Equals("cci", StringComparison.OrdinalIgnoreCase))
385+
return WrapperType.N3DS;
386+
376387
// CIA package -- Not currently supported
377388
// else if (extension.Equals("cia", StringComparison.OrdinalIgnoreCase))
378389
// return WrapperType.N3DS;
379390

380391
#endregion
381392

393+
// TODO: Use constants from Models here
382394
#region NCF
383395

384396
if (magic.StartsWith([0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]))
@@ -411,7 +423,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
411423

412424
#region PAK
413425

414-
if (magic.StartsWith([0x50, 0x41, 0x43, 0x4B]))
426+
if (magic.StartsWith(Models.PAK.Constants.SignatureBytes))
415427
return WrapperType.PAK;
416428

417429
// Both PAK and Quantum share one extension
@@ -420,6 +432,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
420432

421433
#endregion
422434

435+
// TODO: Use constants from Models here
423436
#region PFF
424437

425438
// Version 2
@@ -439,22 +452,25 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
439452

440453
#endregion
441454

455+
// TODO: Use constants from Models here
442456
#region PKZIP
443457

444458
// PKZIP (Unknown)
445459
if (magic.StartsWith([0x50, 0x4b, 0x00, 0x00]))
446460
return WrapperType.PKZIP;
447461

448462
// PKZIP
449-
if (magic.StartsWith([0x50, 0x4b, 0x03, 0x04]))
463+
if (magic.StartsWith(Models.PKZIP.Constants.LocalFileHeaderSignatureBytes))
450464
return WrapperType.PKZIP;
451465

452466
// PKZIP (Empty Archive)
453-
if (magic.StartsWith([0x50, 0x4b, 0x05, 0x06]))
467+
if (magic.StartsWith(Models.PKZIP.Constants.EndOfCentralDirectoryRecordSignatureBytes))
468+
return WrapperType.PKZIP;
469+
else if (magic.StartsWith(Models.PKZIP.Constants.EndOfCentralDirectoryRecord64SignatureBytes))
454470
return WrapperType.PKZIP;
455471

456472
// PKZIP (Spanned Archive)
457-
if (magic.StartsWith([0x50, 0x4b, 0x07, 0x08]))
473+
if (magic.StartsWith(Models.PKZIP.Constants.DataDescriptorSignatureBytes))
458474
return WrapperType.PKZIP;
459475

460476
// PKZIP
@@ -546,7 +562,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
546562
#region PLJ
547563

548564
// https://www.iana.org/assignments/media-types/audio/vnd.everad.plj
549-
if (magic.StartsWith([0xFF, 0x9D, 0x53, 0x4B]))
565+
if (magic.StartsWith(Models.PlayJ.Constants.SignatureBytes))
550566
return WrapperType.PlayJAudioFile;
551567

552568
// https://www.iana.org/assignments/media-types/audio/vnd.everad.plj
@@ -557,7 +573,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
557573

558574
#region Quantum
559575

560-
if (magic.StartsWith([0x44, 0x53]))
576+
if (magic.StartsWith(Models.Quantum.Constants.SignatureBytes))
561577
return WrapperType.Quantum;
562578

563579
if (extension.Equals("q", StringComparison.OrdinalIgnoreCase))
@@ -569,6 +585,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
569585

570586
#endregion
571587

588+
// TODO: Use constants from Models here
572589
#region RAR
573590

574591
// RAR archive version 1.50 onwards
@@ -584,6 +601,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
584601

585602
#endregion
586603

604+
// TODO: Use constants from Models here
587605
#region RealArcade
588606

589607
// RASGI2.0
@@ -598,6 +616,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
598616

599617
#endregion
600618

619+
// TODO: Use constants from Models here
601620
#region SevenZip
602621

603622
if (magic.StartsWith([0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]))
@@ -611,21 +630,22 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
611630
#region SFFS
612631

613632
// Found in Redump entry 81756, confirmed to be "StarForce Filesystem" by PiD.
614-
if (magic.StartsWith([0x53, 0x46, 0x46, 0x53]))
633+
if (magic.StartsWith(Models.SFFS.Constants.SignatureBytes))
615634
return WrapperType.SFFS;
616635

617636
#endregion
618637

619638
#region SGA
620639

621-
if (magic.StartsWith([0x5F, 0x41, 0x52, 0x43, 0x48, 0x49, 0x56, 0x45]))
640+
if (magic.StartsWith(Models.SGA.Constants.SignatureBytes))
622641
return WrapperType.SGA;
623642

624643
if (extension.Equals("sga", StringComparison.OrdinalIgnoreCase))
625644
return WrapperType.SGA;
626645

627646
#endregion
628647

648+
// TODO: Use constants from Models here
629649
#region TapeArchive
630650

631651
if (magic.StartsWith([0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30]))
@@ -639,6 +659,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
639659

640660
#endregion
641661

662+
// TODO: Use constants from Models here
642663
#region Textfile
643664

644665
// Not all textfiles can be determined through magic number
@@ -722,7 +743,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
722743

723744
#region VBSP
724745

725-
if (magic.StartsWith([0x56, 0x42, 0x53, 0x50]))
746+
if (magic.StartsWith(Models.BSP.Constants.SignatureBytes))
726747
return WrapperType.VBSP;
727748

728749
// Shares an extension with BSP
@@ -733,7 +754,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
733754

734755
#region VPK
735756

736-
if (magic.StartsWith([0x34, 0x12, 0xaa, 0x55]))
757+
if (magic.StartsWith(Models.VPK.Constants.SignatureBytes))
737758
return WrapperType.VPK;
738759

739760
// Common extension so this cannot be used accurately
@@ -744,7 +765,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
744765

745766
#region WAD
746767

747-
if (magic.StartsWith([0x57, 0x41, 0x44, 0x33]))
768+
if (magic.StartsWith(Models.WAD3.Constants.SignatureBytes))
748769
return WrapperType.WAD;
749770

750771
// Common extension so this cannot be used accurately
@@ -753,6 +774,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
753774

754775
#endregion
755776

777+
// TODO: Use constants from Models here
756778
#region XZ
757779

758780
if (magic.StartsWith([0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00]))
@@ -765,7 +787,7 @@ public static WrapperType GetFileType(byte[]? magic, string? extension)
765787

766788
#region XZP
767789

768-
if (magic.StartsWith([0x70, 0x69, 0x5A, 0x78]))
790+
if (magic.StartsWith(Models.XZP.Constants.HeaderSignatureBytes))
769791
return WrapperType.XZP;
770792

771793
if (extension.Equals("xzp", StringComparison.OrdinalIgnoreCase))

0 commit comments

Comments
 (0)