From 5faa49a031336e9aef0bf0dfab65fada4f568af4 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 3 Oct 2023 22:51:02 +0100 Subject: [PATCH] [Aaru.Compression] Reformat and cleanup. --- Aaru.Compression/ADC.cs | 43 ++++++++++-------- Aaru.Compression/Aaru.Compression.csproj | 56 ++++++++++++------------ Aaru.Compression/AppleRle.cs | 17 +++---- Aaru.Compression/BZip2.cs | 6 +-- Aaru.Compression/FLAC.cs | 20 +++++---- Aaru.Compression/LZFSE.cs | 10 +++-- Aaru.Compression/LZIP.cs | 10 +++-- Aaru.Compression/LZMA.cs | 18 ++++---- Aaru.Compression/TeleDiskLzh.cs | 45 ++++++++++++------- Aaru.Compression/ZSTD.cs | 8 ++-- 10 files changed, 132 insertions(+), 101 deletions(-) diff --git a/Aaru.Compression/ADC.cs b/Aaru.Compression/ADC.cs index c85f07486..edeb33330 100644 --- a/Aaru.Compression/ADC.cs +++ b/Aaru.Compression/ADC.cs @@ -49,6 +49,7 @@ public static class ADC const int PLAIN = 1; const int TWO_BYTE = 2; const int THREE_BYTE = 3; + /// Set to true if this algorithm is supported, false otherwise. 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 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 + }; /// Decompresses a byte buffer that's compressed with ADC /// Compressed buffer @@ -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 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; } diff --git a/Aaru.Compression/Aaru.Compression.csproj b/Aaru.Compression/Aaru.Compression.csproj index 6ccb93c25..456441051 100644 --- a/Aaru.Compression/Aaru.Compression.csproj +++ b/Aaru.Compression/Aaru.Compression.csproj @@ -36,8 +36,8 @@ CS1591;CS1574 - - + + $(Version)+{chash:8} @@ -50,25 +50,25 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + @@ -76,14 +76,14 @@ - - - - - - + + + + + + - + \ No newline at end of file diff --git a/Aaru.Compression/AppleRle.cs b/Aaru.Compression/AppleRle.cs index e0f553e57..268b0d3e7 100644 --- a/Aaru.Compression/AppleRle.cs +++ b/Aaru.Compression/AppleRle.cs @@ -39,6 +39,7 @@ namespace Aaru.Compression; public static class AppleRle { const uint DART_CHUNK = 20960; + /// Set to true if this algorithm is supported, false otherwise. 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++]; diff --git a/Aaru.Compression/BZip2.cs b/Aaru.Compression/BZip2.cs index 3dcc72591..112ce4535 100644 --- a/Aaru.Compression/BZip2.cs +++ b/Aaru.Compression/BZip2.cs @@ -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); /// Decodes a buffer compressed with BZIP2 /// Encoded buffer @@ -51,7 +51,7 @@ public class BZip2 /// The number of decoded bytes 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 /// public static int EncodeBuffer(byte[] source, byte[] destination, int blockSize100K) { - uint destinationSize = (uint)destination.Length; + var destinationSize = (uint)destination.Length; if(Native.IsSupported) { diff --git a/Aaru.Compression/FLAC.cs b/Aaru.Compression/FLAC.cs index c15f3a71f..fb8451d5d 100644 --- a/Aaru.Compression/FLAC.cs +++ b/Aaru.Compression/FLAC.cs @@ -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); /// Decodes a buffer compressed with FLAC /// Encoded buffer @@ -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; diff --git a/Aaru.Compression/LZFSE.cs b/Aaru.Compression/LZFSE.cs index 165acd9f6..ec0f27b91 100644 --- a/Aaru.Compression/LZFSE.cs +++ b/Aaru.Compression/LZFSE.cs @@ -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; /// Compresses a buffer using BZIP2 /// Data to compress @@ -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; } \ No newline at end of file diff --git a/Aaru.Compression/LZIP.cs b/Aaru.Compression/LZIP.cs index b5137e445..490a05e60 100644 --- a/Aaru.Compression/LZIP.cs +++ b/Aaru.Compression/LZIP.cs @@ -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); /// Decodes a buffer compressed with LZIP /// Encoded buffer @@ -58,6 +58,8 @@ public class LZIP /// Match length limit /// The size of the compressed data 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; } \ No newline at end of file diff --git a/Aaru.Compression/LZMA.cs b/Aaru.Compression/LZMA.cs index c11ade22a..2f621a93c 100644 --- a/Aaru.Compression/LZMA.cs +++ b/Aaru.Compression/LZMA.cs @@ -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); /// Decodes a buffer compressed with LZMA /// Encoded buffer @@ -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 /// Forward bits /// How many bytes have been written to the destination buffer 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; } diff --git a/Aaru.Compression/TeleDiskLzh.cs b/Aaru.Compression/TeleDiskLzh.cs index 52f966d14..d71224d16 100644 --- a/Aaru.Compression/TeleDiskLzh.cs +++ b/Aaru.Compression/TeleDiskLzh.cs @@ -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 } \ No newline at end of file diff --git a/Aaru.Compression/ZSTD.cs b/Aaru.Compression/ZSTD.cs index e2e21569b..ecb625d9f 100644 --- a/Aaru.Compression/ZSTD.cs +++ b/Aaru.Compression/ZSTD.cs @@ -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); /// Decodes a buffer compressed with ZSTD /// Encoded buffer @@ -50,7 +50,8 @@ public class ZSTD /// The number of decoded bytes 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); /// Compresses a buffer using ZSTD /// Data to compress @@ -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); } \ No newline at end of file