-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.cpp
1161 lines (1027 loc) · 33.3 KB
/
extract.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
#include "rar.hpp"
CmdExtract::CmdExtract(CommandData *Cmd)
{
CmdExtract::Cmd=Cmd;
*ArcName=0;
*DestFileName=0;
TotalFileCount=0;
Unp=new Unpack(&DataIO);
#ifdef RAR_SMP
Unp->SetThreads(Cmd->Threads);
#endif
}
CmdExtract::~CmdExtract()
{
delete Unp;
}
void CmdExtract::DoExtract()
{
#if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT)
Fat32=NotFat32=false;
#endif
PasswordCancelled=false;
DataIO.SetCurrentCommand(Cmd->Command[0]);
FindData FD;
while (Cmd->GetArcName(ArcName,ASIZE(ArcName)))
if (FindFile::FastFind(ArcName,&FD))
DataIO.TotalArcSize+=FD.Size;
Cmd->ArcNames.Rewind();
while (Cmd->GetArcName(ArcName,ASIZE(ArcName)))
{
if (Cmd->ManualPassword)
Cmd->Password.Clean(); // Clean user entered password before processing next archive.
while (true)
{
EXTRACT_ARC_CODE Code=ExtractArchive();
if (Code!=EXTRACT_ARC_REPEAT)
break;
}
if (FindFile::FastFind(ArcName,&FD))
DataIO.ProcessedArcSize+=FD.Size;
}
// Clean user entered password. Not really required, just for extra safety.
if (Cmd->ManualPassword)
Cmd->Password.Clean();
if (TotalFileCount==0 && Cmd->Command[0]!='I' &&
ErrHandler.GetErrorCode()!=RARX_BADPWD) // Not in case of wrong archive password.
{
if (!PasswordCancelled)
uiMsg(UIERROR_NOFILESTOEXTRACT,ArcName);
ErrHandler.SetErrorCode(RARX_NOFILES);
}
#ifndef GUI
else
if (!Cmd->DisableDone)
if (Cmd->Command[0]=='I')
mprintf(St(MDone));
else
if (ErrHandler.GetErrorCount()==0)
mprintf(St(MExtrAllOk));
else
mprintf(St(MExtrTotalErr),ErrHandler.GetErrorCount());
#endif
}
void CmdExtract::ExtractArchiveInit(Archive &Arc)
{
DataIO.UnpArcSize=Arc.FileLength();
FileCount=0;
MatchedArgs=0;
#ifndef SFX_MODULE
FirstFile=true;
#endif
PasswordAll=(Cmd->Password.IsSet());
DataIO.UnpVolume=false;
PrevProcessed=false;
AllMatchesExact=true;
ReconstructDone=false;
AnySolidDataUnpackedWell=false;
StartTime.SetCurrentTime();
}
EXTRACT_ARC_CODE CmdExtract::ExtractArchive()
{
Archive Arc(Cmd);
if (!Arc.WOpen(ArcName))
return EXTRACT_ARC_NEXT;
if (!Arc.IsArchive(true))
{
#if !defined(SFX_MODULE) && !defined(RARDLL)
if (CmpExt(ArcName,L"rev"))
{
wchar FirstVolName[NM];
VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),true);
// If several volume names from same volume set are specified
// and current volume is not first in set and first volume is present
// and specified too, let's skip the current volume.
if (wcsicomp(ArcName,FirstVolName)!=0 && FileExist(FirstVolName) &&
Cmd->ArcNames.Search(FirstVolName,false))
return EXTRACT_ARC_NEXT;
RecVolumesTest(Cmd,NULL,ArcName);
TotalFileCount++; // Suppress "No files to extract" message.
return EXTRACT_ARC_NEXT;
}
#endif
#ifndef GUI
mprintf(St(MNotRAR),ArcName);
#endif
#ifndef SFX_MODULE
if (CmpExt(ArcName,L"rar"))
#endif
ErrHandler.SetErrorCode(RARX_WARNING);
return EXTRACT_ARC_NEXT;
}
if (Arc.FailedHeaderDecryption) // Bad archive password.
return EXTRACT_ARC_NEXT;
#ifndef SFX_MODULE
if (Arc.Volume && !Arc.FirstVolume)
{
wchar FirstVolName[NM];
VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),Arc.NewNumbering);
// If several volume names from same volume set are specified
// and current volume is not first in set and first volume is present
// and specified too, let's skip the current volume.
if (wcsicomp(ArcName,FirstVolName)!=0 && FileExist(FirstVolName) &&
Cmd->ArcNames.Search(FirstVolName,false))
return EXTRACT_ARC_NEXT;
}
#endif
int64 VolumeSetSize=0; // Total size of volumes after the current volume.
if (Arc.Volume)
{
// Calculate the total size of all accessible volumes.
// This size is necessary to display the correct total progress indicator.
wchar NextName[NM];
wcscpy(NextName,Arc.FileName);
while (true)
{
// First volume is already added to DataIO.TotalArcSize
// in initial TotalArcSize calculation in DoExtract.
// So we skip it and start from second volume.
NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering);
FindData FD;
if (FindFile::FastFind(NextName,&FD))
VolumeSetSize+=FD.Size;
else
break;
}
DataIO.TotalArcSize+=VolumeSetSize;
}
ExtractArchiveInit(Arc);
if (*Cmd->Command=='T' || *Cmd->Command=='I')
Cmd->Test=true;
if (*Cmd->Command=='I')
{
#ifndef GUI
Cmd->DisablePercentage=true;
#endif
}
else
uiStartArchiveExtract(!Cmd->Test,ArcName);
Arc.ViewComment();
while (1)
{
size_t Size=Arc.ReadHeader();
bool Repeat=false;
if (!ExtractCurrentFile(Arc,Size,Repeat))
if (Repeat)
{
// If we started extraction from not first volume and need to
// restart it from first, we must correct DataIO.TotalArcSize
// for correct total progress display. We subtract the size
// of current volume and all volumes after it and add the size
// of new (first) volume.
FindData OldArc,NewArc;
if (FindFile::FastFind(Arc.FileName,&OldArc) &&
FindFile::FastFind(ArcName,&NewArc))
DataIO.TotalArcSize-=VolumeSetSize+OldArc.Size-NewArc.Size;
return EXTRACT_ARC_REPEAT;
}
else
break;
}
#if !defined(SFX_MODULE) && !defined(RARDLL)
if (Cmd->Test && Arc.Volume)
RecVolumesTest(Cmd,&Arc,ArcName);
#endif
return EXTRACT_ARC_NEXT;
}
bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat)
{
// We can get negative sizes in corrupt archive and it is unacceptable
// for size comparisons in CmdExtract::UnstoreFile and ComprDataIO::UnpRead,
// where we cast sizes to size_t and can exceed another read or available
// size. We could fix it when reading an archive. But we prefer to do it
// here, because this function is called directly in unrar.dll, so we fix
// bad parameters passed to dll. Also we want to see real negative sizes
// in the listing of corrupt archive.
if (Arc.FileHead.PackSize<0)
Arc.FileHead.PackSize=0;
if (Arc.FileHead.UnpSize<0)
Arc.FileHead.UnpSize=0;
wchar Command=Cmd->Command[0];
if (HeaderSize==0)
if (DataIO.UnpVolume)
{
#ifdef NOVOLUME
return false;
#else
// Supposing we unpack an old RAR volume without end of archive record
// and last file is not split between volumes.
if (!MergeArchive(Arc,&DataIO,false,Command))
{
ErrHandler.SetErrorCode(RARX_WARNING);
return false;
}
#endif
}
else
return false;
HEADER_TYPE HeaderType=Arc.GetHeaderType();
if (HeaderType!=HEAD_FILE)
{
#ifndef SFX_MODULE
if (HeaderType==HEAD3_OLDSERVICE && PrevProcessed)
SetExtraInfo20(Cmd,Arc,DestFileName);
#endif
if (HeaderType==HEAD_SERVICE && PrevProcessed)
SetExtraInfo(Cmd,Arc,DestFileName);
if (HeaderType==HEAD_ENDARC)
if (Arc.EndArcHead.NextVolume)
{
#ifndef NOVOLUME
if (!MergeArchive(Arc,&DataIO,false,Command))
{
ErrHandler.SetErrorCode(RARX_WARNING);
return false;
}
#endif
Arc.Seek(Arc.CurBlockPos,SEEK_SET);
return true;
}
else
return false;
Arc.SeekToNext();
return true;
}
PrevProcessed=false;
if (!Cmd->Recurse && MatchedArgs>=Cmd->FileArgs.ItemsCount() && AllMatchesExact)
return false;
int MatchType=MATCH_WILDSUBPATH;
bool EqualNames=false;
wchar MatchedArg[NM];
int MatchNumber=Cmd->IsProcessFile(Arc.FileHead,&EqualNames,MatchType,MatchedArg,ASIZE(MatchedArg));
bool MatchFound=MatchNumber!=0;
#ifndef SFX_MODULE
if (Cmd->ExclPath==EXCL_BASEPATH)
{
wcsncpyz(Cmd->ArcPath,MatchedArg,ASIZE(Cmd->ArcPath));
*PointToName(Cmd->ArcPath)=0;
if (IsWildcard(Cmd->ArcPath)) // Cannot correctly process path*\* masks here.
*Cmd->ArcPath=0;
}
#endif
if (MatchFound && !EqualNames)
AllMatchesExact=false;
Arc.ConvertAttributes();
#if !defined(SFX_MODULE) && !defined(RARDLL)
if (Arc.FileHead.SplitBefore && FirstFile)
{
wchar CurVolName[NM];
wcsncpyz(CurVolName,ArcName,ASIZE(CurVolName));
VolNameToFirstName(ArcName,ArcName,ASIZE(ArcName),Arc.NewNumbering);
if (wcsicomp(ArcName,CurVolName)!=0 && FileExist(ArcName))
{
// If first volume name does not match the current name and if such
// volume name really exists, let's unpack from this first volume.
Repeat=true;
return false;
}
#ifndef RARDLL
if (!ReconstructDone)
{
ReconstructDone=true;
if (RecVolumesRestore(Cmd,Arc.FileName,true))
{
Repeat=true;
return false;
}
}
#endif
wcsncpyz(ArcName,CurVolName,ASIZE(ArcName));
}
#endif
wchar ArcFileName[NM];
ConvertPath(Arc.FileHead.FileName,ArcFileName);
#ifdef _WIN_ALL
if (!Cmd->AllowIncompatNames)
MakeNameCompatible(ArcFileName);
#endif
if (Arc.FileHead.Version)
{
if (Cmd->VersionControl!=1 && !EqualNames)
{
if (Cmd->VersionControl==0)
MatchFound=false;
int Version=ParseVersionFileName(ArcFileName,false);
if (Cmd->VersionControl-1==Version)
ParseVersionFileName(ArcFileName,true);
else
MatchFound=false;
}
}
else
if (!Arc.IsArcDir() && Cmd->VersionControl>1)
MatchFound=false;
DataIO.UnpVolume=Arc.FileHead.SplitAfter;
DataIO.NextVolumeMissing=false;
Arc.Seek(Arc.NextBlockPos-Arc.FileHead.PackSize,SEEK_SET);
bool ExtrFile=false;
bool SkipSolid=false;
#ifndef SFX_MODULE
if (FirstFile && (MatchFound || Arc.Solid) && Arc.FileHead.SplitBefore)
{
if (MatchFound)
{
uiMsg(UIERROR_NEEDPREVVOL,Arc.FileName,ArcFileName);
#ifdef RARDLL
Cmd->DllError=ERAR_BAD_DATA;
#endif
ErrHandler.SetErrorCode(RARX_OPEN);
}
MatchFound=false;
}
FirstFile=false;
#endif
if (MatchFound || (SkipSolid=Arc.Solid)!=0)
{
// First common call of uiStartFileExtract. It is done before overwrite
// prompts, so if SkipSolid state is changed below, we'll need to make
// additional uiStartFileExtract calls with updated parameters.
if (!uiStartFileExtract(ArcFileName,!Cmd->Test,Cmd->Test && Command!='I',SkipSolid))
return false;
ExtrPrepareName(Arc,ArcFileName,DestFileName,ASIZE(DestFileName));
// DestFileName can be set empty in case of excessive -ap switch.
ExtrFile=!SkipSolid && *DestFileName!=0 && !Arc.FileHead.SplitBefore;
if ((Cmd->FreshFiles || Cmd->UpdateFiles) && (Command=='E' || Command=='X'))
{
FindData FD;
if (FindFile::FastFind(DestFileName,&FD))
{
if (FD.mtime >= Arc.FileHead.mtime)
{
// If directory already exists and its modification time is newer
// than start of extraction, it is likely it was created
// when creating a path to one of already extracted items.
// In such case we'll better update its time even if archived
// directory is older.
if (!FD.IsDir || FD.mtime<StartTime)
ExtrFile=false;
}
}
else
if (Cmd->FreshFiles)
ExtrFile=false;
}
if (Arc.FileHead.Encrypted)
{
// Stop archive extracting if user cancelled a password prompt.
#ifdef RARDLL
if (!ExtrDllGetPassword())
{
Cmd->DllError=ERAR_MISSING_PASSWORD;
return false;
}
#else
if (!ExtrGetPassword(Arc,ArcFileName))
{
PasswordCancelled=true;
return false;
}
#endif
// Skip only the current encrypted file if empty password is entered.
// Actually our "cancel" code above intercepts empty passwords too now,
// so we keep the code below just in case we'll decide process empty
// and cancelled passwords differently sometimes.
if (!Cmd->Password.IsSet())
{
ErrHandler.SetErrorCode(RARX_WARNING);
#ifdef RARDLL
Cmd->DllError=ERAR_MISSING_PASSWORD;
#endif
ExtrFile=false;
}
}
#ifdef RARDLL
if (*Cmd->DllDestName!=0)
wcsncpyz(DestFileName,Cmd->DllDestName,ASIZE(DestFileName));
#endif
if (!CheckUnpVer(Arc,ArcFileName))
{
ErrHandler.SetErrorCode(RARX_FATAL);
#ifdef RARDLL
Cmd->DllError=ERAR_UNKNOWN_FORMAT;
#endif
Arc.SeekToNext();
return !Arc.Solid; // Can try extracting next file only in non-solid archive.
}
// Set a password before creating the file, so we can skip creating
// in case of wrong password.
SecPassword FilePassword=Cmd->Password;
#if defined(_WIN_ALL) && !defined(SFX_MODULE)
ConvertDosPassword(Arc,FilePassword);
#endif
byte PswCheck[SIZE_PSWCHECK];
DataIO.SetEncryption(false,Arc.FileHead.CryptMethod,&FilePassword,
Arc.FileHead.SaltSet ? Arc.FileHead.Salt:NULL,
Arc.FileHead.InitV,Arc.FileHead.Lg2Count,
Arc.FileHead.HashKey,PswCheck);
// If header is damaged, we cannot rely on password check value,
// because it can be damaged too.
if (Arc.FileHead.Encrypted && Arc.FileHead.UsePswCheck &&
memcmp(Arc.FileHead.PswCheck,PswCheck,SIZE_PSWCHECK)!=0 &&
!Arc.BrokenHeader)
{
uiMsg(UIERROR_BADPSW,Arc.FileName);
ErrHandler.SetErrorCode(RARX_BADPWD);
ExtrFile=false;
#ifdef RARDLL
// If we already have ERAR_EOPEN as result of missing volume,
// we should not replace it with less precise ERAR_BAD_DATA.
if (Cmd->DllError!=ERAR_EOPEN)
Cmd->DllError=ERAR_BAD_PASSWORD;
#endif
}
File CurFile;
bool LinkEntry=Arc.FileHead.RedirType!=FSREDIR_NONE;
if (LinkEntry && Arc.FileHead.RedirType!=FSREDIR_FILECOPY)
{
if (ExtrFile && Command!='P' && !Cmd->Test)
{
// Overwrite prompt for symbolic and hard links.
bool UserReject=false;
if (FileExist(DestFileName) && !UserReject)
FileCreate(Cmd,NULL,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime);
if (UserReject)
ExtrFile=false;
}
}
else
if (Arc.IsArcDir())
{
if (!ExtrFile || Command=='P' || Command=='I' || Command=='E' || Cmd->ExclPath==EXCL_SKIPWHOLEPATH)
return true;
TotalFileCount++;
ExtrCreateDir(Arc,ArcFileName);
return true;
}
else
if (ExtrFile) // Create files and file copies (FSREDIR_FILECOPY).
ExtrFile=ExtrCreateFile(Arc,CurFile);
if (!ExtrFile && Arc.Solid)
{
SkipSolid=true;
ExtrFile=true;
// We changed SkipSolid, so we need to call uiStartFileExtract
// with "Skip" parameter to change the operation status
// from "extracting" to "skipping". For example, it can be necessary
// if user answered "No" to overwrite prompt when unpacking
// a solid archive.
if (!uiStartFileExtract(ArcFileName,false,false,true))
return false;
}
if (ExtrFile)
{
// Set it in test mode, so we also test subheaders such as NTFS streams
// after tested file.
if (Cmd->Test)
PrevProcessed=true;
bool TestMode=Cmd->Test || SkipSolid; // Unpack to memory, not to disk.
if (!SkipSolid)
{
if (!TestMode && Command!='P' && CurFile.IsDevice())
{
uiMsg(UIERROR_INVALIDNAME,Arc.FileName,DestFileName);
ErrHandler.WriteError(Arc.FileName,DestFileName);
}
TotalFileCount++;
}
FileCount++;
#ifndef GUI
if (Command!='I')
if (SkipSolid)
mprintf(St(MExtrSkipFile),ArcFileName);
else
switch(Cmd->Test ? 'T':Command) // "Test" can be also enabled by -t switch.
{
case 'T':
mprintf(St(MExtrTestFile),ArcFileName);
break;
#ifndef SFX_MODULE
case 'P':
mprintf(St(MExtrPrinting),ArcFileName);
break;
#endif
case 'X':
case 'E':
mprintf(St(MExtrFile),DestFileName);
break;
}
if (!Cmd->DisablePercentage)
mprintf(L" ");
#endif
DataIO.CurUnpRead=0;
DataIO.CurUnpWrite=0;
DataIO.UnpHash.Init(Arc.FileHead.FileHash.Type,Cmd->Threads);
DataIO.PackedDataHash.Init(Arc.FileHead.FileHash.Type,Cmd->Threads);
DataIO.SetPackedSizeToRead(Arc.FileHead.PackSize);
DataIO.SetFiles(&Arc,&CurFile);
DataIO.SetTestMode(TestMode);
DataIO.SetSkipUnpCRC(SkipSolid);
#if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT)
if (!TestMode && !Arc.BrokenHeader &&
Arc.FileHead.UnpSize>0xffffffff && (Fat32 || !NotFat32))
{
if (!Fat32) // Not detected yet.
NotFat32=!(Fat32=IsFAT(Cmd->ExtrPath));
if (Fat32)
uiMsg(UIMSG_FAT32SIZE); // Inform user about FAT32 size limit.
}
#endif
if (!TestMode && !Arc.BrokenHeader &&
(Arc.FileHead.PackSize<<11)>Arc.FileHead.UnpSize &&
(Arc.FileHead.UnpSize<100000000 || Arc.FileLength()>Arc.FileHead.PackSize))
CurFile.Prealloc(Arc.FileHead.UnpSize);
CurFile.SetAllowDelete(!Cmd->KeepBroken);
bool FileCreateMode=!TestMode && !SkipSolid && Command!='P';
bool ShowChecksum=true; // Display checksum verification result.
bool LinkSuccess=true; // Assume success for test mode.
if (LinkEntry)
{
FILE_SYSTEM_REDIRECT Type=Arc.FileHead.RedirType;
if (Type==FSREDIR_HARDLINK || Type==FSREDIR_FILECOPY)
{
wchar NameExisting[NM];
ExtrPrepareName(Arc,Arc.FileHead.RedirName,NameExisting,ASIZE(NameExisting));
if (FileCreateMode && *NameExisting!=0) // *NameExisting can be 0 in case of excessive -ap switch.
if (Type==FSREDIR_HARDLINK)
LinkSuccess=ExtractHardlink(DestFileName,NameExisting,ASIZE(NameExisting));
else
LinkSuccess=ExtractFileCopy(CurFile,Arc.FileName,DestFileName,NameExisting,ASIZE(NameExisting));
}
else
if (Type==FSREDIR_UNIXSYMLINK || Type==FSREDIR_WINSYMLINK || Type==FSREDIR_JUNCTION)
{
if (FileCreateMode)
LinkSuccess=ExtractSymlink(Cmd,DataIO,Arc,DestFileName);
}
else
{
uiMsg(UIERROR_UNKNOWNEXTRA, Arc.FileName, DestFileName);
LinkSuccess=false;
}
if (!LinkSuccess || Arc.Format==RARFMT15 && !FileCreateMode)
{
// RAR 5.x links have a valid data checksum even in case of
// failure, because they do not store any data.
// We do not want to display "OK" in this case.
// For 4.x symlinks we verify the checksum only when extracting,
// but not when testing an archive.
ShowChecksum=false;
}
PrevProcessed=FileCreateMode && LinkSuccess;
}
else
if (!Arc.FileHead.SplitBefore)
if (Arc.FileHead.Method==0)
UnstoreFile(DataIO,Arc.FileHead.UnpSize);
else
{
#ifdef _ANDROID
// malloc and new do not report memory allocation errors
// in Android, so if free memory is set, check it here
// trying to prevent crash.
if (Cmd->FreeMem!=0 && Cmd->FreeMem < Arc.FileHead.WinSize)
throw std::bad_alloc();
#endif
Unp->Init(Arc.FileHead.WinSize,Arc.FileHead.Solid);
Unp->SetDestSize(Arc.FileHead.UnpSize);
#ifndef SFX_MODULE
if (Arc.Format!=RARFMT50 && Arc.FileHead.UnpVer<=15)
Unp->DoUnpack(15,FileCount>1 && Arc.Solid);
else
#endif
Unp->DoUnpack(Arc.FileHead.UnpVer,Arc.FileHead.Solid);
}
Arc.SeekToNext();
// We check for "split after" flag to detect partially extracted files
// from incomplete volume sets. For them file header contains packed
// data hash, which must not be compared against unpacked data hash
// to prevent accidental match. Moreover, for -m0 volumes packed data
// hash would match truncated unpacked data hash and lead to fake "OK"
// in incomplete volume set.
bool ValidCRC=!Arc.FileHead.SplitAfter && DataIO.UnpHash.Cmp(&Arc.FileHead.FileHash,Arc.FileHead.UseHashKey ? Arc.FileHead.HashKey:NULL);
// We set AnySolidDataUnpackedWell to true if we found at least one
// valid non-zero solid file in preceding solid stream. If it is true
// and if current encrypted file is broken, we do not need to hint
// about a wrong password and can report CRC error only.
if (!Arc.FileHead.Solid)
AnySolidDataUnpackedWell=false; // Reset the flag, because non-solid file is found.
else
if (Arc.FileHead.Method!=0 && Arc.FileHead.UnpSize>0 && ValidCRC)
AnySolidDataUnpackedWell=true;
bool BrokenFile=false;
// Checksum is not calculated in skip solid mode for performance reason.
if (!SkipSolid && ShowChecksum)
{
if (ValidCRC)
{
#ifndef GUI
if (Command!='P' && Command!='I')
mprintf(L"%s%s ",Cmd->DisablePercentage ? L" ":L"\b\b\b\b\b ",
Arc.FileHead.FileHash.Type==HASH_NONE ? L" ?":St(MOk));
#endif
}
else
{
if (Arc.FileHead.Encrypted && (!Arc.FileHead.UsePswCheck ||
Arc.BrokenHeader) && !AnySolidDataUnpackedWell)
uiMsg(UIERROR_CHECKSUMENC,Arc.FileName,ArcFileName);
else
uiMsg(UIERROR_CHECKSUM,Arc.FileName,ArcFileName);
BrokenFile=true;
ErrHandler.SetErrorCode(RARX_CRC);
#ifdef RARDLL
// If we already have ERAR_EOPEN as result of missing volume
// or ERAR_BAD_PASSWORD for RAR5 wrong password,
// we should not replace it with less precise ERAR_BAD_DATA.
if (Cmd->DllError!=ERAR_EOPEN && Cmd->DllError!=ERAR_BAD_PASSWORD)
Cmd->DllError=ERAR_BAD_DATA;
#endif
}
}
#ifndef GUI
else
mprintf(L"\b\b\b\b\b ");
#endif
if (!TestMode && (Command=='X' || Command=='E') &&
(!LinkEntry || Arc.FileHead.RedirType==FSREDIR_FILECOPY && LinkSuccess) &&
(!BrokenFile || Cmd->KeepBroken))
{
// We could preallocate more space that really written to broken file.
if (BrokenFile)
CurFile.Truncate();
#if defined(_WIN_ALL) || defined(_EMX)
if (Cmd->ClearArc)
Arc.FileHead.FileAttr&=~FILE_ATTRIBUTE_ARCHIVE;
#endif
CurFile.SetOpenFileTime(
Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime,
Cmd->xctime==EXTTIME_NONE ? NULL:&Arc.FileHead.ctime,
Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime);
CurFile.Close();
#if defined(_WIN_ALL) && !defined(SFX_MODULE)
if (Cmd->SetCompressedAttr &&
(Arc.FileHead.FileAttr & FILE_ATTRIBUTE_COMPRESSED)!=0)
SetFileCompression(CurFile.FileName,true);
#endif
#ifdef _UNIX
if (Cmd->ProcessOwners && Arc.Format==RARFMT50 && Arc.FileHead.UnixOwnerSet)
SetUnixOwner(Arc,CurFile.FileName);
#endif
CurFile.SetCloseFileTime(
Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime,
Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime);
if (!Cmd->IgnoreGeneralAttr && !SetFileAttr(CurFile.FileName,Arc.FileHead.FileAttr))
uiMsg(UIERROR_FILEATTR,Arc.FileName,CurFile.FileName);
PrevProcessed=true;
}
}
}
if (MatchFound)
MatchedArgs++;
if (DataIO.NextVolumeMissing)
return false;
if (!ExtrFile)
if (!Arc.Solid)
Arc.SeekToNext();
else
if (!SkipSolid)
return false;
return true;
}
void CmdExtract::UnstoreFile(ComprDataIO &DataIO,int64 DestUnpSize)
{
// 512 KB and larger buffer reported to reduce performance on old XP
// computers with WDC WD2000JD HDD. According to test made by user
// 256 KB buffer is optimal.
Array<byte> Buffer(0x40000);
while (1)
{
uint Code=DataIO.UnpRead(&Buffer[0],Buffer.Size());
if (Code==0 || (int)Code==-1)
break;
Code=Code<DestUnpSize ? Code:(uint)DestUnpSize;
DataIO.UnpWrite(&Buffer[0],Code);
if (DestUnpSize>=0)
DestUnpSize-=Code;
}
}
bool CmdExtract::ExtractFileCopy(File &New,wchar *ArcName,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize)
{
SlashToNative(NameExisting,NameExisting,NameExistingSize); // Not needed for RAR 5.1+ archives.
File Existing;
if (!Existing.WOpen(NameExisting))
{
uiMsg(UIERROR_FILECOPY,ArcName,NameExisting,NameNew);
uiMsg(UIERROR_FILECOPYHINT,ArcName);
#ifdef RARDLL
Cmd->DllError=ERAR_EREFERENCE;
#endif
return false;
}
Array<char> Buffer(0x100000);
int64 CopySize=0;
while (true)
{
Wait();
int ReadSize=Existing.Read(&Buffer[0],Buffer.Size());
if (ReadSize==0)
break;
New.Write(&Buffer[0],ReadSize);
CopySize+=ReadSize;
}
return true;
}
void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *DestName,size_t DestSize)
{
wcsncpyz(DestName,Cmd->ExtrPath,DestSize);
if (*Cmd->ExtrPath!=0)
{
wchar LastChar=*PointToLastChar(Cmd->ExtrPath);
// We need IsPathDiv check here to correctly handle Unix forward slash
// in the end of destination path in Windows: rar x arc dest/
// IsDriveDiv is needed for current drive dir: rar x arc d:
if (!IsPathDiv(LastChar) && !IsDriveDiv(LastChar))
{
// Destination path can be without trailing slash if it come from GUI shell.
AddEndSlash(DestName,DestSize);
}
}
#ifndef SFX_MODULE
if (Cmd->AppendArcNameToPath)
{
wcsncatz(DestName,PointToName(Arc.FirstVolumeName),DestSize);
SetExt(DestName,NULL,DestSize);
AddEndSlash(DestName,DestSize);
}
#endif
#ifndef SFX_MODULE
size_t ArcPathLength=wcslen(Cmd->ArcPath);
if (ArcPathLength>0)
{
size_t NameLength=wcslen(ArcFileName);
ArcFileName+=Min(ArcPathLength,NameLength);
while (*ArcFileName==CPATHDIVIDER)
ArcFileName++;
if (*ArcFileName==0) // Excessive -ap switch.
{
*DestName=0;
return;
}
}
#endif
wchar Command=Cmd->Command[0];
// Use -ep3 only in systems, where disk letters are exist, not in Unix.
bool AbsPaths=Cmd->ExclPath==EXCL_ABSPATH && Command=='X' && IsDriveDiv(':');
// We do not use any user specified destination paths when extracting
// absolute paths in -ep3 mode.
if (AbsPaths)
*DestName=0;
if (Command=='E' || Cmd->ExclPath==EXCL_SKIPWHOLEPATH)
wcsncatz(DestName,PointToName(ArcFileName),DestSize);
else
wcsncatz(DestName,ArcFileName,DestSize);
wchar DiskLetter=toupperw(DestName[0]);
if (AbsPaths)
{
if (DestName[1]=='_' && IsPathDiv(DestName[2]) &&
DiskLetter>='A' && DiskLetter<='Z')
DestName[1]=':';
else
if (DestName[0]=='_' && DestName[1]=='_')
{
// Convert __server\share to \\server\share.
DestName[0]=CPATHDIVIDER;
DestName[1]=CPATHDIVIDER;
}
}
}
#ifdef RARDLL
bool CmdExtract::ExtrDllGetPassword()
{
if (!Cmd->Password.IsSet())
{
if (Cmd->Callback!=NULL)
{
wchar PasswordW[MAXPASSWORD];
*PasswordW=0;
if (Cmd->Callback(UCM_NEEDPASSWORDW,Cmd->UserData,(LPARAM)PasswordW,ASIZE(PasswordW))==-1)
*PasswordW=0;
if (*PasswordW==0)
{
char PasswordA[MAXPASSWORD];
*PasswordA=0;
if (Cmd->Callback(UCM_NEEDPASSWORD,Cmd->UserData,(LPARAM)PasswordA,ASIZE(PasswordA))==-1)
*PasswordA=0;
GetWideName(PasswordA,NULL,PasswordW,ASIZE(PasswordW));
cleandata(PasswordA,sizeof(PasswordA));
}
Cmd->Password.Set(PasswordW);
cleandata(PasswordW,sizeof(PasswordW));
Cmd->ManualPassword=true;
}
if (!Cmd->Password.IsSet())
return false;
}
return true;
}
#endif
#ifndef RARDLL
bool CmdExtract::ExtrGetPassword(Archive &Arc,const wchar *ArcFileName)
{
if (!Cmd->Password.IsSet())
{
if (!uiGetPassword(UIPASSWORD_FILE,ArcFileName,&Cmd->Password) || !Cmd->Password.IsSet())
{
// Suppress "test is ok" message in GUI if user entered
// an empty password or cancelled a password prompt.
uiMsg(UIERROR_INCERRCOUNT);
return false;
}
Cmd->ManualPassword=true;
}
#if !defined(GUI) && !defined(SILENT)
else
if (!PasswordAll && !Arc.FileHead.Solid)
{
eprintf(St(MUseCurPsw),ArcFileName);
switch(Cmd->AllYes ? 1 : Ask(St(MYesNoAll)))
{
case -1:
ErrHandler.Exit(RARX_USERBREAK);
case 2:
if (!uiGetPassword(UIPASSWORD_FILE,ArcFileName,&Cmd->Password))
return false;
break;
case 3:
PasswordAll=true;
break;
}
}
#endif
return true;
}
#endif
#if defined(_WIN_ALL) && !defined(SFX_MODULE)
void CmdExtract::ConvertDosPassword(Archive &Arc,SecPassword &DestPwd)
{
if (Arc.Format==RARFMT15 && Arc.FileHead.HostOS==HOST_MSDOS)
{
// We need the password in OEM encoding if file was encrypted by
// native RAR/DOS (not extender based). Let's make the conversion.
wchar PlainPsw[MAXPASSWORD];
Cmd->Password.Get(PlainPsw,ASIZE(PlainPsw));
char PswA[MAXPASSWORD];
CharToOemBuffW(PlainPsw,PswA,ASIZE(PswA));
PswA[ASIZE(PswA)-1]=0;
CharToWide(PswA,PlainPsw,ASIZE(PlainPsw));
DestPwd.Set(PlainPsw);
cleandata(PlainPsw,sizeof(PlainPsw));