diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ExportAddressTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ExportAddressTableEntry.cs index 3f3b0d8a..cbe505f3 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ExportAddressTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ExportAddressTableEntry.cs @@ -39,7 +39,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var eate = new ExportAddressTableEntry(); - eate.ExportRVA = BitConverter.ToUInt32(content, offset); offset += 4; + eate.ExportRVA = content.ReadUInt32(ref offset); eate.ForwarderRVA = eate.ExportRVA; return eate; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/HintNameTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/HintNameTableEntry.cs index 294b75f8..cb1a8b0a 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/HintNameTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/HintNameTableEntry.cs @@ -62,7 +62,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var hnte = new HintNameTableEntry(); - hnte.Hint = BitConverter.ToUInt16(content, offset); offset += 2; + hnte.Hint = content.ReadUInt16(ref offset); hnte.Name = string.Empty; while (true) { diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportAddressTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportAddressTableEntry.cs index b3eaf7fd..25eff610 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportAddressTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportAddressTableEntry.cs @@ -70,11 +70,11 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var iate = new ImportAddressTableEntry(); - iate.ImportLookupTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; - iate.TimeDateStamp = BitConverter.ToUInt32(content, offset); offset += 4; - iate.ForwarderChain = BitConverter.ToUInt32(content, offset); offset += 4; - iate.NameRVA = BitConverter.ToUInt32(content, offset); offset += 4; - iate.ImportAddressTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; + iate.ImportLookupTableRVA = content.ReadUInt32(ref offset); + iate.TimeDateStamp = content.ReadUInt32(ref offset); + iate.ForwarderChain = content.ReadUInt32(ref offset); + iate.NameRVA = content.ReadUInt32(ref offset); + iate.ImportAddressTableRVA = content.ReadUInt32(ref offset); return iate; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportDirectoryTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportDirectoryTableEntry.cs index 08d3d426..ffa37703 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportDirectoryTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ImportDirectoryTableEntry.cs @@ -70,11 +70,11 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var idte = new ImportDirectoryTableEntry(); - idte.ImportLookupTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; - idte.TimeDateStamp = BitConverter.ToUInt32(content, offset); offset += 4; - idte.ForwarderChain = BitConverter.ToUInt32(content, offset); offset += 4; - idte.NameRVA = BitConverter.ToUInt32(content, offset); offset += 4; - idte.ImportAddressTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; + idte.ImportLookupTableRVA = content.ReadUInt32(ref offset); + idte.TimeDateStamp = content.ReadUInt32(ref offset); + idte.ForwarderChain = content.ReadUInt32(ref offset); + idte.NameRVA = content.ReadUInt32(ref offset); + idte.ImportAddressTableRVA = content.ReadUInt32(ref offset); return idte; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceNameString.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceNameString.cs index 4385bd06..076a1f1e 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceNameString.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceNameString.cs @@ -35,7 +35,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var rds = new NEResourceNameString(); - rds.Length = content[offset++]; + rds.Length = content.ReadByte(ref offset); rds.Value = Encoding.ASCII.GetChars(content, offset, rds.Length); offset += rds.Length; return rds; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceTableEntry.cs index 879d827d..2aab74ef 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/NEResourceTableEntry.cs @@ -63,12 +63,12 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var ni = new NEResourceTableEntry(); - ni.Offset = BitConverter.ToUInt16(content, offset); offset += 2; - ni.Length = BitConverter.ToUInt16(content, offset); offset += 2; - ni.Flags = (ResourceTableEntryFlags)BitConverter.ToUInt16(content, offset); offset += 2; - ni.ResourceID = BitConverter.ToUInt16(content, offset); offset += 2; - ni.Handle = BitConverter.ToUInt16(content, offset); offset += 2; - ni.Usage = BitConverter.ToUInt16(content, offset); offset += 2; + ni.Offset = content.ReadUInt16(ref offset); + ni.Length = content.ReadUInt16(ref offset); + ni.Flags = (ResourceTableEntryFlags)content.ReadUInt16(ref offset); + ni.ResourceID = content.ReadUInt16(ref offset); + ni.Handle = content.ReadUInt16(ref offset); + ni.Usage = content.ReadUInt16(ref offset); return ni; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/NESegmentTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/NESegmentTableEntry.cs index 5813435e..b05c2eb7 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/NESegmentTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/NESegmentTableEntry.cs @@ -51,10 +51,10 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var nste = new NESegmentTableEntry(); - nste.StartFileSector = BitConverter.ToUInt16(content, offset); offset += 2; - nste.BytesInFile = BitConverter.ToUInt16(content, offset); offset += 2; - nste.Flags = (SegmentTableEntryFlags)BitConverter.ToUInt16(content, offset); offset += 2; - nste.MinimumAllocation = BitConverter.ToUInt16(content, offset); offset += 2; + nste.StartFileSector = content.ReadUInt16(ref offset); + nste.BytesInFile = content.ReadUInt16(ref offset); + nste.Flags = (SegmentTableEntryFlags)content.ReadUInt16(ref offset); + nste.MinimumAllocation = content.ReadUInt16(ref offset); return nste; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDataEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDataEntry.cs index c6bb2da8..164aa98f 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDataEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDataEntry.cs @@ -89,10 +89,10 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var rde = new ResourceDataEntry(); - rde.OffsetToData = BitConverter.ToUInt32(content, offset); offset += 4; - rde.Size = BitConverter.ToUInt32(content, offset); offset += 4; - rde.CodePage = BitConverter.ToUInt32(content, offset); offset += 4; - rde.Reserved = BitConverter.ToUInt32(content, offset); offset += 4; + rde.OffsetToData = content.ReadUInt32(ref offset); + rde.Size = content.ReadUInt32(ref offset); + rde.CodePage = content.ReadUInt32(ref offset); + rde.Reserved = content.ReadUInt32(ref offset); int realOffsetToData = (int)PortableExecutable.ConvertVirtualAddress(rde.OffsetToData, sections); if (realOffsetToData > -1 && realOffsetToData < content.Length && (int)rde.Size > 0 && realOffsetToData + (int)rde.Size < content.Length) diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryString.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryString.cs index 07198cf7..dc9ce473 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryString.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryString.cs @@ -39,7 +39,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var rds = new ResourceDirectoryString(); - rds.Length = BitConverter.ToUInt16(content, offset); offset += 2; + rds.Length = content.ReadUInt16(ref offset); if (rds.Length + offset > content.Length) return null; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryTableEntry.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryTableEntry.cs index bed8391e..66351076 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryTableEntry.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceDirectoryTableEntry.cs @@ -113,7 +113,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var rdte = new ResourceDirectoryTableEntry(); - rdte.IntegerId = BitConverter.ToUInt32(content, offset); offset += 4; + rdte.IntegerId = content.ReadUInt32(ref offset); if (!rdte.IsIntegerIDEntry()) { int nameAddress = (int)(rdte.NameOffset + sectionStart); @@ -121,7 +121,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries rdte.Name = ResourceDirectoryString.Deserialize(content, ref nameAddress); } - rdte.DataEntryOffset = BitConverter.ToUInt32(content, offset); offset += 4; + rdte.DataEntryOffset = content.ReadUInt32(ref offset); if (rdte.IsResourceDataEntry()) { int dataEntryAddress = (int)(rdte.DataEntryOffset + sectionStart); diff --git a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceTypeInformationBlock.cs b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceTypeInformationBlock.cs index b23a0f66..5383db8a 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceTypeInformationBlock.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Entries/ResourceTypeInformationBlock.cs @@ -54,9 +54,9 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Entries { var rtib = new ResourceTypeInformationBlock(); - rtib.TypeID = BitConverter.ToUInt16(content, offset); offset += 2; - rtib.ResourceCount = BitConverter.ToUInt16(content, offset); offset += 2; - rtib.Reserved = BitConverter.ToUInt32(content, offset); offset += 4; + rtib.TypeID = content.ReadUInt16(ref offset); + rtib.ResourceCount = content.ReadUInt16(ref offset); + rtib.Reserved = content.ReadUInt32(ref offset); rtib.ResourceTable = new NEResourceTableEntry[rtib.ResourceCount]; for (int i = 0; i < rtib.ResourceCount; i++) diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/CommonObjectFileFormatHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/CommonObjectFileFormatHeader.cs index c3245451..ed63d5fe 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/CommonObjectFileFormatHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/CommonObjectFileFormatHeader.cs @@ -73,14 +73,14 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers { var ifh = new CommonObjectFileFormatHeader(); - ifh.Signature = BitConverter.ToUInt32(content, offset); offset += 4; - ifh.Machine = (MachineType)BitConverter.ToUInt16(content, offset); offset += 2; - ifh.NumberOfSections = BitConverter.ToUInt16(content, offset); offset += 2; - ifh.TimeDateStamp = BitConverter.ToUInt32(content, offset); offset += 4; - ifh.PointerToSymbolTable = BitConverter.ToUInt32(content, offset); offset += 4; - ifh.NumberOfSymbols = BitConverter.ToUInt32(content, offset); offset += 4; - ifh.SizeOfOptionalHeader = BitConverter.ToUInt16(content, offset); offset += 2; - ifh.Characteristics = (ImageObjectCharacteristics)BitConverter.ToUInt16(content, offset); offset += 2; + ifh.Signature = content.ReadUInt32(ref offset); + ifh.Machine = (MachineType)content.ReadUInt16(ref offset); + ifh.NumberOfSections = content.ReadUInt16(ref offset); + ifh.TimeDateStamp = content.ReadUInt32(ref offset); + ifh.PointerToSymbolTable = content.ReadUInt32(ref offset); + ifh.NumberOfSymbols = content.ReadUInt32(ref offset); + ifh.SizeOfOptionalHeader = content.ReadUInt16(ref offset); + ifh.Characteristics = (ImageObjectCharacteristics)content.ReadUInt16(ref offset); return ifh; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/DataDirectoryHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/DataDirectoryHeader.cs index 3a26ee63..e2f6347d 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/DataDirectoryHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/DataDirectoryHeader.cs @@ -31,8 +31,8 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers { var ddh = new DataDirectoryHeader(); - ddh.VirtualAddress = BitConverter.ToUInt32(content, offset); offset += 4; - ddh.Size = BitConverter.ToUInt32(content, offset); offset += 4; + ddh.VirtualAddress = content.ReadUInt32(ref offset); + ddh.Size = content.ReadUInt32(ref offset); return ddh; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/MSDOSExecutableHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/MSDOSExecutableHeader.cs index ebbc55cd..74d6ef52 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/MSDOSExecutableHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/MSDOSExecutableHeader.cs @@ -168,20 +168,20 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers { MSDOSExecutableHeader idh = new MSDOSExecutableHeader(); - idh.Magic = BitConverter.ToUInt16(content, offset); offset += 2; - idh.LastPageBytes = BitConverter.ToUInt16(content, offset); offset += 2; - idh.Pages = BitConverter.ToUInt16(content, offset); offset += 2; - idh.Relocations = BitConverter.ToUInt16(content, offset); offset += 2; - idh.HeaderParagraphSize = BitConverter.ToUInt16(content, offset); offset += 2; - idh.MinimumExtraParagraphs = BitConverter.ToUInt16(content, offset); offset += 2; - idh.MaximumExtraParagraphs = BitConverter.ToUInt16(content, offset); offset += 2; - idh.InitialSSValue = BitConverter.ToUInt16(content, offset); offset += 2; - idh.InitialSPValue = BitConverter.ToUInt16(content, offset); offset += 2; - idh.Checksum = BitConverter.ToUInt16(content, offset); offset += 2; - idh.InitialIPValue = BitConverter.ToUInt16(content, offset); offset += 2; - idh.InitialCSValue = BitConverter.ToUInt16(content, offset); offset += 2; - idh.RelocationTableAddr = BitConverter.ToUInt16(content, offset); offset += 2; - idh.OverlayNumber = BitConverter.ToUInt16(content, offset); offset += 2; + idh.Magic = content.ReadUInt16(ref offset); + idh.LastPageBytes = content.ReadUInt16(ref offset); + idh.Pages = content.ReadUInt16(ref offset); + idh.Relocations = content.ReadUInt16(ref offset); + idh.HeaderParagraphSize = content.ReadUInt16(ref offset); + idh.MinimumExtraParagraphs = content.ReadUInt16(ref offset); + idh.MaximumExtraParagraphs = content.ReadUInt16(ref offset); + idh.InitialSSValue = content.ReadUInt16(ref offset); + idh.InitialSPValue = content.ReadUInt16(ref offset); + idh.Checksum = content.ReadUInt16(ref offset); + idh.InitialIPValue = content.ReadUInt16(ref offset); + idh.InitialCSValue = content.ReadUInt16(ref offset); + idh.RelocationTableAddr = content.ReadUInt16(ref offset); + idh.OverlayNumber = content.ReadUInt16(ref offset); // If we're not reading as a stub, return now if (!asStub) @@ -190,16 +190,16 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers idh.Reserved1 = new ushort[Constants.ERES1WDS]; for (int i = 0; i < Constants.ERES1WDS; i++) { - idh.Reserved1[i] = BitConverter.ToUInt16(content, offset); offset += 2; + idh.Reserved1[i] = content.ReadUInt16(ref offset); } - idh.OEMIdentifier = BitConverter.ToUInt16(content, offset); offset += 2; - idh.OEMInformation = BitConverter.ToUInt16(content, offset); offset += 2; + idh.OEMIdentifier = content.ReadUInt16(ref offset); + idh.OEMInformation = content.ReadUInt16(ref offset); idh.Reserved2 = new ushort[Constants.ERES2WDS]; for (int i = 0; i < Constants.ERES2WDS; i++) { - idh.Reserved2[i] = BitConverter.ToUInt16(content, offset); offset += 2; + idh.Reserved2[i] = content.ReadUInt16(ref offset); } - idh.NewExeHeaderAddr = BitConverter.ToInt32(content, offset); offset += 4; + idh.NewExeHeaderAddr = content.ReadInt32(ref offset); return idh; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/NewExecutableHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/NewExecutableHeader.cs index 7d97a4c4..6514e493 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/NewExecutableHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/NewExecutableHeader.cs @@ -218,38 +218,38 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers { var neh = new NewExecutableHeader(); - neh.Magic = BitConverter.ToUInt16(content, offset); offset += 2; - neh.LinkerVersion = content[offset++]; - neh.LinkerRevision = content[offset++]; - neh.EntryTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.EntryTableSize = BitConverter.ToUInt16(content, offset); offset += 2; - neh.CrcChecksum = BitConverter.ToUInt32(content, offset); offset += 4; - neh.ProgramFlags = content[offset++]; - neh.ApplicationFlags = content[offset++]; - neh.Autodata = BitConverter.ToUInt16(content, offset); offset += 2; - neh.InitialHeapAlloc = BitConverter.ToUInt16(content, offset); offset += 2; - neh.InitialStackAlloc = BitConverter.ToUInt16(content, offset); offset += 2; - neh.InitialCSIPSetting = BitConverter.ToUInt32(content, offset); offset += 4; - neh.InitialSSSPSetting = BitConverter.ToUInt32(content, offset); offset += 4; - neh.FileSegmentCount = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ModuleReferenceTableSize = BitConverter.ToUInt16(content, offset); offset += 2; - neh.NonResidentNameTableSize = BitConverter.ToUInt16(content, offset); offset += 2; - neh.SegmentTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ResourceTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ResidentNameTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ModuleReferenceTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ImportedNamesTableOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.NonResidentNamesTableOffset = BitConverter.ToUInt32(content, offset); offset += 4; - neh.MovableEntriesCount = BitConverter.ToUInt16(content, offset); offset += 2; - neh.SegmentAlignmentShiftCount = BitConverter.ToUInt16(content, offset); offset += 2; - neh.ResourceEntriesCount = BitConverter.ToUInt16(content, offset); offset += 2; - neh.TargetOperatingSystem = content[offset++]; - neh.AdditionalFlags = content[offset++]; - neh.ReturnThunkOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.SegmentReferenceThunkOffset = BitConverter.ToUInt16(content, offset); offset += 2; - neh.MinCodeSwapAreaSize = BitConverter.ToUInt16(content, offset); offset += 2; - neh.WindowsSDKRevision = content[offset++]; - neh.WindowsSDKVersion = content[offset++]; + neh.Magic = content.ReadUInt16(ref offset); + neh.LinkerVersion = content.ReadByte(ref offset); + neh.LinkerRevision = content.ReadByte(ref offset); + neh.EntryTableOffset = content.ReadUInt16(ref offset); + neh.EntryTableSize = content.ReadUInt16(ref offset); + neh.CrcChecksum = content.ReadUInt32(ref offset); + neh.ProgramFlags = content.ReadByte(ref offset); + neh.ApplicationFlags = content.ReadByte(ref offset); + neh.Autodata = content.ReadUInt16(ref offset); + neh.InitialHeapAlloc = content.ReadUInt16(ref offset); + neh.InitialStackAlloc = content.ReadUInt16(ref offset); + neh.InitialCSIPSetting = content.ReadUInt32(ref offset); + neh.InitialSSSPSetting = content.ReadUInt32(ref offset); + neh.FileSegmentCount = content.ReadUInt16(ref offset); + neh.ModuleReferenceTableSize = content.ReadUInt16(ref offset); + neh.NonResidentNameTableSize = content.ReadUInt16(ref offset); + neh.SegmentTableOffset = content.ReadUInt16(ref offset); + neh.ResourceTableOffset = content.ReadUInt16(ref offset); + neh.ResidentNameTableOffset = content.ReadUInt16(ref offset); + neh.ModuleReferenceTableOffset = content.ReadUInt16(ref offset); + neh.ImportedNamesTableOffset = content.ReadUInt16(ref offset); + neh.NonResidentNamesTableOffset = content.ReadUInt32(ref offset); + neh.MovableEntriesCount = content.ReadUInt16(ref offset); + neh.SegmentAlignmentShiftCount = content.ReadUInt16(ref offset); + neh.ResourceEntriesCount = content.ReadUInt16(ref offset); + neh.TargetOperatingSystem = content.ReadByte(ref offset); + neh.AdditionalFlags = content.ReadByte(ref offset); + neh.ReturnThunkOffset = content.ReadUInt16(ref offset); + neh.SegmentReferenceThunkOffset = content.ReadUInt16(ref offset); + neh.MinCodeSwapAreaSize = content.ReadUInt16(ref offset); + neh.WindowsSDKRevision = content.ReadByte(ref offset); + neh.WindowsSDKVersion = content.ReadByte(ref offset); return neh; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/OptionalHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/OptionalHeader.cs index e44d382a..934922cd 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/OptionalHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/OptionalHeader.cs @@ -302,62 +302,62 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers { var ioh = new OptionalHeader(); - ioh.Magic = (OptionalHeaderType)BitConverter.ToUInt16(content, offset); offset += 2; + ioh.Magic = (OptionalHeaderType)content.ReadUInt16(ref offset); ioh.MajorLinkerVersion = content[offset]; offset++; ioh.MinorLinkerVersion = content[offset]; offset++; - ioh.SizeOfCode = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfInitializedData = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfUninitializedData = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.AddressOfEntryPoint = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.BaseOfCode = BitConverter.ToUInt32(content, offset); offset += 4; + ioh.SizeOfCode = content.ReadUInt32(ref offset); + ioh.SizeOfInitializedData = content.ReadUInt32(ref offset); + ioh.SizeOfUninitializedData = content.ReadUInt32(ref offset); + ioh.AddressOfEntryPoint = content.ReadUInt32(ref offset); + ioh.BaseOfCode = content.ReadUInt32(ref offset); // Only standard PE32 has this value if (ioh.Magic == OptionalHeaderType.PE32) - ioh.BaseOfData = BitConverter.ToUInt32(content, offset); offset += 4; + ioh.BaseOfData = content.ReadUInt32(ref offset); // PE32+ has an 8-bit value here if (ioh.Magic == OptionalHeaderType.PE32Plus) { - ioh.ImageBasePE32Plus = BitConverter.ToUInt64(content, offset); offset += 8; + ioh.ImageBasePE32Plus = content.ReadUInt64(ref offset); } else { - ioh.ImageBasePE32 = BitConverter.ToUInt32(content, offset); offset += 4; + ioh.ImageBasePE32 = content.ReadUInt32(ref offset); } - ioh.SectionAlignment = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.FileAlignment = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.MajorOperatingSystemVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.MinorOperatingSystemVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.MajorImageVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.MinorImageVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.MajorSubsystemVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.MinorSubsystemVersion = BitConverter.ToUInt16(content, offset); offset += 2; - ioh.Reserved1 = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfImage = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfHeaders = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.CheckSum = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.Subsystem = (WindowsSubsystem)BitConverter.ToUInt16(content, offset); offset += 2; - ioh.DllCharacteristics = (DllCharacteristics)BitConverter.ToUInt16(content, offset); offset += 2; + ioh.SectionAlignment = content.ReadUInt32(ref offset); + ioh.FileAlignment = content.ReadUInt32(ref offset); + ioh.MajorOperatingSystemVersion = content.ReadUInt16(ref offset); + ioh.MinorOperatingSystemVersion = content.ReadUInt16(ref offset); + ioh.MajorImageVersion = content.ReadUInt16(ref offset); + ioh.MinorImageVersion = content.ReadUInt16(ref offset); + ioh.MajorSubsystemVersion = content.ReadUInt16(ref offset); + ioh.MinorSubsystemVersion = content.ReadUInt16(ref offset); + ioh.Reserved1 = content.ReadUInt32(ref offset); + ioh.SizeOfImage = content.ReadUInt32(ref offset); + ioh.SizeOfHeaders = content.ReadUInt32(ref offset); + ioh.CheckSum = content.ReadUInt32(ref offset); + ioh.Subsystem = (WindowsSubsystem)content.ReadUInt16(ref offset); + ioh.DllCharacteristics = (DllCharacteristics)content.ReadUInt16(ref offset); // PE32+ uses 8-byte values if (ioh.Magic == OptionalHeaderType.PE32Plus) { - ioh.SizeOfStackReservePE32Plus = BitConverter.ToUInt64(content, offset); offset += 8; - ioh.SizeOfStackCommitPE32Plus = BitConverter.ToUInt64(content, offset); offset += 8; - ioh.SizeOfHeapReservePE32Plus = BitConverter.ToUInt64(content, offset); offset += 8; - ioh.SizeOfHeapCommitPE32Plus = BitConverter.ToUInt64(content, offset); offset += 8; + ioh.SizeOfStackReservePE32Plus = content.ReadUInt64(ref offset); + ioh.SizeOfStackCommitPE32Plus = content.ReadUInt64(ref offset); + ioh.SizeOfHeapReservePE32Plus = content.ReadUInt64(ref offset); + ioh.SizeOfHeapCommitPE32Plus = content.ReadUInt64(ref offset); } else { - ioh.SizeOfStackReservePE32 = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfStackCommitPE32 = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfHeapReservePE32 = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.SizeOfHeapCommitPE32 = BitConverter.ToUInt32(content, offset); offset += 4; + ioh.SizeOfStackReservePE32 = content.ReadUInt32(ref offset); + ioh.SizeOfStackCommitPE32 = content.ReadUInt32(ref offset); + ioh.SizeOfHeapReservePE32 = content.ReadUInt32(ref offset); + ioh.SizeOfHeapCommitPE32 = content.ReadUInt32(ref offset); } - ioh.LoaderFlags = BitConverter.ToUInt32(content, offset); offset += 4; - ioh.NumberOfRvaAndSizes = BitConverter.ToUInt32(content, offset); offset += 4; + ioh.LoaderFlags = content.ReadUInt32(ref offset); + ioh.NumberOfRvaAndSizes = content.ReadUInt32(ref offset); ioh.DataDirectories = new DataDirectoryHeader[Constants.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; for (int i = 0; i < Constants.IMAGE_NUMBEROF_DIRECTORY_ENTRIES; i++) { diff --git a/BurnOutSharp/ExecutableType/Microsoft/Headers/SectionHeader.cs b/BurnOutSharp/ExecutableType/Microsoft/Headers/SectionHeader.cs index 247f8a39..d220953a 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Headers/SectionHeader.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Headers/SectionHeader.cs @@ -113,15 +113,15 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Headers ish.Name = new byte[Constants.IMAGE_SIZEOF_SHORT_NAME]; Array.Copy(content, offset, ish.Name, 0, Constants.IMAGE_SIZEOF_SHORT_NAME); offset += Constants.IMAGE_SIZEOF_SHORT_NAME; - ish.VirtualSize = BitConverter.ToUInt32(content, offset); offset += 4; - ish.VirtualAddress = BitConverter.ToUInt32(content, offset); offset += 4; - ish.SizeOfRawData = BitConverter.ToUInt32(content, offset); offset += 4; - ish.PointerToRawData = BitConverter.ToUInt32(content, offset); offset += 4; - ish.PointerToRelocations = BitConverter.ToUInt32(content, offset); offset += 4; - ish.PointerToLinenumbers = BitConverter.ToUInt32(content, offset); offset += 4; - ish.NumberOfRelocations = BitConverter.ToUInt16(content, offset); offset += 2; - ish.NumberOfLinenumbers = BitConverter.ToUInt16(content, offset); offset += 2; - ish.Characteristics = (SectionCharacteristics)BitConverter.ToUInt32(content, offset); offset += 4; + ish.VirtualSize = content.ReadUInt32(ref offset); + ish.VirtualAddress = content.ReadUInt32(ref offset); + ish.SizeOfRawData = content.ReadUInt32(ref offset); + ish.PointerToRawData = content.ReadUInt32(ref offset); + ish.PointerToRelocations = content.ReadUInt32(ref offset); + ish.PointerToLinenumbers = content.ReadUInt32(ref offset); + ish.NumberOfRelocations = content.ReadUInt16(ref offset); + ish.NumberOfLinenumbers = content.ReadUInt16(ref offset); + ish.Characteristics = (SectionCharacteristics)content.ReadUInt32(ref offset); return ish; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Resources/FixedFileInfo.cs b/BurnOutSharp/ExecutableType/Microsoft/Resources/FixedFileInfo.cs index e5adf024..20d199fd 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Resources/FixedFileInfo.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Resources/FixedFileInfo.cs @@ -120,28 +120,27 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Resources ushort temp; bool padded = false; - while ((temp = BitConverter.ToUInt16(content, offset)) == 0x0000) + while ((temp = content.ReadUInt16(ref offset)) == 0x0000) { - offset += 2; padded = true; } if (padded) offset -= 2; - ffi.Signature = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.StrucVersion = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileVersionMS = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileVersionLS = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.ProductVersionMS = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.ProductVersionLS = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileFlagsMask = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileFlags = (FileInfoFileFlags)BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileOS = (FileInfoOS)BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileType = (FileInfoFileType)BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileSubtype = (FileInfoFileSubtype)BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileDateMS = BitConverter.ToUInt32(content, offset); offset += 4; - ffi.FileDateLS = BitConverter.ToUInt32(content, offset); offset += 4; + ffi.Signature = content.ReadUInt32(ref offset); + ffi.StrucVersion = content.ReadUInt32(ref offset); + ffi.FileVersionMS = content.ReadUInt32(ref offset); + ffi.FileVersionLS = content.ReadUInt32(ref offset); + ffi.ProductVersionMS = content.ReadUInt32(ref offset); + ffi.ProductVersionLS = content.ReadUInt32(ref offset); + ffi.FileFlagsMask = content.ReadUInt32(ref offset); + ffi.FileFlags = (FileInfoFileFlags)content.ReadUInt32(ref offset); + ffi.FileOS = (FileInfoOS)content.ReadUInt32(ref offset); + ffi.FileType = (FileInfoFileType)content.ReadUInt32(ref offset); + ffi.FileSubtype = (FileInfoFileSubtype)content.ReadUInt32(ref offset); + ffi.FileDateMS = content.ReadUInt32(ref offset); + ffi.FileDateLS = content.ReadUInt32(ref offset); return ffi; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Resources/LanguageCodePage.cs b/BurnOutSharp/ExecutableType/Microsoft/Resources/LanguageCodePage.cs index a7c160bf..85385793 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Resources/LanguageCodePage.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Resources/LanguageCodePage.cs @@ -37,8 +37,8 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Resources { LanguageCodePage lcp = new LanguageCodePage(); - lcp.MicrosoftLanguageIdentifier = BitConverter.ToUInt16(content, offset); offset += 2; - lcp.IBMCodePageNumber = BitConverter.ToUInt16(content, offset); offset += 2; + lcp.MicrosoftLanguageIdentifier = content.ReadUInt16(ref offset); + lcp.IBMCodePageNumber = content.ReadUInt16(ref offset); return lcp; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Resources/Resource.cs b/BurnOutSharp/ExecutableType/Microsoft/Resources/Resource.cs index fbffadf4..1c7b0ee2 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Resources/Resource.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Resources/Resource.cs @@ -49,14 +49,11 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Resources { Resource r = new Resource(); - while ((r.Length = BitConverter.ToUInt16(content, offset)) == 0x0000) - { - offset += 2; - } + while ((r.Length = content.ReadUInt16(ref offset)) == 0x0000); offset += 2; - r.ValueLength = BitConverter.ToUInt16(content, offset); offset += 2; - r.Type = BitConverter.ToUInt16(content, offset); offset += 2; + r.ValueLength = content.ReadUInt16(ref offset); + r.Type = content.ReadUInt16(ref offset); List keyChars = new List(); while (BitConverter.ToUInt16(content, offset) != 0x0000) diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportDirectoryTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportDirectoryTable.cs index e9a04805..ce721d33 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportDirectoryTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportDirectoryTable.cs @@ -94,17 +94,17 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables { var edt = new ExportDirectoryTable(); - edt.ExportFlags = BitConverter.ToUInt32(content, offset); offset += 4; - edt.TimeDateStamp = BitConverter.ToUInt32(content, offset); offset += 4; - edt.MajorVersion = BitConverter.ToUInt16(content, offset); offset += 2; - edt.MinorVersion = BitConverter.ToUInt16(content, offset); offset += 2; - edt.NameRVA = BitConverter.ToUInt32(content, offset); offset += 4; - edt.OrdinalBase = BitConverter.ToUInt32(content, offset); offset += 4; - edt.AddressTableEntries = BitConverter.ToUInt32(content, offset); offset += 4; - edt.NumberOfNamePointers = BitConverter.ToUInt32(content, offset); offset += 4; - edt.ExportAddressTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; - edt.NamePointerRVA = BitConverter.ToUInt32(content, offset); offset += 4; - edt.OrdinalTableRVA = BitConverter.ToUInt32(content, offset); offset += 4; + edt.ExportFlags = content.ReadUInt32(ref offset); + edt.TimeDateStamp = content.ReadUInt32(ref offset); + edt.MajorVersion = content.ReadUInt16(ref offset); + edt.MinorVersion = content.ReadUInt16(ref offset); + edt.NameRVA = content.ReadUInt32(ref offset); + edt.OrdinalBase = content.ReadUInt32(ref offset); + edt.AddressTableEntries = content.ReadUInt32(ref offset); + edt.NumberOfNamePointers = content.ReadUInt32(ref offset); + edt.ExportAddressTableRVA = content.ReadUInt32(ref offset); + edt.NamePointerRVA = content.ReadUInt32(ref offset); + edt.OrdinalTableRVA = content.ReadUInt32(ref offset); return edt; } diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportNamePointerTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportNamePointerTable.cs index fb02bf5a..cda299db 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportNamePointerTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportNamePointerTable.cs @@ -35,7 +35,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables enpt.Entries = new uint[count]; for (int i = 0; i < count; i++) { - enpt.Entries[i] = BitConverter.ToUInt32(content, offset); offset += 4; + enpt.Entries[i] = content.ReadUInt32(ref offset); } return enpt; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportOrdinalTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportOrdinalTable.cs index e8789e29..da43707b 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportOrdinalTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/ExportOrdinalTable.cs @@ -35,7 +35,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables edt.Entries = new ushort[count]; for (int i = 0; i < count; i++) { - edt.Entries[i] = BitConverter.ToUInt16(content, offset); offset += 2; + edt.Entries[i] = content.ReadUInt16(ref offset); } return edt; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/ImportLookupTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/ImportLookupTable.cs index 00acff29..5b61f95f 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/ImportLookupTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/ImportLookupTable.cs @@ -68,7 +68,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables List tempEntries = new List(); while (true) { - ulong bitfield = BitConverter.ToUInt64(content, offset); offset += 8; + ulong bitfield = content.ReadUInt64(ref offset); tempEntries.Add(bitfield); if (bitfield == 0) break; @@ -82,7 +82,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables List tempEntries = new List(); while (true) { - uint bitfield = BitConverter.ToUInt32(content, offset); offset += 4; + uint bitfield = content.ReadUInt32(ref offset); tempEntries.Add(bitfield); if (bitfield == 0) break; diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/NEResourceTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/NEResourceTable.cs index 7a9cdaf3..ad265565 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/NEResourceTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/NEResourceTable.cs @@ -71,7 +71,7 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables { var nrt = new NEResourceTable(); - nrt.AlignmentShiftCount = BitConverter.ToUInt16(content, offset); offset += 2; + nrt.AlignmentShiftCount = content.ReadUInt16(ref offset); var typeInformationBlocks = new List(); while (true) { diff --git a/BurnOutSharp/ExecutableType/Microsoft/Tables/ResourceDirectoryTable.cs b/BurnOutSharp/ExecutableType/Microsoft/Tables/ResourceDirectoryTable.cs index addfa635..e1e4689c 100644 --- a/BurnOutSharp/ExecutableType/Microsoft/Tables/ResourceDirectoryTable.cs +++ b/BurnOutSharp/ExecutableType/Microsoft/Tables/ResourceDirectoryTable.cs @@ -93,12 +93,12 @@ namespace BurnOutSharp.ExecutableType.Microsoft.Tables { var rdt = new ResourceDirectoryTable(); - rdt.Characteristics = BitConverter.ToUInt32(content, offset); offset += 4; - rdt.TimeDateStamp = BitConverter.ToUInt32(content, offset); offset += 4; - rdt.MajorVersion = BitConverter.ToUInt16(content, offset); offset += 2; - rdt.MinorVersion = BitConverter.ToUInt16(content, offset); offset += 2; - rdt.NumberOfNamedEntries = BitConverter.ToUInt16(content, offset); offset += 2; - rdt.NumberOfIdEntries = BitConverter.ToUInt16(content, offset); offset += 2; + rdt.Characteristics = content.ReadUInt32(ref offset); + rdt.TimeDateStamp = content.ReadUInt32(ref offset); + rdt.MajorVersion = content.ReadUInt16(ref offset); + rdt.MinorVersion = content.ReadUInt16(ref offset); + rdt.NumberOfNamedEntries = content.ReadUInt16(ref offset); + rdt.NumberOfIdEntries = content.ReadUInt16(ref offset); rdt.NamedEntries = new ResourceDirectoryTableEntry[rdt.NumberOfNamedEntries]; for (int i = 0; i < rdt.NumberOfNamedEntries; i++) diff --git a/BurnOutSharp/ProtectionType/SafeCast.cs b/BurnOutSharp/ProtectionType/SafeCast.cs index bfb3be80..505c74ae 100644 --- a/BurnOutSharp/ProtectionType/SafeCast.cs +++ b/BurnOutSharp/ProtectionType/SafeCast.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using BurnOutSharp.ExecutableType.Microsoft; using BurnOutSharp.Matching; +using BurnOutSharp.Tools; namespace BurnOutSharp.ProtectionType { @@ -74,21 +75,17 @@ namespace BurnOutSharp.ProtectionType public static string GetVersion(string file, byte[] fileContent, List positions) { int index = positions[0] + 20; // Begin reading after "BoG_ *90.0&!! Yy>" for old SafeDisc - int version = BitConverter.ToInt32(fileContent, index); - index += 4; - int subVersion = BitConverter.ToInt32(fileContent, index); - index += 4; - int subsubVersion = BitConverter.ToInt32(fileContent, index); + int version = fileContent.ReadInt32(ref index); + int subVersion = fileContent.ReadInt32(ref index); + int subsubVersion = fileContent.ReadInt32(ref index); if (version != 0) return $"{version}.{subVersion:00}.{subsubVersion:000}"; index = positions[0] + 18 + 14; // Begin reading after "BoG_ *90.0&!! Yy>" for newer SafeDisc - version = BitConverter.ToInt32(fileContent, index); - index += 4; - subVersion = BitConverter.ToInt32(fileContent, index); - index += 4; - subsubVersion = BitConverter.ToInt32(fileContent, index); + version = fileContent.ReadInt32(ref index); + subVersion = fileContent.ReadInt32(ref index); + subsubVersion = fileContent.ReadInt32(ref index); if (version == 0) return string.Empty; diff --git a/BurnOutSharp/ProtectionType/SafeDisc.cs b/BurnOutSharp/ProtectionType/SafeDisc.cs index 2ae3f5c4..ecedcf74 100644 --- a/BurnOutSharp/ProtectionType/SafeDisc.cs +++ b/BurnOutSharp/ProtectionType/SafeDisc.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using BurnOutSharp.ExecutableType.Microsoft; using BurnOutSharp.Matching; +using BurnOutSharp.Tools; namespace BurnOutSharp.ProtectionType { @@ -163,21 +164,17 @@ namespace BurnOutSharp.ProtectionType public static string GetVersion(string file, byte[] fileContent, List positions) { int index = positions[0] + 20; // Begin reading after "BoG_ *90.0&!! Yy>" for old SafeDisc - int version = BitConverter.ToInt32(fileContent, index); - index += 4; - int subVersion = BitConverter.ToInt32(fileContent, index); - index += 4; - int subsubVersion = BitConverter.ToInt32(fileContent, index); + int version = fileContent.ReadInt32(ref index); + int subVersion = fileContent.ReadInt32(ref index); + int subsubVersion = fileContent.ReadInt32(ref index); if (version != 0) return $"{version}.{subVersion:00}.{subsubVersion:000}"; index = positions[0] + 18 + 14; // Begin reading after "BoG_ *90.0&!! Yy>" for newer SafeDisc - version = BitConverter.ToInt32(fileContent, index); - index += 4; - subVersion = BitConverter.ToInt32(fileContent, index); - index += 4; - subsubVersion = BitConverter.ToInt32(fileContent, index); + version = fileContent.ReadInt32(ref index); + subVersion = fileContent.ReadInt32(ref index); + subsubVersion = fileContent.ReadInt32(ref index); if (version == 0) return string.Empty; diff --git a/BurnOutSharp/Tools/Extensions.cs b/BurnOutSharp/Tools/Extensions.cs index 2b0d93fc..5d9cce91 100644 --- a/BurnOutSharp/Tools/Extensions.cs +++ b/BurnOutSharp/Tools/Extensions.cs @@ -9,9 +9,129 @@ namespace BurnOutSharp.Tools { internal static class Extensions { - // TODO: Add extensions for BitConverter.ToX(); offset += x; #region Byte Arrays + /// + /// Read a byte and increment the pointer to an array + /// + public static byte ReadByte(this byte[] content, ref int offset) + { + return content[offset++]; + } + + /// + /// Read a char and increment the pointer to an array + /// + public static char ReadChar(this byte[] content, ref int offset) + { + return (char)content[offset++]; + } + + /// + /// Read a character array and increment the pointer to an array + /// + public static char[] ReadChars(this byte[] content, ref int offset, int count) => content.ReadChars(ref offset, count, Encoding.Default); + + /// + /// Read a character array and increment the pointer to an array + /// + public static char[] ReadChars(this byte[] content, ref int offset, int count, Encoding encoding) + { + // TODO: Fix the code below to make it work with byte arrays and not streams + return null; + + // byte[] buffer = new byte[count]; + // stream.Read(buffer, 0, count); + // return encoding.GetString(buffer).ToCharArray(); + } + + /// + /// Read a short and increment the pointer to an array + /// + public static short ReadInt16(this byte[] content, ref int offset) + { + short value = BitConverter.ToInt16(content, offset); + offset += 2; + return value; + } + + /// + /// Read a ushort and increment the pointer to an array + /// + public static ushort ReadUInt16(this byte[] content, ref int offset) + { + ushort value = BitConverter.ToUInt16(content, offset); + offset += 2; + return value; + } + + /// + /// Read a int and increment the pointer to an array + /// + public static int ReadInt32(this byte[] content, ref int offset) + { + int value = BitConverter.ToInt32(content, offset); + offset += 4; + return value; + } + + /// + /// Read a uint and increment the pointer to an array + /// + public static uint ReadUInt32(this byte[] content, ref int offset) + { + uint value = BitConverter.ToUInt32(content, offset); + offset += 4; + return value; + } + + /// + /// Read a long and increment the pointer to an array + /// + public static long ReadInt64(this byte[] content, ref int offset) + { + long value = BitConverter.ToInt64(content, offset); + offset += 8; + return value; + } + + /// + /// Read a ulong and increment the pointer to an array + /// + public static ulong ReadUInt64(this byte[] content, ref int offset) + { + ulong value = BitConverter.ToUInt64(content, offset); + offset += 8; + return value; + } + + /// + /// Read a null-terminated string from the stream + /// + public static string ReadString(this byte[] content, ref int offset) => content.ReadString(ref offset, Encoding.Default); + + /// + /// Read a null-terminated string from the stream + /// + public static string ReadString(this byte[] content, ref int offset, Encoding encoding) + { + // TODO: Fix the code below to make it work with byte arrays and not streams + return null; + + // byte[] nullTerminator = encoding.GetBytes(new char[] { '\0' }); + // int charWidth = nullTerminator.Length; + + // List tempBuffer = new List(); + + // byte[] buffer = new byte[charWidth]; + // while (stream.Read(buffer, 0, charWidth) != 0 && buffer.SequenceEqual(nullTerminator)) + // { + // tempBuffer.AddRange(buffer); + // } + + // return encoding.GetString(tempBuffer.ToArray()); + } + /// /// Find all positions of one array in another, if possible, if possible ///