-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client7z.cpp
executable file
·1574 lines (1303 loc) · 42.6 KB
/
Client7z.cpp
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
// Client7z.cpp
#define EXTERNAL_CODECS
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "tabi.h"
// Required for SetProperties in p7zip code
#define COMPRESS_MT
// For correct filename translation in Unix
#define LOCALE_IS_UTF8
#ifdef FREEARC_WIN
# include <initguid.h>
#else
// Because IID_IUnknown is already defined in LZMA code
# define __COMMON_MYINITGUID_H
# define INITGUID
# include "Common/MyGuidDef.h"
# define ENV_UNIX
#endif
#include "Common/IntToString.h"
#include "Common/StringConvert.h"
#include "Windows/DLL.h"
#include "Windows/FileDir.h"
#include "Windows/FileFind.h"
#include "Windows/FileName.h"
#include "Windows/PropVariant.h"
#include "Windows/PropVariantConversions.h"
#include "7zip/Common/ProgressUtils.h"
#include "7zip/UI/Common/LoadCodecs.h"
#include "7zip/UI/Common/OpenArchive.h"
#include "7zip/UI/Common/ExtractMode.h"
#include "7zip/UI/Console/OpenCallbackConsole.h"
#include "7zip/Archive/IArchive.h"
#include "7zip/Archive/Common/OutStreamWithCRC.h"
#include "7zip/IPassword.h"
#include "7zip/MyVersion.h"
// Inline required source files
#include "Common/MyVector.cpp"
#include "Common/IntToString.cpp"
#include "Common/MyString.cpp"
#include "Common/StringConvert.cpp"
#include "Common/Wildcard.cpp"
#include "Common/UTFConvert.cpp"
#include "Common/MyWindows.cpp"
#include "Common/StringToInt.cpp"
#include "Windows/PropVariant.cpp"
#include "Windows/PropVariantConversions.cpp"
#include "Windows/DLL.cpp"
#include "Windows/Error.cpp"
#include "Windows/FileIO.cpp"
#include "Windows/FileDir.cpp"
#include "Windows/FileFind.cpp"
#include "Windows/FileName.cpp"
#include "Windows/Time.cpp"
#ifdef FREEARC_WIN
#include "Windows/Registry.cpp"
#else
#include "myWindows/wine_date_and_time.cpp"
#endif
#ifndef FREEARC_WIN
#include "../C/Threads.c"
#endif
#include "7zip/Common/FileStreams.cpp"
#include "7zip/Common/StreamUtils.cpp"
#include "7zip/Common/ProgressUtils.cpp"
#include "7zip/UI/Console/ConsoleClose.cpp"
#include "7zip/UI/Common/DefaultName.cpp"
#include "7zip/UI/Common/LoadCodecs.cpp"
#include "7zip/UI/Common/OpenArchive.cpp"
#include "7zip/UI/Common/ArchiveOpenCallback.cpp"
#include "7zip/UI/Common/ExtractingFilePath.cpp"
#include "7zip/UI/Common/SetProperties.cpp"
//////////////////////////////////////////////////////////////
// Some common definitions
using namespace NWindows;
HINSTANCE g_hInstance;
int g_CodePage = -1;
// Ôëàã, óñòàíàâëèâàåìûé â 1 êîãäà íóæíî ýêñòðåííî ïðåðâàòü âïîëíÿåìóþ îïåðàöèþ
int BreakFlag = 0;
extern "C" void c_szSetBreakFlag (int flag)
{
BreakFlag = flag;
}
//////////////////////////////////////////////////////////////
// Archive Open callback class
class COpenCallbackConsoleZ: public IOpenCallbackUI
{
public:
INTERFACE_IOpenCallbackUI(;)
#ifndef _NO_CRYPTO
bool PasswordIsDefined;
bool PasswordWasAsked;
UString Password;
TABI_FUNCTION *cb;
COpenCallbackConsoleZ(TABI_FUNCTION *_cb): cb(_cb), PasswordIsDefined(false), PasswordWasAsked(false) {}
#endif
};
HRESULT COpenCallbackConsoleZ::Open_CheckBreak()
{
if (BreakFlag)
return E_ABORT;
return S_OK;
}
HRESULT COpenCallbackConsoleZ::Open_SetTotal(const UInt64 *, const UInt64 *)
{
return Open_CheckBreak();
}
HRESULT COpenCallbackConsoleZ::Open_SetCompleted(const UInt64 *, const UInt64 *)
{
return Open_CheckBreak();
}
HRESULT COpenCallbackConsoleZ::Open_CryptoGetTextPassword(BSTR *password)
{
PasswordWasAsked = true;
RINOK(Open_CheckBreak());
if (!PasswordIsDefined)
{
int password_size = 1000;
wchar_t password_buf[password_size];
cb(TABI_DYNAMAP ("request","ask_password") ("password_buf", (void*)password_buf) ("password_size", password_size));
Password = password_buf;
PasswordIsDefined = true;
}
return StringToBstr(Password, password);
}
HRESULT COpenCallbackConsoleZ::Open_GetPasswordIfAny(UString &password)
{
if (PasswordIsDefined)
password = Password;
return S_OK;
}
bool COpenCallbackConsoleZ::Open_WasPasswordAsked()
{
return PasswordWasAsked;
}
void COpenCallbackConsoleZ::Open_ClearPasswordWasAskedFlag()
{
PasswordWasAsked = false;
}
//////////////////////////////////////////////////////////////
// Exported functions
static CCodecs *codecs = NULL;
static CIntVector formatIndices;
// Load archive formats from 7z.dll
static void szInitLibrary()
{
UString ArcType = L"";
if (!codecs)
{
#ifndef FREEARC_WIN
global_use_utf16_conversion = 1;
#endif
codecs = new CCodecs;
CMyComPtr<ICompressCodecsInfo> *compressCodecsInfo = new CMyComPtr<ICompressCodecsInfo> (codecs);
HRESULT result = codecs->Load();
if (result != S_OK)
throw "CSystemException(result)";
if (!codecs->FindFormatForArchiveType(ArcType, formatIndices))
throw "kUnsupportedArcTypeMessage";
}
}
// Open existing archive
extern "C" int c_szOpenArchive (TABI_ELEMENT* params)
{
try {
TABI_MAP p(params);
wchar_t *archiveName = (wchar_t *)(p._ptr("arcname"));
CArchiveLink* &arc = *(CArchiveLink**)(p._ptr("archive"));
szInitLibrary();
CArchiveLink *archiveLink = new CArchiveLink;
COpenCallbackConsoleZ *openCallback = new COpenCallbackConsoleZ(p._callback("callback"));
bool stdInMode = false;
try {
HRESULT result = archiveLink->Open2(codecs, formatIndices, stdInMode, NULL, archiveName, openCallback);
if (result != S_OK)
throw "can't open file as archive";
} catch (...) {
throw "can't open file as archive";
}
arc = archiveLink;
return 0;
} catch (const char *msg) {
// sprintf(errmsg, "c_szOpenArchive: %s", msg);
return 1;
} catch (int code) {
// sprintf(errmsg, "c_szOpenArchive: error %d", code);
return 1;
}
}
// Close archive
extern "C" UInt32 c_szArcClose (CArchiveLink &arc, char *errmsg)
{
try {
arc.Close();
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcClose: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcClose: error %d", code);
return 1;
}
}
// Number of files in archive
extern "C" UInt32 c_szArcItems (CArchiveLink &arc, UInt32 *value, char *errmsg)
{
try {
CMyComPtr<IInArchive> archive = arc.GetArchive();
archive->GetNumberOfItems(value);
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcItems: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcItems: error %d", code);
return 1;
}
}
// Get numeric property of file in archive
extern "C" UInt32 c_szArcGetInt64Property (CArchiveLink &arc, Int32 index, PROPID propID, UInt64 *value, char *errmsg)
{
try {
CMyComPtr<IInArchive> archive = arc.GetArchive();
NCOM::CPropVariant prop;
if ((index==-1? archive->GetArchiveProperty(propID, &prop) : archive->GetProperty(index, propID, &prop)) != S_OK)
throw "GetProperty failed";
if (prop.vt == VT_EMPTY)
*value = 0; //throw "empty value";
else *value = ConvertPropVariantToUInt64(prop);
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcGetInt64Property: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcGetInt64Property: error %d", code);
return 1;
}
}
// Get boolean property of file in archive
extern "C" UInt32 c_szArcGetBoolProperty (CArchiveLink &arc, Int32 index, PROPID propID, UInt32 *value, char *errmsg)
{
try {
CMyComPtr<IInArchive> archive = arc.GetArchive();
NCOM::CPropVariant prop;
if ((index==-1? archive->GetArchiveProperty(propID, &prop) : archive->GetProperty(index, propID, &prop)) != S_OK)
throw "GetProperty failed";
switch (prop.vt)
{
case VT_EMPTY: *value = 0; break;
case VT_BOOL: *value = VARIANT_BOOLToBool(prop.boolVal) ? 1 : 0; break;
default: throw "not an Empty or Bool value";
}
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcGetBoolProperty: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcGetBoolProperty: error %d", code);
return 1;
}
}
// Get string property of file in archive
extern "C" UInt32 c_szArcGetStrProperty (CArchiveLink &arc, Int32 index, PROPID propID, wchar_t *value, UInt32 valueSize, char *errmsg)
{
try {
CMyComPtr<IInArchive> archive = arc.GetArchive();
NCOM::CPropVariant prop;
if (index==-1 && propID==kpidType)
prop = codecs->Formats[arc.Arcs.Back().FormatIndex].Name;
else if ((index==-1? archive->GetArchiveProperty(propID, &prop) : archive->GetProperty(index, propID, &prop)) != S_OK)
throw "GetProperty failed";
switch (prop.vt)
{
case VT_EMPTY: value[0] = 0;
break;
case VT_BSTR: {int i;
for (i=0; i+1<valueSize && prop.bstrVal[i]; i++)
value[i] = prop.bstrVal[i];
value[i] = 0;
break;}
default: throw "not an Empty or BStr value";
}
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcGetStrProperty: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcGetStrProperty: error %d", code);
return 1;
}
}
static BOOL IsFileTimeZero(CONST FILETIME *lpFileTime)
{
return (lpFileTime->dwLowDateTime == 0) && (lpFileTime->dwHighDateTime == 0);
}
// Get datetime property of file in archive
extern "C" UInt32 c_szArcGetTimeProperty (CArchiveLink &arc, Int32 index, PROPID propID, UInt32 *value, char *errmsg)
{
try {
CMyComPtr<IInArchive> archive = arc.GetArchive();
NCOM::CPropVariant prop;
if ((index==-1? archive->GetArchiveProperty(propID, &prop) : archive->GetProperty(index, propID, &prop)) != S_OK)
throw "GetProperty failed";
if (prop.vt != VT_FILETIME)
throw "not a Filetime value";
if (IsFileTimeZero(&prop.filetime))
*value = 0;
else
{
//FILETIME localFileTime;
//if (!FileTimeToLocalFileTime(&prop.filetime, &localFileTime))
// throw "FileTimeToLocalFileTime failed";
if (!NTime::FileTimeToUnixTime(prop.filetime, *value))
throw "FileTimeToUnixTime failed";
}
return 0;
} catch (const char *msg) {
sprintf(errmsg, "c_szArcGetTimeProperty: %s", msg);
return 1;
} catch (int code) {
sprintf(errmsg, "c_szArcGetTimeProperty: error %d", code);
return 1;
}
}
//////////////////////////////////////////////////////////////
// Archive Extracting callback class #1
#include "7zip/UI/Common/IFileExtractCallback.h"
static const wchar_t *kUniversalWildcard = L"*";
class CExtractCallbackConsole:
public IExtractCallbackUI,
public ICompressProgressInfo,
#ifndef _NO_CRYPTO
public ICryptoGetTextPassword,
#endif
public CMyUnknownImp
{
public:
MY_QUERYINTERFACE_BEGIN2(IFolderArchiveExtractCallback)
MY_QUERYINTERFACE_ENTRY(ICompressProgressInfo)
#ifndef _NO_CRYPTO
MY_QUERYINTERFACE_ENTRY(ICryptoGetTextPassword)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
STDMETHOD(SetTotal)(UInt64 total);
STDMETHOD(SetCompleted)(const UInt64 *completeValue);
// IFolderArchiveExtractCallback
STDMETHOD(AskOverwrite)(
const wchar_t *existName, const FILETIME *existTime, const UInt64 *existSize,
const wchar_t *newName, const FILETIME *newTime, const UInt64 *newSize,
Int32 *answer);
STDMETHOD (PrepareOperation)(const wchar_t *name, bool isFolder, Int32 askExtractMode, const UInt64 *position);
STDMETHOD(MessageError)(const wchar_t *message);
STDMETHOD(SetOperationResult)(Int32 operationResult, bool encrypted);
HRESULT BeforeOpen(const wchar_t *name);
HRESULT OpenResult(const wchar_t *name, HRESULT result, bool encrypted);
HRESULT ThereAreNoFiles();
HRESULT ExtractResult(HRESULT result);
// ICompressProgressInfo
STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize);
#ifndef _NO_CRYPTO
HRESULT SetPassword(const UString &password);
STDMETHOD(CryptoGetTextPassword)(BSTR *password);
bool PasswordIsDefined;
UString Password;
#endif
UInt64 NumArchives;
UInt64 NumArchiveErrors;
UInt64 NumFileErrors;
UInt64 NumFileErrorsInCurrentArchive;
void Init(TABI_FUNCTION *_cb)
{
NumArchives = 0;
NumArchiveErrors = 0;
NumFileErrors = 0;
NumFileErrorsInCurrentArchive = 0;
cb = _cb;
old_inSize = old_outSize = 0;
}
TABI_FUNCTION *cb;
UInt64 old_inSize;
UInt64 old_outSize;
};
using namespace NWindows;
using namespace NFile;
using namespace NDirectory;
// static const char *kCantAutoRename = "can not create file with auto name\n";
// static const char *kCantRenameFile = "can not rename existing file\n";
// static const char *kCantDeleteOutputFile = "can not delete output file ";
static const char *kError = "ERROR: ";
static const char *kMemoryExceptionMessage = "Can't allocate required memory!";
static const char *kProcessing = "Processing archive: ";
static const char *kEverythingIsOk = "Everything is Ok";
static const char *kNoFiles = "No files to process";
static const char *kUnsupportedMethod = "Unsupported Method";
static const char *kCrcFailed = "CRC Failed";
static const char *kCrcFailedEncrypted = "CRC Failed in encrypted file. Wrong password?";
static const char *kDataError = "Data Error";
static const char *kDataErrorEncrypted = "Data Error in encrypted file. Wrong password?";
static const char *kUnknownError = "Unknown Error";
STDMETHODIMP CExtractCallbackConsole::SetTotal(UInt64 x)
{
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::SetCompleted(const UInt64 *x)
{
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::AskOverwrite(
const wchar_t *existName, const FILETIME *, const UInt64 *index,
const wchar_t *, const FILETIME *, const UInt64 *,
Int32 *answer)
{
if (BreakFlag)
return E_ABORT;
*answer = cb(TABI_DYNAMAP ("request","can_be_extracted?") ("outname", existName) ("index", *index));
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::PrepareOperation(const wchar_t *name, bool isFolder, Int32 askExtractMode, const UInt64 *position)
{
if (BreakFlag)
return E_ABORT;
cb(TABI_DYNAMAP ("request","filename") ("filename", name) ("is_folder?", isFolder) ("mode", askExtractMode));
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
{
if (BreakFlag)
return E_ABORT;
cb(TABI_DYNAMAP ("request","progress") ("compressed", *inSize - old_inSize) ("original", *outSize - old_outSize));
old_inSize = *inSize;
old_outSize = *outSize;
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::MessageError(const wchar_t *message)
{
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::SetOperationResult(Int32 operationResult, bool encrypted)
{
if (BreakFlag)
return E_ABORT;
cb(TABI_DYNAMAP ("request","filedone") ("operationResult", operationResult) ("encrypted?", encrypted));
return S_OK;
}
#ifndef _NO_CRYPTO
HRESULT CExtractCallbackConsole::SetPassword(const UString &password)
{
PasswordIsDefined = true;
Password = password;
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::CryptoGetTextPassword(BSTR *password)
{
if (!PasswordIsDefined)
{
int password_size = 1000;
wchar_t password_buf[password_size];
cb(TABI_DYNAMAP ("request","ask_password") ("password_buf", (void*)password_buf) ("password_size", password_size));
Password = password_buf;
PasswordIsDefined = true;
}
return StringToBstr(Password, password);
}
#endif
HRESULT CExtractCallbackConsole::BeforeOpen(const wchar_t *name)
{
return S_OK;
}
HRESULT CExtractCallbackConsole::OpenResult(const wchar_t * /* name */, HRESULT result, bool encrypted)
{
return S_OK;
}
HRESULT CExtractCallbackConsole::ThereAreNoFiles()
{
return S_OK;
}
HRESULT CExtractCallbackConsole::ExtractResult(HRESULT result)
{
return S_OK;
}
//////////////////////////////////////////////////////////////
// Archive Extracting callback class #2
#define __ARCHIVE_EXTRACT_CALLBACK_H
class CArchiveExtractCallback:
public IArchiveExtractCallback,
// public IArchiveVolumeExtractCallback,
public ICryptoGetTextPassword,
public ICompressProgressInfo,
public CMyUnknownImp
{
const CArc *_arc;
const NWildcard::CCensorNode *_wildcardCensor;
CMyComPtr<IFolderArchiveExtractCallback> _extractCallback2;
CMyComPtr<ICompressProgressInfo> _compressProgress;
CMyComPtr<ICryptoGetTextPassword> _cryptoGetTextPassword;
UString _directoryPath;
NExtract::NPathMode::EEnum _pathMode;
NExtract::NOverwriteMode::EEnum _overwriteMode;
UString _diskFilePath;
UString _filePath;
UInt64 _position;
bool _isSplit;
bool _extractMode;
bool WriteCTime;
bool WriteATime;
bool WriteMTime;
bool _encrypted;
struct CProcessedFileInfo
{
FILETIME CTime;
FILETIME ATime;
FILETIME MTime;
UInt32 Attrib;
bool CTimeDefined;
bool ATimeDefined;
bool MTimeDefined;
bool AttribDefined;
bool IsDir;
} _fi;
UInt32 _index;
UInt64 _curSize;
bool _curSizeDefined;
COutFileStream *_outFileStreamSpec;
CMyComPtr<ISequentialOutStream> _outFileStream;
COutStreamWithCRC *_crcStreamSpec;
CMyComPtr<ISequentialOutStream> _crcStream;
UStringVector _removePathParts;
bool _stdOutMode;
bool _testMode;
bool _crcMode;
bool _multiArchives;
CMyComPtr<ICompressProgressInfo> _localProgress;
UInt64 _packTotal;
UInt64 _unpTotal;
void CreateComplexDirectory(const UStringVector &dirPathParts, UString &fullPath);
HRESULT GetTime(int index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined);
HRESULT GetUnpackSize();
public:
CLocalProgress *LocalProgressSpec;
UInt64 NumFolders;
UInt64 NumFiles;
UInt64 UnpackSize;
UInt32 CrcSum;
MY_UNKNOWN_IMP2(ICryptoGetTextPassword, ICompressProgressInfo)
// COM_INTERFACE_ENTRY(IArchiveVolumeExtractCallback)
INTERFACE_IArchiveExtractCallback(;)
STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize);
// IArchiveVolumeExtractCallback
// STDMETHOD(GetInStream)(const wchar_t *name, ISequentialInStream **inStream);
STDMETHOD(CryptoGetTextPassword)(BSTR *password);
CArchiveExtractCallback():
WriteCTime(true),
WriteATime(true),
WriteMTime(true),
_multiArchives(false)
{
LocalProgressSpec = new CLocalProgress();
_localProgress = LocalProgressSpec;
}
void InitForMulti(bool multiArchives,
NExtract::NPathMode::EEnum pathMode,
NExtract::NOverwriteMode::EEnum overwriteMode)
{
_multiArchives = multiArchives;
_pathMode = pathMode;
_overwriteMode = overwriteMode;
NumFolders = NumFiles = UnpackSize = 0;
CrcSum = 0;
}
void Init(
const NWildcard::CCensorNode *wildcardCensor,
const CArc *arc,
IFolderArchiveExtractCallback *extractCallback2,
bool stdOutMode, bool testMode, bool crcMode,
const UString &directoryPath,
const UStringVector &removePathParts,
UInt64 packSize);
bool optionKeepBroken;
};
static const wchar_t *kCantAutoRename = L"ERROR: Can not create file with auto name";
static const wchar_t *kCantRenameFile = L"ERROR: Can not rename existing file ";
static const wchar_t *kCantDeleteOutputFile = L"ERROR: Can not delete output file ";
void CArchiveExtractCallback::Init(
const NWildcard::CCensorNode *wildcardCensor,
const CArc *arc,
IFolderArchiveExtractCallback *extractCallback2,
bool stdOutMode, bool testMode, bool crcMode,
const UString &directoryPath,
const UStringVector &removePathParts,
UInt64 packSize)
{
_wildcardCensor = wildcardCensor;
_stdOutMode = stdOutMode;
_testMode = testMode;
_crcMode = crcMode;
_unpTotal = 1;
_packTotal = packSize;
_extractCallback2 = extractCallback2;
_compressProgress.Release();
_extractCallback2.QueryInterface(IID_ICompressProgressInfo, &_compressProgress);
LocalProgressSpec->Init(extractCallback2, true);
LocalProgressSpec->SendProgress = false;
_removePathParts = removePathParts;
_arc = arc;
_directoryPath = directoryPath;
NFile::NName::NormalizeDirPathPrefix(_directoryPath);
}
STDMETHODIMP CArchiveExtractCallback::SetTotal(UInt64 size)
{
COM_TRY_BEGIN
_unpTotal = size;
if (!_multiArchives && _extractCallback2)
return _extractCallback2->SetTotal(size);
return S_OK;
COM_TRY_END
}
static void NormalizeVals(UInt64 &v1, UInt64 &v2)
{
const UInt64 kMax = (UInt64)1 << 31;
while (v1 > kMax)
{
v1 >>= 1;
v2 >>= 1;
}
}
static UInt64 MyMultDiv64(UInt64 unpCur, UInt64 unpTotal, UInt64 packTotal)
{
NormalizeVals(packTotal, unpTotal);
NormalizeVals(unpCur, unpTotal);
if (unpTotal == 0)
unpTotal = 1;
return unpCur * packTotal / unpTotal;
}
STDMETHODIMP CArchiveExtractCallback::SetCompleted(const UInt64 *completeValue)
{
COM_TRY_BEGIN
if (!_extractCallback2)
return S_OK;
if (_multiArchives)
{
if (completeValue != NULL)
{
UInt64 packCur = LocalProgressSpec->InSize + MyMultDiv64(*completeValue, _unpTotal, _packTotal);
return _extractCallback2->SetCompleted(&packCur);
}
}
return _extractCallback2->SetCompleted(completeValue);
COM_TRY_END
}
STDMETHODIMP CArchiveExtractCallback::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
{
COM_TRY_BEGIN
return _localProgress->SetRatioInfo(inSize, outSize);
COM_TRY_END
}
void CArchiveExtractCallback::CreateComplexDirectory(const UStringVector &dirPathParts, UString &fullPath)
{
fullPath = _directoryPath;
for (int i = 0; i < dirPathParts.Size(); i++)
{
if (i > 0)
fullPath += wchar_t(NFile::NName::kDirDelimiter);
fullPath += dirPathParts[i];
NFile::NDirectory::MyCreateDirectory(fullPath);
}
}
HRESULT CArchiveExtractCallback::GetTime(int index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined)
{
filetimeIsDefined = false;
NCOM::CPropVariant prop;
RINOK(_arc->Archive->GetProperty(index, propID, &prop));
if (prop.vt == VT_FILETIME)
{
filetime = prop.filetime;
filetimeIsDefined = (filetime.dwHighDateTime != 0 || filetime.dwLowDateTime != 0);
}
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
HRESULT CArchiveExtractCallback::GetUnpackSize()
{
NCOM::CPropVariant prop;
RINOK(_arc->Archive->GetProperty(_index, kpidSize, &prop));
_curSizeDefined = (prop.vt != VT_EMPTY);
if (_curSizeDefined)
_curSize = ConvertPropVariantToUInt64(prop);
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index, ISequentialOutStream **outStream, Int32 askExtractMode)
{
COM_TRY_BEGIN
_crcStream.Release();
*outStream = 0;
_outFileStream.Release();
_encrypted = false;
_isSplit = false;
_curSize = 0;
_curSizeDefined = false;
_index = index;
UString fullPath;
IInArchive *archive = _arc->Archive;
RINOK(_arc->GetItemPath(index, fullPath));
RINOK(IsArchiveItemFolder(archive, index, _fi.IsDir));
_filePath = fullPath;
{
NCOM::CPropVariant prop;
RINOK(archive->GetProperty(index, kpidPosition, &prop));
if (prop.vt != VT_EMPTY)
{
if (prop.vt != VT_UI8)
return E_FAIL;
_position = prop.uhVal.QuadPart;
_isSplit = true;
}
}
RINOK(GetArchiveItemBoolProp(archive, index, kpidEncrypted, _encrypted));
RINOK(GetUnpackSize());
if (_wildcardCensor)
{
if (!_wildcardCensor->CheckPath(fullPath, !_fi.IsDir))
return S_OK;
}
if (askExtractMode == NArchive::NExtract::NAskMode::kExtract && !_testMode)
{
if (_stdOutMode)
{
CMyComPtr<ISequentialOutStream> outStreamLoc = new CStdOutFileStream;
*outStream = outStreamLoc.Detach();
return S_OK;
}
{
NCOM::CPropVariant prop;
RINOK(archive->GetProperty(index, kpidAttrib, &prop));
if (prop.vt == VT_UI4)
{
_fi.Attrib = prop.ulVal;
_fi.AttribDefined = true;
}
else if (prop.vt == VT_EMPTY)
_fi.AttribDefined = false;
else
return E_FAIL;
}
RINOK(GetTime(index, kpidCTime, _fi.CTime, _fi.CTimeDefined));
RINOK(GetTime(index, kpidATime, _fi.ATime, _fi.ATimeDefined));
RINOK(GetTime(index, kpidMTime, _fi.MTime, _fi.MTimeDefined));
bool isAnti = false;
RINOK(_arc->IsItemAnti(index, isAnti));
UStringVector pathParts;
SplitPathToParts(fullPath, pathParts);
if (pathParts.IsEmpty())
return E_FAIL;
int numRemovePathParts = 0;
switch(_pathMode)
{
case NExtract::NPathMode::kFullPathnames:
break;
case NExtract::NPathMode::kCurrentPathnames:
{
numRemovePathParts = _removePathParts.Size();
if (pathParts.Size() <= numRemovePathParts)
return E_FAIL;
for (int i = 0; i < numRemovePathParts; i++)
if (_removePathParts[i].CompareNoCase(pathParts[i]) != 0)
return E_FAIL;
break;
}
case NExtract::NPathMode::kNoPathnames:
{
numRemovePathParts = pathParts.Size() - 1;
break;
}
}
pathParts.Delete(0, numRemovePathParts);
MakeCorrectPath(pathParts);
UString processedPath = MakePathNameFromParts(pathParts);
if (!isAnti)
{
if (!_fi.IsDir)
{
if (!pathParts.IsEmpty())
pathParts.DeleteBack();
}
if (!pathParts.IsEmpty())
{
UString fullPathNew;
CreateComplexDirectory(pathParts, fullPathNew);
if (_fi.IsDir)
NFile::NDirectory::SetDirTime(fullPathNew,
(WriteCTime && _fi.CTimeDefined) ? &_fi.CTime : NULL,
(WriteATime && _fi.ATimeDefined) ? &_fi.ATime : NULL,
(WriteMTime && _fi.MTimeDefined) ? &_fi.MTime : (_arc->MTimeDefined ? &_arc->MTime : NULL));
}
}
UString fullProcessedPath = _directoryPath + processedPath;
if (_fi.IsDir)
{
_diskFilePath = fullProcessedPath;
if (isAnti)
NFile::NDirectory::MyRemoveDirectory(_diskFilePath);
return S_OK;
}
if (!_isSplit)
{
Int32 overwiteResult;
UInt64 index64 = index;
RINOK(_extractCallback2->AskOverwrite(fullProcessedPath, NULL, &index64, NULL, NULL, NULL, &overwiteResult));
if (overwiteResult==0)
return S_OK; // no overwrite
}
if (!isAnti)
{
_outFileStreamSpec = new COutFileStream;
CMyComPtr<ISequentialOutStream> outStreamLoc(_outFileStreamSpec);
if (!_outFileStreamSpec->Open(fullProcessedPath, _isSplit ? OPEN_ALWAYS: CREATE_ALWAYS))
{
// if (::GetLastError() != ERROR_FILE_EXISTS || !isSplit)
{
UString message = L"can not open output file " + fullProcessedPath;
RINOK(_extractCallback2->MessageError(message));
return S_OK;
}
}