Add deheader to git

This code is based on the deheader experimental code in the web version. It is very rough but it works for now.
This commit is contained in:
Matt Nadareski
2016-03-28 01:01:56 -07:00
parent 35d892017c
commit d730b58dd6
5 changed files with 221 additions and 0 deletions

113
Deheader/Program.cs Normal file
View File

@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace Deheader
{
class Program
{
private static string help = @"Deheader.exe type filename|dirname
Type can be one of the following:
a7800, fds, lynx, nes, snes";
static void Main(string[] args)
{
// Type mapped to header size (in decimal bytes)
Dictionary<string, int> types = new Dictionary<string, int>();
types.Add("a7800", 128);
types.Add("fds", 16);
types.Add("lynx", 64);
types.Add("nes", 16);
types.Add("snes", 512);
if (args.Length != 2 || !types.ContainsKey(args[0]))
{
Console.WriteLine(help);
return;
}
// Get type of file and the filename (or foldername) itself
string type = args[0];
string file = args[1];
int hs = types[type];
// If it's a single file, just check it
if (File.Exists(file))
{
DetectRemoveHeader(type, file, hs);
}
// If it's a directory, recursively check all
else if (Directory.Exists(file))
{
foreach (string sub in Directory.GetFiles(file))
{
if (sub != ".." && sub != ".")
{
DetectRemoveHeader(type, sub, hs);
}
}
}
// Else, show that help text
else
{
Console.WriteLine(help);
}
}
private static void DetectRemoveHeader(string type, string file, int hs)
{
// Open the file in read mode
BinaryReader br = new BinaryReader(File.OpenRead(file));
// Extract the header from the file
byte[] hbin = br.ReadBytes(hs);
string header = BitConverter.ToString(hbin).Replace("-", string.Empty);
Console.WriteLine("Possible header: " + header);
// Deal with each possible type
bool hasHeader = false;
switch (type)
{
case "a7800":
bool a7800a = Regex.IsMatch(header, "^.415441524937383030");
bool a7800b = Regex.IsMatch(header, "^.{64}41435455414C20434152542044415441205354415254532048455245");
hasHeader = a7800a || a7800b;
break;
case "fds":
hasHeader = Regex.IsMatch(header, "^4644531A0[1-4]0000000000000000000000");
break;
case "lynx":
bool lynxa = Regex.IsMatch(header, "^4C594E58");
bool lynxb = Regex.IsMatch(header, "^425339");
hasHeader = lynxa || lynxb;
break;
case "nes":
hasHeader = Regex.IsMatch(header, "^4E45531A");
break;
case "snes":
bool fig = Regex.IsMatch(header, "^.{16}0000000000000000");
bool smc = Regex.IsMatch(header, "^.{16}AABB040000000000");
bool ufo = Regex.IsMatch(header, "^.{16}535550455255464F");
hasHeader = fig || smc || ufo;
break;
}
Console.WriteLine("File has header: " + hasHeader);
if (hasHeader)
{
Console.WriteLine("Creating unheadered file: " + file + ".new");
BinaryWriter bw = new BinaryWriter(File.OpenWrite(file + ".new"));
FileInfo fi = new FileInfo(file);
bw.Write(br.ReadBytes((int)fi.Length - hs));
bw.Close();
Console.WriteLine("Unheadered file created!");
}
br.Close();
}
}
}