diff --git a/SabreTools.Serialization/Readers/SpoonInstaller.cs b/SabreTools.Serialization/Readers/SpoonInstaller.cs index 6fa71826..6e53eeee 100644 --- a/SabreTools.Serialization/Readers/SpoonInstaller.cs +++ b/SabreTools.Serialization/Readers/SpoonInstaller.cs @@ -62,11 +62,8 @@ namespace SabreTools.Serialization.Readers /// Filled Footer on success, null on error public static Footer ParseFooter(Stream data) { - // Seek to the end of the end of the data - data.Seek(0, SeekOrigin.End); - - // Seek backward to the theoretical starts - data.Seek(-24, SeekOrigin.Current); + // Seek from the end (24 bytes has to use -23) + data.Seek(-23, SeekOrigin.End); var obj = new Footer(); diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.Extraction.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.Extraction.cs index 1d43b1a1..e0e49b19 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.Extraction.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.Extraction.cs @@ -50,6 +50,9 @@ namespace SabreTools.Serialization.Wrappers { try { + // Ensure the stream is starting at the beginning + _dataSource.Seek(0, SeekOrigin.Begin); + // Try to deserialize the source data var deserializer = new Readers.AdvancedInstaller(); var sfx = deserializer.Deserialize(_dataSource); @@ -541,6 +544,9 @@ namespace SabreTools.Serialization.Wrappers { try { + // Ensure the stream is starting at the beginning + _dataSource.Seek(0, SeekOrigin.Begin); + // Try to deserialize the source data var deserializer = new Readers.SpoonInstaller(); var sfx = deserializer.Deserialize(_dataSource); @@ -554,19 +560,23 @@ namespace SabreTools.Serialization.Wrappers // Get the offset and compressed size long offset = entry.FileOffset; - int size = (int)entry.CompressedSize; + int compressed = (int)entry.CompressedSize; + int extracted = (int)entry.UncompressedSize; // Try to read the file data - byte[] data = ReadRangeFromSource(offset, size); - if (data.Length == 0) + byte[] bz2Data = ReadRangeFromSource(offset, compressed); + if (bz2Data.Length == 0) continue; // Try opening the stream - using var ms = new MemoryStream(data); - using var bz2File = new BZip2InputStream(ms, true); + using var ms = new MemoryStream(bz2Data); + using var bz2File = new BZip2InputStream(ms, false); + + // Try to read the decompressed data + byte[] data = bz2File.ReadBytes(extracted); // Ensure directory separators are consistent - string filename = entry.Filename ?? $"FILE_{i}"; + string filename = entry.Filename?.TrimEnd('\0') ?? $"FILE_{i}"; if (Path.DirectorySeparatorChar == '\\') filename = filename.Replace('/', '\\'); else if (Path.DirectorySeparatorChar == '/') @@ -580,7 +590,7 @@ namespace SabreTools.Serialization.Wrappers // Write the output file var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); - bz2File.CopyTo(fs); + fs.Write(data, 0, data.Length); fs.Flush(); }