Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the ZLib P/Invokes all blittable #53968

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/libraries/Common/src/Interop/Interop.zlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ internal static partial class Interop
internal static partial class zlib
{
[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_DeflateInit2_")]
internal static extern ZLibNative.ErrorCode DeflateInit2_(
ref ZLibNative.ZStream stream,
internal static extern unsafe ZLibNative.ErrorCode DeflateInit2_(
ZLibNative.ZStream* stream,
ZLibNative.CompressionLevel level,
ZLibNative.CompressionMethod method,
int windowBits,
int memLevel,
ZLibNative.CompressionStrategy strategy);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_Deflate")]
internal static extern ZLibNative.ErrorCode Deflate(ref ZLibNative.ZStream stream, ZLibNative.FlushCode flush);
internal static extern unsafe ZLibNative.ErrorCode Deflate(ZLibNative.ZStream* stream, ZLibNative.FlushCode flush);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_DeflateReset")]
internal static extern ZLibNative.ErrorCode DeflateReset(ref ZLibNative.ZStream stream);
internal static extern unsafe ZLibNative.ErrorCode DeflateReset(ZLibNative.ZStream* stream);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_DeflateEnd")]
internal static extern ZLibNative.ErrorCode DeflateEnd(ref ZLibNative.ZStream stream);
internal static extern unsafe ZLibNative.ErrorCode DeflateEnd(ZLibNative.ZStream* stream);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_InflateInit2_")]
internal static extern ZLibNative.ErrorCode InflateInit2_(ref ZLibNative.ZStream stream, int windowBits);
internal static extern unsafe ZLibNative.ErrorCode InflateInit2_(ZLibNative.ZStream* stream, int windowBits);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_Inflate")]
internal static extern ZLibNative.ErrorCode Inflate(ref ZLibNative.ZStream stream, ZLibNative.FlushCode flush);
internal static extern unsafe ZLibNative.ErrorCode Inflate(ZLibNative.ZStream* stream, ZLibNative.FlushCode flush);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_InflateReset")]
internal static extern ZLibNative.ErrorCode InflateReset(ref ZLibNative.ZStream stream);
internal static extern unsafe ZLibNative.ErrorCode InflateReset(ZLibNative.ZStream* stream);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_InflateEnd")]
internal static extern ZLibNative.ErrorCode InflateEnd(ref ZLibNative.ZStream stream);
internal static extern unsafe ZLibNative.ErrorCode InflateEnd(ZLibNative.ZStream* stream);

[DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_Crc32")]
internal static extern unsafe uint crc32(uint crc, byte* buffer, int len);
Expand Down
76 changes: 52 additions & 24 deletions src/libraries/Common/src/System/IO/Compression/ZLibNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,81 +262,109 @@ private void EnsureState(State requiredState)
}


public ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
public unsafe ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
{
EnsureNotDisposed();
EnsureState(State.NotInitialized);

ErrorCode errC = Interop.zlib.DeflateInit2_(ref _zStream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy);
_initializationState = State.InitializedForDeflate;
fixed (ZStream* stream = &_zStream)
{
ErrorCode errC = Interop.zlib.DeflateInit2_(stream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy);
_initializationState = State.InitializedForDeflate;

return errC;
return errC;
}
}


public ErrorCode Deflate(FlushCode flush)
public unsafe ErrorCode Deflate(FlushCode flush)
{
EnsureNotDisposed();
EnsureState(State.InitializedForDeflate);
return Interop.zlib.Deflate(ref _zStream, flush);

fixed (ZStream* stream = &_zStream)
{
return Interop.zlib.Deflate(stream, flush);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
}
}


public ErrorCode DeflateReset()
public unsafe ErrorCode DeflateReset()
{
EnsureNotDisposed();
EnsureState(State.InitializedForDeflate);
return Interop.zlib.DeflateReset(ref _zStream);

fixed (ZStream* stream = &_zStream)
{
return Interop.zlib.DeflateReset(stream);
}
}

public ErrorCode DeflateEnd()
public unsafe ErrorCode DeflateEnd()
{
EnsureNotDisposed();
EnsureState(State.InitializedForDeflate);

ErrorCode errC = Interop.zlib.DeflateEnd(ref _zStream);
_initializationState = State.Disposed;
fixed (ZStream* stream = &_zStream)
{
ErrorCode errC = Interop.zlib.DeflateEnd(stream);
_initializationState = State.Disposed;

return errC;
return errC;
}
}


public ErrorCode InflateInit2_(int windowBits)
public unsafe ErrorCode InflateInit2_(int windowBits)
{
EnsureNotDisposed();
EnsureState(State.NotInitialized);

ErrorCode errC = Interop.zlib.InflateInit2_(ref _zStream, windowBits);
_initializationState = State.InitializedForInflate;
fixed (ZStream* stream = &_zStream)
{
ErrorCode errC = Interop.zlib.InflateInit2_(stream, windowBits);
_initializationState = State.InitializedForInflate;

return errC;
return errC;
}
}


public ErrorCode Inflate(FlushCode flush)
public unsafe ErrorCode Inflate(FlushCode flush)
{
EnsureNotDisposed();
EnsureState(State.InitializedForInflate);
return Interop.zlib.Inflate(ref _zStream, flush);

fixed (ZStream* stream = &_zStream)
{
return Interop.zlib.Inflate(stream, flush);
}
}


public ErrorCode InflateReset()
public unsafe ErrorCode InflateReset()
{
EnsureNotDisposed();
EnsureState(State.InitializedForInflate);
return Interop.zlib.InflateReset(ref _zStream);

fixed (ZStream* stream = &_zStream)
{
return Interop.zlib.InflateReset(stream);
}
}

public ErrorCode InflateEnd()
public unsafe ErrorCode InflateEnd()
{
EnsureNotDisposed();
EnsureState(State.InitializedForInflate);

ErrorCode errC = Interop.zlib.InflateEnd(ref _zStream);
_initializationState = State.Disposed;
fixed (ZStream* stream = &_zStream)
{
ErrorCode errC = Interop.zlib.InflateEnd(stream);
_initializationState = State.Disposed;

return errC;
return errC;
}
}

// This can work even after XxflateEnd().
Expand Down