From 9bff6d5fe16dcce62b9cc505657f42c1988275f4 Mon Sep 17 00:00:00 2001 From: SilasLaspada Date: Sat, 20 Mar 2021 22:29:19 -0600 Subject: [PATCH] Improve version detection (#29) * Improve version detection * Address comments * Address comments Co-authored-by: Matt Nadareski --- BurnOutSharp/PackerType/PECompact.cs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/BurnOutSharp/PackerType/PECompact.cs b/BurnOutSharp/PackerType/PECompact.cs index 22f97358..ac44c02e 100644 --- a/BurnOutSharp/PackerType/PECompact.cs +++ b/BurnOutSharp/PackerType/PECompact.cs @@ -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 { /// 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;