Files
DP44/Common/DTS.Common.Serialization/SliceRaw/SliceRaw.File.BinaryDasTimestampHeader.cs
2026-04-17 14:55:32 -04:00

134 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using DTS.Common.Utilities;
using DTS.Common.Utilities.Logging;
using DTS.Common.Utils;
namespace DTS.Serialization.SliceRaw
{
public partial class File
{ ///
/// <summary>
/// Representation of the information found in a binary module timestamp header.
/// </summary>
///
public class BinaryDasTimestampHeader
: Exceptional,
IDasTimestampHeader
{
///
/// <summary>
/// "Magic Key" required for our binary file type.
/// </summary>
///
public static uint RequiredMagicKey => 0xF15363C2; //persistentchannel's backwards
public static uint CurrentVersionNumber => 0x01;
/// <summary>
/// Header version number required for our binary file type.
/// </summary>
public static List<uint> KnownHeaderVersionNumbers => new List<uint>(new uint[] { 0x01 });
/// <summary>
/// "Magic Key" relatively-UID <see cref="UInt32"/> for our binary file type.
/// </summary>
public uint MagicKey
{
get;
set;
}
/// <summary>
/// <see cref="UInt32"/> header version number for our binary file type.
/// </summary>
public uint HeaderVersionNumber
{
get;
set;
}
/// <summary>
/// Get/set offset of sample data start value.
/// </summary>
public ulong OffsetOfSampleDataStart
{
get;
set;
}
/// <summary>
/// Get/set number of samples.
/// </summary>
public ulong NumberOfSamples
{
get;
set;
}
/// <summary>
/// Get/set sample rate value.
/// </summary>
public uint NumberOfBitsPerSample
{
get;
set;
}
/// <summary>
/// Calculate the CRC32 for this binary channe header.
/// </summary>
///
/// <returns>
/// The CRC32 of the binary channel header.
/// </returns>
///
private uint CalculateCrc32()
{
try
{
var data = new List<byte>();
data.AddRange(BitConverter.GetBytes(MagicKey));
data.AddRange(BitConverter.GetBytes(HeaderVersionNumber));
data.AddRange(BitConverter.GetBytes(OffsetOfSampleDataStart));
data.AddRange(BitConverter.GetBytes(NumberOfSamples));
data.AddRange(BitConverter.GetBytes(NumberOfBitsPerSample));
if (data.Count % 2 > 0) data.Add(0x0);
var byteDataArray = data.ToArray();
//APILogger.Log($"[DTM]: data array for CRC: {BitConverter.ToString(byteDataArray)}");
ushort crc = 0xFFFF;
for (var i = 0; i < data.Count; i += 2)
crc = Utils.Math_DoCRC16Step(BitConverter.ToUInt16(byteDataArray, i), crc);
//APILogger.Log($"[DTM]: crc {crc}");
return crc;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem calculating CRC 32", ex);
}
}
/// <summary>
/// Get the CRC for the current state of the header.
/// </summary>
public uint Crc32
{
get
{
try
{
return CalculateCrc32();
}
catch (System.Exception ex)
{
throw new Exception("encountered problem generating CRC for binary channel header information", ex);
}
}
}
}
}
}