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
23 changes: 22 additions & 1 deletion src/DwgIO/DwgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private DwgDocument(string path, byte[] fileBytes, DwgFileHeader fileHeader, R20
{
AppInfo = TryReadMetadata("AcDb:AppInfo", b => AppInfoParser.Parse(b, fileHeader.Codepage));
FileDepList = TryReadMetadata("AcDb:FileDepList", b => FileDepListParser.Parse(b, fileHeader.Codepage));
Preview = TryReadMetadata("AcDb:Preview", PreviewParser.Parse);
Preview = ReadOptionalPreview();
}

if (options.ReadObjects)
Expand Down Expand Up @@ -256,6 +256,27 @@ private List<DwgEntity> ReadOptionalEntities()
return entities;
}

private DwgPreview? ReadOptionalPreview()
{
var entry = _container!.SectionMap.FindByName("AcDb:Preview");
if (entry is null || entry.Encryption == 1)
{
return null;
}

try
{
var pageLayout = new List<SectionPageLayout>();
var bytes = _container.ReadSection(_fileBytes, entry, _verifyChecksums, _diagnostics.Add, pageLayout);
return PreviewParser.Parse(bytes, pageLayout);
}
catch (Exception ex) when (IsParseFailure(ex))
{
_diagnostics.Add($"AcDb:Preview: {ex.Message}");
return null;
}
}

private T? TryReadMetadata<T>(string sectionName, Func<byte[], T> parse)
where T : class
{
Expand Down
16 changes: 15 additions & 1 deletion src/DwgIO/Internal/Container/R2004Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

namespace DwgIO.Internal.Container;

/// <summary>
/// One decompressed page of a section: where it starts in the section and where its
/// payload starts in the file. Absolute file offsets stored inside a section only map
/// linearly back to section positions when the page is stored uncompressed.
/// </summary>
internal readonly record struct SectionPageLayout(
int SectionOffset,
int Length,
long FileOffset,
bool IsStoredUncompressed);

/// <summary>
/// The parsed R2004-family container: decrypted file header,
/// page map and section map. Assembles named sections from their pages.
Expand Down Expand Up @@ -58,12 +69,14 @@ public static R2004Container Parse(byte[] fileBytes)
/// A declared size up to <see cref="MaxSizeOvershoot"/> bytes beyond what the pages
/// can hold is zero-filled and reported through <paramref name="reportDiagnostic"/>.
/// When <paramref name="verifyChecksums"/> is set, a page checksum mismatch throws.
/// When <paramref name="pageLayout"/> is passed, it receives each page's section and file placement.
/// </summary>
public byte[] ReadSection(
byte[] fileBytes,
SectionEntry entry,
bool verifyChecksums = false,
Action<string>? reportDiagnostic = null)
Action<string>? reportDiagnostic = null,
List<SectionPageLayout>? pageLayout = null)
{
if (entry.Encryption == 1)
{
Expand Down Expand Up @@ -143,6 +156,7 @@ public byte[] ReadSection(
int start = (int)pageRef.StartOffset;
int take = Math.Min(decompressed.Length, totalSize - start);
Buffer.BlockCopy(decompressed, 0, result, start, take);
pageLayout?.Add(new SectionPageLayout(start, take, payloadStart, entry.Compression == 1));
}

return result;
Expand Down
Loading
Loading