private Span <byte> DecompressNative(ReadOnlySpan <byte> src, int decompressBound) { fixed(byte *srcPtr = src) { var buffer = new byte[decompressBound]; fixed(byte *decompressedBufferNativePtr = buffer) { var dctx = ExternMethods.ZSTD_createDCtx(); try { var length = ExternMethods.ZSTD_decompressDCtx(dctx, (IntPtr)decompressedBufferNativePtr, (nuint)buffer.Length, (IntPtr)srcPtr, (nuint)src.Length); return(new Span <byte>(buffer, 0, (int)length)); } finally { ExternMethods.ZSTD_freeDCtx(dctx); } } } }
static unsafe void Test2() { var cctx = ExternMethods.ZSTD_createCCtx(); var dctx = ExternMethods.ZSTD_createDCtx(); var src = File.ReadAllBytes("dickens"); var dest = new byte[ExternMethods.ZSTD_compressBound((nuint)src.Length)]; var uncompressed = new byte[src.Length]; fixed(byte *dstPtr = dest) fixed(byte *srcPtr = src) fixed(byte *uncompressedPtr = uncompressed) { var compressedLength = ExternMethods.ZSTD_compressCCtx(cctx, (IntPtr)dstPtr, (nuint)dest.Length, (IntPtr)srcPtr, (nuint)src.Length, level); var decompressedLength = ExternMethods.ZSTD_decompressDCtx(dctx, (IntPtr)uncompressedPtr, (nuint)uncompressed.Length, (IntPtr)dstPtr, compressedLength); Console.WriteLine($"{compressedLength} {decompressedLength} {src.Length}"); } ExternMethods.ZSTD_freeCCtx(cctx); ExternMethods.ZSTD_freeDCtx(dctx); }