2023-09-08 22:16:47 -04:00
|
|
|
using System.IO;
|
2024-04-03 20:55:02 -04:00
|
|
|
using System.Text;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.MSDOS;
|
2024-04-17 11:52:22 -04:00
|
|
|
using SabreTools.IO.Extensions;
|
2026-03-24 19:17:25 -04:00
|
|
|
using SabreTools.Numerics.Extensions;
|
2025-09-26 13:06:18 -04:00
|
|
|
using static SabreTools.Data.Models.MSDOS.Constants;
|
2023-09-08 22:16:47 -04:00
|
|
|
|
2026-01-27 12:03:01 -05:00
|
|
|
#pragma warning disable IDE0017 // Simplify object initialization
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-08 22:16:47 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class MSDOS : BaseBinaryReader<Executable>
|
2023-09-08 22:16:47 -04:00
|
|
|
{
|
2024-04-03 20:55:02 -04:00
|
|
|
/// <inheritdoc/>
|
2024-04-04 01:55:05 -04:00
|
|
|
public override Executable? Deserialize(Stream? data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
|
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2024-04-03 20:55:02 -04:00
|
|
|
return null;
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Cache the current offset
|
2025-08-19 09:09:33 -04:00
|
|
|
long initialOffset = data.Position;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create a new executable to fill
|
|
|
|
|
var executable = new Executable();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#region Executable Header
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Try to parse the executable header
|
|
|
|
|
var executableHeader = ParseExecutableHeader(data);
|
2024-12-16 23:08:45 -05:00
|
|
|
if (executableHeader.Magic != SignatureString)
|
2024-11-28 22:57:12 -05:00
|
|
|
return null;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Set the executable header
|
|
|
|
|
executable.Header = executableHeader;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#endregion
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#region Relocation Table
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If the offset for the relocation table doesn't exist
|
2025-08-19 09:09:33 -04:00
|
|
|
long tableAddress = initialOffset + executableHeader.RelocationTableAddr;
|
2024-11-28 22:57:12 -05:00
|
|
|
if (tableAddress >= data.Length)
|
|
|
|
|
return executable;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Try to parse the relocation table
|
2025-10-27 22:43:56 -04:00
|
|
|
data.SeekIfPossible(tableAddress, SeekOrigin.Begin);
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Set the relocation table
|
2024-12-16 23:08:45 -05:00
|
|
|
executable.RelocationTable = new RelocationEntry[executableHeader.RelocationItems];
|
|
|
|
|
for (int i = 0; i < executableHeader.RelocationItems; i++)
|
|
|
|
|
{
|
|
|
|
|
executable.RelocationTable[i] = ParseRelocationEntry(data);
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#endregion
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Return the executable
|
|
|
|
|
return executable;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// Parse a Stream into an ExecutableHeader
|
2024-04-03 20:55:02 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// <returns>Filled ExecutableHeader on success, null on error</returns>
|
|
|
|
|
public static ExecutableHeader ParseExecutableHeader(Stream data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-12-16 23:08:45 -05:00
|
|
|
var obj = new ExecutableHeader();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
|
|
|
|
#region Standard Fields
|
|
|
|
|
|
2024-11-27 13:47:07 -05:00
|
|
|
byte[] magic = data.ReadBytes(2);
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.Magic = Encoding.ASCII.GetString(magic);
|
|
|
|
|
obj.LastPageBytes = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.Pages = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.RelocationItems = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.HeaderParagraphSize = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.MinimumExtraParagraphs = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.MaximumExtraParagraphs = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.InitialSSValue = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.InitialSPValue = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.Checksum = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.InitialIPValue = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.InitialCSValue = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.RelocationTableAddr = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.OverlayNumber = data.ReadUInt16LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// If we don't have enough data for PE extensions
|
|
|
|
|
if (data.Position >= data.Length || data.Length - data.Position < 36)
|
2024-12-16 23:08:45 -05:00
|
|
|
return obj;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
|
|
|
|
#region PE Extensions
|
|
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.Reserved1 = new ushort[4];
|
|
|
|
|
for (int i = 0; i < obj.Reserved1.Length; i++)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.Reserved1[i] = data.ReadUInt16LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
2026-01-25 16:15:05 -05:00
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.OEMIdentifier = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.OEMInformation = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.Reserved2 = new ushort[10];
|
|
|
|
|
for (int i = 0; i < obj.Reserved2.Length; i++)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.Reserved2[i] = data.ReadUInt16LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
2026-01-25 16:15:05 -05:00
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.NewExeHeaderAddr = data.ReadUInt32LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
return obj;
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// Parse a Stream into an ExecutableHeader
|
2024-04-03 20:55:02 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// <returns>Filled ExecutableHeader on success, null on error</returns>
|
|
|
|
|
public static RelocationEntry ParseRelocationEntry(Stream data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-12-16 23:08:45 -05:00
|
|
|
var obj = new RelocationEntry();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
obj.Offset = data.ReadUInt16LittleEndian();
|
|
|
|
|
obj.Segment = data.ReadUInt16LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
return obj;
|
2024-04-03 17:27:08 -04:00
|
|
|
}
|
2023-09-08 22:16:47 -04:00
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|