135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StgSharp.Data
|
|
{
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public unsafe struct SSDSegment
|
|
{
|
|
|
|
[FieldOffset(16)] public fixed byte Data[16 * 16];
|
|
[FieldOffset(12)] public int DataHash;
|
|
[FieldOffset(0 * 16)] public SSDSegmentHead Head;
|
|
[FieldOffset(8)] public int PreviousHash;
|
|
|
|
public SSDSegment(int previousHash)
|
|
{
|
|
this.PreviousHash = previousHash;
|
|
}
|
|
|
|
public int ID
|
|
{
|
|
get { return Head.globalID; }
|
|
set { Head.globalID = value; }
|
|
}
|
|
|
|
public byte this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index > (16 * 14) - 1)
|
|
{
|
|
throw new IndexOutOfRangeException();
|
|
}
|
|
return Data[index];
|
|
}
|
|
|
|
set
|
|
{
|
|
if (index > (16 * 14) - 1)
|
|
{
|
|
throw new IndexOutOfRangeException();
|
|
}
|
|
Data[index] = value;
|
|
}
|
|
}
|
|
|
|
public short Size => Head.size;
|
|
|
|
public void FillRandomBytes(int begin)
|
|
{
|
|
if (begin > 255)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = begin; i < 255; i++)
|
|
{
|
|
Data[i] = 0; //TODO: 使用STGSHARP内部随机数工具处理它
|
|
}
|
|
}
|
|
|
|
public byte[] GetBytes()
|
|
{
|
|
byte[] ret = new byte[16 * 18];
|
|
fixed (M128* mptr = &Head.data)
|
|
{
|
|
fixed (byte* aptr = ret)
|
|
{
|
|
Vector4* vsptr = (Vector4*)mptr;
|
|
Vector4* vtptr = (Vector4*)aptr;
|
|
for (int i = 0; i < 18; i++)
|
|
{
|
|
*(vtptr + i) = *(vsptr + i);
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public int GetDataHash()
|
|
{
|
|
Vector4 v = Vector4.One;
|
|
fixed (M128* mptr = &Head.data)
|
|
{
|
|
Vector4* vptr = (Vector4*)mptr;
|
|
for (int i = 0; i < 16; i += 1)
|
|
{
|
|
v += (*vptr) * 31;
|
|
}
|
|
}
|
|
M128 m = new M128(v);
|
|
DataHash = m.vec.GetHashCode();
|
|
return DataHash;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int ret = 17;
|
|
fixed (M128* mptr = &Head.data)
|
|
{
|
|
Vector4* vptr = (Vector4*)mptr;
|
|
for (int i = 0; i < 18; i++)
|
|
{
|
|
ret = (ret + (*vptr).GetHashCode()) * 31;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public unsafe T ReadData<T>(int index) where T : struct
|
|
{
|
|
fixed (byte* bptr = Data)
|
|
{
|
|
T* tptr = (T*)bptr;
|
|
return *(tptr + index);
|
|
}
|
|
}
|
|
public unsafe void WriteData<T>(int index, T value) where T : struct
|
|
{
|
|
fixed (byte* bptr = Data)
|
|
{
|
|
T* tptr = (T*)bptr;
|
|
*(tptr + index) = value;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|