diff --git a/BinaryObjectScanner/BinaryObjectScanner.csproj b/BinaryObjectScanner/BinaryObjectScanner.csproj index 9fa21f26..7da2e0c1 100644 --- a/BinaryObjectScanner/BinaryObjectScanner.csproj +++ b/BinaryObjectScanner/BinaryObjectScanner.csproj @@ -85,10 +85,10 @@ - + - - + + diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 245f7385..0cd2306a 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -8,7 +8,9 @@ using System.Linq; #if NET462_OR_GREATER || NETCOREAPP using System.Text; #endif +#if NET40_OR_GREATER || NETCOREAPP using System.Threading.Tasks; +#endif using BinaryObjectScanner.FileType; using BinaryObjectScanner.Interfaces; using BinaryObjectScanner.Utilities; @@ -327,11 +329,7 @@ namespace BinaryObjectScanner } // Get the file type either from magic number or extension - WrapperType fileType = WrapperFactory.GetFileType(magic); - if (fileType == WrapperType.UNKNOWN) - fileType = WrapperFactory.GetFileType(extension); - - // If we still got unknown, just return null + WrapperType fileType = WrapperFactory.GetFileType(magic, extension); if (fileType == WrapperType.UNKNOWN) return null; diff --git a/Test/Extractor.cs b/Test/Extractor.cs index 5098f300..3f365ade 100644 --- a/Test/Extractor.cs +++ b/Test/Extractor.cs @@ -3,7 +3,6 @@ using System.IO; #if NET452_OR_GREATER || NETCOREAPP using System.Text; #endif -using BinaryObjectScanner.Utilities; #if NET40_OR_GREATER || NETCOREAPP using OpenMcdf; #endif @@ -24,14 +23,43 @@ using UnshieldSharp.Archive; namespace Test { - internal static class Extractor + internal class Extractor { + #region Options + + /// + public bool IncludeDebug => _options?.IncludeDebug ?? false; + + /// + /// Options object for configuration + /// + private readonly BinaryObjectScanner.Options _options; + + #endregion + + /// + /// Constructor + /// + /// Enable including debug information + public Extractor(bool includeDebug) + { + this._options = new BinaryObjectScanner.Options + { + IncludeDebug = includeDebug, + }; + +#if NET462_OR_GREATER || NETCOREAPP + // Register the codepages + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); +#endif + } + /// /// Wrapper to extract data for a single path /// /// File or directory path /// Output directory path - public static void ExtractPath(string path, string outputDirectory) + public void ExtractPath(string path, string outputDirectory) { Console.WriteLine($"Checking possible path: {path}"); @@ -56,17 +84,30 @@ namespace Test /// /// Print information for a single file, if possible /// - private static void ExtractFile(string file, string outputDirectory) + private void ExtractFile(string file, string outputDirectory) { Console.WriteLine($"Attempting to extract all files from {file}"); using Stream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - // Read the first 8 bytes - byte[]? magic = stream.ReadBytes(8); - stream.Seek(0, SeekOrigin.Begin); + // Get the extension for certain checks + string extension = Path.GetExtension(file).ToLower().TrimStart('.'); + + // Get the first 16 bytes for matching + byte[] magic = new byte[16]; + try + { + stream.Read(magic, 0, 16); + stream.Seek(0, SeekOrigin.Begin); + } + catch (Exception ex) + { + if (IncludeDebug) Console.WriteLine(ex); + + return; + } // Get the file type - WrapperType ft = WrapperFactory.GetFileType(magic ?? []); + WrapperType ft = WrapperFactory.GetFileType(magic, extension); // Executables technically can be "extracted", but let's ignore that // TODO: Support executables that include other stuff diff --git a/Test/Printer.cs b/Test/Printer.cs index e818023d..465297a4 100644 --- a/Test/Printer.cs +++ b/Test/Printer.cs @@ -1,20 +1,50 @@ using System; using System.IO; +#if NET452_OR_GREATER || NETCOREAPP +using System.Text; +#endif using SabreTools.IO.Extensions; using SabreTools.Serialization.Wrappers; using SPrinter = SabreTools.Serialization.Printer; namespace Test { - internal static class Printer + internal class Printer { + #region Options + + /// + public bool IncludeDebug => _options?.IncludeDebug ?? false; + /// - /// Wrapper to print information for a single path + /// Options object for configuration /// - /// File or directory path - /// Enable JSON output, if supported - /// Enable debug output - public static void PrintPathInfo(string path, bool json, bool debug) + private readonly BinaryObjectScanner.Options _options; + + #endregion + + /// + /// Constructor + /// + /// Enable including debug information + public Printer(bool includeDebug) + { + this._options = new BinaryObjectScanner.Options + { + IncludeDebug = includeDebug, + }; + +#if NET462_OR_GREATER || NETCOREAPP + // Register the codepages + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); +#endif + }/// + /// Wrapper to print information for a single path + /// + /// File or directory path + /// Enable JSON output, if supported + /// Enable debug output + public void PrintPathInfo(string path, bool json, bool debug) { Console.WriteLine($"Checking possible path: {path}"); @@ -39,7 +69,7 @@ namespace Test /// /// Print information for a single file, if possible /// - private static void PrintFileInfo(string file, bool json, bool debug) + private void PrintFileInfo(string file, bool json, bool debug) { Console.WriteLine($"Attempting to print info for {file}"); @@ -47,17 +77,25 @@ namespace Test { using Stream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - // Read the first 8 bytes - byte[]? magic = stream.ReadBytes(8); - stream.Seek(0, SeekOrigin.Begin); + // Get the extension for certain checks + string extension = Path.GetExtension(file).ToLower().TrimStart('.'); + + // Get the first 16 bytes for matching + byte[] magic = new byte[16]; + try + { + stream.Read(magic, 0, 16); + stream.Seek(0, SeekOrigin.Begin); + } + catch (Exception ex) + { + if (IncludeDebug) Console.WriteLine(ex); + + return; + } // Get the file type - WrapperType ft = WrapperFactory.GetFileType(magic ?? []); - if (ft == WrapperType.UNKNOWN) - { - string extension = Path.GetExtension(file).TrimStart('.'); - ft = WrapperFactory.GetFileType(extension); - } + WrapperType ft = WrapperFactory.GetFileType(magic, extension); // Print out the file format Console.WriteLine($"File format found: {ft}"); diff --git a/Test/Program.cs b/Test/Program.cs index 6d618560..8cad82a9 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -28,6 +28,12 @@ namespace Test return; } + // Create extractor for all paths + var extractor = new Extractor(options.Debug); + + // Create printer for all paths + var printer = new Printer(options.Debug); + // Create scanner for all paths var scanner = new Scanner( options.ScanArchives, @@ -43,14 +49,14 @@ namespace Test { // Extraction if (options.EnableExtraction) - Extractor.ExtractPath(inputPath, options.OutputPath); + extractor.ExtractPath(inputPath, options.OutputPath); // Information printing if (options.EnableInformation) #if NETFRAMEWORK - Printer.PrintPathInfo(inputPath, false, options.Debug); + printer.PrintPathInfo(inputPath, false, options.Debug); #else - Printer.PrintPathInfo(inputPath, options.Json, options.Debug); + printer.PrintPathInfo(inputPath, options.Json, options.Debug); #endif // Scanning diff --git a/Test/Test.csproj b/Test/Test.csproj index 9e9982ab..800ed586 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -28,10 +28,10 @@ - + - - + +