[Skippers] Add stream-based versions of check and transform; convert current versions to use this internally

This commit is contained in:
Matt Nadareski
2016-09-17 17:30:25 -07:00
parent e395ad2301
commit 0b52d2502e

View File

@@ -311,29 +311,42 @@ namespace SabreTools.Helper
/// Get the SkipperRule associated with a given file
/// </summary>
/// <param name="input">Name of the file to be checked</param>
/// <param name="skippername">Name of the skipper to be used</param>
/// <param name="skipperName">Name of the skipper to be used</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>The SkipperRule that matched the file</returns>
public static SkipperRule MatchesSkipper(string input, string skippername, Logger logger)
public static SkipperRule MatchesSkipper(string input, string skipperName, Logger logger)
{
SkipperRule skipperRule = new SkipperRule();
// If the file doesn't exist, return a blank skipper rule
if (!File.Exists(input))
{
logger.Error("The file '" + input + "' does not exist so it cannot be tested");
return skipperRule;
return new SkipperRule();
}
return MatchesSkipper(File.OpenRead(input), skipperName, logger);
}
/// <summary>
/// Get the SkipperRule associated with a given stream
/// </summary>
/// <param name="input">Name of the file to be checked</param>
/// <param name="skipperName">Name of the skipper to be used</param>
/// <param name="logger">Logger object for file and console output</param>
/// <param name="keepOpen">True if the underlying stream should be kept open, false otherwise</param>
/// <returns>The SkipperRule that matched the file</returns>
public static SkipperRule MatchesSkipper(Stream input, string skipperName, Logger logger, bool keepOpen = false)
{
SkipperRule skipperRule = new SkipperRule();
// Loop through and find a Skipper that has the right name
logger.Log("Beginning search for matching header skip rules");
foreach (Skipper skipper in List)
{
if (String.IsNullOrEmpty(skippername) || (!String.IsNullOrEmpty(skipper.Name) && skippername.ToLowerInvariant() == skipper.Name.ToLowerInvariant()))
if (String.IsNullOrEmpty(skipperName) || (!String.IsNullOrEmpty(skipper.Name) && skipperName.ToLowerInvariant() == skipper.Name.ToLowerInvariant()))
{
// Loop through the rules until one is found that works
using (BinaryReader br = new BinaryReader(File.OpenRead(input)))
{
BinaryReader br = new BinaryReader(input);
foreach (SkipperRule rule in skipper.Rules)
{
// Always reset the stream back to the original place
@@ -461,6 +474,13 @@ namespace SabreTools.Helper
}
}
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
{
br.Close();
br.Dispose();
}
// If we still have a success, then return this rule
if (success)
{
@@ -470,7 +490,6 @@ namespace SabreTools.Helper
}
}
}
}
// If we have a blank rule, inform the user
if (skipperRule.Tests == null)
@@ -488,7 +507,7 @@ namespace SabreTools.Helper
/// <param name="output">Output file name</param>
/// <param name="rule">SkipperRule to apply to the file</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns></returns>
/// <returns>True if the file was transformed properly, false otherwise</returns>
public static bool TransformFile(string input, string output, SkipperRule rule, Logger logger)
{
bool success = true;
@@ -506,23 +525,58 @@ namespace SabreTools.Helper
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(output));
}
logger.User("Attempting to apply rule to '" + input + "'");
success = TransformStream(File.OpenRead(input), File.OpenWrite(output), rule, logger);
// If the output file has size 0, delete it
if (new FileInfo(output).Length == 0)
{
try
{
File.Delete(output);
}
catch
{
// Don't log this file deletion error
}
}
return success;
}
/// <summary>
/// Transform an input stream using the given rule
/// </summary>
/// <param name="input">Input stream</param>
/// <param name="output">Output stream</param>
/// <param name="rule">SkipperRule to apply to the stream</param>
/// <param name="logger">Logger object for file and console output</param>
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param>
/// <param name="keepWriteOpen">True if the underlying write stream should be kept open, false otherwise</param>
/// <returns>True if the file was transformed properly, false otherwise</returns>
public static bool TransformStream(Stream input, Stream output, SkipperRule rule, Logger logger, bool keepReadOpen = false, bool keepWriteOpen = false)
{
bool success = true;
// If the sizes are wrong for the values, fail
long extsize = new FileInfo(input).Length;
long extsize = input.Length;
if ((rule.Operation > HeaderSkipOperation.Bitswap && (extsize % 2) != 0)
|| (rule.Operation > HeaderSkipOperation.Byteswap && (extsize % 4) != 0)
|| (rule.Operation > HeaderSkipOperation.Bitswap && (rule.StartOffset == null || rule.StartOffset % 2 == 0)))
{
logger.Error("The file did not have the correct size to be transformed!");
logger.Error("The stream did not have the correct size to be transformed!");
return false;
}
// Now read the proper part of the file and apply the rule
BinaryWriter bw = null;
BinaryReader br = null;
try
{
logger.User("Applying found rule to file '" + input + "'");
using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(output)))
using (BinaryReader br = new BinaryReader(File.OpenRead(input)))
{
logger.User("Applying found rule to input stream");
bw = new BinaryWriter(output);
br = new BinaryReader(input);
// Seek to the beginning offset
if (rule.StartOffset == null)
{
@@ -604,25 +658,26 @@ namespace SabreTools.Helper
bw.Write(buffer[i]);
}
}
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
return false;
}
// If the output file has size 0, delete it
if (new FileInfo(output).Length == 0)
finally
{
try
// If we're not keeping the read stream open, dispose of the binary reader
if (!keepReadOpen)
{
File.Delete(output);
br?.Close();
br?.Dispose();
}
catch
// If we're not keeping the write stream open, dispose of the binary reader
if (!keepWriteOpen)
{
// Don't log this file deletion error
bw?.Close();
bw?.Dispose();
}
}