Exemplo 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);
        }
Exemplo n.º 2
0
 private void Dispose(bool disposing)
 {
     if (Ddict != IntPtr.Zero)
     {
         ExternMethods.ZSTD_freeDDict(Ddict);
         Ddict = IntPtr.Zero;
     }
 }
Exemplo n.º 3
0
 internal static UIntPtr EnsureZdictSuccess(UIntPtr returnValue)
 {
     if (ExternMethods.ZDICT_isError(returnValue) != 0)
     {
         ThrowException(returnValue, Marshal.PtrToStringAnsi(ExternMethods.ZDICT_getErrorName(returnValue)));
     }
     return(returnValue);
 }
Exemplo n.º 4
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            ExternMethods.ZSTD_freeCCtx(cctx);

            disposed = true;
        }
Exemplo n.º 5
0
        public unsafe ZstdDecompressionOptions(byte[] dict)
        {
            Dictionary = dict;

            fixed(byte *ptr = dict)
            {
                if (dict != null)
                {
                    Ddict = ExternMethods.ZSTD_createDDict(ptr, (UIntPtr)dict.Length).EnsureZstdSuccess();
                }
                else
                {
                    GC.SuppressFinalize(this);                     // No unmanaged resources
                }
            }
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
 public static int GetCompressBound(int size)
 {
     return((int)ExternMethods.ZSTD_compressBound((UIntPtr)size));
 }
Exemplo n.º 9
0
        public ZstdCompressor(ZstdCompressionOptions options)
        {
            Options = options;

            cctx = ExternMethods.ZSTD_createCCtx().EnsureZstdSuccess();
        }
Exemplo n.º 10
0
 public static unsafe ulong GetDecompressedSize(ReadOnlySpan <byte> src)
 {
     fixed(byte *srcPtr = src)
     return(ExternMethods.ZSTD_getDecompressedSize(srcPtr, (UIntPtr)src.Length));
 }