daily update on 20240419 by nitload youmu, SSD merged into main stream
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="CodeCounter.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public static class CodeSeparator
|
||||
{
|
||||
|
||||
public static List<(T, ulong)> CountCodeFrequency<T>(T[] rawArray) where T : struct
|
||||
{
|
||||
Dictionary<T, ulong> frequencyCollection = new Dictionary<T, ulong>();
|
||||
|
||||
foreach (var item in rawArray)
|
||||
{
|
||||
if (frequencyCollection.ContainsKey(item))
|
||||
{
|
||||
frequencyCollection[item] += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
frequencyCollection.Add(item, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return frequencyCollection.OrderByDescending(pair => pair.Value)
|
||||
.Select(pair => (pair.Key, pair.Value)).ToList();
|
||||
}
|
||||
public static unsafe T[] SeparateCode<T>(string filePath) where T : struct
|
||||
{
|
||||
byte[] allByte = File.ReadAllBytes(filePath);
|
||||
List<T> rawList = new List<T>();
|
||||
|
||||
fixed (byte* allBytePtr = allByte)
|
||||
{
|
||||
for (int i = 0; i < allByte.Length; i += Marshal.SizeOf<T>())
|
||||
{
|
||||
T t = new T();
|
||||
t = *(T*)(allBytePtr + i);
|
||||
rawList.Add(t);
|
||||
}
|
||||
}
|
||||
return rawList.ToArray();
|
||||
}
|
||||
|
||||
public static unsafe T[] SeparateCode<T>(byte[] stream) where T : struct
|
||||
{
|
||||
List<T> rawList = new List<T>();
|
||||
|
||||
fixed (byte* allBytePtr = stream)
|
||||
{
|
||||
for (int i = 0; i < stream.Length; i += Marshal.SizeOf<T>())
|
||||
{
|
||||
T t = new T();
|
||||
t = *(T*)(allBytePtr + i);
|
||||
rawList.Add(t);
|
||||
}
|
||||
}
|
||||
return rawList.ToArray();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="EncodStream.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharpData
|
||||
{
|
||||
public class EncodeStream
|
||||
{
|
||||
|
||||
byte[] buffer;
|
||||
BitArray bufferArray;
|
||||
byte count;
|
||||
List<byte> data;
|
||||
|
||||
public EncodeStream()
|
||||
{
|
||||
count = 0;
|
||||
buffer = new byte[1];
|
||||
data = new List<byte>();
|
||||
bufferArray = new BitArray(8, true);
|
||||
}
|
||||
|
||||
public List<byte> EncodeData
|
||||
=> data;
|
||||
|
||||
public int Length => data.Count;
|
||||
|
||||
public string Output => Encoding.UTF8.GetString(data.ToArray());
|
||||
|
||||
public void WriteBits<T>(HuffmanKey<T> key) where T : struct
|
||||
{
|
||||
BitArray bit = key.GetBitArray();
|
||||
for (int i = 0; i < bit.Count; i++)
|
||||
{
|
||||
if (count == 8)
|
||||
{
|
||||
bufferArray.CopyTo(buffer, 0);
|
||||
data.Add(buffer[0]);
|
||||
count = 0;
|
||||
}
|
||||
bufferArray[count] = bit[i];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Encoder.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharpData;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public static class Encode
|
||||
{
|
||||
|
||||
public static void Freeze<T>(
|
||||
Dictionary<T, HuffmanKey<T>> indexDict
|
||||
)
|
||||
where T : struct
|
||||
{
|
||||
foreach (var key in indexDict)
|
||||
{
|
||||
key.Value.Freeze();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="HuffmanTree.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class HuffmanTree<T> where T : struct
|
||||
{
|
||||
|
||||
HuffmanNodeList<T> leaves;
|
||||
|
||||
internal HuffmanTree(HuffmanNodeList<T> nodeList)
|
||||
{
|
||||
leaves = nodeList;
|
||||
}
|
||||
|
||||
public HuffmanNodeList<T> LeafList
|
||||
=> leaves;
|
||||
|
||||
public TreeNode<T> Root
|
||||
=> leaves.Last;
|
||||
|
||||
public static HuffmanTree<T> BuildHuffmanTree(List<(T, ulong)> metaData)
|
||||
{
|
||||
if (metaData.Count <= 3)
|
||||
{
|
||||
throw new Exception("Data is too little, no need to compress");
|
||||
}
|
||||
HuffmanNodeList<T> leaves = new HuffmanNodeList<T>();
|
||||
HuffmanNodeList<T> all = new HuffmanNodeList<T>();
|
||||
|
||||
foreach (var item in metaData)
|
||||
{
|
||||
leaves.Add(new TreeNode<T>(item));
|
||||
}
|
||||
|
||||
|
||||
while (leaves.Count > 2)
|
||||
{
|
||||
TreeNode<T> node, toMove;
|
||||
if (leaves.Last._frequency < leaves.BeforeLast._frequency)
|
||||
{
|
||||
if (leaves.BeforeLast._frequency < leaves.OverLast._frequency)
|
||||
{
|
||||
//第三节点最大
|
||||
node = new TreeNode<T>(leaves.BeforeLast, leaves.Last);
|
||||
toMove = leaves.RemoveAndGetLast; all.Add(toMove);
|
||||
toMove = leaves.RemoveAndGetLast; all.Add(toMove);
|
||||
leaves.Add(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
//第二节点最大
|
||||
node = (leaves.Last._frequency < leaves.OverLast._frequency) ?
|
||||
(new TreeNode<T>(leaves.OverLast, leaves.Last)) :
|
||||
(new TreeNode<T>(leaves.Last, leaves.OverLast));
|
||||
toMove = leaves.RemoveAndGetLast; all.Add(toMove);
|
||||
toMove = leaves.RemoveAndGetBeforeLast; all.Add(toMove);
|
||||
leaves.Add(node);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (leaves.Last._frequency < leaves.OverLast._frequency)
|
||||
{
|
||||
//第三节点最大
|
||||
node = new TreeNode<T>(leaves.Last, leaves.BeforeLast);
|
||||
toMove = leaves.RemoveAndGetLast; all.Add(toMove);
|
||||
toMove = leaves.RemoveAndGetLast; all.Add(toMove);
|
||||
leaves.Add(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
//第一节点最大
|
||||
node = (leaves.BeforeLast._frequency < leaves.OverLast._frequency) ?
|
||||
(new TreeNode<T>(leaves.OverLast, leaves.BeforeLast)) :
|
||||
(new TreeNode<T>(leaves.BeforeLast, leaves.OverLast));
|
||||
toMove = leaves.RemoveAndGetBeforeLast; all.Add(toMove);
|
||||
toMove = leaves.RemoveAndGetBeforeLast; all.Add(toMove);
|
||||
leaves.Add(node);
|
||||
}
|
||||
}
|
||||
|
||||
leaves = new HuffmanNodeList<T>(leaves.OrderByDescending(pair => pair.frequency)
|
||||
.Select(pair => pair).ToList());
|
||||
}
|
||||
|
||||
TreeNode<T> lastOne = leaves[0];
|
||||
TreeNode<T> lastTwo = leaves[1];
|
||||
TreeNode<T> root = (lastOne.frequency > lastTwo.frequency) ?
|
||||
(new TreeNode<T>(lastOne, lastTwo)) :
|
||||
(new TreeNode<T>(lastTwo, lastOne));
|
||||
|
||||
all.Add(lastOne);
|
||||
all.Add(lastTwo);
|
||||
all.Add(root);
|
||||
|
||||
leaves = all;
|
||||
return new HuffmanTree<T>(all);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class HuffmanNodeList<T> : List<TreeNode<T>> where T : struct
|
||||
{
|
||||
|
||||
public HuffmanNodeList() : base()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public HuffmanNodeList(IEnumerable<TreeNode<T>> values) : base(values)
|
||||
{ }
|
||||
|
||||
public TreeNode<T> BeforeLast
|
||||
=> this[Count - 2];
|
||||
|
||||
public TreeNode<T> Last
|
||||
=> this[Count - 1];
|
||||
|
||||
public TreeNode<T> OverLast
|
||||
=> this[Count - 3];
|
||||
|
||||
public TreeNode<T> RemoveAndGetBeforeLast
|
||||
{
|
||||
get
|
||||
{
|
||||
TreeNode<T> ret = this[Count - 2];
|
||||
RemoveAt(Count - 2);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public TreeNode<T> RemoveAndGetLast
|
||||
{
|
||||
get
|
||||
{
|
||||
TreeNode<T> ret = this[Count - 1];
|
||||
RemoveAt(Count - 1);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public TreeNode<T> RemoveAndGetOverLast
|
||||
{
|
||||
get
|
||||
{
|
||||
TreeNode<T> ret = this[Count - 3];
|
||||
RemoveAt(Count - 3);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(T element, ulong f)
|
||||
{
|
||||
base.Add(new TreeNode<T>(element, f));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class TreeNode<T> where T : struct
|
||||
{
|
||||
|
||||
internal ulong _frequency;
|
||||
internal TreeNode<T> _left;
|
||||
internal TreeNode<T> _right;
|
||||
internal T? _value;
|
||||
|
||||
internal TreeNode(TreeNode<T> left, TreeNode<T> right)
|
||||
{
|
||||
_left = left;
|
||||
_right = right;
|
||||
_value = null;
|
||||
_frequency = left._frequency + right._frequency;
|
||||
}
|
||||
|
||||
public TreeNode((T, ulong) data)
|
||||
{
|
||||
_value = data.Item1;
|
||||
_frequency = data.Item2;
|
||||
|
||||
}
|
||||
|
||||
public TreeNode(T? value, ulong frequncy)
|
||||
{
|
||||
_value = value;
|
||||
_frequency = frequncy;
|
||||
|
||||
}
|
||||
|
||||
public ulong frequency => _frequency;
|
||||
|
||||
public TreeNode<T> Left => _left;
|
||||
public TreeNode<T> Right => _right;
|
||||
|
||||
public T? Value => _value;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="MappingTable.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class HuffmanTree<T> where T : struct
|
||||
{
|
||||
|
||||
public Dictionary<T, HuffmanKey<T>> BuildHuffmanDictionary()
|
||||
{
|
||||
Dictionary<T, HuffmanKey<T>> ret = new Dictionary<T, HuffmanKey<T>>();
|
||||
TreeNode<T> root = Root;
|
||||
HuffmanKey<T> key = new HuffmanKey<T>();
|
||||
key = key.SetSide(KeySide.Right);
|
||||
InternalBuildDictionary(ret, root, key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void InternalBuildDictionary(
|
||||
Dictionary<T, HuffmanKey<T>> dict,
|
||||
TreeNode<T> node,
|
||||
HuffmanKey<T> valueTemp
|
||||
)
|
||||
{
|
||||
HuffmanKey<T> nativeNode = new HuffmanKey<T>(valueTemp.bitList);
|
||||
nativeNode.AddIndexLevel();
|
||||
//检查左节点
|
||||
if (node._left.Value == null)
|
||||
{
|
||||
//左节点不是叶子节点,继续搜索
|
||||
InternalBuildDictionary(
|
||||
dict,
|
||||
node._left,
|
||||
nativeNode.SetSide(KeySide.Left));
|
||||
}
|
||||
else
|
||||
{
|
||||
//左节点是叶子节点,添加值
|
||||
dict.Add(node._left.Value.Value, nativeNode.SetSide(KeySide.Left));
|
||||
}
|
||||
//检查右节点
|
||||
if (node._right.Value == null)
|
||||
{
|
||||
//右节点不是叶子节点,继续搜索
|
||||
InternalBuildDictionary(
|
||||
dict,
|
||||
node._right,
|
||||
nativeNode.SetSide(KeySide.Right));
|
||||
}
|
||||
else
|
||||
{
|
||||
//右节点是叶子节点,添加值
|
||||
dict.Add(node._right.Value.Value, nativeNode.SetSide(KeySide.Right));
|
||||
}
|
||||
//这里检查自己是不是叶子节点
|
||||
//但是上面已经检查过了,不需要被检查
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class HuffmanKey<T> where T : struct
|
||||
{
|
||||
|
||||
internal List<bool> bitList;
|
||||
internal BitArray code;
|
||||
|
||||
public HuffmanKey()
|
||||
{
|
||||
bitList = new List<bool>();
|
||||
bitList.Add(false);
|
||||
}
|
||||
|
||||
public HuffmanKey(List<bool> former)
|
||||
{
|
||||
bitList = new List<bool>(former);
|
||||
}
|
||||
|
||||
public bool IsSupported
|
||||
{
|
||||
get
|
||||
{
|
||||
return
|
||||
(typeof(T) == typeof(byte)) ||
|
||||
(typeof(T) == typeof(uint)) ||
|
||||
(typeof(T) == typeof(ushort)) ||
|
||||
(typeof(T) == typeof(ulong)) ||
|
||||
#if NET7_0_OR_GREATER
|
||||
typeof(T) == typeof(UInt128) ||
|
||||
#endif
|
||||
(typeof(T) == typeof(M128));
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void AddIndexLevel()
|
||||
{
|
||||
bitList.Add(true);
|
||||
}
|
||||
|
||||
public void Freeze()
|
||||
{
|
||||
if (code != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bitList.RemoveAt(0);
|
||||
code = new BitArray(bitList.ToArray());
|
||||
}
|
||||
|
||||
public BitArray GetBitArray()
|
||||
{
|
||||
if (code == null)
|
||||
{
|
||||
List<bool> list = new List<bool>(bitList);
|
||||
list.RemoveAt(0);
|
||||
return new BitArray(list.ToArray());
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
public unsafe byte[] GetBytes()
|
||||
{
|
||||
byte[] bytes = new byte[(bitList.Count / 8) + 1];
|
||||
BitArray array = new BitArray(bitList.ToArray());
|
||||
array.CopyTo(bytes, 0);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public unsafe HuffmanKey<T> SetSide(KeySide side)
|
||||
{
|
||||
HuffmanKey<T> newKey = new HuffmanKey<T>(bitList);
|
||||
newKey.bitList[newKey.bitList.Count - 1] = side == KeySide.Left;
|
||||
return newKey;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string str = string.Empty;
|
||||
foreach (var bit in bitList)
|
||||
{
|
||||
str += $"{bit} ";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum KeySide
|
||||
{
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="M128.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
#if NET5_0_OR_GREATER
|
||||
using System.Runtime.Intrinsics;
|
||||
#endif
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit, Pack = 16)]
|
||||
public unsafe struct M128
|
||||
{
|
||||
[FieldOffset(0)] internal Vector4 vec;
|
||||
|
||||
internal M128(Vector4 v)
|
||||
{
|
||||
vec = v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ulong GetLongHash()
|
||||
{
|
||||
return (Read<ulong>(0) % 2147483647L) * (Read<ulong>(1) % 2147483647L);
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T Read<T>(int index) where T : struct
|
||||
{
|
||||
fixed (Vector4* p = &vec)
|
||||
{
|
||||
return *(((T*)p) + index);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string ToString(int sample)
|
||||
{
|
||||
string str = string.Empty;
|
||||
for (int i = 15; i >= 0; i--)
|
||||
{
|
||||
str += $"{Convert.ToString(this.Read<byte>(i), sample)} ";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Write<T>(int index, T value) where T : struct
|
||||
#if NET8_0_OR_GREATER
|
||||
, INumber
|
||||
#endif
|
||||
{
|
||||
fixed (Vector4* p = &vec)
|
||||
{
|
||||
*(((T*)p) + index) = value;
|
||||
}
|
||||
}
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<byte>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, byte>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<sbyte>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, sbyte>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<short>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, short>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<ushort>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, ushort>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<int>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, int>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<uint>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, uint>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<long>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, long>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<ulong>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, ulong>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<float>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, float>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Vector128<double>(M128 m)
|
||||
{
|
||||
return Vector128.As<float, double>(m.vec.AsVector128());
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="DataHead.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
public struct SSDSegmentHead
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal M128 data;
|
||||
[FieldOffset(12)] internal byte size;
|
||||
|
||||
[FieldOffset(0)] public int globalID;
|
||||
[FieldOffset(4)] public int nextID;
|
||||
|
||||
|
||||
[FieldOffset(8)] public int previousHash;
|
||||
[FieldOffset(13)] public byte selfDefine_1;
|
||||
[FieldOffset(14)] public byte selfDefine_2;
|
||||
[FieldOffset(15)] public byte selfDefine_3;
|
||||
|
||||
public int Size
|
||||
{
|
||||
get => ((int)size) + 1;
|
||||
set
|
||||
{
|
||||
if ((value > 256) || (value < 0))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
size = (byte)(value - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe byte[] GetBytes()
|
||||
{
|
||||
byte[] ret = new byte[16];
|
||||
fixed (Vector4* vptr = &data.vec)
|
||||
{
|
||||
byte* bptr = (byte*)vptr;
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
ret[i] = *bptr;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public unsafe class SSDrawHead : IEnumerable<int>
|
||||
{
|
||||
|
||||
private List<int> freeSegments;
|
||||
private string name;
|
||||
|
||||
private List<(int, int)> segmentRange;
|
||||
internal SSDSegmentHead _head;
|
||||
|
||||
internal SSDrawHead(string name)
|
||||
{
|
||||
int hash;
|
||||
this.name = name;
|
||||
using (SHA256 sha256Hash = SHA256.Create())
|
||||
{
|
||||
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(name));
|
||||
|
||||
hash = BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
_head.previousHash = hash;
|
||||
segmentRange = new List<(int, int)>();
|
||||
freeSegments = new List<int>();
|
||||
}
|
||||
|
||||
public SSDrawHead()
|
||||
{
|
||||
segmentRange = new List<(int, int)>();
|
||||
freeSegments = new List<int>();
|
||||
}
|
||||
|
||||
public void AddSingleSegment(int id)
|
||||
{
|
||||
freeSegments.Add(id);
|
||||
}
|
||||
|
||||
public unsafe byte[] GetBytes(out int size)
|
||||
{
|
||||
byte[] namecode = Encoding.UTF8.GetBytes(name);
|
||||
size = 16 + 4 +
|
||||
(segmentRange.Count * 8) +
|
||||
(freeSegments.Count * 4) +
|
||||
namecode.Length;
|
||||
byte[] ret = new byte[size];
|
||||
fixed (byte* bptr = ret)
|
||||
{
|
||||
*(SSDSegmentHead*)bptr = _head;
|
||||
*(bptr + 16) = (byte)segmentRange.Count;
|
||||
*(bptr + 17) = (byte)namecode.Length;
|
||||
*(bptr + 18) = (byte)freeSegments.Count;
|
||||
int t = 20;
|
||||
for (int i = 0; i < segmentRange.Count; i++, t += 8)
|
||||
{
|
||||
*(int*)(bptr + t) = segmentRange[i].Item1;
|
||||
*(int*)(bptr + t + 4) = segmentRange[i].Item2;
|
||||
}
|
||||
for (int i = 0; i < freeSegments.Count; i++, t += 4)
|
||||
{
|
||||
*(int*)(bptr + t) = freeSegments[i];
|
||||
}
|
||||
for (int i = 0; i < namecode.Length; i++, t++)
|
||||
{
|
||||
*(bptr + t) = namecode[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void WriteRange(int begin, int end)
|
||||
{
|
||||
segmentRange.Add((begin, end));
|
||||
}
|
||||
|
||||
internal void MarkSegment(int id)
|
||||
{
|
||||
freeSegments.Add(id);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
foreach (var tuple in segmentRange)
|
||||
{
|
||||
for (int i = tuple.Item1; i <= tuple.Item2; i++)
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in freeSegments)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<int> IEnumerable<int>.GetEnumerator()
|
||||
{
|
||||
foreach (var tuple in segmentRange)
|
||||
{
|
||||
for (int i = tuple.Item1; i <= tuple.Item2; i++)
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in freeSegments)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="SSD.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data.FileIO;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class SSD
|
||||
{
|
||||
|
||||
private SSDFolder _root;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="SSDVirtualFolder.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data.FileIO;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public class SSDFolder: SSDSerializable
|
||||
{
|
||||
internal string _name;
|
||||
internal int _folderID;
|
||||
internal List<SSDraw> files;
|
||||
internal List<SSDFolder> folders;
|
||||
|
||||
public SSDFolder(string name)
|
||||
{
|
||||
if (name == "root")
|
||||
{
|
||||
throw new ArgumentNullException("Name cannot be root");
|
||||
}
|
||||
_name = name;
|
||||
folders = new List<SSDFolder>();
|
||||
|
||||
}
|
||||
|
||||
public override byte SSDTypeCode => SerializableTypeCode.VirtualFolder;
|
||||
|
||||
public override byte[] GetBytes()
|
||||
{
|
||||
List<byte> rawBytes =new List<byte>();
|
||||
byte[] size = new byte[5];
|
||||
List<int> fileid = new List<int>();
|
||||
fileid.Add(4);
|
||||
foreach (var item in files)
|
||||
{
|
||||
fileid.Add(item.)
|
||||
}
|
||||
|
||||
|
||||
return rawBytes.ToArray();
|
||||
}
|
||||
|
||||
public override byte[] GetBytes(out int length)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal override void BuildFromByteStream(byte[] stream)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="SSDraw.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.Data.FileIO
|
||||
{
|
||||
public class SSDraw
|
||||
{
|
||||
|
||||
public const string FileType = "ssdraw";
|
||||
|
||||
public const int SegmentCapacity = 16 * 16;
|
||||
|
||||
private SSDrawHead _head;
|
||||
private string _name;
|
||||
private SSDSegment currentSegment;
|
||||
private int previousHash;
|
||||
private List<SSDSegment> segments;
|
||||
|
||||
public int ID
|
||||
{
|
||||
get { return _head._head.globalID; }
|
||||
}
|
||||
|
||||
public SSDraw(string name)
|
||||
{
|
||||
_name = name;
|
||||
segments = new List<SSDSegment>();
|
||||
}
|
||||
|
||||
public void CreateRawFile(string outputFolder, bool rewrite)
|
||||
{
|
||||
string route =
|
||||
$"{(((outputFolder == string.Empty) ? " " : ($"{outputFolder}\\")))}{($"{_name}.{FileType}")}";
|
||||
if (File.Exists(route) && !rewrite)
|
||||
{
|
||||
throw new Exception("This SSDRAW has already existed.");
|
||||
}
|
||||
using (File.Create(route)) { }
|
||||
using (File.Open(route, FileMode.Truncate)) { }
|
||||
FileStream f = File.Open(route, FileMode.Open);
|
||||
int size = 0;
|
||||
f.Write(_head.GetBytes(out size), 0, size);
|
||||
foreach (var _head in segments)
|
||||
{
|
||||
f.Write(_head.GetBytes(), 0, 18 * 16);
|
||||
}
|
||||
f.Close();
|
||||
}
|
||||
|
||||
public static SSDraw readRaw(string route)
|
||||
{
|
||||
string[] routeFolders = route.Split('\\');
|
||||
if (!routeFolders.Last().Contains($".{FileType}"))
|
||||
{
|
||||
throw new Exception($"File to open is not a {FileType} file.");
|
||||
}
|
||||
|
||||
FileStream stream = File.Open(route, FileMode.Open);
|
||||
return new SSDraw(string.Empty);
|
||||
}
|
||||
|
||||
public void WriteRaw(byte[] dataStream)
|
||||
{
|
||||
previousHash = BuildRawHead(0);
|
||||
currentSegment = new SSDSegment(previousHash);
|
||||
int t = 0;
|
||||
for (int i = 0; i < dataStream.Length; i++, t++)
|
||||
{
|
||||
if (t == SegmentCapacity)
|
||||
{
|
||||
currentSegment.GetDataHash();
|
||||
currentSegment.Head.Size = 256;
|
||||
currentSegment.Head.globalID = i;
|
||||
segments.Add(currentSegment);
|
||||
previousHash = currentSegment.GetHashCode();
|
||||
currentSegment = new SSDSegment(previousHash);
|
||||
t = 0;
|
||||
}
|
||||
currentSegment[i] = dataStream[i];
|
||||
}
|
||||
currentSegment.FillRandomBytes(t);
|
||||
currentSegment.GetDataHash();
|
||||
currentSegment.Head.Size = t;
|
||||
segments.Add(currentSegment);
|
||||
|
||||
}
|
||||
|
||||
private int BuildRawHead(int beginID)
|
||||
{
|
||||
SSDrawHead rawHead = new SSDrawHead(_name);
|
||||
_head = rawHead;
|
||||
return rawHead.GetHashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[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 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="BitArray.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public static partial class Serializer
|
||||
{
|
||||
|
||||
public static void Deserialize(byte[] stream, out BitArray target)
|
||||
{
|
||||
List<bool> boolStream = new List<bool>(4 * stream.Length);
|
||||
int i = 0, count = 0;
|
||||
foreach (byte b in stream)
|
||||
{
|
||||
uint raw = b;
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
switch (raw & 0b11)
|
||||
{
|
||||
case (uint)BoolMap.True_0:
|
||||
boolStream.Add(true);
|
||||
break;
|
||||
case (uint)BoolMap.False_0:
|
||||
boolStream.Add(false);
|
||||
break;
|
||||
case (uint)BoolMap.None_0:
|
||||
goto end;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
raw >>= 2;
|
||||
count++;
|
||||
continue;
|
||||
end:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (boolStream.Count > count)
|
||||
{
|
||||
boolStream.RemoveAt(count);
|
||||
}
|
||||
target = new BitArray(boolStream.ToArray());
|
||||
}
|
||||
public static byte[] Serialize(BitArray bits)
|
||||
{
|
||||
int size = (bits.Count / 4) + 1;
|
||||
List<byte> ret = new List<byte>(size);
|
||||
int count = 0, total = 0; byte temp = new byte();
|
||||
for (int i = 0; i < bits.Count; i++)
|
||||
{
|
||||
if (count == 4)
|
||||
{
|
||||
ret.Add(temp);
|
||||
count = 0;
|
||||
temp = 0;
|
||||
total++;
|
||||
}
|
||||
switch (count)
|
||||
{
|
||||
case 0:
|
||||
temp |= (byte)(bits[i] ? BoolMap.True_0 : BoolMap.False_0);
|
||||
break;
|
||||
case 1:
|
||||
temp |= (byte)(bits[i] ? BoolMap.True_1 : BoolMap.False_1);
|
||||
break;
|
||||
case 2:
|
||||
temp |= (byte)(bits[i] ? BoolMap.True_2 : BoolMap.False_2);
|
||||
break;
|
||||
case 3:
|
||||
temp |= (byte)(bits[i] ? BoolMap.True_3 : BoolMap.False_3);
|
||||
break;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
count++;
|
||||
}
|
||||
for (; count < 4; count++)
|
||||
{
|
||||
switch (count)
|
||||
{
|
||||
case 1:
|
||||
temp |= (byte)BoolMap.None_1;
|
||||
break;
|
||||
case 2:
|
||||
temp |= (byte)BoolMap.None_2;
|
||||
break;
|
||||
case 3:
|
||||
temp |= (byte)BoolMap.None_3;
|
||||
break;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ret.Add(temp);
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
internal enum BoolMap : byte
|
||||
{
|
||||
False_0 = 0b00000010,
|
||||
True_0 = 0b00000001,
|
||||
None_0 = 0b00000011,
|
||||
False_1 = 0b00001000,
|
||||
True_1 = 0b00000100,
|
||||
None_1 = 0b00001100,
|
||||
False_2 = 0b00100000,
|
||||
True_2 = 0b00010000,
|
||||
None_2 = 0b00110000,
|
||||
False_3 = 0b10000000,
|
||||
True_3 = 0b01000000,
|
||||
None_3 = 0b11000000,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Define.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A class contains a sets of methods to serialize several types
|
||||
/// </summary>
|
||||
public static partial class Serializer
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Dictionary.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A class contains a sets of methods to serialize several types
|
||||
/// </summary>
|
||||
public static partial class Serializer
|
||||
{
|
||||
/**/
|
||||
|
||||
public static void Deserialize(byte[] data, out Dictionary<byte, BitArray> dict)
|
||||
{
|
||||
dict = new Dictionary<byte, BitArray>();
|
||||
int stat = 0; byte key = 0;
|
||||
byte[] raw = new byte[0];
|
||||
foreach (var b in data)
|
||||
{
|
||||
switch (stat)
|
||||
{
|
||||
case 0: //getting key
|
||||
if (raw.Length != 0)
|
||||
{
|
||||
BitArray bits;
|
||||
Deserialize(raw, out bits);
|
||||
dict.Add(key, bits);
|
||||
}
|
||||
key = b;
|
||||
stat -= 1;
|
||||
break;
|
||||
case -1: //getting length
|
||||
raw = new byte[b];
|
||||
stat = b;
|
||||
break;
|
||||
default:
|
||||
raw[raw.Length - stat] = b;
|
||||
stat--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**/
|
||||
public static byte[] Serialize(Dictionary<byte, BitArray> table)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
foreach (var pair in table)
|
||||
{
|
||||
stream.WriteByte(pair.Key);
|
||||
|
||||
byte[] keyCode = Serialize(pair.Value);
|
||||
stream.WriteByte((byte)keyCode.Length);
|
||||
|
||||
stream.Write(keyCode, 0, keyCode.Length);
|
||||
}
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
public abstract class SSDSerializable
|
||||
{
|
||||
public abstract byte SSDTypeCode { get; }
|
||||
|
||||
public abstract byte[] GetBytes();
|
||||
|
||||
public abstract byte[] GetBytes(out int length);
|
||||
|
||||
internal abstract void BuildFromByteStream(byte[] stream);
|
||||
}
|
||||
|
||||
public class SerializableTypeCode
|
||||
{
|
||||
public const byte IndexTable = 1;
|
||||
public const byte Image = 2;
|
||||
public const byte Audio = 3;
|
||||
public const byte Shader = 4;
|
||||
public const byte Text = 5;
|
||||
public const byte RawData = 6;
|
||||
public const byte VirtualFolder = 7;
|
||||
}
|
||||
|
||||
public static partial class Serializer
|
||||
{
|
||||
public static byte[] Serialize(SSDSerializable serializableObject)
|
||||
{
|
||||
return serializableObject.GetBytes();
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(byte[] stream)where T : SSDSerializable
|
||||
{
|
||||
T serializableObject = (T)Activator.CreateInstance<T>();
|
||||
serializableObject.BuildFromByteStream(stream);
|
||||
return serializableObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user