using System; using System.IO; using System.Text; using BinaryObjectScanner.Utilities; using BinaryObjectScanner.Wrappers; namespace Test { internal static class Printer { /// /// Wrapper to print information for a single path /// /// File or directory path /// Enable JSON output, if supported /// Enable debug output public static void PrintPathInfo(string path, bool json, bool debug) { Console.WriteLine($"Checking possible path: {path}"); // Check if the file or directory exists if (File.Exists(path)) { PrintFileInfo(path, json, debug); } else if (Directory.Exists(path)) { foreach (string file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) { PrintFileInfo(file, json, debug); } } else { Console.WriteLine($"{path} does not exist, skipping..."); } } /// /// Print information for a single file, if possible /// private static void PrintFileInfo(string file, bool json, bool debug) { Console.WriteLine($"Attempting to print info for {file}"); using (Stream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { // Read the first 8 bytes byte[] magic = stream.ReadBytes(8); stream.Seek(0, SeekOrigin.Begin); // Get the file type SupportedFileType ft = FileTypes.GetFileType(magic); if (ft == SupportedFileType.UNKNOWN) { string extension = Path.GetExtension(file).TrimStart('.'); ft = FileTypes.GetFileType(extension); } // Print out the file format Console.WriteLine($"File format found: {ft}"); // Setup the wrapper to print var wrapper = WrapperFactory.CreateWrapper(ft, stream); // If we don't have a wrapper if (wrapper == null) { Console.WriteLine($"Either {ft} is not supported or something went wrong during parsing!"); Console.WriteLine(); return; } // Print the wrapper name Console.WriteLine($"{wrapper.Description} wrapper created successfully!"); // Get the base info output name string filenameBase = $"info-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}"; #if NET6_0_OR_GREATER // If we have the JSON flag if (json) { // Create the output data string serializedData = wrapper.ExportJSON(); Console.WriteLine(serializedData); // Write the output data using (var sw = new StreamWriter(File.OpenWrite($"{filenameBase}.json"))) { sw.WriteLine(serializedData); } } #endif // Create the output data StringBuilder builder = wrapper.PrettyPrint(); Console.WriteLine(builder); // Write the output data using (var sw = new StreamWriter(File.OpenWrite($"{filenameBase}.txt"))) { sw.WriteLine(builder.ToString()); } } } } }