Improve version detection (#29)

* Improve version detection

* Address comments

* Address comments

Co-authored-by: Matt Nadareski <mnadareski@outlook.com>
This commit is contained in:
SilasLaspada
2021-03-20 22:29:19 -06:00
committed by GitHub
parent 544aaed9da
commit 9bff6d5fe1

View File

@@ -1,13 +1,32 @@
namespace BurnOutSharp.PackerType
using System;
using System.IO;
namespace BurnOutSharp.PackerType
{
// TODO: Add extraction and better version detection
public class PECompact : IContentCheck
{
/// <inheritdoc/>
public string CheckContents(string file, byte[] fileContent, bool includePosition = false)
{
// "PEC2"
byte?[] check = new byte?[] { 0x50, 0x45, 0x43, 0x32 };
// Another possible version string for version 1 is "PECO" (50 45 43 4F)
// "pec1"
byte?[] check = new byte?[] { 0x70, 0x65, 0x63, 0x31 };
if (fileContent.FirstPosition(check, out int position, end: 2048))
return "PE Compact 1" + (includePosition ? $" (Index {position})" : string.Empty);
// "PEC2"
check = new byte?[] { 0x50, 0x45, 0x43, 0x32 };
if (fileContent.FirstPosition(check, out position, end: 2048))
{
int version = BitConverter.ToInt16(fileContent, position + 4);
return $"PE Compact 2 v{version}" + (includePosition ? $" (Index {position})" : string.Empty);
}
// "PECompact2"
check = new byte?[] { 0x50, 0x45, 0x43, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x32 };
if (fileContent.FirstPosition(check, out position))
return "PE Compact 2" + (includePosition ? $" (Index {position})" : string.Empty);
return null;