Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/MSDOS.cs

268 lines
8.2 KiB
C#
Raw Normal View History

2023-01-13 14:04:21 -08:00
using System.IO;
using System.Text;
2022-12-02 15:20:44 -08:00
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2022-12-02 15:20:44 -08:00
{
2023-09-11 23:25:09 -04:00
public class MSDOS : WrapperBase<SabreTools.Models.MSDOS.Executable>
2022-12-02 15:20:44 -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 => "MS-DOS Executable";
2023-01-18 11:18:53 -08:00
#endregion
2022-12-02 15:20:44 -08:00
#region Pass-Through Properties
2022-12-02 15:29:10 -08:00
#region Header
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.Magic"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public string Magic => _model.Header.Magic;
2023-09-12 17:12:23 -04:00
#else
2023-09-13 00:08:11 -04:00
public string? Magic => _model.Header?.Magic;
2023-09-12 17:12:23 -04:00
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.LastPageBytes"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort LastPageBytes => _model.Header.LastPageBytes;
2023-09-13 00:08:11 -04:00
#else
public ushort? LastPageBytes => _model.Header?.LastPageBytes;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.Pages"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort Pages => _model.Header.Pages;
2023-09-13 00:08:11 -04:00
#else
public ushort? Pages => _model.Header?.Pages;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.RelocationItems"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort RelocationItems => _model.Header.RelocationItems;
2023-09-13 00:08:11 -04:00
#else
public ushort? RelocationItems => _model.Header?.RelocationItems;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.HeaderParagraphSize"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort HeaderParagraphSize => _model.Header.HeaderParagraphSize;
2023-09-13 00:08:11 -04:00
#else
public ushort? HeaderParagraphSize => _model.Header?.HeaderParagraphSize;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.MinimumExtraParagraphs"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort MinimumExtraParagraphs => _model.Header.MinimumExtraParagraphs;
2023-09-13 00:08:11 -04:00
#else
public ushort? MinimumExtraParagraphs => _model.Header?.MinimumExtraParagraphs;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.MaximumExtraParagraphs"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort MaximumExtraParagraphs => _model.Header.MaximumExtraParagraphs;
2023-09-13 00:08:11 -04:00
#else
public ushort? MaximumExtraParagraphs => _model.Header?.MaximumExtraParagraphs;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.InitialSSValue"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort InitialSSValue => _model.Header.InitialSSValue;
2023-09-13 00:08:11 -04:00
#else
public ushort? InitialSSValue => _model.Header?.InitialSSValue;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.InitialSPValue"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort InitialSPValue => _model.Header.InitialSPValue;
2023-09-13 00:08:11 -04:00
#else
public ushort? InitialSPValue => _model.Header?.InitialSPValue;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.Checksum"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort Checksum => _model.Header.Checksum;
2023-09-13 00:08:11 -04:00
#else
public ushort? Checksum => _model.Header?.Checksum;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.InitialIPValue"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort InitialIPValue => _model.Header.InitialIPValue;
2023-09-13 00:08:11 -04:00
#else
public ushort? InitialIPValue => _model.Header?.InitialIPValue;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.InitialCSValue"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort InitialCSValue => _model.Header.InitialCSValue;
2023-09-13 00:08:11 -04:00
#else
public ushort? InitialCSValue => _model.Header?.InitialCSValue;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.RelocationTableAddr"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort RelocationTableAddr => _model.Header.RelocationTableAddr;
2023-09-13 00:08:11 -04:00
#else
public ushort? RelocationTableAddr => _model.Header?.RelocationTableAddr;
#endif
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.OverlayNumber"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort OverlayNumber => _model.Header.OverlayNumber;
2023-09-13 00:08:11 -04:00
#else
public ushort? OverlayNumber => _model.Header?.OverlayNumber;
#endif
2022-12-02 15:20:44 -08:00
#endregion
#region PE Extensions
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.Reserved1"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort[] Reserved1 => _model.Header.Reserved1;
2023-09-12 17:12:23 -04:00
#else
2023-09-13 00:08:11 -04:00
public ushort[]? Reserved1 => _model.Header?.Reserved1;
2023-09-12 17:12:23 -04:00
#endif
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.OEMIdentifier"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort OEMIdentifier => _model.Header.OEMIdentifier;
2023-09-13 00:08:11 -04:00
#else
public ushort? OEMIdentifier => _model.Header?.OEMIdentifier;
#endif
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.OEMInformation"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort OEMInformation => _model.Header.OEMInformation;
2023-09-13 00:08:11 -04:00
#else
public ushort? OEMInformation => _model.Header?.OEMInformation;
#endif
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.Reserved2"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public ushort[] Reserved2 => _model.Header.Reserved2;
2023-09-12 17:12:23 -04:00
#else
2023-09-13 00:08:11 -04:00
public ushort[]? Reserved2 => _model.Header?.Reserved2;
2023-09-12 17:12:23 -04:00
#endif
/// <inheritdoc cref="Models.MSDOS.ExecutableHeader.NewExeHeaderAddr"/>
2023-09-13 00:08:11 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public uint NewExeHeaderAddr => _model.Header.NewExeHeaderAddr;
2023-09-13 00:08:11 -04:00
#else
public uint? NewExeHeaderAddr => _model.Header?.NewExeHeaderAddr;
#endif
#endregion
2022-12-02 15:29:10 -08:00
#region Relocation Table
2022-12-02 15:20:44 -08:00
2022-12-02 15:29:10 -08:00
/// <inheritdoc cref="Models.MSDOS.Executable.RelocationTable"/>
#if NET48
2023-09-11 23:25:09 -04:00
public SabreTools.Models.MSDOS.RelocationEntry[] RelocationTable => _model.RelocationTable;
#else
public SabreTools.Models.MSDOS.RelocationEntry?[]? RelocationTable => _model.RelocationTable;
#endif
2022-12-02 15:29:10 -08:00
#endregion
2022-12-02 15:20:44 -08:00
#endregion
2022-12-02 21:20:52 -08:00
#region Constructors
2023-09-11 23:25:09 -04:00
/// <inheritdoc/>
#if NET48
public MSDOS(SabreTools.Models.MSDOS.Executable model, byte[] data, int offset)
#else
public MSDOS(SabreTools.Models.MSDOS.Executable? model, byte[]? data, int offset)
#endif
: base(model, data, offset)
{
// All logic is handled by the base class
}
2022-12-02 15:20:44 -08:00
2023-09-11 23:25:09 -04:00
/// <inheritdoc/>
#if NET48
public MSDOS(SabreTools.Models.MSDOS.Executable model, Stream data)
#else
public MSDOS(SabreTools.Models.MSDOS.Executable? model, Stream? data)
#endif
: base(model, data)
{
// All logic is handled by the base class
}/// <summary>
/// Create an MS-DOS executable from a byte array and offset
/// </summary>
/// <param name="data">Byte array representing the executable</param>
/// <param name="offset">Offset within the array to parse</param>
/// <returns>An MS-DOS executable wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2022-12-02 15:20:44 -08:00
public static MSDOS Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static MSDOS? Create(byte[]? data, int offset)
#endif
2022-12-02 15:20:44 -08:00
{
2022-12-15 14:20:27 -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);
2022-12-15 12:41:08 -08:00
return Create(dataStream);
2022-12-02 15:20:44 -08:00
}
/// <summary>
/// Create an MS-DOS executable from a Stream
/// </summary>
/// <param name="data">Stream representing the executable</param>
/// <returns>An MS-DOS executable wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2022-12-02 15:20:44 -08:00
public static MSDOS Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static MSDOS? Create(Stream? data)
#endif
2022-12-02 15:20:44 -08:00
{
2022-12-15 14:20:27 -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 executable = new SabreTools.Serialization.Streams.MSDOS().Deserialize(data);
2022-12-02 15:20:44 -08:00
if (executable == null)
return null;
2023-09-11 23:25:09 -04:00
try
{
2023-09-11 23:25:09 -04:00
return new MSDOS(executable, data);
}
catch
{
return null;
}
2022-12-02 15:20:44 -08:00
}
2023-09-11 23:25:09 -04:00
2022-12-02 21:20:52 -08:00
#endregion
2022-12-02 20:09:55 -08:00
#region Printing
2023-09-11 23:25:09 -04:00
2022-12-02 21:20:52 -08:00
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
2023-09-13 22:46:46 -04:00
Printing.MSDOS.Print(builder, _model);
2023-01-13 14:04:21 -08:00
return builder;
2022-12-02 21:20:52 -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
2022-12-02 20:09:55 -08:00
#endregion
2022-12-02 15:20:44 -08:00
}
}