[Aaru.Compression] Reformat and cleanup.

This commit is contained in:
2023-10-03 22:51:02 +01:00
parent 3afe84193b
commit 5faa49a031
10 changed files with 132 additions and 101 deletions

View File

@@ -49,6 +49,7 @@ public static class ADC
const int PLAIN = 1;
const int TWO_BYTE = 2;
const int THREE_BYTE = 3;
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true;
@@ -64,21 +65,21 @@ public static class ADC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int GetChunkSize(byte byt) => GetChunkType(byt) switch
{
PLAIN => (byt & 0x7F) + 1,
TWO_BYTE => ((byt & 0x3F) >> 2) + 3,
THREE_BYTE => (byt & 0x3F) + 4,
_ => -1
};
{
PLAIN => (byt & 0x7F) + 1,
TWO_BYTE => ((byt & 0x3F) >> 2) + 3,
THREE_BYTE => (byt & 0x3F) + 4,
_ => -1
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int GetOffset(ReadOnlySpan<byte> chunk) => GetChunkType(chunk[0]) switch
{
PLAIN => 0,
TWO_BYTE => ((chunk[0] & 0x03) << 8) + chunk[1],
THREE_BYTE => (chunk[1] << 8) + chunk[2],
_ => -1
};
{
PLAIN => 0,
TWO_BYTE => ((chunk[0] & 0x03) << 8) + chunk[1],
THREE_BYTE => (chunk[1] << 8) + chunk[2],
_ => -1
};
/// <summary>Decompresses a byte buffer that's compressed with ADC</summary>
/// <param name="source">Compressed buffer</param>
@@ -90,8 +91,8 @@ public static class ADC
if(Native.IsSupported)
return AARU_adc_decode_buffer(destination, destination.Length, source, source.Length);
int inputPosition = 0;
int outPosition = 0;
var inputPosition = 0;
var outPosition = 0;
Span<byte> temp = stackalloc byte[3];
while(inputPosition < source.Length)
@@ -130,18 +131,20 @@ public static class ADC
{
byte lastByte = destination[outPosition - 1];
for(int i = 0; i < chunkSize; i++)
for(var i = 0; i < chunkSize; i++)
{
destination[outPosition] = lastByte;
outPosition++;
}
}
else
for(int i = 0; i < chunkSize; i++)
{
for(var i = 0; i < chunkSize; i++)
{
destination[outPosition] = destination[outPosition - offset - 1];
outPosition++;
}
}
break;
case THREE_BYTE:
@@ -158,24 +161,26 @@ public static class ADC
{
byte lastByte = destination[outPosition - 1];
for(int i = 0; i < chunkSize; i++)
for(var i = 0; i < chunkSize; i++)
{
destination[outPosition] = lastByte;
outPosition++;
}
}
else
for(int i = 0; i < chunkSize; i++)
{
for(var i = 0; i < chunkSize; i++)
{
destination[outPosition] = destination[outPosition - offset - 1];
outPosition++;
}
}
break;
}
}
finished:
finished:
return outPosition;
}

View File

@@ -36,8 +36,8 @@
<NoWarn>CS1591;CS1574</NoWarn>
</PropertyGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<InternalsVisibleTo Include="Aaru.Tests" />
<InternalsVisibleTo Include="Aaru.Tests.Devices" />
<InternalsVisibleTo Include="Aaru.Tests"/>
<InternalsVisibleTo Include="Aaru.Tests.Devices"/>
</ItemGroup>
<PropertyGroup>
<NrtRevisionFormat>$(Version)+{chash:8}</NrtRevisionFormat>
@@ -50,25 +50,25 @@
</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<Compile Include="ADC.cs" />
<Compile Include="AppleRle.cs" />
<Compile Include="BZip2.cs" />
<Compile Include="FLAC.cs" />
<Compile Include="LZFSE.cs" />
<Compile Include="LZIP.cs" />
<Compile Include="LZMA.cs" />
<Compile Include="Native.cs" />
<Compile Include="NonClosableStream.cs" />
<Compile Include="TeleDiskLzh.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/CommandLine/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/CRC/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/NULL/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/ViewModel/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs/WAV/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs.Flake/*.cs" />
<Compile Include="cuetools.net/CUETools.Codecs.Flake/Properties/*.cs" />
<Compile Include="ZSTD.cs" />
<Compile Include="ADC.cs"/>
<Compile Include="AppleRle.cs"/>
<Compile Include="BZip2.cs"/>
<Compile Include="FLAC.cs"/>
<Compile Include="LZFSE.cs"/>
<Compile Include="LZIP.cs"/>
<Compile Include="LZMA.cs"/>
<Compile Include="Native.cs"/>
<Compile Include="NonClosableStream.cs"/>
<Compile Include="TeleDiskLzh.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/CommandLine/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/CRC/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/NULL/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/ViewModel/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs/WAV/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs.Flake/*.cs"/>
<Compile Include="cuetools.net/CUETools.Codecs.Flake/Properties/*.cs"/>
<Compile Include="ZSTD.cs"/>
</ItemGroup>
<ItemGroup>
<Content Include="..\LICENSE.LGPL">
@@ -76,14 +76,14 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aaru.Compression.Native" Version="6.0.0-alpha.10" />
<PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpCompress" Version="0.34.0" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all" />
<PackageReference Include="Aaru.Compression.Native" Version="6.0.0-alpha.10"/>
<PackageReference Include="DotNetZip" Version="1.16.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="SharpCompress" Version="0.34.0"/>
<PackageReference Include="System.Resources.Extensions" Version="8.0.0-rc.1.23419.4"/>
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Aaru.Helpers\Aaru.Helpers.csproj" />
<ProjectReference Include="..\Aaru.Helpers\Aaru.Helpers.csproj"/>
</ItemGroup>
</Project>

View File

@@ -39,6 +39,7 @@ namespace Aaru.Compression;
public static class AppleRle
{
const uint DART_CHUNK = 20960;
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true;
@@ -54,10 +55,10 @@ public static class AppleRle
if(Native.IsSupported)
return AARU_apple_rle_decode_buffer(destination, destination.Length, source, source.Length);
int count = 0;
bool nextA = true; // true if A, false if B
var count = 0;
var nextA = true; // true if A, false if B
byte repeatedByteA = 0, repeatedByteB = 0;
bool repeatMode = false; // true if we're repeating, false if we're just copying
var repeatMode = false; // true if we're repeating, false if we're just copying
int inPosition = 0, outPosition = 0;
while(inPosition <= source.Length &&
@@ -97,9 +98,9 @@ public static class AppleRle
while(true)
{
byte b1 = source[inPosition++];
byte b2 = source[inPosition++];
short s = (short)((b1 << 8) | b2);
byte b1 = source[inPosition++];
byte b2 = source[inPosition++];
var s = (short)(b1 << 8 | b2);
if(s == 0 ||
s >= DART_CHUNK ||
@@ -111,7 +112,7 @@ public static class AppleRle
repeatMode = true;
repeatedByteA = source[inPosition++];
repeatedByteB = source[inPosition++];
count = (-s * 2) - 1;
count = -s * 2 - 1;
nextA = false;
destination[outPosition++] = repeatedByteA;
@@ -120,7 +121,7 @@ public static class AppleRle
}
repeatMode = false;
count = (s * 2) - 1;
count = s * 2 - 1;
destination[outPosition++] = source[inPosition++];

View File

@@ -43,7 +43,7 @@ public class BZip2
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize,
int blockSize100K);
int blockSize100K);
/// <summary>Decodes a buffer compressed with BZIP2</summary>
/// <param name="source">Encoded buffer</param>
@@ -51,7 +51,7 @@ public class BZip2
/// <returns>The number of decoded bytes</returns>
public static int DecodeBuffer(byte[] source, byte[] destination)
{
uint destinationSize = (uint)destination.Length;
var destinationSize = (uint)destination.Length;
if(Native.IsSupported)
{
@@ -73,7 +73,7 @@ public class BZip2
/// <returns></returns>
public static int EncodeBuffer(byte[] source, byte[] destination, int blockSize100K)
{
uint destinationSize = (uint)destination.Length;
var destinationSize = (uint)destination.Length;
if(Native.IsSupported)
{

View File

@@ -42,16 +42,16 @@ public class FLAC
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_flac_decode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize);
nuint srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_flac_encode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize, uint blocksize, int doMidSideStereo,
int looseMidSideStereo, string apodization, uint maxLpcOrder,
uint qlpCoeffPrecision, int doQlpCoeffPrecSearch,
int doExhaustiveModelSearch, uint minResidualPartitionOrder,
uint maxResidualPartitionOrder, string applicationID,
uint applicationIDLen);
nuint srcSize, uint blocksize, int doMidSideStereo,
int looseMidSideStereo, string apodization, uint maxLpcOrder,
uint qlpCoeffPrecision, int doQlpCoeffPrecSearch,
int doExhaustiveModelSearch, uint minResidualPartitionOrder,
uint maxResidualPartitionOrder, string applicationID,
uint applicationIDLen);
/// <summary>Decodes a buffer compressed with FLAC</summary>
/// <param name="source">Encoded buffer</param>
@@ -60,8 +60,10 @@ public class FLAC
public static int DecodeBuffer(byte[] source, byte[] destination)
{
if(Native.IsSupported)
{
return (int)AARU_flac_decode_redbook_buffer(destination, (nuint)destination.Length, source,
(nuint)source.Length);
}
var flacMs = new MemoryStream(source);
var flakeReader = new AudioDecoder(new DecoderSettings(), "", flacMs);
@@ -95,6 +97,7 @@ public class FLAC
uint minResidualPartitionOrder, uint maxResidualPartitionOrder, string applicationID)
{
if(Native.IsSupported)
{
return (int)AARU_flac_encode_redbook_buffer(destination, (nuint)destination.Length, source,
(nuint)source.Length, blockSize, doMidSideStereo ? 1 : 0,
looseMidSideStereo ? 1 : 0, apodization, maxLpcOrder,
@@ -102,6 +105,7 @@ public class FLAC
doExhaustiveModelSearch ? 1 : 0, minResidualPartitionOrder,
maxResidualPartitionOrder, applicationID,
(uint)applicationID.Length);
}
var flakeWriterSettings = new EncoderSettings
{
@@ -138,7 +142,7 @@ public class FLAC
flakeWriter.Write(audioBuffer);
flakeWriter.Close();
int len = (int)flacMs.Length;
var len = (int)flacMs.Length;
flacMs.ReallyClose();
return len;

View File

@@ -52,8 +52,9 @@ public class LZFSE
public static int DecodeBuffer(byte[] source, byte[] destination) => Native.IsSupported
? (int)
AARU_lzfse_decode_buffer(destination,
(nuint)destination.Length, source,
(nuint)source.Length, null) : 0;
(nuint)destination.Length, source,
(nuint)source.Length, null)
: 0;
/// <summary>Compresses a buffer using BZIP2</summary>
/// <param name="source">Data to compress</param>
@@ -62,6 +63,7 @@ public class LZFSE
public static int EncodeBuffer(byte[] source, byte[] destination) => Native.IsSupported
? (int)
AARU_lzfse_encode_buffer(destination,
(nuint)destination.Length, source,
(nuint)source.Length, null) : 0;
(nuint)destination.Length, source,
(nuint)source.Length, null)
: 0;
}

View File

@@ -41,8 +41,8 @@ public class LZIP
static extern int AARU_lzip_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzip_encode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize,
int dictionarySize, int matchLenLimit);
static extern int AARU_lzip_encode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize,
int dictionarySize, int matchLenLimit);
/// <summary>Decodes a buffer compressed with LZIP</summary>
/// <param name="source">Encoded buffer</param>
@@ -58,6 +58,8 @@ public class LZIP
/// <param name="matchLengthLimit">Match length limit</param>
/// <returns>The size of the compressed data</returns>
public static int EncodeBuffer(byte[] source, byte[] destination, int dictionarySize, int matchLengthLimit) =>
Native.IsSupported ? AARU_lzip_encode_buffer(destination, destination.Length, source, source.Length,
dictionarySize, matchLengthLimit) : 0;
Native.IsSupported
? AARU_lzip_encode_buffer(destination, destination.Length, source, source.Length,
dictionarySize, matchLengthLimit)
: 0;
}

View File

@@ -41,12 +41,12 @@ public class LZMA
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzma_decode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer, ref nuint srcSize,
byte[] props, nuint propsSize);
byte[] props, nuint propsSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzma_encode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer, nuint srcSize,
byte[] outProps, ref nuint outPropsSize, int level, uint dictSize, int lc,
int lp, int pb, int fb, int numThreads);
int lp, int pb, int fb, int numThreads);
/// <summary>Decodes a buffer compressed with LZMA</summary>
/// <param name="source">Encoded buffer</param>
@@ -57,8 +57,8 @@ public class LZMA
{
if(Native.IsSupported)
{
nuint srcSize = (nuint)source.Length;
nuint dstSize = (nuint)destination.Length;
var srcSize = (nuint)source.Length;
var dstSize = (nuint)destination.Length;
AARU_lzma_decode_buffer(destination, ref dstSize, source, ref srcSize, properties,
(nuint)properties.Length);
@@ -85,17 +85,17 @@ public class LZMA
/// <param name="fb">Forward bits</param>
/// <returns>How many bytes have been written to the destination buffer</returns>
public static int EncodeBuffer(byte[] source, byte[] destination, out byte[] properties, int level, uint dictSize,
int lc, int lp, int pb, int fb)
int lc, int lp, int pb, int fb)
{
if(Native.IsSupported)
{
properties = new byte[5];
nuint dstSize = (nuint)destination.Length;
nuint propsSize = (nuint)properties.Length;
nuint srcSize = (nuint)source.Length;
var dstSize = (nuint)destination.Length;
var propsSize = (nuint)properties.Length;
var srcSize = (nuint)source.Length;
AARU_lzma_encode_buffer(destination, ref dstSize, source, srcSize, properties, ref propsSize, level,
dictSize, lc, lp, pb, fb, 0);
dictSize, lc, lp, pb, fb, 0);
return (int)dstSize;
}

View File

@@ -69,8 +69,8 @@ public class TeleDiskLzh
const int N_CHAR = 256 - THRESHOLD + F;
/* character code (= 0..N_CHAR-1) */
const int T = (N_CHAR * 2) - 1; /* Size of table */
const int ROOT = T - 1; /* root position */
const int T = N_CHAR * 2 - 1; /* Size of table */
const int ROOT = T - 1; /* root position */
const int MAX_FREQ = 0x8000;
/*
@@ -174,6 +174,7 @@ public class TeleDiskLzh
int count; // was an unsigned long, seems unnecessary
for(count = 0; count < len;)
{
if(_tdctl.Bufcnt == 0)
{
if((c = DecodeChar()) < 0)
@@ -193,8 +194,8 @@ public class TeleDiskLzh
if((pos = DecodePosition()) < 0)
return count; // fatal error
_tdctl.Bufpos = (ushort)((_tdctl.R - pos - 1) & (N - 1));
_tdctl.Bufcnt = (ushort)(c - 255 + THRESHOLD);
_tdctl.Bufpos = (ushort)(_tdctl.R - pos - 1 & N - 1);
_tdctl.Bufcnt = (ushort)(c - 255 + THRESHOLD);
_tdctl.Bufndx = 0;
}
}
@@ -204,7 +205,7 @@ public class TeleDiskLzh
while(_tdctl.Bufndx < _tdctl.Bufcnt &&
count < len)
{
c = _textBuf[(_tdctl.Bufpos + _tdctl.Bufndx) & (N - 1)];
c = _textBuf[_tdctl.Bufpos + _tdctl.Bufndx & N - 1];
buf[count] = (byte)c;
_tdctl.Bufndx++;
_textBuf[_tdctl.R++] = (byte)c;
@@ -216,6 +217,7 @@ public class TeleDiskLzh
if(_tdctl.Bufndx >= _tdctl.Bufcnt)
_tdctl.Bufndx = _tdctl.Bufcnt = 0;
}
}
return count; // count == len, success
}
@@ -245,7 +247,7 @@ public class TeleDiskLzh
while(_getlen <= 8)
{
// typically reads a word at a time
_getbuf |= (ushort)(_tdctl.Inbuf[_tdctl.Ibufndx++] << (8 - _getlen));
_getbuf |= (ushort)(_tdctl.Inbuf[_tdctl.Ibufndx++] << 8 - _getlen);
_getlen += 8;
}
@@ -257,7 +259,7 @@ public class TeleDiskLzh
if(NextWord() < 0)
return -1;
short i = (short)_getbuf;
var i = (short)_getbuf;
_getbuf <<= 1;
_getlen--;
@@ -316,12 +318,14 @@ public class TeleDiskLzh
short j = 0;
for(i = 0; i < T; i++)
{
if(_son[i] >= T)
{
_freq[j] = (ushort)((_freq[i] + 1) / 2);
_son[j] = _son[i];
j++;
}
}
/* make a tree : first, connect children nodes */
for(i = 0, j = N_CHAR; j < T; i += 2, j++)
@@ -332,7 +336,7 @@ public class TeleDiskLzh
for(k = (short)(j - 1); f < _freq[k]; k--) {}
k++;
ushort l = (ushort)((j - k) * 2);
var l = (ushort)((j - k) * 2);
Array.ConstrainedCopy(_freq, k, _freq, k + 1, l);
_freq[k] = f;
@@ -342,10 +346,12 @@ public class TeleDiskLzh
/* connect parent nodes */
for(i = 0; i < T; i++)
{
if((k = _son[i]) >= T)
_prnt[k] = i;
else
_prnt[k] = _prnt[k + 1] = i;
}
}
/* update freq tree */
@@ -395,7 +401,7 @@ public class TeleDiskLzh
short DecodeChar()
{
ushort c = (ushort)_son[ROOT];
var c = (ushort)_son[ROOT];
/*
* start searching tree from the root to leaves.
@@ -427,8 +433,8 @@ public class TeleDiskLzh
if((bit = (short)GetByte()) < 0)
return -1;
ushort i = (ushort)bit;
ushort c = (ushort)(_dCode[i] << 6);
var i = (ushort)bit;
var c = (ushort)(_dCode[i] << 6);
ushort j = _dLen[i];
/* input lower 6 bits directly */
@@ -442,16 +448,25 @@ public class TeleDiskLzh
i = (ushort)((i << 1) + bit);
}
return (short)(c | (i & 0x3f));
return (short)(c | i & 0x3f);
}
#region Nested type: Tdlzhuf
/* update when cumulative frequency */
/* reaches to this value */
struct Tdlzhuf
{
public ushort R, Bufcnt, Bufndx, Bufpos, // string buffer
public ushort R,
Bufcnt,
Bufndx,
Bufpos, // string buffer
// the following to allow block reads from input in next_word()
Ibufcnt, Ibufndx; // input buffer counters
public byte[] Inbuf; // input buffer
Ibufcnt,
Ibufndx; // input buffer counters
public byte[] Inbuf; // input buffer
}
#endregion
}

View File

@@ -42,7 +42,7 @@ public class ZSTD
[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_zstd_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize,
int compressionLevel);
int compressionLevel);
/// <summary>Decodes a buffer compressed with ZSTD</summary>
/// <param name="source">Encoded buffer</param>
@@ -50,7 +50,8 @@ public class ZSTD
/// <returns>The number of decoded bytes</returns>
public static int DecodeBuffer(byte[] source, byte[] destination) =>
(int)(Native.IsSupported
? AARU_zstd_decode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length) : 0);
? AARU_zstd_decode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length)
: 0);
/// <summary>Compresses a buffer using ZSTD</summary>
/// <param name="source">Data to compress</param>
@@ -60,5 +61,6 @@ public class ZSTD
public static int EncodeBuffer(byte[] source, byte[] destination, int compressionLevel) =>
(int)(Native.IsSupported
? AARU_zstd_encode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length,
compressionLevel) : 0);
compressionLevel)
: 0);
}