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); }
private void Dispose(bool disposing) { if (Ddict != IntPtr.Zero) { ExternMethods.ZSTD_freeDDict(Ddict); Ddict = IntPtr.Zero; } }
internal static UIntPtr EnsureZdictSuccess(UIntPtr returnValue) { if (ExternMethods.ZDICT_isError(returnValue) != 0) { ThrowException(returnValue, Marshal.PtrToStringAnsi(ExternMethods.ZDICT_getErrorName(returnValue))); } return(returnValue); }
private void Dispose(bool disposing) { if (disposed) { return; } ExternMethods.ZSTD_freeCCtx(cctx); disposed = true; }
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 } } }
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); }
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); }
public static int GetCompressBound(int size) { return((int)ExternMethods.ZSTD_compressBound((UIntPtr)size)); }
public ZstdCompressor(ZstdCompressionOptions options) { Options = options; cctx = ExternMethods.ZSTD_createCCtx().EnsureZstdSuccess(); }
public static unsafe ulong GetDecompressedSize(ReadOnlySpan <byte> src) { fixed(byte *srcPtr = src) return(ExternMethods.ZSTD_getDecompressedSize(srcPtr, (UIntPtr)src.Length)); }