-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathCabinetHelper.h
545 lines (418 loc) · 10.5 KB
/
CabinetHelper.h
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#pragma once
#include <fdi.h>
#include <Fci.h>
#pragma comment(lib,"cabinet.lib")
#include <io.h>
#include <fcntl.h>
#include <comdef.h>
#include <atlstr.h>
#include <Strsafe.h>
#include "Base.h"
#include "StringHelper.h"
//#define _IsDots(FileName) (((FileName)[0] == L'.' &&((FileName)[1] == NULL ||((FileName)[1] == L'.'&&(FileName)[2] == NULL))))
static FNOPEN(fnFileOpen)
{
DWORD dwDesiredAccess = 0;
DWORD dwCreationDisposition = 0;
UNREFERENCED_PARAMETER(pmode);
if (oflag & _O_RDWR)
{
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
}
else if (oflag & _O_WRONLY)
{
dwDesiredAccess = GENERIC_WRITE;
}
else
{
dwDesiredAccess = GENERIC_READ;
}
if (oflag & _O_CREAT)
{
dwCreationDisposition = CREATE_ALWAYS;
}
else
{
dwCreationDisposition = OPEN_EXISTING;
}
return (INT_PTR)CreateFile(UTF8ToUnicode( pszFile),
dwDesiredAccess,
FILE_SHARE_READ,
NULL,
dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS,
NULL);
}
static FNREAD(fnFileRead)
{
DWORD dwBytesRead = 0;
return ReadFile((HANDLE)hf, pv, cb, &dwBytesRead, NULL) ? dwBytesRead : -1;
}
static FNWRITE(fnFileWrite)
{
DWORD dwBytesWritten = 0;
if (WriteFile((HANDLE)hf, pv, cb, &dwBytesWritten, NULL) == FALSE)
{
dwBytesWritten = (DWORD)-1;
}
return dwBytesWritten;
}
static FNCLOSE(fnFileClose)
{
return CloseHandle((HANDLE)hf) ? 0 : -1;
}
static FNSEEK(fnFileSeek)
{
return SetFilePointer((HANDLE)hf, dist, NULL, seektype);
}
#define FDICreate2(pErf) FDICreate((PFNALLOC)malloc,(PFNFREE)free,(PFNOPEN)fnFileOpen,(PFNREAD)fnFileRead,(PFNWRITE)fnFileWrite,(PFNCLOSE)fnFileClose,(PFNSEEK)fnFileSeek,cpuUNKNOWN,pErf)
static FNFCIFILEPLACED(fnFCIFilePlaced)
{
UNREFERENCED_PARAMETER(pv);
return 0;
}
static FNFCIOPEN(fnFCIFileOpen)
{
UNREFERENCED_PARAMETER(pv);
auto hFile=fnFileOpen(pszFile, oflag, pmode);
if (hFile == -1)
{
*err = GetLastError();
}
return (INT_PTR)hFile;
}
static FNFCIREAD(fnFCIFileRead)
{
DWORD dwBytesRead = 0;
UNREFERENCED_PARAMETER(pv);
if (!ReadFile((HANDLE)hf, memory, cb, &dwBytesRead, NULL))
{
dwBytesRead = (DWORD)-1;
*err = GetLastError();
}
return dwBytesRead;
}
static FNFCIWRITE(fnFCIFileWrite)
{
DWORD dwBytesWritten = 0;
UNREFERENCED_PARAMETER(pv);
if (!WriteFile((HANDLE)hf, memory, cb, &dwBytesWritten, NULL))
{
dwBytesWritten = (DWORD)-1;
*err = GetLastError();
}
return dwBytesWritten;
}
static FNFCIDELETE(fnFCIFileDelete)
{
INT iResult = 0;
UNREFERENCED_PARAMETER(pv);
if (DeleteFileW(UTF8ToUnicode( pszFile)) == FALSE)
{
*err = GetLastError();
iResult = -1;
}
return iResult;
}
static FNFCIGETTEMPFILE(fnFCIGetTempFileName)
{
BOOL bSucceeded = FALSE;
CHAR pszTempPath[MAX_PATH];
CHAR pszTempFile[MAX_PATH];
UNREFERENCED_PARAMETER(pv);
UNREFERENCED_PARAMETER(cbTempName);
if (GetTempPathA(MAX_PATH, pszTempPath) != 0)
{
if (GetTempFileNameA(pszTempPath, "CABINET", 0, pszTempFile) != 0)
{
DeleteFileA(pszTempFile);
bSucceeded = SUCCEEDED(StringCbCopyA(pszTempName, cbTempName, pszTempFile));
}
}
return bSucceeded;
}
static FNFCICLOSE(fnFCIFileClose)
{
INT iResult = 0;
UNREFERENCED_PARAMETER(pv);
if (CloseHandle((HANDLE)hf) == FALSE)
{
*err = GetLastError();
iResult = -1;
}
return iResult;
}
static FNFCISEEK(fnFCIFileSeek)
{
INT iResult = 0;
UNREFERENCED_PARAMETER(pv);
iResult = SetFilePointer((HANDLE)hf, dist, NULL, seektype);
if (iResult == -1)
{
*err = GetLastError();
}
return iResult;
}
static FNFCIGETNEXTCABINET(fnFDIGetNextCabinet)
{
return FALSE;
}
static FNFCISTATUS(fnFCIStatus)
{
return ERROR_SUCCESS;
}
static FNFCIGETOPENINFO(fnFCIGetOpenInfo)
{
FILETIME fileTime;
BY_HANDLE_FILE_INFORMATION fileInfo;
auto hFile = (HANDLE)fnFCIFileOpen(pszName, _O_RDONLY, 0, err, pv);
if (hFile != (HANDLE)-1)
{
if (GetFileInformationByHandle(hFile, &fileInfo)
&& FileTimeToLocalFileTime(&fileInfo.ftCreationTime, &fileTime)
&& FileTimeToDosDateTime(&fileTime, pdate, ptime))
{
*pattribs = (USHORT)fileInfo.dwFileAttributes;
*pattribs &= (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_ARCH);
}
else
{
fnFCIFileClose((INT_PTR)hFile, err, pv);
hFile = (HANDLE)-1;
}
}
return (INT_PTR)hFile;
}
#define FCICreate2(pErf,ccab) FCICreate(pErf,fnFCIFilePlaced,(PFNALLOC)malloc,(PFNFREE)free,(PFNFCIOPEN)fnFCIFileOpen,(PFNFCIREAD)fnFCIFileRead,(PFNFCIWRITE)fnFCIFileWrite,(PFNFCICLOSE)fnFCIFileClose,(PFNFCISEEK)fnFCIFileSeek,fnFCIFileDelete,fnFCIGetTempFileName,&ccab,NULL)
_Check_return_
static LSTATUS CabExtractFile(
_In_z_ LPCWSTR CabFilePath,
_In_z_ LPCWSTR ExtractPath
)
{
ERF erf;
auto hfdi= FDICreate2(&erf);
if (!hfdi)
return ERROR_FUNCTION_FAILED;
auto szExtractPathUTF8 = Unicode2UTF8(ExtractPath);
if (szExtractPathUTF8.IsEmpty())
return ERROR_PATH_NOT_FOUND;
if (szExtractPathUTF8[szExtractPathUTF8.GetLength() - 1] != '\\')
szExtractPathUTF8 += '\\';
LSTATUS lStatus = ERROR_SUCCESS;
if (!FDICopy(hfdi, (LPSTR)Unicode2UTF8( CabFilePath).GetString(), (LPSTR)"", 0, [](FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)->INT_PTR
{
INT_PTR iResult = 0;
switch (fdint)
{
case fdintCOPY_FILE:
{
CStringA FilePath = (LPCSTR)pfdin->pv;
FilePath += pfdin->psz1;
CreateRoot(UTF8ToUnicode(FilePath, FilePath.GetLength()));
iResult = fnFileOpen(FilePath.GetBuffer(), _O_WRONLY | _O_CREAT, 0);
break;
}
case fdintCLOSE_FILE_INFO:
{
//pfdin->hf
//auto pData = (vector<BYTE>*) ((void**)pfdin->pv)[1];
//pData->_Mylast = pData->_Myfirst + pfdin->cb;
//pfdin->hf = NULL;
iResult = !fnFileClose(pfdin->hf);
break;
}
case fdintNEXT_CABINET:
break;
case fdintPARTIAL_FILE:
iResult = 0;
break;
case fdintCABINET_INFO:
iResult = 0;
break;
case fdintENUMERATE:
iResult = 0;
break;
default:
iResult = -1;
break;
}
return iResult;
}, NULL, (void*)szExtractPathUTF8.GetString()))
{
lStatus = ERROR_FUNCTION_FAILED;
}
FDIDestroy(hfdi);
return lStatus;
}
_Check_return_
static LSTATUS CabExtractFile(
_In_z_ LPCSTR CabFilePath,
_In_z_ LPCSTR ExtractPath
)
{
return CabExtractFile(CStringW(CabFilePath), CStringW(ExtractPath));
}
//此接口仅内部调用
_Check_return_
static LSTATUS CabCreateFileInternal_U(
_In_ HFCI hfci,
_In_ int chRoot,
_In_ CStringA FilePath,
_In_ TCOMP typeCompress
)
{
WIN32_FIND_DATAW FindData;
HANDLE hFileFind = FindFirstFileW(UTF8ToUnicode( FilePath) + L'*', &FindData);
if (hFileFind == INVALID_HANDLE_VALUE)
return GetLastError();
LSTATUS lStatus = ERROR_SUCCESS;
do
{
auto TempFileName = FilePath + Unicode2UTF8( FindData.cFileName);
if (FindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if (_IsDots(FindData.cFileName))
continue;
TempFileName += '\\';
lStatus = CabCreateFileInternal_U(hfci, chRoot, TempFileName, typeCompress);
if (lStatus != ERROR_SUCCESS)
break;
}
else
{
if (!FCIAddFile(hfci, TempFileName.GetBuffer(), TempFileName.GetBuffer() + chRoot, FALSE, NULL, fnFCIStatus, fnFCIGetOpenInfo, typeCompress))
{
lStatus = ERROR_FUNCTION_FAILED;
break;
}
}
} while (FindNextFileW(hFileFind,&FindData));
FindClose(hFileFind);
return lStatus;
}
//将一个文件夹压缩为cab
_Check_return_
static LSTATUS CabCreateDirectory_U(
_In_z_ LPCSTR CabFilePath,
_In_ CStringA RootPath,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
if (RootPath.IsEmpty())
return ERROR_INVALID_PARAMETER;
if (RootPath[RootPath.GetLength() - 1] != '\\')
RootPath += '\\';
if (GetFileType(CabFilePath))
{
if (!DeleteFileW( UTF8ToUnicode( CabFilePath)))
return GetLastError();
}
ERF erf; //FCI error structure
CCAB ccab = { 0 ,900000 }; //cabinet information structure
ccab.setID = 12345;
ccab.iCab = 1;
ccab.iDisk = 0;
auto FileName = PathFindFileNameA(CabFilePath);
StrCpyA(ccab.szCab, FileName);
StrCpyNA(ccab.szCabPath, CabFilePath, FileName - CabFilePath +1);
HFCI hfci = FCICreate2(&erf, ccab);
if (hfci == NULL)
{
return ERROR_FUNCTION_FAILED;
}
auto lStatus = CabCreateFileInternal_U(hfci, RootPath.GetLength(), RootPath, typeCompress);
if (lStatus == ERROR_SUCCESS)
{
if (!FCIFlushCabinet(hfci, FALSE, NULL, fnFCIStatus))
{
lStatus = ERROR_FUNCTION_FAILED;
}
}
FCIDestroy(hfci);
return lStatus;
}
_Check_return_
static LSTATUS CabCreateDirectory(
_In_z_ LPCWSTR CabFilePath,
_In_z_ LPCWSTR RootPath,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
return CabCreateDirectory_U(Unicode2UTF8(CabFilePath), Unicode2UTF8(RootPath), typeCompress);
}
_Check_return_
static LSTATUS CabCreateDirectory(
_In_z_ LPCSTR CabFilePath,
_In_z_ LPCSTR RootPath,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
return CabCreateDirectory(CStringW(CabFilePath), CStringW(RootPath), typeCompress);
}
_Check_return_
static LSTATUS CabCreateFile(
_In_z_ LPCWSTR CabFilePath,
_In_z_ LPCWSTR SourceFile,
_In_z_ LPCWSTR FileName,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
if (GetFileType(CabFilePath))
{
if (!DeleteFileW(CabFilePath))
return GetLastError();
}
ERF erf; //FCI error structure
CCAB ccab = { 0 ,900000 }; //cabinet information structure
ccab.setID = 12345;
ccab.iCab = 1;
ccab.iDisk = 0;
auto CabFilePath_U = Unicode2UTF8(CabFilePath);
auto CabFileName = PathFindFileNameA(CabFilePath_U);
StrCpyA(ccab.szCab, CabFileName);
StrCpyNA(ccab.szCabPath, CabFilePath_U, CabFileName - CabFilePath_U + 1);
HFCI hfci = FCICreate2(&erf, ccab);
if (hfci == NULL)
{
return ERROR_FUNCTION_FAILED;
}
LSTATUS lStatus = ERROR_SUCCESS;
if (FCIAddFile(hfci, (LPSTR)Unicode2UTF8(SourceFile).GetString(), (LPSTR)Unicode2UTF8(FileName).GetString(), FALSE, NULL, fnFCIStatus, fnFCIGetOpenInfo, typeCompress))
{
if (!FCIFlushCabinet(hfci, FALSE, NULL, fnFCIStatus))
{
lStatus = ERROR_FUNCTION_FAILED;
}
}
FCIDestroy(hfci);
return lStatus;
}
_Check_return_
static LSTATUS CabCreateFile(
_In_z_ LPCSTR CabFilePath,
_In_z_ LPCSTR SourceFile,
_In_z_ LPCSTR FileName,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
return CabCreateFile(CStringW(CabFilePath), CStringW(SourceFile), CStringW(FileName), typeCompress);
}
_Check_return_
static LSTATUS CabCreateFile(
_In_z_ LPCSTR CabFilePath,
_In_z_ LPCSTR SourceFile,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
return CabCreateFile(CabFilePath, SourceFile,PathFindFileNameA(SourceFile), typeCompress);
}
_Check_return_
static LSTATUS CabCreateFile(
_In_z_ LPCWSTR CabFilePath,
_In_z_ LPCWSTR SourceFile,
_In_ TCOMP typeCompress = TCOMPfromLZXWindow(21)
)
{
return CabCreateFile(CabFilePath,SourceFile, PathFindFileNameW(SourceFile),typeCompress);
}