Ejemplo n.º 1
0
        public unsafe int Wrap(ReadOnlySpan <byte> src, Span <byte> dst, int compressionLevel = 3)
        {
            if (src.Length == 0)
            {
                return(0);
            }

            UIntPtr dstSize;

            fixed(byte *dstPtr = dst)
            fixed(byte *srcPtr = src)
            {
                if (Options.Cdict == IntPtr.Zero)
                {
                    dstSize = ExternMethods.ZSTD_compressCCtx(cctx, dstPtr, (UIntPtr)dst.Length, srcPtr, (UIntPtr)src.Length,
                                                              compressionLevel);
                }
                else
                {
                    dstSize = ExternMethods.ZSTD_compress_usingCDict(cctx, dstPtr, (UIntPtr)dst.Length, srcPtr,
                                                                     (UIntPtr)src.Length, Options.Cdict);
                }
            }

            ReturnValueExtensions.EnsureZdictSuccess(dstSize);
            return((int)dstSize);
        }
Ejemplo n.º 2
0
        public static byte[] TrainFromBuffer(IEnumerable <byte[]> samples, int dictCapacity = DefaultDictCapacity)
        {
            var ms           = new MemoryStream();
            var samplesSizes = samples.Select(sample =>
            {
                ms.Write(sample, 0, sample.Length);
                return((UIntPtr)sample.Length);
            }).ToArray();

            var dictBuffer = new byte[dictCapacity];
            var dictSize   = ExternMethods.ZDICT_trainFromBuffer(dictBuffer, (UIntPtr)dictCapacity, ms.ToArray(), samplesSizes, (uint)samplesSizes.Length);

            ReturnValueExtensions.EnsureZdictSuccess(dictSize);

            if (dictCapacity != (int)dictSize)
            {
                Array.Resize(ref dictBuffer, (int)dictSize);
            }

            return(dictBuffer);
        }
Ejemplo n.º 3
0
        public unsafe int Unwrap(ReadOnlySpan <byte> src, Span <byte> dst, bool bufferSizePrecheck = true)
        {
            if (src.Length == 0)
            {
                return(0);
            }

            if (bufferSizePrecheck)
            {
                if (GetDecompressedSize(src) > (ulong)dst.Length)
                {
                    throw new InsufficientMemoryException(
                              "Buffer size is less than specified decompressed data size");
                }
            }

            UIntPtr dstSize;


            fixed(byte *dstPtr = dst)
            fixed(byte *srcPtr = src)
            {
                if (Options.Ddict == IntPtr.Zero)
                {
                    dstSize = ExternMethods.ZSTD_decompressDCtx(dctx, dstPtr, (UIntPtr)dst.Length, srcPtr,
                                                                (UIntPtr)src.Length);
                }
                else
                {
                    dstSize = ExternMethods.ZSTD_decompress_usingDDict(dctx, dstPtr, (UIntPtr)dst.Length, srcPtr,
                                                                       (UIntPtr)src.Length, Options.Ddict);
                }
            }

            ReturnValueExtensions.EnsureZstdSuccess(dstSize);
            return((int)dstSize);
        }