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