I'm pretending that I know what I'm doing

This commit is contained in:
Lani Aung
2024-09-21 22:40:15 -06:00
parent 87d60b6918
commit 2054009b97
10 changed files with 193 additions and 32 deletions
@@ -0,0 +1,81 @@
namespace fxl.codes.kisekae.data.Archives;
/**
* Ported from https://github.com/kisekae/UltraKiss/blob/master/src/Kisekae/Lhhuf.java
*/
public class LhArchiveDecoder
{
private const int Buffer = 4096;
private const int BufferSize = 60;
private const int Threshold = 2;
private const int EndOfFile = -1;
private const int NChar = 256 - Threshold + BufferSize; // For some reason????
private const int TableSize = NChar * 2 - 1;
private const int RootPosition = TableSize - 1;
private const int MaxFreq = 0x8000;
private readonly int[] _child = new int[TableSize];
private readonly int[] _frequency = new int[TableSize];
private readonly int[] _parent = new int[TableSize + NChar];
private int _bytesProcessed;
private int[] _data = new int[Buffer + 1];
private int _lastBytesProcessed;
private int[] _leftChild = new int[Buffer + 1];
private int[] _rightChild = new int[Buffer * 2 + 1];
private int[] _same = new int[Buffer + 1];
private byte[] _textBuffer = new byte[Buffer + BufferSize - 1];
private int putlen, getlen, putbuf, getbuf;
private void Initialize()
{
for (var i = 0; i < NChar; i++)
{
_frequency[i] = 1;
_child[i] = i + TableSize;
_parent[i + TableSize] = i;
}
for (int i = 0, j = NChar; j <= RootPosition; i += 2, j++)
{
_frequency[j] = _frequency[i] + _frequency[i + 1];
_child[j] = i;
_parent[i] = _parent[i + 1] = j;
}
_frequency[TableSize] = 0xffff;
_parent[RootPosition] = 0;
}
private void Reconstruct()
{
int k;
for (int i = 0, j = 0; i < TableSize; i++, j++)
{
if (_child[i] < TableSize) continue;
_frequency[j] = (_frequency[i] + 1) / 2;
_child[j] = _child[i];
}
for (int i = 0, j = NChar; j < TableSize; i += 2, j++)
{
k = i + 1;
var f = _frequency[j] = _frequency[i] + _frequency[k];
for (k = j - 1; f < _frequency[k]; k--);
k++;
for (int p = j, e = k; p > e; p--) _frequency[p] = _frequency[p - 1];
_frequency[k] = f;
for (var p = j; p > k; p--) _child[p] = _child[p - 1];
_child[k] = i;
}
/* link parents */
for (var i = 0; i < TableSize; i++)
if ((k = _child[i]) >= TableSize)
_parent[k] = i;
else
_parent[k] = _parent[k + 1] = i;
}
}
@@ -0,0 +1,34 @@
using System.Text;
namespace fxl.codes.kisekae.data.Archives;
public class LzhExtractor
{
public async void Open(Stream stream, string filename)
{
var headerSize = stream.ReadByte();
var headerSum = stream.ReadByte();
var methodBytes = new byte[5];
await stream.ReadExactlyAsync(methodBytes);
var method = Encoding.ASCII.GetString(methodBytes);
var skipSize = await ReadLittleEndian(stream);
var origSize = await ReadLittleEndian(stream);
var time = await ReadLittleEndian(stream);
var attribute = stream.ReadByte();
var level = stream.ReadByte();
}
private static async Task<int> ReadLittleEndian(Stream stream, int length = 4)
{
var buffer = new byte[length];
await stream.ReadExactlyAsync(buffer);
var x = BitConverter.ToInt32(buffer);
var b0 = (x >> 24) & 255;
var b1 = (x >> 16) & 255;
var b2 = (x >> 8) & 255;
var b3 = (x >> 0) & 255;
return (b0 << 0) + (b1 << 8) + (b2 << 16) + (b3 << 24);
}
}