Files
Aaru/Aaru.Compression/LzdStream.cs

188 lines
6.0 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
2025-08-26 02:15:05 +01:00
using System.Runtime.InteropServices;
2025-08-26 02:15:05 +01:00
namespace Aaru.Compression;
2025-08-26 02:15:05 +01:00
public partial class LzdStream : Stream
{
2025-08-26 02:15:05 +01:00
private const int INPUT_CHUNK = 8192;
private const int OUTPUT_CHUNK = 8192;
2025-08-26 02:15:05 +01:00
private readonly Stream _baseStream;
private readonly byte[] _inBuffer = new byte[INPUT_CHUNK];
private readonly byte[] _outBuffer = new byte[OUTPUT_CHUNK];
private IntPtr _ctx;
private bool _disposed;
private bool _eof; // native says DONE
private bool _flushed; // whether we've sent the final empty feed
private int _outCount;
private int _outOffset;
public LzdStream(Stream compressedStream)
{
2025-08-26 02:15:05 +01:00
_baseStream = compressedStream ?? throw new ArgumentNullException(nameof(compressedStream));
_ctx = CreateLZDContext();
2025-08-26 02:15:05 +01:00
if(_ctx == IntPtr.Zero) throw new InvalidOperationException("Failed to create LZD context");
}
2025-08-26 02:15:05 +01:00
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
2025-08-26 02:15:05 +01:00
public override long Position
{
2025-08-26 02:15:05 +01:00
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
2025-08-26 02:15:05 +01:00
[LibraryImport("libAaru.Compression.Native")]
private static partial IntPtr CreateLZDContext();
2025-08-26 02:15:05 +01:00
[LibraryImport("libAaru.Compression.Native")]
private static partial void DestroyLZDContext(IntPtr ctx);
2025-08-26 02:15:05 +01:00
[LibraryImport("libAaru.Compression.Native")]
private static partial int LZD_FeedNative(IntPtr ctx, [In] byte[] inputBuffer, nuint inputSize);
2025-08-26 02:15:05 +01:00
[LibraryImport("libAaru.Compression.Native")]
private static partial int LZD_DrainNative(IntPtr ctx, [Out] byte[] outputBuffer, nuint outputCapacity,
out nuint produced);
2025-08-26 02:15:05 +01:00
public override void Flush() {}
2025-08-26 02:15:05 +01:00
public override int Read(byte[] buffer, int offset, int count)
{
if(_disposed) throw new ObjectDisposedException(nameof(LzdStream));
if(buffer == null) throw new ArgumentNullException(nameof(buffer));
if(offset < 0 || count < 0 || offset + count > buffer.Length) throw new ArgumentOutOfRangeException();
2025-11-24 03:08:01 +00:00
var totalRead = 0;
var totalOut = 0;
var iter = 0;
2025-08-26 02:15:05 +01:00
while(totalRead < count)
{
2025-08-26 02:15:05 +01:00
Debug.WriteLine($"-- LOOP iter={iter++} total_out={totalOut} --");
2025-08-26 02:15:05 +01:00
// serve leftovers first
if(_outOffset < _outCount)
{
2025-08-26 02:15:05 +01:00
int toCopy = Math.Min(count - totalRead, _outCount - _outOffset);
Buffer.BlockCopy(_outBuffer, _outOffset, buffer, offset + totalRead, toCopy);
_outOffset += toCopy;
totalRead += toCopy;
totalOut += toCopy;
2025-08-26 02:15:05 +01:00
continue;
}
2025-08-26 02:15:05 +01:00
if(_eof) break; // nothing more to decode
2025-08-26 02:15:05 +01:00
// drain from native
LZDStatus status = TryDrain(out int produced);
Debug.WriteLine($"[DRAIN] produced={produced} status={status} flushed={_flushed} eof={_eof}");
2025-08-26 02:15:05 +01:00
if(produced > 0)
{
_outCount = produced;
_outOffset = 0;
continue; // go copy them on next loop
}
2025-08-26 02:15:05 +01:00
if(status == LZDStatus.NEED_INPUT)
{
int n = _baseStream.Read(_inBuffer, 0, _inBuffer.Length);
if(n > 0)
{
2025-08-26 02:15:05 +01:00
_eof = false; // we're not done, fresh data incoming
Debug.WriteLine($"[FEED] size={n} flushed={_flushed} (real data)");
var f = (LZDStatus)LZD_FeedNative(_ctx, _inBuffer, (UIntPtr)n);
if(f == LZDStatus.ERROR) ThrowDecoderError();
2025-08-26 02:15:05 +01:00
continue;
}
2025-08-26 02:15:05 +01:00
// end of base stream: flush native once
if(!_flushed)
{
2025-08-26 02:15:05 +01:00
Debug.WriteLine($"[FEED] size=0 flushed={_flushed} (final empty feed)");
2025-11-24 03:08:01 +00:00
var f = (LZDStatus)LZD_FeedNative(_ctx, [], UIntPtr.Zero);
2025-08-26 02:15:05 +01:00
if(f == LZDStatus.ERROR) ThrowDecoderError();
_flushed = true;
Debug.WriteLine(">>> SET _flushed=true");
2025-08-26 02:15:05 +01:00
continue;
}
2025-08-26 02:15:05 +01:00
// no more to give
_eof = true;
Debug.WriteLine(">>> SET _eof=true (DONE from decoder)");
2025-08-26 02:15:05 +01:00
break;
}
if(status != LZDStatus.DONE) continue;
_eof = true;
Debug.WriteLine(">>> SET _eof=true (no more data and already flushed)");
break;
2025-08-26 02:15:05 +01:00
// if OK but no bytes, loop again
}
2025-08-26 02:15:05 +01:00
Debug.WriteLine($"TOTAL OUT={totalOut} bytes");
2025-08-26 02:15:05 +01:00
return totalRead;
}
2025-08-26 02:15:05 +01:00
private LZDStatus TryDrain(out int produced)
{
var st = (LZDStatus)LZD_DrainNative(_ctx, _outBuffer, (UIntPtr)_outBuffer.Length, out UIntPtr p);
if(st == LZDStatus.ERROR) ThrowDecoderError();
if(st == LZDStatus.DONE) _eof = true;
produced = (int)p;
2025-08-26 02:15:05 +01:00
return st;
}
2025-08-26 02:15:05 +01:00
private static void ThrowDecoderError() => throw new IOException("LZD decompression error");
2025-08-26 02:15:05 +01:00
protected override void Dispose(bool disposing)
{
if(!_disposed)
{
2025-08-26 02:15:05 +01:00
DestroyLZDContext(_ctx);
_ctx = IntPtr.Zero;
_disposed = true;
}
2025-08-26 02:15:05 +01:00
base.Dispose(disposing);
}
2025-08-26 02:15:05 +01:00
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
2025-08-26 02:15:05 +01:00
#region Nested type: LZDStatus
2025-08-26 02:15:05 +01:00
internal enum LZDStatus
{
OK = 0,
NEED_INPUT = 1,
NEED_OUTPUT = 2,
DONE = 3,
ERROR = -1
}
#endregion
}