Compare commits

..

24 Commits
1.6.2 ... 1.6.7

Author SHA1 Message Date
Matt Nadareski
b7978cafa5 Bump version 2024-06-13 11:12:40 -04:00
Matt Nadareski
17f376c76f Remove all instances of this. 2024-06-05 22:49:27 -04:00
Matt Nadareski
2774fdf158 Clean up enumerables and namespace use 2024-06-05 22:48:42 -04:00
Matt Nadareski
11081efcb0 Make PE header reading even saferer 2024-06-05 22:22:22 -04:00
TheRogueArchivist
1b412c3027 Add header length safeguards to PortableExecutable wrapper (#9) 2024-06-05 22:19:35 -04:00
Matt Nadareski
73ec66e627 Fix ISv3 deserialization 2024-06-03 11:55:12 -04:00
Matt Nadareski
4ae4cd80b1 Bump version 2024-05-30 21:07:04 -04:00
Matt Nadareski
6eb27c66fc Merge pull request #8 from TheRogueArchivist/streamdatalock
Add lock for reading data from stream
2024-05-30 12:30:33 -04:00
TheRogueArchivist
f96fd17fd3 Add lock for reading data from stream 2024-05-27 15:36:04 -06:00
Matt Nadareski
c255a2494d Fix IS-CAB file group name parsing 2024-05-18 21:27:09 -04:00
Matt Nadareski
86a9846300 Bump version 2024-05-15 15:10:58 -04:00
Matt Nadareski
db877d253c Update Models, fix build 2024-05-15 14:59:55 -04:00
Matt Nadareski
0acf1e3b08 Handle bounds-defying reads 2024-05-15 13:38:44 -04:00
Matt Nadareski
362ed3a9b6 Protect against odd end-of-stream issues 2024-05-15 13:08:51 -04:00
Matt Nadareski
758878a229 Bump version 2024-05-15 12:02:21 -04:00
Matt Nadareski
ffb6dfc333 Update packages 2024-05-13 16:29:53 -04:00
Matt Nadareski
66da74e00a Fix resource table issues with NE 2024-05-12 11:46:05 -04:00
Matt Nadareski
d41a0045cb Fix input paths for test program 2024-05-09 21:54:30 -04:00
Matt Nadareski
b65629ba0e Combine magic and extension checks; helps with complex situations 2024-05-09 21:34:58 -04:00
Matt Nadareski
9518e6d1a0 Unicode (UTF-16) not UTF-8 2024-05-08 12:09:11 -04:00
Matt Nadareski
4f374ee885 Only read resources that are valid 2024-05-08 12:02:48 -04:00
Matt Nadareski
afa239056e Handle future model fix 2024-05-07 08:55:54 -04:00
Matt Nadareski
886825af11 Bump version 2024-05-07 05:17:06 -04:00
Matt Nadareski
198de925aa Update IO 2024-05-07 05:13:30 -04:00
24 changed files with 614 additions and 744 deletions

View File

@@ -27,7 +27,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="SabreTools.Models" Version="1.4.5" />
<PackageReference Include="SabreTools.Models" Version="1.4.8" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.IO.Extensions;
using SabreTools.Models.InstallShieldArchiveV3;
@@ -114,7 +115,17 @@ namespace SabreTools.Serialization.Deserializers
/// <returns>Filled directory on success, null on error</returns>
public static Models.InstallShieldArchiveV3.Directory? ParseDirectory(Stream data)
{
return data.ReadType<Models.InstallShieldArchiveV3.Directory>();
var directory = new Models.InstallShieldArchiveV3.Directory();
directory.FileCount = data.ReadUInt16();
directory.ChunkSize = data.ReadUInt16();
// TODO: Is there any equivilent automatic type for UInt16-prefixed ANSI?
ushort nameLength = data.ReadUInt16();
byte[] nameBytes = data.ReadBytes(nameLength);
directory.Name = Encoding.ASCII.GetString(nameBytes);
return directory;
}
/// <summary>

View File

@@ -489,7 +489,7 @@ namespace SabreTools.Serialization.Deserializers
if (majorVersion >= 17)
fileGroup.Name = data.ReadNullTerminatedUnicodeString();
else
fileGroup.Name = data.ReadNullTerminatedUnicodeString();
fileGroup.Name = data.ReadNullTerminatedAnsiString();
}
// Seek back to the correct offset

View File

@@ -476,7 +476,7 @@ namespace SabreTools.Serialization.Deserializers
var entry = new ResidentNamesTableEntry();
entry.Length = data.ReadByteValue();
if (entry.Length > 0)
if (entry.Length > 0 && data.Position + entry.Length <= data.Length)
{
byte[]? name = data.ReadBytes(entry.Length);
if (name != null)
@@ -810,7 +810,7 @@ namespace SabreTools.Serialization.Deserializers
var entry = new ImportModuleNameTableEntry();
entry.Length = data.ReadByteValue();
if (entry.Length > 0)
if (entry.Length > 0 && data.Position + entry.Length <= data.Length)
{
byte[]? name = data.ReadBytes(entry.Length);
if (name != null)
@@ -831,7 +831,7 @@ namespace SabreTools.Serialization.Deserializers
var entry = new ImportModuleProcedureNameTableEntry();
entry.Length = data.ReadByteValue();
if (entry.Length > 0)
if (entry.Length > 0 && data.Position + entry.Length <= data.Length)
{
byte[]? name = data.ReadBytes(entry.Length);
if (name != null)
@@ -862,7 +862,7 @@ namespace SabreTools.Serialization.Deserializers
var entry = new NonResidentNamesTableEntry();
entry.Length = data.ReadByteValue();
if (entry.Length > 0)
if (entry.Length > 0 && data.Position + entry.Length <= data.Length)
{
byte[]? name = data.ReadBytes(entry.Length);
if (name != null)

View File

@@ -76,7 +76,7 @@ namespace SabreTools.Serialization.Deserializers
// If the offset for the segment table doesn't exist
tableAddress = initialOffset
+ (int)stub.Header.NewExeHeaderAddr
+ executableHeader.SegmentTableOffset;
+ executableHeader.ResourceTableOffset;
if (tableAddress >= data.Length)
return executable;
@@ -262,7 +262,7 @@ namespace SabreTools.Serialization.Deserializers
/// <param name="data">Stream to parse</param>
/// <param name="count">Number of resource table entries to read</param>
/// <returns>Filled resource table on success, null on error</returns>
public static ResourceTable? ParseResourceTable(Stream data, int count)
public static ResourceTable? ParseResourceTable(Stream data, ushort count)
{
long initialOffset = data.Position;
@@ -270,13 +270,23 @@ namespace SabreTools.Serialization.Deserializers
var resourceTable = new ResourceTable();
resourceTable.AlignmentShiftCount = data.ReadUInt16();
resourceTable.ResourceTypes = new ResourceTypeInformationEntry[count];
for (int i = 0; i < resourceTable.ResourceTypes.Length; i++)
var resourceTypes = new List<ResourceTypeInformationEntry>();
for (int i = 0; i < count; i++)
{
var entry = new ResourceTypeInformationEntry();
entry.TypeID = data.ReadUInt16();
entry.ResourceCount = data.ReadUInt16();
entry.Reserved = data.ReadUInt32();
// A zero type ID marks the end of the resource type information blocks.
if (entry.TypeID == 0)
{
resourceTypes.Add(entry);
break;
}
entry.Resources = new ResourceTypeResourceEntry[entry.ResourceCount];
for (int j = 0; j < entry.ResourceCount; j++)
{
@@ -287,20 +297,23 @@ namespace SabreTools.Serialization.Deserializers
entry.Resources[j] = resource;
}
resourceTable.ResourceTypes[i] = entry;
resourceTypes.Add(entry);
}
resourceTable.ResourceTypes = [.. resourceTypes];
// Get the full list of unique string offsets
var stringOffsets = resourceTable.ResourceTypes
.Where(rt => rt != null)
.Where(rt => rt!.IsIntegerType() == false)
.Where(rt => !rt!.IsIntegerType() && rt!.TypeID != 0)
.Select(rt => rt!.TypeID)
.Union(resourceTable.ResourceTypes
.Where(rt => rt != null)
.Where(rt => rt != null && rt!.TypeID != 0)
.SelectMany(rt => rt!.Resources ?? [])
.Where(r => r!.IsIntegerType() == false)
.Where(r => !r!.IsIntegerType())
.Select(r => r!.ResourceID))
.Distinct()
.Where(o => o != 0)
.OrderBy(o => o)
.ToList();
@@ -359,7 +372,7 @@ namespace SabreTools.Serialization.Deserializers
// TODO: Use marshalling here instead of building
var residentNameTable = new List<ResidentNameTableEntry>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
var entry = ParseResidentNameTableEntry(data);
if (entry == null)
@@ -432,7 +445,7 @@ namespace SabreTools.Serialization.Deserializers
// TODO: Use marshalling here instead of building
var importedNameTable = new Dictionary<ushort, ImportedNameTableEntry>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
ushort currentOffset = (ushort)data.Position;
var entry = ParseImportedNameTableEntry(data);
@@ -472,7 +485,7 @@ namespace SabreTools.Serialization.Deserializers
// TODO: Use marshalling here instead of building
var entryTable = new List<EntryTableBundle>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
var entry = new EntryTableBundle();
entry.EntryCount = data.ReadByteValue();
@@ -511,7 +524,7 @@ namespace SabreTools.Serialization.Deserializers
// TODO: Use marshalling here instead of building
var residentNameTable = new List<NonResidentNameTableEntry>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
var entry = ParseNonResidentNameTableEntry(data);
if (entry == null)

View File

@@ -368,7 +368,7 @@ namespace SabreTools.Serialization.Deserializers
record.CentralDirectorySize = data.ReadUInt32();
record.CentralDirectoryOffset = data.ReadUInt32();
record.FileCommentLength = data.ReadUInt16();
if (record.FileCommentLength > 0)
if (record.FileCommentLength > 0 && data.Position + record.FileCommentLength <= data.Length)
{
byte[] commentBytes = data.ReadBytes(record.FileCommentLength);
if (commentBytes.Length != record.FileCommentLength)
@@ -501,7 +501,7 @@ namespace SabreTools.Serialization.Deserializers
header.ExternalFileAttributes = data.ReadUInt32();
header.RelativeOffsetOfLocalHeader = data.ReadUInt32();
if (header.FileNameLength > 0)
if (header.FileNameLength > 0 && data.Position + header.FileNameLength <= data.Length)
{
byte[] filenameBytes = data.ReadBytes(header.FileNameLength);
if (filenameBytes.Length != header.FileNameLength)
@@ -509,7 +509,7 @@ namespace SabreTools.Serialization.Deserializers
header.FileName = Encoding.ASCII.GetString(filenameBytes);
}
if (header.ExtraFieldLength > 0)
if (header.ExtraFieldLength > 0 && data.Position + header.ExtraFieldLength <= data.Length)
{
byte[] extraBytes = data.ReadBytes(header.ExtraFieldLength);
if (extraBytes.Length != header.ExtraFieldLength)
@@ -517,7 +517,7 @@ namespace SabreTools.Serialization.Deserializers
header.ExtraField = extraBytes;
}
if (header.FileCommentLength > 0)
if (header.FileCommentLength > 0 && data.Position + header.FileCommentLength <= data.Length)
{
byte[] commentBytes = data.ReadBytes(header.FileCommentLength);
if (commentBytes.Length != header.FileCommentLength)
@@ -593,7 +593,7 @@ namespace SabreTools.Serialization.Deserializers
return null;
record.ExtraFieldLength = data.ReadUInt32();
if (record.ExtraFieldLength > 0)
if (record.ExtraFieldLength > 0 && data.Position + record.ExtraFieldLength <= data.Length)
{
byte[] extraBytes = data.ReadBytes((int)record.ExtraFieldLength);
if (extraBytes.Length != record.ExtraFieldLength)
@@ -630,7 +630,7 @@ namespace SabreTools.Serialization.Deserializers
header.FileNameLength = data.ReadUInt16();
header.ExtraFieldLength = data.ReadUInt16();
if (header.FileNameLength > 0)
if (header.FileNameLength > 0 && data.Position + header.FileNameLength <= data.Length)
{
byte[] filenameBytes = data.ReadBytes(header.FileNameLength);
if (filenameBytes.Length != header.FileNameLength)
@@ -638,7 +638,7 @@ namespace SabreTools.Serialization.Deserializers
header.FileName = Encoding.ASCII.GetString(filenameBytes);
}
if (header.ExtraFieldLength > 0)
if (header.ExtraFieldLength > 0 && data.Position + header.ExtraFieldLength <= data.Length)
{
byte[] extraBytes = data.ReadBytes(header.ExtraFieldLength);
if (extraBytes.Length != header.ExtraFieldLength)

View File

@@ -668,7 +668,7 @@ namespace SabreTools.Serialization.Deserializers
entry.CertificateType = (WindowsCertificateType)data.ReadUInt16();
int certificateDataLength = (int)(entry.Length - 8);
if (certificateDataLength > 0)
if (certificateDataLength > 0 && data.Position + certificateDataLength <= data.Length)
entry.Certificate = data.ReadBytes(certificateDataLength);
attributeCertificateTable.Add(entry);
@@ -703,7 +703,7 @@ namespace SabreTools.Serialization.Deserializers
// TODO: Use marshalling here instead of building
var baseRelocationTable = new List<BaseRelocationBlock>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
var baseRelocationBlock = new BaseRelocationBlock();
@@ -724,12 +724,12 @@ namespace SabreTools.Serialization.Deserializers
totalSize += 2;
}
baseRelocationBlock.TypeOffsetFieldEntries = typeOffsetFieldEntries.ToArray();
baseRelocationBlock.TypeOffsetFieldEntries = [.. typeOffsetFieldEntries];
baseRelocationTable.Add(baseRelocationBlock);
}
return baseRelocationTable.ToArray();
return [.. baseRelocationTable];
}
/// <summary>
@@ -745,7 +745,7 @@ namespace SabreTools.Serialization.Deserializers
var debugDirectoryTable = new List<DebugDirectoryEntry>();
while (data.Position < endOffset)
while (data.Position < endOffset && data.Position < data.Length)
{
var debugDirectoryEntry = data.ReadType<DebugDirectoryEntry>();
if (debugDirectoryEntry == null)
@@ -1164,7 +1164,7 @@ namespace SabreTools.Serialization.Deserializers
var resourceDirectoryString = new ResourceDirectoryString();
resourceDirectoryString.Length = data.ReadUInt16();
if (resourceDirectoryString.Length > 0)
if (resourceDirectoryString.Length > 0 && data.Position + resourceDirectoryString.Length <= data.Length)
resourceDirectoryString.UnicodeString = data.ReadBytes(resourceDirectoryString.Length * 2);
entry.Name = resourceDirectoryString;
@@ -1194,7 +1194,7 @@ namespace SabreTools.Serialization.Deserializers
// Read the data from the offset
offset = resourceDataEntry.DataRVA.ConvertVirtualAddress(sections);
if (offset > 0 && resourceDataEntry.Size > 0)
if (offset > 0 && resourceDataEntry.Size > 0 && offset + (int)resourceDataEntry.Size < data.Length)
{
data.Seek(offset, SeekOrigin.Begin);
resourceDataEntry.Data = data.ReadBytes((int)resourceDataEntry.Size);
@@ -1246,7 +1246,7 @@ namespace SabreTools.Serialization.Deserializers
resourceDirectoryTable.Entries[totalEntryCount] = new ResourceDirectoryEntry
{
Name = new ResourceDirectoryString { UnicodeString = Encoding.ASCII.GetBytes("HIDDEN RESOURCE") },
Name = new ResourceDirectoryString { UnicodeString = Encoding.Unicode.GetBytes("HIDDEN RESOURCE") },
IntegerID = uint.MaxValue,
DataEntryOffset = (uint)data.Position,
DataEntry = new ResourceDataEntry

View File

@@ -67,7 +67,10 @@ namespace SabreTools.Serialization.Deserializers
#region Archive Hashes
if (header?.Version == 2 && file.ExtendedHeader != null && file.ExtendedHeader.ArchiveHashLength > 0)
if (header?.Version == 2
&& file.ExtendedHeader != null
&& file.ExtendedHeader.ArchiveHashLength > 0
&& data.Position + file.ExtendedHeader.ArchiveHashLength <= data.Length)
{
// Create the archive hashes list
var archiveHashes = new List<ArchiveHash>();
@@ -218,7 +221,9 @@ namespace SabreTools.Serialization.Deserializers
// Get the preload data pointer
long preloadDataPointer = -1; int preloadDataLength = -1;
if (directoryEntry.ArchiveIndex == HL_VPK_NO_ARCHIVE && directoryEntry.EntryLength > 0)
if (directoryEntry.ArchiveIndex == HL_VPK_NO_ARCHIVE
&& directoryEntry.EntryLength > 0
&& data.Position + directoryEntry.EntryLength <= data.Length)
{
preloadDataPointer = directoryEntry.EntryOffset;
preloadDataLength = (int)directoryEntry.EntryLength;
@@ -231,7 +236,9 @@ namespace SabreTools.Serialization.Deserializers
// If we had a valid preload data pointer
byte[]? preloadData = null;
if (preloadDataPointer >= 0 && preloadDataLength > 0)
if (preloadDataPointer >= 0
&& preloadDataLength > 0
&& data.Position + preloadDataLength <= data.Length)
{
// Cache the current offset
long initialOffset = data.Position;

View File

@@ -9,13 +9,8 @@ namespace SabreTools.Serialization
/// </summary>
/// <param name="entry">Resource type information entry to check</param>
/// <returns>True if the entry is an integer type, false if an offset, null on error</returns>
public static bool? IsIntegerType(this ResourceTypeInformationEntry entry)
public static bool IsIntegerType(this ResourceTypeInformationEntry entry)
{
// We can't do anything with an invalid entry
if (entry == null)
return null;
// If the highest order bit is set, it's an integer type
return (entry.TypeID & 0x8000) != 0;
}
@@ -24,13 +19,8 @@ namespace SabreTools.Serialization
/// </summary>
/// <param name="entry">Resource type resource entry to check</param>
/// <returns>True if the entry is an integer type, false if an offset, null on error</returns>
public static bool? IsIntegerType(this ResourceTypeResourceEntry entry)
public static bool IsIntegerType(this ResourceTypeResourceEntry entry)
{
// We can't do anything with an invalid entry
if (entry == null)
return null;
// If the highest order bit is set, it's an integer type
return (entry.ResourceID & 0x8000) != 0;
}
@@ -41,10 +31,6 @@ namespace SabreTools.Serialization
/// <returns>SegmentEntryType corresponding to the type</returns>
public static SegmentEntryType GetEntryType(this EntryTableBundle entry)
{
// We can't do anything with an invalid entry
if (entry == null)
return SegmentEntryType.Unused;
// Determine the entry type based on segment indicator
if (entry.SegmentIndicator == 0x00)
return SegmentEntryType.Unused;

View File

@@ -127,6 +127,15 @@ namespace SabreTools.Serialization
if (rsdsProgramDatabase.Signature != 0x53445352)
return null;
#if NET20 || NET35 || NET40 || NET452 || NET462
// Convert ASCII string to UTF-8
if (rsdsProgramDatabase.PathAndFileName != null)
{
byte[] bytes = Encoding.ASCII.GetBytes(rsdsProgramDatabase.PathAndFileName);
rsdsProgramDatabase.PathAndFileName = Encoding.UTF8.GetString(bytes);
}
#endif
return rsdsProgramDatabase;
}
@@ -595,7 +604,11 @@ namespace SabreTools.Serialization
#region Class resource
currentOffset = offset;
ushort classResourceIdentifier = entry.Data.ReadUInt16(ref offset);
ushort classResourceIdentifier;
if (offset >= entry.Data.Length)
classResourceIdentifier = 0x0000;
else
classResourceIdentifier = entry.Data.ReadUInt16(ref offset);
offset = currentOffset;
// 0x0000 means no elements
@@ -631,7 +644,11 @@ namespace SabreTools.Serialization
#region Title resource
currentOffset = offset;
ushort titleResourceIdentifier = entry.Data.ReadUInt16(ref offset);
ushort titleResourceIdentifier;
if (offset >= entry.Data.Length)
titleResourceIdentifier = 0x0000;
else
titleResourceIdentifier = entry.Data.ReadUInt16(ref offset);
offset = currentOffset;
// 0x0000 means no elements
@@ -891,7 +908,7 @@ namespace SabreTools.Serialization
if (menuHeaderExtended == null)
return null;
menuResource.ExtendedMenuHeader = menuHeaderExtended;
menuResource.MenuHeader = menuHeaderExtended;
#endregion
@@ -920,7 +937,7 @@ namespace SabreTools.Serialization
}
}
menuResource.ExtendedMenuItems = [.. extendedMenuItems];
menuResource.MenuItems = [.. extendedMenuItems];
#endregion
}
@@ -928,7 +945,7 @@ namespace SabreTools.Serialization
{
#region Menu header
var menuHeader = entry.Data.ReadType<MenuHeader>(ref offset);
var menuHeader = entry.Data.ReadType<NormalMenuHeader>(ref offset);
if (menuHeader == null)
return null;
@@ -1067,21 +1084,9 @@ namespace SabreTools.Serialization
// Loop through and add
while (offset < entry.Data.Length)
{
ushort stringLength = entry.Data.ReadUInt16(ref offset);
if (stringLength == 0)
string? stringValue = entry.Data.ReadPrefixedUnicodeString(ref offset);
if (stringValue != null)
{
stringTable[stringIndex++] = "[EMPTY]";
}
else
{
if (stringLength * 2 > entry.Data.Length - offset)
{
Console.WriteLine($"{stringLength * 2} requested but {entry.Data.Length - offset} remains");
stringLength = (ushort)((entry.Data.Length - offset) / 2);
}
string stringValue = Encoding.Unicode.GetString(entry.Data, offset, stringLength * 2);
offset += stringLength * 2;
stringValue = stringValue.Replace("\n", "\\n").Replace("\r", newValue: "\\r");
stringTable[stringIndex++] = stringValue;
}
@@ -1118,7 +1123,7 @@ namespace SabreTools.Serialization
versionInfo.Padding1 = entry.Data.ReadUInt16(ref offset);
// Read fixed file info
if (versionInfo.ValueLength > 0)
if (versionInfo.ValueLength > 0 && offset + versionInfo.ValueLength <= entry.Data.Length)
{
var fixedFileInfo = entry.Data.ReadType<FixedFileInfo>(ref offset);

View File

@@ -188,7 +188,7 @@ namespace SabreTools.Serialization.Printers
{
// TODO: If not integer type, print out name
var resource = entry.Resources[j];
builder.AppendLine($" Resource Entry {i}");
builder.AppendLine($" Resource Entry {j}");
if (resource == null)
{
builder.AppendLine(" [NULL]");

View File

@@ -964,7 +964,7 @@ namespace SabreTools.Serialization.Printers
var newTypes = new List<object>(types ?? []);
if (entry.Name?.UnicodeString != null)
newTypes.Add(Encoding.UTF8.GetString(entry.Name.UnicodeString));
newTypes.Add(Encoding.Unicode.GetString(entry.Name.UnicodeString));
else
newTypes.Add(entry.IntegerID);
@@ -1120,79 +1120,67 @@ namespace SabreTools.Serialization.Printers
if (menu.MenuHeader != null)
{
builder.AppendLine(menu.MenuHeader.Version, $"{padding}Version");
builder.AppendLine(menu.MenuHeader.HeaderSize, $"{padding}Header size");
if (menu.MenuHeader is NormalMenuHeader normalMenuHeader)
{
builder.AppendLine(normalMenuHeader.Version, $"{padding}Version");
builder.AppendLine(normalMenuHeader.HeaderSize, $"{padding}Header size");
}
else if (menu.MenuHeader is MenuHeaderExtended menuHeaderExtended)
{
builder.AppendLine(menuHeaderExtended.Version, $"{padding}Version");
builder.AppendLine(menuHeaderExtended.Offset, $"{padding}Offset");
builder.AppendLine(menuHeaderExtended.HelpID, $"{padding}Help ID");
}
else
{
builder.AppendLine($"{padding}Menu header found, but malformed");
}
builder.AppendLine();
builder.AppendLine($"{padding}Menu items");
builder.AppendLine($"{padding}-------------------------");
if (menu.MenuItems == null || menu.MenuItems.Length == 0)
{
builder.AppendLine($"{padding}No menu items");
return;
}
for (int i = 0; i < menu.MenuItems.Length; i++)
{
var menuItem = menu.MenuItems[i];
builder.AppendLine($"{padding}Menu item {i}");
if (menuItem == null)
{
builder.AppendLine($"{padding} [NULL]");
continue;
}
if (menuItem is NormalMenuItem normalMenuItem)
{
builder.AppendLine($"{padding} Resource info: {normalMenuItem.NormalResInfo} (0x{normalMenuItem.NormalResInfo:X})");
builder.AppendLine(normalMenuItem.NormalMenuText, $"{padding} Menu text");
}
else if (menuItem is PopupMenuItem popupMenuItem)
{
builder.AppendLine($"{padding} Item type: {popupMenuItem.PopupItemType} (0x{popupMenuItem.PopupItemType:X})");
builder.AppendLine($"{padding} State: {popupMenuItem.PopupState} (0x{popupMenuItem.PopupState:X})");
builder.AppendLine(popupMenuItem.PopupID, $"{padding} ID");
builder.AppendLine($"{padding} Resource info: {popupMenuItem.PopupResInfo} (0x{popupMenuItem.PopupResInfo:X})");
builder.AppendLine(popupMenuItem.PopupMenuText, $"{padding} Menu text");
}
}
}
else if (menu.ExtendedMenuHeader != null)
builder.AppendLine($"{padding}Menu items");
builder.AppendLine($"{padding}-------------------------");
if (menu.MenuItems == null || menu.MenuItems.Length == 0)
{
builder.AppendLine(menu.ExtendedMenuHeader.Version, $"{padding}Version");
builder.AppendLine(menu.ExtendedMenuHeader.Offset, $"{padding}Offset");
builder.AppendLine(menu.ExtendedMenuHeader.HelpID, $"{padding}Help ID");
builder.AppendLine();
builder.AppendLine($"{padding}Menu items");
builder.AppendLine($"{padding}-------------------------");
if (menu.ExtendedMenuHeader.Offset == 0
|| menu.ExtendedMenuItems == null
|| menu.ExtendedMenuItems.Length == 0)
{
builder.AppendLine($"{padding}No menu items");
return;
}
for (int i = 0; i < menu.ExtendedMenuItems.Length; i++)
{
var menuItem = menu.ExtendedMenuItems[i];
builder.AppendLine($"{padding}Menu item {i}");
if (menuItem == null)
{
builder.AppendLine($"{padding} [NULL]");
continue;
}
builder.AppendLine($"{padding} Item type: {menuItem.ItemType} (0x{menuItem.ItemType:X})");
builder.AppendLine($"{padding} State: {menuItem.State} (0x{menuItem.State:X})");
builder.AppendLine(menuItem.ID, $"{padding} ID");
builder.AppendLine($"{padding} Flags: {menuItem.Flags} (0x{menuItem.Flags:X})");
builder.AppendLine(menuItem.MenuText, $"{padding} Menu text");
}
builder.AppendLine($"{padding}No menu items");
return;
}
else
for (int i = 0; i < menu.MenuItems.Length; i++)
{
builder.AppendLine($"{padding}Menu resource found, but malformed");
var menuItem = menu.MenuItems[i];
builder.AppendLine($"{padding}Menu item {i}");
if (menuItem == null)
{
builder.AppendLine($"{padding} [NULL]");
continue;
}
if (menuItem is NormalMenuItem normalMenuItem)
{
builder.AppendLine($"{padding} Resource info: {normalMenuItem.NormalResInfo} (0x{normalMenuItem.NormalResInfo:X})");
builder.AppendLine(normalMenuItem.NormalMenuText, $"{padding} Menu text");
}
else if (menuItem is PopupMenuItem popupMenuItem)
{
builder.AppendLine($"{padding} Item type: {popupMenuItem.PopupItemType} (0x{popupMenuItem.PopupItemType:X})");
builder.AppendLine($"{padding} State: {popupMenuItem.PopupState} (0x{popupMenuItem.PopupState:X})");
builder.AppendLine(popupMenuItem.PopupID, $"{padding} ID");
builder.AppendLine($"{padding} Resource info: {popupMenuItem.PopupResInfo} (0x{popupMenuItem.PopupResInfo:X})");
builder.AppendLine(popupMenuItem.PopupMenuText, $"{padding} Menu text");
}
else if (menuItem is MenuItemExtended menuItemExtended)
{
builder.AppendLine($"{padding} Item type: {menuItemExtended.ItemType} (0x{menuItemExtended.ItemType:X})");
builder.AppendLine($"{padding} State: {menuItemExtended.State} (0x{menuItemExtended.State:X})");
builder.AppendLine(menuItemExtended.ID, $"{padding} ID");
builder.AppendLine($"{padding} Flags: {menuItemExtended.Flags} (0x{menuItemExtended.Flags:X})");
builder.AppendLine(menuItemExtended.MenuText, $"{padding} Menu text");
}
else
{
builder.AppendLine($"{padding} Menu item found, but malformed");
}
}
}

View File

@@ -8,7 +8,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.6.2</Version>
<Version>1.6.7</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -30,8 +30,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SabreTools.ASN1" Version="1.3.2" />
<PackageReference Include="SabreTools.Hashing" Version="1.2.0" />
<PackageReference Include="SabreTools.IO" Version="1.4.9" />
<PackageReference Include="SabreTools.Models" Version="1.4.5" />
<PackageReference Include="SabreTools.IO" Version="1.4.11" />
<PackageReference Include="SabreTools.Models" Version="1.4.8" />
</ItemGroup>
</Project>

View File

@@ -18,12 +18,12 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Normal sector size in bytes
/// </summary>
public long SectorSize => (long)Math.Pow(2, this.Model.Header?.SectorShift ?? 0);
public long SectorSize => (long)Math.Pow(2, Model.Header?.SectorShift ?? 0);
/// <summary>
/// Mini sector size in bytes
/// </summary>
public long MiniSectorSize => (long)Math.Pow(2, this.Model.Header?.MiniSectorShift ?? 0);
public long MiniSectorSize => (long)Math.Pow(2, Model.Header?.MiniSectorShift ?? 0);
#endregion
@@ -101,7 +101,7 @@ namespace SabreTools.Serialization.Wrappers
public List<Models.CFB.SectorNumber?>? GetFATSectorChain(Models.CFB.SectorNumber? startingSector)
{
// If we have an invalid sector
if (startingSector == null || startingSector < 0 || this.Model.FATSectorNumbers == null || (long)startingSector >= this.Model.FATSectorNumbers.Length)
if (startingSector == null || startingSector < 0 || Model.FATSectorNumbers == null || (long)startingSector >= Model.FATSectorNumbers.Length)
return null;
// Setup the returned list
@@ -114,10 +114,10 @@ namespace SabreTools.Serialization.Wrappers
break;
// Get the next sector from the lookup table
var nextSector = this.Model.FATSectorNumbers[(uint)lastSector!.Value];
var nextSector = Model.FATSectorNumbers[(uint)lastSector!.Value];
// If we have an end of chain or free sector
if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT)
if (nextSector == Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == Models.CFB.SectorNumber.FREESECT)
break;
// Add the next sector to the list and replace the last sector
@@ -188,7 +188,7 @@ namespace SabreTools.Serialization.Wrappers
public List<Models.CFB.SectorNumber?>? GetMiniFATSectorChain(Models.CFB.SectorNumber? startingSector)
{
// If we have an invalid sector
if (startingSector == null || startingSector < 0 || this.Model.MiniFATSectorNumbers == null || (long)startingSector >= this.Model.MiniFATSectorNumbers.Length)
if (startingSector == null || startingSector < 0 || Model.MiniFATSectorNumbers == null || (long)startingSector >= Model.MiniFATSectorNumbers.Length)
return null;
// Setup the returned list
@@ -201,10 +201,10 @@ namespace SabreTools.Serialization.Wrappers
break;
// Get the next sector from the lookup table
var nextSector = this.Model.MiniFATSectorNumbers[(uint)lastSector!.Value];
var nextSector = Model.MiniFATSectorNumbers[(uint)lastSector!.Value];
// If we have an end of chain or free sector
if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT)
if (nextSector == Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == Models.CFB.SectorNumber.FREESECT)
break;
// Add the next sector to the list and replace the last sector

View File

@@ -26,16 +26,16 @@ namespace SabreTools.Serialization.Wrappers
return _files;
// If we don't have a required property
if (this.Model.DirectoryEntries == null || this.Model.DirectoryMapEntries == null || this.Model.BlockEntries == null)
if (Model.DirectoryEntries == null || Model.DirectoryMapEntries == null || Model.BlockEntries == null)
return null;
// Otherwise, scan and build the files
var files = new List<FileInfo>();
for (int i = 0; i < this.Model.DirectoryEntries.Length; i++)
for (int i = 0; i < Model.DirectoryEntries.Length; i++)
{
// Get the directory entry
var directoryEntry = this.Model.DirectoryEntries[i];
var directoryMapEntry = this.Model.DirectoryMapEntries[i];
var directoryEntry = Model.DirectoryEntries[i];
var directoryMapEntry = Model.DirectoryMapEntries[i];
if (directoryEntry == null || directoryMapEntry == null)
continue;
@@ -57,26 +57,26 @@ namespace SabreTools.Serialization.Wrappers
Encrypted = directoryEntry.DirectoryFlags.HasFlag(Models.GCF.HL_GCF_FLAG.HL_GCF_FLAG_ENCRYPTED),
#endif
};
var pathParts = new List<string> { this.Model.DirectoryNames![directoryEntry.NameOffset] ?? string.Empty };
var pathParts = new List<string> { Model.DirectoryNames![directoryEntry.NameOffset] ?? string.Empty };
var blockEntries = new List<Models.GCF.BlockEntry?>();
// Traverse the parent tree
uint index = directoryEntry.ParentIndex;
while (index != 0xFFFFFFFF)
{
var parentDirectoryEntry = this.Model.DirectoryEntries[index];
var parentDirectoryEntry = Model.DirectoryEntries[index];
if (parentDirectoryEntry == null)
break;
pathParts.Add(this.Model.DirectoryNames![parentDirectoryEntry.NameOffset] ?? string.Empty);
pathParts.Add(Model.DirectoryNames![parentDirectoryEntry.NameOffset] ?? string.Empty);
index = parentDirectoryEntry.ParentIndex;
}
// Traverse the block entries
index = directoryMapEntry.FirstBlockIndex;
while (index != this.Model.DataBlockHeader?.BlockCount)
while (index != Model.DataBlockHeader?.BlockCount)
{
var nextBlock = this.Model.BlockEntries[index];
var nextBlock = Model.BlockEntries[index];
if (nextBlock == null)
break;
@@ -134,14 +134,14 @@ namespace SabreTools.Serialization.Wrappers
return _dataBlockOffsets;
// If we don't have a block count, offset, or size
if (this.Model.DataBlockHeader?.BlockCount == null || this.Model.DataBlockHeader?.FirstBlockOffset == null || this.Model.DataBlockHeader?.BlockSize == null)
if (Model.DataBlockHeader?.BlockCount == null || Model.DataBlockHeader?.FirstBlockOffset == null || Model.DataBlockHeader?.BlockSize == null)
return null;
// Otherwise, build the data block set
_dataBlockOffsets = new long[this.Model.DataBlockHeader.BlockCount];
for (int i = 0; i < this.Model.DataBlockHeader.BlockCount; i++)
_dataBlockOffsets = new long[Model.DataBlockHeader.BlockCount];
for (int i = 0; i < Model.DataBlockHeader.BlockCount; i++)
{
long dataBlockOffset = this.Model.DataBlockHeader.FirstBlockOffset + (i * this.Model.DataBlockHeader.BlockSize);
long dataBlockOffset = Model.DataBlockHeader.FirstBlockOffset + (i * Model.DataBlockHeader.BlockSize);
_dataBlockOffsets[i] = dataBlockOffset;
}

View File

@@ -53,7 +53,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
uint majorVersion = this.Model.CommonHeader?.Version ?? 0;
uint majorVersion = Model.CommonHeader?.Version ?? 0;
if (majorVersion >> 24 == 1)
{
majorVersion = (majorVersion >> 12) & 0x0F;

View File

@@ -124,11 +124,11 @@ namespace SabreTools.Serialization.Wrappers
public DateTime? GetDateTime(int fileIndex)
{
// If we have an invalid file index
if (fileIndex < 0 || this.Model.Files == null || fileIndex >= this.Model.Files.Length)
if (fileIndex < 0 || Model.Files == null || fileIndex >= Model.Files.Length)
return null;
// Get the file header
var file = this.Model.Files[fileIndex];
var file = Model.Files[fileIndex];
if (file == null)
return null;

View File

@@ -33,19 +33,26 @@ namespace SabreTools.Serialization.Wrappers
// TODO: Don't scan the known header data as well
// If the section table is missing
if (this.Model.SectionTable == null)
return null;
// If any required pieces are missing
if (Model.Stub?.Header == null)
return [];
if (Model.SectionTable == null)
return [];
// Populate the raw header padding data based on the source
uint headerStartAddress = this.Model.Stub?.Header?.NewExeHeaderAddr ?? 0;
uint firstSectionAddress = this.Model.SectionTable
uint headerStartAddress = Model.Stub.Header.NewExeHeaderAddr;
uint firstSectionAddress = Model.SectionTable
.Select(s => s?.PointerToRawData ?? 0)
.Where(s => s != 0)
.Where(s => s != 0 && s >= headerStartAddress)
.OrderBy(s => s)
.First();
.FirstOrDefault();
// Check if the header length is more than 0 before reading data
int headerLength = (int)(firstSectionAddress - headerStartAddress);
_headerPaddingData = ReadFromDataSource((int)headerStartAddress, headerLength);
if (headerLength <= 0)
_headerPaddingData = [];
else
_headerPaddingData = ReadFromDataSource((int)headerStartAddress, headerLength);
// Cache and return the header padding data, even if null
return _headerPaddingData;
@@ -68,19 +75,26 @@ namespace SabreTools.Serialization.Wrappers
// TODO: Don't scan the known header data as well
// If the section table is missing
if (this.Model.SectionTable == null)
return null;
// If any required pieces are missing
if (Model.Stub?.Header == null)
return [];
if (Model.SectionTable == null)
return [];
// Populate the raw header padding data based on the source
uint headerStartAddress = this.Model.Stub?.Header?.NewExeHeaderAddr ?? 0;
uint firstSectionAddress = this.Model.SectionTable
// Populate the header padding strings based on the source
uint headerStartAddress = Model.Stub.Header.NewExeHeaderAddr;
uint firstSectionAddress = Model.SectionTable
.Select(s => s?.PointerToRawData ?? 0)
.Where(s => s != 0)
.Where(s => s != 0 && s >= headerStartAddress)
.OrderBy(s => s)
.First();
.FirstOrDefault();
// Check if the header length is more than 0 before reading strings
int headerLength = (int)(firstSectionAddress - headerStartAddress);
_headerPaddingStrings = ReadStringsFromDataSource((int)headerStartAddress, headerLength, charLimit: 3);
if (headerLength <= 0)
_headerPaddingStrings = [];
else
_headerPaddingStrings = ReadStringsFromDataSource((int)headerStartAddress, headerLength, charLimit: 3);
// Cache and return the header padding data, even if null
return _headerPaddingStrings;
@@ -98,21 +112,21 @@ namespace SabreTools.Serialization.Wrappers
lock (_sourceDataLock)
{
// If the section table is missing
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// If the address is missing
if (this.Model.OptionalHeader?.AddressOfEntryPoint == null)
if (Model.OptionalHeader?.AddressOfEntryPoint == null)
return null;
// If we have no entry point
int entryPointAddress = (int)this.Model.OptionalHeader.AddressOfEntryPoint.ConvertVirtualAddress(this.Model.SectionTable);
int entryPointAddress = (int)Model.OptionalHeader.AddressOfEntryPoint.ConvertVirtualAddress(Model.SectionTable);
if (entryPointAddress == 0)
return null;
// If the entry point matches with the start of a section, use that
int entryPointSection = FindEntryPointSectionIndex();
if (entryPointSection >= 0 && this.Model.OptionalHeader.AddressOfEntryPoint == this.Model.SectionTable[entryPointSection]?.VirtualAddress)
if (entryPointSection >= 0 && Model.OptionalHeader.AddressOfEntryPoint == Model.SectionTable[entryPointSection]?.VirtualAddress)
return GetSectionData(entryPointSection);
// If we already have cached data, just use that immediately
@@ -148,27 +162,27 @@ namespace SabreTools.Serialization.Wrappers
return -1;
// If the section table is missing
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return -1;
// If we have certificate data, use that as the end
if (this.Model.OptionalHeader?.CertificateTable != null)
if (Model.OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)this.Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int certificateTableAddress = (int)Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
int endOfSectionData = -1;
foreach (var section in this.Model.SectionTable)
foreach (var section in Model.SectionTable)
{
// If we have an invalid section
if (section == null)
continue;
// If we have an invalid section address
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (sectionAddress == 0)
continue;
@@ -219,27 +233,27 @@ namespace SabreTools.Serialization.Wrappers
return null;
// If the section table is missing
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// If we have certificate data, use that as the end
if (this.Model.OptionalHeader?.CertificateTable != null)
if (Model.OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)this.Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int certificateTableAddress = (int)Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
int endOfSectionData = -1;
foreach (var section in this.Model.SectionTable)
foreach (var section in Model.SectionTable)
{
// If we have an invalid section
if (section == null)
continue;
// If we have an invalid section address
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (sectionAddress == 0)
continue;
@@ -297,27 +311,27 @@ namespace SabreTools.Serialization.Wrappers
return null;
// If the section table is missing
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// If we have certificate data, use that as the end
if (this.Model.OptionalHeader?.CertificateTable != null)
if (Model.OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)this.Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int certificateTableAddress = (int)Model.OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
int endOfSectionData = -1;
foreach (var section in this.Model.SectionTable)
foreach (var section in Model.SectionTable)
{
// If we have an invalid section
if (section == null)
continue;
// If we have an invalid section address
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
int sectionAddress = (int)section.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (sectionAddress == 0)
continue;
@@ -370,14 +384,14 @@ namespace SabreTools.Serialization.Wrappers
return _sectionNames;
// If there are no sections
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// Otherwise, build and return the cached array
_sectionNames = new string[this.Model.SectionTable.Length];
_sectionNames = new string[Model.SectionTable.Length];
for (int i = 0; i < _sectionNames.Length; i++)
{
var section = this.Model.SectionTable[i];
var section = Model.SectionTable[i];
if (section == null)
continue;
@@ -408,12 +422,12 @@ namespace SabreTools.Serialization.Wrappers
if (_stubExecutableData != null)
return _stubExecutableData;
if (this.Model.Stub?.Header?.NewExeHeaderAddr == null)
if (Model.Stub?.Header?.NewExeHeaderAddr == null)
return null;
// Populate the raw stub executable data based on the source
int endOfStubHeader = 0x40;
int lengthOfStubExecutableData = (int)this.Model.Stub.Header.NewExeHeaderAddr - endOfStubHeader;
int lengthOfStubExecutableData = (int)Model.Stub.Header.NewExeHeaderAddr - endOfStubHeader;
_stubExecutableData = ReadFromDataSource(endOfStubHeader, lengthOfStubExecutableData);
// Cache and return the stub executable data, even if null
@@ -436,8 +450,8 @@ namespace SabreTools.Serialization.Wrappers
return _debugData;
// If we have no resource table, just return
if (this.Model.DebugTable?.DebugDirectoryTable == null
|| this.Model.DebugTable.DebugDirectoryTable.Length == 0)
if (Model.DebugTable?.DebugDirectoryTable == null
|| Model.DebugTable.DebugDirectoryTable.Length == 0)
return null;
// Otherwise, build and return the cached dictionary
@@ -461,13 +475,13 @@ namespace SabreTools.Serialization.Wrappers
return _resourceData;
// If we have no resource table, just return
if (this.Model.OptionalHeader?.ResourceTable == null
|| this.Model.OptionalHeader.ResourceTable.VirtualAddress == 0
|| this.Model.ResourceDirectoryTable == null)
if (Model.OptionalHeader?.ResourceTable == null
|| Model.OptionalHeader.ResourceTable.VirtualAddress == 0
|| Model.ResourceDirectoryTable == null)
return null;
// Otherwise, build and return the cached dictionary
ParseResourceDirectoryTable(this.Model.ResourceDirectoryTable, types: []);
ParseResourceDirectoryTable(Model.ResourceDirectoryTable, types: []);
return _resourceData;
}
}
@@ -585,15 +599,15 @@ namespace SabreTools.Serialization.Wrappers
/// <remarks>The internal version is either the file version, product version, or assembly version, in that order</remarks>
public string? GetInternalVersion()
{
string? version = this.FileVersion;
string? version = FileVersion;
if (!string.IsNullOrEmpty(version))
return version!.Replace(", ", ".");
version = this.ProductVersion;
version = ProductVersion;
if (!string.IsNullOrEmpty(version))
return version!.Replace(", ", ".");
version = this.AssemblyVersion;
version = AssemblyVersion;
if (!string.IsNullOrEmpty(version))
return version;
@@ -864,7 +878,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the debug data cached
if (DebugData == null)
return Enumerable.Empty<object?>();
return [];
var nb10Found = DebugData.Select(r => r.Value)
.Select(r => r as SabreTools.Models.PortableExecutable.NB10ProgramDatabase)
@@ -890,7 +904,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (DebugData == null)
return Enumerable.Empty<byte[]?>();
return [];
return DebugData.Select(r => r.Value)
.Select(b => b as byte[])
@@ -935,13 +949,13 @@ namespace SabreTools.Serialization.Wrappers
private void ParseDebugTable()
{
// If there is no debug table
if (this.Model.DebugTable?.DebugDirectoryTable == null)
if (Model.DebugTable?.DebugDirectoryTable == null)
return;
// Loop through all debug table entries
for (int i = 0; i < this.Model.DebugTable.DebugDirectoryTable.Length; i++)
for (int i = 0; i < Model.DebugTable.DebugDirectoryTable.Length; i++)
{
var entry = this.Model.DebugTable.DebugDirectoryTable[i];
var entry = Model.DebugTable.DebugDirectoryTable[i];
if (entry == null)
continue;
@@ -953,7 +967,7 @@ namespace SabreTools.Serialization.Wrappers
continue;
// If we have CodeView debug data, try to parse it
if (entry.DebugType == SabreTools.Models.PortableExecutable.DebugType.IMAGE_DEBUG_TYPE_CODEVIEW)
if (entry.DebugType == Models.PortableExecutable.DebugType.IMAGE_DEBUG_TYPE_CODEVIEW)
{
// Read the signature
int offset = 0;
@@ -1004,7 +1018,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (ResourceData == null)
return Enumerable.Empty<Models.PortableExecutable.DialogBoxResource?>();
return [];
return ResourceData.Select(r => r.Value)
.Select(r => r as SabreTools.Models.PortableExecutable.DialogBoxResource)
@@ -1025,7 +1039,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (ResourceData == null)
return Enumerable.Empty<Models.PortableExecutable.DialogBoxResource?>();
return [];
return ResourceData.Select(r => r.Value)
.Select(r => r as SabreTools.Models.PortableExecutable.DialogBoxResource)
@@ -1058,7 +1072,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (ResourceData == null)
return Enumerable.Empty<Dictionary<int, string?>?>();
return [];
return ResourceData.Select(r => r.Value)
.Select(r => r as Dictionary<int, string?>)
@@ -1076,7 +1090,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (ResourceData == null)
return Enumerable.Empty<byte[]?>();
return [];
return ResourceData.Where(kvp => kvp.Key.Contains(typeName))
.Select(kvp => kvp.Value as byte[])
@@ -1092,7 +1106,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Ensure that we have the resource data cached
if (ResourceData == null)
return Enumerable.Empty<byte[]?>();
return [];
return ResourceData.Select(r => r.Value)
.Select(r => r as byte[])
@@ -1148,7 +1162,7 @@ namespace SabreTools.Serialization.Wrappers
var newTypes = new List<object>(types ?? []);
if (entry.Name?.UnicodeString != null)
newTypes.Add(Encoding.UTF8.GetString(entry.Name.UnicodeString));
newTypes.Add(Encoding.Unicode.GetString(entry.Name.UnicodeString));
else
newTypes.Add(entry.IntegerID);
@@ -1188,68 +1202,68 @@ namespace SabreTools.Serialization.Wrappers
{
switch ((Models.PortableExecutable.ResourceType)resourceType)
{
case SabreTools.Models.PortableExecutable.ResourceType.RT_CURSOR:
case Models.PortableExecutable.ResourceType.RT_CURSOR:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_BITMAP:
case Models.PortableExecutable.ResourceType.RT_BITMAP:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_ICON:
case Models.PortableExecutable.ResourceType.RT_ICON:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_MENU:
case Models.PortableExecutable.ResourceType.RT_MENU:
value = entry.AsMenu();
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_DIALOG:
case Models.PortableExecutable.ResourceType.RT_DIALOG:
value = entry.AsDialogBox();
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_STRING:
case Models.PortableExecutable.ResourceType.RT_STRING:
value = entry.AsStringTable();
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_FONTDIR:
case Models.PortableExecutable.ResourceType.RT_FONTDIR:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_FONT:
case Models.PortableExecutable.ResourceType.RT_FONT:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_ACCELERATOR:
case Models.PortableExecutable.ResourceType.RT_ACCELERATOR:
value = entry.AsAcceleratorTableResource();
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_RCDATA:
case Models.PortableExecutable.ResourceType.RT_RCDATA:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_MESSAGETABLE:
case Models.PortableExecutable.ResourceType.RT_MESSAGETABLE:
value = entry.AsMessageResourceData();
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_GROUP_CURSOR:
case Models.PortableExecutable.ResourceType.RT_GROUP_CURSOR:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_GROUP_ICON:
case Models.PortableExecutable.ResourceType.RT_GROUP_ICON:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_VERSION:
case Models.PortableExecutable.ResourceType.RT_VERSION:
_versionInfo = entry.AsVersionInfo();
value = _versionInfo;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_DLGINCLUDE:
case Models.PortableExecutable.ResourceType.RT_DLGINCLUDE:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_PLUGPLAY:
case Models.PortableExecutable.ResourceType.RT_PLUGPLAY:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_VXD:
case Models.PortableExecutable.ResourceType.RT_VXD:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_ANICURSOR:
case Models.PortableExecutable.ResourceType.RT_ANICURSOR:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_ANIICON:
case Models.PortableExecutable.ResourceType.RT_ANIICON:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_HTML:
case Models.PortableExecutable.ResourceType.RT_HTML:
value = entry.Data;
break;
case SabreTools.Models.PortableExecutable.ResourceType.RT_MANIFEST:
case Models.PortableExecutable.ResourceType.RT_MANIFEST:
_assemblyManifest = entry.AsAssemblyManifest();
value = _versionInfo;
break;
@@ -1311,19 +1325,19 @@ namespace SabreTools.Serialization.Wrappers
public int FindEntryPointSectionIndex()
{
// If the section table is missing
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return -1;
// If the address is missing
if (this.Model.OptionalHeader?.AddressOfEntryPoint == null)
if (Model.OptionalHeader?.AddressOfEntryPoint == null)
return -1;
// If we don't have an entry point
if (this.Model.OptionalHeader.AddressOfEntryPoint.ConvertVirtualAddress(this.Model.SectionTable) == 0)
if (Model.OptionalHeader.AddressOfEntryPoint.ConvertVirtualAddress(Model.SectionTable) == 0)
return -1;
// Otherwise, find the section it exists within
return this.Model.OptionalHeader.AddressOfEntryPoint.ContainingSectionIndex(this.Model.SectionTable
return Model.OptionalHeader.AddressOfEntryPoint.ContainingSectionIndex(Model.SectionTable
.Where(sh => sh != null)
.Cast<Models.PortableExecutable.SectionHeader>()
.ToArray());
@@ -1335,10 +1349,10 @@ namespace SabreTools.Serialization.Wrappers
/// <param name="name">Name of the section to check for</param>
/// <param name="exact">True to enable exact matching of names, false for starts-with</param>
/// <returns>Section data on success, null on error</returns>
public SabreTools.Models.PortableExecutable.SectionHeader? GetFirstSection(string? name, bool exact = false)
public Models.PortableExecutable.SectionHeader? GetFirstSection(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1351,7 +1365,7 @@ namespace SabreTools.Serialization.Wrappers
return null;
// Return the section
return this.Model.SectionTable[index];
return Model.SectionTable[index];
}
/// <summary>
@@ -1360,10 +1374,10 @@ namespace SabreTools.Serialization.Wrappers
/// <param name="name">Name of the section to check for</param>
/// <param name="exact">True to enable exact matching of names, false for starts-with</param>
/// <returns>Section data on success, null on error</returns>
public SabreTools.Models.PortableExecutable.SectionHeader? GetLastSection(string? name, bool exact = false)
public Models.PortableExecutable.SectionHeader? GetLastSection(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1376,7 +1390,7 @@ namespace SabreTools.Serialization.Wrappers
return null;
// Return the section
return this.Model.SectionTable[index];
return Model.SectionTable[index];
}
/// <summary>
@@ -1384,18 +1398,18 @@ namespace SabreTools.Serialization.Wrappers
/// </summary>
/// <param name="index">Index of the section to check for</param>
/// <returns>Section data on success, null on error</returns>
public SabreTools.Models.PortableExecutable.SectionHeader? GetSection(int index)
public Models.PortableExecutable.SectionHeader? GetSection(int index)
{
// If we have no sections
if (this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
if (index < 0 || index >= this.Model.SectionTable.Length)
if (index < 0 || index >= Model.SectionTable.Length)
return null;
// Return the section
return this.Model.SectionTable[index];
return Model.SectionTable[index];
}
/// <summary>
@@ -1407,7 +1421,7 @@ namespace SabreTools.Serialization.Wrappers
public byte[]? GetFirstSectionData(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1428,7 +1442,7 @@ namespace SabreTools.Serialization.Wrappers
public byte[]? GetLastSectionData(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1448,19 +1462,19 @@ namespace SabreTools.Serialization.Wrappers
public byte[]? GetSectionData(int index)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
if (index < 0 || index >= this.Model.SectionTable.Length)
if (index < 0 || index >= Model.SectionTable.Length)
return null;
// Get the section data from the table
var section = this.Model.SectionTable[index];
var section = Model.SectionTable[index];
if (section == null)
return null;
uint address = section.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
uint address = section.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (address == 0)
return null;
@@ -1493,7 +1507,7 @@ namespace SabreTools.Serialization.Wrappers
public List<string>? GetFirstSectionStrings(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1514,7 +1528,7 @@ namespace SabreTools.Serialization.Wrappers
public List<string>? GetLastSectionStrings(string? name, bool exact = false)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
@@ -1534,19 +1548,19 @@ namespace SabreTools.Serialization.Wrappers
public List<string>? GetSectionStrings(int index)
{
// If we have no sections
if (SectionNames == null || !SectionNames.Any() || this.Model.SectionTable == null || !this.Model.SectionTable.Any())
if (SectionNames == null || !SectionNames.Any() || Model.SectionTable == null || !Model.SectionTable.Any())
return null;
// If the section doesn't exist
if (index < 0 || index >= this.Model.SectionTable.Length)
if (index < 0 || index >= Model.SectionTable.Length)
return null;
// Get the section data from the table
var section = this.Model.SectionTable[index];
var section = Model.SectionTable[index];
if (section == null)
return null;
uint address = section.VirtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
uint address = section.VirtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (address == 0)
return null;
@@ -1582,7 +1596,7 @@ namespace SabreTools.Serialization.Wrappers
public byte[]? GetTableData(int index)
{
// If the table doesn't exist
if (this.Model.OptionalHeader == null || index < 0 || index > 16)
if (Model.OptionalHeader == null || index < 0 || index > 16)
return null;
// Get the virtual address and size from the entries
@@ -1590,64 +1604,64 @@ namespace SabreTools.Serialization.Wrappers
switch (index)
{
case 1:
virtualAddress = this.Model.OptionalHeader.ExportTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ExportTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ExportTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ExportTable?.Size ?? 0;
break;
case 2:
virtualAddress = this.Model.OptionalHeader.ImportTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ImportTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ImportTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ImportTable?.Size ?? 0;
break;
case 3:
virtualAddress = this.Model.OptionalHeader.ResourceTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ResourceTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ResourceTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ResourceTable?.Size ?? 0;
break;
case 4:
virtualAddress = this.Model.OptionalHeader.ExceptionTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ExceptionTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ExceptionTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ExceptionTable?.Size ?? 0;
break;
case 5:
virtualAddress = this.Model.OptionalHeader.CertificateTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.CertificateTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.CertificateTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.CertificateTable?.Size ?? 0;
break;
case 6:
virtualAddress = this.Model.OptionalHeader.BaseRelocationTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.BaseRelocationTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.BaseRelocationTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.BaseRelocationTable?.Size ?? 0;
break;
case 7:
virtualAddress = this.Model.OptionalHeader.Debug?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.Debug?.Size ?? 0;
virtualAddress = Model.OptionalHeader.Debug?.VirtualAddress ?? 0;
size = Model.OptionalHeader.Debug?.Size ?? 0;
break;
case 8: // Architecture Table
virtualAddress = 0;
size = 0;
break;
case 9:
virtualAddress = this.Model.OptionalHeader.GlobalPtr?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.GlobalPtr?.Size ?? 0;
virtualAddress = Model.OptionalHeader.GlobalPtr?.VirtualAddress ?? 0;
size = Model.OptionalHeader.GlobalPtr?.Size ?? 0;
break;
case 10:
virtualAddress = this.Model.OptionalHeader.ThreadLocalStorageTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ThreadLocalStorageTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ThreadLocalStorageTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ThreadLocalStorageTable?.Size ?? 0;
break;
case 11:
virtualAddress = this.Model.OptionalHeader.LoadConfigTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.LoadConfigTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.LoadConfigTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.LoadConfigTable?.Size ?? 0;
break;
case 12:
virtualAddress = this.Model.OptionalHeader.BoundImport?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.BoundImport?.Size ?? 0;
virtualAddress = Model.OptionalHeader.BoundImport?.VirtualAddress ?? 0;
size = Model.OptionalHeader.BoundImport?.Size ?? 0;
break;
case 13:
virtualAddress = this.Model.OptionalHeader.ImportAddressTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ImportAddressTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ImportAddressTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ImportAddressTable?.Size ?? 0;
break;
case 14:
virtualAddress = this.Model.OptionalHeader.DelayImportDescriptor?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.DelayImportDescriptor?.Size ?? 0;
virtualAddress = Model.OptionalHeader.DelayImportDescriptor?.VirtualAddress ?? 0;
size = Model.OptionalHeader.DelayImportDescriptor?.Size ?? 0;
break;
case 15:
virtualAddress = this.Model.OptionalHeader.CLRRuntimeHeader?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.CLRRuntimeHeader?.Size ?? 0;
virtualAddress = Model.OptionalHeader.CLRRuntimeHeader?.VirtualAddress ?? 0;
size = Model.OptionalHeader.CLRRuntimeHeader?.Size ?? 0;
break;
case 16: // Reserved
virtualAddress = 0;
@@ -1656,11 +1670,11 @@ namespace SabreTools.Serialization.Wrappers
}
// If there is no section table
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// Get the physical address from the virtual one
uint address = virtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
uint address = virtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (address == 0 || size == 0)
return null;
@@ -1690,7 +1704,7 @@ namespace SabreTools.Serialization.Wrappers
public List<string>? GetTableStrings(int index)
{
// If the table doesn't exist
if (this.Model.OptionalHeader == null || index < 0 || index > 16)
if (Model.OptionalHeader == null || index < 0 || index > 16)
return null;
// Get the virtual address and size from the entries
@@ -1698,64 +1712,64 @@ namespace SabreTools.Serialization.Wrappers
switch (index)
{
case 1:
virtualAddress = this.Model.OptionalHeader.ExportTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ExportTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ExportTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ExportTable?.Size ?? 0;
break;
case 2:
virtualAddress = this.Model.OptionalHeader.ImportTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ImportTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ImportTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ImportTable?.Size ?? 0;
break;
case 3:
virtualAddress = this.Model.OptionalHeader.ResourceTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ResourceTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ResourceTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ResourceTable?.Size ?? 0;
break;
case 4:
virtualAddress = this.Model.OptionalHeader.ExceptionTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ExceptionTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ExceptionTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ExceptionTable?.Size ?? 0;
break;
case 5:
virtualAddress = this.Model.OptionalHeader.CertificateTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.CertificateTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.CertificateTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.CertificateTable?.Size ?? 0;
break;
case 6:
virtualAddress = this.Model.OptionalHeader.BaseRelocationTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.BaseRelocationTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.BaseRelocationTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.BaseRelocationTable?.Size ?? 0;
break;
case 7:
virtualAddress = this.Model.OptionalHeader.Debug?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.Debug?.Size ?? 0;
virtualAddress = Model.OptionalHeader.Debug?.VirtualAddress ?? 0;
size = Model.OptionalHeader.Debug?.Size ?? 0;
break;
case 8: // Architecture Table
virtualAddress = 0;
size = 0;
break;
case 9:
virtualAddress = this.Model.OptionalHeader.GlobalPtr?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.GlobalPtr?.Size ?? 0;
virtualAddress = Model.OptionalHeader.GlobalPtr?.VirtualAddress ?? 0;
size = Model.OptionalHeader.GlobalPtr?.Size ?? 0;
break;
case 10:
virtualAddress = this.Model.OptionalHeader.ThreadLocalStorageTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ThreadLocalStorageTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ThreadLocalStorageTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ThreadLocalStorageTable?.Size ?? 0;
break;
case 11:
virtualAddress = this.Model.OptionalHeader.LoadConfigTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.LoadConfigTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.LoadConfigTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.LoadConfigTable?.Size ?? 0;
break;
case 12:
virtualAddress = this.Model.OptionalHeader.BoundImport?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.BoundImport?.Size ?? 0;
virtualAddress = Model.OptionalHeader.BoundImport?.VirtualAddress ?? 0;
size = Model.OptionalHeader.BoundImport?.Size ?? 0;
break;
case 13:
virtualAddress = this.Model.OptionalHeader.ImportAddressTable?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.ImportAddressTable?.Size ?? 0;
virtualAddress = Model.OptionalHeader.ImportAddressTable?.VirtualAddress ?? 0;
size = Model.OptionalHeader.ImportAddressTable?.Size ?? 0;
break;
case 14:
virtualAddress = this.Model.OptionalHeader.DelayImportDescriptor?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.DelayImportDescriptor?.Size ?? 0;
virtualAddress = Model.OptionalHeader.DelayImportDescriptor?.VirtualAddress ?? 0;
size = Model.OptionalHeader.DelayImportDescriptor?.Size ?? 0;
break;
case 15:
virtualAddress = this.Model.OptionalHeader.CLRRuntimeHeader?.VirtualAddress ?? 0;
size = this.Model.OptionalHeader.CLRRuntimeHeader?.Size ?? 0;
virtualAddress = Model.OptionalHeader.CLRRuntimeHeader?.VirtualAddress ?? 0;
size = Model.OptionalHeader.CLRRuntimeHeader?.Size ?? 0;
break;
case 16: // Reserved
virtualAddress = 0;
@@ -1764,11 +1778,11 @@ namespace SabreTools.Serialization.Wrappers
}
// If there is no section table
if (this.Model.SectionTable == null)
if (Model.SectionTable == null)
return null;
// Get the physical address from the virtual one
uint address = virtualAddress.ConvertVirtualAddress(this.Model.SectionTable);
uint address = virtualAddress.ConvertVirtualAddress(Model.SectionTable);
if (address == 0 || size == 0)
return null;

View File

@@ -43,9 +43,9 @@ namespace SabreTools.Serialization.Wrappers
return null;
// Get the archive count
int archiveCount = this.Model.DirectoryItems == null
int archiveCount = Model.DirectoryItems == null
? 0
: this.Model.DirectoryItems
: Model.DirectoryItems
.Select(di => di?.DirectoryEntry)
.Select(de => de?.ArchiveIndex ?? 0)
.Where(ai => ai != HL_VPK_NO_ARCHIVE)

View File

@@ -29,6 +29,11 @@ namespace SabreTools.Serialization.Wrappers
/// </summary>
protected DataSource _dataSource = DataSource.UNKNOWN;
/// <summary>
/// Lock object for reading from the source
/// </summary>
private readonly object _streamDataLock = new();
/// <summary>
/// Source byte array data
/// </summary>
@@ -86,7 +91,7 @@ namespace SabreTools.Serialization.Wrappers
if (offset < 0 || offset >= data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
this.Model = model;
Model = model;
_dataSource = DataSource.ByteArray;
_byteArrayData = data;
_byteArrayOffset = offset;
@@ -104,7 +109,7 @@ namespace SabreTools.Serialization.Wrappers
if (data.Length == 0 || !data.CanSeek || !data.CanRead)
throw new ArgumentOutOfRangeException(nameof(data));
this.Model = model;
Model = model;
_dataSource = DataSource.Stream;
_streamData = data;
}
@@ -193,11 +198,14 @@ namespace SabreTools.Serialization.Wrappers
break;
case DataSource.Stream:
long currentLocation = _streamData!.Position;
_streamData.Seek(position, SeekOrigin.Begin);
sectionData = _streamData.ReadBytes(length);
_streamData.Seek(currentLocation, SeekOrigin.Begin);
break;
lock (_streamDataLock)
{
long currentLocation = _streamData!.Position;
_streamData.Seek(position, SeekOrigin.Begin);
sectionData = _streamData.ReadBytes(length);
_streamData.Seek(currentLocation, SeekOrigin.Begin);
break;
}
}
return sectionData;

View File

@@ -116,15 +116,18 @@ namespace SabreTools.Serialization.Wrappers
}
/// <summary>
/// Get the supported file type for a magic string
/// Get the supported file type for a magic string and an extension
/// </summary>
/// <remarks>Recommend sending in 16 bytes to check</remarks>
public static WrapperType GetFileType(byte[] magic)
public static WrapperType GetFileType(byte[]? magic, string? extension)
{
// If we have an invalid magic byte array
if (magic == null || magic.Length == 0)
// If we have an invalid magic byte array and extension
if (magic == null || magic.Length == 0 || extension == null)
return WrapperType.UNKNOWN;
// Normalize the extension
extension = extension.TrimStart('.').Trim();
// TODO: For all modelled types, use the constants instead of hardcoded values here
#region AACSMediaKeyBlock
@@ -136,6 +139,15 @@ namespace SabreTools.Serialization.Wrappers
if (magic.StartsWith(new byte?[] { 0x10, 0x00, 0x00, 0x0C }))
return WrapperType.AACSMediaKeyBlock;
// Shares an extension with INF setup information so it can't be used accurately
// Blu-ray
// if (extension.Equals("inf", StringComparison.OrdinalIgnoreCase))
// return WrapperType.AACSMediaKeyBlock;
// HD-DVD
if (extension.Equals("aacs", StringComparison.OrdinalIgnoreCase))
return WrapperType.AACSMediaKeyBlock;
#endregion
#region BDPlusSVM
@@ -143,6 +155,9 @@ namespace SabreTools.Serialization.Wrappers
if (magic.StartsWith(new byte?[] { 0x42, 0x44, 0x53, 0x56, 0x4D, 0x5F, 0x43, 0x43 }))
return WrapperType.BDPlusSVM;
if (extension.Equals("svm", StringComparison.OrdinalIgnoreCase))
return WrapperType.BDPlusSVM;
#endregion
#region BFPK
@@ -154,7 +169,14 @@ namespace SabreTools.Serialization.Wrappers
#region BSP
if (magic.StartsWith(new byte?[] { 0x1e, 0x00, 0x00, 0x00 }))
// Shares a first 4 bytes with some .mc files
// Shares an extension with VBSP
if (magic.StartsWith(new byte?[] { 0x1d, 0x00, 0x00, 0x00 }) && extension.Equals("bsp", StringComparison.OrdinalIgnoreCase))
return WrapperType.BSP;
// Shares a first 4 bytes with some .mc files
// Shares an extension with VBSP
if (magic.StartsWith(new byte?[] { 0x1e, 0x00, 0x00, 0x00 }) && extension.Equals("bsp", StringComparison.OrdinalIgnoreCase))
return WrapperType.BSP;
#endregion
@@ -164,6 +186,9 @@ namespace SabreTools.Serialization.Wrappers
if (magic.StartsWith(new byte?[] { 0x42, 0x52, 0x68 }))
return WrapperType.BZip2;
if (extension.Equals("bz2", StringComparison.OrdinalIgnoreCase))
return WrapperType.BZip2;
#endregion
#region CFB
@@ -171,11 +196,32 @@ namespace SabreTools.Serialization.Wrappers
if (magic.StartsWith(new byte?[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }))
return WrapperType.CFB;
// Installer package
if (extension.Equals("msi", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Merge module
else if (extension.Equals("msm", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Patch Package
else if (extension.Equals("msp", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Transform
else if (extension.Equals("mst", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Patch Creation Properties
else if (extension.Equals("pcp", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
#endregion
#region CIA
// No magic checks for CIA
if (extension.Equals("cia", StringComparison.OrdinalIgnoreCase))
return WrapperType.CIA;
#endregion
@@ -213,369 +259,6 @@ namespace SabreTools.Serialization.Wrappers
return FileTypes.Executable;
*/
#endregion
#region GCF
if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }))
return WrapperType.GCF;
#endregion
#region GZIP
if (magic.StartsWith(new byte?[] { 0x1f, 0x8b }))
return WrapperType.GZIP;
#endregion
#region IniFile
// No magic checks for IniFile
#endregion
#region InstallShieldArchiveV3
if (magic.StartsWith(new byte?[] { 0x13, 0x5D, 0x65, 0x8C }))
return WrapperType.InstallShieldArchiveV3;
#endregion
#region InstallShieldCAB
if (magic.StartsWith(new byte?[] { 0x49, 0x53, 0x63 }))
return WrapperType.InstallShieldCAB;
#endregion
#region LDSCRYPT
if (magic.StartsWith(new byte?[] { 0x4C, 0x44, 0x53, 0x43, 0x52, 0x59, 0x50, 0x54 }))
return WrapperType.LDSCRYPT;
#endregion
#region MicrosoftCAB
if (magic.StartsWith(new byte?[] { 0x4d, 0x53, 0x43, 0x46 }))
return WrapperType.MicrosoftCAB;
#endregion
#region MicrosoftLZ
if (magic.StartsWith(new byte?[] { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 }))
return WrapperType.MicrosoftLZ;
#endregion
#region MPQ
if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1a }))
return WrapperType.MoPaQ;
if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1b }))
return WrapperType.MoPaQ;
#endregion
#region N3DS
// No magic checks for N3DS
#endregion
#region NCF
if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 }))
return WrapperType.NCF;
#endregion
#region Nitro
// No magic checks for Nitro
#endregion
#region PAK
if (magic.StartsWith(new byte?[] { 0x50, 0x41, 0x43, 0x4B }))
return WrapperType.PAK;
#endregion
#region PFF
// Version 2
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x32 }))
return WrapperType.PFF;
// Version 3
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x33 }))
return WrapperType.PFF;
// Version 4
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x34 }))
return WrapperType.PFF;
#endregion
#region PKZIP
// PKZIP (Unknown)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x00, 0x00 }))
return WrapperType.PKZIP;
// PKZIP
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x03, 0x04 }))
return WrapperType.PKZIP;
// PKZIP (Empty Archive)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x05, 0x06 }))
return WrapperType.PKZIP;
// PKZIP (Spanned Archive)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x07, 0x08 }))
return WrapperType.PKZIP;
#endregion
#region PLJ
// https://www.iana.org/assignments/media-types/audio/vnd.everad.plj
if (magic.StartsWith(new byte?[] { 0xFF, 0x9D, 0x53, 0x4B }))
return WrapperType.PlayJAudioFile;
#endregion
#region Quantum
if (magic.StartsWith(new byte?[] { 0x44, 0x53 }))
return WrapperType.Quantum;
#endregion
#region RAR
// RAR archive version 1.50 onwards
if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 }))
return WrapperType.RAR;
// RAR archive version 5.0 onwards
if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00 }))
return WrapperType.RAR;
#endregion
#region RealArcade
// RASGI2.0
// Found in the ".rgs files in IA item "Nova_RealArcadeCD_USA".
if (magic.StartsWith(new byte?[] { 0x52, 0x41, 0x53, 0x47, 0x49, 0x32, 0x2E, 0x30 }))
return WrapperType.RealArcadeInstaller;
// XZip2.0
// Found in the ".mez" files in IA item "Nova_RealArcadeCD_USA".
if (magic.StartsWith(new byte?[] { 0x58, 0x5A, 0x69, 0x70, 0x32, 0x2E, 0x30 }))
return WrapperType.RealArcadeMezzanine;
#endregion
#region SevenZip
if (magic.StartsWith(new byte?[] { 0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c }))
return WrapperType.SevenZip;
#endregion
#region SFFS
// Found in Redump entry 81756, confirmed to be "StarForce Filesystem" by PiD.
if (magic.StartsWith(new byte?[] { 0x53, 0x46, 0x46, 0x53 }))
return WrapperType.SFFS;
#endregion
#region SGA
if (magic.StartsWith(new byte?[] { 0x5F, 0x41, 0x52, 0x43, 0x48, 0x49, 0x56, 0x45 }))
return WrapperType.SGA;
#endregion
#region TapeArchive
if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30 }))
return WrapperType.TapeArchive;
if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00 }))
return WrapperType.TapeArchive;
#endregion
#region Textfile
// Not all textfiles can be determined through magic number
// HTML
if (magic.StartsWith(new byte?[] { 0x3c, 0x68, 0x74, 0x6d, 0x6c }))
return WrapperType.Textfile;
// HTML and XML
if (magic.StartsWith(new byte?[] { 0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45 }))
return WrapperType.Textfile;
// InstallShield Compiled Rules
if (magic.StartsWith(new byte?[] { 0x61, 0x4C, 0x75, 0x5A }))
return WrapperType.Textfile;
// Microsoft Office File (old)
if (magic.StartsWith(new byte?[] { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 }))
return WrapperType.Textfile;
// Rich Text File
if (magic.StartsWith(new byte?[] { 0x7b, 0x5c, 0x72, 0x74, 0x66, 0x31 }))
return WrapperType.Textfile;
// Windows Help File
if (magic.StartsWith(new byte?[] { 0x3F, 0x5F, 0x03, 0x00 }))
return WrapperType.Textfile;
// XML
// "<?xml"
if (magic.StartsWith(new byte?[] { 0x3C, 0x3F, 0x78, 0x6D, 0x6C }))
return WrapperType.Textfile;
#endregion
#region VBSP
if (magic.StartsWith(new byte?[] { 0x56, 0x42, 0x53, 0x50 }))
return WrapperType.VBSP;
#endregion
#region VPK
if (magic.StartsWith(new byte?[] { 0x34, 0x12, 0xaa, 0x55 }))
return WrapperType.VPK;
#endregion
#region WAD
if (magic.StartsWith(new byte?[] { 0x57, 0x41, 0x44, 0x33 }))
return WrapperType.WAD;
#endregion
#region XZ
if (magic.StartsWith(new byte?[] { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }))
return WrapperType.XZ;
#endregion
#region XZP
if (magic.StartsWith(new byte?[] { 0x70, 0x69, 0x5A, 0x78 }))
return WrapperType.XZP;
#endregion
// We couldn't find a supported match
return WrapperType.UNKNOWN;
}
/// <summary>
/// Get the supported file type for an extension
/// </summary>
/// <remarks>This is less accurate than a magic string match</remarks>
public static WrapperType GetFileType(string extension)
{
// If we have an invalid extension
if (string.IsNullOrEmpty(extension))
return WrapperType.UNKNOWN;
// Normalize the extension
extension = extension.TrimStart('.').Trim();
#region AACSMediaKeyBlock
// Shares an extension with INF setup information so it can't be used accurately
// Blu-ray
// if (extension.Equals("inf", StringComparison.OrdinalIgnoreCase))
// return WrapperType.AACSMediaKeyBlock;
// HD-DVD
if (extension.Equals("aacs", StringComparison.OrdinalIgnoreCase))
return WrapperType.AACSMediaKeyBlock;
#endregion
#region BDPlusSVM
if (extension.Equals("svm", StringComparison.OrdinalIgnoreCase))
return WrapperType.BDPlusSVM;
#endregion
#region BFPK
// No extensions registered for BFPK
#endregion
#region BSP
// Shares an extension with VBSP so it can't be used accurately
// if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase))
// return WrapperType.BSP;
#endregion
#region BZip2
if (extension.Equals("bz2", StringComparison.OrdinalIgnoreCase))
return WrapperType.BZip2;
#endregion
#region CFB
// Installer package
if (extension.Equals("msi", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Merge module
else if (extension.Equals("msm", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Patch Package
else if (extension.Equals("msp", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Transform
else if (extension.Equals("mst", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
// Patch Creation Properties
else if (extension.Equals("pcp", StringComparison.OrdinalIgnoreCase))
return WrapperType.CFB;
#endregion
#region CIA
if (extension.Equals("cia", StringComparison.OrdinalIgnoreCase))
return WrapperType.CIA;
#endregion
#region Executable
// DOS MZ executable file format (and descendants)
if (extension.Equals("exe", StringComparison.OrdinalIgnoreCase))
return WrapperType.Executable;
@@ -588,6 +271,9 @@ namespace SabreTools.Serialization.Wrappers
#region GCF
if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }))
return WrapperType.GCF;
if (extension.Equals("gcf", StringComparison.OrdinalIgnoreCase))
return WrapperType.GCF;
@@ -595,6 +281,9 @@ namespace SabreTools.Serialization.Wrappers
#region GZIP
if (magic.StartsWith(new byte?[] { 0x1f, 0x8b }))
return WrapperType.GZIP;
if (extension.Equals("gz", StringComparison.OrdinalIgnoreCase))
return WrapperType.GZIP;
@@ -609,6 +298,9 @@ namespace SabreTools.Serialization.Wrappers
#region InstallShieldArchiveV3
if (magic.StartsWith(new byte?[] { 0x13, 0x5D, 0x65, 0x8C }))
return WrapperType.InstallShieldArchiveV3;
if (extension.Equals("z", StringComparison.OrdinalIgnoreCase))
return WrapperType.InstallShieldArchiveV3;
@@ -616,19 +308,43 @@ namespace SabreTools.Serialization.Wrappers
#region InstallShieldCAB
// No extensions registered for InstallShieldCAB
if (magic.StartsWith(new byte?[] { 0x49, 0x53, 0x63 }))
return WrapperType.InstallShieldCAB;
// Both InstallShieldCAB and MicrosoftCAB share the same extension
#endregion
#region LDSCRYPT
if (magic.StartsWith(new byte?[] { 0x4C, 0x44, 0x53, 0x43, 0x52, 0x59, 0x50, 0x54 }))
return WrapperType.LDSCRYPT;
#endregion
#region MicrosoftCAB
// No extensions registered for InstallShieldCAB
if (magic.StartsWith(new byte?[] { 0x4d, 0x53, 0x43, 0x46 }))
return WrapperType.MicrosoftCAB;
// Both InstallShieldCAB and MicrosoftCAB share the same extension
#endregion
#region MPQ
#region MicrosoftLZ
if (magic.StartsWith(new byte?[] { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 }))
return WrapperType.MicrosoftLZ;
#endregion
#region MoPaQ
if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1a }))
return WrapperType.MoPaQ;
if (magic.StartsWith(new byte?[] { 0x4d, 0x50, 0x51, 0x1b }))
return WrapperType.MoPaQ;
if (extension.Equals("mpq", StringComparison.OrdinalIgnoreCase))
return WrapperType.MoPaQ;
@@ -649,6 +365,9 @@ namespace SabreTools.Serialization.Wrappers
#region NCF
if (magic.StartsWith(new byte?[] { 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 }))
return WrapperType.NCF;
if (extension.Equals("ncf", StringComparison.OrdinalIgnoreCase))
return WrapperType.NCF;
@@ -676,7 +395,9 @@ namespace SabreTools.Serialization.Wrappers
#region PAK
// No extensions registered for PAK
if (magic.StartsWith(new byte?[] { 0x50, 0x41, 0x43, 0x4B }))
return WrapperType.PAK;
// Both PAK and Quantum share one extension
// if (extension.Equals("pak", StringComparison.OrdinalIgnoreCase))
// return WrapperType.PAK;
@@ -685,6 +406,18 @@ namespace SabreTools.Serialization.Wrappers
#region PFF
// Version 2
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x32 }))
return WrapperType.PFF;
// Version 3
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x33 }))
return WrapperType.PFF;
// Version 4
if (magic.StartsWith(new byte?[] { 0x14, 0x00, 0x00, 0x00, 0x50, 0x46, 0x46, 0x34 }))
return WrapperType.PFF;
if (extension.Equals("pff", StringComparison.OrdinalIgnoreCase))
return WrapperType.PFF;
@@ -692,6 +425,22 @@ namespace SabreTools.Serialization.Wrappers
#region PKZIP
// PKZIP (Unknown)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x00, 0x00 }))
return WrapperType.PKZIP;
// PKZIP
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x03, 0x04 }))
return WrapperType.PKZIP;
// PKZIP (Empty Archive)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x05, 0x06 }))
return WrapperType.PKZIP;
// PKZIP (Spanned Archive)
if (magic.StartsWith(new byte?[] { 0x50, 0x4b, 0x07, 0x08 }))
return WrapperType.PKZIP;
// PKZIP
if (extension.Equals("zip", StringComparison.OrdinalIgnoreCase))
return WrapperType.PKZIP;
@@ -780,6 +529,10 @@ namespace SabreTools.Serialization.Wrappers
#region PLJ
// https://www.iana.org/assignments/media-types/audio/vnd.everad.plj
if (magic.StartsWith(new byte?[] { 0xFF, 0x9D, 0x53, 0x4B }))
return WrapperType.PlayJAudioFile;
// https://www.iana.org/assignments/media-types/audio/vnd.everad.plj
if (extension.Equals("plj", StringComparison.OrdinalIgnoreCase))
return WrapperType.PlayJAudioFile;
@@ -788,6 +541,9 @@ namespace SabreTools.Serialization.Wrappers
#region Quantum
if (magic.StartsWith(new byte?[] { 0x44, 0x53 }))
return WrapperType.Quantum;
if (extension.Equals("q", StringComparison.OrdinalIgnoreCase))
return WrapperType.Quantum;
@@ -799,20 +555,56 @@ namespace SabreTools.Serialization.Wrappers
#region RAR
// RAR archive version 1.50 onwards
if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 }))
return WrapperType.RAR;
// RAR archive version 5.0 onwards
if (magic.StartsWith(new byte?[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00 }))
return WrapperType.RAR;
if (extension.Equals("rar", StringComparison.OrdinalIgnoreCase))
return WrapperType.RAR;
#endregion
#region RealArcade
// RASGI2.0
// Found in the ".rgs files in IA item "Nova_RealArcadeCD_USA".
if (magic.StartsWith(new byte?[] { 0x52, 0x41, 0x53, 0x47, 0x49, 0x32, 0x2E, 0x30 }))
return WrapperType.RealArcadeInstaller;
// XZip2.0
// Found in the ".mez" files in IA item "Nova_RealArcadeCD_USA".
if (magic.StartsWith(new byte?[] { 0x58, 0x5A, 0x69, 0x70, 0x32, 0x2E, 0x30 }))
return WrapperType.RealArcadeMezzanine;
#endregion
#region SevenZip
if (magic.StartsWith(new byte?[] { 0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c }))
return WrapperType.SevenZip;
if (extension.Equals("7z", StringComparison.OrdinalIgnoreCase))
return WrapperType.SevenZip;
#endregion
#region SFFS
// Found in Redump entry 81756, confirmed to be "StarForce Filesystem" by PiD.
if (magic.StartsWith(new byte?[] { 0x53, 0x46, 0x46, 0x53 }))
return WrapperType.SFFS;
#endregion
#region SGA
if (magic.StartsWith(new byte?[] { 0x5F, 0x41, 0x52, 0x43, 0x48, 0x49, 0x56, 0x45 }))
return WrapperType.SGA;
if (extension.Equals("sga", StringComparison.OrdinalIgnoreCase))
return WrapperType.SGA;
@@ -820,6 +612,12 @@ namespace SabreTools.Serialization.Wrappers
#region TapeArchive
if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30 }))
return WrapperType.TapeArchive;
if (magic.StartsWith(new byte?[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00 }))
return WrapperType.TapeArchive;
if (extension.Equals("tar", StringComparison.OrdinalIgnoreCase))
return WrapperType.SevenZip;
@@ -827,6 +625,37 @@ namespace SabreTools.Serialization.Wrappers
#region Textfile
// Not all textfiles can be determined through magic number
// HTML
if (magic.StartsWith(new byte?[] { 0x3c, 0x68, 0x74, 0x6d, 0x6c }))
return WrapperType.Textfile;
// HTML and XML
if (magic.StartsWith(new byte?[] { 0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45 }))
return WrapperType.Textfile;
// InstallShield Compiled Rules
if (magic.StartsWith(new byte?[] { 0x61, 0x4C, 0x75, 0x5A }))
return WrapperType.Textfile;
// Microsoft Office File (old)
if (magic.StartsWith(new byte?[] { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 }))
return WrapperType.Textfile;
// Rich Text File
if (magic.StartsWith(new byte?[] { 0x7b, 0x5c, 0x72, 0x74, 0x66, 0x31 }))
return WrapperType.Textfile;
// Windows Help File
if (magic.StartsWith(new byte?[] { 0x3F, 0x5F, 0x03, 0x00 }))
return WrapperType.Textfile;
// XML
// "<?xml"
if (magic.StartsWith(new byte?[] { 0x3C, 0x3F, 0x78, 0x6D, 0x6C }))
return WrapperType.Textfile;
// "Description in Zip"
if (extension.Equals("diz", StringComparison.OrdinalIgnoreCase))
return WrapperType.Textfile;
@@ -873,20 +702,24 @@ namespace SabreTools.Serialization.Wrappers
if (extension.Equals("xml", StringComparison.OrdinalIgnoreCase))
return WrapperType.Textfile;
#endregion
#region VBSP
// Shares an extension with BSP so it can't be used accurately
// if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase))
// return WrapperType.VBSP;
if (magic.StartsWith(new byte?[] { 0x56, 0x42, 0x53, 0x50 }))
return WrapperType.VBSP;
// Shares an extension with BSP
if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase))
return WrapperType.VBSP;
#endregion
#region VPK
if (magic.StartsWith(new byte?[] { 0x34, 0x12, 0xaa, 0x55 }))
return WrapperType.VPK;
// Common extension so this cannot be used accurately
// if (extension.Equals("vpk", StringComparison.OrdinalIgnoreCase))
// return WrapperType.VPK;
@@ -895,6 +728,9 @@ namespace SabreTools.Serialization.Wrappers
#region WAD
if (magic.StartsWith(new byte?[] { 0x57, 0x41, 0x44, 0x33 }))
return WrapperType.WAD;
// Common extension so this cannot be used accurately
// if (extension.Equals("wad", StringComparison.OrdinalIgnoreCase))
// return WrapperType.WAD;
@@ -903,6 +739,9 @@ namespace SabreTools.Serialization.Wrappers
#region XZ
if (magic.StartsWith(new byte?[] { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }))
return WrapperType.XZ;
if (extension.Equals("xz", StringComparison.OrdinalIgnoreCase))
return WrapperType.XZ;
@@ -910,6 +749,9 @@ namespace SabreTools.Serialization.Wrappers
#region XZP
if (magic.StartsWith(new byte?[] { 0x70, 0x69, 0x5A, 0x78 }))
return WrapperType.XZP;
if (extension.Equals("xzp", StringComparison.OrdinalIgnoreCase))
return WrapperType.XZP;

View File

@@ -22,7 +22,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
var publisherIdentifier = this.Model.PublisherIdentifier;
var publisherIdentifier = Model.PublisherIdentifier;
if (string.IsNullOrEmpty(publisherIdentifier))
return "Unknown";
@@ -40,7 +40,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
var regionIdentifier = this.Model.RegionIdentifier;
var regionIdentifier = Model.RegionIdentifier;
if (Regions.ContainsKey(regionIdentifier))
return Regions[regionIdentifier];
@@ -51,12 +51,12 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Get the human-readable serial string
/// </summary>
public string Serial => $"{this.Model.PublisherIdentifier}-{this.Model.GameID}";
public string Serial => $"{Model.PublisherIdentifier}-{Model.GameID}";
/// <summary>
/// Get the human-readable version string
/// </summary>
public string Version => $"1.{this.Model.VersionNumber}";
public string Version => $"1.{Model.VersionNumber}";
#endregion

View File

@@ -22,7 +22,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
char mediaSubtype = this.Model.MediaSubtypeIdentifier;
char mediaSubtype = Model.MediaSubtypeIdentifier;
if (MediaSubtypes.ContainsKey(mediaSubtype))
return MediaSubtypes[mediaSubtype];
@@ -37,7 +37,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
var publisherIdentifier = this.Model.PublisherIdentifier;
var publisherIdentifier = Model.PublisherIdentifier;
if (string.IsNullOrEmpty(publisherIdentifier))
return "Unknown";
@@ -55,7 +55,7 @@ namespace SabreTools.Serialization.Wrappers
{
get
{
var regionIdentifier = this.Model.RegionIdentifier;
var regionIdentifier = Model.RegionIdentifier;
if (Regions.ContainsKey(regionIdentifier))
return Regions[regionIdentifier];
@@ -66,12 +66,12 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Get the human-readable serial string
/// </summary>
public string Serial => $"{this.Model.PublisherIdentifier}-{this.Model.PlatformIdentifier}{this.Model.GameID}";
public string Serial => $"{Model.PublisherIdentifier}-{Model.PlatformIdentifier}{Model.GameID}";
/// <summary>
/// Get the human-readable version string
/// </summary>
public string Version => $"1.{this.Model.SKU}";
public string Version => $"1.{Model.SKU}";
#endregion

View File

@@ -23,7 +23,7 @@ namespace Test
}
// Loop through the input paths
foreach (string inputPath in args)
foreach (string inputPath in options.InputPaths)
{
#if NETFRAMEWORK
PrintPathInfo(inputPath, false, options.Debug);
@@ -81,12 +81,8 @@ namespace Test
stream.Seek(0, SeekOrigin.Begin);
// Get the file type
WrapperType ft = WrapperFactory.GetFileType(magic ?? []);
if (ft == WrapperType.UNKNOWN)
{
string extension = Path.GetExtension(file).TrimStart('.');
ft = WrapperFactory.GetFileType(extension);
}
string extension = Path.GetExtension(file).TrimStart('.');
WrapperType ft = WrapperFactory.GetFileType(magic ?? [], extension);
// Print out the file format
Console.WriteLine($"File format found: {ft}");