Skip to content

Commit d71cb41

Browse files
committed
Fix parsing of large DIFAT tables in CFB
1 parent 8e745db commit d71cb41

File tree

1 file changed

+52
-49
lines changed
  • BinaryObjectScanner/FileType

1 file changed

+52
-49
lines changed

BinaryObjectScanner/FileType/CFB.cs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -280,28 +280,31 @@ private static int Mime2Utf(int x)
280280
var currentSector = (SectorNumber?)fileHeader.FirstDIFATSectorLocation;
281281
for (int i = 0; i < fileHeader.NumberOfDIFATSectors; i++)
282282
{
283-
// If we have a readable sector
284-
if (currentSector <= SectorNumber.MAXREGSECT)
285-
{
286-
// Get the new next sector information
287-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
288-
if (sectorOffset < 0 || sectorOffset >= data.Length)
289-
return null;
283+
// If we have an unreadable sector
284+
if (currentSector > SectorNumber.MAXREGSECT)
285+
break;
286+
287+
// Get the new next sector information
288+
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
289+
if (sectorOffset < 0 || sectorOffset >= data.Length)
290+
return null;
290291

291-
// Seek to the next sector
292-
data.Seek(sectorOffset, SeekOrigin.Begin);
292+
// Seek to the next sector
293+
data.Seek(sectorOffset, SeekOrigin.Begin);
293294

294-
// Try to parse the sectors
295-
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
296-
if (sectorNumbers == null)
297-
return null;
295+
// Try to parse the sectors
296+
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
297+
if (sectorNumbers == null)
298+
return null;
298299

299-
// Add the sector shifts
300-
difatSectors.AddRange(sectorNumbers);
300+
// Add all but the last sector number that was parsed
301+
for (int j = 0; j < sectorNumbers.Length - 1; j++)
302+
{
303+
difatSectors.Add(sectorNumbers[j]);
301304
}
302305

303-
// Get the next sector from the DIFAT
304-
currentSector = difatSectors[i];
306+
// Get the next sector from the final sector number
307+
currentSector = sectorNumbers[sectorNumbers.Length - 1];
305308
}
306309

307310
// Assign the DIFAT sectors table
@@ -320,25 +323,25 @@ private static int Mime2Utf(int x)
320323
// Get the next sector from the DIFAT
321324
currentSector = binary.DIFATSectorNumbers[i];
322325

323-
// If we have a readable sector
324-
if (currentSector <= SectorNumber.MAXREGSECT)
325-
{
326-
// Get the new next sector information
327-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
328-
if (sectorOffset < 0 || sectorOffset >= data.Length)
329-
return null;
326+
// If we have an unreadable sector
327+
if (currentSector > SectorNumber.MAXREGSECT)
328+
break;
330329

331-
// Seek to the next sector
332-
data.Seek(sectorOffset, SeekOrigin.Begin);
330+
// Get the new next sector information
331+
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
332+
if (sectorOffset < 0 || sectorOffset >= data.Length)
333+
return null;
333334

334-
// Try to parse the sectors
335-
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
336-
if (sectorNumbers == null)
337-
return null;
335+
// Seek to the next sector
336+
data.Seek(sectorOffset, SeekOrigin.Begin);
338337

339-
// Add the sector shifts
340-
fatSectors.AddRange(sectorNumbers);
341-
}
338+
// Try to parse the sectors
339+
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
340+
if (sectorNumbers == null)
341+
return null;
342+
343+
// Add the sector shifts
344+
fatSectors.AddRange(sectorNumbers);
342345
}
343346

344347
// Assign the FAT sectors table
@@ -355,25 +358,25 @@ private static int Mime2Utf(int x)
355358
currentSector = (SectorNumber)fileHeader.FirstMiniFATSectorLocation;
356359
for (int i = 0; i < fileHeader.NumberOfMiniFATSectors; i++)
357360
{
358-
// If we have a readable sector
359-
if (currentSector <= SectorNumber.MAXREGSECT)
360-
{
361-
// Get the new next sector information
362-
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
363-
if (sectorOffset < 0 || sectorOffset >= data.Length)
364-
return null;
361+
// If we have an unreadable sector
362+
if (currentSector > SectorNumber.MAXREGSECT)
363+
break;
365364

366-
// Seek to the next sector
367-
data.Seek(sectorOffset, SeekOrigin.Begin);
365+
// Get the new next sector information
366+
long sectorOffset = (long)((long)(currentSector + 1) * Math.Pow(2, fileHeader.SectorShift));
367+
if (sectorOffset < 0 || sectorOffset >= data.Length)
368+
return null;
368369

369-
// Try to parse the sectors
370-
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
371-
if (sectorNumbers == null)
372-
return null;
370+
// Seek to the next sector
371+
data.Seek(sectorOffset, SeekOrigin.Begin);
373372

374-
// Add the sector shifts
375-
miniFatSectors.AddRange(sectorNumbers);
376-
}
373+
// Try to parse the sectors
374+
var sectorNumbers = ParseSectorNumbers(data, fileHeader.SectorShift);
375+
if (sectorNumbers == null)
376+
return null;
377+
378+
// Add the sector shifts
379+
miniFatSectors.AddRange(sectorNumbers);
377380

378381
// Get the next sector from the FAT
379382
currentSector = binary.FATSectorNumbers[(int)currentSector];

0 commit comments

Comments
 (0)