| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.IO.Compression; |
| | | 9 | | |
| | | 10 | | namespace 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; |
| | 0 | 91 | | public SafeZstdDecompressHandle() : base(IntPtr.Zero, true) { } |
| | | 92 | | |
| | | 93 | | protected override bool ReleaseHandle() |
| | 0 | 94 | | { |
| | 0 | 95 | | Interop.Zstd.ZSTD_freeDCtx(handle); |
| | | 96 | | |
| | | 97 | | // release the addref we took in SetDictionary |
| | 0 | 98 | | _dictionary?.DangerousRelease(); |
| | | 99 | | |
| | 0 | 100 | | if (_prefixHandle != null) |
| | 0 | 101 | | { |
| | 0 | 102 | | _prefixHandle.Value.Dispose(); |
| | 0 | 103 | | _prefixHandle = null; |
| | 0 | 104 | | } |
| | 0 | 105 | | return true; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public void SetDictionary(SafeZstdDDictHandle dictionary) |
| | 0 | 109 | | { |
| | 0 | 110 | | Debug.Assert(_dictionary == null); |
| | 0 | 111 | | Debug.Assert(dictionary != null); |
| | | 112 | | |
| | 0 | 113 | | bool added = false; |
| | | 114 | | try |
| | 0 | 115 | | { |
| | 0 | 116 | | dictionary.DangerousAddRef(ref added); |
| | 0 | 117 | | ZstandardUtils.ThrowIfError(Interop.Zstd.ZSTD_DCtx_refDDict(this, dictionary)); |
| | | 118 | | |
| | 0 | 119 | | _dictionary = dictionary; |
| | 0 | 120 | | } |
| | 0 | 121 | | catch when (added) |
| | 0 | 122 | | { |
| | 0 | 123 | | dictionary.DangerousRelease(); |
| | 0 | 124 | | throw; |
| | | 125 | | } |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | public unsafe nuint SetPrefix(ReadOnlyMemory<byte> prefix) |
| | 0 | 129 | | { |
| | 0 | 130 | | MemoryHandle handle = prefix.Pin(); |
| | | 131 | | |
| | 0 | 132 | | nuint result = Interop.Zstd.ZSTD_DCtx_refPrefix(this, (byte*)handle.Pointer, (nuint)prefix.Length); |
| | | 133 | | |
| | 0 | 134 | | if (Interop.Zstd.ZSTD_isError(result) != 0) |
| | 0 | 135 | | { |
| | 0 | 136 | | handle.Dispose(); |
| | 0 | 137 | | } |
| | | 138 | | else |
| | 0 | 139 | | { |
| | 0 | 140 | | _prefixHandle?.Dispose(); |
| | 0 | 141 | | _prefixHandle = handle; |
| | 0 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | return result; |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | public unsafe void Reset() |
| | 0 | 148 | | { |
| | 0 | 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 |
| | 0 | 152 | | if (_prefixHandle != null) |
| | 0 | 153 | | { |
| | 0 | 154 | | _prefixHandle.Value.Dispose(); |
| | 0 | 155 | | _prefixHandle = null; |
| | 0 | 156 | | } |
| | 0 | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | 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 | | } |