< Summary

Information
Class: Microsoft.Win32.SafeHandles.SafeZstdDecompressHandle
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\Common\src\Microsoft\Win32\SafeHandles\SafeZstdHandle.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 193
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
ReleaseHandle()0%440%
SetDictionary(...)100%110%
SetPrefix(...)0%440%
Reset()0%220%

File(s)

D:\runner\runtime\src\libraries\Common\src\Microsoft\Win32\SafeHandles\SafeZstdHandle.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Buffers;
 6using System.Diagnostics;
 7using System.Runtime.InteropServices;
 8using System.IO.Compression;
 9
 10namespace Microsoft.Win32.SafeHandles
 11{
 12    internal sealed class SafeZstdCompressHandle : SafeHandle
 13    {
 14        internal SafeZstdCDictHandle? _dictionary;
 15        internal MemoryHandle? _prefixHandle;
 16        public SafeZstdCompressHandle() : base(IntPtr.Zero, true) { }
 17
 18        protected override bool ReleaseHandle()
 19        {
 20            Interop.Zstd.ZSTD_freeCCtx(handle);
 21
 22            // release the addref we took in SetDictionary
 23            _dictionary?.DangerousRelease();
 24
 25            if (_prefixHandle != null)
 26            {
 27                _prefixHandle.Value.Dispose();
 28                _prefixHandle = null;
 29            }
 30            return true;
 31        }
 32
 33        public void SetDictionary(SafeZstdCDictHandle dictionary)
 34        {
 35            Debug.Assert(_dictionary == null);
 36            Debug.Assert(dictionary != null);
 37
 38            bool added = false;
 39            try
 40            {
 41                dictionary.DangerousAddRef(ref added);
 42                ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_CCtx_refCDict(this, dictionary));
 43
 44                _dictionary = dictionary;
 45            }
 46            catch when (added)
 47            {
 48                dictionary.DangerousRelease();
 49                throw;
 50            }
 51        }
 52
 53        public unsafe nuint SetPrefix(ReadOnlyMemory<byte> prefix)
 54        {
 55            MemoryHandle handle = prefix.Pin();
 56
 57            nuint result = Interop.Zstd.ZSTD_CCtx_refPrefix(this, (byte*)handle.Pointer, (nuint)prefix.Length);
 58
 59            if (Interop.Zstd.ZSTD_isError(result) != 0)
 60            {
 61                handle.Dispose();
 62            }
 63            else
 64            {
 65                _prefixHandle?.Dispose();
 66                _prefixHandle = handle;
 67            }
 68
 69            return result;
 70        }
 71
 72        public unsafe void Reset()
 73        {
 74            ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_CCtx_reset(this, Interop.Zstd.ZstdResetDirective.ZSTD_reset_se
 75
 76            // prefix is not sticky and is cleared by reset
 77            if (_prefixHandle != null)
 78            {
 79                _prefixHandle.Value.Dispose();
 80                _prefixHandle = null;
 81            }
 82        }
 83
 84        public override bool IsInvalid => handle == IntPtr.Zero;
 85    }
 86
 87    internal sealed class SafeZstdDecompressHandle : SafeHandle
 88    {
 89        internal SafeZstdDDictHandle? _dictionary;
 90        internal MemoryHandle? _prefixHandle;
 091        public SafeZstdDecompressHandle() : base(IntPtr.Zero, true) { }
 92
 93        protected override bool ReleaseHandle()
 094        {
 095            Interop.Zstd.ZSTD_freeDCtx(handle);
 96
 97            // release the addref we took in SetDictionary
 098            _dictionary?.DangerousRelease();
 99
 0100            if (_prefixHandle != null)
 0101            {
 0102                _prefixHandle.Value.Dispose();
 0103                _prefixHandle = null;
 0104            }
 0105            return true;
 0106        }
 107
 108        public void SetDictionary(SafeZstdDDictHandle dictionary)
 0109        {
 0110            Debug.Assert(_dictionary == null);
 0111            Debug.Assert(dictionary != null);
 112
 0113            bool added = false;
 114            try
 0115            {
 0116                dictionary.DangerousAddRef(ref added);
 0117                ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_DCtx_refDDict(this, dictionary));
 118
 0119                _dictionary = dictionary;
 0120            }
 0121            catch when (added)
 0122            {
 0123                dictionary.DangerousRelease();
 0124                throw;
 125            }
 0126        }
 127
 128        public unsafe nuint SetPrefix(ReadOnlyMemory<byte> prefix)
 0129        {
 0130            MemoryHandle handle = prefix.Pin();
 131
 0132            nuint result = Interop.Zstd.ZSTD_DCtx_refPrefix(this, (byte*)handle.Pointer, (nuint)prefix.Length);
 133
 0134            if (Interop.Zstd.ZSTD_isError(result) != 0)
 0135            {
 0136                handle.Dispose();
 0137            }
 138            else
 0139            {
 0140                _prefixHandle?.Dispose();
 0141                _prefixHandle = handle;
 0142            }
 143
 0144            return result;
 0145        }
 146
 147        public unsafe void Reset()
 0148        {
 0149            ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_DCtx_reset(this, Interop.Zstd.ZstdResetDirective.ZSTD_reset_se
 150
 151            // prefix is not sticky and is cleared by reset
 0152            if (_prefixHandle != null)
 0153            {
 0154                _prefixHandle.Value.Dispose();
 0155                _prefixHandle = null;
 0156            }
 0157        }
 158
 0159        public override bool IsInvalid => handle == IntPtr.Zero;
 160    }
 161
 162    internal sealed class SafeZstdCDictHandle : SafeHandle
 163    {
 164        public SafeZstdCDictHandle() : base(IntPtr.Zero, true) { }
 165
 166        internal PinnedGCHandle<byte[]> _pinnedData;
 167
 168        protected override bool ReleaseHandle()
 169        {
 170            Interop.Zstd.ZSTD_freeCDict(handle);
 171            _pinnedData.Dispose();
 172            return true;
 173        }
 174
 175        public override bool IsInvalid => handle == IntPtr.Zero;
 176    }
 177
 178    internal sealed class SafeZstdDDictHandle : SafeHandle
 179    {
 180        public SafeZstdDDictHandle() : base(IntPtr.Zero, true) { }
 181
 182        internal PinnedGCHandle<byte[]> _pinnedData;
 183
 184        protected override bool ReleaseHandle()
 185        {
 186            Interop.Zstd.ZSTD_freeDDict(handle);
 187            _pinnedData.Dispose();
 188            return true;
 189        }
 190
 191        public override bool IsInvalid => handle == IntPtr.Zero;
 192    }
 193}