Skip to content

Commit

Permalink
Make some Brotli p/invokes blittable (dotnet#54029)
Browse files Browse the repository at this point in the history
* Make some Brotli p/invokes blittable

* Use Interop.BOOL
  • Loading branch information
elinor-fung authored Jun 11, 2021
1 parent 9ac357d commit ef9e276
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/libraries/Common/src/Interop/Interop.Brotli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ internal static extern unsafe int BrotliDecoderDecompressStream(
ref nuint availableOut, byte** nextOut, out nuint totalOut);

[DllImport(Libraries.CompressionNative)]
internal static extern unsafe bool BrotliDecoderDecompress(nuint availableInput, byte* inBytes, ref nuint availableOutput, byte* outBytes);
internal static extern unsafe BOOL BrotliDecoderDecompress(nuint availableInput, byte* inBytes, nuint* availableOutput, byte* outBytes);

[DllImport(Libraries.CompressionNative)]
internal static extern void BrotliDecoderDestroyInstance(IntPtr state);

[DllImport(Libraries.CompressionNative)]
internal static extern bool BrotliDecoderIsFinished(SafeBrotliDecoderHandle state);
internal static extern BOOL BrotliDecoderIsFinished(SafeBrotliDecoderHandle state);

[DllImport(Libraries.CompressionNative)]
internal static extern SafeBrotliEncoderHandle BrotliEncoderCreateInstance(IntPtr allocFunc, IntPtr freeFunc, IntPtr opaque);

[DllImport(Libraries.CompressionNative)]
internal static extern bool BrotliEncoderSetParameter(SafeBrotliEncoderHandle state, BrotliEncoderParameter parameter, uint value);
internal static extern BOOL BrotliEncoderSetParameter(SafeBrotliEncoderHandle state, BrotliEncoderParameter parameter, uint value);

[DllImport(Libraries.CompressionNative)]
internal static extern unsafe bool BrotliEncoderCompressStream(
internal static extern unsafe BOOL BrotliEncoderCompressStream(
SafeBrotliEncoderHandle state, BrotliEncoderOperation op, ref nuint availableIn,
byte** nextIn, ref nuint availableOut, byte** nextOut, out nuint totalOut);

[DllImport(Libraries.CompressionNative)]
internal static extern bool BrotliEncoderHasMoreOutput(SafeBrotliEncoderHandle state);
internal static extern BOOL BrotliEncoderHasMoreOutput(SafeBrotliEncoderHandle state);

[DllImport(Libraries.CompressionNative)]
internal static extern void BrotliEncoderDestroyInstance(IntPtr state);

[DllImport(Libraries.CompressionNative)]
internal static extern unsafe bool BrotliEncoderCompress(int quality, int window, int v, nuint availableInput, byte* inBytes, ref nuint availableOutput, byte* outBytes);
internal static extern unsafe BOOL BrotliEncoderCompress(int quality, int window, int v, nuint availableInput, byte* inBytes, nuint* availableOutput, byte* outBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetsAnyOS)' != 'true'">
<Compile Include="$(CommonPath)Interop\Interop.Brotli.cs" />
<!-- The native compression lib uses a BROTLI_BOOL type analogous to the Windows BOOL type -->
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" />
<Compile Include="System\IO\Compression\enc\BrotliStream.Compress.cs" />
<Compile Include="System\IO\Compression\dec\BrotliStream.Decompress.cs" />
<Compile Include="System\IO\Compression\BrotliUtils.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destinat

bytesConsumed = 0;
bytesWritten = 0;
if (Interop.Brotli.BrotliDecoderIsFinished(_state))
if (Interop.Brotli.BrotliDecoderIsFinished(_state) != Interop.BOOL.FALSE)
return OperationStatus.Done;
nuint availableOutput = (nuint)destination.Length;
nuint availableInput = (nuint)source.Length;
Expand Down Expand Up @@ -117,7 +117,7 @@ public static unsafe bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> de
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
nuint availableOutput = (nuint)destination.Length;
bool success = Interop.Brotli.BrotliDecoderDecompress((nuint)source.Length, inBytes, ref availableOutput, outBytes);
bool success = Interop.Brotli.BrotliDecoderDecompress((nuint)source.Length, inBytes, &availableOutput, outBytes) != Interop.BOOL.FALSE;

Debug.Assert(success ? availableOutput <= (nuint)destination.Length : availableOutput == 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal void SetQuality(int quality)
{
throw new ArgumentOutOfRangeException(nameof(quality), SR.Format(SR.BrotliEncoder_Quality, quality, 0, BrotliUtils.Quality_Max));
}
if (!Interop.Brotli.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.Quality, (uint)quality))
if (Interop.Brotli.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.Quality, (uint)quality) == Interop.BOOL.FALSE)
{
throw new InvalidOperationException(SR.Format(SR.BrotliEncoder_InvalidSetParameter, "Quality"));
}
Expand All @@ -96,7 +96,7 @@ internal void SetWindow(int window)
{
throw new ArgumentOutOfRangeException(nameof(window), SR.Format(SR.BrotliEncoder_Window, window, BrotliUtils.WindowBits_Min, BrotliUtils.WindowBits_Max));
}
if (!Interop.Brotli.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.LGWin, (uint)window))
if (Interop.Brotli.BrotliEncoderSetParameter(_state, BrotliEncoderParameter.LGWin, (uint)window) == Interop.BOOL.FALSE)
{
throw new InvalidOperationException(SR.Format(SR.BrotliEncoder_InvalidSetParameter, "Window"));
}
Expand Down Expand Up @@ -161,7 +161,7 @@ internal OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destinat
fixed (byte* inBytes = &MemoryMarshal.GetReference(source))
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
if (!Interop.Brotli.BrotliEncoderCompressStream(_state, operation, ref availableInput, &inBytes, ref availableOutput, &outBytes, out _))
if (Interop.Brotli.BrotliEncoderCompressStream(_state, operation, ref availableInput, &inBytes, ref availableOutput, &outBytes, out _) == Interop.BOOL.FALSE)
{
return OperationStatus.InvalidData;
}
Expand All @@ -173,7 +173,7 @@ internal OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destinat
bytesWritten += destination.Length - (int)availableOutput;

// no bytes written, no remaining input to give to the encoder, and no output in need of retrieving means we are Done
if ((int)availableOutput == destination.Length && !Interop.Brotli.BrotliEncoderHasMoreOutput(_state) && availableInput == 0)
if ((int)availableOutput == destination.Length && Interop.Brotli.BrotliEncoderHasMoreOutput(_state) == Interop.BOOL.FALSE && availableInput == 0)
{
return OperationStatus.Done;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination
fixed (byte* outBytes = &MemoryMarshal.GetReference(destination))
{
nuint availableOutput = (nuint)destination.Length;
bool success = Interop.Brotli.BrotliEncoderCompress(quality, window, /*BrotliEncoderMode*/ 0, (nuint)source.Length, inBytes, ref availableOutput, outBytes);
bool success = Interop.Brotli.BrotliEncoderCompress(quality, window, /*BrotliEncoderMode*/ 0, (nuint)source.Length, inBytes, &availableOutput, outBytes) != Interop.BOOL.FALSE;

Debug.Assert(success ? availableOutput <= (nuint)destination.Length : availableOutput == 0);

Expand Down

0 comments on commit ef9e276

Please sign in to comment.