-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy path7zIn.cpp
More file actions
1764 lines (1531 loc) · 42.6 KB
/
7zIn.cpp
File metadata and controls
1764 lines (1531 loc) · 42.6 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
// 7zIn.cpp
#include "StdAfx.h"
#ifdef _WIN32
#include <wchar.h>
#else
#include <ctype.h>
#endif
#include "../../../../C/7zCrc.h"
#include "../../../../C/CpuArch.h"
#include "../../../Common/MyBuffer2.h"
// #include "../../../Common/UTFConvert.h"
#include "../../Common/StreamObjects.h"
#include "../../Common/StreamUtils.h"
#include "7zDecode.h"
#include "7zIn.h"
#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
#define Get64(p) GetUi64(p)
// define FORMAT_7Z_RECOVERY if you want to recover multivolume archives with empty StartHeader
#ifndef Z7_SFX
#define FORMAT_7Z_RECOVERY
#endif
using namespace NWindows;
using namespace NCOM;
unsigned BoolVector_CountSum(const CBoolVector &v);
Z7_NO_INLINE
unsigned BoolVector_CountSum(const CBoolVector &v)
{
unsigned sum = 0;
const unsigned size = v.Size();
if (size)
{
const bool *p = v.ConstData();
const bool * const lim = p + size;
do
if (*p)
sum++;
while (++p != lim);
}
return sum;
}
static inline bool BoolVector_Item_IsValidAndTrue(const CBoolVector &v, unsigned i)
{
return i < v.Size() ? v[i] : false;
}
Z7_NO_INLINE
static void BoolVector_Fill_False(CBoolVector &v, unsigned size)
{
v.ClearAndSetSize(size);
bool *p = v.NonConstData();
for (unsigned i = 0; i < size; i++)
p[i] = false;
}
namespace NArchive {
namespace N7z {
#define k_Scan_NumCoders_MAX 64
#define k_Scan_NumCodersStreams_in_Folder_MAX 64
class CInArchiveException {};
class CUnsupportedFeatureException: public CInArchiveException {};
Z7_ATTR_NORETURN
static void ThrowException() { throw CInArchiveException(); }
Z7_ATTR_NORETURN
static inline void ThrowEndOfData() { ThrowException(); }
Z7_ATTR_NORETURN
static inline void ThrowUnsupported() { throw CUnsupportedFeatureException(); }
Z7_ATTR_NORETURN
static inline void ThrowIncorrect() { ThrowException(); }
class CStreamSwitch
{
CInArchive *_archive;
bool _needRemove;
bool _needUpdatePos;
public:
CStreamSwitch(): _needRemove(false), _needUpdatePos(false) {}
~CStreamSwitch() { Remove(); }
void Remove();
void Set(CInArchive *archive, const Byte *data, size_t size, bool needUpdatePos);
void Set(CInArchive *archive, const CByteBuffer &byteBuffer);
void Set(CInArchive *archive, const CObjectVector<CByteBuffer> *dataVector);
};
void CStreamSwitch::Remove()
{
if (_needRemove)
{
if (_archive->_inByteBack->GetRem() != 0)
_archive->ThereIsHeaderError = true;
_archive->DeleteByteStream(_needUpdatePos);
_needRemove = false;
}
}
void CStreamSwitch::Set(CInArchive *archive, const Byte *data, size_t size, bool needUpdatePos)
{
Remove();
_archive = archive;
_archive->AddByteStream(data, size);
_needRemove = true;
_needUpdatePos = needUpdatePos;
}
void CStreamSwitch::Set(CInArchive *archive, const CByteBuffer &byteBuffer)
{
Set(archive, byteBuffer, byteBuffer.Size(), false);
}
void CStreamSwitch::Set(CInArchive *archive, const CObjectVector<CByteBuffer> *dataVector)
{
Remove();
const Byte external = archive->ReadByte();
if (external != 0)
{
if (!dataVector)
ThrowIncorrect();
const CNum dataIndex = archive->ReadNum();
if (dataIndex >= dataVector->Size())
ThrowIncorrect();
Set(archive, (*dataVector)[dataIndex]);
}
}
void CInArchive::AddByteStream(const Byte *buf, size_t size)
{
if (_numInByteBufs == kNumBufLevelsMax)
ThrowIncorrect();
_inByteBack = &_inByteVector[_numInByteBufs++];
_inByteBack->Init(buf, size);
}
Byte CInByte2::ReadByte()
{
if (_pos >= _size)
ThrowEndOfData();
return _buffer[_pos++];
}
void CInByte2::ReadBytes(Byte *data, size_t size)
{
if (size == 0)
return;
if (size > _size - _pos)
ThrowEndOfData();
memcpy(data, _buffer + _pos, size);
_pos += size;
}
void CInByte2::SkipData(UInt64 size)
{
if (size > _size - _pos)
ThrowEndOfData();
_pos += (size_t)size;
}
void CInByte2::SkipData()
{
SkipData(ReadNumber());
}
static UInt64 ReadNumberSpec(const Byte *p, size_t size, size_t &processed)
{
if (size == 0)
{
processed = 0;
return 0;
}
const unsigned b = *p++;
size--;
if ((b & 0x80) == 0)
{
processed = 1;
return b;
}
if (size == 0)
{
processed = 0;
return 0;
}
UInt64 value = (UInt64)*p;
p++;
size--;
for (unsigned i = 1; i < 8; i++)
{
const unsigned mask = (unsigned)0x80 >> i;
if ((b & mask) == 0)
{
const UInt64 high = b & (mask - 1);
value |= (high << (i * 8));
processed = i + 1;
return value;
}
if (size == 0)
{
processed = 0;
return 0;
}
value |= ((UInt64)*p << (i * 8));
p++;
size--;
}
processed = 9;
return value;
}
UInt64 CInByte2::ReadNumber()
{
size_t processed;
const UInt64 res = ReadNumberSpec(_buffer + _pos, _size - _pos, processed);
if (processed == 0)
ThrowEndOfData();
_pos += processed;
return res;
}
CNum CInByte2::ReadNum()
{
/*
if (_pos < _size)
{
Byte val = _buffer[_pos];
if ((unsigned)val < 0x80)
{
_pos++;
return (unsigned)val;
}
}
*/
const UInt64 value = ReadNumber();
if (value > kNumMax)
ThrowUnsupported();
return (CNum)value;
}
UInt32 CInByte2::ReadUInt32()
{
if (_pos + 4 > _size)
ThrowEndOfData();
const UInt32 res = Get32(_buffer + _pos);
_pos += 4;
return res;
}
UInt64 CInByte2::ReadUInt64()
{
if (_pos + 8 > _size)
ThrowEndOfData();
const UInt64 res = Get64(_buffer + _pos);
_pos += 8;
return res;
}
#define Y0 '7'
#define Y1 'z'
#define Y2 0xBC
#define Y3 0xAF
#define Y4 0x27
#define Y5 0x1C
#define IS_SIGNATURE(p)( \
(p)[2] == Y2 && \
(p)[3] == Y3 && \
(p)[5] == Y5 && \
(p)[4] == Y4 && \
(p)[1] == Y1 && \
(p)[0] == Y0)
/* FindSignature_10() is allowed to access data up to and including &limit[9].
limit[10] access is not allowed.
return:
(return_ptr < limit) : signature was found at (return_ptr)
(return_ptr >= limit) : limit was reached or crossed. So no signature found before limit
*/
Z7_NO_INLINE
static const Byte *FindSignature_10(const Byte *p, const Byte *limit)
{
for (;;)
{
for (;;)
{
if (p >= limit)
return limit;
const Byte b = p[5];
p += 6;
if (b == Y0) { break; }
if (b == Y1) { p -= 1; break; }
if (b == Y2) { p -= 2; break; }
if (b == Y3) { p -= 3; break; }
if (b == Y4) { p -= 4; break; }
if (b == Y5) { p -= 5; break; }
}
if (IS_SIGNATURE(p - 1))
return p - 1;
}
}
static inline bool TestStartCrc(const Byte *p)
{
return CrcCalc(p + 12, 20) == Get32(p + 8);
}
static inline bool TestSignature2(const Byte *p)
{
if (!IS_SIGNATURE(p))
return false;
#ifdef FORMAT_7Z_RECOVERY
if (TestStartCrc(p))
return true;
for (unsigned i = 8; i < kHeaderSize; i++)
if (p[i] != 0)
return false;
return (p[6] != 0 || p[7] != 0);
#else
return TestStartCrc(p);
#endif
}
HRESULT CInArchive::FindAndReadSignature(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
RINOK(ReadStream_FALSE(stream, _header, kHeaderSize))
if (TestSignature2(_header))
return S_OK;
if (searchHeaderSizeLimit && *searchHeaderSizeLimit == 0)
return S_FALSE;
const UInt32 kBufSize = (1 << 15) + kHeaderSize; // must be > (kHeaderSize * 2)
CAlignedBuffer1 buf(kBufSize);
memcpy(buf, _header, kHeaderSize);
UInt64 offset = 0;
for (;;)
{
UInt32 readSize =
(offset == 0) ?
kBufSize - kHeaderSize - kHeaderSize :
kBufSize - kHeaderSize;
if (searchHeaderSizeLimit)
{
const UInt64 rem = *searchHeaderSizeLimit - offset;
if (readSize > rem)
readSize = (UInt32)rem;
if (readSize == 0)
return S_FALSE;
}
UInt32 processed = 0;
RINOK(stream->Read(buf + kHeaderSize, readSize, &processed))
if (processed == 0)
return S_FALSE;
/* &buf[0] was already tested for signature before.
So first search here will be for &buf[1] */
for (UInt32 pos = 0;;)
{
const Byte *p = buf + pos + 1;
const Byte *lim = buf + processed + 1;
/* we have (kHeaderSize - 1 = 31) filled bytes starting from (lim),
and it's safe to access just 10 bytes in that reserved area */
p = FindSignature_10(p, lim);
if (p >= lim)
break;
pos = (UInt32)(p - buf);
if (TestStartCrc(p))
{
memcpy(_header, p, kHeaderSize);
_arhiveBeginStreamPosition += offset + pos;
return InStream_SeekSet(stream, _arhiveBeginStreamPosition + kHeaderSize);
}
}
offset += processed;
memmove(buf, buf + processed, kHeaderSize);
}
}
// S_FALSE means that file is not archive
HRESULT CInArchive::Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
HeadersSize = 0;
Close();
RINOK(InStream_GetPos_GetSize(stream, _arhiveBeginStreamPosition, _fileEndPosition))
RINOK(FindAndReadSignature(stream, searchHeaderSizeLimit))
_stream = stream;
return S_OK;
}
void CInArchive::Close()
{
_numInByteBufs = 0;
_stream.Release();
ThereIsHeaderError = false;
}
void CInArchive::ReadArchiveProperties(CInArchiveInfo & /* archiveInfo */)
{
for (;;)
{
if (ReadID() == NID::kEnd)
break;
SkipData();
}
}
// CFolder &folder can be non empty. So we must set all fields
void CInByte2::ParseFolder(CFolder &folder)
{
const UInt32 numCoders = ReadNum();
if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX)
ThrowUnsupported();
folder.Coders.SetSize(numCoders);
UInt32 numInStreams = 0;
UInt32 i;
for (i = 0; i < numCoders; i++)
{
CCoderInfo &coder = folder.Coders[i];
{
const Byte mainByte = ReadByte();
if ((mainByte & 0xC0) != 0)
ThrowUnsupported();
const unsigned idSize = (mainByte & 0xF);
if (idSize > 8 || idSize > GetRem())
ThrowUnsupported();
const Byte *longID = GetPtr();
UInt64 id = 0;
for (unsigned j = 0; j < idSize; j++)
id = ((id << 8) | longID[j]);
SkipDataNoCheck(idSize);
coder.MethodID = id;
if ((mainByte & 0x10) != 0)
{
coder.NumStreams = ReadNum();
// if (coder.NumStreams > k_Scan_NumCodersStreams_in_Folder_MAX) ThrowUnsupported();
/* numOutStreams = */ ReadNum();
// if (ReadNum() != 1) // numOutStreams ThrowUnsupported();
}
else
{
coder.NumStreams = 1;
}
if ((mainByte & 0x20) != 0)
{
const CNum propsSize = ReadNum();
coder.Props.Alloc((size_t)propsSize);
ReadBytes((Byte *)coder.Props, (size_t)propsSize);
}
else
coder.Props.Free();
}
numInStreams += coder.NumStreams;
}
const UInt32 numBonds = numCoders - 1;
folder.Bonds.SetSize(numBonds);
for (i = 0; i < numBonds; i++)
{
CBond &bp = folder.Bonds[i];
bp.PackIndex = ReadNum();
bp.UnpackIndex = ReadNum();
}
if (numInStreams < numBonds)
ThrowUnsupported();
const UInt32 numPackStreams = numInStreams - numBonds;
folder.PackStreams.SetSize(numPackStreams);
if (numPackStreams == 1)
{
for (i = 0; i < numInStreams; i++)
if (folder.FindBond_for_PackStream(i) < 0)
{
folder.PackStreams[0] = i;
break;
}
if (i == numInStreams)
ThrowUnsupported();
}
else
for (i = 0; i < numPackStreams; i++)
folder.PackStreams[i] = ReadNum();
}
void CFolders::ParseFolderInfo(unsigned folderIndex, CFolder &folder) const
{
const size_t startPos = FoCodersDataOffset[folderIndex];
CInByte2 inByte;
inByte.Init(CodersData.ConstData() + startPos, FoCodersDataOffset[folderIndex + 1] - startPos);
inByte.ParseFolder(folder);
if (inByte.GetRem() != 0)
throw 20120424;
}
void CDatabase::GetPath(unsigned index, UString &path) const
{
path.Empty();
if (!NameOffsets || !NamesBuf)
return;
const size_t offset = NameOffsets[index];
const size_t size = NameOffsets[index + 1] - offset;
if (size >= (1 << 28))
return;
wchar_t *s = path.GetBuf((unsigned)size - 1);
const Byte *p = ((const Byte *)NamesBuf + offset * 2);
#if defined(_WIN32) && defined(MY_CPU_LE)
wmemcpy(s, (const wchar_t *)(const void *)p, size);
#else
for (size_t i = 0; i < size; i++)
{
*s = Get16(p);
p += 2;
s++;
}
#endif
path.ReleaseBuf_SetLen((unsigned)size - 1);
}
HRESULT CDatabase::GetPath_Prop(unsigned index, PROPVARIANT *path) const throw()
{
PropVariant_Clear(path);
if (!NameOffsets || !NamesBuf)
return S_OK;
const size_t offset = NameOffsets[index];
const size_t size = NameOffsets[index + 1] - offset;
if (size >= (1 << 14))
return S_OK;
// (size) includes null terminator
/*
#if WCHAR_MAX > 0xffff
const Byte *p = ((const Byte *)NamesBuf + offset * 2);
size = Utf16LE__Get_Num_WCHARs(p, size - 1);
// (size) doesn't include null terminator
RINOK(PropVarEm_Alloc_Bstr(path, (unsigned)size));
wchar_t *s = path->bstrVal;
wchar_t *sEnd = Utf16LE__To_WCHARs_Sep(p, size, s);
*sEnd = 0;
if (s + size != sEnd) return E_FAIL;
#else
*/
RINOK(PropVarEm_Alloc_Bstr(path, (unsigned)size - 1))
wchar_t *s = path->bstrVal;
const Byte *p = ((const Byte *)NamesBuf + offset * 2);
// Utf16LE__To_WCHARs_Sep(p, size, s);
for (size_t i = 0; i < size; i++)
{
wchar_t c = Get16(p);
p += 2;
#if WCHAR_PATH_SEPARATOR != L'/'
if (c == L'/')
c = WCHAR_PATH_SEPARATOR;
else if (c == L'\\')
c = WCHAR_IN_FILE_NAME_BACKSLASH_REPLACEMENT; // WSL scheme
#endif
*s++ = c;
}
// #endif
return S_OK;
/*
unsigned cur = index;
unsigned size = 0;
for (int i = 0;; i++)
{
size_t len = NameOffsets[cur + 1] - NameOffsets[cur];
size += (unsigned)len;
if (i > 256 || len > (1 << 14) || size > (1 << 14))
return PropVarEm_Set_Str(path, "[TOO-LONG]");
cur = Files[cur].Parent;
if (cur < 0)
break;
}
size--;
RINOK(PropVarEm_Alloc_Bstr(path, size));
wchar_t *s = path->bstrVal;
s += size;
*s = 0;
cur = index;
for (;;)
{
unsigned len = (unsigned)(NameOffsets[cur + 1] - NameOffsets[cur] - 1);
const Byte *p = (const Byte *)NamesBuf + (NameOffsets[cur + 1] * 2) - 2;
for (; len != 0; len--)
{
p -= 2;
--s;
wchar_t c = Get16(p);
if (c == '/')
c = WCHAR_PATH_SEPARATOR;
*s = c;
}
const CFileItem &file = Files[cur];
cur = file.Parent;
if (cur < 0)
return S_OK;
*(--s) = (file.IsAltStream ? ':' : WCHAR_PATH_SEPARATOR);
}
*/
}
void CInArchive::WaitId(UInt64 id)
{
for (;;)
{
const UInt64 type = ReadID();
if (type == id)
return;
if (type == NID::kEnd)
ThrowIncorrect();
SkipData();
}
}
void CInArchive::Read_UInt32_Vector(CUInt32DefVector &v)
{
const unsigned numItems = v.Defs.Size();
v.Vals.ClearAndSetSize(numItems);
UInt32 *p = &v.Vals[0];
const bool *defs = &v.Defs[0];
for (unsigned i = 0; i < numItems; i++)
{
UInt32 a = 0;
if (defs[i])
a = ReadUInt32();
p[i] = a;
}
}
void CInArchive::ReadHashDigests(unsigned numItems, CUInt32DefVector &crcs)
{
ReadBoolVector2(numItems, crcs.Defs);
Read_UInt32_Vector(crcs);
}
void CInArchive::ReadPackInfo(CFolders &f)
{
const CNum numPackStreams = ReadNum();
WaitId(NID::kSize);
f.PackPositions.Alloc(numPackStreams + 1);
f.NumPackStreams = numPackStreams;
UInt64 sum = 0;
for (CNum i = 0; i < numPackStreams; i++)
{
f.PackPositions[i] = sum;
const UInt64 packSize = ReadNumber();
sum += packSize;
if (sum < packSize)
ThrowIncorrect();
}
f.PackPositions[numPackStreams] = sum;
UInt64 type;
for (;;)
{
type = ReadID();
if (type == NID::kEnd)
return;
if (type == NID::kCRC)
{
CUInt32DefVector PackCRCs;
ReadHashDigests(numPackStreams, PackCRCs);
continue;
}
SkipData();
}
}
void CInArchive::ReadUnpackInfo(
const CObjectVector<CByteBuffer> *dataVector,
CFolders &folders)
{
WaitId(NID::kFolder);
const CNum numFolders = ReadNum();
CNum numCodersOutStreams = 0;
{
CStreamSwitch streamSwitch;
streamSwitch.Set(this, dataVector);
const Byte *startBufPtr = _inByteBack->GetPtr();
folders.NumFolders = numFolders;
folders.FoStartPackStreamIndex.Alloc(numFolders + 1);
folders.FoToMainUnpackSizeIndex.Alloc(numFolders);
folders.FoCodersDataOffset.Alloc(numFolders + 1);
folders.FoToCoderUnpackSizes.Alloc(numFolders + 1);
CBoolVector StreamUsed;
CBoolVector CoderUsed;
CNum packStreamIndex = 0;
CNum fo;
CInByte2 *inByte = _inByteBack;
for (fo = 0; fo < numFolders; fo++)
{
UInt32 indexOfMainStream = 0;
UInt32 numPackStreams = 0;
folders.FoCodersDataOffset[fo] = (size_t)(_inByteBack->GetPtr() - startBufPtr);
CNum numInStreams = 0;
const CNum numCoders = inByte->ReadNum();
if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX)
ThrowUnsupported();
for (CNum ci = 0; ci < numCoders; ci++)
{
const Byte mainByte = inByte->ReadByte();
if ((mainByte & 0xC0) != 0)
ThrowUnsupported();
const unsigned idSize = (mainByte & 0xF);
if (idSize > 8)
ThrowUnsupported();
if (idSize > inByte->GetRem())
ThrowEndOfData();
const Byte *longID = inByte->GetPtr();
UInt64 id = 0;
for (unsigned j = 0; j < idSize; j++)
id = ((id << 8) | longID[j]);
inByte->SkipDataNoCheck(idSize);
if (folders.ParsedMethods.IDs.Size() < 128)
folders.ParsedMethods.IDs.AddToUniqueSorted(id);
CNum coderInStreams = 1;
if ((mainByte & 0x10) != 0)
{
coderInStreams = inByte->ReadNum();
if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX)
ThrowUnsupported();
if (inByte->ReadNum() != 1)
ThrowUnsupported();
}
numInStreams += coderInStreams;
if (numInStreams > k_Scan_NumCodersStreams_in_Folder_MAX)
ThrowUnsupported();
if ((mainByte & 0x20) != 0)
{
const CNum propsSize = inByte->ReadNum();
if (propsSize > inByte->GetRem())
ThrowEndOfData();
if (id == k_LZMA2 && propsSize == 1)
{
const Byte v = *_inByteBack->GetPtr();
if (folders.ParsedMethods.Lzma2Prop < v)
folders.ParsedMethods.Lzma2Prop = v;
}
else if (id == k_LZMA && propsSize == 5)
{
const UInt32 dicSize = GetUi32(_inByteBack->GetPtr() + 1);
if (folders.ParsedMethods.LzmaDic < dicSize)
folders.ParsedMethods.LzmaDic = dicSize;
}
inByte->SkipDataNoCheck((size_t)propsSize);
}
}
if (numCoders == 1 && numInStreams == 1)
{
indexOfMainStream = 0;
numPackStreams = 1;
}
else
{
UInt32 i;
const CNum numBonds = numCoders - 1;
if (numInStreams < numBonds)
ThrowUnsupported();
BoolVector_Fill_False(StreamUsed, numInStreams);
BoolVector_Fill_False(CoderUsed, numCoders);
for (i = 0; i < numBonds; i++)
{
CNum index = ReadNum();
if (index >= numInStreams || StreamUsed[index])
ThrowUnsupported();
StreamUsed[index] = true;
index = ReadNum();
if (index >= numCoders || CoderUsed[index])
ThrowUnsupported();
CoderUsed[index] = true;
}
numPackStreams = numInStreams - numBonds;
if (numPackStreams != 1)
for (i = 0; i < numPackStreams; i++)
{
const CNum index = inByte->ReadNum(); // PackStreams
if (index >= numInStreams || StreamUsed[index])
ThrowUnsupported();
StreamUsed[index] = true;
}
for (i = 0; i < numCoders; i++)
if (!CoderUsed[i])
{
indexOfMainStream = i;
break;
}
if (i == numCoders)
ThrowUnsupported();
}
folders.FoToCoderUnpackSizes[fo] = numCodersOutStreams;
numCodersOutStreams += numCoders;
folders.FoStartPackStreamIndex[fo] = packStreamIndex;
if (numPackStreams > folders.NumPackStreams - packStreamIndex)
ThrowIncorrect();
packStreamIndex += numPackStreams;
folders.FoToMainUnpackSizeIndex[fo] = (Byte)indexOfMainStream;
}
const size_t dataSize = (size_t)(_inByteBack->GetPtr() - startBufPtr);
folders.FoToCoderUnpackSizes[fo] = numCodersOutStreams;
folders.FoStartPackStreamIndex[fo] = packStreamIndex;
folders.FoCodersDataOffset[fo] = (size_t)(_inByteBack->GetPtr() - startBufPtr);
folders.CodersData.CopyFrom(startBufPtr, dataSize);
// if (folders.NumPackStreams != packStreamIndex) ThrowUnsupported();
}
WaitId(NID::kCodersUnpackSize);
folders.CoderUnpackSizes.Alloc(numCodersOutStreams);
for (CNum i = 0; i < numCodersOutStreams; i++)
folders.CoderUnpackSizes[i] = ReadNumber();
for (;;)
{
const UInt64 type = ReadID();
if (type == NID::kEnd)
return;
if (type == NID::kCRC)
{
ReadHashDigests(numFolders, folders.FolderCRCs);
continue;
}
SkipData();
}
}
void CInArchive::ReadSubStreamsInfo(
CFolders &folders,
CRecordVector<UInt64> &unpackSizes,
CUInt32DefVector &digests)
{
folders.NumUnpackStreamsVector.Alloc(folders.NumFolders);
CNum i;
for (i = 0; i < folders.NumFolders; i++)
folders.NumUnpackStreamsVector[i] = 1;
UInt64 type;
for (;;)
{
type = ReadID();
if (type == NID::kNumUnpackStream)
{
for (i = 0; i < folders.NumFolders; i++)
folders.NumUnpackStreamsVector[i] = ReadNum();
continue;
}
if (type == NID::kCRC || type == NID::kSize || type == NID::kEnd)
break;
SkipData();
}
if (type == NID::kSize)
{
for (i = 0; i < folders.NumFolders; i++)
{
// v3.13 incorrectly worked with empty folders
// v4.07: we check that folder is empty
const CNum numSubstreams = folders.NumUnpackStreamsVector[i];
if (numSubstreams == 0)
continue;
UInt64 sum = 0;
for (CNum j = 1; j < numSubstreams; j++)
{
const UInt64 size = ReadNumber();
unpackSizes.Add(size);
sum += size;
if (sum < size)
ThrowIncorrect();
}
const UInt64 folderUnpackSize = folders.GetFolderUnpackSize(i);
if (folderUnpackSize < sum)
ThrowIncorrect();
unpackSizes.Add(folderUnpackSize - sum);
}
type = ReadID();
}
else
{
for (i = 0; i < folders.NumFolders; i++)
{
/* v9.26 - v9.29 incorrectly worked:
if (folders.NumUnpackStreamsVector[i] == 0), it threw error */
const CNum val = folders.NumUnpackStreamsVector[i];
if (val > 1)
ThrowIncorrect();
if (val == 1)
unpackSizes.Add(folders.GetFolderUnpackSize(i));
}
}
unsigned numDigests = 0;
for (i = 0; i < folders.NumFolders; i++)
{
const CNum numSubstreams = folders.NumUnpackStreamsVector[i];
if (numSubstreams != 1 || !folders.FolderCRCs.ValidAndDefined(i))
numDigests += numSubstreams;
}
for (;;)
{
if (type == NID::kEnd)
break;
if (type == NID::kCRC)
{
// CUInt32DefVector digests2;
// ReadHashDigests(numDigests, digests2);
CBoolVector digests2;
ReadBoolVector2(numDigests, digests2);
digests.ClearAndSetSize(unpackSizes.Size());
unsigned k = 0;
unsigned k2 = 0;
for (i = 0; i < folders.NumFolders; i++)
{
const CNum numSubstreams = folders.NumUnpackStreamsVector[i];
if (numSubstreams == 1 && folders.FolderCRCs.ValidAndDefined(i))