From 742b25e4dddca6c5e1d466afe37cc17ea0afc41e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 24 Aug 2021 15:28:23 -0700 Subject: [PATCH] Split manifest reading into helper methods --- BurnOutSharp/Utilities.cs | 91 ++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/BurnOutSharp/Utilities.cs b/BurnOutSharp/Utilities.cs index a7f61cd5..0f658146 100644 --- a/BurnOutSharp/Utilities.cs +++ b/BurnOutSharp/Utilities.cs @@ -301,41 +301,18 @@ namespace BurnOutSharp /// /// Byte array representing the file contents /// Version string, null on error - /// TODO: How do we find the manifest specifically better? public static string GetManifestVersion(byte[] fileContent) { - // - byte?[] manifestEnd = new byte?[] { 0x3C, 0x2F, 0x61, 0x73, 0x73, 0x65, 0x6D, 0x62, 0x6C, 0x79, 0x3E }; - if (!fileContent.FirstPosition(manifestEnd, out int manifestEndPosition, start: manifestStartPosition)) - return null; - // Read in the manifest to a string - int manifestLength = manifestEndPosition + "".Length - manifestStartPosition; - string manifestString = Encoding.ASCII.GetString(fileContent, manifestStartPosition, manifestLength); + string manifestString = GetEmbeddedAssemblyManifest(fileContent); + if (string.IsNullOrWhiteSpace(manifestString)) + return null; // Try to read the XML in from the string try { - // Load the XML string as a document - var manifestDoc = new XmlDocument(); - manifestDoc.LoadXml(manifestString); - - // If the XML has no children, it's invalid - if (!manifestDoc.HasChildNodes) - return null; - - // Try to read the assembly node - var assemblyNode = manifestDoc["assembly"]; - if (assemblyNode == null) - return null; - // Try to read the assemblyIdentity - var assemblyIdentityNode = assemblyNode["assemblyIdentity"]; + var assemblyIdentityNode = GetAssemblyIdentityNode(manifestString); if (assemblyIdentityNode == null) return null; @@ -353,8 +330,41 @@ namespace BurnOutSharp /// /// Byte array representing the file contents /// Version string, null on error - /// TODO: How do we find the manifest specifically better? public static string GetManifestDescription(byte[] fileContent) + { + // Read in the manifest to a string + string manifestString = GetEmbeddedAssemblyManifest(fileContent); + if (string.IsNullOrWhiteSpace(manifestString)) + return null; + + // Try to read the XML in from the string + try + { + // Try to read the assemblyIdentity + var assemblyIdentityNode = GetAssemblyIdentityNode(manifestString); + if (assemblyIdentityNode == null) + return null; + + // Return the content of the description node, if possible + var descriptionNode = assemblyIdentityNode["description"]; + if (descriptionNode == null) + return null; + + return descriptionNode.InnerXml; + } + catch + { + return null; + } + } + + /// + /// Get the embedded assembly manifest + /// + /// Byte array representing the file contents + /// Embedded assembly manifest as a string, if possible + /// TODO: How do we find the manifest specifically better? + private static string GetEmbeddedAssemblyManifest(byte[] fileContent) { // ".Length - manifestStartPosition; - string manifestString = Encoding.ASCII.GetString(fileContent, manifestStartPosition, manifestLength); + return Encoding.ASCII.GetString(fileContent, manifestStartPosition, manifestLength); + } + + /// + /// Get the assembly identity node from an embedded manifest + /// + /// String representing the XML document + /// Assembly identity node, if possible + private static XmlElement GetAssemblyIdentityNode(string manifestString) + { + // An invalid string means we can't read it + if (string.IsNullOrWhiteSpace(manifestString)) + return null; - // Try to read the XML in from the string try { // Load the XML string as a document @@ -387,15 +408,7 @@ namespace BurnOutSharp return null; // Try to read the assemblyIdentity - var assemblyIdentityNode = assemblyNode["assemblyIdentity"]; - if (assemblyIdentityNode == null) - return null; - - // Return the content of the description node, if possible - var DescriptionNode = assemblyNode["description"]; - if (DescriptionNode == null) - return null; - return DescriptionNode.InnerXml; + return assemblyNode["assemblyIdentity"]; } catch {