diff --git a/Aaru.Compression/Aaru.Compression.csproj b/Aaru.Compression/Aaru.Compression.csproj index dd7a19655..3146a437d 100644 --- a/Aaru.Compression/Aaru.Compression.csproj +++ b/Aaru.Compression/Aaru.Compression.csproj @@ -53,6 +53,7 @@ + diff --git a/Aaru.Compression/LZO.cs b/Aaru.Compression/LZO.cs new file mode 100644 index 000000000..e346781c1 --- /dev/null +++ b/Aaru.Compression/LZO.cs @@ -0,0 +1,104 @@ +using System.Runtime.InteropServices; +// ReSharper disable InconsistentNaming +// ReSharper disable UnusedMember.Global + +namespace Aaru.Compression; + +public sealed partial class LZO +{ + /// + /// LZO algorithm types. The LZO1X algorithm is the most commonly used + /// + public enum Algorithm + { + /// LZO1 algorithm + LZO1 = 0, + /// LZO1A algorithm + LZO1A = 1, + /// LZO1B algorithm (supports compression levels 1-9, 99, 999) + LZO1B = 2, + /// LZO1C algorithm (supports compression levels 1-9, 99, 999) + LZO1C = 3, + /// LZO1F algorithm (supports compression level 999) + LZO1F = 4, + /// LZO1X algorithm (supports compression levels 11, 12, 15, 999) - most common + LZO1X = 5, + /// LZO1Y algorithm (supports compression level 999) + LZO1Y = 6, + /// LZO1Z algorithm (only 999 compression level) + LZO1Z = 7, + /// LZO2A algorithm (only 999 compression level) + LZO2A = 8 + } + + /// + /// AARU_EXPORT int32_t AARU_CALL AARU_lzo_decode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t + /// *src_buffer, size_t src_size, int32_t algorithm); + /// + /// Buffer to write the decompressed data to + /// Size of the destination buffer + /// Buffer that contains the compressed data + /// Size of the source buffer + /// Which algorithm to use + /// Error code or 0 on success + [LibraryImport("libAaru.Compression.Native", SetLastError = true)] + private static partial int AARU_lzo_decode_buffer(byte[] dst_buffer, ref nuint dst_size, in byte[] src_buffer, + nuint src_size, Algorithm algorithm); + + + /// + /// AARU_EXPORT int32_t AARU_CALL AARU_lzo_encode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t + /// *src_buffer, size_t src_size, int32_t algorithm, int32_t compression_level); + /// + /// Buffer to write the decompressed data to + /// Size of the destination buffer + /// Buffer that contains the compressed data + /// Size of the source buffer + /// Compression level + /// Which algorithm to use + /// Error code or 0 on success + [LibraryImport("libAaru.Compression.Native", SetLastError = true)] + private static partial int AARU_lzo_encode_buffer(byte[] dst_buffer, ref nuint dst_size, in byte[] src_buffer, + nuint src_size, Algorithm algorithm, int compression_level); + + /// Decodes a buffer compressed with LZO + /// Encoded buffer + /// Buffer where to write the decoded data + /// LZO algorithm variant to use for decompression + /// The number of decoded bytes, or -1 on error + public static int DecodeBuffer(byte[] source, byte[] destination, Algorithm algorithm) + { + if(!Native.IsSupported) return -1; + + var dstSize = (nuint)destination.Length; + int res = AARU_lzo_decode_buffer(destination, ref dstSize, source, (nuint)source.Length, algorithm); + + if(res != 0) return -1; + + return (int)dstSize; + } + + /// Compresses a buffer using LZO + /// Data to compress + /// Buffer to store the compressed data + /// LZO algorithm variant to use for compression + /// Compression level (valid values depend on the algorithm) + /// The size of the compressed data, or -1 on error + public static int EncodeBuffer(byte[] source, byte[] destination, Algorithm algorithm, int compressionLevel) + { + if(!Native.IsSupported) return -1; + + var dstSize = (nuint)destination.Length; + + int res = AARU_lzo_encode_buffer(destination, + ref dstSize, + source, + (nuint)source.Length, + algorithm, + compressionLevel); + + if(res != 0) return -1; + + return (int)dstSize; + } +} \ No newline at end of file