Skip to content

Commit cd7e6ff

Browse files
committed
Fix some logic bugs due to offsets
1 parent 510a0f7 commit cd7e6ff

26 files changed

+339
-287
lines changed

SabreTools.Serialization/Deserializers/BFPK.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class BFPK : BaseBinaryDeserializer<Archive>
1717

1818
try
1919
{
20+
// Cache the current offset
21+
long initialOffset = data.Position;
22+
2023
// Create a new archive to fill
2124
var archive = new Archive();
2225

@@ -40,7 +43,7 @@ public class BFPK : BaseBinaryDeserializer<Archive>
4043
// Read all entries in turn
4144
for (int i = 0; i < header.Files; i++)
4245
{
43-
files[i] = ParseFileEntry(data);
46+
files[i] = ParseFileEntry(data, initialOffset);
4447
}
4548

4649
// Set the files
@@ -62,7 +65,7 @@ public class BFPK : BaseBinaryDeserializer<Archive>
6265
/// </summary>
6366
/// <param name="data">Stream to parse</param>
6467
/// <returns>Filled FileEntry on success, null on error</returns>
65-
public static FileEntry ParseFileEntry(Stream data)
68+
public static FileEntry ParseFileEntry(Stream data, long initialOffset)
6669
{
6770
var fileEntry = new FileEntry();
6871

@@ -78,7 +81,7 @@ public static FileEntry ParseFileEntry(Stream data)
7881
if (fileEntry.Offset > 0)
7982
{
8083
long currentOffset = data.Position;
81-
data.Seek(fileEntry.Offset, SeekOrigin.Begin);
84+
data.Seek(initialOffset + fileEntry.Offset, SeekOrigin.Begin);
8285
fileEntry.CompressedSize = data.ReadInt32LittleEndian();
8386
data.Seek(currentOffset, SeekOrigin.Begin);
8487
}

SabreTools.Serialization/Deserializers/BSP.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class BSP : BaseBinaryDeserializer<BspFile>
1919

2020
try
2121
{
22+
// Cache the current offset
23+
long initialOffset = data.Position;
24+
2225
// Create a new Half-Life Level to fill
2326
var file = new BspFile();
2427

@@ -46,7 +49,7 @@ public class BSP : BaseBinaryDeserializer<BspFile>
4649
continue;
4750

4851
// Seek to the lump offset
49-
data.Seek(lumpEntry.Offset, SeekOrigin.Begin);
52+
data.Seek(initialOffset + lumpEntry.Offset, SeekOrigin.Begin);
5053

5154
// Read according to the lump type
5255
switch ((LumpType)l)

SabreTools.Serialization/Deserializers/CFB.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class CFB : BaseBinaryDeserializer<Binary>
1919

2020
try
2121
{
22+
// Cache the current offset
23+
long initialOffset = data.Position;
24+
2225
// Create a new binary to fill
2326
var binary = new Binary();
2427

@@ -62,8 +65,9 @@ public class CFB : BaseBinaryDeserializer<Binary>
6265
break;
6366

6467
// Get the new next sector information
65-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
66-
if (sectorOffset < 0 || sectorOffset >= data.Length)
68+
long sectorOffset = initialOffset
69+
+ (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
70+
if (sectorOffset < initialOffset || sectorOffset >= data.Length)
6771
return null;
6872

6973
// Seek to the next sector
@@ -105,8 +109,9 @@ public class CFB : BaseBinaryDeserializer<Binary>
105109
break;
106110

107111
// Get the new next sector information
108-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
109-
if (sectorOffset < 0 || sectorOffset >= data.Length)
112+
long sectorOffset = initialOffset
113+
+ (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
114+
if (sectorOffset < initialOffset || sectorOffset >= data.Length)
110115
return null;
111116

112117
// Seek to the next sector
@@ -140,8 +145,9 @@ public class CFB : BaseBinaryDeserializer<Binary>
140145
break;
141146

142147
// Get the new next sector information
143-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
144-
if (sectorOffset < 0 || sectorOffset >= data.Length)
148+
long sectorOffset = initialOffset
149+
+ (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
150+
if (sectorOffset < initialOffset || sectorOffset >= data.Length)
145151
return null;
146152

147153
// Seek to the next sector
@@ -194,8 +200,9 @@ public class CFB : BaseBinaryDeserializer<Binary>
194200
break;
195201

196202
// Get the new next sector information
197-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
198-
if (sectorOffset < 0 || sectorOffset >= data.Length)
203+
long sectorOffset = initialOffset
204+
+ (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
205+
if (sectorOffset < initialOffset || sectorOffset >= data.Length)
199206
return null;
200207

201208
// Seek to the next sector

SabreTools.Serialization/Deserializers/CHD.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ public class CHD : BaseBinaryDeserializer<Header>
1818

1919
try
2020
{
21+
// Cache the current offset
22+
long initialOffset = data.Position;
23+
2124
// Determine the header version
22-
uint version = GetVersion(data);
25+
uint version = GetVersion(data, initialOffset);
2326

2427
// Read and return the current CHD
2528
switch (version)
@@ -98,7 +101,7 @@ public class CHD : BaseBinaryDeserializer<Header>
98101
/// Get the matching CHD version, if possible
99102
/// </summary>
100103
/// <returns>Matching version, 0 if none</returns>
101-
private static uint GetVersion(Stream data)
104+
private static uint GetVersion(Stream data, long initialOffset)
102105
{
103106
// Read the header values
104107
byte[] tagBytes = data.ReadBytes(8);
@@ -107,7 +110,7 @@ private static uint GetVersion(Stream data)
107110
uint version = data.ReadUInt32BigEndian();
108111

109112
// Seek back to start
110-
data.SeekIfPossible();
113+
data.SeekIfPossible(initialOffset);
111114

112115
// Check the signature
113116
if (!string.Equals(tag, Constants.SignatureString, StringComparison.Ordinal))

SabreTools.Serialization/Deserializers/GCF.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class GCF : BaseBinaryDeserializer<Models.GCF.File>
1616

1717
try
1818
{
19+
// Cache the current offset
20+
long initialOffset = data.Position;
21+
1922
// Create a new Half-Life Game Cache to fill
2023
var file = new Models.GCF.File();
2124

@@ -100,7 +103,7 @@ public class GCF : BaseBinaryDeserializer<Models.GCF.File>
100103
#endregion
101104

102105
// Cache the current offset
103-
long initialOffset = data.Position;
106+
long afterMapPosition = data.Position;
104107

105108
#region Directory Header
106109

@@ -213,7 +216,7 @@ public class GCF : BaseBinaryDeserializer<Models.GCF.File>
213216
#endregion
214217

215218
// Seek to end of directory section, just in case
216-
data.Seek(initialOffset + file.DirectoryHeader.DirectorySize, SeekOrigin.Begin);
219+
data.Seek(afterMapPosition + file.DirectoryHeader.DirectorySize, SeekOrigin.Begin);
217220

218221
#region Directory Map Header
219222

@@ -301,7 +304,7 @@ public class GCF : BaseBinaryDeserializer<Models.GCF.File>
301304
#endregion
302305

303306
// Seek to end of checksum section, just in case
304-
data.Seek(initialOffset + checksumHeader.ChecksumSize, SeekOrigin.Begin);
307+
data.Seek(afterMapPosition + checksumHeader.ChecksumSize, SeekOrigin.Begin);
305308

306309
#region Data Block Header
307310

SabreTools.Serialization/Deserializers/InstallShieldArchiveV3.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class InstallShieldArchiveV3 : BaseBinaryDeserializer<Archive>
1616

1717
try
1818
{
19+
// Cache the current offset
20+
long initialOffset = data.Position;
21+
1922
// Create a new archive to fill
2023
var archive = new Archive();
2124

@@ -25,7 +28,7 @@ public class InstallShieldArchiveV3 : BaseBinaryDeserializer<Archive>
2528
var header = ParseHeader(data);
2629
if (header.Signature1 != Constants.HeaderSignature)
2730
return null;
28-
if (header.TocAddress >= data.Length)
31+
if (initialOffset + header.TocAddress >= data.Length)
2932
return null;
3033

3134
// Set the archive header
@@ -36,8 +39,8 @@ public class InstallShieldArchiveV3 : BaseBinaryDeserializer<Archive>
3639
#region Directories
3740

3841
// Get the directories offset
39-
uint directoriesOffset = header.TocAddress;
40-
if (directoriesOffset < 0 || directoriesOffset >= data.Length)
42+
long directoriesOffset = initialOffset + header.TocAddress;
43+
if (directoriesOffset < initialOffset || directoriesOffset >= data.Length)
4144
return null;
4245

4346
// Seek to the directories

0 commit comments

Comments
 (0)