GetEndOffset really shouldn't be used

This commit is contained in:
Matt Nadareski
2025-08-20 07:54:52 -04:00
parent 601e781ef9
commit 6fbe1cffaa
7 changed files with 55 additions and 61 deletions

View File

@@ -132,7 +132,7 @@ namespace SabreTools.Serialization.Wrappers
int compressedSize = file.CompressedSize;
// Some files can lack the length prefix
if (compressedSize > GetEndOffset())
if (compressedSize > Length)
{
offset -= 4;
compressedSize = file.UncompressedSize;

View File

@@ -314,7 +314,7 @@ namespace SabreTools.Serialization.Wrappers
{
// Try to get the sector data offset
int sectorDataOffset = (int)FATSectorToFileOffset(sectorChain[i]);
if (sectorDataOffset < 0 || sectorDataOffset >= GetEndOffset())
if (sectorDataOffset < 0 || sectorDataOffset >= Length)
return null;
// Try to read the sector data

View File

@@ -165,9 +165,9 @@ namespace SabreTools.Serialization.Wrappers
if (InformationBlock == null)
return -1;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// Get the matching entry
@@ -177,7 +177,7 @@ namespace SabreTools.Serialization.Wrappers
// Verify the entry offset
int offset = (int)(entry.PageDataOffset << (int)InformationBlock.BytesOnLastPage);
if (offset < 0 || offset + entry.DataSize >= endOfFile)
if (offset < 0 || offset + entry.DataSize >= dataLength)
return -1;
// Return the verified offset
@@ -267,9 +267,9 @@ namespace SabreTools.Serialization.Wrappers
if (InformationBlock == null)
return -1;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// Get the matching entry
@@ -309,7 +309,7 @@ namespace SabreTools.Serialization.Wrappers
// If we have an unset length, read the whole source
if (length == -1)
length = GetEndOffset();
length = Length;
return ReadFromDataSource(rangeStart, (int)length);
}

View File

@@ -39,9 +39,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayAddress != null)
return _overlayAddress.Value;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// If a required property is missing
@@ -109,9 +109,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayData != null)
return _overlayData;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return null;
// If a required property is missing
@@ -151,14 +151,14 @@ namespace SabreTools.Serialization.Wrappers
endOfSectionData += 705;
// If we're at the end of the file, cache an empty byte array
if (endOfSectionData >= endOfFile)
if (endOfSectionData >= dataLength)
{
_overlayData = [];
return _overlayData;
}
// Otherwise, cache and return the data
long overlayLength = endOfFile - endOfSectionData;
long overlayLength = dataLength - endOfSectionData;
_overlayData = ReadFromDataSource(endOfSectionData, (int)overlayLength);
return _overlayData;
}
@@ -178,9 +178,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayStrings != null)
return _overlayStrings;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return null;
// If a required property is missing
@@ -220,7 +220,7 @@ namespace SabreTools.Serialization.Wrappers
endOfSectionData += 705;
// If we're at the end of the file, cache an empty list
if (endOfSectionData >= endOfFile)
if (endOfSectionData >= dataLength)
{
_overlayStrings = [];
return _overlayStrings;
@@ -228,7 +228,7 @@ namespace SabreTools.Serialization.Wrappers
// TODO: Revisit the 16 MiB limit
// Cap the check for overlay strings to 16 MiB (arbitrary)
long overlayLength = Math.Min(endOfFile - endOfSectionData, 16 * 1024 * 1024);
long overlayLength = Math.Min(dataLength - endOfSectionData, 16 * 1024 * 1024);
// Otherwise, cache and return the strings
_overlayStrings = ReadStringsFromDataSource(endOfSectionData, (int)overlayLength, charLimit: 3);
@@ -384,9 +384,9 @@ namespace SabreTools.Serialization.Wrappers
if (Header == null)
return null;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return null;
// If the resource table is invalid
@@ -462,9 +462,9 @@ namespace SabreTools.Serialization.Wrappers
/// <returns>Resource offset on success, -1 otherwise</returns>
public int GetResourceOffset(int id)
{
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// If the resource table is invalid
@@ -478,7 +478,7 @@ namespace SabreTools.Serialization.Wrappers
// Verify the resource offset
int offset = resource.Offset << ResourceTable.AlignmentShiftCount;
if (offset < 0 || offset + resource.Length >= endOfFile)
if (offset < 0 || offset + resource.Length >= dataLength)
return -1;
// Return the verified offset
@@ -558,9 +558,9 @@ namespace SabreTools.Serialization.Wrappers
if (Header == null)
return -1;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// Get the matching segment
@@ -570,7 +570,7 @@ namespace SabreTools.Serialization.Wrappers
// Verify the segment offset
int offset = segment.Offset << Header.SegmentAlignmentShiftCount;
if (offset < 0 || offset + segment.Length >= endOfFile)
if (offset < 0 || offset + segment.Length >= dataLength)
return -1;
// Return the verified offset
@@ -596,7 +596,7 @@ namespace SabreTools.Serialization.Wrappers
// If we have an unset length, read the whole source
if (length == -1)
length = GetEndOffset();
length = Length;
return ReadFromDataSource(rangeStart, (int)length);
}

View File

@@ -207,9 +207,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayAddress != null)
return _overlayAddress.Value;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return -1;
// If the section table is missing
@@ -220,8 +220,8 @@ namespace SabreTools.Serialization.Wrappers
if (OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
if (certificateTableAddress != 0 && certificateTableAddress < dataLength)
dataLength = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
@@ -278,9 +278,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayData != null)
return _overlayData;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return null;
// If the section table is missing
@@ -291,8 +291,8 @@ namespace SabreTools.Serialization.Wrappers
if (OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
if (certificateTableAddress != 0 && certificateTableAddress < dataLength)
dataLength = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
@@ -329,14 +329,14 @@ namespace SabreTools.Serialization.Wrappers
return null;
// If we're at the end of the file, cache an empty byte array
if (endOfSectionData >= endOfFile)
if (endOfSectionData >= dataLength)
{
_overlayData = [];
return _overlayData;
}
// Otherwise, cache and return the data
long overlayLength = endOfFile - endOfSectionData;
long overlayLength = dataLength - endOfSectionData;
_overlayData = ReadFromDataSource(endOfSectionData, (int)overlayLength);
return _overlayData;
}
@@ -356,9 +356,9 @@ namespace SabreTools.Serialization.Wrappers
if (_overlayStrings != null)
return _overlayStrings;
// Get the end of the file, if possible
long endOfFile = GetEndOffset();
if (endOfFile == -1)
// Get the available source length, if possible
long dataLength = Length;
if (dataLength == -1)
return null;
// If the section table is missing
@@ -369,8 +369,8 @@ namespace SabreTools.Serialization.Wrappers
if (OptionalHeader?.CertificateTable != null)
{
int certificateTableAddress = (int)OptionalHeader.CertificateTable.VirtualAddress.ConvertVirtualAddress(SectionTable);
if (certificateTableAddress != 0 && certificateTableAddress < endOfFile)
endOfFile = certificateTableAddress;
if (certificateTableAddress != 0 && certificateTableAddress < dataLength)
dataLength = certificateTableAddress;
}
// Search through all sections and find the furthest a section goes
@@ -407,7 +407,7 @@ namespace SabreTools.Serialization.Wrappers
return null;
// If we're at the end of the file, cache an empty list
if (endOfSectionData >= endOfFile)
if (endOfSectionData >= dataLength)
{
_overlayStrings = [];
return _overlayStrings;
@@ -415,7 +415,7 @@ namespace SabreTools.Serialization.Wrappers
// TODO: Revisit the 16 MiB limit
// Cap the check for overlay strings to 16 MiB (arbitrary)
long overlayLength = Math.Min(endOfFile - endOfSectionData, 16 * 1024 * 1024);
long overlayLength = Math.Min(dataLength - endOfSectionData, 16 * 1024 * 1024);
// Otherwise, cache and return the strings
_overlayStrings = ReadStringsFromDataSource(endOfSectionData, (int)overlayLength, charLimit: 3);

View File

@@ -135,7 +135,7 @@ namespace SabreTools.Serialization.Wrappers
// Read the entire compressed data
int compressedDataOffset = (int)CompressedDataOffset;
long compressedDataLength = GetEndOffset() - compressedDataOffset;
long compressedDataLength = Length - compressedDataOffset;
var compressedData = ReadFromDataSource(compressedDataOffset, (int)compressedDataLength);
// Print a debug reminder

View File

@@ -93,12 +93,6 @@ namespace SabreTools.Serialization.Wrappers
#region Data
/// <summary>
/// Get the ending offset of the source
/// </summary>
/// <returns>Value greater than 0 for a valid end of file, -1 on error</returns>
public long GetEndOffset() => _dataSource.GetEndOffset();
/// <summary>
/// Read data from the source
/// </summary>