From 9371665c850f0b5c039b60c97256e0173a7a93ae Mon Sep 17 00:00:00 2001 From: mahach666 Date: Mon, 16 Nov 2026 05:21:45 +0300 Subject: [PATCH] Read AppInfo entries by identifier instead of fixed positions The UTF-16 AppInfo layout stores an entry count followed by 16-byte identifier and string pairs. The parser assumed exactly one comment and one product XML entry, so one-entry sections ran past the end and Version stayed empty when the version entry was present. Loop over the declared count, map version, notice and product XML by their stable identifiers, skip unknown entries, and fall back to the XML build_version when no version entry exists. --- src/DwgIO/Internal/Parsing/AppInfoParser.cs | 55 +++++++--- tests/DwgIO.Tests/MetadataParserTests.cs | 105 +++++++++++++++++++- 2 files changed, 143 insertions(+), 17 deletions(-) diff --git a/src/DwgIO/Internal/Parsing/AppInfoParser.cs b/src/DwgIO/Internal/Parsing/AppInfoParser.cs index 90fb190..3af9783 100644 --- a/src/DwgIO/Internal/Parsing/AppInfoParser.cs +++ b/src/DwgIO/Internal/Parsing/AppInfoParser.cs @@ -12,6 +12,15 @@ namespace DwgIO.Internal.Parsing; /// internal static partial class AppInfoParser { + private static readonly byte[] VersionEntryId = + [0x1B, 0xD8, 0x48, 0xF3, 0xCC, 0x0A, 0x3E, 0x4D, 0xBA, 0xB1, 0xCF, 0x81, 0xF7, 0xB4, 0x50, 0xB3]; + + private static readonly byte[] CommentEntryId = + [0xB8, 0xD0, 0xF0, 0x25, 0xA1, 0xD7, 0x93, 0x49, 0xB2, 0xFA, 0x9B, 0xF9, 0x28, 0x6F, 0xA1, 0xFD]; + + private static readonly byte[] ProductXmlEntryId = + [0xE8, 0xE0, 0x96, 0x51, 0xC5, 0xCE, 0xB2, 0x44, 0xA8, 0xBF, 0xF6, 0xE8, 0x3B, 0x85, 0x9D, 0x44]; + [GeneratedRegex("build_version=\\\\?\"([^\"\\\\]+)")] private static partial Regex BuildVersionRegex(); @@ -52,9 +61,11 @@ private static DwgAppInfo ParseAnsi(byte[] data, int codepage) /// /// UTF-16 layout written by AC1021+ (R2007+) files: - /// u32 marker, [u16] name, u32, 16B checksum, [u16] comment, 16B checksum, - /// [u16] product XML, trailing reserve. The XML blob's build_version - /// attribute doubles as the application version. + /// u32 marker, [u16] name, u32 entry count, then count entries of + /// 16B identifier, [u16] string. Identifiers are stable across files, but + /// entry order and count vary; unknown entries are skipped. The version entry + /// holds the application version, and the product XML's build_version + /// attribute is the fallback when the version entry is absent. /// private static DwgAppInfo? TryParseUnicode(byte[] data, int codepage) { @@ -63,16 +74,34 @@ private static DwgAppInfo ParseAnsi(byte[] data, int codepage) var cursor = new MetadataByteCursor(data, codepage); cursor.ReadU32(); // variant marker var name = cursor.ReadUnicodeStringWithNul(); - cursor.ReadU32(); - cursor.Skip(16); // data checksum - var comment = cursor.ReadUnicodeStringWithNul(); - cursor.Skip(16); // data checksum - var productXml = cursor.ReadUnicodeStringWithNul(); - var version = string.IsNullOrEmpty(productXml) - ? string.Empty - : BuildVersionRegex().Match(productXml) is { Success: true } match - ? match.Groups[1].Value - : string.Empty; + uint entryCount = cursor.ReadU32(); + var version = string.Empty; + var comment = string.Empty; + var productXml = string.Empty; + + for (uint i = 0; i < entryCount; i++) + { + var id = cursor.ReadBytes(16); + var text = cursor.ReadUnicodeStringWithNul(); + if (id.AsSpan().SequenceEqual(VersionEntryId)) + { + version = text; + } + else if (id.AsSpan().SequenceEqual(CommentEntryId)) + { + comment = text; + } + else if (id.AsSpan().SequenceEqual(ProductXmlEntryId)) + { + productXml = text; + } + } + + if (version.Length == 0 && productXml.Length > 0 + && BuildVersionRegex().Match(productXml) is { Success: true } match) + { + version = match.Groups[1].Value; + } return new DwgAppInfo { diff --git a/tests/DwgIO.Tests/MetadataParserTests.cs b/tests/DwgIO.Tests/MetadataParserTests.cs index dba56ec..4facbda 100644 --- a/tests/DwgIO.Tests/MetadataParserTests.cs +++ b/tests/DwgIO.Tests/MetadataParserTests.cs @@ -1,3 +1,4 @@ +using System.Buffers.Binary; using DwgIO.Internal.Parsing; using DwgIO.Model; using Xunit; @@ -6,6 +7,10 @@ namespace DwgIO.Tests; public class MetadataParserTests { + private const string VersionEntryId = "1BD848F3CC0A3E4DBAB1CF81F7B450B3"; + private const string CommentEntryId = "B8D0F025A1D79349B2FA9BF9286FA1FD"; + private const string ProductXmlEntryId = "E8E09651C5CEB244A8BFF6E83B859D44"; + private static void WriteUnicodeString(List list, string text) { // AC1021+ layout: u16 char count + count×2 bytes, where the counted content @@ -30,6 +35,97 @@ private static void WriteAnsiString32(List list, string text) list.AddRange(bytes); } + private static void WriteU32(List data, uint value) + { + Span buffer = stackalloc byte[4]; + BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); + data.AddRange(buffer.ToArray()); + } + + private static void WriteAppInfoEntry(List data, string id, string text) + { + data.AddRange(Convert.FromHexString(id)); + WriteUnicodeString(data, text); + } + + private static byte[] BuildUnicodeAppInfo(string name, params (string Id, string Text)[] entries) + { + var data = new List(); + data.AddRange([0x03, 0x00, 0x00, 0x00]); // variant marker + WriteUnicodeString(data, name); + WriteU32(data, (uint)entries.Length); + foreach (var entry in entries) + { + WriteAppInfoEntry(data, entry.Id, entry.Text); + } + + data.AddRange(new byte[8]); // trailing reserve + return [.. data]; + } + + [Fact] + public void AppInfo_SingleCommentEntry_Parses() + { + var data = BuildUnicodeAppInfo( + "AppInfoDataList", + (CommentEntryId, "Autodesk DWG. Trusted DWG notice.")); + + var appInfo = AppInfoParser.Parse(data, 1251); + + Assert.Equal("AppInfoDataList", appInfo.Name); + Assert.Equal("Autodesk DWG. Trusted DWG notice.", appInfo.Comment); + Assert.Equal(string.Empty, appInfo.Version); + Assert.Equal(string.Empty, appInfo.ProductXml); + } + + [Fact] + public void AppInfo_ThreeEntries_MapsEachByIdentifier() + { + var data = BuildUnicodeAppInfo( + "AppInfoDataList", + (ProductXmlEntryId, ""), + (CommentEntryId, "Autodesk DWG. Trusted DWG notice."), + (VersionEntryId, "23.1.163.0.0")); + + var appInfo = AppInfoParser.Parse(data, 1251); + + Assert.Equal("AppInfoDataList", appInfo.Name); + Assert.Equal("23.1.163.0.0", appInfo.Version); + Assert.Equal("Autodesk DWG. Trusted DWG notice.", appInfo.Comment); + Assert.Contains("Q.163.0.0", appInfo.ProductXml); + } + + [Fact] + public void AppInfo_EntryOrderDoesNotMatter() + { + var data = BuildUnicodeAppInfo( + "AppInfoDataList", + (ProductXmlEntryId, ""), + (VersionEntryId, "18.0.40.0.200")); + + var appInfo = AppInfoParser.Parse(data, 1251); + + // The explicit version entry wins over the XML build_version attribute. + Assert.Equal("18.0.40.0.200", appInfo.Version); + Assert.Contains("D.40.0.200", appInfo.ProductXml); + Assert.Equal(string.Empty, appInfo.Comment); + } + + [Fact] + public void AppInfo_UnknownEntry_IsSkipped() + { + var data = BuildUnicodeAppInfo( + "AppInfoDataList", + ("000102030405060708090A0B0C0D0E0F", "ignored"), + (CommentEntryId, "notice")); + + var appInfo = AppInfoParser.Parse(data, 1251); + + Assert.Equal("notice", appInfo.Comment); + Assert.Equal(string.Empty, appInfo.Version); + Assert.Equal(string.Empty, appInfo.ProductXml); + } + [Fact] public void AppInfo_UnicodeLayout_ParsesVersionFromXml() { @@ -37,10 +133,11 @@ public void AppInfo_UnicodeLayout_ParsesVersionFromXml() data.AddRange([0x02, 0x00, 0x00, 0x00]); // variant marker WriteUnicodeString(data, "AppInfoDataList"); data.AddRange([0x02, 0x00, 0x00, 0x00]); - data.AddRange(new byte[16]); // checksum - WriteUnicodeString(data, "Autodesk DWG. Trusted DWG notice."); - data.AddRange(new byte[16]); // checksum - WriteUnicodeString(data, ""); + WriteAppInfoEntry(data, CommentEntryId, "Autodesk DWG. Trusted DWG notice."); + WriteAppInfoEntry( + data, + ProductXmlEntryId, + ""); data.AddRange(new byte[8]); // trailing reserve var appInfo = AppInfoParser.Parse([.. data], 1251);