public void Setup() { cCtx = Methods.ZSTD_createCCtx(); dCtx = Methods.ZSTD_createDCtx(); cCtxNative = ExternMethods.ZSTD_createCCtx(); dCtxNative = ExternMethods.ZSTD_createDCtx(); src = File.ReadAllBytes("dickens"); dest = new byte[Methods.ZSTD_compressBound((nuint)src.Length)]; uncompressed = new byte[src.Length]; fixed(byte *dstPtr = dest) fixed(byte *srcPtr = src) { compressedLength = ExternMethods.ZSTD_compressCCtx(cCtxNative, (IntPtr)dstPtr, (nuint)dest.Length, (IntPtr)srcPtr, (nuint)src.Length, level); } }
private Span <byte> CompressNative(byte[] srcBuffer, int compressBound, int level) { var buffer = new byte[compressBound]; fixed(byte *bufferPtr = buffer) fixed(byte *srcPtr = srcBuffer) { var cctx = ExternMethods.ZSTD_createCCtx(); try { var length = ExternMethods.ZSTD_compressCCtx(cctx, (IntPtr)bufferPtr, (nuint)buffer.Length, (IntPtr)srcPtr, (nuint)srcBuffer.Length, level); return(new Span <byte>(buffer, 0, (int)length)); } finally { ExternMethods.ZSTD_freeCCtx(cctx); } } }
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); }