Zips are weird

This commit is contained in:
Matt Nadareski
2019-12-04 22:00:46 -08:00
parent 2a6b850aae
commit 1de4bc7b18

View File

@@ -2102,27 +2102,23 @@ namespace SabreTools.Library.Tools
/// <param name="chdsAsFiles">True if CHDs should be treated like regular files, false otherwise</param> /// <param name="chdsAsFiles">True if CHDs should be treated like regular files, false otherwise</param>
/// <returns>Populated BaseFile object if success, empty one on error</returns> /// <returns>Populated BaseFile object if success, empty one on error</returns>
public static BaseFile GetStreamInfo(Stream input, long size, Hash omitFromScan = 0x0, public static BaseFile GetStreamInfo(Stream input, long size, Hash omitFromScan = 0x0,
long offset = 0, bool keepReadOpen = false, bool chdsAsFiles = true) long? offset = null, bool keepReadOpen = false, bool chdsAsFiles = true)
{ {
// We first check to see if it's a CHD // We first check to see if it's a CHD
if (chdsAsFiles == false && GetCHDInfo(input) != null) if (chdsAsFiles == false && GetCHDInfo(input) != null)
{ {
// Seek to the starting position, if one is set // Seek to the starting position, if one is set
if (input.CanSeek) if (input.CanSeek && offset.HasValue)
{ {
try try
{ {
if (offset < 0) if (offset < 0)
{ {
input.Seek(offset, SeekOrigin.End); input.Seek(offset.Value, SeekOrigin.End);
} }
else if (offset > 0) else if (offset >= 0)
{ {
input.Seek(offset, SeekOrigin.Begin); input.Seek(offset.Value, SeekOrigin.Begin);
}
else
{
input.Seek(0, SeekOrigin.Begin);
} }
} }
catch (NotSupportedException) catch (NotSupportedException)
@@ -2176,21 +2172,17 @@ namespace SabreTools.Library.Tools
SHA512 sha512 = SHA512.Create(); SHA512 sha512 = SHA512.Create();
// Seek to the starting position, if one is set // Seek to the starting position, if one is set
if (input.CanSeek) if (input.CanSeek && offset.HasValue)
{ {
try try
{ {
if (offset < 0) if (offset < 0)
{ {
input.Seek(offset, SeekOrigin.End); input.Seek(offset.Value, SeekOrigin.End);
} }
else if (offset > 0) else if (offset >= 0)
{ {
input.Seek(offset, SeekOrigin.Begin); input.Seek(offset.Value, SeekOrigin.Begin);
}
else
{
input.Seek(0, SeekOrigin.Begin);
} }
} }
catch (NotSupportedException) catch (NotSupportedException)
@@ -2205,8 +2197,10 @@ namespace SabreTools.Library.Tools
byte[] buffer = new byte[32 * 1024 * 1024]; byte[] buffer = new byte[32 * 1024 * 1024];
int read; int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) while ((read = input.Read(buffer, 0, (int)Math.Min(size, buffer.Length))) > 0)
{ {
size -= read;
crc.Update(buffer, 0, read); crc.Update(buffer, 0, read);
if ((omitFromScan & Hash.MD5) == 0) if ((omitFromScan & Hash.MD5) == 0)
{ {
@@ -2228,6 +2222,9 @@ namespace SabreTools.Library.Tools
{ {
sha512.TransformBlock(buffer, 0, read, buffer, 0); sha512.TransformBlock(buffer, 0, read, buffer, 0);
} }
if (size <= 0)
break;
} }
crc.Update(buffer, 0, 0); crc.Update(buffer, 0, 0);