Skip to content

Commit

Permalink
[Refactor] Use LibraryImport instead of DllImport.
Browse files Browse the repository at this point in the history
  • Loading branch information
claunia committed May 1, 2024
1 parent 0111610 commit c2e6e12
Show file tree
Hide file tree
Showing 23 changed files with 349 additions and 281 deletions.
1 change: 1 addition & 0 deletions Aaru.Checksums/Aaru.Checksums.csproj
Expand Up @@ -27,6 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
Expand Down
18 changes: 9 additions & 9 deletions Aaru.Checksums/Adler32Context.cs
Expand Up @@ -52,7 +52,7 @@ namespace Aaru.Checksums;
/// <inheritdoc />
/// <summary>Implements the Adler-32 algorithm</summary>
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
public sealed class Adler32Context : IChecksum
public sealed partial class Adler32Context : IChecksum
{
internal const ushort ADLER_MODULE = 65521;
internal const uint NMAX = 5552;
Expand Down Expand Up @@ -130,17 +130,17 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr adler32_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr adler32_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int adler32_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_final(IntPtr ctx, ref uint checksum);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int adler32_final(IntPtr ctx, ref uint checksum);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void adler32_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void adler32_free(IntPtr ctx);

static void Step(ref ushort preSum1, ref ushort preSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext)
Expand Down
34 changes: 17 additions & 17 deletions Aaru.Checksums/CRC16Context.cs
Expand Up @@ -41,7 +41,7 @@ namespace Aaru.Checksums;

/// <inheritdoc />
/// <summary>Implements a CRC16 algorithm</summary>
public class Crc16Context : IChecksum
public partial class Crc16Context : IChecksum
{
readonly ushort _finalSeed;
readonly bool _inverse;
Expand Down Expand Up @@ -198,29 +198,29 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc16_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr crc16_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc16_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_final(IntPtr ctx, ref ushort crc);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc16_final(IntPtr ctx, ref ushort crc);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc16_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void crc16_free(IntPtr ctx);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc16_ccitt_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr crc16_ccitt_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_ccitt_final(IntPtr ctx, ref ushort crc);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc16_ccitt_final(IntPtr ctx, ref ushort crc);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc16_ccitt_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void crc16_ccitt_free(IntPtr ctx);

static void Step(ref ushort previousCrc, ushort[][] table, byte[] data, uint len)
{
Expand Down
18 changes: 9 additions & 9 deletions Aaru.Checksums/CRC32Context.cs
Expand Up @@ -49,7 +49,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public sealed class Crc32Context : IChecksum
public sealed partial class Crc32Context : IChecksum
{
const uint CRC32_ISO_POLY = 0xEDB88320;
const uint CRC32_ISO_SEED = 0xFFFFFFFF;
Expand Down Expand Up @@ -426,17 +426,17 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc32_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr crc32_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc32_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc32_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc32_final(IntPtr ctx, ref uint crc);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc32_final(IntPtr ctx, ref uint crc);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc32_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void crc32_free(IntPtr ctx);

static uint[][] GenerateTable(uint polynomial)
{
Expand Down
18 changes: 9 additions & 9 deletions Aaru.Checksums/CRC64Context.cs
Expand Up @@ -48,7 +48,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class Crc64Context : IChecksum
public sealed partial class Crc64Context : IChecksum
{
/// <summary>ECMA CRC64 polynomial</summary>
const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42;
Expand Down Expand Up @@ -371,17 +371,17 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc64_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr crc64_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc64_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc64_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc64_final(IntPtr ctx, ref ulong crc);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int crc64_final(IntPtr ctx, ref ulong crc);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc64_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void crc64_free(IntPtr ctx);

static ulong[][] GenerateTable(ulong polynomial)
{
Expand Down
36 changes: 18 additions & 18 deletions Aaru.Checksums/FletcherContext.cs
Expand Up @@ -50,7 +50,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public sealed class Fletcher32Context : IChecksum
public sealed partial class Fletcher32Context : IChecksum
{
internal const ushort FLETCHER_MODULE = 0xFFFF;
internal const uint NMAX = 5552;
Expand Down Expand Up @@ -128,17 +128,17 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr fletcher32_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr fletcher32_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher32_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int fletcher32_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher32_final(IntPtr ctx, ref uint crc);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int fletcher32_final(IntPtr ctx, ref uint crc);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void fletcher32_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void fletcher32_free(IntPtr ctx);

static void Step(ref ushort previousSum1, ref ushort previousSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext)
Expand Down Expand Up @@ -418,7 +418,7 @@ public static string Data(byte[] data, uint len, out byte[] hash)
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
public sealed class Fletcher16Context : IChecksum
public sealed partial class Fletcher16Context : IChecksum
{
const byte FLETCHER_MODULE = 0xFF;
const byte NMAX = 22;
Expand Down Expand Up @@ -497,17 +497,17 @@ public string End()

#endregion

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr fletcher16_init();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial IntPtr fletcher16_init();

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher16_update(IntPtr ctx, byte[] data, uint len);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int fletcher16_update(IntPtr ctx, byte[] data, uint len);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher16_final(IntPtr ctx, ref ushort checksum);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial int fletcher16_final(IntPtr ctx, ref ushort checksum);

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void fletcher16_free(IntPtr ctx);
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial void fletcher16_free(IntPtr ctx);

static void Step(ref byte previousSum1, ref byte previousSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext)
Expand Down
6 changes: 3 additions & 3 deletions Aaru.Checksums/Native.cs
Expand Up @@ -35,7 +35,7 @@
namespace Aaru.Checksums;

/// <summary>Handles native implementations of compression algorithms</summary>
public static class Native
public static partial class Native
{
static bool _checked;
static bool _supported;
Expand Down Expand Up @@ -76,6 +76,6 @@ public static bool IsSupported
}
}

[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern ulong get_acn_version();
[LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
private static partial ulong get_acn_version();
}
1 change: 1 addition & 0 deletions Aaru.CommonTypes/Aaru.CommonTypes.csproj
Expand Up @@ -27,6 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down
15 changes: 10 additions & 5 deletions Aaru.CommonTypes/Interop/DetectOS.cs
Expand Up @@ -40,12 +40,13 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Security.Principal;

namespace Aaru.CommonTypes.Interop;

/// <summary>Detects the underlying execution framework and operating system</summary>
public static class DetectOS
public static partial class DetectOS
{
/// <summary>Are we running under Mono?</summary>
public static readonly bool IsMono =
Expand Down Expand Up @@ -98,11 +99,15 @@ public static bool IsAdmin
}
}

[DllImport("libc", SetLastError = true)]
static extern int uname(out UtsName name);
[LibraryImport("libc", SetLastError = true)]
private static partial int uname(out UtsName name);

Check failure on line 103 in Aaru.CommonTypes/Interop/DetectOS.cs

View workflow job for this annotation

GitHub Actions / build

The type 'Aaru.CommonTypes.Interop.DetectOS.UtsName' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'name'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)

Check failure on line 103 in Aaru.CommonTypes/Interop/DetectOS.cs

View workflow job for this annotation

GitHub Actions / build

The type 'Aaru.CommonTypes.Interop.DetectOS.UtsName' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'name'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)

Check failure on line 103 in Aaru.CommonTypes/Interop/DetectOS.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The type 'Aaru.CommonTypes.Interop.DetectOS.UtsName' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'name'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)

Check failure on line 103 in Aaru.CommonTypes/Interop/DetectOS.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The type 'Aaru.CommonTypes.Interop.DetectOS.UtsName' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'name'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)

Check failure on line 103 in Aaru.CommonTypes/Interop/DetectOS.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The type 'Aaru.CommonTypes.Interop.DetectOS.UtsName' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'name'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)

[DllImport("libc", SetLastError = true, EntryPoint = "sysctlbyname", CharSet = CharSet.Ansi)]
static extern int OSX_sysctlbyname(string name, IntPtr oldp, IntPtr oldlenp, IntPtr newp, uint newlen);
[LibraryImport("libc",
EntryPoint = "sysctlbyname",
SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
private static partial int OSX_sysctlbyname(string name, IntPtr oldp, IntPtr oldlenp, IntPtr newp, uint newlen);

/// <summary>Gets the real platform ID, not the incomplete .NET framework one</summary>
/// <returns>Platform ID</returns>
Expand Down
6 changes: 3 additions & 3 deletions Aaru.Compression/ADC.cs
Expand Up @@ -44,7 +44,7 @@ namespace Aaru.Compression;
/// <summary>Implements the Apple version of RLE</summary>

// ReSharper disable once InconsistentNaming
public static class ADC
public static partial class ADC
{
const int PLAIN = 1;
const int TWO_BYTE = 2;
Expand All @@ -53,8 +53,8 @@ public static class ADC
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true;

[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_adc_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial int AARU_adc_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int GetChunkType(byte byt) => (byt & 0x80) == 0x80
Expand Down
7 changes: 4 additions & 3 deletions Aaru.Compression/AppleRle.cs
Expand Up @@ -36,15 +36,16 @@
namespace Aaru.Compression;

/// <summary>Implements the Apple version of RLE</summary>
public static class AppleRle
public static partial 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;

[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_apple_rle_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial int AARU_apple_rle_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer,
int srcSize);

/// <summary>Decodes a buffer compressed with Apple RLE</summary>
/// <param name="source">Encoded buffer</param>
Expand Down
13 changes: 7 additions & 6 deletions Aaru.Compression/BZip2.cs
Expand Up @@ -33,17 +33,18 @@
namespace Aaru.Compression;

/// <summary>Implements the BZIP2 compression algorithm</summary>
public class BZip2
public partial class BZip2
{
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true;

[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize);
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer,
uint srcSize);

[DllImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize,
int blockSize100K);
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer,
uint srcSize, int blockSize100K);

/// <summary>Decodes a buffer compressed with BZIP2</summary>
/// <param name="source">Encoded buffer</param>
Expand Down

0 comments on commit c2e6e12

Please sign in to comment.