Integrate with SabreHelper.dll

This commit is contained in:
Matt Nadareski
2016-04-12 23:24:08 -07:00
parent 8ace3106d0
commit 1b9589f973
3 changed files with 25 additions and 193 deletions

View File

@@ -1,119 +0,0 @@
// Copyright (c) Damien Guard. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace DamienG.Security.Cryptography
{
/// <summary>
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
/// </summary>
/// <remarks>
/// Crc32 should only be used for backward compatibility with older file formats
/// and algorithms. It is not secure enough for new applications.
/// If you need to call multiple times for the same data either use the HashAlgorithm
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
/// being passed in as the seed for the next Compute call.
/// </remarks>
public sealed class Crc32 : HashAlgorithm
{
public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu;
static UInt32[] defaultTable;
readonly UInt32 seed;
readonly UInt32[] table;
UInt32 hash;
public Crc32()
: this(DefaultPolynomial, DefaultSeed)
{
}
public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = hash = seed;
}
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
hash = CalculateHash(table, hash, array, ibStart, cbSize);
}
protected override byte[] HashFinal()
{
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
return hashBuffer;
}
public override int HashSize { get { return 32; } }
public static UInt32 Compute(byte[] buffer)
{
return Compute(DefaultSeed, buffer);
}
public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
return Compute(DefaultPolynomial, seed, buffer);
}
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
static UInt32[] InitializeTable(UInt32 polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
var createTable = new UInt32[256];
for (var i = 0; i < 256; i++)
{
var entry = (UInt32)i;
for (var j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
defaultTable = createTable;
return createTable;
}
static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
{
var crc = seed;
for (var i = start; i < size - start; i++)
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
return crc;
}
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
{
var result = BitConverter.GetBytes(uint32);
if (BitConverter.IsLittleEndian)
Array.Reverse(result);
return result;
}
}
}

View File

@@ -1,13 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Web; using System.Web;
//using SabreTools.Helper; using SabreTools.Helper;
using DamienG.Security.Cryptography; using DamienG.Security.Cryptography;
using SharpCompress.Archive; using SharpCompress.Archive;
using SharpCompress.Common; using SharpCompress.Common;
@@ -26,7 +25,6 @@ namespace SabreTools
// Extraction and listing related variables // Extraction and listing related variables
private static List<RomData> _roms; private static List<RomData> _roms;
private static bool _isMono;
// User specified variables // User specified variables
private static bool _noMD5; private static bool _noMD5;
@@ -42,6 +40,7 @@ namespace SabreTools
// Other required variables // Other required variables
private static string _date = DateTime.Now.ToString("yyyy-MM-dd"); private static string _date = DateTime.Now.ToString("yyyy-MM-dd");
private static Logger logger;
/// <summary> /// <summary>
/// Start help or use supplied parameters /// Start help or use supplied parameters
@@ -50,10 +49,9 @@ namespace SabreTools
public static void Main(string[] args) public static void Main(string[] args)
{ {
Console.Clear(); Console.Clear();
Console.Title = "DATFromDir " + "0.6.0.0"; Console.Title = "DATFromDir " + Build.Version;
//Console.Title = "DATFromDir " + Build.Version; logger = new Logger(false, "datfromdir.log");
//Logger logger = new Logger(false, "datfromdir.log"); logger.Start();
//logger.Start();
// First things first, take care of all of the arguments that this could have // First things first, take care of all of the arguments that this could have
_noMD5 = false; _noSHA1 = false; _forceunzip = false; _allfiles = false; _old = false; _noMD5 = false; _noSHA1 = false; _forceunzip = false; _allfiles = false; _old = false;
@@ -66,9 +64,8 @@ namespace SabreTools
case "-h": case "-h":
case "-?": case "-?":
case "--help": case "--help":
Help(); Build.Help();
//Build.Help(); logger.Close();
//logger.Close();
return; return;
case "-m": case "-m":
case "--noMD5": case "--noMD5":
@@ -122,15 +119,11 @@ namespace SabreTools
// If there's no inputs, show the help // If there's no inputs, show the help
if (inputs.Count == 0) if (inputs.Count == 0)
{ {
Help(); Build.Help();
//Build.Help(); logger.Close();
//logger.Close();
return; return;
} }
// Set 7za required variables
_isMono = (Type.GetType("Mono.Runtime") != null);
// Create an output array for all found items // Create an output array for all found items
_roms = new List<RomData>(); _roms = new List<RomData>();
@@ -144,12 +137,12 @@ namespace SabreTools
// This is where the main loop would go // This is where the main loop would go
if (File.Exists(_basePath)) if (File.Exists(_basePath))
{ {
Console.WriteLine("File found: " + _basePath); logger.Log("File found: " + _basePath);
ProcessFile(_basePath); ProcessFile(_basePath);
} }
else else
{ {
Console.WriteLine("Folder found: " + _basePath); logger.Log("Folder found: " + _basePath);
foreach (string item in Directory.EnumerateFiles(_basePath, "*", SearchOption.AllDirectories)) foreach (string item in Directory.EnumerateFiles(_basePath, "*", SearchOption.AllDirectories))
{ {
ProcessFile(item); ProcessFile(item);
@@ -256,40 +249,15 @@ namespace SabreTools
} }
sw.Write((_old ? ")" : "\t</machine>\n</datafile>")); sw.Write((_old ? ")" : "\t</machine>\n</datafile>"));
Console.Write("File written!" + Environment.NewLine); logger.Log("File written!" + Environment.NewLine);
//logger.Log("File + _desc + ".xml written!");
sw.Close(); sw.Close();
fs.Close(); fs.Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.ToString()); logger.Error(ex.ToString());
//logger.Error(ex.ToString());
} }
//logger.Close(); logger.Close();
}
/// <summary>
/// Show text-based help
/// </summary>
private static void Help()
{
Console.WriteLine(@"DATFromDir - Create a DAT file from a directory
-----------------------------------------
Usage: DATFromDir [options] [filename|dirname] <filename|dirname> ...
Options:
-h, -?, --help Show this help dialog
-m, --noMD5 Don't include MD5 in output
-s, --noSHA1 Don't include SHA1 in output
-u, --unzip Force unzipping in created DAT
-f, --files Treat archives as files
-o, --old Output DAT in RV format instead of XML
-n=, --name= Set the name of the DAT
-d=, --desc= Set the description of the DAT
-c=, --cat= Set the category of the DAT
-v=, --version= Set the version of the DAT
-a=, --author= Set the author of the DAT");
} }
/// <summary> /// <summary>
@@ -324,10 +292,10 @@ Options:
// If the file was an archive and was extracted successfully, check it // If the file was an archive and was extracted successfully, check it
if (!encounteredErrors) if (!encounteredErrors)
{ {
Console.WriteLine(Path.GetFileName(item) + " treated like an archive"); logger.Log(Path.GetFileName(item) + " treated like an archive");
foreach (string entry in Directory.EnumerateFiles(_tempDir, "*", SearchOption.AllDirectories)) foreach (string entry in Directory.EnumerateFiles(_tempDir, "*", SearchOption.AllDirectories))
{ {
Console.WriteLine("\tFound file: " + entry); logger.Log("\tFound file: " + entry);
string fileCRC = String.Empty; string fileCRC = String.Empty;
string fileMD5 = String.Empty; string fileMD5 = String.Empty;
string fileSHA1 = String.Empty; string fileSHA1 = String.Empty;
@@ -371,14 +339,13 @@ Options:
SHA1 = fileSHA1, SHA1 = fileSHA1,
}); });
Console.WriteLine("File added" + Environment.NewLine); logger.Log("File added" + Environment.NewLine);
//logger.Log("File parsed: " + entry.Remove(0, _tempDir.Length));
} }
} }
// Otherwise, just get the info on the file itself // Otherwise, just get the info on the file itself
else if (!Directory.Exists(item) && File.Exists(item)) else if (!Directory.Exists(item) && File.Exists(item))
{ {
Console.WriteLine(Path.GetFileName(item) + " treated like a file"); logger.Log(Path.GetFileName(item) + " treated like a file");
string fileCRC = String.Empty; string fileCRC = String.Empty;
string fileMD5 = String.Empty; string fileMD5 = String.Empty;
@@ -428,8 +395,7 @@ Options:
SHA1 = fileSHA1, SHA1 = fileSHA1,
}); });
Console.WriteLine("File added" + Environment.NewLine); logger.Log("File added" + Environment.NewLine);
//logger.Log("File parsed: " + item.Remove(0, _basePath.Length));
} }
catch (IOException) { } catch (IOException) { }
} }
@@ -440,25 +406,5 @@ Options:
di.Delete(true); di.Delete(true);
} }
} }
/// <summary>
/// Intermediate struct for holding and processing rom data
/// </summary>
public struct RomData
{
public string Manufacturer;
public string System;
public int SystemID;
public string Source;
public string URL;
public int SourceID;
public string Game;
public string Name;
public string Type;
public long Size;
public string CRC;
public string MD5;
public string SHA1;
}
} }
} }

View File

@@ -48,7 +48,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CRC32.cs" />
<Compile Include="DATFromDir.cs" /> <Compile Include="DATFromDir.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
@@ -56,6 +55,12 @@
<None Include="App.config" /> <None Include="App.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SabreHelper\SabreHelper.csproj">
<Project>{225a1afd-0890-44e8-b779-7502665c23a5}</Project>
<Name>SabreHelper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.