Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/LinearExecutable.cs

153 lines
4.7 KiB
C#
Raw Normal View History

2023-01-11 13:39:23 -08:00
using System;
using System.IO;
2023-01-13 14:04:21 -08:00
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 LinearExecutable : WrapperBase<SabreTools.Models.LinearExecutable.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 => "Linear Executable (LE/LX)";
2023-01-18 11:18:53 -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 LinearExecutable(SabreTools.Models.LinearExecutable.Executable model, byte[] data, int offset)
#else
public LinearExecutable(SabreTools.Models.LinearExecutable.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 LinearExecutable(SabreTools.Models.LinearExecutable.Executable model, Stream data)
#else
public LinearExecutable(SabreTools.Models.LinearExecutable.Executable? model, Stream? data)
#endif
: base(model, data)
{
// All logic is handled by the base class
}/// <summary>
2023-09-13 00:08:11 -04:00
/// Create an LE/LX 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 LE/LX 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 LinearExecutable Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static LinearExecutable? 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 LE/LX executable from a Stream
/// </summary>
/// <param name="data">Stream representing the executable</param>
/// <returns>An LE/LX 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 LinearExecutable Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static LinearExecutable? 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.LinearExecutable().Deserialize(data);
2022-12-02 15:20:44 -08:00
if (executable == null)
return null;
2023-09-11 23:25:09 -04:00
try
{
return new LinearExecutable(executable, data);
}
catch
{
2023-09-11 23:25:09 -04:00
return null;
}
2022-12-02 15:20:44 -08:00
}
2022-12-02 21:20:52 -08:00
#endregion
2022-12-02 20:09:55 -08:00
#region Printing
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-15 14:15:28 -04:00
Printing.LinearExecutable.Print(builder, this.Model);
2023-01-13 14:04:21 -08:00
return builder;
2023-01-11 13:39:23 -08:00
}
#endregion
#region REMOVE -- DO NOT USE
/// <summary>
/// Read an arbitrary range from the source
/// </summary>
/// <param name="rangeStart">The start of where to read data from, -1 means start of source</param>
/// <param name="length">How many bytes to read, -1 means read until end</param>
/// <returns>Byte array representing the range, null on error</returns>
[Obsolete]
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-11 13:39:23 -08:00
public byte[] ReadArbitraryRange(int rangeStart = -1, int length = -1)
2023-09-12 17:12:23 -04:00
#else
public byte[]? ReadArbitraryRange(int rangeStart = -1, int length = -1)
#endif
2023-01-11 13:39:23 -08:00
{
// If we have an unset range start, read from the start of the source
if (rangeStart == -1)
rangeStart = 0;
// If we have an unset length, read the whole source
if (length == -1)
{
switch (_dataSource)
{
case DataSource.ByteArray:
2023-09-13 00:08:11 -04:00
#if NET48
2023-01-11 13:39:23 -08:00
length = _byteArrayData.Length - _byteArrayOffset;
2023-09-13 00:08:11 -04:00
#else
length = _byteArrayData!.Length - _byteArrayOffset;
#endif
2023-01-11 13:39:23 -08:00
break;
case DataSource.Stream:
2023-09-13 00:08:11 -04:00
#if NET48
2023-01-11 13:39:23 -08:00
length = (int)_streamData.Length;
2023-09-13 00:08:11 -04:00
#else
length = (int)_streamData!.Length;
#endif
2023-01-11 13:39:23 -08:00
break;
}
}
return ReadFromDataSource(rangeStart, length);
}
2022-12-02 20:09:55 -08:00
#endregion
2022-12-02 15:20:44 -08:00
}
}