2020-07-15 09:41:59 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2020-12-07 12:33:24 -08:00
|
|
|
|
using SabreTools.Data;
|
2020-08-01 22:10:29 -07:00
|
|
|
|
using SabreTools.Library.DatFiles;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
using SabreTools.Library.FileTypes;
|
2020-10-07 15:42:30 -07:00
|
|
|
|
using SabreTools.Library.Logging;
|
2020-08-01 23:04:11 -07:00
|
|
|
|
using SabreTools.Library.Tools;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
using Compress.ThreadReaders;
|
|
|
|
|
|
|
2020-08-01 23:04:11 -07:00
|
|
|
|
namespace SabreTools.Library.IO
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Extensions to Stream functionality
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class StreamExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add an aribtrary number of bytes to the inputted stream
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Stream to be appended to</param>
|
|
|
|
|
|
/// <param name="output">Outputted stream</param>
|
|
|
|
|
|
/// <param name="bytesToAddToHead">Bytes to be added to head of stream</param>
|
|
|
|
|
|
/// <param name="bytesToAddToTail">Bytes to be added to tail of stream</param>
|
|
|
|
|
|
public static void AppendBytes(Stream input, Stream output, byte[] bytesToAddToHead, byte[] bytesToAddToTail)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Write out prepended bytes
|
|
|
|
|
|
if (bytesToAddToHead != null && bytesToAddToHead.Count() > 0)
|
|
|
|
|
|
output.Write(bytesToAddToHead, 0, bytesToAddToHead.Length);
|
|
|
|
|
|
|
|
|
|
|
|
// Now copy the existing file over
|
|
|
|
|
|
input.CopyTo(output);
|
|
|
|
|
|
|
|
|
|
|
|
// Write out appended bytes
|
|
|
|
|
|
if (bytesToAddToTail != null && bytesToAddToTail.Count() > 0)
|
|
|
|
|
|
output.Write(bytesToAddToTail, 0, bytesToAddToTail.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve file information for a single file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Filename to get information from</param>
|
|
|
|
|
|
/// <param name="size">Size of the input stream</param>
|
2020-10-05 17:43:44 -07:00
|
|
|
|
/// <param name="hashes">Hashes to include in the information</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param>
|
|
|
|
|
|
/// <returns>Populated BaseFile object if success, empty one on error</returns>
|
2020-10-05 17:43:44 -07:00
|
|
|
|
public static BaseFile GetInfo(this Stream input, long size = -1, Hash hashes = Hash.Standard, bool keepReadOpen = false)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-10-05 17:43:44 -07:00
|
|
|
|
return GetInfoAsync(input, size, hashes, keepReadOpen).ConfigureAwait(false).GetAwaiter().GetResult();
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve file information for a single file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Filename to get information from</param>
|
|
|
|
|
|
/// <param name="size">Size of the input stream</param>
|
2020-10-05 17:43:44 -07:00
|
|
|
|
/// <param name="hashes">Hashes to include in the information</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param>
|
|
|
|
|
|
/// <returns>Populated BaseFile object if success, empty one on error</returns>
|
2020-10-05 17:43:44 -07:00
|
|
|
|
public static async Task<BaseFile> GetInfoAsync(Stream input, long size = -1, Hash hashes = Hash.Standard, bool keepReadOpen = false)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If we want to automatically set the size
|
|
|
|
|
|
if (size == -1)
|
|
|
|
|
|
size = input.Length;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get a list of hashers to run over the buffer
|
2020-10-05 14:34:53 -07:00
|
|
|
|
List<Hasher> hashers = new List<Hasher>();
|
|
|
|
|
|
|
2020-10-05 17:43:44 -07:00
|
|
|
|
if (hashes.HasFlag(Hash.CRC))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.CRC));
|
|
|
|
|
|
if (hashes.HasFlag(Hash.MD5))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.MD5));
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#if NET_FRAMEWORK
|
2020-10-05 17:43:44 -07:00
|
|
|
|
if (hashes.HasFlag(Hash.RIPEMD160))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.RIPEMD160));
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#endif
|
2020-10-05 17:43:44 -07:00
|
|
|
|
if (hashes.HasFlag(Hash.SHA1))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.SHA1));
|
|
|
|
|
|
if (hashes.HasFlag(Hash.SHA256))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.SHA256));
|
|
|
|
|
|
if (hashes.HasFlag(Hash.SHA384))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.SHA384));
|
|
|
|
|
|
if (hashes.HasFlag(Hash.SHA512))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.SHA512));
|
|
|
|
|
|
if (hashes.HasFlag(Hash.SpamSum))
|
|
|
|
|
|
hashers.Add(new Hasher(Hash.SpamSum));
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// Initialize the hashing helpers
|
|
|
|
|
|
var loadBuffer = new ThreadLoadBuffer(input);
|
|
|
|
|
|
int buffersize = 3 * 1024 * 1024;
|
|
|
|
|
|
byte[] buffer0 = new byte[buffersize];
|
|
|
|
|
|
byte[] buffer1 = new byte[buffersize];
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
Please note that some of the following code is adapted from
|
|
|
|
|
|
RomVault. This is a modified version of how RomVault does
|
|
|
|
|
|
threaded hashing. As such, some of the terminology and code
|
|
|
|
|
|
is the same, though variable names and comments may have
|
|
|
|
|
|
been tweaked to better fit this code base.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// Pre load the first buffer
|
2020-10-02 16:23:52 -07:00
|
|
|
|
long refsize = size;
|
|
|
|
|
|
int next = refsize > buffersize ? buffersize : (int)refsize;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
input.Read(buffer0, 0, next);
|
|
|
|
|
|
int current = next;
|
2020-10-02 16:23:52 -07:00
|
|
|
|
refsize -= next;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
bool bufferSelect = true;
|
|
|
|
|
|
|
|
|
|
|
|
while (current > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Trigger the buffer load on the second buffer
|
2020-10-02 16:23:52 -07:00
|
|
|
|
next = refsize > buffersize ? buffersize : (int)refsize;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (next > 0)
|
|
|
|
|
|
loadBuffer.Trigger(bufferSelect ? buffer1 : buffer0, next);
|
|
|
|
|
|
|
|
|
|
|
|
byte[] buffer = bufferSelect ? buffer0 : buffer1;
|
|
|
|
|
|
|
|
|
|
|
|
// Run hashes in parallel
|
2020-10-05 17:43:44 -07:00
|
|
|
|
Parallel.ForEach(hashers, Globals.ParallelOptions, h => h.Process(buffer, current));
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// Wait for the load buffer worker, if needed
|
|
|
|
|
|
if (next > 0)
|
|
|
|
|
|
loadBuffer.Wait();
|
|
|
|
|
|
|
|
|
|
|
|
// Setup for the next hashing step
|
|
|
|
|
|
current = next;
|
2020-10-02 16:23:52 -07:00
|
|
|
|
refsize -= next;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
bufferSelect = !bufferSelect;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finalize all hashing helpers
|
|
|
|
|
|
loadBuffer.Finish();
|
2020-10-05 17:43:44 -07:00
|
|
|
|
Parallel.ForEach(hashers, Globals.ParallelOptions, h => h.Finalize());
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// Get the results
|
2020-09-17 23:07:49 -07:00
|
|
|
|
BaseFile baseFile = new BaseFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
Size = size,
|
2020-10-05 17:43:44 -07:00
|
|
|
|
CRC = hashes.HasFlag(Hash.CRC) ? hashers.First(h => h.HashType == Hash.CRC).GetHash() : null,
|
|
|
|
|
|
MD5 = hashes.HasFlag(Hash.MD5) ? hashers.First(h => h.HashType == Hash.MD5).GetHash() : null,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#if NET_FRAMEWORK
|
2020-10-05 17:43:44 -07:00
|
|
|
|
RIPEMD160 = hashes.HasFlag(Hash.RIPEMD160) ? hashers.First(h => h.HashType == Hash.RIPEMD160).GetHash() : null,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#endif
|
2020-10-05 17:43:44 -07:00
|
|
|
|
SHA1 = hashes.HasFlag(Hash.SHA1) ? hashers.First(h => h.HashType == Hash.SHA1).GetHash() : null,
|
|
|
|
|
|
SHA256 = hashes.HasFlag(Hash.SHA256) ? hashers.First(h => h.HashType == Hash.SHA256).GetHash() : null,
|
|
|
|
|
|
SHA384 = hashes.HasFlag(Hash.SHA384) ? hashers.First(h => h.HashType == Hash.SHA384).GetHash() : null,
|
|
|
|
|
|
SHA512 = hashes.HasFlag(Hash.SHA512) ? hashers.First(h => h.HashType == Hash.SHA512).GetHash() : null,
|
|
|
|
|
|
SpamSum = hashes.HasFlag(Hash.SpamSum) ? hashers.First(h => h.HashType == Hash.SpamSum).GetHash() : null,
|
2020-09-17 23:07:49 -07:00
|
|
|
|
};
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// Dispose of the hashers
|
|
|
|
|
|
loadBuffer.Dispose();
|
|
|
|
|
|
hashers.ForEach(h => h.Dispose());
|
2020-09-17 23:07:49 -07:00
|
|
|
|
|
|
|
|
|
|
return baseFile;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
2020-09-15 12:12:13 -07:00
|
|
|
|
catch (IOException ex)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
LoggerImpl.Warning(ex, "An exception occurred during hashing.");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return new BaseFile();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!keepReadOpen)
|
|
|
|
|
|
input.Dispose();
|
|
|
|
|
|
else
|
|
|
|
|
|
input.SeekIfPossible();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Seek to a specific point in the stream, if possible
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Input stream to try seeking on</param>
|
|
|
|
|
|
/// <param name="offset">Optional offset to seek to</param>
|
2020-08-28 01:13:55 -07:00
|
|
|
|
public static long SeekIfPossible(this Stream input, long offset = 0)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-08-28 01:13:55 -07:00
|
|
|
|
try
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-08-28 01:13:55 -07:00
|
|
|
|
if (input.CanSeek)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
|
|
return input.Seek(offset, SeekOrigin.End);
|
|
|
|
|
|
else if (offset >= 0)
|
|
|
|
|
|
return input.Seek(offset, SeekOrigin.Begin);
|
|
|
|
|
|
}
|
2020-08-28 01:13:55 -07:00
|
|
|
|
|
|
|
|
|
|
return input.Position;
|
|
|
|
|
|
}
|
2020-09-15 12:12:13 -07:00
|
|
|
|
catch (NotSupportedException ex)
|
2020-08-28 01:13:55 -07:00
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
LoggerImpl.Verbose(ex, "Stream does not support seeking to starting offset. Stream position not changed");
|
2020-08-28 01:13:55 -07:00
|
|
|
|
}
|
2020-09-15 12:12:13 -07:00
|
|
|
|
catch (NotImplementedException ex)
|
2020-08-28 01:13:55 -07:00
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
LoggerImpl.Warning(ex, "Stream does not support seeking to starting offset. Stream position not changed");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-28 01:13:55 -07:00
|
|
|
|
return -1;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|