mirror of
https://github.com/aaru-dps/Aaru.git
synced 2026-07-08 18:16:24 +00:00
Add RarStream class for RAR decompression support
This commit is contained in:
@@ -66,6 +66,7 @@
|
||||
<Compile Include="Pak\CrushStream.cs"/>
|
||||
<Compile Include="Pak\DistillStream.cs"/>
|
||||
<Compile Include="PmarcStream.cs"/>
|
||||
<Compile Include="RarStream.cs"/>
|
||||
<Compile Include="TeleDiskLzh.cs"/>
|
||||
<Compile Include="cuetools.net/CUETools.Codecs/*.cs"/>
|
||||
<Compile Include="cuetools.net/CUETools.Codecs/CommandLine/*.cs"/>
|
||||
@@ -76,13 +77,13 @@
|
||||
<Compile Include="cuetools.net/CUETools.Codecs.Flake/*.cs"/>
|
||||
<Compile Include="cuetools.net/CUETools.Codecs.Flake/Properties/*.cs"/>
|
||||
<Compile Include="XZ.cs"/>
|
||||
<Compile Include="Zip\Deflate64Stream.cs" />
|
||||
<Compile Include="Zip\Deflate64Stream.cs"/>
|
||||
<Compile Include="Zip\ImplodeStream.cs"/>
|
||||
<Compile Include="Zip\JpegStream.cs" />
|
||||
<Compile Include="Zip\PpmdStream.cs" />
|
||||
<Compile Include="Zip\JpegStream.cs"/>
|
||||
<Compile Include="Zip\PpmdStream.cs"/>
|
||||
<Compile Include="Zip\ReduceStream.cs"/>
|
||||
<Compile Include="Zip\ShrinkStream.cs"/>
|
||||
<Compile Include="Zip\WavpackStream.cs" />
|
||||
<Compile Include="Zip\WavpackStream.cs"/>
|
||||
<Compile Include="ZSTD.cs"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
127
Aaru.Compression/RarStream.cs
Normal file
127
Aaru.Compression/RarStream.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// ReSharper disable LocalizableElement
|
||||
|
||||
namespace Aaru.Compression;
|
||||
|
||||
public partial class RarStream : Stream
|
||||
{
|
||||
readonly byte[] _decoded;
|
||||
readonly long _length;
|
||||
long _position;
|
||||
|
||||
public RarStream(Stream compressedStream, long decompressedLength, int method, nint windowSize = 0)
|
||||
{
|
||||
if(compressedStream == null) throw new ArgumentNullException(nameof(compressedStream));
|
||||
if(!compressedStream.CanRead) throw new ArgumentException("Stream must be readable", nameof(compressedStream));
|
||||
if(decompressedLength < 0) throw new ArgumentOutOfRangeException(nameof(decompressedLength));
|
||||
|
||||
// Read full compressed data into memory
|
||||
compressedStream.Position = 0;
|
||||
var inBuf = new byte[compressedStream.Length];
|
||||
compressedStream.ReadExactly(inBuf, 0, inBuf.Length);
|
||||
|
||||
// Allocate output buffer
|
||||
_decoded = new byte[decompressedLength];
|
||||
var outLen = (nint)decompressedLength;
|
||||
|
||||
// Call native decompressor
|
||||
int err = method switch
|
||||
{
|
||||
15 => rar15_decompress(inBuf, inBuf.Length, _decoded, ref outLen),
|
||||
20 => rar20_decompress(inBuf, inBuf.Length, _decoded, ref outLen),
|
||||
29 => rar30_decompress(inBuf, inBuf.Length, _decoded, ref outLen),
|
||||
50 => rar50_decompress(inBuf, inBuf.Length, _decoded, ref outLen, windowSize),
|
||||
_ => throw new ArgumentException("Invalid method")
|
||||
};
|
||||
|
||||
if(err != 0) throw new InvalidOperationException("RAR decompression failed");
|
||||
|
||||
// Adjust actual length in case it differs
|
||||
_length = outLen;
|
||||
_position = 0;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => true;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => _length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _position;
|
||||
set => Seek(value, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
[LibraryImport("libAaru.Compression.Native")]
|
||||
public static partial int rar15_decompress(byte[] in_buf, nint in_len, byte[] out_buf, ref nint out_len);
|
||||
|
||||
[LibraryImport("libAaru.Compression.Native")]
|
||||
public static partial int rar20_decompress(byte[] in_buf, nint in_len, byte[] out_buf, ref nint out_len);
|
||||
|
||||
[LibraryImport("libAaru.Compression.Native")]
|
||||
public static partial int rar30_decompress(byte[] in_buf, nint in_len, byte[] out_buf, ref nint out_len);
|
||||
|
||||
[LibraryImport("libAaru.Compression.Native")]
|
||||
public static partial int rar50_decompress(byte[] in_buf, nint in_len, byte[] out_buf, ref nint out_len,
|
||||
nint window_size);
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads up to <paramref name="count" /> bytes from the decompressed buffer
|
||||
/// into <paramref name="buffer" />, starting at <paramref name="offset" />.
|
||||
/// </summary>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if(buffer == null) throw new ArgumentNullException(nameof(buffer));
|
||||
if(offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
if(count < 0) throw new ArgumentOutOfRangeException(nameof(count));
|
||||
if(offset + count > buffer.Length) throw new ArgumentException("offset+count exceeds buffer length");
|
||||
|
||||
long remaining = _length - _position;
|
||||
|
||||
if(remaining <= 0) return 0;
|
||||
|
||||
var toRead = (int)Math.Min(count, remaining);
|
||||
Array.Copy(_decoded, _position, buffer, offset, toRead);
|
||||
_position += toRead;
|
||||
|
||||
return toRead;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current position within the decompressed buffer.
|
||||
/// </summary>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
long newPos = origin switch
|
||||
{
|
||||
SeekOrigin.Begin => offset,
|
||||
SeekOrigin.Current => _position + offset,
|
||||
SeekOrigin.End => _length + offset,
|
||||
_ => throw new ArgumentException("Invalid SeekOrigin", nameof(origin))
|
||||
};
|
||||
|
||||
if(newPos < 0 || newPos > _length) throw new IOException("Attempt to seek outside the buffer");
|
||||
|
||||
_position = newPos;
|
||||
|
||||
return _position;
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException("Cannot resize decompressed buffer");
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException("Stream is read-only");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user