diff --git a/BinaryObjectScanner/BinaryObjectScanner.csproj b/BinaryObjectScanner/BinaryObjectScanner.csproj index b5ea86ab..2e934730 100644 --- a/BinaryObjectScanner/BinaryObjectScanner.csproj +++ b/BinaryObjectScanner/BinaryObjectScanner.csproj @@ -83,14 +83,14 @@ - + - + - - - + + + diff --git a/BinaryObjectScanner/Factory.cs b/BinaryObjectScanner/Factory.cs index 46ce7135..94794f8a 100644 --- a/BinaryObjectScanner/Factory.cs +++ b/BinaryObjectScanner/Factory.cs @@ -1,5 +1,5 @@ using BinaryObjectScanner.Interfaces; -using BinaryObjectScanner.Utilities; +using SabreTools.Serialization.Wrappers; namespace BinaryObjectScanner { @@ -8,22 +8,22 @@ namespace BinaryObjectScanner /// /// Create an instance of a detectable based on file type /// - public static IDetectable? CreateDetectable(SupportedFileType fileType) + public static IDetectable? CreateDetectable(WrapperType fileType) { switch (fileType) { - case SupportedFileType.AACSMediaKeyBlock: return new FileType.AACSMediaKeyBlock(); - case SupportedFileType.BDPlusSVM: return new FileType.BDPlusSVM(); - //case SupportedFileType.CIA: return new FileType.CIA(); - case SupportedFileType.Executable: return new FileType.Executable(); - case SupportedFileType.LDSCRYPT: return new FileType.LDSCRYPT(); - //case SupportedFileType.N3DS: return new FileType.N3DS(); - //case SupportedFileType.Nitro: return new FileType.Nitro(); - case SupportedFileType.PLJ: return new FileType.PLJ(); - case SupportedFileType.RealArcadeInstaller: return new FileType.RealArcadeInstaller(); - case SupportedFileType.RealArcadeMezzanine: return new FileType.RealArcadeMezzanine(); - case SupportedFileType.SFFS: return new FileType.SFFS(); - case SupportedFileType.Textfile: return new FileType.Textfile(); + case WrapperType.AACSMediaKeyBlock: return new FileType.AACSMediaKeyBlock(); + case WrapperType.BDPlusSVM: return new FileType.BDPlusSVM(); + //case WrapperType.CIA: return new FileType.CIA(); + case WrapperType.Executable: return new FileType.Executable(); + case WrapperType.LDSCRYPT: return new FileType.LDSCRYPT(); + //case WrapperType.N3DS: return new FileType.N3DS(); + //case WrapperType.Nitro: return new FileType.Nitro(); + case WrapperType.PlayJAudioFile: return new FileType.PLJ(); + case WrapperType.RealArcadeInstaller: return new FileType.RealArcadeInstaller(); + case WrapperType.RealArcadeMezzanine: return new FileType.RealArcadeMezzanine(); + case WrapperType.SFFS: return new FileType.SFFS(); + case WrapperType.Textfile: return new FileType.Textfile(); default: return null; } } @@ -31,40 +31,40 @@ namespace BinaryObjectScanner /// /// Create an instance of an extractable based on file type /// - public static IExtractable? CreateExtractable(SupportedFileType fileType) + public static IExtractable? CreateExtractable(WrapperType fileType) { switch (fileType) { - case SupportedFileType.BFPK: return new FileType.BFPK(); - case SupportedFileType.BSP: return new FileType.BSP(); - case SupportedFileType.BZip2: return new FileType.BZip2(); - case SupportedFileType.CFB: return new FileType.CFB(); - //case SupportedFileType.CIA: return new FileType.CIA(); - case SupportedFileType.GCF: return new FileType.GCF(); - case SupportedFileType.GZIP: return new FileType.GZIP(); - case SupportedFileType.InstallShieldArchiveV3: return new FileType.InstallShieldArchiveV3(); - case SupportedFileType.InstallShieldCAB: return new FileType.InstallShieldCAB(); - case SupportedFileType.MicrosoftCAB: return new FileType.MicrosoftCAB(); - case SupportedFileType.MicrosoftLZ: return new FileType.MicrosoftLZ(); - case SupportedFileType.MPQ: return new FileType.MPQ(); - //case SupportedFileType.N3DS: return new FileType.N3DS(); - //case SupportedFileType.NCF: return new FileType.NCF(); - //case SupportedFileType.Nitro: return new FileType.Nitro(); - case SupportedFileType.PAK: return new FileType.PAK(); - case SupportedFileType.PFF: return new FileType.PFF(); - case SupportedFileType.PKZIP: return new FileType.PKZIP(); - //case SupportedFileType.PLJ: return new FileType.PLJ(); - //case SupportedFileType.Quantum: return new FileType.Quantum(); - case SupportedFileType.RAR: return new FileType.RAR(); - case SupportedFileType.SevenZip: return new FileType.SevenZip(); - case SupportedFileType.SFFS: return new FileType.SFFS(); - case SupportedFileType.SGA: return new FileType.SGA(); - case SupportedFileType.TapeArchive: return new FileType.TapeArchive(); - case SupportedFileType.VBSP: return new FileType.VBSP(); - case SupportedFileType.VPK: return new FileType.VPK(); - case SupportedFileType.WAD: return new FileType.WAD(); - case SupportedFileType.XZ: return new FileType.XZ(); - case SupportedFileType.XZP: return new FileType.XZP(); + case WrapperType.BFPK: return new FileType.BFPK(); + case WrapperType.BSP: return new FileType.BSP(); + case WrapperType.BZip2: return new FileType.BZip2(); + case WrapperType.CFB: return new FileType.CFB(); + //case WrapperType.CIA: return new FileType.CIA(); + case WrapperType.GCF: return new FileType.GCF(); + case WrapperType.GZIP: return new FileType.GZIP(); + case WrapperType.InstallShieldArchiveV3: return new FileType.InstallShieldArchiveV3(); + case WrapperType.InstallShieldCAB: return new FileType.InstallShieldCAB(); + case WrapperType.MicrosoftCAB: return new FileType.MicrosoftCAB(); + case WrapperType.MicrosoftLZ: return new FileType.MicrosoftLZ(); + case WrapperType.MoPaQ: return new FileType.MPQ(); + //case WrapperType.N3DS: return new FileType.N3DS(); + //case WrapperType.NCF: return new FileType.NCF(); + //case WrapperType.Nitro: return new FileType.Nitro(); + case WrapperType.PAK: return new FileType.PAK(); + case WrapperType.PFF: return new FileType.PFF(); + case WrapperType.PKZIP: return new FileType.PKZIP(); + //case WrapperType.PLJ: return new FileType.PLJ(); + //case WrapperType.Quantum: return new FileType.Quantum(); + case WrapperType.RAR: return new FileType.RAR(); + case WrapperType.SevenZip: return new FileType.SevenZip(); + case WrapperType.SFFS: return new FileType.SFFS(); + case WrapperType.SGA: return new FileType.SGA(); + case WrapperType.TapeArchive: return new FileType.TapeArchive(); + case WrapperType.VBSP: return new FileType.VBSP(); + case WrapperType.VPK: return new FileType.VPK(); + case WrapperType.WAD: return new FileType.WAD(); + case WrapperType.XZ: return new FileType.XZ(); + case WrapperType.XZP: return new FileType.XZP(); default: return null; } } diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 73aabc59..245f7385 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using BinaryObjectScanner.FileType; using BinaryObjectScanner.Interfaces; using BinaryObjectScanner.Utilities; +using SabreTools.IO.Extensions; using SabreTools.Serialization.Wrappers; using static BinaryObjectScanner.Utilities.Dictionary; @@ -133,11 +134,7 @@ namespace BinaryObjectScanner if (Directory.Exists(path)) { // Enumerate all files at first for easier access -#if NET20 || NET35 - var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories).ToList(); -#else - var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).ToList(); -#endif + var files = IOExtensions.SafeEnumerateFiles(path, "*", SearchOption.AllDirectories).ToList(); // Scan for path-detectable protections if (ScanPaths) @@ -330,12 +327,12 @@ namespace BinaryObjectScanner } // Get the file type either from magic number or extension - SupportedFileType fileType = FileTypes.GetFileType(magic); - if (fileType == SupportedFileType.UNKNOWN) - fileType = FileTypes.GetFileType(extension); + WrapperType fileType = WrapperFactory.GetFileType(magic); + if (fileType == WrapperType.UNKNOWN) + fileType = WrapperFactory.GetFileType(extension); // If we still got unknown, just return null - if (fileType == SupportedFileType.UNKNOWN) + if (fileType == WrapperType.UNKNOWN) return null; #region Non-Archive File Types diff --git a/BinaryObjectScanner/Utilities/Enums.cs b/BinaryObjectScanner/Utilities/Enums.cs deleted file mode 100644 index ffe29922..00000000 --- a/BinaryObjectScanner/Utilities/Enums.cs +++ /dev/null @@ -1,203 +0,0 @@ -namespace BinaryObjectScanner.Utilities -{ - /// - /// Subset of file types that are supported by the library - /// - public enum SupportedFileType - { - /// - /// Unknown or unsupported - /// - UNKNOWN, - - /// - /// AACS media key block - /// - AACSMediaKeyBlock, - - /// - /// BD+ SVM - /// - BDPlusSVM, - - /// - /// BFPK custom archive - /// - BFPK, - - /// - /// Half-Life Level - /// - BSP, - - /// - /// bzip2 archive - /// - BZip2, - - /// - /// Compound File Binary - /// - CFB, - - /// - /// CTR Importable Archive - /// - CIA, - - /// - /// Executable or library - /// - Executable, - - /// - /// Half-Life Game Cache File - /// - GCF, - - /// - /// gzip archive - /// - GZIP, - - /// - /// Key-value pair INI file - /// - IniFile, - - /// - /// InstallShield archive v3 - /// - InstallShieldArchiveV3, - - /// - /// InstallShield cabinet file - /// - InstallShieldCAB, - - /// - /// Link Data Security encrypted file - /// - LDSCRYPT, - - /// - /// Microsoft cabinet file - /// - MicrosoftCAB, - - /// - /// Microsoft LZ-compressed file - /// - MicrosoftLZ, - - /// - /// MPQ game data archive - /// - MPQ, - - /// - /// Nintendo 3DS cart image - /// - N3DS, - - /// - /// Half-Life No Cache File - /// - NCF, - - /// - /// Nintendo DS/DSi cart image - /// - Nitro, - - /// - /// Half-Life Package File - /// - PAK, - - /// - /// NovaLogic Game Archive Format - /// - PFF, - - /// - /// PKWARE ZIP archive and derivatives - /// - PKZIP, - - /// - /// PlayJ audio file - /// - PLJ, - - /// - /// Quantum archive - /// - Quantum, - - /// - /// RAR archive - /// - RAR, - - /// - /// RealArcade Installer - /// - RealArcadeInstaller, - - /// - /// RealArcade Mezzanine - /// - RealArcadeMezzanine, - - /// - /// 7-zip archive - /// - SevenZip, - - /// - /// StarForce FileSystem file - /// - SFFS, - - /// - /// SGA - /// - SGA, - - /// - /// Tape archive - /// - TapeArchive, - - /// - /// Various generic textfile formats - /// - Textfile, - - /// - /// Half-Life 2 Level - /// - VBSP, - - /// - /// Valve Package File - /// - VPK, - - /// - /// Half-Life Texture Package File - /// - WAD, - - /// - /// xz archive - /// - XZ, - - /// - /// Xbox Package File - /// - XZP, - } -} diff --git a/BinaryObjectScanner/Utilities/FileTypes.cs b/BinaryObjectScanner/Utilities/FileTypes.cs deleted file mode 100644 index e49cad4a..00000000 --- a/BinaryObjectScanner/Utilities/FileTypes.cs +++ /dev/null @@ -1,812 +0,0 @@ -using System; -using SabreTools.Matching; - -namespace BinaryObjectScanner.Utilities -{ - public static class FileTypes - { - /// - /// Get the supported file type for a magic string - /// - /// Recommend sending in 16 bytes to check - public static SupportedFileType GetFileType(byte[] magic) - { - // If we have an invalid magic byte array - if (magic == null || magic.Length == 0) - return SupportedFileType.UNKNOWN; - - // TODO: For all modelled types, use the constants instead of hardcoded values here - #region AACSMediaKeyBlock - - // Block starting with verify media key record - if (magic.StartsWith(new byte?[] { 0x81, 0x00, 0x00, 0x14 })) - return SupportedFileType.AACSMediaKeyBlock; - - // Block starting with type and version record - if (magic.StartsWith(new byte?[] { 0x10, 0x00, 0x00, 0x0C })) - return SupportedFileType.AACSMediaKeyBlock; - - #endregion - - #region BDPlusSVM - - if (magic.StartsWith(new byte?[] { 0x42, 0x44, 0x53, 0x56, 0x4D, 0x5F, 0x43, 0x43 })) - return SupportedFileType.BDPlusSVM; - - #endregion - - #region BFPK - - if (magic.StartsWith(new byte?[] { 0x42, 0x46, 0x50, 0x4b })) - return SupportedFileType.BFPK; - - #endregion - - #region BSP - - if (magic.StartsWith(new byte?[] { 0x1e, 0x00, 0x00, 0x00 })) - return SupportedFileType.BSP; - - #endregion - - #region BZip2 - - if (magic.StartsWith(new byte?[] { 0x42, 0x52, 0x68 })) - return SupportedFileType.BZip2; - - #endregion - - #region CFB - - if (magic.StartsWith(new byte?[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 })) - return SupportedFileType.CFB; - - #endregion - - #region CIA - - // No magic checks for CIA - - #endregion - - #region Executable - - // DOS MZ executable file format (and descendants) - if (magic.StartsWith(new byte?[] { 0x4d, 0x5a })) - return SupportedFileType.Executable; - - /* - // None of the following are supported in scans yet - - // Executable and Linkable Format - if (magic.StartsWith(new byte?[] { 0x7f, 0x45, 0x4c, 0x46 })) - return FileTypes.Executable; - - // Mach-O binary (32-bit) - if (magic.StartsWith(new byte?[] { 0xfe, 0xed, 0xfa, 0xce })) - return FileTypes.Executable; - - // Mach-O binary (32-bit, reverse byte ordering scheme) - if (magic.StartsWith(new byte?[] { 0xce, 0xfa, 0xed, 0xfe })) - return FileTypes.Executable; - - // Mach-O binary (64-bit) - if (magic.StartsWith(new byte?[] { 0xfe, 0xed, 0xfa, 0xcf })) - return FileTypes.Executable; - - // Mach-O binary (64-bit, reverse byte ordering scheme) - if (magic.StartsWith(new byte?[] { 0xcf, 0xfa, 0xed, 0xfe })) - return FileTypes.Executable; - - // Prefrred Executable File Format - if (magic.StartsWith(new byte?[] { 0x4a, 0x6f, 0x79, 0x21, 0x70, 0x65, 0x66, 0x66 })) - return FileTypes.Executable; - */ - - #endregion - - #region GCF - - if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 })) - return SupportedFileType.GCF; - - #endregion - - #region GZIP - - if (magic.StartsWith(new byte?[] { 0x1f, 0x8b })) - return SupportedFileType.GZIP; - - #endregion - - #region IniFile - - // No magic checks for IniFile - - #endregion - - #region InstallShieldArchiveV3 - - if (magic.StartsWith(new byte?[] { 0x13, 0x5D, 0x65, 0x8C })) - return SupportedFileType.InstallShieldArchiveV3; - - #endregion - - #region InstallShieldCAB - - if (magic.StartsWith(new byte?[] { 0x49, 0x53, 0x63 })) - return SupportedFileType.InstallShieldCAB; - - #endregion - - #region LDSCRYPT - - if (magic.StartsWith(new byte?[] { 0x4C, 0x44, 0x53, 0x43, 0x52, 0x59, 0x50, 0x54 })) - return SupportedFileType.LDSCRYPT; - - #endregion - - #region MicrosoftCAB - - if (magic.StartsWith(new byte?[] { 0x4d, 0x53, 0x43, 0x46 })) - return SupportedFileType.MicrosoftCAB; - - #endregion - - #region MicrosoftLZ - - if (magic.StartsWith(new byte?[] { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 })) - return SupportedFileType.MicrosoftLZ; - - #endregion - - #region MPQ - - if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1a })) - return SupportedFileType.MPQ; - - if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1b })) - return SupportedFileType.MPQ; - - #endregion - - #region N3DS - - // No magic checks for N3DS - - #endregion - - #region NCF - - if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 })) - return SupportedFileType.NCF; - - #endregion - - #region Nitro - - // No magic checks for Nitro - - #endregion - - #region PAK - - if (magic.StartsWith(new byte?[] { 0x50, 0x41, 0x43, 0x4B })) - return SupportedFileType.PAK; - - #endregion - - #region PFF - - // Version 2 - if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x32 })) - return SupportedFileType.PFF; - - // Version 3 - if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x33 })) - return SupportedFileType.PFF; - - // Version 4 - if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x34 })) - return SupportedFileType.PFF; - - #endregion - - #region PKZIP - - // PKZIP (Unknown) - if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x00, 0x00 })) - return SupportedFileType.PKZIP; - - // PKZIP - if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x03, 0x04 })) - return SupportedFileType.PKZIP; - - // PKZIP (Empty Archive) - if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x05, 0x06 })) - return SupportedFileType.PKZIP; - - // PKZIP (Spanned Archive) - if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x07, 0x08 })) - return SupportedFileType.PKZIP; - - #endregion - - #region PLJ - - // https://www.iana.org/assignments/media-types/audio/vnd.everad.plj - if (magic.StartsWith(new byte?[] { 0xFF, 0x9D, 0x53, 0x4B })) - return SupportedFileType.PLJ; - - #endregion - - #region Quantum - - if (magic.StartsWith(new byte?[] { 0x44, 0x53 })) - return SupportedFileType.Quantum; - - #endregion - - #region RAR - - // RAR archive version 1.50 onwards - if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 })) - return SupportedFileType.RAR; - - // RAR archive version 5.0 onwards - if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00 })) - return SupportedFileType.RAR; - - #endregion - - #region RealArcade - - // RASGI2.0 - // Found in the ".rgs files in IA item "Nova_RealArcadeCD_USA". - if (magic.StartsWith(new byte?[] { 0x52, 0x41, 0x53, 0x47, 0x49, 0x32, 0x2E, 0x30 })) - return SupportedFileType.RealArcadeInstaller; - - // XZip2.0 - // Found in the ".mez" files in IA item "Nova_RealArcadeCD_USA". - if (magic.StartsWith(new byte?[] { 0x58, 0x5A, 0x69, 0x70, 0x32, 0x2E, 0x30 })) - return SupportedFileType.RealArcadeMezzanine; - - #endregion - - #region SevenZip - - if (magic.StartsWith(new byte?[] { 0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c })) - return SupportedFileType.SevenZip; - - #endregion - - #region SFFS - - // Found in Redump entry 81756, confirmed to be "StarForce Filesystem" by PiD. - if (magic.StartsWith(new byte?[] { 0x53, 0x46, 0x46, 0x53 })) - return SupportedFileType.SFFS; - - #endregion - - #region SGA - - if (magic.StartsWith(new byte?[] { 0x5F, 0x41, 0x52, 0x43, 0x48, 0x49, 0x56, 0x45 })) - return SupportedFileType.SGA; - - #endregion - - #region TapeArchive - - if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30 })) - return SupportedFileType.TapeArchive; - - if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00 })) - return SupportedFileType.TapeArchive; - - #endregion - - #region Textfile - - // Not all textfiles can be determined through magic number - - // HTML - if (magic.StartsWith(new byte?[] { 0x3c, 0x68, 0x74, 0x6d, 0x6c })) - return SupportedFileType.Textfile; - - // HTML and XML - if (magic.StartsWith(new byte?[] { 0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45 })) - return SupportedFileType.Textfile; - - // InstallShield Compiled Rules - if (magic.StartsWith(new byte?[] { 0x61, 0x4C, 0x75, 0x5A })) - return SupportedFileType.Textfile; - - // Microsoft Office File (old) - if (magic.StartsWith(new byte?[] { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 })) - return SupportedFileType.Textfile; - - // Rich Text File - if (magic.StartsWith(new byte?[] { 0x7b, 0x5c, 0x72, 0x74, 0x66, 0x31 })) - return SupportedFileType.Textfile; - - // Windows Help File - if (magic.StartsWith(new byte?[] { 0x3F, 0x5F, 0x03, 0x00 })) - return SupportedFileType.Textfile; - - // XML - // " - /// Get the supported file type for an extension - /// - /// This is less accurate than a magic string match - public static SupportedFileType GetFileType(string extension) - { - // If we have an invalid extension - if (string.IsNullOrEmpty(extension)) - return SupportedFileType.UNKNOWN; - - // Normalize the extension - extension = extension.TrimStart('.').Trim(); - - #region AACSMediaKeyBlock - - // Shares an extension with INF setup information so it can't be used accurately - // Blu-ray - // if (extension.Equals("inf", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.AACSMediaKeyBlock; - - // HD-DVD - if (extension.Equals("aacs", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.AACSMediaKeyBlock; - - #endregion - - #region BDPlusSVM - - if (extension.Equals("svm", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.BDPlusSVM; - - #endregion - - #region BFPK - - // No extensions registered for BFPK - - #endregion - - #region BSP - - // Shares an extension with VBSP so it can't be used accurately - // if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.BSP; - - #endregion - - #region BZip2 - - if (extension.Equals("bz2", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.BZip2; - - #endregion - - #region CFB - - // Installer package - if (extension.Equals("msi", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CFB; - - // Merge module - else if (extension.Equals("msm", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CFB; - - // Patch Package - else if (extension.Equals("msp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CFB; - - // Transform - else if (extension.Equals("mst", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CFB; - - // Patch Creation Properties - else if (extension.Equals("pcp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CFB; - - #endregion - - #region CIA - - if (extension.Equals("cia", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.CIA; - - #endregion - - #region Executable - - // DOS MZ executable file format (and descendants) - if (extension.Equals("exe", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Executable; - - // DOS MZ library file format (and descendants) - if (extension.Equals("dll", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Executable; - - #endregion - - #region GCF - - if (extension.Equals("gcf", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.GCF; - - #endregion - - #region GZIP - - if (extension.Equals("gz", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.GZIP; - - #endregion - - #region IniFile - - if (extension.Equals("ini", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.IniFile; - - #endregion - - #region InstallShieldArchiveV3 - - if (extension.Equals("z", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.InstallShieldArchiveV3; - - #endregion - - #region InstallShieldCAB - - // No extensions registered for InstallShieldCAB - // Both InstallShieldCAB and MicrosoftCAB share the same extension - - #endregion - - #region MicrosoftCAB - - // No extensions registered for InstallShieldCAB - // Both InstallShieldCAB and MicrosoftCAB share the same extension - - #endregion - - #region MPQ - - if (extension.Equals("mpq", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.MPQ; - - #endregion - - #region N3DS - - // 3DS cart image - if (extension.Equals("3ds", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.N3DS; - - // CIA package -- Not currently supported - // else if (extension.Equals("cia", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.N3DS; - - #endregion - - #region NCF - - if (extension.Equals("ncf", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.NCF; - - #endregion - - #region Nitro - - // DS cart image - if (extension.Equals("nds", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Nitro; - - // DS development cart image - else if (extension.Equals("srl", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Nitro; - - // DSi cart image - else if (extension.Equals("dsi", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Nitro; - - // iQue DS cart image - else if (extension.Equals("ids", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Nitro; - - #endregion - - #region PAK - - // No extensions registered for PAK - // Both PAK and Quantum share one extension - // if (extension.Equals("pak", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.PAK; - - #endregion - - #region PFF - - if (extension.Equals("pff", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PFF; - - #endregion - - #region PKZIP - - // PKZIP - if (extension.Equals("zip", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Android package - if (extension.Equals("apk", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Java archive - if (extension.Equals("jar", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Google Earth saved working session file - if (extension.Equals("kmz", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // KWord document - if (extension.Equals("kwd", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Microsoft Office Open XML Format (OOXML) Document - if (extension.Equals("docx", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Microsoft Office Open XML Format (OOXML) Presentation - if (extension.Equals("pptx", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Microsoft Office Open XML Format (OOXML) Spreadsheet - if (extension.Equals("xlsx", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenDocument text document - if (extension.Equals("odt", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenDocument presentation - if (extension.Equals("odp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenDocument text document template - if (extension.Equals("ott", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Microsoft Open XML paper specification file - if (extension.Equals("oxps", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenOffice spreadsheet - if (extension.Equals("sxc", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenOffice drawing - if (extension.Equals("sxd", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenOffice presentation - if (extension.Equals("sxi", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // OpenOffice word processing - if (extension.Equals("sxw", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // StarOffice spreadsheet - if (extension.Equals("sxc", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Windows Media compressed skin file - if (extension.Equals("wmz", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // Mozilla Browser Archive - if (extension.Equals("xpi", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // XML paper specification file - if (extension.Equals("xps", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - // eXact Packager Models - if (extension.Equals("xpt", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PKZIP; - - #endregion - - #region PLJ - - // https://www.iana.org/assignments/media-types/audio/vnd.everad.plj - if (extension.Equals("plj", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.PLJ; - - #endregion - - #region Quantum - - if (extension.Equals("q", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Quantum; - - // Both PAK and Quantum share one extension - // if (extension.Equals("pak", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.Quantum; - - #endregion - - #region RAR - - if (extension.Equals("rar", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.RAR; - - #endregion - - #region SevenZip - - if (extension.Equals("7z", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.SevenZip; - - #endregion - - #region SGA - - if (extension.Equals("sga", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.SGA; - - #endregion - - #region TapeArchive - - if (extension.Equals("tar", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.SevenZip; - - #endregion - - #region Textfile - - // "Description in Zip" - if (extension.Equals("diz", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Generic textfile (no header) - if (extension.Equals("txt", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // HTML - if (extension.Equals("htm", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - if (extension.Equals("html", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // InstallShield Script - if (extension.Equals("ins", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Microsoft Office File (old) - if (extension.Equals("doc", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Property list - if (extension.Equals("plist", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Rich Text File - if (extension.Equals("rtf", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Setup information - if (extension.Equals("inf", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // Windows Help File - if (extension.Equals("hlp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // WZC - if (extension.Equals("wzc", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - // XML - if (extension.Equals("xml", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.Textfile; - - - - #endregion - - #region VBSP - - // Shares an extension with BSP so it can't be used accurately - // if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.VBSP; - - #endregion - - #region VPK - - // Common extension so this cannot be used accurately - // if (extension.Equals("vpk", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.VPK; - - #endregion - - #region WAD - - // Common extension so this cannot be used accurately - // if (extension.Equals("wad", StringComparison.OrdinalIgnoreCase)) - // return SupportedFileType.WAD; - - #endregion - - #region XZ - - if (extension.Equals("xz", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.XZ; - - #endregion - - #region XZP - - if (extension.Equals("xzp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.XZP; - - #endregion - - // We couldn't find a supported match - return SupportedFileType.UNKNOWN; - } - } -} diff --git a/BinaryObjectScanner/Utilities/WrapperFactory.cs b/BinaryObjectScanner/Utilities/WrapperFactory.cs deleted file mode 100644 index 9aceaf6b..00000000 --- a/BinaryObjectScanner/Utilities/WrapperFactory.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System.IO; -using SabreTools.IO.Extensions; -using SabreTools.Matching; -using SabreTools.Serialization.Interfaces; -using SabreTools.Serialization.Wrappers; - -namespace BinaryObjectScanner.Utilities -{ - public static class WrapperFactory - { - /// - /// Create an instance of a wrapper based on file type - /// - public static IWrapper? CreateWrapper(SupportedFileType fileType, Stream? data) - { - switch (fileType) - { - case SupportedFileType.AACSMediaKeyBlock: return AACSMediaKeyBlock.Create(data); - case SupportedFileType.BDPlusSVM: return BDPlusSVM.Create(data); - case SupportedFileType.BFPK: return BFPK.Create(data); - case SupportedFileType.BSP: return BSP.Create(data); - //case SupportedFileType.BZip2: return BZip2.Create(data); - case SupportedFileType.CFB: return CFB.Create(data); - case SupportedFileType.CIA: return CIA.Create(data); - case SupportedFileType.Executable: return CreateExecutableWrapper(data); - case SupportedFileType.GCF: return GCF.Create(data); - //case SupportedFileType.GZIP: return GZIP.Create(data); - //case SupportedFileType.IniFile: return IniFile.Create(data); - //case SupportedFileType.InstallShieldArchiveV3: return InstallShieldArchiveV3.Create(data); - case SupportedFileType.InstallShieldCAB: return InstallShieldCabinet.Create(data); - //case SupportedFileType.LDSCRYPT: return LDSCRYPT.Create(data); - case SupportedFileType.MicrosoftCAB: return MicrosoftCabinet.Create(data); - //case SupportedFileType.MicrosoftLZ: return MicrosoftLZ.Create(data); - case SupportedFileType.MPQ: return MoPaQ.Create(data); - case SupportedFileType.N3DS: return N3DS.Create(data); - case SupportedFileType.NCF: return NCF.Create(data); - case SupportedFileType.Nitro: return Nitro.Create(data); - case SupportedFileType.PAK: return PAK.Create(data); - case SupportedFileType.PFF: return PFF.Create(data); - //case SupportedFileType.PIC: return PIC.Create(data); - //case SupportedFileType.PKZIP: return PKZIP.Create(data); - case SupportedFileType.PLJ: return PlayJAudioFile.Create(data); - //case SupportedFileType.PLJPlaylist: return PlayJPlaylist.Create(data); - case SupportedFileType.Quantum: return Quantum.Create(data); - //case SupportedFileType.RAR: return RAR.Create(data); - //case SupportedFileType.SevenZip: return SevenZip.Create(data); - //case SupportedFileType.SFFS: return SFFS.Create(data); - case SupportedFileType.SGA: return SGA.Create(data); - //case SupportedFileType.TapeArchive: return TapeArchive.Create(data); - //case SupportedFileType.Textfile: return Textfile.Create(data); - case SupportedFileType.VBSP: return VBSP.Create(data); - case SupportedFileType.VPK: return VPK.Create(data); - case SupportedFileType.WAD: return WAD.Create(data); - //case SupportedFileType.XZ: return XZ.Create(data); - case SupportedFileType.XZP: return XZP.Create(data); - default: return null; - } - } - - /// - /// Create an instance of a wrapper based on the executable type - /// - /// Stream data to parse - /// IWrapper representing the executable, null on error - public static IWrapper? CreateExecutableWrapper(Stream? stream) - { - // If we have no stream - if (stream == null) - return null; - - // Try to get an MS-DOS wrapper first - var wrapper = MSDOS.Create(stream); - if (wrapper == null || !(wrapper is MSDOS msdos)) - return null; - - // Check for a valid new executable address - if (msdos.Model.Header?.NewExeHeaderAddr == null || msdos.Model.Header.NewExeHeaderAddr >= stream.Length) - return wrapper; - - // Try to read the executable info - stream.Seek(msdos.Model.Header.NewExeHeaderAddr, SeekOrigin.Begin); - var magic = stream.ReadBytes(4); - - // If we didn't get valid data at the offset - if (magic == null) - { - return wrapper; - } - - // New Executable - else if (magic.StartsWith(SabreTools.Models.NewExecutable.Constants.SignatureBytes)) - { - stream.Seek(0, SeekOrigin.Begin); - return NewExecutable.Create(stream); - } - - // Linear Executable - else if (magic.StartsWith(SabreTools.Models.LinearExecutable.Constants.LESignatureBytes) - || magic.StartsWith(SabreTools.Models.LinearExecutable.Constants.LXSignatureBytes)) - { - stream.Seek(0, SeekOrigin.Begin); - return LinearExecutable.Create(stream); - } - - // Portable Executable - else if (magic.StartsWith(SabreTools.Models.PortableExecutable.Constants.SignatureBytes)) - { - stream.Seek(0, SeekOrigin.Begin); - return PortableExecutable.Create(stream); - } - - // Everything else fails - return null; - } - } -} diff --git a/Test/Extractor.cs b/Test/Extractor.cs index 00605e2e..66b21220 100644 --- a/Test/Extractor.cs +++ b/Test/Extractor.cs @@ -42,11 +42,7 @@ namespace Test } else if (Directory.Exists(path)) { -#if NET20 || NET35 - foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) -#else - foreach (string file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) -#endif + foreach (string file in IOExtensions.SafeEnumerateFiles(path, "*", SearchOption.AllDirectories)) { ExtractFile(file, outputDirectory); } @@ -70,13 +66,13 @@ namespace Test stream.Seek(0, SeekOrigin.Begin); // Get the file type - SupportedFileType ft = FileTypes.GetFileType(magic ?? []); + WrapperType ft = WrapperFactory.GetFileType(magic ?? []); // Executables technically can be "extracted", but let's ignore that // TODO: Support executables that include other stuff // 7-zip - if (ft == SupportedFileType.SevenZip) + if (ft == WrapperType.SevenZip) { // Build the archive information Console.WriteLine("Extracting 7-zip contents"); @@ -122,7 +118,7 @@ namespace Test } // BFPK archive - else if (ft == SupportedFileType.BFPK) + else if (ft == WrapperType.BFPK) { // Build the BFPK information Console.WriteLine("Extracting BFPK contents"); @@ -149,7 +145,7 @@ namespace Test } // BSP - else if (ft == SupportedFileType.BSP) + else if (ft == WrapperType.BSP) { // Build the BSP information Console.WriteLine("Extracting BSP contents"); @@ -177,7 +173,7 @@ namespace Test } // bzip2 - else if (ft == SupportedFileType.BZip2) + else if (ft == WrapperType.BZip2) { // Build the bzip2 information Console.WriteLine("Extracting bzip2 contents"); @@ -205,7 +201,7 @@ namespace Test } // CFB - else if (ft == SupportedFileType.CFB) + else if (ft == WrapperType.CFB) { // Build the installer information Console.WriteLine("Extracting CFB contents"); @@ -255,7 +251,7 @@ namespace Test } // GCF - else if (ft == SupportedFileType.GCF) + else if (ft == WrapperType.GCF) { // Build the GCF information Console.WriteLine("Extracting GCF contents"); @@ -282,7 +278,7 @@ namespace Test } // gzip - else if (ft == SupportedFileType.GZIP) + else if (ft == WrapperType.GZIP) { // Build the gzip information Console.WriteLine("Extracting gzip contents"); @@ -320,7 +316,7 @@ namespace Test } // InstallShield Archive V3 (Z) - else if (ft == SupportedFileType.InstallShieldArchiveV3) + else if (ft == WrapperType.InstallShieldArchiveV3) { // Build the InstallShield Archive V3 information Console.WriteLine("Extracting InstallShield Archive V3 contents"); @@ -365,7 +361,7 @@ namespace Test } // IS-CAB archive - else if (ft == SupportedFileType.InstallShieldCAB) + else if (ft == WrapperType.InstallShieldCAB) { // Build the archive information Console.WriteLine("Extracting IS-CAB contents"); @@ -416,7 +412,7 @@ namespace Test #if ((NETFRAMEWORK && !NET20 && !NET35 && !NET40) || NETCOREAPP) && WIN // Microsoft Cabinet archive - else if (ft == SupportedFileType.MicrosoftCAB) + else if (ft == WrapperType.MicrosoftCAB) { // Build the cabinet information Console.WriteLine("Extracting MS-CAB contents"); @@ -460,7 +456,7 @@ namespace Test #endif // Microsoft LZ / LZ32 - else if (ft == SupportedFileType.MicrosoftLZ) + else if (ft == WrapperType.MicrosoftLZ) { // Build the Microsoft LZ / LZ32 information Console.WriteLine("Extracting Microsoft LZ / LZ32 contents"); @@ -501,7 +497,7 @@ namespace Test #if ((NETFRAMEWORK && !NET20 && !NET35 && !NET40) || NETCOREAPP) && WIN // MoPaQ (MPQ) archive - else if (ft == SupportedFileType.MPQ) + else if (ft == WrapperType.MPQ) { // Build the archive information Console.WriteLine("Extracting MoPaQ contents"); @@ -561,7 +557,7 @@ namespace Test #endif // PAK - else if (ft == SupportedFileType.PAK) + else if (ft == WrapperType.PAK) { // Build the archive information Console.WriteLine("Extracting PAK contents"); @@ -588,7 +584,7 @@ namespace Test } // PFF - else if (ft == SupportedFileType.PFF) + else if (ft == WrapperType.PFF) { // Build the archive information Console.WriteLine("Extracting PFF contents"); @@ -615,7 +611,7 @@ namespace Test } // PKZIP - else if (ft == SupportedFileType.PKZIP) + else if (ft == WrapperType.PKZIP) { // Build the archive information Console.WriteLine("Extracting PKZIP contents"); @@ -664,7 +660,7 @@ namespace Test } // Quantum - else if (ft == SupportedFileType.Quantum) + else if (ft == WrapperType.Quantum) { // Build the archive information Console.WriteLine("Extracting Quantum contents"); @@ -691,7 +687,7 @@ namespace Test } // RAR - else if (ft == SupportedFileType.RAR) + else if (ft == WrapperType.RAR) { // Build the archive information Console.WriteLine("Extracting RAR contents"); @@ -737,7 +733,7 @@ namespace Test } // SGA - else if (ft == SupportedFileType.SGA) + else if (ft == WrapperType.SGA) { // Build the archive information Console.WriteLine("Extracting SGA contents"); @@ -764,7 +760,7 @@ namespace Test } // Tape Archive - else if (ft == SupportedFileType.RAR) + else if (ft == WrapperType.RAR) { // Build the archive information Console.WriteLine("Extracting Tape Archive contents"); @@ -810,7 +806,7 @@ namespace Test } // VBSP - else if (ft == SupportedFileType.VBSP) + else if (ft == WrapperType.VBSP) { // Build the archive information Console.WriteLine("Extracting VBSP contents"); @@ -837,7 +833,7 @@ namespace Test } // VPK - else if (ft == SupportedFileType.VPK) + else if (ft == WrapperType.VPK) { // Build the archive information Console.WriteLine("Extracting VPK contents"); @@ -864,7 +860,7 @@ namespace Test } // WAD - else if (ft == SupportedFileType.WAD) + else if (ft == WrapperType.WAD) { // Build the archive information Console.WriteLine("Extracting WAD contents"); @@ -891,7 +887,7 @@ namespace Test } // xz - else if (ft == SupportedFileType.RAR) + else if (ft == WrapperType.RAR) { // Build the xz information Console.WriteLine("Extracting xz contents"); @@ -918,7 +914,7 @@ namespace Test } // XZP - else if (ft == SupportedFileType.XZP) + else if (ft == WrapperType.XZP) { // Build the archive information Console.WriteLine("Extracting XZP contents"); diff --git a/Test/Printer.cs b/Test/Printer.cs index 421399c2..e818023d 100644 --- a/Test/Printer.cs +++ b/Test/Printer.cs @@ -1,8 +1,8 @@ using System; using System.IO; -using BinaryObjectScanner.Utilities; using SabreTools.IO.Extensions; -using SabreTools.Printing; +using SabreTools.Serialization.Wrappers; +using SPrinter = SabreTools.Serialization.Printer; namespace Test { @@ -25,11 +25,7 @@ namespace Test } else if (Directory.Exists(path)) { -#if NET20 || NET35 - foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) -#else - foreach (string file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) -#endif + foreach (string file in IOExtensions.SafeEnumerateFiles(path, "*", SearchOption.AllDirectories)) { PrintFileInfo(file, json, debug); } @@ -56,11 +52,11 @@ namespace Test stream.Seek(0, SeekOrigin.Begin); // Get the file type - SupportedFileType ft = FileTypes.GetFileType(magic ?? []); - if (ft == SupportedFileType.UNKNOWN) + WrapperType ft = WrapperFactory.GetFileType(magic ?? []); + if (ft == WrapperType.UNKNOWN) { string extension = Path.GetExtension(file).TrimStart('.'); - ft = FileTypes.GetFileType(extension); + ft = WrapperFactory.GetFileType(extension); } // Print out the file format @@ -95,7 +91,7 @@ namespace Test #endif // Create the output data - var builder = wrapper.ExportStringBuilder(); + var builder = SPrinter.ExportStringBuilder(wrapper); if (builder == null) { Console.WriteLine("No item information could be generated"); diff --git a/Test/Test.csproj b/Test/Test.csproj index 2c4b65e5..fd907f26 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -27,13 +27,12 @@ - - + + - - - + + \ No newline at end of file