Esempio n. 1
0
        private int Decompress(ArraySegment <byte> src, byte[] dst, int offset, bool bufferSizePrecheck = true)
        {
            if (offset < 0 || offset >= dst.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (src.Count == 0)
            {
                return(0);
            }

            int dstCapacity = dst.Length - offset;

            using (var srcPtr = new ArraySegmentPtr(src)) {
                if (bufferSizePrecheck)
                {
                    ulong expectedDstSize = ExternMethods.ZSTD_getDecompressedSize(srcPtr, (size_t)src.Count);
                    if ((int)expectedDstSize > dstCapacity)
#if NETSTANDARD2_0
                    { throw new OutOfMemoryException("Buffer size is less than specified decompressed data size"); }
#else
                    { throw new InsufficientMemoryException("Buffer size is less than specified decompressed data size"); }
#endif
                }

                size_t dstSize;
                using (var dstPtr = new ArraySegmentPtr(new ArraySegment <byte>(dst, offset, dstCapacity)))
                    dstSize = ExternMethods.ZSTD_decompressDCtx(dctx, dstPtr, (size_t)dstCapacity, srcPtr, (size_t)src.Count).EnsureZstdSuccess();
                return((int)dstSize);
            }
        }
Esempio n. 2
0
 public static size_t EnsureZstdSuccess(this size_t returnValue)
 {
     if (ExternMethods.ZSTD_isError(returnValue) != 0)
     {
         ThrowException(returnValue, Marshal.PtrToStringAnsi(ExternMethods.ZSTD_getErrorName(returnValue)));
     }
     return(returnValue);
 }
Esempio n. 3
0
 private void Dispose(bool disposing)
 {
     if (disposed)
     {
         return;
     }
     ExternMethods.ZSTD_freeDCtx(dctx);
     disposed = true;
 }
Esempio n. 4
0
        private int Compress(ArraySegment <byte> src, byte[] dst, int offset)
        {
            if (offset < 0 || offset >= dst.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (src.Count == 0)
            {
                return(0);
            }

            int    dstCapacity = dst.Length - offset;
            size_t dstSize;

            using (var srcPtr = new ArraySegmentPtr(src))
                using (var dstPtr = new ArraySegmentPtr(new ArraySegment <byte>(dst, offset, dstCapacity)))
                    dstSize = ExternMethods.ZSTD_compressCCtx(cctx, dstPtr, (size_t)dstCapacity, srcPtr, (size_t)src.Count, compressionLevel).EnsureZstdSuccess();
            return((int)dstSize);
        }
Esempio n. 5
0
 private static ulong GetDecompressedSize(ArraySegment <byte> src)
 {
     using (var srcPtr = new ArraySegmentPtr(src))
         return(ExternMethods.ZSTD_getDecompressedSize(srcPtr, (size_t)src.Count));
 }
Esempio n. 6
0
 public Decompressor()
 {
     dctx = ExternMethods.ZSTD_createDCtx().EnsureZstdSuccess();
 }
Esempio n. 7
0
 private static int GetCompressBound(int size)
 {
     return((int)ExternMethods.ZSTD_compressBound((size_t)size));
 }
Esempio n. 8
0
 public Compressor(int compressionLevel)
 {
     this.compressionLevel = compressionLevel;
     cctx = ExternMethods.ZSTD_createCCtx().EnsureZstdSuccess();
 }