Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/NewExecutable.cs

155 lines
4.7 KiB
C#
Raw Normal View History

using System;
2022-12-02 15:44:33 -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 NewExecutable : WrapperBase<SabreTools.Models.NewExecutable.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 => "New Executable (NE)";
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 NewExecutable(SabreTools.Models.NewExecutable.Executable model, byte[] data, int offset)
#else
public NewExecutable(SabreTools.Models.NewExecutable.Executable? model, byte[]? data, int offset)
#endif
: base(model, data, offset)
{
// All logic is handled by the base class
}
/// <inheritdoc/>
#if NET48
public NewExecutable(SabreTools.Models.NewExecutable.Executable model, Stream data)
#else
public NewExecutable(SabreTools.Models.NewExecutable.Executable? model, Stream? data)
#endif
: base(model, data)
{
// All logic is handled by the base class
}
2022-12-02 15:20:44 -08:00
/// <summary>
/// Create an NE 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 NE 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 NewExecutable Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static NewExecutable? 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 NE executable from a Stream
/// </summary>
/// <param name="data">Stream representing the executable</param>
/// <returns>An NE 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 NewExecutable Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static NewExecutable? 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.NewExecutable().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 NewExecutable(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.NewExecutable.Print(builder, this.Model);
2023-01-13 14:04:21 -08:00
return builder;
2022-12-02 21:20:52 -08:00
}
2022-12-02 20:09:55 -08:00
#endregion
2022-12-03 21:37:05 -08:00
#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
2022-12-03 21:37:05 -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
2022-12-03 21:37:05 -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
2022-12-03 21:37:05 -08:00
length = _byteArrayData.Length - _byteArrayOffset;
2023-09-13 00:08:11 -04:00
#else
length = _byteArrayData!.Length - _byteArrayOffset;
#endif
2022-12-03 21:37:05 -08:00
break;
case DataSource.Stream:
2023-09-13 00:08:11 -04:00
#if NET48
2022-12-03 21:37:05 -08:00
length = (int)_streamData.Length;
2023-09-13 00:08:11 -04:00
#else
length = (int)_streamData!.Length;
#endif
2022-12-03 21:37:05 -08:00
break;
}
}
return ReadFromDataSource(rangeStart, length);
}
#endregion
2022-12-02 15:20:44 -08:00
}
}