예제 #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);
            }
        }
예제 #2
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);
        }
예제 #3
0
 private static ulong GetDecompressedSize(ArraySegment <byte> src)
 {
     using (var srcPtr = new ArraySegmentPtr(src))
         return(ExternMethods.ZSTD_getDecompressedSize(srcPtr, (size_t)src.Count));
 }