2016-03-28 01:01:56 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2016-03-28 01:29:55 -07:00
|
|
|
|
using System.Linq;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Deheader
|
|
|
|
|
|
{
|
|
|
|
|
|
class Program
|
|
|
|
|
|
{
|
2016-03-28 01:10:21 -07:00
|
|
|
|
private static Dictionary<string, int> types;
|
2016-03-28 01:29:55 -07:00
|
|
|
|
private static string help = @"Deheader.exe filename|dirname";
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Type mapped to header size (in decimal bytes)
|
2016-03-28 01:10:21 -07:00
|
|
|
|
types = new Dictionary<string, int>();
|
2016-03-28 01:01:56 -07:00
|
|
|
|
types.Add("a7800", 128);
|
|
|
|
|
|
types.Add("fds", 16);
|
|
|
|
|
|
types.Add("lynx", 64);
|
|
|
|
|
|
types.Add("nes", 16);
|
|
|
|
|
|
types.Add("snes", 512);
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
if (args.Length != 1)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(help);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Get the filename (or foldername)
|
|
|
|
|
|
string file = args[0];
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
|
|
|
|
|
// If it's a single file, just check it
|
|
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
|
{
|
2016-03-28 01:29:55 -07:00
|
|
|
|
DetectRemoveHeader(file);
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string sub in Directory.GetFiles(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
2016-03-28 01:29:55 -07:00
|
|
|
|
DetectRemoveHeader(sub);
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Else, show that help text
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(help);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
|
string type = "";
|
2016-03-28 01:48:28 -07:00
|
|
|
|
if (Regex.IsMatch(header, "^.415441524937383030") || Regex.IsMatch(header, "^.{200}41435455414C20434152542044415441205354415254532048455245"))
|
2016-03-28 01:29:55 -07:00
|
|
|
|
{
|
|
|
|
|
|
type = "a7800";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(header, "^4644531A0[1-4]0000000000000000000000"))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-28 01:29:55 -07:00
|
|
|
|
type = "fds";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(header, "^4C594E58") || Regex.IsMatch(header, "^425339"))
|
|
|
|
|
|
{
|
|
|
|
|
|
type = "lynx";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(header, "^4E45531A"))
|
|
|
|
|
|
{
|
|
|
|
|
|
type = "nes";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(header, "^.{16}0000000000000000") || Regex.IsMatch(header, "^.{16}AABB040000000000") || Regex.IsMatch(header, "^.{16}535550455255464F")) // fig, smc, ufo
|
|
|
|
|
|
{
|
|
|
|
|
|
type = "snes";
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
Console.WriteLine("File has header: " + (type != ""));
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
if (type != "")
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-28 01:29:55 -07:00
|
|
|
|
Console.WriteLine("Deteched header type: " + type);
|
|
|
|
|
|
int hs = types[type];
|
|
|
|
|
|
|
|
|
|
|
|
// 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!");
|
|
|
|
|
|
}
|
|
|
|
|
|
br.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|