Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/AACSMediaKeyBlock.cs

130 lines
3.6 KiB
C#
Raw Normal View History

2023-01-12 13:28:12 -08:00
using System.IO;
2023-01-13 14:04:21 -08:00
using System.Text;
using SabreTools.Models.AACS;
2023-01-12 13:28:12 -08:00
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2023-01-12 13:28:12 -08:00
{
public class AACSMediaKeyBlock : WrapperBase<MediaKeyBlock>
2023-01-12 13:28:12 -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 => "AACS Media Key Block";
2023-01-18 11:18:53 -08:00
#endregion
2023-01-12 13:28:12 -08:00
#region Pass-Through Properties
#region Records
/// <inheritdoc cref="Models.AACS.MediaKeyBlock.Records"/>
#if NET48
public Record[] Records => _model.Records;
#else
public Record?[]? Records => _model.Records;
#endif
2023-01-12 13:28:12 -08:00
#endregion
#endregion
#region Constructors
2023-09-11 23:25:09 -04:00
/// <inheritdoc/>
#if NET48
public AACSMediaKeyBlock(MediaKeyBlock model, byte[] data, int offset)
2023-09-11 23:25:09 -04:00
#else
public AACSMediaKeyBlock(MediaKeyBlock? model, byte[]? data, int offset)
2023-09-11 23:25:09 -04:00
#endif
: base(model, data, offset)
{
// All logic is handled by the base class
}
/// <inheritdoc/>
#if NET48
public AACSMediaKeyBlock(MediaKeyBlock model, Stream data)
2023-09-11 23:25:09 -04:00
#else
public AACSMediaKeyBlock(MediaKeyBlock? model, Stream? data)
2023-09-11 23:25:09 -04:00
#endif
: base(model, data)
{
// All logic is handled by the base class
}
2023-01-12 13:28:12 -08:00
/// <summary>
/// Create an AACS media key block from a byte array and offset
/// </summary>
/// <param name="data">Byte array representing the archive</param>
/// <param name="offset">Offset within the array to parse</param>
/// <returns>An AACS media key block wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-12 13:28:12 -08:00
public static AACSMediaKeyBlock Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static AACSMediaKeyBlock? Create(byte[]? data, int offset)
#endif
2023-01-12 13:28:12 -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);
return Create(dataStream);
}
/// <summary>
/// Create an AACS media key block from a Stream
/// </summary>
/// <param name="data">Stream representing the archive</param>
/// <returns>An AACS media key block wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-12 13:28:12 -08:00
public static AACSMediaKeyBlock Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static AACSMediaKeyBlock? Create(Stream? data)
#endif
2023-01-12 13:28:12 -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 mediaKeyBlock = new SabreTools.Serialization.Streams.AACS().Deserialize(data);
2023-01-12 13:28:12 -08:00
if (mediaKeyBlock == null)
return null;
2023-09-11 23:25:09 -04:00
try
{
return new AACSMediaKeyBlock(mediaKeyBlock, data);
}
catch
2023-01-12 13:28:12 -08:00
{
2023-09-11 23:25:09 -04:00
return null;
}
2023-01-12 13:28:12 -08:00
}
#endregion
#region Printing
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
2023-01-12 13:28:12 -08:00
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
Printing.AACSMediaKeyBlock.Print(builder, _model);
2023-09-13 01:15:57 -04:00
return builder;
}
2023-01-12 13:28:12 -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
2023-01-12 13:28:12 -08:00
#endregion
}
}