mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-27 08:39:47 +00:00
76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System.IO;
|
|
|
|
namespace SabreTools.Serialization.Wrappers
|
|
{
|
|
public class MSDOS : WrapperBase<Models.MSDOS.Executable>
|
|
{
|
|
#region Descriptive Properties
|
|
|
|
/// <inheritdoc/>
|
|
public override string DescriptionString => "MS-DOS Executable";
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
/// <inheritdoc/>
|
|
public MSDOS(Models.MSDOS.Executable? model, byte[]? data, int offset)
|
|
: base(model, data, offset)
|
|
{
|
|
// All logic is handled by the base class
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public MSDOS(Models.MSDOS.Executable? model, Stream? data)
|
|
: 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>
|
|
public static MSDOS? Create(byte[]? data, int offset)
|
|
{
|
|
// 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 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>
|
|
public static MSDOS? Create(Stream? data)
|
|
{
|
|
// If the data is invalid
|
|
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
|
|
return null;
|
|
|
|
var executable = new Streams.MSDOS().Deserialize(data);
|
|
if (executable == null)
|
|
return null;
|
|
|
|
try
|
|
{
|
|
return new MSDOS(executable, data);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |