forked from ZEISS/czicompress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PInvokeFileProcessor.cs
222 lines (184 loc) · 7.68 KB
/
PInvokeFileProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
//
// SPDX-License-Identifier: GPL-3.0-or-later
namespace netczicompress.Models;
using System;
using System.Buffers;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
/// <summary>
/// An <see cref="IFileProcessor"/> that uses the native libczicompressc C API to do the actual work.
/// </summary>
public sealed partial class PInvokeFileProcessor : IFileProcessor
{
private IntPtr nativeFileProcessor;
/// <summary>
/// Initializes a new instance of the <see cref="PInvokeFileProcessor"/> class.
/// </summary>
/// <param name="mode">The compression mode to use, not all modes may be supported.</param>
/// <param name="options">The processing mode to use.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="mode"/> is <see cref="CompressionMode.NoOp"/>.</exception>
/// <exception cref="ApplicationException">Thrown when the native file processor cannot be created.</exception>
public PInvokeFileProcessor(CompressionMode mode, ProcessingOptions options)
{
if (mode == CompressionMode.NoOp)
{
throw new ArgumentOutOfRangeException(nameof(mode));
}
NativeMethods.Command cmd = mode switch
{
CompressionMode.Decompress => NativeMethods.Command.Decompress,
_ => NativeMethods.Command.Compress,
};
NativeMethods.CompressionStrategy strategy = mode switch
{
CompressionMode.CompressAll => NativeMethods.CompressionStrategy.All,
CompressionMode.CompressUncompressedAndZstd =>
NativeMethods.CompressionStrategy.UncompressedAndZStdCompressed,
_ => NativeMethods.CompressionStrategy.OnlyUncompressed,
};
this.nativeFileProcessor = NativeMethods.CreateFileProcessor(cmd, strategy, options.CompressionLevel.Value);
if (this.nativeFileProcessor == IntPtr.Zero)
{
throw new ApplicationException("Failed to create native file processor.");
}
}
public bool NeedsExistingOutputDirectory => true;
private bool IsDisposed
{
get => this.nativeFileProcessor == IntPtr.Zero;
}
public static IFileProcessor Create(CompressionMode mode, ProcessingOptions options)
{
return new PInvokeFileProcessor(mode, options);
}
public static string GetLibFullName() => NativeMethods.GetLibFullName();
public void ProcessFile(string inputPath, string outputPath, ReportProgress progressReport, CancellationToken token)
{
this.CheckDisposed();
var errorMessageBuffer = ArrayPool<byte>.Shared.Rent(1024);
var errorMessageLength = errorMessageBuffer.Length;
bool ReportProgressCheckCancelImpl(int progress)
{
progressReport(progress);
return !token.IsCancellationRequested;
}
var result = NativeMethods.ProcessFile(
this.nativeFileProcessor,
inputPath,
outputPath,
errorMessageBuffer,
ref errorMessageLength,
ReportProgressCheckCancelImpl);
GC.KeepAlive(this.nativeFileProcessor);
GC.KeepAlive(errorMessageBuffer);
GC.KeepAlive(errorMessageLength);
GC.KeepAlive(progressReport);
if (result != 0)
{
var message = Encoding.UTF8.GetString(errorMessageBuffer, 0, errorMessageLength);
throw new IOException(message);
}
// Make sure that we don't report cancellation as success, even if the native lib returns 0 when canceled.
token.ThrowIfCancellationRequested();
}
public void Dispose()
{
if (!this.IsDisposed)
{
NativeMethods.DestroyFileProcessor(this.nativeFileProcessor);
}
this.nativeFileProcessor = IntPtr.Zero;
}
private void CheckDisposed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException(this.ToString());
}
}
internal static partial class NativeMethods
{
private const string LibName = "libczicompressc";
private const StringMarshalling LibStringEncoding = StringMarshalling.Utf8;
static NativeMethods()
{
GetLibVersion(out int major, out int minor, out int patch);
var actual = (major, minor);
(int Major, int Minor) expected = (0, 5);
bool isCompatible = major == 0
? actual == expected
: major == expected.Major && minor >= expected.Minor;
if (!isCompatible)
{
throw new InvalidOperationException($"Expecting {LibName} {nameof(GetLibVersion)} to be compatible with {expected} but found {actual}.");
}
#if !DEBUG
var fullName = GetLibFullName();
if (fullName.Contains("DEBUG"))
{
throw new InvalidOperationException($"You must not use {fullName} in a Release build of {nameof(netczicompress)}. Use a Release build of the library.");
}
#endif
}
/// <summary>
/// Delegate that is invoked by processing function to provide updates.
/// </summary>
/// <param name="progressPercent">The progress in percent.</param>
/// <returns>True if operation should continue; false if canceled.</returns>
public delegate bool ReportProgressCheckCancel(int progressPercent);
public enum Command
{
Compress = 1,
Decompress = 2,
}
public enum CompressionStrategy
{
All = 1,
OnlyUncompressed = 2,
UncompressedAndZStdCompressed = 3,
}
public static string GetLibFullName(int suggestedBufferLength = 64)
{
checked
{
var buffer = ArrayPool<byte>.Shared.Rent(suggestedBufferLength);
ulong bufferLength = (ulong)buffer.Length;
if (NativeMethods.GetLibVersionString(buffer, ref bufferLength))
{
var version = Encoding.UTF8.GetString(buffer, 0, buffer.Count(b => b != 0));
return $"{LibName} {version}";
}
else if (bufferLength > (ulong)suggestedBufferLength)
{
// This means that the buffer was too small
return GetLibFullName((int)bufferLength);
}
else
{
throw new Exception($"Failed to determine version of {LibName}");
}
}
}
[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool GetLibVersionString(byte[] buffer, ref ulong bufferLength);
[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
public static partial void GetLibVersion(out int major, out int minor, out int patch);
[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
public static partial IntPtr CreateFileProcessor(Command command, CompressionStrategy strategy, int compressionLevel);
[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
public static partial void DestroyFileProcessor(IntPtr file_processor);
[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
public static partial int ProcessFile(
IntPtr file_processor,
string input_path,
string output_path,
byte[] error_message,
ref int error_message_length,
ReportProgressCheckCancel reportProgress);
}
}