-
Notifications
You must be signed in to change notification settings - Fork 641
/
NativeMethods.cs
409 lines (360 loc) · 16.6 KB
/
NativeMethods.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace Microsoft.Deployment.Compression.Cab
{
using System;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Native DllImport methods and related structures and constants used for
/// cabinet creation and extraction via cabinet.dll.
/// </summary>
internal static class NativeMethods
{
/// <summary>
/// A direct import of constants, enums, structures, delegates, and functions from fci.h.
/// Refer to comments in fci.h for documentation.
/// </summary>
internal static class FCI
{
internal const int MIN_DISK = 32768;
internal const int MAX_DISK = Int32.MaxValue;
internal const int MAX_FOLDER = 0x7FFF8000;
internal const int MAX_FILENAME = 256;
internal const int MAX_CABINET_NAME = 256;
internal const int MAX_CAB_PATH = 256;
internal const int MAX_DISK_NAME = 256;
internal const int CPU_80386 = 1;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate IntPtr PFNALLOC(int cb);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void PFNFREE(IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNOPEN(string path, int oflag, int pmode, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNREAD(int fileHandle, IntPtr memory, int cb, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNWRITE(int fileHandle, IntPtr memory, int cb, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNCLOSE(int fileHandle, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNSEEK(int fileHandle, int dist, int seekType, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNDELETE(string path, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNGETNEXTCABINET(IntPtr pccab, uint cbPrevCab, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNFILEPLACED(IntPtr pccab, string path, long fileSize, int continuation, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNGETOPENINFO(string path, out short date, out short time, out short pattribs, out int err, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNSTATUS(STATUS typeStatus, uint cb1, uint cb2, IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNGETTEMPFILE(IntPtr tempNamePtr, int tempNameSize, IntPtr pv);
/// <summary>
/// Error codes that can be returned by FCI.
/// </summary>
internal enum ERROR : int
{
NONE,
OPEN_SRC,
READ_SRC,
ALLOC_FAIL,
TEMP_FILE,
BAD_COMPR_TYPE,
CAB_FILE,
USER_ABORT,
MCI_FAIL,
}
/// <summary>
/// FCI compression algorithm types and parameters.
/// </summary>
internal enum TCOMP : ushort
{
MASK_TYPE = 0x000F,
TYPE_NONE = 0x0000,
TYPE_MSZIP = 0x0001,
TYPE_QUANTUM = 0x0002,
TYPE_LZX = 0x0003,
BAD = 0x000F,
MASK_LZX_WINDOW = 0x1F00,
LZX_WINDOW_LO = 0x0F00,
LZX_WINDOW_HI = 0x1500,
SHIFT_LZX_WINDOW = 0x0008,
MASK_QUANTUM_LEVEL = 0x00F0,
QUANTUM_LEVEL_LO = 0x0010,
QUANTUM_LEVEL_HI = 0x0070,
SHIFT_QUANTUM_LEVEL = 0x0004,
MASK_QUANTUM_MEM = 0x1F00,
QUANTUM_MEM_LO = 0x0A00,
QUANTUM_MEM_HI = 0x1500,
SHIFT_QUANTUM_MEM = 0x0008,
MASK_RESERVED = 0xE000,
}
/// <summary>
/// Reason for FCI status callback.
/// </summary>
internal enum STATUS : uint
{
FILE = 0,
FOLDER = 1,
CABINET = 2,
}
[SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments")]
[DllImport("cabinet.dll", EntryPoint = "FCICreate", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern Handle Create(IntPtr perf, PFNFILEPLACED pfnfcifp, PFNALLOC pfna, PFNFREE pfnf, PFNOPEN pfnopen, PFNREAD pfnread, PFNWRITE pfnwrite, PFNCLOSE pfnclose, PFNSEEK pfnseek, PFNDELETE pfndelete, PFNGETTEMPFILE pfnfcigtf, [MarshalAs(UnmanagedType.LPStruct)] CCAB pccab, IntPtr pv);
[DllImport("cabinet.dll", EntryPoint = "FCIAddFile", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern int AddFile(Handle hfci, string pszSourceFile, IntPtr pszFileName, [MarshalAs(UnmanagedType.Bool)] bool fExecute, PFNGETNEXTCABINET pfnfcignc, PFNSTATUS pfnfcis, PFNGETOPENINFO pfnfcigoi, TCOMP typeCompress);
[DllImport("cabinet.dll", EntryPoint = "FCIFlushCabinet", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern int FlushCabinet(Handle hfci, [MarshalAs(UnmanagedType.Bool)] bool fGetNextCab, PFNGETNEXTCABINET pfnfcignc, PFNSTATUS pfnfcis);
[DllImport("cabinet.dll", EntryPoint = "FCIFlushFolder", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern int FlushFolder(Handle hfci, PFNGETNEXTCABINET pfnfcignc, PFNSTATUS pfnfcis);
[SuppressUnmanagedCodeSecurity]
[DllImport("cabinet.dll", EntryPoint = "FCIDestroy", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool Destroy(IntPtr hfci);
/// <summary>
/// Cabinet information structure used for FCI initialization and GetNextCabinet callback.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class CCAB
{
internal int cb = MAX_DISK;
internal int cbFolderThresh = MAX_FOLDER;
internal int cbReserveCFHeader;
internal int cbReserveCFFolder;
internal int cbReserveCFData;
internal int iCab;
internal int iDisk;
internal int fFailOnIncompressible;
internal short setID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_DISK_NAME )] internal string szDisk = String.Empty;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_CABINET_NAME)] internal string szCab = String.Empty;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_CAB_PATH )] internal string szCabPath = String.Empty;
}
/// <summary>
/// Ensures that the FCI handle is safely released.
/// </summary>
internal class Handle : SafeHandle
{
/// <summary>
/// Creates a new unintialized handle. The handle will be initialized
/// when it is marshalled back from native code.
/// </summary>
internal Handle()
: base(IntPtr.Zero, true)
{
}
/// <summary>
/// Checks if the handle is invalid. An FCI handle is invalid when it is zero.
/// </summary>
public override bool IsInvalid
{
get
{
return this.handle == IntPtr.Zero;
}
}
/// <summary>
/// Releases the handle by calling FDIDestroy().
/// </summary>
/// <returns>True if the release succeeded.</returns>
[SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)]
protected override bool ReleaseHandle()
{
return FCI.Destroy(this.handle);
}
}
}
/// <summary>
/// A direct import of constants, enums, structures, delegates, and functions from fdi.h.
/// Refer to comments in fdi.h for documentation.
/// </summary>
internal static class FDI
{
internal const int MAX_DISK = Int32.MaxValue;
internal const int MAX_FILENAME = 256;
internal const int MAX_CABINET_NAME = 256;
internal const int MAX_CAB_PATH = 256;
internal const int MAX_DISK_NAME = 256;
internal const int CPU_80386 = 1;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate IntPtr PFNALLOC(int cb);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void PFNFREE(IntPtr pv);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNOPEN(string path, int oflag, int pmode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNREAD(int hf, IntPtr pv, int cb);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNWRITE(int hf, IntPtr pv, int cb);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNCLOSE(int hf);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNSEEK(int hf, int dist, int seektype);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int PFNNOTIFY(NOTIFICATIONTYPE fdint, NOTIFICATION fdin);
/// <summary>
/// Error codes that can be returned by FDI.
/// </summary>
internal enum ERROR : int
{
NONE,
CABINET_NOT_FOUND,
NOT_A_CABINET,
UNKNOWN_CABINET_VERSION,
CORRUPT_CABINET,
ALLOC_FAIL,
BAD_COMPR_TYPE,
MDI_FAIL,
TARGET_FILE,
RESERVE_MISMATCH,
WRONG_CABINET,
USER_ABORT,
}
/// <summary>
/// Type of notification message for the FDI Notify callback.
/// </summary>
internal enum NOTIFICATIONTYPE : int
{
CABINET_INFO,
PARTIAL_FILE,
COPY_FILE,
CLOSE_FILE_INFO,
NEXT_CABINET,
ENUMERATE,
}
[DllImport("cabinet.dll", EntryPoint = "FDICreate", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern Handle Create([MarshalAs(UnmanagedType.FunctionPtr)] PFNALLOC pfnalloc, [MarshalAs(UnmanagedType.FunctionPtr)] PFNFREE pfnfree, PFNOPEN pfnopen, PFNREAD pfnread, PFNWRITE pfnwrite, PFNCLOSE pfnclose, PFNSEEK pfnseek, int cpuType, IntPtr perf);
[DllImport("cabinet.dll", EntryPoint = "FDICopy", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Copy(Handle hfdi, string pszCabinet, string pszCabPath, int flags, PFNNOTIFY pfnfdin, IntPtr pfnfdid, IntPtr pvUser);
[SuppressUnmanagedCodeSecurity]
[DllImport("cabinet.dll", EntryPoint = "FDIDestroy", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool Destroy(IntPtr hfdi);
[DllImport("cabinet.dll", EntryPoint = "FDIIsCabinet", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, CallingConvention = CallingConvention.Cdecl)]
[SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Justification="FDI file handles definitely remain 4 bytes on 64bit platforms.")]
internal static extern int IsCabinet(Handle hfdi, int hf, out CABINFO pfdici);
/// <summary>
/// Cabinet information structure filled in by FDI IsCabinet.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct CABINFO
{
internal int cbCabinet;
internal short cFolders;
internal short cFiles;
internal short setID;
internal short iCabinet;
internal int fReserve;
internal int hasprev;
internal int hasnext;
}
/// <summary>
/// Cabinet notification details passed to the FDI Notify callback.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class NOTIFICATION
{
internal int cb;
internal IntPtr psz1;
internal IntPtr psz2;
internal IntPtr psz3;
internal IntPtr pv;
internal IntPtr hf_ptr;
internal short date;
internal short time;
internal short attribs;
internal short setID;
internal short iCabinet;
internal short iFolder;
internal int fdie;
// Unlike all the other file handles in FCI/FDI, this one is
// actually pointer-sized. Use a property to pretend it isn't.
internal int hf
{
get { return (int) this.hf_ptr; }
}
}
/// <summary>
/// Ensures that the FDI handle is safely released.
/// </summary>
internal class Handle : SafeHandle
{
/// <summary>
/// Creates a new unintialized handle. The handle will be initialized
/// when it is marshalled back from native code.
/// </summary>
internal Handle()
: base(IntPtr.Zero, true)
{
}
/// <summary>
/// Checks if the handle is invalid. An FDI handle is invalid when it is zero.
/// </summary>
public override bool IsInvalid
{
get
{
return this.handle == IntPtr.Zero;
}
}
/// <summary>
/// Releases the handle by calling FDIDestroy().
/// </summary>
/// <returns>True if the release succeeded.</returns>
protected override bool ReleaseHandle()
{
return FDI.Destroy(this.handle);
}
}
}
/// <summary>
/// Error info structure for FCI and FDI.
/// </summary>
/// <remarks>Before being passed to FCI or FDI, this structure is
/// pinned in memory via a GCHandle. The pinning is necessary
/// to be able to read the results, since the ERF structure doesn't
/// get marshalled back out after an error.</remarks>
[StructLayout(LayoutKind.Sequential)]
internal class ERF
{
private int erfOper;
private int erfType;
private int fError;
/// <summary>
/// Gets or sets the cabinet error code.
/// </summary>
internal int Oper
{
get
{
return this.erfOper;
}
set
{
this.erfOper = value;
}
}
/// <summary>
/// Gets or sets the Win32 error code.
/// </summary>
internal int Type
{
get
{
return this.erfType;
}
set
{
this.erfType = value;
}
}
/// <summary>
/// GCHandle doesn't like the bool type, so use an int underneath.
/// </summary>
internal bool Error
{
get
{
return this.fError != 0;
}
set
{
this.fError = value ? 1 : 0;
}
}
/// <summary>
/// Clears the error information.
/// </summary>
internal void Clear()
{
this.Oper = 0;
this.Type = 0;
this.Error = false;
}
}
}
}