2016-03-28 01:01:56 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
using System.Data.SQLite;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
using System.IO;
|
2016-03-28 01:29:55 -07:00
|
|
|
|
using System.Linq;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
using System.Security.Cryptography;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
2016-04-06 00:26:13 -07:00
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Entry class for the Deheader application
|
|
|
|
|
|
/// </summary>
|
2016-03-30 01:31:07 -07:00
|
|
|
|
class Headerer
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
private static string _dbName = "Headerer.sqlite";
|
|
|
|
|
|
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
|
2016-04-09 04:07:46 -07:00
|
|
|
|
private static Logger logger;
|
2016-04-09 00:34:37 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Possible detected header type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private enum HeaderType
|
|
|
|
|
|
{
|
|
|
|
|
|
None = 0,
|
|
|
|
|
|
A7800,
|
|
|
|
|
|
FDS,
|
|
|
|
|
|
Lynx,
|
|
|
|
|
|
//N64,
|
|
|
|
|
|
NES,
|
|
|
|
|
|
PCE,
|
|
|
|
|
|
SNES,
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Start deheader operation with supplied parameters
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="args">String array representing command line parameters</param>
|
2016-03-28 01:01:56 -07:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
|
{
|
2016-04-09 04:07:46 -07:00
|
|
|
|
// Perform initial setup and verification
|
|
|
|
|
|
logger = new Logger(false, "database.log");
|
|
|
|
|
|
logger.Start();
|
2016-04-06 00:26:13 -07:00
|
|
|
|
DBTools.EnsureDatabase(_dbName, _connectionString);
|
2016-04-09 04:07:46 -07:00
|
|
|
|
Remapping.CreateHeaderSkips();
|
|
|
|
|
|
|
|
|
|
|
|
Console.Title = "Headerer " + Build.Version;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
2016-03-28 18:23:49 -07:00
|
|
|
|
if (args.Length == 0 || args.Length > 2)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
2016-03-28 01:01:56 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Get the filename (or foldername)
|
2016-03-28 18:23:49 -07:00
|
|
|
|
string file = "";
|
2016-03-30 02:36:23 -07:00
|
|
|
|
bool deheader = true;
|
2016-03-28 18:23:49 -07:00
|
|
|
|
if (args.Length == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
file = args[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (args[0] == "-e")
|
|
|
|
|
|
{
|
|
|
|
|
|
deheader = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (args[0] == "-r")
|
|
|
|
|
|
{
|
|
|
|
|
|
deheader = false;
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-03-30 02:36:23 -07:00
|
|
|
|
file = args[1];
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (deheader)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// If it's a single file, just check it
|
|
|
|
|
|
if (File.Exists(file))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
DetectRemoveHeader(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string sub in Directory.GetFiles(file))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
|
|
|
|
|
DetectRemoveHeader(sub);
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// Else, show that help text
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// If it's a single file, just check it
|
|
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
ReplaceHeader(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string sub in Directory.GetFiles(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
|
|
|
|
|
ReplaceHeader(sub);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Else, show that help text
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Detect and remove header from the given file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="file">Name of the file to be parsed</param>
|
2016-03-28 01:29:55 -07:00
|
|
|
|
private static void DetectRemoveHeader(string file)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Open the file in read mode
|
|
|
|
|
|
BinaryReader br = new BinaryReader(File.OpenRead(file));
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Extract the first 1024 bytes of the file
|
|
|
|
|
|
byte[] hbin = br.ReadBytes(1024);
|
2016-03-28 01:01:56 -07:00
|
|
|
|
string header = BitConverter.ToString(hbin).Replace("-", string.Empty);
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Determine the type of the file from the header, if possible
|
2016-04-09 00:34:37 -07:00
|
|
|
|
HeaderType type = HeaderType.None;
|
|
|
|
|
|
int headerSize = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Loop over the header types and see if there's a match
|
|
|
|
|
|
foreach (HeaderType test in Enum.GetValues(typeof(HeaderType)))
|
2016-03-28 01:29:55 -07:00
|
|
|
|
{
|
2016-04-09 00:34:37 -07:00
|
|
|
|
Dictionary<string, int> tempDict = new Dictionary<string, int>();
|
|
|
|
|
|
switch (test)
|
|
|
|
|
|
{
|
|
|
|
|
|
case HeaderType.A7800:
|
|
|
|
|
|
tempDict = Remapping.A7800;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HeaderType.FDS:
|
|
|
|
|
|
tempDict = Remapping.FDS;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HeaderType.Lynx:
|
|
|
|
|
|
tempDict = Remapping.Lynx;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HeaderType.PCE:
|
|
|
|
|
|
tempDict = Remapping.PCE;
|
|
|
|
|
|
break;
|
|
|
|
|
|
/*
|
|
|
|
|
|
case HeaderType.N64:
|
|
|
|
|
|
tempDict = Remapping.N64;
|
|
|
|
|
|
break;
|
|
|
|
|
|
*/
|
|
|
|
|
|
case HeaderType.NES:
|
|
|
|
|
|
tempDict = Remapping.NES;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HeaderType.SNES:
|
|
|
|
|
|
tempDict = Remapping.SNES;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Loop over the dictionary and see if there are matches
|
|
|
|
|
|
foreach (KeyValuePair<string, int> entry in tempDict)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Regex.IsMatch(header, entry.Key))
|
|
|
|
|
|
{
|
|
|
|
|
|
type = test;
|
|
|
|
|
|
headerSize = entry.Value;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we found something, break out
|
|
|
|
|
|
if (type != HeaderType.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-09 00:34:37 -07:00
|
|
|
|
Console.WriteLine("File has header: " + (type != HeaderType.None));
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-04-09 00:34:37 -07:00
|
|
|
|
if (type != HeaderType.None)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-28 01:29:55 -07:00
|
|
|
|
Console.WriteLine("Deteched header type: " + type);
|
2016-04-09 00:34:37 -07:00
|
|
|
|
int hs = headerSize;
|
2016-03-28 01:29:55 -07:00
|
|
|
|
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// Save header as string in the database
|
|
|
|
|
|
string realhead = "";
|
|
|
|
|
|
for (int i = 0; i < hs; i++)
|
2016-03-28 18:23:49 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
realhead += BitConverter.ToString(new byte[] { hbin[i] });
|
2016-03-28 18:23:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Get the bytes that aren't from the header from the extracted bit so they can be written before the rest of the file
|
|
|
|
|
|
hbin = hbin.Skip(hs).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
// Write out the new file
|
2016-03-28 01:01:56 -07:00
|
|
|
|
Console.WriteLine("Creating unheadered file: " + file + ".new");
|
|
|
|
|
|
BinaryWriter bw = new BinaryWriter(File.OpenWrite(file + ".new"));
|
|
|
|
|
|
FileInfo fi = new FileInfo(file);
|
2016-03-28 01:29:55 -07:00
|
|
|
|
bw.Write(hbin);
|
2016-03-28 01:01:56 -07:00
|
|
|
|
bw.Write(br.ReadBytes((int)fi.Length - hs));
|
|
|
|
|
|
bw.Close();
|
|
|
|
|
|
Console.WriteLine("Unheadered file created!");
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
// Now add the information to the database if it's not already there
|
|
|
|
|
|
SHA1 sha1 = SHA1.Create();
|
|
|
|
|
|
sha1.ComputeHash(File.ReadAllBytes(file + ".new"));
|
|
|
|
|
|
bool exists = false;
|
|
|
|
|
|
|
2016-03-31 12:38:58 -07:00
|
|
|
|
string query = @"SELECT * FROM data WHERE sha1='" + BitConverter.ToString(sha1.Hash) + "' AND header='" + realhead + "'";
|
2016-03-30 02:36:23 -07:00
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
exists = sldr.HasRows;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = @"INSERT INTO data (sha1, header, type) VALUES ('" +
|
|
|
|
|
|
BitConverter.ToString(sha1.Hash) + "', " +
|
|
|
|
|
|
"'" + realhead + "', " +
|
2016-04-09 00:34:37 -07:00
|
|
|
|
"'" + type.ToString() + "')";
|
2016-03-30 02:36:23 -07:00
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Result of inserting header: " + slc.ExecuteNonQuery());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
br.Close();
|
|
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-31 12:38:58 -07:00
|
|
|
|
/// Detect and replace header(s) to the given file
|
2016-03-30 02:36:23 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="file">Name of the file to be parsed</param>
|
|
|
|
|
|
private static void ReplaceHeader(string file)
|
|
|
|
|
|
{
|
|
|
|
|
|
// First, get the SHA-1 hash of the file
|
|
|
|
|
|
SHA1 sha1 = SHA1.Create();
|
|
|
|
|
|
sha1.ComputeHash(File.ReadAllBytes(file));
|
|
|
|
|
|
string hash = BitConverter.ToString(sha1.Hash);
|
|
|
|
|
|
|
2016-03-31 12:38:58 -07:00
|
|
|
|
// Then try to pull the corresponding headers from the database
|
2016-03-30 02:36:23 -07:00
|
|
|
|
string header = "";
|
|
|
|
|
|
|
|
|
|
|
|
string query = @"SELECT header, type FROM data WHERE sha1='" + hash + "'";
|
|
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-31 12:38:58 -07:00
|
|
|
|
int sub = 0;
|
|
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Found match with rom type " + sldr.GetString(1));
|
|
|
|
|
|
header = sldr.GetString(0);
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Creating reheadered file: " + file + ".new" + sub);
|
|
|
|
|
|
BinaryWriter bw = new BinaryWriter(File.OpenWrite(file + ".new" + sub));
|
|
|
|
|
|
|
|
|
|
|
|
// Source: http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa
|
|
|
|
|
|
for (int i = 0; i < header.Length; i += 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
bw.Write(Convert.ToByte(header.Substring(i, 2), 16));
|
|
|
|
|
|
}
|
|
|
|
|
|
bw.Write(File.ReadAllBytes(file));
|
|
|
|
|
|
bw.Close();
|
|
|
|
|
|
Console.WriteLine("Reheadered file created!");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("No matching header could be found!");
|
|
|
|
|
|
return;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|