Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        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);
        }