Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions src/DwgIO/Internal/Parsing/AppInfoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ namespace DwgIO.Internal.Parsing;
/// </summary>
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();

Expand Down Expand Up @@ -52,9 +61,11 @@ private static DwgAppInfo ParseAnsi(byte[] data, int codepage)

/// <summary>
/// UTF-16 layout written by AC1021+ (R2007+) files:
/// <c>u32 marker, [u16] name, u32, 16B checksum, [u16] comment, 16B checksum,
/// [u16] product XML, trailing reserve</c>. The XML blob's build_version
/// attribute doubles as the application version.
/// <c>u32 marker, [u16] name, u32 entry count</c>, then <c>count</c> entries of
/// <c>16B identifier, [u16] string</c>. 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.
/// </summary>
private static DwgAppInfo? TryParseUnicode(byte[] data, int codepage)
{
Expand All @@ -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
{
Expand Down
105 changes: 101 additions & 4 deletions tests/DwgIO.Tests/MetadataParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers.Binary;
using DwgIO.Internal.Parsing;
using DwgIO.Model;
using Xunit;
Expand All @@ -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<byte> list, string text)
{
// AC1021+ layout: u16 char count + count×2 bytes, where the counted content
Expand All @@ -30,17 +35,109 @@ private static void WriteAnsiString32(List<byte> list, string text)
list.AddRange(bytes);
}

private static void WriteU32(List<byte> data, uint value)
{
Span<byte> buffer = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(buffer, value);
data.AddRange(buffer.ToArray());
}

private static void WriteAppInfoEntry(List<byte> 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<byte>();
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, "<ProductInformation build_version=\"Q.163.0.0(x64)\"/>"),
(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, "<ProductInformation build_version=\"D.40.0.200\"/>"),
(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()
{
var data = new List<byte>();
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, "<ProductInformation name=\"Writer\" build_version=\"1.2.3.4\"/>");
WriteAppInfoEntry(data, CommentEntryId, "Autodesk DWG. Trusted DWG notice.");
WriteAppInfoEntry(
data,
ProductXmlEntryId,
"<ProductInformation name=\"Writer\" build_version=\"1.2.3.4\"/>");
data.AddRange(new byte[8]); // trailing reserve

var appInfo = AppInfoParser.Parse([.. data], 1251);
Expand Down
Loading