From 4a3741312ed9ae3602af3f8cca6197705b1b41e1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 27 Nov 2024 16:14:56 -0500 Subject: [PATCH] Reduce unncessary methods in deserialization --- .../Deserializers/BDPlus.cs | 3 - .../Deserializers/BFPK.cs | 19 +- SabreTools.Serialization/Deserializers/BSP.cs | 22 +- SabreTools.Serialization/Deserializers/CFB.cs | 1 - SabreTools.Serialization/Deserializers/CHD.cs | 3 - SabreTools.Serialization/Deserializers/CIA.cs | 78 +---- SabreTools.Serialization/Deserializers/GCF.cs | 282 ++---------------- .../Deserializers/InstallShieldArchiveV3.cs | 37 +-- .../Deserializers/InstallShieldCabinet.cs | 31 +- .../Deserializers/LinearExecutable.cs | 101 +------ .../Deserializers/MSDOS.cs | 12 +- .../Deserializers/MoPaQ.cs | 61 +--- .../Deserializers/N3DS.cs | 73 +---- SabreTools.Serialization/Deserializers/NCF.cs | 222 ++------------ .../Deserializers/NewExecutable.cs | 90 ++---- .../Deserializers/Nitro.cs | 48 +-- SabreTools.Serialization/Deserializers/PAK.cs | 31 +- SabreTools.Serialization/Deserializers/PFF.cs | 17 +- SabreTools.Serialization/Deserializers/PIC.cs | 34 +-- .../Deserializers/PKZIP.cs | 12 +- .../Deserializers/PortableExecutable.cs | 36 +-- .../Deserializers/Quantum.cs | 19 +- SabreTools.Serialization/Deserializers/SFB.cs | 4 +- .../Deserializers/VBSP.cs | 25 +- SabreTools.Serialization/Deserializers/VPK.cs | 66 +--- .../Deserializers/WAD3.cs | 52 +--- .../Deserializers/XMID.cs | 3 +- SabreTools.Serialization/Deserializers/XZP.cs | 68 +---- .../Deserializers/XeMID.cs | 3 +- 29 files changed, 186 insertions(+), 1267 deletions(-) diff --git a/SabreTools.Serialization/Deserializers/BDPlus.cs b/SabreTools.Serialization/Deserializers/BDPlus.cs index 8c35b837..744ada84 100644 --- a/SabreTools.Serialization/Deserializers/BDPlus.cs +++ b/SabreTools.Serialization/Deserializers/BDPlus.cs @@ -19,9 +19,6 @@ namespace SabreTools.Serialization.Deserializers if (data.Position < 0 || data.Position >= data.Length) return null; - // Cache the current offset - int initialOffset = (int)data.Position; - // Try to parse the SVM return ParseSVMData(data); } diff --git a/SabreTools.Serialization/Deserializers/BFPK.cs b/SabreTools.Serialization/Deserializers/BFPK.cs index 8dd795b1..d5146524 100644 --- a/SabreTools.Serialization/Deserializers/BFPK.cs +++ b/SabreTools.Serialization/Deserializers/BFPK.cs @@ -28,8 +28,8 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseHeader(data); - if (header == null) + var header = data.ReadType
(); + if (header?.Magic != SignatureString) return null; // Set the archive header @@ -60,21 +60,6 @@ namespace SabreTools.Serialization.Deserializers return archive; } - /// - /// Parse a Stream into a header - /// - /// Stream to parse - /// Filled header on success, null on error - private static Header? ParseHeader(Stream data) - { - var header = data.ReadType
(); - - if (header?.Magic != SignatureString) - return null; - - return header; - } - /// /// Parse a Stream into a file entry /// diff --git a/SabreTools.Serialization/Deserializers/BSP.cs b/SabreTools.Serialization/Deserializers/BSP.cs index 59958322..aa7f1553 100644 --- a/SabreTools.Serialization/Deserializers/BSP.cs +++ b/SabreTools.Serialization/Deserializers/BSP.cs @@ -27,9 +27,11 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseHeader(data); + var header = data.ReadType(); if (header?.Lumps == null) return null; + if (header.Version < 29 || header.Version > 30) + return null; // Set the level header file.Header = header; @@ -109,24 +111,6 @@ namespace SabreTools.Serialization.Deserializers return file; } - /// - /// Parse a Stream into a Half-Life Level header - /// - /// Stream to parse - /// Filled Half-Life Level header on success, null on error - /// Only recognized versions are 29 and 30 - private static BspHeader? ParseHeader(Stream data) - { - var header = data.ReadType(); - - if (header == null) - return null; - if (header.Version < 29 || header.Version > 30) - return null; - - return header; - } - /// /// Parse a Stream into LUMP_ENTITIES /// diff --git a/SabreTools.Serialization/Deserializers/CFB.cs b/SabreTools.Serialization/Deserializers/CFB.cs index e1e60c4c..0068233a 100644 --- a/SabreTools.Serialization/Deserializers/CFB.cs +++ b/SabreTools.Serialization/Deserializers/CFB.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; using SabreTools.IO.Extensions; using SabreTools.Models.CFB; using static SabreTools.Models.CFB.Constants; diff --git a/SabreTools.Serialization/Deserializers/CHD.cs b/SabreTools.Serialization/Deserializers/CHD.cs index fa72bd2d..a742d19f 100644 --- a/SabreTools.Serialization/Deserializers/CHD.cs +++ b/SabreTools.Serialization/Deserializers/CHD.cs @@ -20,9 +20,6 @@ namespace SabreTools.Serialization.Deserializers if (data.Position < 0 || data.Position >= data.Length) return null; - // Cache the current offset - int initialOffset = (int)data.Position; - // Determine the header version uint version = GetVersion(data); diff --git a/SabreTools.Serialization/Deserializers/CIA.cs b/SabreTools.Serialization/Deserializers/CIA.cs index e37c6a75..b5f896a9 100644 --- a/SabreTools.Serialization/Deserializers/CIA.cs +++ b/SabreTools.Serialization/Deserializers/CIA.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Text; using SabreTools.IO.Extensions; @@ -25,9 +24,19 @@ namespace SabreTools.Serialization.Deserializers #region CIA Header // Try to parse the header - var header = ParseCIAHeader(data); + var header = data.ReadType(); if (header == null) return null; + if (header.CertificateChainSize > data.Length) + return null; + if (header.TicketSize > data.Length) + return null; + if (header.TMDFileSize > data.Length) + return null; + if (header.MetaSize > data.Length) + return null; + if ((long)header.ContentSize > data.Length) + return null; // Set the CIA archive header cia.Header = header; @@ -124,7 +133,7 @@ namespace SabreTools.Serialization.Deserializers if (header.MetaSize > 0) { // Try to parse the meta - var meta = ParseMetaData(data); + var meta = data.ReadType(); if (meta == null) return null; @@ -137,31 +146,6 @@ namespace SabreTools.Serialization.Deserializers return cia; } - /// - /// Parse a Stream into a CIA header - /// - /// Stream to parse - /// Filled CIA header on success, null on error - public static CIAHeader? ParseCIAHeader(Stream data) - { - var header = data.ReadType(); - - if (header == null) - return null; - if (header.CertificateChainSize > data.Length) - return null; - if (header.TicketSize > data.Length) - return null; - if (header.TMDFileSize > data.Length) - return null; - if (header.MetaSize > data.Length) - return null; - if ((long)header.ContentSize > data.Length) - return null; - - return header; - } - /// /// Parse a Stream into a certificate /// @@ -399,20 +383,20 @@ namespace SabreTools.Serialization.Deserializers titleMetadata.ContentInfoRecords = new ContentInfoRecord[64]; for (int i = 0; i < 64; i++) { - var contentInfoRecord = ParseContentInfoRecord(data); + var contentInfoRecord = data.ReadType(); if (contentInfoRecord == null) return null; - titleMetadata.ContentInfoRecords[i] = ParseContentInfoRecord(data); + titleMetadata.ContentInfoRecords[i] = contentInfoRecord; } titleMetadata.ContentChunkRecords = new ContentChunkRecord[titleMetadata.ContentCount]; for (int i = 0; i < titleMetadata.ContentCount; i++) { - var contentChunkRecord = ParseContentChunkRecord(data); + var contentChunkRecord = data.ReadType(); if (contentChunkRecord == null) return null; - titleMetadata.ContentChunkRecords[i] = ParseContentChunkRecord(data); + titleMetadata.ContentChunkRecords[i] = contentChunkRecord; } // Certificates only exist in standalone TMD files @@ -431,35 +415,5 @@ namespace SabreTools.Serialization.Deserializers return titleMetadata; } - - /// - /// Parse a Stream into a content info record - /// - /// Stream to parse - /// Filled content info record on success, null on error - public static ContentInfoRecord? ParseContentInfoRecord(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a content chunk record - /// - /// Stream to parse - /// Filled content chunk record on success, null on error - public static ContentChunkRecord? ParseContentChunkRecord(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a meta data - /// - /// Stream to parse - /// Filled meta data on success, null on error - public static MetaData? ParseMetaData(Stream data) - { - return data.ReadType(); - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/GCF.cs b/SabreTools.Serialization/Deserializers/GCF.cs index c30f27e9..26d32440 100644 --- a/SabreTools.Serialization/Deserializers/GCF.cs +++ b/SabreTools.Serialization/Deserializers/GCF.cs @@ -27,8 +27,12 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseHeader(data); - if (header == null) + var header = data.ReadType
(); + if (header?.Dummy0 != 0x00000001) + return null; + if (header?.MajorVersion != 0x00000001) + return null; + if (header.MinorVersion != 3 && header.MinorVersion != 5 && header.MinorVersion != 6) return null; // Set the game cache header @@ -39,7 +43,7 @@ namespace SabreTools.Serialization.Deserializers #region Block Entry Header // Try to parse the block entry header - var blockEntryHeader = ParseBlockEntryHeader(data); + var blockEntryHeader = data.ReadType(); if (blockEntryHeader == null) return null; @@ -56,7 +60,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the block entries for (int i = 0; i < blockEntryHeader.BlockCount; i++) { - var blockEntry = ParseBlockEntry(data); + var blockEntry = data.ReadType(); if (blockEntry == null) return null; @@ -68,7 +72,7 @@ namespace SabreTools.Serialization.Deserializers #region Fragmentation Map Header // Try to parse the fragmentation map header - var fragmentationMapHeader = ParseFragmentationMapHeader(data); + var fragmentationMapHeader = data.ReadType(); if (fragmentationMapHeader == null) return null; @@ -85,7 +89,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the fragmentation maps for (int i = 0; i < fragmentationMapHeader.BlockCount; i++) { - var fragmentationMap = ParseFragmentationMap(data); + var fragmentationMap = data.ReadType(); if (fragmentationMap == null) return null; @@ -99,7 +103,7 @@ namespace SabreTools.Serialization.Deserializers if (header.MinorVersion < 6) { // Try to parse the block entry map header - var blockEntryMapHeader = ParseBlockEntryMapHeader(data); + var blockEntryMapHeader = data.ReadType(); if (blockEntryMapHeader == null) return null; @@ -119,7 +123,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the block entry maps for (int i = 0; i < file.BlockEntryMapHeader.BlockCount; i++) { - var blockEntryMap = ParseBlockEntryMap(data); + var blockEntryMap = data.ReadType(); if (blockEntryMap == null) return null; @@ -135,7 +139,7 @@ namespace SabreTools.Serialization.Deserializers #region Directory Header // Try to parse the directory header - var directoryHeader = ParseDirectoryHeader(data); + var directoryHeader = data.ReadType(); if (directoryHeader == null) return null; @@ -152,7 +156,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var directoryEntry = ParseDirectoryEntry(data); + var directoryEntry = data.ReadType(); if (directoryEntry == null) return null; @@ -200,7 +204,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory info 1 entries for (int i = 0; i < directoryHeader.Info1Count; i++) { - var directoryInfo1Entry = ParseDirectoryInfo1Entry(data); + var directoryInfo1Entry = data.ReadType(); if (directoryInfo1Entry == null) return null; @@ -217,7 +221,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory info 2 entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var directoryInfo2Entry = ParseDirectoryInfo2Entry(data); + var directoryInfo2Entry = data.ReadType(); if (directoryInfo2Entry == null) return null; @@ -234,7 +238,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory copy entries for (int i = 0; i < directoryHeader.CopyCount; i++) { - var directoryCopyEntry = ParseDirectoryCopyEntry(data); + var directoryCopyEntry = data.ReadType(); if (directoryCopyEntry == null) return null; @@ -251,7 +255,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory local entries for (int i = 0; i < directoryHeader.LocalCount; i++) { - var directoryLocalEntry = ParseDirectoryLocalEntry(data); + var directoryLocalEntry = data.ReadType(); if (directoryLocalEntry == null) return null; @@ -268,9 +272,11 @@ namespace SabreTools.Serialization.Deserializers if (header.MinorVersion >= 5) { // Try to parse the directory map header - var directoryMapHeader = ParseDirectoryMapHeader(data); - if (directoryMapHeader == null) - return null; + var directoryMapHeader = data.ReadType(); + if (directoryMapHeader?.Dummy0 != 0x00000001) + return null; + if (directoryMapHeader?.Dummy1 != 0x00000000) + return null; // Set the game cache directory map header file.DirectoryMapHeader = directoryMapHeader; @@ -286,7 +292,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory map entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var directoryMapEntry = ParseDirectoryMapEntry(data); + var directoryMapEntry = data.ReadType(); if (directoryMapEntry == null) return null; @@ -298,8 +304,8 @@ namespace SabreTools.Serialization.Deserializers #region Checksum Header // Try to parse the checksum header - var checksumHeader = ParseChecksumHeader(data); - if (checksumHeader == null) + var checksumHeader = data.ReadType(); + if (checksumHeader?.Dummy0 != 0x00000001) return null; // Set the game cache checksum header @@ -313,8 +319,10 @@ namespace SabreTools.Serialization.Deserializers #region Checksum Map Header // Try to parse the checksum map header - var checksumMapHeader = ParseChecksumMapHeader(data); - if (checksumMapHeader == null) + var checksumMapHeader = data.ReadType(); + if (checksumMapHeader?.Dummy0 != 0x14893721) + return null; + if (checksumMapHeader?.Dummy1 != 0x00000001) return null; // Set the game cache checksum map header @@ -330,7 +338,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the checksum map entries for (int i = 0; i < checksumMapHeader.ItemCount; i++) { - var checksumMapEntry = ParseChecksumMapEntry(data); + var checksumMapEntry = data.ReadType(); if (checksumMapEntry == null) return null; @@ -347,7 +355,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the checksum entries for (int i = 0; i < checksumMapHeader.ChecksumCount; i++) { - var checksumEntry = ParseChecksumEntry(data); + var checksumEntry = data.ReadType(); if (checksumEntry == null) return null; @@ -374,232 +382,6 @@ namespace SabreTools.Serialization.Deserializers return file; } - /// - /// Parse a Stream into a Half-Life Game Cache header - /// - /// Stream to parse - /// Filled Half-Life Game Cache on success, null on error - private static Header? ParseHeader(Stream data) - { - var header = data.ReadType
(); - - if (header == null) - return null; - if (header.Dummy0 != 0x00000001) - return null; - if (header.MajorVersion != 0x00000001) - return null; - if (header.MinorVersion != 3 && header.MinorVersion != 5 && header.MinorVersion != 6) - return null; - - return header; - } - - /// - /// Parse a Stream into a Half-Life Game Cache block entry header - /// - /// Stream to parse - /// Filled Half-Life Game Cache block entry header on success, null on error - private static BlockEntryHeader? ParseBlockEntryHeader(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache block entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache block entry on success, null on error - private static BlockEntry? ParseBlockEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache fragmentation map header - /// - /// Stream to parse - /// Filled Half-Life Game Cache fragmentation map header on success, null on error - private static FragmentationMapHeader? ParseFragmentationMapHeader(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache fragmentation map - /// - /// Stream to parse - /// Filled Half-Life Game Cache fragmentation map on success, null on error - private static FragmentationMap? ParseFragmentationMap(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache block entry map header - /// - /// Stream to parse - /// Filled Half-Life Game Cache block entry map header on success, null on error - private static BlockEntryMapHeader? ParseBlockEntryMapHeader(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache block entry map - /// - /// Stream to parse - /// Filled Half-Life Game Cache block entry map on success, null on error - private static BlockEntryMap? ParseBlockEntryMap(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory header - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory header on success, null on error - private static DirectoryHeader? ParseDirectoryHeader(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory entry on success, null on error - private static DirectoryEntry? ParseDirectoryEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory info 1 entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory info 1 entry on success, null on error - private static DirectoryInfo1Entry? ParseDirectoryInfo1Entry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory info 2 entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory info 2 entry on success, null on error - private static DirectoryInfo2Entry? ParseDirectoryInfo2Entry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory copy entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory copy entry on success, null on error - private static DirectoryCopyEntry? ParseDirectoryCopyEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory local entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory local entry on success, null on error - private static DirectoryLocalEntry? ParseDirectoryLocalEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory map header - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory map header on success, null on error - private static DirectoryMapHeader? ParseDirectoryMapHeader(Stream data) - { - var directoryMapHeader = data.ReadType(); - - if (directoryMapHeader == null) - return null; - if (directoryMapHeader.Dummy0 != 0x00000001) - return null; - if (directoryMapHeader.Dummy1 != 0x00000000) - return null; - - return directoryMapHeader; - } - - /// - /// Parse a Stream into a Half-Life Game Cache directory map entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache directory map entry on success, null on error - private static DirectoryMapEntry? ParseDirectoryMapEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache checksum header - /// - /// Stream to parse - /// Filled Half-Life Game Cache checksum header on success, null on error - private static ChecksumHeader? ParseChecksumHeader(Stream data) - { - var checksumHeader = data.ReadType(); - - if (checksumHeader == null) - return null; - if (checksumHeader.Dummy0 != 0x00000001) - return null; - - return checksumHeader; - } - - /// - /// Parse a Stream into a Half-Life Game Cache checksum map header - /// - /// Stream to parse - /// Filled Half-Life Game Cache checksum map header on success, null on error - private static ChecksumMapHeader? ParseChecksumMapHeader(Stream data) - { - var checksumMapHeader = data.ReadType(); - - if (checksumMapHeader == null) - return null; - if (checksumMapHeader.Dummy0 != 0x14893721) - return null; - if (checksumMapHeader.Dummy1 != 0x00000001) - return null; - - return checksumMapHeader; - } - - /// - /// Parse a Stream into a Half-Life Game Cache checksum map entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache checksum map entry on success, null on error - private static ChecksumMapEntry? ParseChecksumMapEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life Game Cache checksum entry - /// - /// Stream to parse - /// Filled Half-Life Game Cache checksum entry on success, null on error - private static ChecksumEntry? ParseChecksumEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a Half-Life Game Cache data block header /// diff --git a/SabreTools.Serialization/Deserializers/InstallShieldArchiveV3.cs b/SabreTools.Serialization/Deserializers/InstallShieldArchiveV3.cs index c6373902..975d3556 100644 --- a/SabreTools.Serialization/Deserializers/InstallShieldArchiveV3.cs +++ b/SabreTools.Serialization/Deserializers/InstallShieldArchiveV3.cs @@ -24,8 +24,10 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseHeader(data); - if (header == null) + var header = data.ReadType
(); + if (header?.Signature1 != Constants.HeaderSignature) + return null; + if (header.TocAddress >= data.Length) return null; // Set the archive header @@ -69,7 +71,7 @@ namespace SabreTools.Serialization.Deserializers var directory = archive.Directories[i]; for (int j = 0; j < directory.FileCount; j++) { - var file = ParseFile(data); + var file = data.ReadType(); if (file?.Name == null) return null; @@ -86,25 +88,6 @@ namespace SabreTools.Serialization.Deserializers return archive; } - /// - /// Parse a Stream into a header - /// - /// Stream to parse - /// Filled header on success, null on error - public static Header? ParseHeader(Stream data) - { - var header = data.ReadType
(); - - if (header == null) - return null; - if (header?.Signature1 != Constants.HeaderSignature) - return null; - if (header.TocAddress >= data.Length) - return null; - - return header; - } - /// /// Parse a Stream into a directory /// @@ -123,15 +106,5 @@ namespace SabreTools.Serialization.Deserializers return directory; } - - /// - /// Parse a Stream into a file - /// - /// Stream to parse - /// Filled file on success, null on error - public static Models.InstallShieldArchiveV3.File? ParseFile(Stream data) - { - return data.ReadType(); - } } } diff --git a/SabreTools.Serialization/Deserializers/InstallShieldCabinet.cs b/SabreTools.Serialization/Deserializers/InstallShieldCabinet.cs index 252ac363..2f565493 100644 --- a/SabreTools.Serialization/Deserializers/InstallShieldCabinet.cs +++ b/SabreTools.Serialization/Deserializers/InstallShieldCabinet.cs @@ -27,8 +27,8 @@ namespace SabreTools.Serialization.Deserializers #region Common Header // Try to parse the cabinet header - var commonHeader = ParseCommonHeader(data); - if (commonHeader == null) + var commonHeader = data.ReadType(); + if (commonHeader?.Signature != SignatureString) return null; // Set the cabinet header @@ -59,7 +59,7 @@ namespace SabreTools.Serialization.Deserializers data.Seek(descriptorOffset, SeekOrigin.Begin); // Try to parse the descriptor - var descriptor = ParseDescriptor(data); + var descriptor = data.ReadType(); if (descriptor == null) return null; @@ -325,21 +325,6 @@ namespace SabreTools.Serialization.Deserializers return cabinet; } - /// - /// Parse a Stream into a common header - /// - /// Stream to parse - /// Filled common header on success, null on error - public static CommonHeader? ParseCommonHeader(Stream data) - { - var commonHeader = data.ReadType(); - - if (commonHeader?.Signature != SignatureString) - return null; - - return commonHeader; - } - /// /// Parse a Stream into a volume header /// @@ -387,16 +372,6 @@ namespace SabreTools.Serialization.Deserializers return volumeHeader; } - /// - /// Parse a Stream into a descriptor - /// - /// Stream to parse - /// Filled descriptor on success, null on error - public static Descriptor? ParseDescriptor(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into an offset list /// diff --git a/SabreTools.Serialization/Deserializers/LinearExecutable.cs b/SabreTools.Serialization/Deserializers/LinearExecutable.cs index b8a9ec8f..8d3f5e5f 100644 --- a/SabreTools.Serialization/Deserializers/LinearExecutable.cs +++ b/SabreTools.Serialization/Deserializers/LinearExecutable.cs @@ -42,8 +42,8 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the executable header data.Seek(initialOffset + stub.Header.NewExeHeaderAddr, SeekOrigin.Begin); - var informationBlock = ParseInformationBlock(data); - if (informationBlock == null) + var informationBlock = data.ReadType(); + if (informationBlock?.Signature != LESignatureString && informationBlock?.Signature != LXSignatureString) return null; // Set the executable header @@ -66,7 +66,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the object table for (int i = 0; i < executable.ObjectTable.Length; i++) { - var entry = ParseObjectTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -91,7 +91,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the object page map for (int i = 0; i < executable.ObjectPageMap.Length; i++) { - var entry = ParseObjectPageMapEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -131,7 +131,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the resource table for (int i = 0; i < executable.ResourceTable.Length; i++) { - var entry = ParseResourceTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -215,7 +215,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the module format directives table for (int i = 0; i < executable.ModuleFormatDirectivesTable.Length; i++) { - var entry = ParseModuleFormatDirectivesTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -248,7 +248,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the fix-up page table for (int i = 0; i < executable.FixupPageTable.Length; i++) { - var entry = ParseFixupPageTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -356,7 +356,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the per-page checksum name table for (int i = 0; i < executable.PerPageChecksumTable.Length; i++) { - var entry = ParsePerPageChecksumTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -418,51 +418,6 @@ namespace SabreTools.Serialization.Deserializers return executable; } - /// - /// Parse a Stream into an information block - /// - /// Stream to parse - /// Filled information block on success, null on error - public static InformationBlock? ParseInformationBlock(Stream data) - { - var informationBlock = data.ReadType(); - - if (informationBlock?.Signature != LESignatureString && informationBlock?.Signature != LXSignatureString) - return null; - - return informationBlock; - } - - /// - /// Parse a Stream into an object table entry - /// - /// Stream to parse - /// Filled object table entry on success, null on error - public static ObjectTableEntry? ParseObjectTableEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into an object page map entry - /// - /// Stream to parse - /// Filled object page map entry on success, null on error - public static ObjectPageMapEntry? ParseObjectPageMapEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a resource table entry - /// - /// Stream to parse - /// Filled resource table entry on success, null on error - public static ResourceTableEntry? ParseResourceTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a resident names table entry /// @@ -545,36 +500,6 @@ namespace SabreTools.Serialization.Deserializers return bundle; } - /// - /// Parse a Stream into a module format directives table entry - /// - /// Stream to parse - /// Filled module format directives table entry on success, null on error - public static ModuleFormatDirectivesTableEntry? ParseModuleFormatDirectivesTableEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a verify record directive table entry - /// - /// Stream to parse - /// Filled verify record directive table entry on success, null on error - public static VerifyRecordDirectiveTableEntry? ParseVerifyRecordDirectiveTableEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a fix-up page table entry - /// - /// Stream to parse - /// Filled fix-up page table entry on success, null on error - public static FixupPageTableEntry? ParseFixupPageTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a fix-up record table entry /// @@ -831,16 +756,6 @@ namespace SabreTools.Serialization.Deserializers return entry; } - /// - /// Parse a Stream into a per-page checksum table entry - /// - /// Stream to parse - /// Filled per-page checksum table entry on success, null on error - public static PerPageChecksumTableEntry? ParsePerPageChecksumTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a non-resident names table entry /// diff --git a/SabreTools.Serialization/Deserializers/MSDOS.cs b/SabreTools.Serialization/Deserializers/MSDOS.cs index b0292bf4..fa342fbb 100644 --- a/SabreTools.Serialization/Deserializers/MSDOS.cs +++ b/SabreTools.Serialization/Deserializers/MSDOS.cs @@ -128,7 +128,7 @@ namespace SabreTools.Serialization.Deserializers for (int i = 0; i < count; i++) { - var entry = ParseRelocationEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -137,15 +137,5 @@ namespace SabreTools.Serialization.Deserializers return relocationTable; } - - /// - /// Parse a Stream into a relocation table entry - /// - /// Stream to parse - /// Filled relocation table entry on success, null on error - public static RelocationEntry? ParseRelocationEntry(Stream data) - { - return data.ReadType(); - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/MoPaQ.cs b/SabreTools.Serialization/Deserializers/MoPaQ.cs index 39fe9ca5..30bc3bec 100644 --- a/SabreTools.Serialization/Deserializers/MoPaQ.cs +++ b/SabreTools.Serialization/Deserializers/MoPaQ.cs @@ -35,8 +35,8 @@ namespace SabreTools.Serialization.Deserializers long basePtr = data.Position; // Deserialize the user data, returning null if invalid - var userData = ParseUserData(data); - if (userData == null) + var userData = data.ReadType(); + if (userData?.Signature != UserDataSignatureString) return null; // Set the user data @@ -92,7 +92,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < hashTableEnd) { - var hashEntry = ParseHashEntry(data); + var hashEntry = data.ReadType(); if (hashEntry == null) return null; @@ -121,7 +121,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < hashTableEnd) { - var hashEntry = ParseHashEntry(data); + var hashEntry = data.ReadType(); if (hashEntry == null) return null; @@ -150,7 +150,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < hashTableEnd) { - var hashEntry = ParseHashEntry(data); + var hashEntry = data.ReadType(); if (hashEntry == null) return null; @@ -183,7 +183,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < blockTableEnd) { - var blockEntry = ParseBlockEntry(data); + var blockEntry = data.ReadType(); if (blockEntry == null) return null; @@ -212,7 +212,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < blockTableEnd) { - var blockEntry = ParseBlockEntry(data); + var blockEntry = data.ReadType(); if (blockEntry == null) return null; @@ -241,7 +241,7 @@ namespace SabreTools.Serialization.Deserializers while (data.Position < blockTableEnd) { - var blockEntry = ParseBlockEntry(data); + var blockEntry = data.ReadType(); if (blockEntry == null) return null; @@ -391,21 +391,6 @@ namespace SabreTools.Serialization.Deserializers return archiveHeader; } - /// - /// Parse a Stream into a user data object - /// - /// Stream to parse - /// Filled user data on success, null on error - private static UserData? ParseUserData(Stream data) - { - var userData = data.ReadType(); - - if (userData?.Signature != UserDataSignatureString) - return null; - - return userData; - } - /// /// Parse a Stream into a HET table /// @@ -492,36 +477,6 @@ namespace SabreTools.Serialization.Deserializers return betTable; } - /// - /// Parse a Stream into a hash entry - /// - /// Stream to parse - /// Filled hash entry on success, null on error - private static HashEntry? ParseHashEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a block entry - /// - /// Stream to parse - /// Filled block entry on success, null on error - private static BlockEntry? ParseBlockEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a patch info - /// - /// Stream to parse - /// Filled patch info on success, null on error - private static PatchInfo? ParsePatchInfo(Stream data) - { - return data.ReadType(); - } - #region Helpers /// diff --git a/SabreTools.Serialization/Deserializers/N3DS.cs b/SabreTools.Serialization/Deserializers/N3DS.cs index 09c66b3d..39fa141f 100644 --- a/SabreTools.Serialization/Deserializers/N3DS.cs +++ b/SabreTools.Serialization/Deserializers/N3DS.cs @@ -50,7 +50,7 @@ namespace SabreTools.Serialization.Deserializers #region Development Card Info Header // Try to parse the development card info header - var developmentCardInfoHeader = ParseDevelopmentCardInfoHeader(data); + var developmentCardInfoHeader = data.ReadType(); if (developmentCardInfoHeader == null) return null; @@ -95,7 +95,7 @@ namespace SabreTools.Serialization.Deserializers // Handle the extended header, if it exists if (partition.ExtendedHeaderSizeInBytes > 0) { - var extendedHeader = ParseNCCHExtendedHeader(data); + var extendedHeader = data.ReadType(); if (extendedHeader != null) cart.ExtendedHeaders[i] = extendedHeader; } @@ -119,10 +119,10 @@ namespace SabreTools.Serialization.Deserializers long offset = partition.RomFSOffsetInMediaUnits * mediaUnitSize; data.Seek(partitionOffset + offset, SeekOrigin.Begin); - var romFsHeader = ParseRomFSHeader(data); - if (romFsHeader == null) + var romFsHeader = data.ReadType(); + if (romFsHeader?.MagicString != RomFSMagicNumber) continue; - else if (romFsHeader.MagicString != RomFSMagicNumber || romFsHeader.MagicNumber != RomFSSecondMagicNumber) + if (romFsHeader?.MagicNumber != RomFSSecondMagicNumber) continue; cart.RomFSHeaders[i] = romFsHeader; @@ -157,7 +157,7 @@ namespace SabreTools.Serialization.Deserializers header.PartitionsTable = new PartitionTableEntry[8]; for (int i = 0; i < 8; i++) { - var partitionTableEntry = ParsePartitionTableEntry(data); + var partitionTableEntry = data.ReadType(); if (partitionTableEntry == null) return null; @@ -191,16 +191,6 @@ namespace SabreTools.Serialization.Deserializers return header; } - /// - /// Parse a Stream into a partition table entry - /// - /// Stream to parse - /// Filled partition table entry on success, null on error - public static PartitionTableEntry? ParsePartitionTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a card info header /// @@ -245,16 +235,6 @@ namespace SabreTools.Serialization.Deserializers return id; } - /// - /// Parse a Stream into a development card info header - /// - /// Stream to parse - /// Filled development card info header on success, null on error - public static DevelopmentCardInfoHeader? ParseDevelopmentCardInfoHeader(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into an NCCH header /// @@ -283,7 +263,7 @@ namespace SabreTools.Serialization.Deserializers header.ExtendedHeaderHash = data.ReadBytes(0x20); header.ExtendedHeaderSizeInBytes = data.ReadUInt32(); header.Reserved2 = data.ReadUInt32(); - header.Flags = ParseNCCHHeaderFlags(data); + header.Flags = data.ReadType(); header.PlainRegionOffsetInMediaUnits = data.ReadUInt32(); header.PlainRegionSizeInMediaUnits = data.ReadUInt32(); header.LogoRegionOffsetInMediaUnits = data.ReadUInt32(); @@ -302,26 +282,6 @@ namespace SabreTools.Serialization.Deserializers return header; } - /// - /// Parse a Stream into an NCCH header flags - /// - /// Stream to parse - /// Filled NCCH header flags on success, null on error - public static NCCHHeaderFlags? ParseNCCHHeaderFlags(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into an NCCH extended header - /// - /// Stream to parse - /// Filled NCCH extended header on success, null on error - public static NCCHExtendedHeader? ParseNCCHExtendedHeader(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into an ExeFS header /// @@ -366,24 +326,5 @@ namespace SabreTools.Serialization.Deserializers return exeFSFileHeader; } - - /// - /// Parse a Stream into an RomFS header - /// - /// Stream to parse - /// Filled RomFS header on success, null on error - public static RomFSHeader? ParseRomFSHeader(Stream data) - { - var romFSHeader = data.ReadType(); - - if (romFSHeader == null) - return null; - if (romFSHeader.MagicString != RomFSMagicNumber) - return null; - if (romFSHeader.MagicNumber != RomFSSecondMagicNumber) - return null; - - return romFSHeader; - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/NCF.cs b/SabreTools.Serialization/Deserializers/NCF.cs index e570af2e..d92294f8 100644 --- a/SabreTools.Serialization/Deserializers/NCF.cs +++ b/SabreTools.Serialization/Deserializers/NCF.cs @@ -19,17 +19,18 @@ namespace SabreTools.Serialization.Deserializers if (data.Position < 0 || data.Position >= data.Length) return null; - // Cache the current offset - long initialOffset = data.Position; - // Create a new Half-Life No Cache to fill var file = new Models.NCF.File(); #region Header // Try to parse the header - var header = ParseHeader(data); - if (header == null) + var header = data.ReadType
(); + if (header?.Dummy0 != 0x00000001) + return null; + if (header?.MajorVersion != 0x00000002) + return null; + if (header?.MinorVersion != 1) return null; // Set the no cache header @@ -38,13 +39,13 @@ namespace SabreTools.Serialization.Deserializers #endregion // Cache the current offset - initialOffset = data.Position; + long initialOffset = data.Position; #region Directory Header // Try to parse the directory header - var directoryHeader = ParseDirectoryHeader(data); - if (directoryHeader == null) + var directoryHeader = data.ReadType(); + if (directoryHeader?.Dummy0 != 0x00000004) return null; // Set the game cache directory header @@ -60,7 +61,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var directoryEntry = ParseDirectoryEntry(data); + var directoryEntry = data.ReadType(); if (directoryEntry == null) return null; @@ -108,7 +109,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory info 1 entries for (int i = 0; i < directoryHeader.Info1Count; i++) { - var directoryInfo1Entry = ParseDirectoryInfo1Entry(data); + var directoryInfo1Entry = data.ReadType(); if (directoryInfo1Entry == null) return null; @@ -125,7 +126,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory info 2 entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var directoryInfo2Entry = ParseDirectoryInfo2Entry(data); + var directoryInfo2Entry = data.ReadType(); if (directoryInfo2Entry == null) return null; @@ -142,7 +143,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory copy entries for (int i = 0; i < directoryHeader.CopyCount; i++) { - var directoryCopyEntry = ParseDirectoryCopyEntry(data); + var directoryCopyEntry = data.ReadType(); if (directoryCopyEntry == null) return null; @@ -159,7 +160,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory local entries for (int i = 0; i < directoryHeader.LocalCount; i++) { - var directoryLocalEntry = ParseDirectoryLocalEntry(data); + var directoryLocalEntry = data.ReadType(); if (directoryLocalEntry == null) return null; @@ -174,8 +175,10 @@ namespace SabreTools.Serialization.Deserializers #region Unknown Header // Try to parse the unknown header - var unknownHeader = ParseUnknownHeader(data); - if (unknownHeader == null) + var unknownHeader = data.ReadType(); + if (unknownHeader?.Dummy0 != 0x00000001) + return null; + if (unknownHeader?.Dummy1 != 0x00000000) return null; // Set the game cache unknown header @@ -191,7 +194,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the unknown entries for (int i = 0; i < directoryHeader.ItemCount; i++) { - var unknownEntry = ParseUnknownEntry(data); + var unknownEntry = data.ReadType(); if (unknownEntry == null) return null; @@ -203,8 +206,8 @@ namespace SabreTools.Serialization.Deserializers #region Checksum Header // Try to parse the checksum header - var checksumHeader = ParseChecksumHeader(data); - if (checksumHeader == null) + var checksumHeader = data.ReadType(); + if (checksumHeader?.Dummy0 != 0x00000001) return null; // Set the game cache checksum header @@ -218,8 +221,10 @@ namespace SabreTools.Serialization.Deserializers #region Checksum Map Header // Try to parse the checksum map header - var checksumMapHeader = ParseChecksumMapHeader(data); - if (checksumMapHeader == null) + var checksumMapHeader = data.ReadType(); + if (checksumMapHeader?.Dummy0 != 0x14893721) + return null; + if (checksumMapHeader?.Dummy1 != 0x00000001) return null; // Set the game cache checksum map header @@ -235,7 +240,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the checksum map entries for (int i = 0; i < checksumMapHeader.ItemCount; i++) { - var checksumMapEntry = ParseChecksumMapEntry(data); + var checksumMapEntry = data.ReadType(); if (checksumMapEntry == null) return null; @@ -252,7 +257,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the checksum entries for (int i = 0; i < checksumMapHeader.ChecksumCount; i++) { - var checksumEntry = ParseChecksumEntry(data); + var checksumEntry = data.ReadType(); if (checksumEntry == null) return null; @@ -266,178 +271,5 @@ namespace SabreTools.Serialization.Deserializers return file; } - - /// - /// Parse a Stream into a Half-Life No Cache header - /// - /// Stream to parse - /// Filled Half-Life No Cache header on success, null on error - private static Header? ParseHeader(Stream data) - { - var header = data.ReadType
(); - - if (header == null) - return null; - if (header.Dummy0 != 0x00000001) - return null; - if (header.MajorVersion != 0x00000002) - return null; - if (header.MinorVersion != 1) - return null; - - return header; - } - - /// - /// Parse a Stream into a Half-Life No Cache directory header - /// - /// Stream to parse - /// Filled Half-Life No Cache directory header on success, null on error - private static DirectoryHeader? ParseDirectoryHeader(Stream data) - { - var directoryHeader = data.ReadType(); - - if (directoryHeader == null) - return null; - if (directoryHeader.Dummy0 != 0x00000004) - return null; - - return directoryHeader; - } - - /// - /// Parse a Stream into a Half-Life No Cache directory entry - /// - /// Stream to parse - /// Filled Half-Life No Cache directory entry on success, null on error - private static DirectoryEntry? ParseDirectoryEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache directory info 1 entry - /// - /// Stream to parse - /// Filled Half-Life No Cache directory info 1 entry on success, null on error - private static DirectoryInfo1Entry? ParseDirectoryInfo1Entry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache directory info 2 entry - /// - /// Stream to parse - /// Filled Half-Life No Cache directory info 2 entry on success, null on error - private static DirectoryInfo2Entry? ParseDirectoryInfo2Entry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache directory copy entry - /// - /// Stream to parse - /// Filled Half-Life No Cache directory copy entry on success, null on error - private static DirectoryCopyEntry? ParseDirectoryCopyEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache directory local entry - /// - /// Stream to parse - /// Filled Half-Life No Cache directory local entry on success, null on error - private static DirectoryLocalEntry? ParseDirectoryLocalEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache unknown header - /// - /// Stream to parse - /// Filled Half-Life No Cache unknown header on success, null on error - private static UnknownHeader? ParseUnknownHeader(Stream data) - { - var unknownHeader = data.ReadType(); - - if (unknownHeader == null) - return null; - if (unknownHeader.Dummy0 != 0x00000001) - return null; - if (unknownHeader.Dummy1 != 0x00000000) - return null; - - return unknownHeader; - } - - /// - /// Parse a Stream into a Half-Life No Cache unknown entry - /// - /// Stream to parse - /// Filled Half-Life No Cacheunknown entry on success, null on error - private static UnknownEntry? ParseUnknownEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache checksum header - /// - /// Stream to parse - /// Filled Half-Life No Cache checksum header on success, null on error - private static ChecksumHeader? ParseChecksumHeader(Stream data) - { - var checksumHeader = data.ReadType(); - - if (checksumHeader == null) - return null; - if (checksumHeader.Dummy0 != 0x00000001) - return null; - - return checksumHeader; - } - - /// - /// Parse a Stream into a Half-Life No Cache checksum map header - /// - /// Stream to parse - /// Filled Half-Life No Cache checksum map header on success, null on error - private static ChecksumMapHeader? ParseChecksumMapHeader(Stream data) - { - var checksumMapHeader = data.ReadType(); - - if (checksumMapHeader == null) - return null; - if (checksumMapHeader.Dummy0 != 0x14893721) - return null; - if (checksumMapHeader.Dummy1 != 0x00000001) - return null; - - return checksumMapHeader; - } - - /// - /// Parse a Stream into a Half-Life No Cache checksum map entry - /// - /// Stream to parse - /// Filled Half-Life No Cache checksum map entry on success, null on error - private static ChecksumMapEntry? ParseChecksumMapEntry(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into a Half-Life No Cache checksum entry - /// - /// Stream to parse - /// Filled Half-Life No Cache checksum entry on success, null on error - private static ChecksumEntry? ParseChecksumEntry(Stream data) - { - return data.ReadType(); - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/NewExecutable.cs b/SabreTools.Serialization/Deserializers/NewExecutable.cs index 54faf5ac..db3b683d 100644 --- a/SabreTools.Serialization/Deserializers/NewExecutable.cs +++ b/SabreTools.Serialization/Deserializers/NewExecutable.cs @@ -41,12 +41,12 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the executable header data.Seek(initialOffset + stub.Header.NewExeHeaderAddr, SeekOrigin.Begin); - var executableHeader = ParseExecutableHeader(data); - if (executableHeader == null) + var header = data.ReadType(); + if (header?.Magic != SignatureString) return null; // Set the executable header - executable.Header = executableHeader; + executable.Header = header; #endregion @@ -55,13 +55,13 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the segment table doesn't exist int tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.SegmentTableOffset; + + header.SegmentTableOffset; if (tableAddress >= data.Length) return executable; // Try to parse the segment table data.Seek(tableAddress, SeekOrigin.Begin); - var segmentTable = ParseSegmentTable(data, executableHeader.FileSegmentCount); + var segmentTable = ParseSegmentTable(data, header.FileSegmentCount); if (segmentTable == null) return null; @@ -75,13 +75,13 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the segment table doesn't exist tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.ResourceTableOffset; + + header.ResourceTableOffset; if (tableAddress >= data.Length) return executable; // Try to parse the resource table data.Seek(tableAddress, SeekOrigin.Begin); - var resourceTable = ParseResourceTable(data, executableHeader.ResourceEntriesCount); + var resourceTable = ParseResourceTable(data, header.ResourceEntriesCount); if (resourceTable == null) return null; @@ -95,10 +95,10 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the resident-name table doesn't exist tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.ResidentNameTableOffset; + + header.ResidentNameTableOffset; int endOffset = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.ModuleReferenceTableOffset; + + header.ModuleReferenceTableOffset; if (tableAddress >= data.Length) return executable; @@ -118,13 +118,13 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the module-reference table doesn't exist tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.ModuleReferenceTableOffset; + + header.ModuleReferenceTableOffset; if (tableAddress >= data.Length) return executable; // Try to parse the module-reference table data.Seek(tableAddress, SeekOrigin.Begin); - var moduleReferenceTable = ParseModuleReferenceTable(data, executableHeader.ModuleReferenceTableSize); + var moduleReferenceTable = ParseModuleReferenceTable(data, header.ModuleReferenceTableSize); if (moduleReferenceTable == null) return null; @@ -138,10 +138,10 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the imported-name table doesn't exist tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.ImportedNamesTableOffset; + + header.ImportedNamesTableOffset; endOffset = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.EntryTableOffset; + + header.EntryTableOffset; if (tableAddress >= data.Length) return executable; @@ -161,11 +161,11 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the imported-name table doesn't exist tableAddress = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.EntryTableOffset; + + header.EntryTableOffset; endOffset = initialOffset + (int)stub.Header.NewExeHeaderAddr - + executableHeader.EntryTableOffset - + executableHeader.EntryTableSize; + + header.EntryTableOffset + + header.EntryTableSize; if (tableAddress >= data.Length) return executable; @@ -184,10 +184,10 @@ namespace SabreTools.Serialization.Deserializers // If the offset for the nonresident-name table doesn't exist tableAddress = initialOffset - + (int)executableHeader.NonResidentNamesTableOffset; + + (int)header.NonResidentNamesTableOffset; endOffset = initialOffset - + (int)executableHeader.NonResidentNamesTableOffset - + executableHeader.NonResidentNameTableSize; + + (int)header.NonResidentNamesTableOffset + + header.NonResidentNameTableSize; if (tableAddress >= data.Length) return executable; @@ -205,21 +205,6 @@ namespace SabreTools.Serialization.Deserializers return executable; } - /// - /// Parse a Stream into a New Executable header - /// - /// Stream to parse - /// Filled executable header on success, null on error - public static ExecutableHeader? ParseExecutableHeader(Stream data) - { - var header = data.ReadType(); - - if (header?.Magic != SignatureString) - return null; - - return header; - } - /// /// Parse a Stream into a segment table /// @@ -232,7 +217,7 @@ namespace SabreTools.Serialization.Deserializers for (int i = 0; i < count; i++) { - var entry = ParseSegmentTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -242,16 +227,6 @@ namespace SabreTools.Serialization.Deserializers return segmentTable; } - /// - /// Parse a Stream into a segment table entry - /// - /// Stream to parse - /// Filled segment table entry on success, null on error - public static SegmentTableEntry? ParseSegmentTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a resource table /// @@ -286,7 +261,7 @@ namespace SabreTools.Serialization.Deserializers for (int j = 0; j < entry.ResourceCount; j++) { // TODO: Should we read and store the resource data? - var resource = ParseResourceTypeResourceEntry(data); + var resource = data.ReadType(); if (resource == null) return null; @@ -344,17 +319,6 @@ namespace SabreTools.Serialization.Deserializers return resourceTable; } - /// - /// Parse a Stream into a resource entry - /// - /// Stream to parse - /// Filled resource entry on success, null on error - public static ResourceTypeResourceEntry? ParseResourceTypeResourceEntry(Stream data) - { - // TODO: Should we read and store the resource data? - return data.ReadType(); - } - /// /// Parse a Stream into a resource type and name string /// @@ -420,7 +384,7 @@ namespace SabreTools.Serialization.Deserializers for (int i = 0; i < count; i++) { - var entry = ParseModuleReferenceTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -430,16 +394,6 @@ namespace SabreTools.Serialization.Deserializers return moduleReferenceTable; } - /// - /// Parse a Stream into a module-reference table entry - /// - /// Stream to parse - /// Filled module-reference table entry on success, null on error - public static ModuleReferenceTableEntry? ParseModuleReferenceTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into an imported-name table /// diff --git a/SabreTools.Serialization/Deserializers/Nitro.cs b/SabreTools.Serialization/Deserializers/Nitro.cs index 1b5c18d2..e1b34a31 100644 --- a/SabreTools.Serialization/Deserializers/Nitro.cs +++ b/SabreTools.Serialization/Deserializers/Nitro.cs @@ -25,7 +25,7 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseCommonHeader(data); + var header = data.ReadType(); if (header == null) return null; @@ -39,7 +39,7 @@ namespace SabreTools.Serialization.Deserializers // If we have a DSi-compatible cartridge if (header.UnitCode == Unitcode.NDSPlusDSi || header.UnitCode == Unitcode.DSi) { - var extendedDSiHeader = ParseExtendedDSiHeader(data); + var extendedDSiHeader = data.ReadType(); if (extendedDSiHeader == null) return null; @@ -99,7 +99,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the file allocation table while (data.Position - fileAllocationTableOffset < header.FileAllocationTableLength) { - var entry = ParseFileAllocationTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -117,26 +117,6 @@ namespace SabreTools.Serialization.Deserializers return cart; } - /// - /// Parse a Stream into a common header - /// - /// Stream to parse - /// Filled common header on success, null on error - private static CommonHeader? ParseCommonHeader(Stream data) - { - return data.ReadType(); - } - - /// - /// Parse a Stream into an extended DSi header - /// - /// Stream to parse - /// Filled extended DSi header on success, null on error - private static ExtendedDSiHeader? ParseExtendedDSiHeader(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a name table /// @@ -151,7 +131,7 @@ namespace SabreTools.Serialization.Deserializers int entryCount = int.MaxValue; while (entryCount > 0) { - var entry = ParseFolderAllocationTableEntry(data); + var entry = data.ReadType(); if (entry == null) return null; @@ -185,16 +165,6 @@ namespace SabreTools.Serialization.Deserializers return nameTable; } - /// - /// Parse a Stream into a folder allocation table entry - /// - /// Stream to parse - /// Filled folder allocation table entry on success, null on error - private static FolderAllocationTableEntry? ParseFolderAllocationTableEntry(Stream data) - { - return data.ReadType(); - } - /// /// Parse a Stream into a name list entry /// @@ -222,15 +192,5 @@ namespace SabreTools.Serialization.Deserializers return entry; } - - /// - /// Parse a Stream into a name list entry - /// - /// Stream to parse - /// Filled name list entry on success, null on error - private static FileAllocationTableEntry? ParseFileAllocationTableEntry(Stream data) - { - return data.ReadType(); - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/PAK.cs b/SabreTools.Serialization/Deserializers/PAK.cs index 6acb4570..776149af 100644 --- a/SabreTools.Serialization/Deserializers/PAK.cs +++ b/SabreTools.Serialization/Deserializers/PAK.cs @@ -24,8 +24,8 @@ namespace SabreTools.Serialization.Deserializers #region Header // Try to parse the header - var header = ParseHeader(data); - if (header == null) + var header = data.ReadType
(); + if (header?.Signature != SignatureString) return null; // Set the package header @@ -49,7 +49,7 @@ namespace SabreTools.Serialization.Deserializers // Try to parse the directory items for (int i = 0; i < file.DirectoryItems.Length; i++) { - var directoryItem = ParseDirectoryItem(data); + var directoryItem = data.ReadType(); if (directoryItem == null) return null; @@ -60,30 +60,5 @@ namespace SabreTools.Serialization.Deserializers return file; } - - /// - /// Parse a Stream into a Half-Life Package header - /// - /// Stream to parse - /// Filled Half-Life Package header on success, null on error - private static Header? ParseHeader(Stream data) - { - var header = data.ReadType
(); - - if (header?.Signature != SignatureString) - return null; - - return header; - } - - /// - /// Parse a Stream into a Half-Life Package directory item - /// - /// Stream to parse - /// Filled Half-Life Package directory item on success, null on error - private static DirectoryItem? ParseDirectoryItem(Stream data) - { - return data.ReadType(); - } } } \ No newline at end of file diff --git a/SabreTools.Serialization/Deserializers/PFF.cs b/SabreTools.Serialization/Deserializers/PFF.cs index 64dfaf89..01dccd92 100644 --- a/SabreTools.Serialization/Deserializers/PFF.cs +++ b/SabreTools.Serialization/Deserializers/PFF.cs @@ -70,7 +70,7 @@ namespace SabreTools.Serialization.Deserializers data.Seek(offset, SeekOrigin.Begin); // Try to parse the footer - var footer = ParseFooter(data); + var footer = data.ReadType