forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7zIn.c
1371 lines (1240 loc) · 37.9 KB
/
7zIn.c
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.c -- 7z Input functions
2010-10-29 : Igor Pavlov : Public domain */
#include <stdint.h>
#include <string.h>
#include "7z.h"
#include "7zCrc.h"
#include "CpuArch.h"
uint8_t k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
#define RINOM(x) { if ((x) == 0) return SZ_ERROR_MEM; }
#define NUM_FOLDER_CODERS_MAX 32
#define NUM_CODER_STREAMS_MAX 32
void SzFolder_Free(CSzFolder *p, ISzAlloc *alloc);
int SzFolder_FindBindPairForOutStream(CSzFolder *p, uint32_t outStreamIndex);
void SzCoderInfo_Init(CSzCoderInfo *p)
{
Buf_Init(&p->Props);
}
void SzCoderInfo_Free(CSzCoderInfo *p, ISzAlloc *alloc)
{
Buf_Free(&p->Props, alloc);
SzCoderInfo_Init(p);
}
void SzFolder_Init(CSzFolder *p)
{
p->Coders = 0;
p->BindPairs = 0;
p->PackStreams = 0;
p->UnpackSizes = 0;
p->NumCoders = 0;
p->NumBindPairs = 0;
p->NumPackStreams = 0;
p->UnpackCRCDefined = 0;
p->UnpackCRC = 0;
p->NumUnpackStreams = 0;
}
void SzFolder_Free(CSzFolder *p, ISzAlloc *alloc)
{
uint32_t i;
if (p->Coders)
for (i = 0; i < p->NumCoders; i++)
SzCoderInfo_Free(&p->Coders[i], alloc);
IAlloc_Free(alloc, p->Coders);
IAlloc_Free(alloc, p->BindPairs);
IAlloc_Free(alloc, p->PackStreams);
IAlloc_Free(alloc, p->UnpackSizes);
SzFolder_Init(p);
}
uint32_t SzFolder_GetNumOutStreams(CSzFolder *p)
{
uint32_t result = 0;
uint32_t i;
for (i = 0; i < p->NumCoders; i++)
result += p->Coders[i].NumOutStreams;
return result;
}
int SzFolder_FindBindPairForInStream(CSzFolder *p, uint32_t inStreamIndex)
{
uint32_t i;
for (i = 0; i < p->NumBindPairs; i++)
if (p->BindPairs[i].InIndex == inStreamIndex)
return i;
return -1;
}
int SzFolder_FindBindPairForOutStream(CSzFolder *p, uint32_t outStreamIndex)
{
uint32_t i;
for (i = 0; i < p->NumBindPairs; i++)
if (p->BindPairs[i].OutIndex == outStreamIndex)
return i;
return -1;
}
uint64_t SzFolder_GetUnpackSize(CSzFolder *p)
{
int i = (int)SzFolder_GetNumOutStreams(p);
if (i == 0)
return 0;
for (i--; i >= 0; i--)
if (SzFolder_FindBindPairForOutStream(p, i) < 0)
return p->UnpackSizes[i];
/* throw 1; */
return 0;
}
void SzFile_Init(CSzFileItem *p)
{
p->HasStream = 1;
p->IsDir = 0;
p->IsAnti = 0;
p->CrcDefined = 0;
p->MTimeDefined = 0;
}
void SzAr_Init(CSzAr *p)
{
p->PackSizes = 0;
p->PackCRCsDefined = 0;
p->PackCRCs = 0;
p->Folders = 0;
p->Files = 0;
p->NumPackStreams = 0;
p->NumFolders = 0;
p->NumFiles = 0;
}
void SzAr_Free(CSzAr *p, ISzAlloc *alloc)
{
uint32_t i;
if (p->Folders)
for (i = 0; i < p->NumFolders; i++)
SzFolder_Free(&p->Folders[i], alloc);
IAlloc_Free(alloc, p->PackSizes);
IAlloc_Free(alloc, p->PackCRCsDefined);
IAlloc_Free(alloc, p->PackCRCs);
IAlloc_Free(alloc, p->Folders);
IAlloc_Free(alloc, p->Files);
SzAr_Init(p);
}
void SzArEx_Init(CSzArEx *p)
{
SzAr_Init(&p->db);
p->FolderStartPackStreamIndex = 0;
p->PackStreamStartPositions = 0;
p->FolderStartFileIndex = 0;
p->FileIndexToFolderIndexMap = 0;
p->FileNameOffsets = 0;
Buf_Init(&p->FileNames);
}
void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc)
{
IAlloc_Free(alloc, p->FolderStartPackStreamIndex);
IAlloc_Free(alloc, p->PackStreamStartPositions);
IAlloc_Free(alloc, p->FolderStartFileIndex);
IAlloc_Free(alloc, p->FileIndexToFolderIndexMap);
IAlloc_Free(alloc, p->FileNameOffsets);
Buf_Free(&p->FileNames, alloc);
SzAr_Free(&p->db, alloc);
SzArEx_Init(p);
}
/*
uint64_t GetFolderPackStreamSize(int folderIndex, int streamIndex) const
{
return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex];
}
uint64_t GetFilePackSize(int fileIndex) const
{
int folderIndex = FileIndexToFolderIndexMap[fileIndex];
if (folderIndex >= 0)
{
const CSzFolder &folderInfo = Folders[folderIndex];
if (FolderStartFileIndex[folderIndex] == fileIndex)
return GetFolderFullPackSize(folderIndex);
}
return 0;
}
*/
#define MY_ALLOC(T, p, size, alloc) { if ((size) == 0) p = 0; else \
if ((p = (T *)IAlloc_Alloc(alloc, (size) * sizeof(T))) == 0) return SZ_ERROR_MEM; }
static SRes SzArEx_Fill(CSzArEx *p, ISzAlloc *alloc)
{
uint32_t startPos = 0;
uint64_t startPosSize = 0;
uint32_t i;
uint32_t folderIndex = 0;
uint32_t indexInFolder = 0;
MY_ALLOC(uint32_t, p->FolderStartPackStreamIndex, p->db.NumFolders, alloc);
for (i = 0; i < p->db.NumFolders; i++)
{
p->FolderStartPackStreamIndex[i] = startPos;
startPos += p->db.Folders[i].NumPackStreams;
}
MY_ALLOC(uint64_t, p->PackStreamStartPositions, p->db.NumPackStreams, alloc);
for (i = 0; i < p->db.NumPackStreams; i++)
{
p->PackStreamStartPositions[i] = startPosSize;
startPosSize += p->db.PackSizes[i];
}
MY_ALLOC(uint32_t, p->FolderStartFileIndex, p->db.NumFolders, alloc);
MY_ALLOC(uint32_t, p->FileIndexToFolderIndexMap, p->db.NumFiles, alloc);
for (i = 0; i < p->db.NumFiles; i++)
{
CSzFileItem *file = p->db.Files + i;
int emptyStream = !file->HasStream;
if (emptyStream && indexInFolder == 0)
{
p->FileIndexToFolderIndexMap[i] = (uint32_t)-1;
continue;
}
if (indexInFolder == 0)
{
/*
v3.13 incorrectly worked with empty folders
v4.07: Loop for skipping empty folders
*/
for (;;)
{
if (folderIndex >= p->db.NumFolders)
return SZ_ERROR_ARCHIVE;
p->FolderStartFileIndex[folderIndex] = i;
if (p->db.Folders[folderIndex].NumUnpackStreams != 0)
break;
folderIndex++;
}
}
p->FileIndexToFolderIndexMap[i] = folderIndex;
if (emptyStream)
continue;
indexInFolder++;
if (indexInFolder >= p->db.Folders[folderIndex].NumUnpackStreams)
{
folderIndex++;
indexInFolder = 0;
}
}
return SZ_OK;
}
uint64_t SzArEx_GetFolderStreamPos(const CSzArEx *p, uint32_t folderIndex, uint32_t indexInFolder)
{
return p->dataPos +
p->PackStreamStartPositions[p->FolderStartPackStreamIndex[folderIndex] + indexInFolder];
}
int SzArEx_GetFolderFullPackSize(const CSzArEx *p, uint32_t folderIndex, uint64_t *resSize)
{
uint32_t packStreamIndex = p->FolderStartPackStreamIndex[folderIndex];
CSzFolder *folder = p->db.Folders + folderIndex;
uint64_t size = 0;
uint32_t i;
for (i = 0; i < folder->NumPackStreams; i++)
{
uint64_t t = size + p->db.PackSizes[packStreamIndex + i];
if (t < size) /* check it */
return SZ_ERROR_FAIL;
size = t;
}
*resSize = size;
return SZ_OK;
}
static int TestSignatureCandidate(uint8_t *testuint8_ts)
{
size_t i;
for (i = 0; i < k7zSignatureSize; i++)
if (testuint8_ts[i] != k7zSignature[i])
return 0;
return 1;
}
typedef struct _CSzState
{
uint8_t *Data;
size_t Size;
}CSzData;
static SRes SzReaduint8_t(CSzData *sd, uint8_t *b)
{
if (sd->Size == 0)
return SZ_ERROR_ARCHIVE;
sd->Size--;
*b = *sd->Data++;
return SZ_OK;
}
static SRes SzReaduint8_ts(CSzData *sd, uint8_t *data, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
{
RINOK(SzReaduint8_t(sd, data + i));
}
return SZ_OK;
}
static SRes SzReaduint32_t(CSzData *sd, uint32_t *value)
{
int i;
*value = 0;
for (i = 0; i < 4; i++)
{
uint8_t b;
RINOK(SzReaduint8_t(sd, &b));
*value |= ((uint32_t)(b) << (8 * i));
}
return SZ_OK;
}
static SRes SzReadNumber(CSzData *sd, uint64_t *value)
{
uint8_t firstuint8_t;
uint8_t mask = 0x80;
int i;
RINOK(SzReaduint8_t(sd, &firstuint8_t));
*value = 0;
for (i = 0; i < 8; i++)
{
uint8_t b;
if ((firstuint8_t & mask) == 0)
{
uint64_t highPart = firstuint8_t & (mask - 1);
*value += (highPart << (8 * i));
return SZ_OK;
}
RINOK(SzReaduint8_t(sd, &b));
*value |= ((uint64_t)b << (8 * i));
mask >>= 1;
}
return SZ_OK;
}
static SRes SzReadNumber32(CSzData *sd, uint32_t *value)
{
uint64_t value64;
RINOK(SzReadNumber(sd, &value64));
if (value64 >= 0x80000000)
return SZ_ERROR_UNSUPPORTED;
if (value64 >= ((uint64_t)(1) << ((sizeof(size_t) - 1) * 8 + 2)))
return SZ_ERROR_UNSUPPORTED;
*value = (uint32_t)value64;
return SZ_OK;
}
static SRes SzReadID(CSzData *sd, uint64_t *value)
{
return SzReadNumber(sd, value);
}
static SRes SzSkeepDataSize(CSzData *sd, uint64_t size)
{
if (size > sd->Size)
return SZ_ERROR_ARCHIVE;
sd->Size -= (size_t)size;
sd->Data += (size_t)size;
return SZ_OK;
}
static SRes SzSkeepData(CSzData *sd)
{
uint64_t size;
RINOK(SzReadNumber(sd, &size));
return SzSkeepDataSize(sd, size);
}
static SRes SzReadArchiveProperties(CSzData *sd)
{
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
break;
SzSkeepData(sd);
}
return SZ_OK;
}
static SRes SzWaitAttribute(CSzData *sd, uint64_t attribute)
{
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
if (type == attribute)
return SZ_OK;
if (type == k7zIdEnd)
return SZ_ERROR_ARCHIVE;
RINOK(SzSkeepData(sd));
}
}
static SRes SzReadBoolVector(CSzData *sd, size_t numItems, uint8_t **v, ISzAlloc *alloc)
{
uint8_t b = 0;
uint8_t mask = 0;
size_t i;
MY_ALLOC(uint8_t, *v, numItems, alloc);
for (i = 0; i < numItems; i++)
{
if (mask == 0)
{
RINOK(SzReaduint8_t(sd, &b));
mask = 0x80;
}
(*v)[i] = (uint8_t)(((b & mask) != 0) ? 1 : 0);
mask >>= 1;
}
return SZ_OK;
}
static SRes SzReadBoolVector2(CSzData *sd, size_t numItems, uint8_t **v, ISzAlloc *alloc)
{
uint8_t allAreDefined;
size_t i;
RINOK(SzReaduint8_t(sd, &allAreDefined));
if (allAreDefined == 0)
return SzReadBoolVector(sd, numItems, v, alloc);
MY_ALLOC(uint8_t, *v, numItems, alloc);
for (i = 0; i < numItems; i++)
(*v)[i] = 1;
return SZ_OK;
}
static SRes SzReadHashDigests(
CSzData *sd,
size_t numItems,
uint8_t **digestsDefined,
uint32_t **digests,
ISzAlloc *alloc)
{
size_t i;
RINOK(SzReadBoolVector2(sd, numItems, digestsDefined, alloc));
MY_ALLOC(uint32_t, *digests, numItems, alloc);
for (i = 0; i < numItems; i++)
if ((*digestsDefined)[i])
{
RINOK(SzReaduint32_t(sd, (*digests) + i));
}
return SZ_OK;
}
static SRes SzReadPackInfo(
CSzData *sd,
uint64_t *dataOffset,
uint32_t *numPackStreams,
uint64_t **packSizes,
uint8_t **packCRCsDefined,
uint32_t **packCRCs,
ISzAlloc *alloc)
{
uint32_t i;
RINOK(SzReadNumber(sd, dataOffset));
RINOK(SzReadNumber32(sd, numPackStreams));
RINOK(SzWaitAttribute(sd, k7zIdSize));
MY_ALLOC(uint64_t, *packSizes, (size_t)*numPackStreams, alloc);
for (i = 0; i < *numPackStreams; i++)
{
RINOK(SzReadNumber(sd, (*packSizes) + i));
}
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
break;
if (type == k7zIdCRC)
{
RINOK(SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, alloc));
continue;
}
RINOK(SzSkeepData(sd));
}
if (*packCRCsDefined == 0)
{
MY_ALLOC(uint8_t, *packCRCsDefined, (size_t)*numPackStreams, alloc);
MY_ALLOC(uint32_t, *packCRCs, (size_t)*numPackStreams, alloc);
for (i = 0; i < *numPackStreams; i++)
{
(*packCRCsDefined)[i] = 0;
(*packCRCs)[i] = 0;
}
}
return SZ_OK;
}
static SRes SzReadSwitch(CSzData *sd)
{
uint8_t external;
RINOK(SzReaduint8_t(sd, &external));
return (external == 0) ? SZ_OK: SZ_ERROR_UNSUPPORTED;
}
static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
{
uint32_t numCoders, numBindPairs, numPackStreams, i;
uint32_t numInStreams = 0, numOutStreams = 0;
RINOK(SzReadNumber32(sd, &numCoders));
if (numCoders > NUM_FOLDER_CODERS_MAX)
return SZ_ERROR_UNSUPPORTED;
folder->NumCoders = numCoders;
MY_ALLOC(CSzCoderInfo, folder->Coders, (size_t)numCoders, alloc);
for (i = 0; i < numCoders; i++)
SzCoderInfo_Init(folder->Coders + i);
for (i = 0; i < numCoders; i++)
{
uint8_t mainuint8_t;
CSzCoderInfo *coder = folder->Coders + i;
{
unsigned idSize, j;
uint8_t longID[15];
RINOK(SzReaduint8_t(sd, &mainuint8_t));
idSize = (unsigned)(mainuint8_t & 0xF);
RINOK(SzReaduint8_ts(sd, longID, idSize));
if (idSize > sizeof(coder->MethodID))
return SZ_ERROR_UNSUPPORTED;
coder->MethodID = 0;
for (j = 0; j < idSize; j++)
coder->MethodID |= (uint64_t)longID[idSize - 1 - j] << (8 * j);
if ((mainuint8_t & 0x10) != 0)
{
RINOK(SzReadNumber32(sd, &coder->NumInStreams));
RINOK(SzReadNumber32(sd, &coder->NumOutStreams));
if (coder->NumInStreams > NUM_CODER_STREAMS_MAX ||
coder->NumOutStreams > NUM_CODER_STREAMS_MAX)
return SZ_ERROR_UNSUPPORTED;
}
else
{
coder->NumInStreams = 1;
coder->NumOutStreams = 1;
}
if ((mainuint8_t & 0x20) != 0)
{
uint64_t propertiesSize = 0;
RINOK(SzReadNumber(sd, &propertiesSize));
if (!Buf_Create(&coder->Props, (size_t)propertiesSize, alloc))
return SZ_ERROR_MEM;
RINOK(SzReaduint8_ts(sd, coder->Props.data, (size_t)propertiesSize));
}
}
while ((mainuint8_t & 0x80) != 0)
{
RINOK(SzReaduint8_t(sd, &mainuint8_t));
RINOK(SzSkeepDataSize(sd, (mainuint8_t & 0xF)));
if ((mainuint8_t & 0x10) != 0)
{
uint32_t n;
RINOK(SzReadNumber32(sd, &n));
RINOK(SzReadNumber32(sd, &n));
}
if ((mainuint8_t & 0x20) != 0)
{
uint64_t propertiesSize = 0;
RINOK(SzReadNumber(sd, &propertiesSize));
RINOK(SzSkeepDataSize(sd, propertiesSize));
}
}
numInStreams += coder->NumInStreams;
numOutStreams += coder->NumOutStreams;
}
if (numOutStreams == 0)
return SZ_ERROR_UNSUPPORTED;
folder->NumBindPairs = numBindPairs = numOutStreams - 1;
MY_ALLOC(CSzBindPair, folder->BindPairs, (size_t)numBindPairs, alloc);
for (i = 0; i < numBindPairs; i++)
{
CSzBindPair *bp = folder->BindPairs + i;
RINOK(SzReadNumber32(sd, &bp->InIndex));
RINOK(SzReadNumber32(sd, &bp->OutIndex));
}
if (numInStreams < numBindPairs)
return SZ_ERROR_UNSUPPORTED;
folder->NumPackStreams = numPackStreams = numInStreams - numBindPairs;
MY_ALLOC(uint32_t, folder->PackStreams, (size_t)numPackStreams, alloc);
if (numPackStreams == 1)
{
for (i = 0; i < numInStreams ; i++)
if (SzFolder_FindBindPairForInStream(folder, i) < 0)
break;
if (i == numInStreams)
return SZ_ERROR_UNSUPPORTED;
folder->PackStreams[0] = i;
}
else
for (i = 0; i < numPackStreams; i++)
{
RINOK(SzReadNumber32(sd, folder->PackStreams + i));
}
return SZ_OK;
}
static SRes SzReadUnpackInfo(
CSzData *sd,
uint32_t *numFolders,
CSzFolder **folders, /* for alloc */
ISzAlloc *alloc,
ISzAlloc *allocTemp)
{
uint32_t i;
RINOK(SzWaitAttribute(sd, k7zIdFolder));
RINOK(SzReadNumber32(sd, numFolders));
{
RINOK(SzReadSwitch(sd));
MY_ALLOC(CSzFolder, *folders, (size_t)*numFolders, alloc);
for (i = 0; i < *numFolders; i++)
SzFolder_Init((*folders) + i);
for (i = 0; i < *numFolders; i++)
{
RINOK(SzGetNextFolderItem(sd, (*folders) + i, alloc));
}
}
RINOK(SzWaitAttribute(sd, k7zIdCodersUnpackSize));
for (i = 0; i < *numFolders; i++)
{
uint32_t j;
CSzFolder *folder = (*folders) + i;
uint32_t numOutStreams = SzFolder_GetNumOutStreams(folder);
MY_ALLOC(uint64_t, folder->UnpackSizes, (size_t)numOutStreams, alloc);
for (j = 0; j < numOutStreams; j++)
{
RINOK(SzReadNumber(sd, folder->UnpackSizes + j));
}
}
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
return SZ_OK;
if (type == k7zIdCRC)
{
SRes res;
uint8_t *crcsDefined = 0;
uint32_t *crcs = 0;
res = SzReadHashDigests(sd, *numFolders, &crcsDefined, &crcs, allocTemp);
if (res == SZ_OK)
{
for (i = 0; i < *numFolders; i++)
{
CSzFolder *folder = (*folders) + i;
folder->UnpackCRCDefined = crcsDefined[i];
folder->UnpackCRC = crcs[i];
}
}
IAlloc_Free(allocTemp, crcs);
IAlloc_Free(allocTemp, crcsDefined);
RINOK(res);
continue;
}
RINOK(SzSkeepData(sd));
}
}
static SRes SzReadSubStreamsInfo(
CSzData *sd,
uint32_t numFolders,
CSzFolder *folders,
uint32_t *numUnpackStreams,
uint64_t **unpackSizes,
uint8_t **digestsDefined,
uint32_t **digests,
ISzAlloc *allocTemp)
{
uint64_t type = 0;
uint32_t i;
uint32_t si = 0;
uint32_t numDigests = 0;
for (i = 0; i < numFolders; i++)
folders[i].NumUnpackStreams = 1;
*numUnpackStreams = numFolders;
for (;;)
{
RINOK(SzReadID(sd, &type));
if (type == k7zIdNumUnpackStream)
{
*numUnpackStreams = 0;
for (i = 0; i < numFolders; i++)
{
uint32_t numStreams;
RINOK(SzReadNumber32(sd, &numStreams));
folders[i].NumUnpackStreams = numStreams;
*numUnpackStreams += numStreams;
}
continue;
}
if (type == k7zIdCRC || type == k7zIdSize)
break;
if (type == k7zIdEnd)
break;
RINOK(SzSkeepData(sd));
}
if (*numUnpackStreams == 0)
{
*unpackSizes = 0;
*digestsDefined = 0;
*digests = 0;
}
else
{
*unpackSizes = (uint64_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint64_t));
RINOM(*unpackSizes);
*digestsDefined = (uint8_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint8_t));
RINOM(*digestsDefined);
*digests = (uint32_t *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(uint32_t));
RINOM(*digests);
}
for (i = 0; i < numFolders; i++)
{
/*
v3.13 incorrectly worked with empty folders
v4.07: we check that folder is empty
*/
uint64_t sum = 0;
uint32_t j;
uint32_t numSubstreams = folders[i].NumUnpackStreams;
if (numSubstreams == 0)
continue;
if (type == k7zIdSize)
for (j = 1; j < numSubstreams; j++)
{
uint64_t size;
RINOK(SzReadNumber(sd, &size));
(*unpackSizes)[si++] = size;
sum += size;
}
(*unpackSizes)[si++] = SzFolder_GetUnpackSize(folders + i) - sum;
}
if (type == k7zIdSize)
{
RINOK(SzReadID(sd, &type));
}
for (i = 0; i < *numUnpackStreams; i++)
{
(*digestsDefined)[i] = 0;
(*digests)[i] = 0;
}
for (i = 0; i < numFolders; i++)
{
uint32_t numSubstreams = folders[i].NumUnpackStreams;
if (numSubstreams != 1 || !folders[i].UnpackCRCDefined)
numDigests += numSubstreams;
}
si = 0;
for (;;)
{
if (type == k7zIdCRC)
{
int digestIndex = 0;
uint8_t *digestsDefined2 = 0;
uint32_t *digests2 = 0;
SRes res = SzReadHashDigests(sd, numDigests, &digestsDefined2, &digests2, allocTemp);
if (res == SZ_OK)
{
for (i = 0; i < numFolders; i++)
{
CSzFolder *folder = folders + i;
uint32_t numSubstreams = folder->NumUnpackStreams;
if (numSubstreams == 1 && folder->UnpackCRCDefined)
{
(*digestsDefined)[si] = 1;
(*digests)[si] = folder->UnpackCRC;
si++;
}
else
{
uint32_t j;
for (j = 0; j < numSubstreams; j++, digestIndex++)
{
(*digestsDefined)[si] = digestsDefined2[digestIndex];
(*digests)[si] = digests2[digestIndex];
si++;
}
}
}
}
IAlloc_Free(allocTemp, digestsDefined2);
IAlloc_Free(allocTemp, digests2);
RINOK(res);
}
else if (type == k7zIdEnd)
return SZ_OK;
else
{
RINOK(SzSkeepData(sd));
}
RINOK(SzReadID(sd, &type));
}
}
static SRes SzReadStreamsInfo(
CSzData *sd,
uint64_t *dataOffset,
CSzAr *p,
uint32_t *numUnpackStreams,
uint64_t **unpackSizes, /* allocTemp */
uint8_t **digestsDefined, /* allocTemp */
uint32_t **digests, /* allocTemp */
ISzAlloc *alloc,
ISzAlloc *allocTemp)
{
for (;;)
{
uint64_t type;
RINOK(SzReadID(sd, &type));
if ((uint64_t)(int)type != type)
return SZ_ERROR_UNSUPPORTED;
switch((int)type)
{
case k7zIdEnd:
return SZ_OK;
case k7zIdPackInfo:
{
RINOK(SzReadPackInfo(sd, dataOffset, &p->NumPackStreams,
&p->PackSizes, &p->PackCRCsDefined, &p->PackCRCs, alloc));
break;
}
case k7zIdUnpackInfo:
{
RINOK(SzReadUnpackInfo(sd, &p->NumFolders, &p->Folders, alloc, allocTemp));
break;
}
case k7zIdSubStreamsInfo:
{
RINOK(SzReadSubStreamsInfo(sd, p->NumFolders, p->Folders,
numUnpackStreams, unpackSizes, digestsDefined, digests, allocTemp));
break;
}
default:
return SZ_ERROR_UNSUPPORTED;
}
}
}
size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, uint16_t *dest)
{
size_t len = p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
if (dest != 0)
{
size_t i;
const uint8_t *src = p->FileNames.data + (p->FileNameOffsets[fileIndex] * 2);
for (i = 0; i < len; i++)
dest[i] = GetUi16(src + i * 2);
}
return len;
}
static SRes SzReadFileNames(const uint8_t *p, size_t size, uint32_t numFiles, size_t *sizes)
{
uint32_t i;
size_t pos = 0;
for (i = 0; i < numFiles; i++)
{
sizes[i] = pos;
for (;;)
{
if (pos >= size)
return SZ_ERROR_ARCHIVE;
if (p[pos * 2] == 0 && p[pos * 2 + 1] == 0)
break;
pos++;
}
pos++;
}
sizes[i] = pos;
return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
}
static SRes SzReadHeader2(
CSzArEx *p, /* allocMain */
CSzData *sd,
uint64_t **unpackSizes, /* allocTemp */
uint8_t **digestsDefined, /* allocTemp */
uint32_t **digests, /* allocTemp */
uint8_t **emptyStreamVector, /* allocTemp */
uint8_t **emptyFileVector, /* allocTemp */
uint8_t **lwtVector, /* allocTemp */
ISzAlloc *allocMain,
ISzAlloc *allocTemp)
{
uint64_t type;
uint32_t numUnpackStreams = 0;
uint32_t numFiles = 0;
CSzFileItem *files = 0;
uint32_t numEmptyStreams = 0;
uint32_t i;
RINOK(SzReadID(sd, &type));
if (type == k7zIdArchiveProperties)
{
RINOK(SzReadArchiveProperties(sd));
RINOK(SzReadID(sd, &type));
}
if (type == k7zIdMainStreamsInfo)
{
RINOK(SzReadStreamsInfo(sd,
&p->dataPos,
&p->db,
&numUnpackStreams,
unpackSizes,
digestsDefined,
digests, allocMain, allocTemp));
p->dataPos += p->startPosAfterHeader;
RINOK(SzReadID(sd, &type));
}
if (type == k7zIdEnd)
return SZ_OK;
if (type != k7zIdFilesInfo)
return SZ_ERROR_ARCHIVE;
RINOK(SzReadNumber32(sd, &numFiles));
p->db.NumFiles = numFiles;
MY_ALLOC(CSzFileItem, files, (size_t)numFiles, allocMain);
p->db.Files = files;
for (i = 0; i < numFiles; i++)
SzFile_Init(files + i);
for (;;)
{
uint64_t size;
RINOK(SzReadID(sd, &type));
if (type == k7zIdEnd)
break;
RINOK(SzReadNumber(sd, &size));
if (size > sd->Size)
return SZ_ERROR_ARCHIVE;
if ((uint64_t)(int)type != type)
{
RINOK(SzSkeepDataSize(sd, size));
}
else
switch((int)type)
{
case k7zIdName:
{
size_t namesSize;
RINOK(SzReadSwitch(sd));
namesSize = (size_t)size - 1;
if ((namesSize & 1) != 0)
return SZ_ERROR_ARCHIVE;
if (!Buf_Create(&p->FileNames, namesSize, allocMain))
return SZ_ERROR_MEM;
MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain);
memcpy(p->FileNames.data, sd->Data, namesSize);
RINOK(SzReadFileNames(sd->Data, namesSize >> 1, numFiles, p->FileNameOffsets))
RINOK(SzSkeepDataSize(sd, namesSize));
break;
}
case k7zIdEmptyStream:
{
RINOK(SzReadBoolVector(sd, numFiles, emptyStreamVector, allocTemp));
numEmptyStreams = 0;
for (i = 0; i < numFiles; i++)