Attempt to make GetInfo safer

This commit is contained in:
Matt Nadareski
2025-01-05 22:55:03 -05:00
parent 6ca59a3170
commit 8129d76d78
3 changed files with 19 additions and 21 deletions

View File

@@ -240,7 +240,7 @@ namespace SabreTools.FileTypes.Archives
var gz = new gZip(); var gz = new gZip();
ZipReturn ret = gz.ZipFileOpen(Filename); ZipReturn ret = gz.ZipFileOpen(Filename);
ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize); ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize);
gzipEntryRom = FileTypeTool.GetInfo(gzstream, _hashTypes); gzipEntryRom = FileTypeTool.GetInfo(gzstream, (long)streamSize, _hashTypes);
gzipEntryRom.Filename = gz.GetLocalFile(0).Filename; gzipEntryRom.Filename = gz.GetLocalFile(0).Filename;
gzipEntryRom.Parent = gamename; gzipEntryRom.Parent = gamename;
gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null); gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null);
@@ -406,7 +406,7 @@ namespace SabreTools.FileTypes.Archives
#region Writing #region Writing
/// <inheritdoc/> /// <inheritdoc/>
public override bool Write(string file, string outDir, BaseFile? baseFile = null) public override bool Write(string file, string outDir, BaseFile? baseFile)
{ {
// Check that the input file exists // Check that the input file exists
if (!File.Exists(file)) if (!File.Exists(file))
@@ -423,7 +423,7 @@ namespace SabreTools.FileTypes.Archives
} }
/// <inheritdoc/> /// <inheritdoc/>
public override bool Write(Stream? stream, string outDir, BaseFile? baseFile = null) public override bool Write(Stream? stream, string outDir, BaseFile? baseFile)
{ {
bool success = false; bool success = false;
@@ -437,8 +437,8 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// Now get the Rom info for the file so we have hashes and size // If the base file is null, get the hash information
baseFile = FileTypeTool.GetInfo(stream, _hashTypes, keepReadOpen: true); baseFile ??= FileTypeTool.GetInfo(stream, -1, _hashTypes, keepReadOpen: true);
// Get the output file name // Get the output file name
string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty); string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty);
@@ -457,7 +457,7 @@ namespace SabreTools.FileTypes.Archives
BinaryWriter sw = new(outputStream); BinaryWriter sw = new(outputStream);
// Write standard header and TGZ info // Write standard header and TGZ info
byte[] data = [.. TorrentGZHeader, .. baseFile.MD5!, .. baseFile.CRC!]; byte[] data = [.. TorrentGZHeader, .. baseFile!.MD5!, .. baseFile.CRC!];
sw.Write(data); sw.Write(data);
sw.Write((ulong)(baseFile.Size ?? 0)); // Long size (Unsigned, Mirrored) sw.Write((ulong)(baseFile.Size ?? 0)); // Long size (Unsigned, Mirrored)

View File

@@ -216,7 +216,7 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
var xzStream = new XZStream(File.Open(Filename!, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); var xzStream = new XZStream(File.Open(Filename!, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
xzEntryRom = FileTypeTool.GetInfo(xzStream, _hashTypes); xzEntryRom = FileTypeTool.GetInfo(xzStream, size: -1, _hashTypes);
xzEntryRom.Filename = gamename; xzEntryRom.Filename = gamename;
xzStream.Dispose(); xzStream.Dispose();
} }
@@ -331,8 +331,8 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// Now get the Rom info for the file so we have hashes and size // If the base file is null, get the hash information
baseFile = FileTypeTool.GetInfo(stream, _hashTypes, keepReadOpen: true); baseFile ??= FileTypeTool.GetInfo(stream, -1, _hashTypes, keepReadOpen: true);
// Get the output file name // Get the output file name
string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!); string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!);

View File

@@ -60,15 +60,6 @@ namespace SabreTools.FileTypes
} }
} }
/// <summary>
/// Retrieve file information for a single stream
/// </summary>
/// <param name="input">Stream to get information from</param>
/// <param name="hashes">Hashes to include in the information</param>
/// <returns>Populated BaseFile object if success, null on error</returns>
public static BaseFile GetInfo(Stream? input, HashType[] hashes)
=> GetInfo(input, size: -1, hashes, keepReadOpen: false);
/// <summary> /// <summary>
/// Retrieve file information for a single stream /// Retrieve file information for a single stream
/// </summary> /// </summary>
@@ -105,9 +96,16 @@ namespace SabreTools.FileTypes
try try
{ {
// If we want to automatically set the size try
if (size == -1) {
size = input.Length; // If we want to automatically set the size
if (size == -1)
size = input.Length;
}
catch
{
// Don't set the length if the stream doesn't support it
}
// Run the hashing on the input stream // Run the hashing on the input stream
var hashDict = HashTool.GetStreamHashes(input, hashes); var hashDict = HashTool.GetStreamHashes(input, hashes);