public static int Compress(ReadOnlySpan <byte> src, Span <byte> dst, CompressionDictionary dictionary) { if (_threadCompressContext == null) _threadCompressContext = new CompressContext(); fixed(byte *srcPtr = src) fixed(byte *dstPtr = dst) { UIntPtr result; if (dictionary == null || dictionary.Compression == null) { result = ZSTD_compressCCtx(_threadCompressContext.Compression, dstPtr, (UIntPtr)dst.Length, srcPtr, (UIntPtr)src.Length, 3); } else { result = ZSTD_compress_usingCDict(_threadCompressContext.Compression, dstPtr, (UIntPtr)dst.Length, srcPtr, (UIntPtr)src.Length, dictionary.Compression); } AssertSuccess(result, dictionary); return((int)result); } }
private static void AssertSuccess(UIntPtr v, CompressionDictionary dictionary) { if (ZSTD_isError(v) == 0) { return; } string ptrToStringAnsi = Marshal.PtrToStringAnsi(ZSTD_getErrorName(v)); if (dictionary != null) { throw new InvalidOperationException(ptrToStringAnsi + " on " + dictionary); } throw new InvalidOperationException(ptrToStringAnsi); }
public static int Compress(byte *src, int srcLen, byte *dst, int dstLen, CompressionDictionary dictionary) { _threadCompressContext ??= new CompressContext(); { UIntPtr result; if (dictionary == null || dictionary.Compression == null) { result = ZSTD_compressCCtx(_threadCompressContext.Compression, dst, (UIntPtr)dstLen, src, (UIntPtr)srcLen, 3); } else { result = ZSTD_compress_usingCDict(_threadCompressContext.Compression, dst, (UIntPtr)dstLen, src, (UIntPtr)srcLen, dictionary.Compression); } AssertSuccess(result, dictionary); return((int)result); } }