I dunno man
This commit is contained in:
@@ -4,24 +4,38 @@ namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
internal class LZW : IAlgorithm
|
||||
{
|
||||
private static readonly Dictionary<uint, string> DecodeDictionary;
|
||||
private static readonly Dictionary<string, uint> EncodeDictionary;
|
||||
private readonly int Max;
|
||||
|
||||
static LZW()
|
||||
{
|
||||
DecodeDictionary = new Dictionary<uint, string>();
|
||||
EncodeDictionary = new Dictionary<string, uint>();
|
||||
|
||||
for (uint i = 0; i <= byte.MaxValue; i++)
|
||||
{
|
||||
var value = $"{(char)i}";
|
||||
DecodeDictionary.Add(i, value);
|
||||
EncodeDictionary.Add(value, i);
|
||||
}
|
||||
}
|
||||
|
||||
internal LZW(int max = short.MaxValue)
|
||||
{
|
||||
Max = max;
|
||||
}
|
||||
|
||||
public byte[] Decode(ReadOnlySpan<byte> stream)
|
||||
public byte[] Decode(Stream stream, int bitLength = 32)
|
||||
{
|
||||
var dictionary = DecodeDictionary.ToDictionary(x => x.Key, x => x.Value);
|
||||
uint next = byte.MaxValue + 1;
|
||||
var dictionary = new Dictionary<uint, string>();
|
||||
for (uint i = 0; i <= byte.MaxValue; i++) dictionary.Add(i, $"{(char)i}");
|
||||
|
||||
var buffer = "";
|
||||
var decoded = new List<string>();
|
||||
for (var i = 0; i < stream.Length; i += 4)
|
||||
var bitStream = new BitStream(stream);
|
||||
while (bitStream.Position < bitStream.Length)
|
||||
{
|
||||
var code = BitConverter.ToUInt32(stream[i..(i + 4)]);
|
||||
var code = bitStream.ReadBits(bitLength);
|
||||
if (!dictionary.ContainsKey(code)) dictionary[code] = buffer + buffer[0];
|
||||
|
||||
decoded.Add(dictionary[code]);
|
||||
@@ -33,17 +47,18 @@ internal class LZW : IAlgorithm
|
||||
return decoded.SelectMany(Encoding.UTF8.GetBytes).ToArray();
|
||||
}
|
||||
|
||||
public byte[] Encode(ReadOnlySpan<byte> stream)
|
||||
public byte[] Encode(Stream stream)
|
||||
{
|
||||
var next = byte.MaxValue + 1;
|
||||
var dictionary = new Dictionary<string, int>();
|
||||
for (var i = 0; i <= byte.MaxValue; i++) dictionary.Add($"{(char)i}", i);
|
||||
uint next = byte.MaxValue + 1;
|
||||
var dictionary = EncodeDictionary.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
var buffer = "";
|
||||
var encoded = new List<int>();
|
||||
foreach (var part in stream)
|
||||
var encoded = new List<uint>();
|
||||
while (true)
|
||||
{
|
||||
var charValue = (char)part;
|
||||
if (stream.Position >= stream.Length) break;
|
||||
|
||||
var charValue = (char)stream.ReadByte();
|
||||
buffer += charValue;
|
||||
if (dictionary.ContainsKey(buffer)) continue;
|
||||
if (next <= Max) dictionary.Add(buffer, next++);
|
||||
|
||||
Reference in New Issue
Block a user