diff --git a/README.MD b/README.MD index 37dda3a4..61ee15c2 100644 --- a/README.MD +++ b/README.MD @@ -25,6 +25,7 @@ Options: -c, --hash Output file hashes -f, --file Print to file only -j, --json Print info as JSON +-r, --recursive Recursively print info from embedded files ``` ## ExtractionTool diff --git a/SabreTools.Wrappers/ISO9660.Printing.cs b/SabreTools.Wrappers/ISO9660.Printing.cs index 8eb5546b..8b9b549a 100644 --- a/SabreTools.Wrappers/ISO9660.Printing.cs +++ b/SabreTools.Wrappers/ISO9660.Printing.cs @@ -4,7 +4,6 @@ using System.IO; using System.Text; using SabreTools.Data.Extensions; using SabreTools.Data.Models.ISO9660; -using SabreTools.IO.Extensions; using SabreTools.Matching; using SabreTools.Numerics.Extensions; using SabreTools.Text.Extensions; @@ -18,7 +17,7 @@ namespace SabreTools.Wrappers /// /// List of printed embedded files by their sector offset /// - private readonly HashSet printedFiles = []; + private readonly HashSet _printedFiles = []; #endregion @@ -33,7 +32,7 @@ namespace SabreTools.Wrappers /// public void PrintInformation(StringBuilder builder, bool recursive) { - printedFiles.Clear(); + _printedFiles.Clear(); builder.AppendLine("ISO 9660 Information:"); builder.AppendLine("-------------------------"); @@ -71,7 +70,7 @@ namespace SabreTools.Wrappers rootDirectoryRecord = svd.RootDirectoryRecord; else continue; - + var blockLength = vd.GetLogicalBlockSize(sectorLength); // TODO: Better encoding detection (EscapeSequences) @@ -95,7 +94,7 @@ namespace SabreTools.Wrappers // Expect a directory if (value is not DirectoryExtent dir) return; - + foreach (var dr in dir.DirectoryRecords) { string filename = encoding.GetString(dr.FileIdentifier); @@ -111,17 +110,17 @@ namespace SabreTools.Wrappers // Don't recurse up or self if (dr.FileIdentifier.EqualsExactly(Constants.CurrentDirectory) || dr.FileIdentifier.EqualsExactly(Constants.ParentDirectory)) continue; - + // Add extent before recursion - if (!printedFiles.Contains(dr.ExtentLocation.LittleEndian)) + if (!_printedFiles.Contains(dr.ExtentLocation.LittleEndian)) { - printedFiles.Add(dr.ExtentLocation.LittleEndian); + _printedFiles.Add(dr.ExtentLocation.LittleEndian); RecursivePrint(builder, dr.ExtentLocation.LittleEndian, path, encoding, blockLength, initialOffset); } - if (!dr.ExtentLocation.IsValid && !printedFiles.Contains(dr.ExtentLocation.BigEndian)) + if (!dr.ExtentLocation.IsValid && !_printedFiles.Contains(dr.ExtentLocation.BigEndian)) { - printedFiles.Add(dr.ExtentLocation.BigEndian); + _printedFiles.Add(dr.ExtentLocation.BigEndian); RecursivePrint(builder, dr.ExtentLocation.BigEndian, path, encoding, blockLength, initialOffset); } } @@ -132,11 +131,11 @@ namespace SabreTools.Wrappers continue; // Print embedded file from LittleEndian location - if (!printedFiles.Contains(dr.ExtentLocation.LittleEndian)) + if (!_printedFiles.Contains(dr.ExtentLocation.LittleEndian)) { try { - long offset = initialOffset + ((long)dr.ExtentLocation.LittleEndian + (long)dr.ExtendedAttributeRecordLength) * (long)blockLength; + long offset = initialOffset + ((dr.ExtentLocation.LittleEndian + (long)dr.ExtendedAttributeRecordLength) * blockLength); var wrapper = GetFileWrapper(offset, filename); if (wrapper is not null && wrapper is IPrintable printable) { @@ -145,7 +144,7 @@ namespace SabreTools.Wrappers builder.AppendLine("-------------------------"); printable.PrintInformation(builder, true); - printedFiles.Add(dr.ExtentLocation.LittleEndian); + _printedFiles.Add(dr.ExtentLocation.LittleEndian); } } catch @@ -156,11 +155,11 @@ namespace SabreTools.Wrappers } // Print embedded file from BigEndian location - if (!dr.ExtentLocation.IsValid && !printedFiles.Contains(dr.ExtentLocation.BigEndian)) + if (!dr.ExtentLocation.IsValid && !_printedFiles.Contains(dr.ExtentLocation.BigEndian)) { try { - long offset = initialOffset + (dr.ExtentLocation.BigEndian + dr.ExtendedAttributeRecordLength) * blockLength; + long offset = initialOffset + ((dr.ExtentLocation.BigEndian + dr.ExtendedAttributeRecordLength) * blockLength); var wrapper = GetFileWrapper(offset, filename); if (wrapper is not null && wrapper is IPrintable printable) { @@ -169,7 +168,7 @@ namespace SabreTools.Wrappers builder.AppendLine("-------------------------"); printable.PrintInformation(builder, true); - printedFiles.Add(dr.ExtentLocation.BigEndian); + _printedFiles.Add(dr.ExtentLocation.BigEndian); } } catch @@ -185,8 +184,10 @@ namespace SabreTools.Wrappers private IWrapper? GetFileWrapper(long offset, string filename) { _dataSource.Seek(offset, SeekOrigin.Begin); + byte[] magic = _dataSource.PeekBytes(16); string extension = Path.GetExtension(filename).TrimStart('.'); + WrapperType ft = WrapperFactory.GetFileType(magic, extension); return WrapperFactory.CreateWrapper(ft, _dataSource); } diff --git a/SabreTools.Wrappers/XDVDFS.Printing.cs b/SabreTools.Wrappers/XDVDFS.Printing.cs index 70a50217..ec3f7c83 100644 --- a/SabreTools.Wrappers/XDVDFS.Printing.cs +++ b/SabreTools.Wrappers/XDVDFS.Printing.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Text; using SabreTools.Data.Models.XDVDFS; -using SabreTools.IO.Extensions; using SabreTools.Numerics.Extensions; using SabreTools.Text.Extensions; @@ -16,7 +15,7 @@ namespace SabreTools.Wrappers /// /// List of printed embedded files by their sector offset /// - private readonly HashSet printedFiles = []; + private readonly HashSet _printedFiles = []; #endregion @@ -31,7 +30,7 @@ namespace SabreTools.Wrappers /// public void PrintInformation(StringBuilder builder, bool recursive) { - printedFiles.Clear(); + _printedFiles.Clear(); builder.AppendLine("Xbox DVD Filesystem Information:"); builder.AppendLine("-------------------------"); @@ -57,23 +56,23 @@ namespace SabreTools.Wrappers private void RecursivePrint(StringBuilder builder, uint sectorNumber, string filePath, long initialOffset) { - if (!Model.DirectoryDescriptors.ContainsKey(sectorNumber)) + if (!Model.DirectoryDescriptors.TryGetValue(sectorNumber, out DirectoryDescriptor? dd)) return; - foreach (DirectoryRecord dr in Model.DirectoryDescriptors[sectorNumber].DirectoryRecords) + foreach (DirectoryRecord dr in dd.DirectoryRecords) { string filename = Encoding.UTF8.GetString(dr.Filename); string path = Path.Combine(filePath, filename); // Skip already printed files - if (printedFiles.Contains(dr.ExtentOffset)) + if (_printedFiles.Contains(dr.ExtentOffset)) continue; // Recurse into directory if ((dr.FileFlags & FileFlags.DIRECTORY) == FileFlags.DIRECTORY) { // Add directory extent before recursing - printedFiles.Add(dr.ExtentOffset); + _printedFiles.Add(dr.ExtentOffset); RecursivePrint(builder, dr.ExtentOffset, path, initialOffset); continue; @@ -82,20 +81,22 @@ namespace SabreTools.Wrappers // Parse embedded file try { - _dataSource.Seek(initialOffset + Constants.SectorSize * dr.ExtentOffset, SeekOrigin.Begin); + _dataSource.Seek(initialOffset + (Constants.SectorSize * dr.ExtentOffset), SeekOrigin.Begin); + byte[] magic = _dataSource.PeekBytes(16); string extension = Path.GetExtension(filename).TrimStart('.'); + WrapperType ft = WrapperFactory.GetFileType(magic, extension); var wrapper = WrapperFactory.CreateWrapper(ft, _dataSource); if (wrapper is null || wrapper is not IPrintable printable) continue; - + // Print info for embedded file builder.AppendLine($"Information for {path}"); builder.AppendLine("-------------------------"); printable.PrintInformation(builder, true); - printedFiles.Add(dr.ExtentOffset); + _printedFiles.Add(dr.ExtentOffset); } catch {