Cap overlay at 0x10000 bytes

This commit is contained in:
Matt Nadareski
2025-09-25 20:14:41 -04:00
parent ca354b3b7f
commit 97896b91d7
4 changed files with 17 additions and 9 deletions

View File

@@ -156,8 +156,8 @@ namespace SabreTools.Serialization.Wrappers
// Write the resource data to a temp file
using var tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
// If the overlay is large, read it from the source in blocks
if (OverlaySize >= int.MaxValue)
// If the overlay is partially cached, read it from the source in blocks
if (OverlaySize > overlayData.Length)
{
long currentOffset = OverlayAddress + overlayOffset;
long bytesLeft = OverlaySize;

View File

@@ -115,7 +115,7 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Overlay data, if it exists
/// </summary>
/// <remarks>Can only cache up to <see cref="int.MaxValue"/> bytes</remarks>
/// <remarks>Caches up to 0x10000 bytes</remarks>
/// <see href="https://codeberg.org/CYBERDEV/REWise/src/branch/master/src/exefile.c"/>
public byte[] OverlayData
{
@@ -154,7 +154,7 @@ namespace SabreTools.Serialization.Wrappers
}
// Otherwise, cache and return the data
overlaySize = Math.Min(overlaySize, int.MaxValue - 1);
overlaySize = Math.Min(overlaySize, 0x10000);
_overlayData = ReadRangeFromSource((int)endOfSectionData, (int)overlaySize);
return _overlayData;

View File

@@ -217,8 +217,8 @@ namespace SabreTools.Serialization.Wrappers
// Write the resource data to a temp file
using var tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
// If the overlay is large, read it from the source in blocks
if (OverlaySize >= int.MaxValue)
// If the overlay is partially cached, read it from the source in blocks
if (OverlaySize > overlayData.Length)
{
long currentOffset = OverlayAddress + overlayOffset;
long bytesLeft = OverlaySize;

View File

@@ -55,7 +55,7 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Entry point data, if it exists
/// </summary>
/// <remarks>Can only cache up to 128 bytes</remarks>
/// <remarks>Caches up to 128 bytes</remarks>
public byte[] EntryPointData
{
get
@@ -369,7 +369,7 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Overlay data, if it exists
/// </summary>
/// <remarks>Can only cache up to <see cref="int.MaxValue"/> bytes</remarks>
/// <remarks>Caches up to 0x10000 bytes</remarks>
/// <see href="https://www.autoitscript.com/forum/topic/153277-pe-file-overlay-extraction/"/>
public byte[] OverlayData
{
@@ -389,6 +389,14 @@ namespace SabreTools.Serialization.Wrappers
return _overlayData;
}
// If we have certificate data, use that as the end
if (OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)OptionalHeader.CertificateTable.VirtualAddress;
if (certificateTableAddress != 0 && certificateTableAddress < dataLength)
dataLength = certificateTableAddress;
}
// Get the overlay address and size if possible
long endOfSectionData = OverlayAddress;
long overlaySize = OverlaySize;
@@ -408,7 +416,7 @@ namespace SabreTools.Serialization.Wrappers
}
// Otherwise, cache and return the data
overlaySize = Math.Min(overlaySize, int.MaxValue - 1);
overlaySize = Math.Min(overlaySize, 0x10000);
_overlayData = ReadRangeFromSource(endOfSectionData, (int)overlaySize) ?? [];
return _overlayData;