From 11fa1544f1d51c1073e7f62a5d6cd9e23a542f29 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 17 Jul 2024 14:46:14 -0400 Subject: [PATCH] Gate more seeks --- SabreTools.FileTypes/Archives/GZipArchive.cs | 16 +++++++------- SabreTools.FileTypes/Archives/RarArchive.cs | 14 +++++++------ .../Archives/SevenZipArchive.cs | 21 ++++++++++++------- SabreTools.FileTypes/Archives/TapeArchive.cs | 14 +++++++------ SabreTools.FileTypes/Archives/XZArchive.cs | 18 +++++++++------- SabreTools.FileTypes/Archives/ZipArchive.cs | 17 ++++++++------- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/SabreTools.FileTypes/Archives/GZipArchive.cs b/SabreTools.FileTypes/Archives/GZipArchive.cs index b310304c..8fc7e8e3 100644 --- a/SabreTools.FileTypes/Archives/GZipArchive.cs +++ b/SabreTools.FileTypes/Archives/GZipArchive.cs @@ -130,10 +130,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -146,21 +146,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } @@ -226,7 +228,7 @@ namespace SabreTools.FileTypes.Archives BaseFile gzipEntryRom = new(); // Perform a quickscan, if flagged to - if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32) + if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32) { gzipEntryRom.Filename = gamename; diff --git a/SabreTools.FileTypes/Archives/RarArchive.cs b/SabreTools.FileTypes/Archives/RarArchive.cs index ea53f0a1..f5811f75 100644 --- a/SabreTools.FileTypes/Archives/RarArchive.cs +++ b/SabreTools.FileTypes/Archives/RarArchive.cs @@ -96,10 +96,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -110,21 +110,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } diff --git a/SabreTools.FileTypes/Archives/SevenZipArchive.cs b/SabreTools.FileTypes/Archives/SevenZipArchive.cs index a6cd908b..e991a36f 100644 --- a/SabreTools.FileTypes/Archives/SevenZipArchive.cs +++ b/SabreTools.FileTypes/Archives/SevenZipArchive.cs @@ -183,10 +183,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -197,21 +197,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } @@ -445,7 +447,8 @@ namespace SabreTools.FileTypes.Archives return success; // Seek to the beginning of the stream - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent?.EndsWith(".7z") ?? false ? string.Empty : ".7z")); @@ -465,7 +468,9 @@ namespace SabreTools.FileTypes.Archives // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); + zipReturn = zipFile.ZipFileCreate(tempFile); // Open the input file for reading diff --git a/SabreTools.FileTypes/Archives/TapeArchive.cs b/SabreTools.FileTypes/Archives/TapeArchive.cs index 6838e570..f1f99d90 100644 --- a/SabreTools.FileTypes/Archives/TapeArchive.cs +++ b/SabreTools.FileTypes/Archives/TapeArchive.cs @@ -97,10 +97,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -111,21 +111,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } diff --git a/SabreTools.FileTypes/Archives/XZArchive.cs b/SabreTools.FileTypes/Archives/XZArchive.cs index 31854bef..e26f8360 100644 --- a/SabreTools.FileTypes/Archives/XZArchive.cs +++ b/SabreTools.FileTypes/Archives/XZArchive.cs @@ -114,10 +114,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -128,21 +128,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } @@ -208,10 +210,10 @@ namespace SabreTools.FileTypes.Archives BaseFile xzEntryRom = new(); // Perform a quickscan, if flagged to - if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32) + if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32) { xzEntryRom.Filename = gamename; - + using BinaryReader br = new(File.OpenRead(this.Filename!)); br.BaseStream.Seek(-8, SeekOrigin.End); xzEntryRom.CRC = br.ReadBytesBigEndian(4); diff --git a/SabreTools.FileTypes/Archives/ZipArchive.cs b/SabreTools.FileTypes/Archives/ZipArchive.cs index 149db892..0e14ed3a 100644 --- a/SabreTools.FileTypes/Archives/ZipArchive.cs +++ b/SabreTools.FileTypes/Archives/ZipArchive.cs @@ -158,10 +158,10 @@ namespace SabreTools.FileTypes.Archives public override string? CopyToFile(string entryName, string outDir) { // Try to extract a stream using the given information - (Stream? ms, string? realEntry) = GetEntryStream(entryName); + (Stream? stream, string? realEntry) = GetEntryStream(entryName); // If the stream and the entry name are both non-null, we write to file - if (ms != null && realEntry != null) + if (stream != null && realEntry != null) { realEntry = Path.Combine(outDir, realEntry); @@ -172,21 +172,23 @@ namespace SabreTools.FileTypes.Archives FileStream fs = File.Create(realEntry); if (fs != null) { - ms.Seek(0, SeekOrigin.Begin); + if (stream.CanSeek) + stream.Seek(0, SeekOrigin.Begin); + byte[] zbuffer = new byte[_bufferSize]; int zlen; - while ((zlen = ms.Read(zbuffer, 0, _bufferSize)) > 0) + while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0) { fs.Write(zbuffer, 0, zlen); fs.Flush(); } - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); } else { - ms?.Dispose(); + stream?.Dispose(); fs?.Dispose(); realEntry = null; } @@ -408,7 +410,8 @@ namespace SabreTools.FileTypes.Archives return success; // Seek to the beginning of the stream - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); // Get the output archive name from the first rebuild rom string archiveFileName = Path.Combine(outDir, TextHelper.RemovePathUnsafeCharacters(baseFile.Parent) + (baseFile.Parent!.EndsWith(".zip") ? string.Empty : ".zip"));