Repository navigation
Expand file tree
/
Copy pathMerge7zCommon.cpp
More file actions
1203 lines (1107 loc) · 32.3 KB
/
Copy pathMerge7zCommon.cpp
File metadata and controls
1203 lines (1107 loc) · 32.3 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Merge7zCommon.cpp: Provide a handy C++ interface to access 7Zip services
* Copyright (c) 2003 Jochen Tucht
*
* License: This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Remarks: This file contains the presumably version-independent parts of
* Merge7z code. Version specific code resides in Merge7zXXX.cpp.
Please mind 2. a) of the GNU General Public License, and log your changes below.
DATE: BY: DESCRIPTION:
========== ================== ================================================
2003-12-09 Jochen Tucht Created
2003-12-16 Jochen Tucht GuessFormat() now checks for directory
2004-03-18 Jochen Tucht Experimental DllGetVersion() based on rcsid.
2004-10-10 Jochen Tucht DllGetVersion() based on new REVISION.TXT
2005-01-15 Jochen Tucht Changed as explained in revision.txt
2005-02-26 Jochen Tucht Changed as explained in revision.txt
2005-03-19 Jochen Tucht Changed as explained in revision.txt
2005-06-22 Jochen Tucht Treat .ear and .war like .zip
2005-07-05 Jochen Tucht Add missing .tbz2
2005-08-20 Jochen Tucht Option to guess archive format by signature.
EnumerateDirectory() in EnumDirItems.cpp has
somewhat changed so I can no longer use it.
2005-10-02 Jochen Tucht Add CHM format
2005-11-19 Jochen Tucht Minor changes to build against 7z430 beta
2006-06-28 Jochen Neubeck Add ISO format (introduced with 7z436 beta)
Add NSIS format (introduced with 7z440 beta)
2007-01-27 Jochen Neubeck Unassociate .exe filename extension from NSIS
format due to undesired side effect on WinMerge
2007-04-20 Jochen Neubeck Cope with 7z445's revised plugin system
2007-07-13 Jochen Neubeck Pass MSI files to CAB handler
2007-08-25 Jochen Neubeck Add COM format (introduced with 7z452 beta)
This format also handles MSI files, which are
therefore no longer passed to the CAB handler.
2007-09-01 Jochen Neubeck No longer #include "LangUtils.h", which has
moved to a different location as of 7z453 beta.
2007-12-22 Jochen Neubeck Unassociate .001 filename extension
2008-08-03 Jochen Neubeck Add LZMA format (introduced with 7z458 beta)
2010-04-24 Jochen Neubeck New formats introduced with 7z459 beta:
XAR, MUB, HFS, DMG, ELF (not sure if they work)
*/
#include "stdafx.h"
#include "Merge7zCommon.h"
bool g_DisableUserQuestions;
void ReloadLang(); //Implemented in /*/FileManager/LangUtils.cpp
using namespace NWindows;
using namespace NFile;
void AddDirFileInfo(
const UString &prefix,
const UString &fullPathName,
NFind::CFileInfo &fileInfo,
CDirItems &dirItems)
{
CDirItem item;
item.Attrib = fileInfo.Attrib;
item.Size = fileInfo.Size;
item.CTime = fileInfo.CTime;
item.ATime = fileInfo.ATime;
item.MTime = fileInfo.MTime;
//UString
item.LogParent = dirItems.AddPrefix(-1, -1, prefix + fileInfo.Name);
item.PhyParent = dirItems.AddPrefix(-1, -1, fullPathName);
//item.Name = prefix + fileInfo.Name;
//item.FullPath = fullPathName;
dirItems.Items.Add(item);
}
static void EnumerateDirectory(
const UString &baseFolderPrefix,
const UString &directory,
const UString &prefix,
CDirItems &dirItems)
{
NFind::CEnumerator enumerator;
enumerator.SetDirPrefix(baseFolderPrefix + directory);
NFind::CFileInfo fileInfo;
while (enumerator.Next(fileInfo))
{
AddDirFileInfo(prefix, directory + fileInfo.Name, fileInfo, dirItems);
if (fileInfo.IsDir())
{
EnumerateDirectory(baseFolderPrefix, directory + fileInfo.Name + WCHAR_PATH_SEPARATOR,
prefix + fileInfo.Name + WCHAR_PATH_SEPARATOR, dirItems);
}
}
}
HINSTANCE g_hInstance;
#ifndef _UNICODE
bool g_IsNT = false;
static bool IsItWindowsNT()
{
OSVERSIONINFO versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
if (!::GetVersionEx(&versionInfo))
return false;
return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
#endif
DWORD g_dwFlags;
CHAR g_cPath7z[MAX_PATH];
/**
* @brief Dll entry point
*/
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInstance;
# ifndef _UNICODE
g_IsNT = IsItWindowsNT();
# endif
}
return TRUE;
}
/**
* @brief Load a dll and import a number of functions.
*/
static HMODULE DllProxyHelper(LPCSTR *proxy, LPCSTR dir)
{
HMODULE handle = NULL;
if (LPCSTR name = *proxy)
{
if (proxy[1] && proxy[1] != name)
{
char path[MAX_PATH];
FormatMessageA
(
FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
name,
0,
0,
path,
sizeof path,
(va_list *)(&dir)
);
handle = LoadLibraryA(path);
if (handle)
{
LPCSTR *p = proxy;
*proxy = NULL;
while ((name = *++p) != NULL)
{
*p = (LPCSTR)GetProcAddress(handle, name);
if (*p == NULL)
{
*proxy = proxy[1] = name;
p = proxy + 2;
break;
}
}
*p = (LPCSTR)handle;
}
}
if ((name = *proxy) != NULL)
{
DWORD dwError = ERROR_MOD_NOT_FOUND;
HMODULE hContext = NULL;
if (proxy[1] == name)
{
dwError = ERROR_PROC_NOT_FOUND;
hContext = (HMODULE)proxy[2];
}
Complain(dwError, GetSystemString(name), hContext);
}
}
return handle;
}
/**
* @brief Ask archiver dll for an interface of given class.
*/
HRESULT Format7zDLL::Interface::CreateObject(const GUID *interfaceID, void **outObject)
{
PROPVARIANT value;
PropVariantInit(&value);
HRESULT result = proxy->GetHandlerProperty(NArchive::NHandlerPropID::kClassID, &value);
if SUCCEEDED(result)
{
if (value.vt != VT_BSTR || SysStringByteLen(value.bstrVal) != sizeof(GUID))
{
result = DISP_E_TYPEMISMATCH;
}
else
{
result = proxy->CreateObject((const CLSID *)value.bstrVal, interfaceID, outObject);
}
VariantClear((VARIANT *)&value);
}
return result;
}
/**
* @brief Ask archiver dll for an instance of IInArchive.
*/
IInArchive *Format7zDLL::Interface::GetInArchive()
{
void *pv;
if COMPLAIN(CreateObject(&IID_IInArchive, &pv) != S_OK)
{
Complain(RPC_S_INTERFACE_NOT_FOUND, _T("IInArchive"), proxy.handle);
}
return static_cast<IInArchive *>(pv);
}
/**
* @brief Ask archiver dll for an instance of IOutArchive.
*/
IOutArchive *Format7zDLL::Interface::GetOutArchive()
{
void *pv;
if COMPLAIN(CreateObject(&IID_IOutArchive, &pv) != S_OK)
{
Complain(RPC_S_INTERFACE_NOT_FOUND, _T("IOutArchive"), proxy.handle);
}
return static_cast<IOutArchive *>(pv);
}
/**
* @brief Extraction method accessible from outside
*/
HRESULT Format7zDLL::Interface::DeCompressArchive(HWND hwndParent, LPCTSTR path, LPCTSTR folder)
{
HRESULT result = E_FAIL;
if (Merge7z::Format::Inspector *inspector = Open(hwndParent, path))
{
if (AutoBSTR(GetHandlerAddExtension(hwndParent)).Len())
{
//Most handlers seem to be happy with missing index array, but rpm
//handler doesn't know how to "extract all" and needs index array
//even for the one and only file inside (which is .cpio.gz).
static const UINT32 indices[1] = {0};
result = inspector->Extract(hwndParent, folder, indices, 1);
}
else
{
result = inspector->Extract(hwndParent, folder);
}
inspector->Free();
}
return result;
}
/**
* @brief Open archive for inspection.
*/
Merge7z::Format::Inspector *Format7zDLL::Interface::Open(HWND hwndParent, LPCTSTR path)
{
Inspector* inspector = nullptr;
try
{
Interface* pFormat = this;
while (pFormat)
{
inspector = new Inspector(pFormat, path);
try
{
inspector->Init(hwndParent);
break;
}
catch (Complain* complain)
{
inspector->Free();
inspector = 0;
pFormat = pFormat->next;
if (!pFormat)
throw complain;
}
}
}
catch (Complain *complain)
{
complain->Alert(hwndParent);
}
return inspector;
}
/**
* @brief Prepare inspection interface for iteration.
*/
UINT32 Format7zDLL::Interface::Inspector::Open()
{
UINT32 numItems = 0;
archive->GetNumberOfItems(&numItems);
return numItems;
}
/**
* @brief free inspection interface.
*/
void Format7zDLL::Interface::Inspector::Free()
{
Release(archive);
Release(static_cast<IInStream *>(file));
Release(callback);
delete this;
}
HRESULT Format7zDLL::Interface::Inspector::GetProperty(UINT32 index, PROPID propID, PROPVARIANT *value, VARTYPE vt)
{
VariantInit((VARIANT *)value);
HRESULT result = archive->GetProperty(index, propID, value);
if (SUCCEEDED(result) && value->vt != vt)
{
VariantClear((VARIANT *)value);
result = DISP_E_TYPEMISMATCH;
}
return result;
}
BSTR Format7zDLL::Interface::Inspector::GetPath(UINT32 index)
{
PROPVARIANT value;
return SUCCEEDED(GetProperty(index, kpidPath, &value, VT_BSTR)) ? value.bstrVal : 0;
}
BSTR Format7zDLL::Interface::Inspector::GetName(UINT32 index)
{
PROPVARIANT value;
return SUCCEEDED(GetProperty(index, kpidName, &value, VT_BSTR)) ? value.bstrVal : 0;
}
BSTR Format7zDLL::Interface::Inspector::GetExtension(UINT32 index)
{
PROPVARIANT value;
return SUCCEEDED(GetProperty(index, kpidExtension, &value, VT_BSTR)) ? value.bstrVal : 0;
}
VARIANT_BOOL Format7zDLL::Interface::Inspector::IsFolder(UINT32 index)
{
PROPVARIANT value;
return SUCCEEDED(GetProperty(index, kpidIsDir, &value, VT_BOOL)) ? value.boolVal : 0;
}
FILETIME Format7zDLL::Interface::Inspector::LastWriteTime(UINT32 index)
{
static const FILETIME invalid = { 0, 0 };
PROPVARIANT value;
return SUCCEEDED(GetProperty(index, kpidMTime, &value, VT_FILETIME)) ? value.filetime : invalid;
}
void Format7zDLL::Interface::GetDefaultName(HWND hwndParent, UString &ustrDefaultName)
{
int dot = ustrDefaultName.ReverseFind(L'.');
int slash = ustrDefaultName.ReverseFind(L'\\');
if (dot > slash)
{
LPCWSTR pchExtension = ustrDefaultName;
pchExtension += dot + 1;
static OLECHAR const wBlank[] = L" ";
AutoBSTR const bstrHandlerExtension = GetHandlerExtension(hwndParent);
AutoBSTR const bstrHandlerAddExtension = GetHandlerAddExtension(hwndParent);
LPWSTR pchHandlerExtension = bstrHandlerExtension.m_str;
LPWSTR pchHandlerAddExtension = bstrHandlerAddExtension.m_str;
while (int cchHandlerAddExtension = StrCSpnW(pchHandlerAddExtension += StrSpnW(pchHandlerAddExtension, wBlank), wBlank))
{
int cchHandlerExtension = StrCSpnW(pchHandlerExtension += StrSpnW(pchHandlerExtension, wBlank), wBlank);
if (StrIsIntlEqualW(FALSE, pchExtension, pchHandlerExtension, cchHandlerExtension) && pchExtension[cchHandlerExtension] == 0)
{
pchHandlerAddExtension[cchHandlerAddExtension] = L'\0'; // will also stop iteration
ustrDefaultName.ReleaseBuf_SetEnd(dot);
if (*pchHandlerAddExtension == L'.') // consider != '*'
{
ustrDefaultName += pchHandlerAddExtension;
dot += cchHandlerAddExtension; // make ReleaseBuffer(dot) below a NOP
}
}
pchHandlerExtension += cchHandlerExtension;
pchHandlerAddExtension += cchHandlerAddExtension;
}
ustrDefaultName.ReleaseBuf_SetEnd(dot);
ustrDefaultName.Delete(0, slash + 1);
}
else
{
ustrDefaultName = L"noname";
}
}
BSTR Format7zDLL::Interface::GetDefaultName(HWND hwndParent, LPCTSTR path)
{
UString ustrDefaultName = GetUnicodeString(path);
GetDefaultName(hwndParent, ustrDefaultName);
return SysAllocString(ustrDefaultName);
}
BSTR Format7zDLL::Interface::Inspector::GetDefaultName()
{
//UString ustrDefaultName = GetUnicodeString(path);
return SysAllocString(ustrDefaultName);
}
/**
* @brief Open archive for update.
*/
Merge7z::Format::Updater *Format7zDLL::Interface::Update(HWND hwndParent, LPCTSTR path)
{
Updater *updater = new Updater(this, path);
try
{
updater->Init(hwndParent);
}
catch (Complain *complain)
{
complain->Alert(hwndParent);
updater->Free();
updater = 0;
}
return updater;
}
/**
* @brief Add item to updater
*/
UINT32 Format7zDLL::Interface::Updater::Add(Merge7z::DirItemEnumerator::Item &etorItem)
{
// fill in the default values from the enumerator
CDirItem item;
UString Name = GetUnicodeString(etorItem.Name);
UString FullPath = GetUnicodeString(etorItem.FullPath);
if (etorItem.Mask.Item & etorItem.Mask.Name)
//item.Name = GetUnicodeString(etorItem.Name);
item.LogParent = dirItems.AddPrefix(-1, -1, Name);
if (etorItem.Mask.Item & etorItem.Mask.FullPath)
//item.FullPath = GetUnicodeString(etorItem.FullPath);
item.PhyParent = dirItems.AddPrefix(-1, -1, FullPath);
if (etorItem.Mask.Item & etorItem.Mask.Attributes)
item.Attrib = etorItem.Attributes;
if (etorItem.Mask.Item & etorItem.Mask.Size)
item.Size = etorItem.Size;
if (etorItem.Mask.Item & etorItem.Mask.CreationTime)
item.CTime = etorItem.CreationTime;
if (etorItem.Mask.Item & etorItem.Mask.LastAccessTime)
item.ATime = etorItem.LastAccessTime;
if (etorItem.Mask.Item & etorItem.Mask.LastWriteTime)
item.MTime = etorItem.LastWriteTime;
if (etorItem.Mask.Item && (etorItem.Mask.Item & (etorItem.Mask.NeedFindFile|etorItem.Mask.CheckIfPresent)) != etorItem.Mask.NeedFindFile)
{
// Check the info from the disk
NFile::NFind::CFileInfo fileInfo;
if (NFile::NFind::CFindFile().FindFirst(FullPath, fileInfo))
{
if (!(etorItem.Mask.Item & etorItem.Mask.Name))
item.Name = fileInfo.Name;
if (!(etorItem.Mask.Item & etorItem.Mask.Attributes))
item.Attrib = fileInfo.Attrib;
if (!(etorItem.Mask.Item & etorItem.Mask.Size))
item.Size = fileInfo.Size;
if (!(etorItem.Mask.Item & etorItem.Mask.CreationTime))
item.CTime = fileInfo.CTime;
if (!(etorItem.Mask.Item & etorItem.Mask.LastAccessTime))
item.ATime = fileInfo.ATime;
if (!(etorItem.Mask.Item & etorItem.Mask.LastWriteTime))
item.MTime = fileInfo.MTime;
}
else
{
// file not valid, forget it
etorItem.Mask.Item = 0;
}
}
if (etorItem.Mask.Item)
{
// No check from disk, simply use info from enumerators (risky)
// Why risky? This is not at all obvious.
dirItems.Items.Add(item);
// Recurse into directories (call a function of 7zip)
if ((etorItem.Mask.Item & etorItem.Mask.Recurse) && (item.Attrib & FILE_ATTRIBUTE_DIRECTORY))
{
EnumerateDirectory(UString(), FullPath + L'\\',
Name + L'\\', dirItems);
}
}
return etorItem.Mask.Item;
}
/**
* @brief free updater interface.
*/
void Format7zDLL::Interface::Updater::Free()
{
Release(outArchive);
Release(static_cast<IOutStream *>(file));
delete this;
}
/**
* @brief Compression method accessible from outside
*
* @note See CAgent::DoOperation (in 7zip source) for model
*/
HRESULT Format7zDLL::Interface::CompressArchive(HWND hwndParent, LPCTSTR path, Merge7z::DirItemEnumerator *etor)
{
HRESULT result = E_FAIL;
if (Merge7z::Format::Updater *updater = Update(hwndParent, path))
{
UINT count = etor->Open();
while (count--)
{
Merge7z::DirItemEnumerator::Item etorItem;
etorItem.Mask.Item = 0;
Merge7z::Envelope *envelope = etor->Enum(etorItem);
if (etorItem.Mask.Item != 0)
{
updater->Add(etorItem);
}
if (envelope)
{
envelope->Free();
}
}
result = updater->Commit(hwndParent);
updater->Free();
}
return result;
}
/**
* @brief get handler property identified by given propID
*/
HRESULT Format7zDLL::Interface::GetHandlerProperty(HWND hwndParent, PROPID propID, PROPVARIANT *value, VARTYPE vt)
{
VariantInit((VARIANT *)value);
HRESULT result = DISP_E_EXCEPTION;
try
{
result = proxy->GetHandlerProperty(propID, value);
if (SUCCEEDED(result) && value->vt != vt)
{
VariantClear((VARIANT *)value);
result = DISP_E_TYPEMISMATCH;
}
}
catch (Complain *complain)
{
complain->Alert(hwndParent);
}
return result;
}
/**
* @brief get Name handler property
*/
BSTR Format7zDLL::Interface::GetHandlerName(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kName, &value, VT_BSTR)) ? value.bstrVal : 0;
}
/**
* @brief get ClassID handler property
*/
BSTR Format7zDLL::Interface::GetHandlerClassID(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kClassID, &value, VT_BSTR)) ? value.bstrVal : 0;
}
/**
* @brief get Extension handler property
*/
BSTR Format7zDLL::Interface::GetHandlerExtension(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kExtension, &value, VT_BSTR)) ? value.bstrVal : 0;
}
/**
* @brief get AddExtension handler property
*/
BSTR Format7zDLL::Interface::GetHandlerAddExtension(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kAddExtension, &value, VT_BSTR)) ? value.bstrVal : 0;
}
/**
* @brief get Update handler property
*/
VARIANT_BOOL Format7zDLL::Interface::GetHandlerUpdate(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kUpdate, &value, VT_BOOL)) ? value.boolVal : 0;
}
/**
* @brief get KeepName handler property
*/
VARIANT_BOOL Format7zDLL::Interface::GetHandlerKeepName(HWND hwndParent)
{
PROPVARIANT value;
return SUCCEEDED(GetHandlerProperty(hwndParent, NArchive::NHandlerPropID::kKeepName, &value, VT_BOOL)) ? value.boolVal : 0;
}
/**
* @brief Initialize the library.
*/
int Merge7z::Initialize(DWORD dwFlags)
{
g_dwFlags = dwFlags;
if (dwFlags & Initialize::Local7z)
{
GetModuleFileNameA(g_hInstance, g_cPath7z, sizeof g_cPath7z);
PathRemoveFileSpecA(g_cPath7z);
}
else
{
DWORD type = 0;
DWORD size = sizeof g_cPath7z;
SHGetValueA(HKEY_LOCAL_MACHINE, "Software\\7-Zip", "Path", &type, g_cPath7z, &size);
}
PathAddBackslashA(g_cPath7z);
if (WORD wLangID = HIWORD(dwFlags))
{
LoadLang(MAKEINTATOM(wLangID));
}
return 0;
}
Format7zDLL::Interface *Format7zDLL::Interface::head = NULL;
/**
* @brief Access archiver dll functions through proxy.
*/
#define CLS_ARC_ID_ITEM(cls) ((cls).Data4[5])
Format7zDLL::Proxy::Handle Format7zDLL::Proxy::handle =
{
"%1!s!7z.dll",
"CreateObject",
"GetHandlerProperty2",
"GetNumberOfFormats",
"GetIsArc",
(HMODULE)0
};
struct Format7zDLL::Proxy *Format7zDLL::Proxy::operator->()
{
DllProxyHelper(&handle.aModule, g_cPath7z);
if (formatIndex < 0)
{
GUID clsId =
{
0x23170F69, 0x40C1, 0x278A,
0x10, 0x00, 0x00, 0x01, 0x10, (BYTE)-formatIndex, 0x00, 0x00
};
UINT32 i = 0;
handle.GetNumberOfFormats(&i);
while (i)
{
PROPVARIANT value;
::VariantInit((LPVARIANT)&value);
if (SUCCEEDED(handle.GetHandlerProperty2(--i, NArchive::NHandlerPropID::kClassID, &value)) &&
value.vt == VT_BSTR &&
SysStringByteLen(value.bstrVal) == sizeof(GUID) &&
IsEqualGUID(clsId, *value.puuid))
{
formatIndex = i;
i = 0;
}
::VariantClear((LPVARIANT)&value);
}
if (formatIndex < 0)
{
TCHAR szArcID[4];
wsprintf(szArcID, _T("%02x"), (UINT)CLS_ARC_ID_ITEM(clsId));
Complain(RPC_S_INTERFACE_NOT_FOUND, szArcID, handle);
}
}
return this;
}
STDMETHODIMP Format7zDLL::Proxy::CreateObject(const GUID *clsID, const GUID *interfaceID, void **outObject)
{
return handle.CreateObject(clsID, interfaceID, outObject);
}
STDMETHODIMP Format7zDLL::Proxy::GetHandlerProperty(PROPID propID, PROPVARIANT *value)
{
return handle.GetHandlerProperty2(formatIndex, propID, value);
}
STDMETHODIMP Format7zDLL::Proxy::GetIsArc(UINT32 formatIndex, Func_IsArc* isArc)
{
return handle.GetIsArc(formatIndex, isArc);
}
#define DEFINE_FORMAT(name, id, extension) \
Format7zDLL::Proxy PROXY_##name = \
{ \
-0x##id, \
0, \
0, \
'@', \
extension \
}; \
Format7zDLL::Interface name = PROXY_##name;
DEFINE_FORMAT(CZipHandler, 01, "zip jar war ear xpi apk");
DEFINE_FORMAT(CBZip2Handler, 02, "bz2 tbz2 tbz");
DEFINE_FORMAT(CRarHandler, 03, "rar");
DEFINE_FORMAT(CArjHandler, 04, "arj");
DEFINE_FORMAT(CZHandler, 05, "z taz");
DEFINE_FORMAT(CLzhHandler, 06, "lzh lha");//"@-l" doesn't work because signature starts at offset 2
DEFINE_FORMAT(CFormat7z, 07, "7z");
DEFINE_FORMAT(CCabHandler, 08, "cab");
DEFINE_FORMAT(CNsisHandler, 09, "");
DEFINE_FORMAT(CLzmaHandler, 0A, "lzma");
DEFINE_FORMAT(CLzma86Handler, 0B, "lzma86");
DEFINE_FORMAT(CXzHandler, 0C, "xz txz");
DEFINE_FORMAT(CPpmdHandler, 0D, "");
DEFINE_FORMAT(CZstdHandler, 0E, "zst tzst");
//DEFINE_FORMAT(CLvmHandler, BF, "");
//DEFINE_FORMAT(CAvbHandler, C0, "");
DEFINE_FORMAT(CLpHandler, C1, "");
DEFINE_FORMAT(CSparseHandler, C2, "");
DEFINE_FORMAT(CApFsHandler, C3, "");
DEFINE_FORMAT(CVhdxHandler, C4, "");
DEFINE_FORMAT(CBase64Handler, C5, "");
DEFINE_FORMAT(CCOFFHandler, C6, "");
DEFINE_FORMAT(CExtHandler, C7, "");
DEFINE_FORMAT(CVMDKHandler, C8, "");
DEFINE_FORMAT(CVDIHandler, C9, "");
DEFINE_FORMAT(CQcowHandler, CA, "");
DEFINE_FORMAT(CGPTHandler, CB, "");
DEFINE_FORMAT(CRar5Handler, CC, "rar");
DEFINE_FORMAT(CIHEXHandler, CD, "");
DEFINE_FORMAT(CHxsHandler, CE, "");
DEFINE_FORMAT(CTEHandler, CF, "");
DEFINE_FORMAT(CUEFIcHandler, D0, "");
DEFINE_FORMAT(CUEFIsHandler, D1, "");
DEFINE_FORMAT(CSquashFSHandler, D2, "");
DEFINE_FORMAT(CCramFSHandler, D3, "");
DEFINE_FORMAT(CApmHandler, D4, "");
DEFINE_FORMAT(CMslzHandler, D5, "");
DEFINE_FORMAT(CFlvHandler, D6, "");
DEFINE_FORMAT(CSwfHandler, D7, "");
DEFINE_FORMAT(CSwfcHandler, D8, "");
DEFINE_FORMAT(CNtfsHandler, D9, "");
DEFINE_FORMAT(CFatHandler, DA, "");
DEFINE_FORMAT(CMbrHandler, DB, "");
DEFINE_FORMAT(CVhdHandler, DC, "");
DEFINE_FORMAT(CPeHandler, DD, "");
DEFINE_FORMAT(CElfHandler, DE, "elf");
DEFINE_FORMAT(CMachOHandler, DF, "");
DEFINE_FORMAT(CUdfHandler, E0, "udf");
DEFINE_FORMAT(CXarHandler, E1, "xar pkg");
DEFINE_FORMAT(CMubHandler, E2, "mub");
DEFINE_FORMAT(CHfsHandler, E3, "hfs");
DEFINE_FORMAT(CDmgHandler, E4, "dmg");
DEFINE_FORMAT(CComHandler, E5, "");
DEFINE_FORMAT(CWimHandler, E6, "wim swm");
DEFINE_FORMAT(CIsoHandler, E7, "iso");
DEFINE_FORMAT(CSplitHandler, EA, "");
DEFINE_FORMAT(CChmHandler, E9, "chm chi chq chw hxs hxi hxr hxq hxw lit");
DEFINE_FORMAT(CRpmHandler, EB, "rpm");
DEFINE_FORMAT(CDebHandler, EC, "deb");
DEFINE_FORMAT(CCpioHandler, ED, "cpio");
DEFINE_FORMAT(CTarHandler, EE, "tar");
DEFINE_FORMAT(CGZipHandler, EF, "gz tgz");
/**
* @brief Construct Merge7z interface.
*/
Merge7z::Merge7z():
Format7z(CFormat7z),
ZipHandler(CZipHandler),
RarHandler(CRarHandler),
BZip2Handler(CBZip2Handler),
TarHandler(CTarHandler)
{
}
/**
* @brief Figure out which archiver dll to use for a given archive.
*/
Merge7z::Format *Merge7z::GuessFormat(LPCTSTR path)
{
if (g_dwFlags & Initialize::GuessFormatBySignature)
return GuessFormatBySignature(path, g_dwFlags & Initialize::GuessFormatByExtension ? path : 0);
return GuessFormatByExtension(path);
}
/**
* @brief Figure out which archiver dll to use for a given archive.
*/
Merge7z::Format *Merge7z::GuessFormatByExtension(LPCTSTR path)
{
SZ_EXTENSION ext;
if (PathIsDirectory(path))
return 0;
return GuessFormatEx(GetExtension(path, ext), 0, 0);
}
/**
* @brief Figure out which archiver dll to use for a given archive.
*/
Merge7z::Format *Merge7z::GuessFormatBySignature(LPCTSTR path, LPCTSTR extension)
{
SZ_EXTENSION ext;
CH_SIGNATURE sig;
if (PathIsDirectory(path))
return 0;
return GuessFormatEx(GetExtension(extension, ext), sig, GetSignature(path, sig));
}
/**
* @brief Check signature manually using 7-Zip's signature properties when IsArc is not available.
*/
static bool CheckSignatureManually(Format7zDLL::Proxy* proxy, LPCH sig, int cchSig)
{
if (!sig || cchSig <= 0)
return false;
// Get signature offset
UInt32 sigOffset = 0;
PROPVARIANT offsetValue;
PropVariantInit(&offsetValue);
if (SUCCEEDED(proxy->GetHandlerProperty(NArchive::NHandlerPropID::kSignatureOffset, &offsetValue)) &&
offsetValue.vt == VT_UI4)
{
sigOffset = offsetValue.ulVal;
}
VariantClear((VARIANT *)&offsetValue);
// Try kSignature first (single signature)
PROPVARIANT value;
PropVariantInit(&value);
proxy->GetHandlerProperty(NArchive::NHandlerPropID::kSignature, &value);
bool isMultiSignature = false;
// If kSignature is not available, try kMultiSignature
if (value.vt != VT_BSTR || !value.bstrVal)
{
VariantClear((VARIANT *)&value);
PropVariantInit(&value);
proxy->GetHandlerProperty(NArchive::NHandlerPropID::kMultiSignature, &value);
isMultiSignature = true;
}
bool matched = false;
if (value.vt == VT_BSTR && value.bstrVal)
{
UInt32 totalSize = SysStringByteLen(value.bstrVal);
const BYTE *pchSignature = (const BYTE *)value.bstrVal;
if (isMultiSignature)
{
// Multi-signature format: [length1][signature1][length2][signature2]...
// Each signature is prefixed with 1 byte indicating its length
UInt32 pos = 0;
while (pos < totalSize)
{
// Read signature length (1 byte)
UInt32 sigLen = pchSignature[pos++];
if (pos + sigLen > totalSize)
break; // Invalid format
// Check this signature
if (cchSig >= (int)(sigOffset + sigLen))
{
bool signatureMatch = true;
for (UInt32 j = 0; j < sigLen; j++)
{
if ((BYTE)sig[sigOffset + j] != pchSignature[pos + j])
{
signatureMatch = false;
break;
}
}
if (signatureMatch)
{
matched = true;
break;
}
}
pos += sigLen; // Move to next signature
}
}
else
{
// Single signature
if (cchSig >= (int)(sigOffset + totalSize))
{
bool signatureMatch = true;
for (UInt32 j = 0; j < totalSize; j++)
{
if ((BYTE)sig[sigOffset + j] != pchSignature[j])
{
signatureMatch = false;
break;
}
}
if (signatureMatch)
matched = true;
}
}
}
VariantClear((VARIANT *)&value);
return matched;
}
/**
* @brief Figure out which archiver dll to use for a given archive using pre-read signature.
* This function receives an already-read signature buffer, unlike GuessFormatBySignature which reads the file itself.
*/
Merge7z::Format* Merge7z::GuessFormatEx(LPCSTR ext, LPCH sig, int cchSig)
{
// If no signature provided, fall back to extension-only detection
if (!sig || cchSig <= 0)
{
if (!ext || ext[0] == '\0')
return nullptr;
Format7zDLL::Interface* pFormat = Format7zDLL::Interface::head;
while (pFormat)
{
static const char aBlank[] = " ";
LPCSTR pchExtension = pFormat->proxy.extension;
int cchExtension;
while ((cchExtension = StrCSpnA(pchExtension += StrSpnA(pchExtension, aBlank), aBlank)) != 0)
{
if (StrIsIntlEqualA(FALSE, pchExtension, ext, cchExtension) && ext[cchExtension] == '\0')
return pFormat;
pchExtension += cchExtension;
}
pFormat = pFormat->next;
}
return nullptr;
}
// Build list of candidate formats
Format7zDLL::Interface* candidates[100];
int candidateCount = 0;
int extensionMatches = 0;
// First pass: collect formats matching extension if provided
Format7zDLL::Interface* pFormat = Format7zDLL::Interface::head;
bool useExtension = (ext != nullptr && ext[0] != '\0');
while (pFormat && candidateCount < 100)
{
bool matchesExtension = false;
if (useExtension)
{
static const char aBlank[] = " ";
LPCSTR pchExtension = pFormat->proxy.extension;
int cchExtension;
while ((cchExtension = StrCSpnA(pchExtension += StrSpnA(pchExtension, aBlank), aBlank)) != 0)
{
if (StrIsIntlEqualA(FALSE, pchExtension, ext, cchExtension) && ext[cchExtension] == '\0')
{
matchesExtension = true;
break;
}
pchExtension += cchExtension;
}
}
if (matchesExtension)
{
// Insert at beginning (priority for extension matches)
for (int i = candidateCount; i > extensionMatches; i--)
candidates[i] = candidates[i - 1];
candidates[extensionMatches] = pFormat;
extensionMatches++;
candidateCount++;
}
else
{
// Add at end (for signature checking)