From d730b58dd65ea0d2ef5a2d854725a45d3b5e24e7 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 28 Mar 2016 01:01:56 -0700 Subject: [PATCH] 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. --- DATabase.sln | 6 ++ Deheader/App.config | 6 ++ Deheader/Deheader.csproj | 60 +++++++++++++++ Deheader/Program.cs | 113 ++++++++++++++++++++++++++++ Deheader/Properties/AssemblyInfo.cs | 36 +++++++++ 5 files changed, 221 insertions(+) create mode 100644 Deheader/App.config create mode 100644 Deheader/Deheader.csproj create mode 100644 Deheader/Program.cs create mode 100644 Deheader/Properties/AssemblyInfo.cs diff --git a/DATabase.sln b/DATabase.sln index ac02aa72..ad7e5ff7 100644 --- a/DATabase.sln +++ b/DATabase.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.23107.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DATabase", "DATabase\DATabase.csproj", "{3B615702-1866-4D7B-8AF1-7B43FD0CC1D0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deheader", "Deheader\Deheader.csproj", "{66339C8A-F331-467C-B311-08A8F7284671}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {3B615702-1866-4D7B-8AF1-7B43FD0CC1D0}.Debug|Any CPU.Build.0 = Debug|Any CPU {3B615702-1866-4D7B-8AF1-7B43FD0CC1D0}.Release|Any CPU.ActiveCfg = Release|Any CPU {3B615702-1866-4D7B-8AF1-7B43FD0CC1D0}.Release|Any CPU.Build.0 = Release|Any CPU + {66339C8A-F331-467C-B311-08A8F7284671}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {66339C8A-F331-467C-B311-08A8F7284671}.Debug|Any CPU.Build.0 = Debug|Any CPU + {66339C8A-F331-467C-B311-08A8F7284671}.Release|Any CPU.ActiveCfg = Release|Any CPU + {66339C8A-F331-467C-B311-08A8F7284671}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Deheader/App.config b/Deheader/App.config new file mode 100644 index 00000000..88fa4027 --- /dev/null +++ b/Deheader/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Deheader/Deheader.csproj b/Deheader/Deheader.csproj new file mode 100644 index 00000000..fbf3da0c --- /dev/null +++ b/Deheader/Deheader.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {66339C8A-F331-467C-B311-08A8F7284671} + Exe + Properties + Deheader + Deheader + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Deheader/Program.cs b/Deheader/Program.cs new file mode 100644 index 00000000..0eb2db93 --- /dev/null +++ b/Deheader/Program.cs @@ -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 types = new Dictionary(); + 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(); + } + } +} diff --git a/Deheader/Properties/AssemblyInfo.cs b/Deheader/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..6896661b --- /dev/null +++ b/Deheader/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Deheader")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Deheader")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("66339c8a-f331-467c-b311-08a8f7284671")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]