-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path7zUpdate.cpp
2512 lines (2121 loc) · 61.7 KB
/
7zUpdate.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
// 7zUpdate.cpp
#include "StdAfx.h"
#include "../../../../C/CpuArch.h"
#include "../../../Common/Wildcard.h"
#include "../../Common/CreateCoder.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/ProgressUtils.h"
#include "../../Compress/CopyCoder.h"
#include "../Common/ItemNameUtils.h"
#include "7zDecode.h"
#include "7zEncode.h"
#include "7zFolderInStream.h"
#include "7zHandler.h"
#include "7zOut.h"
#include "7zUpdate.h"
#ifndef WIN32
#include "Windows/FileIO.h"
#endif
namespace NArchive {
namespace N7z {
#define k_X86 k_BCJ
struct CFilterMode
{
UInt32 Id;
UInt32 Delta;
CFilterMode(): Id(0), Delta(0) {}
void SetDelta()
{
if (Id == k_IA64)
Delta = 16;
else if (Id == k_ARM || Id == k_PPC || Id == k_SPARC)
Delta = 4;
else if (Id == k_ARMT)
Delta = 2;
else
Delta = 0;
}
};
/* ---------- PE ---------- */
#define MZ_SIG 0x5A4D
#define PE_SIG 0x00004550
#define PE_OptHeader_Magic_32 0x10B
#define PE_OptHeader_Magic_64 0x20B
#define PE_SectHeaderSize 40
#define PE_SECT_EXECUTE 0x20000000
static int Parse_EXE(const Byte *buf, size_t size, CFilterMode *filterMode)
{
if (size < 512 || GetUi16(buf) != MZ_SIG)
return 0;
const Byte *p;
UInt32 peOffset, optHeaderSize, filterId;
peOffset = GetUi32(buf + 0x3C);
if (peOffset >= 0x1000 || peOffset + 512 > size || (peOffset & 7) != 0)
return 0;
p = buf + peOffset;
if (GetUi32(p) != PE_SIG)
return 0;
p += 4;
switch (GetUi16(p))
{
case 0x014C:
case 0x8664: filterId = k_X86; break;
/*
IMAGE_FILE_MACHINE_ARM 0x01C0 // ARM LE
IMAGE_FILE_MACHINE_THUMB 0x01C2 // ARM Thumb / Thumb-2 LE
IMAGE_FILE_MACHINE_ARMNT 0x01C4 // ARM Thumb-2, LE
Note: We use ARM filter for 0x01C2. (WinCE 5 - 0x01C2) files mostly contain ARM code (not Thumb/Thumb-2).
*/
case 0x01C0: // WinCE old
case 0x01C2: filterId = k_ARM; break; // WinCE new
case 0x01C4: filterId = k_ARMT; break; // WinRT
case 0x0200: filterId = k_IA64; break;
default: return 0;
}
optHeaderSize = GetUi16(p + 16);
if (optHeaderSize > (1 << 10))
return 0;
p += 20; /* headerSize */
switch (GetUi16(p))
{
case PE_OptHeader_Magic_32:
case PE_OptHeader_Magic_64:
break;
default:
return 0;
}
filterMode->Id = filterId;
return 1;
}
/* ---------- ELF ---------- */
#define ELF_SIG 0x464C457F
#define ELF_CLASS_32 1
#define ELF_CLASS_64 2
#define ELF_DATA_2LSB 1
#define ELF_DATA_2MSB 2
static UInt16 Get16(const Byte *p, Bool be) { if (be) return (UInt16)GetBe16(p); return (UInt16)GetUi16(p); }
static UInt32 Get32(const Byte *p, Bool be) { if (be) return GetBe32(p); return GetUi32(p); }
// static UInt64 Get64(const Byte *p, Bool be) { if (be) return GetBe64(p); return GetUi64(p); }
static int Parse_ELF(const Byte *buf, size_t size, CFilterMode *filterMode)
{
Bool /* is32, */ be;
UInt32 filterId;
if (size < 512 || buf[6] != 1) /* ver */
return 0;
if (GetUi32(buf) != ELF_SIG)
return 0;
switch (buf[4])
{
case ELF_CLASS_32: /* is32 = True; */ break;
case ELF_CLASS_64: /* is32 = False; */ break;
default: return 0;
}
switch (buf[5])
{
case ELF_DATA_2LSB: be = False; break;
case ELF_DATA_2MSB: be = True; break;
default: return 0;
}
switch (Get16(buf + 0x12, be))
{
case 3:
case 6:
case 62: filterId = k_X86; break;
case 2:
case 18:
case 43: filterId = k_SPARC; break;
case 20:
case 21: if (!be) return 0; filterId = k_PPC; break;
case 40: if ( be) return 0; filterId = k_ARM; break;
/* Some IA-64 ELF exacutable have size that is not aligned for 16 bytes.
So we don't use IA-64 filter for IA-64 ELF */
// case 50: if ( be) return 0; filterId = k_IA64; break;
default: return 0;
}
filterMode->Id = filterId;
return 1;
}
/* ---------- Mach-O ---------- */
#define MACH_SIG_BE_32 0xCEFAEDFE
#define MACH_SIG_BE_64 0xCFFAEDFE
#define MACH_SIG_LE_32 0xFEEDFACE
#define MACH_SIG_LE_64 0xFEEDFACF
#define MACH_ARCH_ABI64 (1 << 24)
#define MACH_MACHINE_386 7
#define MACH_MACHINE_ARM 12
#define MACH_MACHINE_SPARC 14
#define MACH_MACHINE_PPC 18
#define MACH_MACHINE_PPC64 (MACH_ARCH_ABI64 | MACH_MACHINE_PPC)
#define MACH_MACHINE_AMD64 (MACH_ARCH_ABI64 | MACH_MACHINE_386)
static unsigned Parse_MACH(const Byte *buf, size_t size, CFilterMode *filterMode)
{
UInt32 filterId, numCommands, commandsSize;
if (size < 512)
return 0;
Bool /* mode64, */ be;
switch (GetUi32(buf))
{
case MACH_SIG_BE_32: /* mode64 = False; */ be = True; break;
case MACH_SIG_BE_64: /* mode64 = True; */ be = True; break;
case MACH_SIG_LE_32: /* mode64 = False; */ be = False; break;
case MACH_SIG_LE_64: /* mode64 = True; */ be = False; break;
default: return 0;
}
switch (Get32(buf + 4, be))
{
case MACH_MACHINE_386:
case MACH_MACHINE_AMD64: filterId = k_X86; break;
case MACH_MACHINE_ARM: if ( be) return 0; filterId = k_ARM; break;
case MACH_MACHINE_SPARC: if (!be) return 0; filterId = k_SPARC; break;
case MACH_MACHINE_PPC:
case MACH_MACHINE_PPC64: if (!be) return 0; filterId = k_PPC; break;
default: return 0;
}
numCommands = Get32(buf + 0x10, be);
commandsSize = Get32(buf + 0x14, be);
if (commandsSize > (1 << 24) || numCommands > (1 << 18))
return 0;
filterMode->Id = filterId;
return 1;
}
/* ---------- WAV ---------- */
#define WAV_SUBCHUNK_fmt 0x20746D66
#define WAV_SUBCHUNK_data 0x61746164
#define RIFF_SIG 0x46464952
static Bool Parse_WAV(const Byte *buf, size_t size, CFilterMode *filterMode)
{
UInt32 subChunkSize, pos;
if (size < 0x2C)
return False;
if (GetUi32(buf + 0) != RIFF_SIG ||
GetUi32(buf + 8) != 0x45564157 || // WAVE
GetUi32(buf + 0xC) != WAV_SUBCHUNK_fmt)
return False;
subChunkSize = GetUi32(buf + 0x10);
/* [0x14 = format] = 1 (PCM) */
if (subChunkSize < 0x10 || subChunkSize > 0x12 || GetUi16(buf + 0x14) != 1)
return False;
unsigned numChannels = GetUi16(buf + 0x16);
unsigned bitsPerSample = GetUi16(buf + 0x22);
if ((bitsPerSample & 0x7) != 0 || bitsPerSample >= 256 || numChannels >= 256)
return False;
pos = 0x14 + subChunkSize;
const int kNumSubChunksTests = 10;
// Do we need to scan more than 3 sub-chunks?
for (int i = 0; i < kNumSubChunksTests; i++)
{
if (pos + 8 > size)
return False;
subChunkSize = GetUi32(buf + pos + 4);
if (GetUi32(buf + pos) == WAV_SUBCHUNK_data)
{
unsigned delta = numChannels * (bitsPerSample >> 3);
if (delta >= 256)
return False;
filterMode->Id = k_Delta;
filterMode->Delta = delta;
return True;
}
if (subChunkSize > (1 << 16))
return False;
pos += subChunkSize + 8;
}
return False;
}
static Bool ParseFile(const Byte *buf, size_t size, CFilterMode *filterMode)
{
filterMode->Id = 0;
filterMode->Delta = 0;
if (Parse_EXE(buf, size, filterMode)) return True;
if (Parse_ELF(buf, size, filterMode)) return True;
if (Parse_MACH(buf, size, filterMode)) return True;
return Parse_WAV(buf, size, filterMode);
}
struct CFilterMode2: public CFilterMode
{
bool Encrypted;
unsigned GroupIndex;
CFilterMode2(): Encrypted(false) {}
int Compare(const CFilterMode2 &m) const
{
if (!Encrypted)
{
if (m.Encrypted)
return -1;
}
else if (!m.Encrypted)
return 1;
if (Id < m.Id) return -1;
if (Id > m.Id) return 1;
if (Delta < m.Delta) return -1;
if (Delta > m.Delta) return 1;
return 0;
}
bool operator ==(const CFilterMode2 &m) const
{
return Id == m.Id && Delta == m.Delta && Encrypted == m.Encrypted;
}
};
static unsigned GetGroup(CRecordVector<CFilterMode2> &filters, const CFilterMode2 &m)
{
unsigned i;
for (i = 0; i < filters.Size(); i++)
{
const CFilterMode2 &m2 = filters[i];
if (m == m2)
return i;
/*
if (m.Encrypted != m2.Encrypted)
{
if (!m.Encrypted)
break;
continue;
}
if (m.Id < m2.Id) break;
if (m.Id != m2.Id) continue;
if (m.Delta < m2.Delta) break;
if (m.Delta != m2.Delta) continue;
*/
}
// filters.Insert(i, m);
// return i;
return filters.Add(m);
}
static inline bool Is86Filter(CMethodId m)
{
return (m == k_BCJ || m == k_BCJ2);
}
static inline bool IsExeFilter(CMethodId m)
{
switch (m)
{
case k_BCJ:
case k_BCJ2:
case k_ARM:
case k_ARMT:
case k_PPC:
case k_SPARC:
case k_IA64:
return true;
}
return false;
}
static unsigned Get_FilterGroup_for_Folder(
CRecordVector<CFilterMode2> &filters, const CFolderEx &f, bool extractFilter)
{
CFilterMode2 m;
m.Id = 0;
m.Delta = 0;
m.Encrypted = f.IsEncrypted();
if (extractFilter)
{
const CCoderInfo &coder = f.Coders[f.UnpackCoder];
if (coder.MethodID == k_Delta)
{
if (coder.Props.Size() == 1)
{
m.Delta = (unsigned)coder.Props[0] + 1;
m.Id = k_Delta;
}
}
else if (IsExeFilter(coder.MethodID))
{
m.Id = (UInt32)coder.MethodID;
if (m.Id == k_BCJ2)
m.Id = k_BCJ;
m.SetDelta();
}
}
return GetGroup(filters, m);
}
static HRESULT WriteRange(IInStream *inStream, ISequentialOutStream *outStream,
UInt64 position, UInt64 size, ICompressProgressInfo *progress)
{
RINOK(inStream->Seek(position, STREAM_SEEK_SET, 0));
CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
CMyComPtr<CLimitedSequentialInStream> inStreamLimited(streamSpec);
streamSpec->SetStream(inStream);
streamSpec->Init(size);
NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder;
CMyComPtr<ICompressCoder> copyCoder = copyCoderSpec;
RINOK(copyCoder->Code(inStreamLimited, outStream, NULL, NULL, progress));
return (copyCoderSpec->TotalSize == size ? S_OK : E_FAIL);
}
/*
unsigned CUpdateItem::GetExtensionPos() const
{
int slashPos = Name.ReverseFind_PathSepar();
int dotPos = Name.ReverseFind_Dot();
if (dotPos <= slashPos)
return Name.Len();
return dotPos + 1;
}
UString CUpdateItem::GetExtension() const
{
return Name.Ptr(GetExtensionPos());
}
*/
#define RINOZ(x) { int __tt = (x); if (__tt != 0) return __tt; }
#define RINOZ_COMP(a, b) RINOZ(MyCompare(a, b))
/*
static int CompareBuffers(const CByteBuffer &a1, const CByteBuffer &a2)
{
size_t c1 = a1.GetCapacity();
size_t c2 = a2.GetCapacity();
RINOZ_COMP(c1, c2);
for (size_t i = 0; i < c1; i++)
RINOZ_COMP(a1[i], a2[i]);
return 0;
}
static int CompareCoders(const CCoderInfo &c1, const CCoderInfo &c2)
{
RINOZ_COMP(c1.NumInStreams, c2.NumInStreams);
RINOZ_COMP(c1.NumOutStreams, c2.NumOutStreams);
RINOZ_COMP(c1.MethodID, c2.MethodID);
return CompareBuffers(c1.Props, c2.Props);
}
static int CompareBonds(const CBond &b1, const CBond &b2)
{
RINOZ_COMP(b1.InIndex, b2.InIndex);
return MyCompare(b1.OutIndex, b2.OutIndex);
}
static int CompareFolders(const CFolder &f1, const CFolder &f2)
{
int s1 = f1.Coders.Size();
int s2 = f2.Coders.Size();
RINOZ_COMP(s1, s2);
int i;
for (i = 0; i < s1; i++)
RINOZ(CompareCoders(f1.Coders[i], f2.Coders[i]));
s1 = f1.Bonds.Size();
s2 = f2.Bonds.Size();
RINOZ_COMP(s1, s2);
for (i = 0; i < s1; i++)
RINOZ(CompareBonds(f1.Bonds[i], f2.Bonds[i]));
return 0;
}
*/
/*
static int CompareFiles(const CFileItem &f1, const CFileItem &f2)
{
return CompareFileNames(f1.Name, f2.Name);
}
*/
struct CFolderRepack
{
unsigned FolderIndex;
CNum NumCopyFiles;
};
/*
static int CompareFolderRepacks(const CFolderRepack *p1, const CFolderRepack *p2, void *)
{
int i1 = p1->FolderIndex;
int i2 = p2->FolderIndex;
// In that version we don't want to parse folders here, so we don't compare folders
// probably it must be improved in future
// const CDbEx &db = *(const CDbEx *)param;
// RINOZ(CompareFolders(
// db.Folders[i1],
// db.Folders[i2]));
return MyCompare(i1, i2);
// RINOZ_COMP(
// db.NumUnpackStreamsVector[i1],
// db.NumUnpackStreamsVector[i2]);
// if (db.NumUnpackStreamsVector[i1] == 0)
// return 0;
// return CompareFiles(
// db.Files[db.FolderStartFileIndex[i1]],
// db.Files[db.FolderStartFileIndex[i2]]);
}
*/
/*
we sort empty files and dirs in such order:
- Dir.NonAnti (name sorted)
- File.NonAnti (name sorted)
- File.Anti (name sorted)
- Dir.Anti (reverse name sorted)
*/
static int CompareEmptyItems(const unsigned *p1, const unsigned *p2, void *param)
{
const CObjectVector<CUpdateItem> &updateItems = *(const CObjectVector<CUpdateItem> *)param;
const CUpdateItem &u1 = updateItems[*p1];
const CUpdateItem &u2 = updateItems[*p2];
// NonAnti < Anti
if (u1.IsAnti != u2.IsAnti)
return (u1.IsAnti ? 1 : -1);
if (u1.IsDir != u2.IsDir)
{
// Dir.NonAnti < File < Dir.Anti
if (u1.IsDir)
return (u1.IsAnti ? 1 : -1);
return (u2.IsAnti ? -1 : 1);
}
int n = CompareFileNames(u1.Name, u2.Name);
return (u1.IsDir && u1.IsAnti) ? -n : n;
}
static const char *g_Exts =
" 7z xz lzma ace arc arj bz tbz bz2 tbz2 cab deb gz tgz ha lha lzh lzo lzx pak rar rpm sit zoo"
" zip jar ear war msi"
" 3gp avi mov mpeg mpg mpe wmv"
" aac ape fla flac la mp3 m4a mp4 ofr ogg pac ra rm rka shn swa tta wv wma wav"
" swf"
" chm hxi hxs"
" gif jpeg jpg jp2 png tiff bmp ico psd psp"
" awg ps eps cgm dxf svg vrml wmf emf ai md"
" cad dwg pps key sxi"
" max 3ds"
" iso bin nrg mdf img pdi tar cpio xpi"
" vfd vhd vud vmc vsv"
" vmdk dsk nvram vmem vmsd vmsn vmss vmtm"
" inl inc idl acf asa"
" h hpp hxx c cpp cxx m mm go swift"
" rc java cs rs pas bas vb cls ctl frm dlg def"
" f77 f f90 f95"
" asm s"
" sql manifest dep"
" mak clw csproj vcproj sln dsp dsw"
" class"
" bat cmd bash sh"
" xml xsd xsl xslt hxk hxc htm html xhtml xht mht mhtml htw asp aspx css cgi jsp shtml"
" awk sed hta js json php php3 php4 php5 phptml pl pm py pyo rb tcl ts vbs"
" text txt tex ans asc srt reg ini doc docx mcw dot rtf hlp xls xlr xlt xlw ppt pdf"
" sxc sxd sxi sxg sxw stc sti stw stm odt ott odg otg odp otp ods ots odf"
" abw afp cwk lwp wpd wps wpt wrf wri"
" abf afm bdf fon mgf otf pcf pfa snf ttf"
" dbf mdb nsf ntf wdb db fdb gdb"
" exe dll ocx vbx sfx sys tlb awx com obj lib out o so"
" pdb pch idb ncb opt";
static unsigned GetExtIndex(const char *ext)
{
unsigned extIndex = 1;
const char *p = g_Exts;
for (;;)
{
char c = *p++;
if (c == 0)
return extIndex;
if (c == ' ')
continue;
unsigned pos = 0;
for (;;)
{
char c2 = ext[pos++];
if (c2 == 0 && (c == 0 || c == ' '))
return extIndex;
if (c != c2)
break;
c = *p++;
}
extIndex++;
for (;;)
{
if (c == 0)
return extIndex;
if (c == ' ')
break;
c = *p++;
}
}
}
struct CRefItem
{
const CUpdateItem *UpdateItem;
UInt32 Index;
unsigned ExtensionPos;
unsigned NamePos;
unsigned ExtensionIndex;
CRefItem() {};
CRefItem(UInt32 index, const CUpdateItem &ui, bool sortByType):
UpdateItem(&ui),
Index(index),
ExtensionPos(0),
NamePos(0),
ExtensionIndex(0)
{
if (sortByType)
{
int slashPos = ui.Name.ReverseFind_PathSepar();
NamePos = slashPos + 1;
int dotPos = ui.Name.ReverseFind_Dot();
if (dotPos <= slashPos)
ExtensionPos = ui.Name.Len();
else
{
ExtensionPos = dotPos + 1;
if (ExtensionPos != ui.Name.Len())
{
AString s;
for (unsigned pos = ExtensionPos;; pos++)
{
wchar_t c = ui.Name[pos];
if (c >= 0x80)
break;
if (c == 0)
{
ExtensionIndex = GetExtIndex(s);
break;
}
s += (char)MyCharLower_Ascii((char)c);
}
}
}
}
}
};
struct CSortParam
{
// const CObjectVector<CTreeFolder> *TreeFolders;
bool SortByType;
};
/*
we sort files in such order:
- Dir.NonAnti (name sorted)
- alt streams
- Dirs
- Dir.Anti (reverse name sorted)
*/
static int CompareUpdateItems(const CRefItem *p1, const CRefItem *p2, void *param)
{
const CRefItem &a1 = *p1;
const CRefItem &a2 = *p2;
const CUpdateItem &u1 = *a1.UpdateItem;
const CUpdateItem &u2 = *a2.UpdateItem;
/*
if (u1.IsAltStream != u2.IsAltStream)
return u1.IsAltStream ? 1 : -1;
*/
// Actually there are no dirs that time. They were stored in other steps
// So that code is unused?
if (u1.IsDir != u2.IsDir)
return u1.IsDir ? 1 : -1;
if (u1.IsDir)
{
if (u1.IsAnti != u2.IsAnti)
return (u1.IsAnti ? 1 : -1);
int n = CompareFileNames(u1.Name, u2.Name);
return -n;
}
// bool sortByType = *(bool *)param;
const CSortParam *sortParam = (const CSortParam *)param;
bool sortByType = sortParam->SortByType;
if (sortByType)
{
RINOZ_COMP(a1.ExtensionIndex, a2.ExtensionIndex);
RINOZ(CompareFileNames(u1.Name.Ptr(a1.ExtensionPos), u2.Name.Ptr(a2.ExtensionPos)));
RINOZ(CompareFileNames(u1.Name.Ptr(a1.NamePos), u2.Name.Ptr(a2.NamePos)));
if (!u1.MTimeDefined && u2.MTimeDefined) return 1;
if (u1.MTimeDefined && !u2.MTimeDefined) return -1;
if (u1.MTimeDefined && u2.MTimeDefined) RINOZ_COMP(u1.MTime, u2.MTime);
RINOZ_COMP(u1.Size, u2.Size);
}
/*
int par1 = a1.UpdateItem->ParentFolderIndex;
int par2 = a2.UpdateItem->ParentFolderIndex;
const CTreeFolder &tf1 = (*sortParam->TreeFolders)[par1];
const CTreeFolder &tf2 = (*sortParam->TreeFolders)[par2];
int b1 = tf1.SortIndex, e1 = tf1.SortIndexEnd;
int b2 = tf2.SortIndex, e2 = tf2.SortIndexEnd;
if (b1 < b2)
{
if (e1 <= b2)
return -1;
// p2 in p1
int par = par2;
for (;;)
{
const CTreeFolder &tf = (*sortParam->TreeFolders)[par];
par = tf.Parent;
if (par == par1)
{
RINOZ(CompareFileNames(u1.Name, tf.Name));
break;
}
}
}
else if (b2 < b1)
{
if (e2 <= b1)
return 1;
// p1 in p2
int par = par1;
for (;;)
{
const CTreeFolder &tf = (*sortParam->TreeFolders)[par];
par = tf.Parent;
if (par == par2)
{
RINOZ(CompareFileNames(tf.Name, u2.Name));
break;
}
}
}
*/
// RINOZ_COMP(a1.UpdateItem->ParentSortIndex, a2.UpdateItem->ParentSortIndex);
RINOK(CompareFileNames(u1.Name, u2.Name));
RINOZ_COMP(a1.UpdateItem->IndexInClient, a2.UpdateItem->IndexInClient);
RINOZ_COMP(a1.UpdateItem->IndexInArchive, a2.UpdateItem->IndexInArchive);
return 0;
}
struct CSolidGroup
{
CRecordVector<UInt32> Indices;
CRecordVector<CFolderRepack> folderRefs;
};
static const char * const g_ExeExts[] =
{
"dll"
, "exe"
, "ocx"
, "sfx"
, "sys"
};
static bool IsExeExt(const wchar_t *ext)
{
for (unsigned i = 0; i < ARRAY_SIZE(g_ExeExts); i++)
if (StringsAreEqualNoCase_Ascii(ext, g_ExeExts[i]))
return true;
return false;
}
#ifndef _WIN32
static bool IsExeFile(const CUpdateItem &ui)
{
int dotPos = ui.Name.ReverseFind(L'.');
if (dotPos >= 0)
if (IsExeExt(ui.Name.Ptr(dotPos + 1)) ) return true;
if (ui.Attrib & FILE_ATTRIBUTE_UNIX_EXTENSION) {
unsigned short st_mode = ui.Attrib >> 16;
if ((st_mode & 00111) && (ui.Size >= 2048))
{
// file has the execution flag and it's big enought
// try to find if the file is a script
NWindows::NFile::NIO::CInFile file;
if (file.Open(ui.Name))
{
char buffer[2048];
UINT32 processedSize;
if (file.Read(buffer,sizeof(buffer),processedSize))
{
for(UInt32 i = 0; i < processedSize ; i++)
{
if (buffer[i] == 0)
{
return true; // this file is not a text (ascii, utf8, ...) !
}
}
}
}
}
}
return false;
}
#endif
struct CAnalysis
{
CMyComPtr<IArchiveUpdateCallbackFile> Callback;
CByteBuffer Buffer;
bool ParseWav;
bool ParseExe;
bool ParseAll;
CAnalysis():
ParseWav(true),
ParseExe(false),
ParseAll(false)
{}
HRESULT GetFilterGroup(UInt32 index, const CUpdateItem &ui, CFilterMode &filterMode);
};
static const size_t kAnalysisBufSize = 1 << 14;
HRESULT CAnalysis::GetFilterGroup(UInt32 index, const CUpdateItem &ui, CFilterMode &filterMode)
{
filterMode.Id = 0;
filterMode.Delta = 0;
CFilterMode filterModeTemp = filterMode;
int slashPos = ui.Name.ReverseFind_PathSepar();
int dotPos = ui.Name.ReverseFind_Dot();
// if (dotPos > slashPos)
{
bool needReadFile = ParseAll;
bool probablyIsSameIsa = false;
if (!needReadFile || !Callback)
{
const wchar_t *ext;
if (dotPos > slashPos)
ext = ui.Name.Ptr(dotPos + 1);
else
ext = ui.Name.RightPtr(0);
// p7zip uses the trick to store posix attributes in high 16 bits
if (ui.Attrib & 0x8000)
{
unsigned st_mode = ui.Attrib >> 16;
// st_mode = 00111;
if ((st_mode & 00111) && (ui.Size >= 2048))
{
#ifndef _WIN32
probablyIsSameIsa = true;
#endif
needReadFile = true;
}
}
#ifdef _WIN32
if (IsExeExt(ext))
#else
if (IsExeFile(ui))
#endif
{
needReadFile = true;
#ifdef _WIN32
probablyIsSameIsa = true;
needReadFile = ParseExe;
#endif
}
else if (StringsAreEqualNoCase_Ascii(ext, "wav"))
{
needReadFile = ParseWav;
}
/*
else if (!needReadFile && ParseUnixExt)
{
if (StringsAreEqualNoCase_Ascii(ext, "so")
|| StringsAreEqualNoCase_Ascii(ext, ""))
needReadFile = true;
}
*/
}
if (needReadFile && Callback)
{
if (Buffer.Size() != kAnalysisBufSize)
{
Buffer.Alloc(kAnalysisBufSize);
}
{
CMyComPtr<ISequentialInStream> stream;
HRESULT result = Callback->GetStream2(index, &stream, NUpdateNotifyOp::kAnalyze);
if (result == S_OK && stream)
{
size_t size = kAnalysisBufSize;
result = ReadStream(stream, Buffer, &size);
stream.Release();
// RINOK(Callback->SetOperationResult2(index, NUpdate::NOperationResult::kOK));
if (result == S_OK)
{
Bool parseRes = ParseFile(Buffer, size, &filterModeTemp);
if (parseRes && filterModeTemp.Delta == 0)
{
filterModeTemp.SetDelta();
if (filterModeTemp.Delta != 0 && filterModeTemp.Id != k_Delta)
{
if (ui.Size % filterModeTemp.Delta != 0)
{
parseRes = false;
}
}
}
if (!parseRes)
{
filterModeTemp.Id = 0;
filterModeTemp.Delta = 0;
}
}
}
}
}
else if ((needReadFile && !Callback) || probablyIsSameIsa)
{
#ifdef MY_CPU_X86_OR_AMD64
if (probablyIsSameIsa)
filterModeTemp.Id = k_X86;
#endif
}
}
filterMode = filterModeTemp;
return S_OK;
}
static inline void GetMethodFull(UInt64 methodID, UInt32 numStreams, CMethodFull &m)
{
m.Id = methodID;
m.NumStreams = numStreams;
}
static HRESULT AddBondForFilter(CCompressionMethodMode &mode)
{
for (unsigned c = 1; c < mode.Methods.Size(); c++)
{
if (!mode.IsThereBond_to_Coder(c))
{
CBond2 bond;
bond.OutCoder = 0;
bond.OutStream = 0;
bond.InCoder = c;
mode.Bonds.Add(bond);
return S_OK;
}
}
return E_INVALIDARG;
}
static HRESULT AddFilterBond(CCompressionMethodMode &mode)
{
if (!mode.Bonds.IsEmpty())
return AddBondForFilter(mode);