Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/CFB.cs

550 lines
18 KiB
C#
Raw Normal View History

2023-01-09 21:14:21 -08:00
using System;
2023-01-09 22:04:29 -08:00
using System.Collections.Generic;
2023-01-09 21:14:21 -08:00
using System.IO;
2023-01-13 14:04:21 -08:00
using System.Text;
2023-01-09 21:14:21 -08:00
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2023-01-09 21:14:21 -08:00
{
2023-09-11 23:25:09 -04:00
public class CFB : WrapperBase<SabreTools.Models.CFB.Binary>
2023-01-09 21:14:21 -08:00
{
2023-01-18 11:18:53 -08:00
#region Descriptive Properties
/// <inheritdoc/>
2023-09-11 23:25:09 -04:00
public override string DescriptionString => "Compact File Binary";
2023-01-18 11:18:53 -08:00
#endregion
2023-01-09 21:14:21 -08:00
#region Pass-Through Properties
#region Header
/// <inheritdoc cref="Models.CFB.FileHeader.Signature"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ulong Signature => _model.Header.Signature;
2023-09-13 00:08:11 -04:00
#else
public ulong? Signature => _model.Header?.Signature;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.CLSID"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public Guid CLSID => _model.Header.CLSID;
2023-09-13 00:08:11 -04:00
#else
public Guid? CLSID => _model.Header?.CLSID;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.MinorVersion"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort MinorVersion => _model.Header.MinorVersion;
2023-09-13 00:08:11 -04:00
#else
public ushort? MinorVersion => _model.Header?.MinorVersion;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.MajorVersion"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort MajorVersion => _model.Header.MajorVersion;
2023-09-13 00:08:11 -04:00
#else
public ushort? MajorVersion => _model.Header?.MajorVersion;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.ByteOrder"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort ByteOrder => _model.Header.ByteOrder;
2023-09-13 00:08:11 -04:00
#else
public ushort? ByteOrder => _model.Header?.ByteOrder;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.SectorShift"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort SectorShift => _model.Header.SectorShift;
2023-09-13 00:08:11 -04:00
#else
public ushort? SectorShift => _model.Header?.SectorShift;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.MiniSectorShift"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort MiniSectorShift => _model.Header.MiniSectorShift;
2023-09-13 00:08:11 -04:00
#else
public ushort? MiniSectorShift => _model.Header?.MiniSectorShift;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.Reserved"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public byte[] Reserved => _model.Header.Reserved;
2023-09-12 17:12:23 -04:00
#else
2023-09-13 00:08:11 -04:00
public byte[]? Reserved => _model.Header?.Reserved;
2023-09-12 17:12:23 -04:00
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.NumberOfDirectorySectors"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint NumberOfDirectorySectors => _model.Header.NumberOfDirectorySectors;
2023-09-13 00:08:11 -04:00
#else
public uint? NumberOfDirectorySectors => _model.Header?.NumberOfDirectorySectors;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.NumberOfFATSectors"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint NumberOfFATSectors => _model.Header.NumberOfFATSectors;
2023-09-13 00:08:11 -04:00
#else
public uint? NumberOfFATSectors => _model.Header?.NumberOfFATSectors;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.FirstDirectorySectorLocation"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint FirstDirectorySectorLocation => _model.Header.FirstDirectorySectorLocation;
2023-09-13 00:08:11 -04:00
#else
public uint? FirstDirectorySectorLocation => _model.Header?.FirstDirectorySectorLocation;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.TransactionSignatureNumber"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint TransactionSignatureNumber => _model.Header.TransactionSignatureNumber;
2023-09-13 00:08:11 -04:00
#else
public uint? TransactionSignatureNumber => _model.Header?.TransactionSignatureNumber;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.MiniStreamCutoffSize"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint MiniStreamCutoffSize => _model.Header.MiniStreamCutoffSize;
2023-09-13 00:08:11 -04:00
#else
public uint? MiniStreamCutoffSize => _model.Header?.MiniStreamCutoffSize;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.FirstMiniFATSectorLocation"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint FirstMiniFATSectorLocation => _model.Header.FirstMiniFATSectorLocation;
2023-09-13 00:08:11 -04:00
#else
public uint? FirstMiniFATSectorLocation => _model.Header?.FirstMiniFATSectorLocation;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.NumberOfMiniFATSectors"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint NumberOfMiniFATSectors => _model.Header.NumberOfMiniFATSectors;
2023-09-13 00:08:11 -04:00
#else
public uint? NumberOfMiniFATSectors => _model.Header?.NumberOfMiniFATSectors;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.FirstDIFATSectorLocation"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint FirstDIFATSectorLocation => _model.Header.FirstDIFATSectorLocation;
2023-09-13 00:08:11 -04:00
#else
public uint? FirstDIFATSectorLocation => _model.Header?.FirstDIFATSectorLocation;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.NumberOfDIFATSectors"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint NumberOfDIFATSectors => _model.Header.NumberOfDIFATSectors;
2023-09-13 00:08:11 -04:00
#else
public uint? NumberOfDIFATSectors => _model.Header?.NumberOfDIFATSectors;
#endif
2023-01-09 21:14:21 -08:00
/// <inheritdoc cref="Models.CFB.FileHeader.DIFAT"/>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.CFB.SectorNumber[] DIFAT => _model.Header.DIFAT;
2023-09-10 23:26:32 -04:00
#else
2023-09-13 00:08:11 -04:00
public SabreTools.Models.CFB.SectorNumber?[]? DIFAT => _model.Header?.DIFAT;
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 21:14:21 -08:00
#endregion
#region FAT Sector Numbers
/// <inheritdoc cref="Models.CFB.Binary.FATSectorNumbers"/>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.CFB.SectorNumber[] FATSectorNumbers => _model.FATSectorNumbers;
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public SabreTools.Models.CFB.SectorNumber?[]? FATSectorNumbers => _model.FATSectorNumbers;
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 21:14:21 -08:00
#endregion
#region Mini FAT Sector Numbers
/// <inheritdoc cref="Models.CFB.Binary.MiniFATSectorNumbers"/>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.CFB.SectorNumber[] MiniFATSectorNumbers => _model.MiniFATSectorNumbers;
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public SabreTools.Models.CFB.SectorNumber?[]? MiniFATSectorNumbers => _model.MiniFATSectorNumbers;
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 21:14:21 -08:00
#endregion
#region DIFAT Sector Numbers
/// <inheritdoc cref="Models.CFB.Binary.DIFATSectorNumbers"/>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.CFB.SectorNumber[] DIFATSectorNumbers => _model.DIFATSectorNumbers;
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public SabreTools.Models.CFB.SectorNumber?[]? DIFATSectorNumbers => _model.DIFATSectorNumbers;
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 21:14:21 -08:00
#endregion
#region Directory Entries
/// <inheritdoc cref="Models.CFB.Binary.DirectoryEntries"/>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.CFB.DirectoryEntry[] DirectoryEntries => _model.DirectoryEntries;
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public SabreTools.Models.CFB.DirectoryEntry?[]? DirectoryEntries => _model.DirectoryEntries;
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 21:14:21 -08:00
#endregion
#endregion
#region Extension Properties
/// <summary>
/// Normal sector size in bytes
/// </summary>
2023-09-13 00:08:11 -04:00
#if NET48
public long SectorSize => (long)Math.Pow(2, SectorShift);
2023-09-13 00:08:11 -04:00
#else
public long SectorSize => (long)Math.Pow(2, SectorShift ?? 0);
#endif
/// <summary>
/// Mini sector size in bytes
/// </summary>
2023-09-13 00:08:11 -04:00
#if NET48
public long MiniSectorSize => (long)Math.Pow(2, MiniSectorShift);
2023-09-13 00:08:11 -04:00
#else
public long MiniSectorSize => (long)Math.Pow(2, MiniSectorShift ?? 0);
#endif
#endregion
2023-01-09 21:14:21 -08:00
#region Constructors
2023-09-11 23:25:09 -04:00
/// <inheritdoc/>
#if NET48
public CFB(SabreTools.Models.CFB.Binary model, byte[] data, int offset)
#else
public CFB(SabreTools.Models.CFB.Binary? model, byte[]? data, int offset)
#endif
: base(model, data, offset)
{
// All logic is handled by the base class
}
/// <inheritdoc/>
#if NET48
public CFB(SabreTools.Models.CFB.Binary model, Stream data)
#else
public CFB(SabreTools.Models.CFB.Binary? model, Stream? data)
#endif
: base(model, data)
{
// All logic is handled by the base class
}
2023-01-09 21:14:21 -08:00
/// <summary>
/// Create a Compound File Binary from a byte array and offset
/// </summary>
/// <param name="data">Byte array representing the archive</param>
/// <param name="offset">Offset within the array to parse</param>
/// <returns>A Compound File Binary wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-09 21:14:21 -08:00
public static CFB Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static CFB? Create(byte[]? data, int offset)
#endif
2023-01-09 21:14:21 -08:00
{
// If the data is invalid
if (data == null)
return null;
// If the offset is out of bounds
if (offset < 0 || offset >= data.Length)
return null;
// Create a memory stream and use that
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return Create(dataStream);
}
/// <summary>
/// Create a Compound File Binary from a Stream
/// </summary>
/// <param name="data">Stream representing the archive</param>
/// <returns>A Compound File Binary wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-09 21:14:21 -08:00
public static CFB Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static CFB? Create(Stream? data)
#endif
2023-01-09 21:14:21 -08:00
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
2023-09-10 23:51:38 -04:00
var binary = new SabreTools.Serialization.Streams.CFB().Deserialize(data);
2023-01-09 21:14:21 -08:00
if (binary == null)
return null;
2023-09-11 23:25:09 -04:00
try
{
return new CFB(binary, data);
}
catch
2023-01-09 21:14:21 -08:00
{
2023-09-11 23:25:09 -04:00
return null;
}
2023-01-09 21:14:21 -08:00
}
#endregion
2023-01-09 22:17:58 -08:00
#region FAT Sector Data
2023-01-09 22:04:29 -08:00
/// <summary>
/// Get the ordered FAT sector chain for a given starting sector
/// </summary>
/// <param name="startingSector">Initial FAT sector</param>
/// <returns>Ordered list of sector numbers, null on error</returns>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public List<SabreTools.Models.CFB.SectorNumber> GetFATSectorChain(SabreTools.Models.CFB.SectorNumber startingSector)
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public List<SabreTools.Models.CFB.SectorNumber?>? GetFATSectorChain(SabreTools.Models.CFB.SectorNumber? startingSector)
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 22:04:29 -08:00
{
// If we have an invalid sector
2023-09-13 00:29:21 -04:00
#if NET48
2023-09-13 00:08:11 -04:00
if (startingSector < 0 || FATSectorNumbers == null || (long)startingSector >= FATSectorNumbers.Length)
2023-09-13 00:29:21 -04:00
#else
if (startingSector == null || startingSector < 0 || FATSectorNumbers == null || (long)startingSector >= FATSectorNumbers.Length)
#endif
2023-01-09 22:04:29 -08:00
return null;
// Setup the returned list
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
var sectors = new List<SabreTools.Models.CFB.SectorNumber> { startingSector };
2023-09-10 23:26:32 -04:00
#else
var sectors = new List<SabreTools.Models.CFB.SectorNumber?> { startingSector };
#endif
2023-01-09 22:04:29 -08:00
2023-01-09 22:15:37 -08:00
var lastSector = startingSector;
2023-01-09 22:04:29 -08:00
while (true)
{
2023-09-13 00:29:21 -04:00
#if NET6_0_OR_GREATER
if (lastSector == null)
break;
#endif
2023-01-09 22:04:29 -08:00
// Get the next sector from the lookup table
2023-09-13 00:29:21 -04:00
#if NET48
2023-01-09 22:04:29 -08:00
var nextSector = FATSectorNumbers[(uint)lastSector];
2023-09-13 00:29:21 -04:00
#else
var nextSector = FATSectorNumbers[(uint)lastSector!.Value];
#endif
2023-01-09 22:04:29 -08:00
// If we have an end of chain or free sector
2023-09-04 23:44:45 -04:00
if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT)
2023-01-09 22:04:29 -08:00
break;
// Add the next sector to the list and replace the last sector
sectors.Add(nextSector);
lastSector = nextSector;
}
return sectors;
}
2023-01-09 22:15:37 -08:00
/// <summary>
/// Get the data for the FAT sector chain starting at a given starting sector
/// </summary>
/// <param name="startingSector">Initial FAT sector</param>
/// <returns>Ordered list of sector numbers, null on error</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public byte[] GetFATSectorChainData(SabreTools.Models.CFB.SectorNumber startingSector)
2023-09-12 17:12:23 -04:00
#else
public byte[]? GetFATSectorChainData(SabreTools.Models.CFB.SectorNumber startingSector)
#endif
2023-01-09 22:15:37 -08:00
{
// Get the sector chain first
var sectorChain = GetFATSectorChain(startingSector);
if (sectorChain == null)
return null;
// Sequentially read the sectors
var data = new List<byte>();
for (int i = 0; i < sectorChain.Count; i++)
{
// Try to get the sector data offset
2023-01-09 22:17:58 -08:00
int sectorDataOffset = (int)FATSectorToFileOffset(sectorChain[i]);
2023-01-09 22:15:37 -08:00
if (sectorDataOffset < 0 || sectorDataOffset >= GetEndOfFile())
return null;
// Try to read the sector data
var sectorData = ReadFromDataSource(sectorDataOffset, (int)SectorSize);
2023-01-09 22:15:37 -08:00
if (sectorData == null)
return null;
// Add the sector data to the output
data.AddRange(sectorData);
}
return data.ToArray();
}
2023-01-09 22:17:58 -08:00
/// <summary>
/// Convert a FAT sector value to a byte offset
/// </summary>
/// <param name="sector">Sector to convert</param>
/// <returns>File offset in bytes, -1 on error</returns>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public long FATSectorToFileOffset(SabreTools.Models.CFB.SectorNumber sector)
2023-09-10 23:26:32 -04:00
#else
public long FATSectorToFileOffset(SabreTools.Models.CFB.SectorNumber? sector)
#endif
2023-01-09 22:17:58 -08:00
{
// If we have an invalid sector number
2023-09-12 17:12:23 -04:00
#if NET48
if (sector > SabreTools.Models.CFB.SectorNumber.MAXREGSECT)
#else
2023-09-10 23:26:32 -04:00
if (sector == null || sector > SabreTools.Models.CFB.SectorNumber.MAXREGSECT)
2023-09-12 17:12:23 -04:00
#endif
2023-01-09 22:17:58 -08:00
return -1;
// Convert based on the sector shift value
return (long)(sector + 1) * SectorSize;
2023-01-09 22:17:58 -08:00
}
#endregion
#region Mini FAT Sector Data
2023-01-09 22:04:29 -08:00
/// <summary>
/// Get the ordered Mini FAT sector chain for a given starting sector
/// </summary>
/// <param name="startingSector">Initial Mini FAT sector</param>
/// <returns>Ordered list of sector numbers, null on error</returns>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public List<SabreTools.Models.CFB.SectorNumber> GetMiniFATSectorChain(SabreTools.Models.CFB.SectorNumber startingSector)
2023-09-10 23:26:32 -04:00
#else
2023-09-12 17:12:23 -04:00
public List<SabreTools.Models.CFB.SectorNumber?>? GetMiniFATSectorChain(SabreTools.Models.CFB.SectorNumber? startingSector)
2023-09-10 23:26:32 -04:00
#endif
2023-01-09 22:04:29 -08:00
{
// If we have an invalid sector
2023-09-13 00:29:21 -04:00
#if NET48
2023-09-13 00:08:11 -04:00
if (startingSector < 0 || MiniFATSectorNumbers == null || (long)startingSector >= MiniFATSectorNumbers.Length)
2023-09-13 00:29:21 -04:00
#else
if (startingSector == null || startingSector < 0 || MiniFATSectorNumbers == null || (long)startingSector >= MiniFATSectorNumbers.Length)
#endif
2023-01-09 22:04:29 -08:00
return null;
// Setup the returned list
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
var sectors = new List<SabreTools.Models.CFB.SectorNumber> { startingSector };
2023-09-13 00:08:11 -04:00
#else
2023-09-10 23:26:32 -04:00
var sectors = new List<SabreTools.Models.CFB.SectorNumber?> { startingSector };
2023-09-13 00:08:11 -04:00
#endif
2023-01-09 22:04:29 -08:00
2023-01-09 22:15:37 -08:00
var lastSector = startingSector;
2023-01-09 22:04:29 -08:00
while (true)
{
2023-09-13 00:29:21 -04:00
#if NET6_0_OR_GREATER
if (lastSector == null)
break;
#endif
2023-01-09 22:04:29 -08:00
// Get the next sector from the lookup table
2023-09-13 00:29:21 -04:00
#if NET48
2023-01-09 22:04:29 -08:00
var nextSector = MiniFATSectorNumbers[(uint)lastSector];
2023-09-13 00:29:21 -04:00
#else
var nextSector = MiniFATSectorNumbers[(uint)lastSector!.Value];
#endif
2023-01-09 22:04:29 -08:00
// If we have an end of chain or free sector
2023-09-04 23:44:45 -04:00
if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT)
2023-01-09 22:04:29 -08:00
break;
// Add the next sector to the list and replace the last sector
sectors.Add(nextSector);
lastSector = nextSector;
}
return sectors;
}
2023-01-09 22:15:37 -08:00
/// <summary>
/// Get the data for the Mini FAT sector chain starting at a given starting sector
/// </summary>
/// <param name="startingSector">Initial Mini FAT sector</param>
/// <returns>Ordered list of sector numbers, null on error</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public byte[] GetMiniFATSectorChainData(SabreTools.Models.CFB.SectorNumber startingSector)
2023-09-12 17:12:23 -04:00
#else
public byte[]? GetMiniFATSectorChainData(SabreTools.Models.CFB.SectorNumber startingSector)
#endif
2023-01-09 22:15:37 -08:00
{
// Get the sector chain first
var sectorChain = GetMiniFATSectorChain(startingSector);
if (sectorChain == null)
return null;
// Sequentially read the sectors
var data = new List<byte>();
for (int i = 0; i < sectorChain.Count; i++)
{
// Try to get the sector data offset
2023-01-09 22:17:58 -08:00
int sectorDataOffset = (int)MiniFATSectorToFileOffset(sectorChain[i]);
2023-01-09 22:15:37 -08:00
if (sectorDataOffset < 0 || sectorDataOffset >= GetEndOfFile())
return null;
// Try to read the sector data
var sectorData = ReadFromDataSource(sectorDataOffset, (int)MiniSectorSize);
2023-01-09 22:15:37 -08:00
if (sectorData == null)
return null;
// Add the sector data to the output
data.AddRange(sectorData);
}
return data.ToArray();
}
/// <summary>
/// Convert a Mini FAT sector value to a byte offset
/// </summary>
/// <param name="sector">Sector to convert</param>
/// <returns>File offset in bytes, -1 on error</returns>
2023-09-10 23:26:32 -04:00
#if NET48
2023-09-04 23:44:45 -04:00
public long MiniFATSectorToFileOffset(SabreTools.Models.CFB.SectorNumber sector)
2023-09-10 23:26:32 -04:00
#else
public long MiniFATSectorToFileOffset(SabreTools.Models.CFB.SectorNumber? sector)
#endif
2023-01-09 22:15:37 -08:00
{
// If we have an invalid sector number
2023-09-12 17:12:23 -04:00
#if NET48
if (sector > SabreTools.Models.CFB.SectorNumber.MAXREGSECT)
#else
2023-09-10 23:26:32 -04:00
if (sector == null || sector > SabreTools.Models.CFB.SectorNumber.MAXREGSECT)
2023-09-12 17:12:23 -04:00
#endif
2023-01-09 22:15:37 -08:00
return -1;
// Convert based on the sector shift value
return (long)(sector + 1) * MiniSectorSize;
2023-01-09 22:15:37 -08:00
}
2023-01-09 22:04:29 -08:00
#endregion
2023-01-09 21:14:21 -08:00
#region Printing
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
2023-01-09 21:14:21 -08:00
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
2023-09-13 12:49:46 -04:00
Printing.CFB.Print(builder, _model);
2023-01-13 14:04:21 -08:00
return builder;
2023-01-09 21:14:21 -08:00
}
#if NET6_0_OR_GREATER
/// <inheritdoc/>
2023-09-11 23:25:09 -04:00
public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_model, _jsonSerializerOptions);
#endif
2023-01-09 21:14:21 -08:00
#endregion
}
}