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
{ ///
///
/// Representation of the information found in a binary module timestamp header.
///
///
public class BinaryDasTimestampHeader
: Exceptional,
IDasTimestampHeader
{
///
///
/// "Magic Key" required for our binary file type.
///
///
public static uint RequiredMagicKey => 0xF15363C2; //persistentchannel's backwards
public static uint CurrentVersionNumber => 0x01;
///
/// Header version number required for our binary file type.
///
public static List KnownHeaderVersionNumbers => new List(new uint[] { 0x01 });
///
/// "Magic Key" relatively-UID for our binary file type.
///
public uint MagicKey
{
get;
set;
}
///
/// header version number for our binary file type.
///
public uint HeaderVersionNumber
{
get;
set;
}
///
/// Get/set offset of sample data start value.
///
public ulong OffsetOfSampleDataStart
{
get;
set;
}
///
/// Get/set number of samples.
///
public ulong NumberOfSamples
{
get;
set;
}
///
/// Get/set sample rate value.
///
public uint NumberOfBitsPerSample
{
get;
set;
}
///
/// Calculate the CRC32 for this binary channe header.
///
///
///
/// The CRC32 of the binary channel header.
///
///
private uint CalculateCrc32()
{
try
{
var data = new List();
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);
}
}
///
/// Get the CRC for the current state of the header.
///
public uint Crc32
{
get
{
try
{
return CalculateCrc32();
}
catch (System.Exception ex)
{
throw new Exception("encountered problem generating CRC for binary channel header information", ex);
}
}
}
}
}
}