< Summary

Information
Class: Microsoft.Win32.SafeHandles.SafeZstdCDictHandle
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: 7
Coverable lines: 7
Total lines: 193
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
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()100%110%

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;
 91        public SafeZstdDecompressHandle() : base(IntPtr.Zero, true) { }
 92
 93        protected override bool ReleaseHandle()
 94        {
 95            Interop.Zstd.ZSTD_freeDCtx(handle);
 96
 97            // release the addref we took in SetDictionary
 98            _dictionary?.DangerousRelease();
 99
 100            if (_prefixHandle != null)
 101            {
 102                _prefixHandle.Value.Dispose();
 103                _prefixHandle = null;
 104            }
 105            return true;
 106        }
 107
 108        public void SetDictionary(SafeZstdDDictHandle dictionary)
 109        {
 110            Debug.Assert(_dictionary == null);
 111            Debug.Assert(dictionary != null);
 112
 113            bool added = false;
 114            try
 115            {
 116                dictionary.DangerousAddRef(ref added);
 117                ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_DCtx_refDDict(this, dictionary));
 118
 119                _dictionary = dictionary;
 120            }
 121            catch when (added)
 122            {
 123                dictionary.DangerousRelease();
 124                throw;
 125            }
 126        }
 127
 128        public unsafe nuint SetPrefix(ReadOnlyMemory<byte> prefix)
 129        {
 130            MemoryHandle handle = prefix.Pin();
 131
 132            nuint result = Interop.Zstd.ZSTD_DCtx_refPrefix(this, (byte*)handle.Pointer, (nuint)prefix.Length);
 133
 134            if (Interop.Zstd.ZSTD_isError(result) != 0)
 135            {
 136                handle.Dispose();
 137            }
 138            else
 139            {
 140                _prefixHandle?.Dispose();
 141                _prefixHandle = handle;
 142            }
 143
 144            return result;
 145        }
 146
 147        public unsafe void Reset()
 148        {
 149            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
 152            if (_prefixHandle != null)
 153            {
 154                _prefixHandle.Value.Dispose();
 155                _prefixHandle = null;
 156            }
 157        }
 158
 159        public override bool IsInvalid => handle == IntPtr.Zero;
 160    }
 161
 162    internal sealed class SafeZstdCDictHandle : SafeHandle
 163    {
 0164        public SafeZstdCDictHandle() : base(IntPtr.Zero, true) { }
 165
 166        internal PinnedGCHandle<byte[]> _pinnedData;
 167
 168        protected override bool ReleaseHandle()
 0169        {
 0170            Interop.Zstd.ZSTD_freeCDict(handle);
 0171            _pinnedData.Dispose();
 0172            return true;
 0173        }
 174
 0175        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}

Methods/Properties

.ctor()
ReleaseHandle()
IsInvalid()