diff options
Diffstat (limited to '')
-rw-r--r-- | C/7zArcIn.c | 405 |
1 files changed, 204 insertions, 201 deletions
diff --git a/C/7zArcIn.c b/C/7zArcIn.c index 0d9dec4..43fa7c2 100644 --- a/C/7zArcIn.c +++ b/C/7zArcIn.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* 7zArcIn.c -- 7z Input functions | 1 | /* 7zArcIn.c -- 7z Input functions |
2 | 2021-02-09 : Igor Pavlov : Public domain */ | 2 | 2023-05-11 : Igor Pavlov : Public domain */ |
3 | 3 | ||
4 | #include "Precomp.h" | 4 | #include "Precomp.h" |
5 | 5 | ||
@@ -10,10 +10,11 @@ | |||
10 | #include "7zCrc.h" | 10 | #include "7zCrc.h" |
11 | #include "CpuArch.h" | 11 | #include "CpuArch.h" |
12 | 12 | ||
13 | #define MY_ALLOC(T, p, size, alloc) { \ | 13 | #define MY_ALLOC(T, p, size, alloc) \ |
14 | if ((p = (T *)ISzAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; } | 14 | { if ((p = (T *)ISzAlloc_Alloc(alloc, (size) * sizeof(T))) == NULL) return SZ_ERROR_MEM; } |
15 | 15 | ||
16 | #define MY_ALLOC_ZE(T, p, size, alloc) { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) } | 16 | #define MY_ALLOC_ZE(T, p, size, alloc) \ |
17 | { if ((size) == 0) p = NULL; else MY_ALLOC(T, p, size, alloc) } | ||
17 | 18 | ||
18 | #define MY_ALLOC_AND_CPY(to, size, from, alloc) \ | 19 | #define MY_ALLOC_AND_CPY(to, size, from, alloc) \ |
19 | { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); } | 20 | { MY_ALLOC(Byte, to, size, alloc); memcpy(to, from, size); } |
@@ -58,7 +59,7 @@ enum EIdEnum | |||
58 | 59 | ||
59 | const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; | 60 | const Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; |
60 | 61 | ||
61 | #define SzBitUi32s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; } | 62 | #define SzBitUi32s_INIT(p) { (p)->Defs = NULL; (p)->Vals = NULL; } |
62 | 63 | ||
63 | static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc) | 64 | static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc) |
64 | { | 65 | { |
@@ -69,8 +70,8 @@ static SRes SzBitUi32s_Alloc(CSzBitUi32s *p, size_t num, ISzAllocPtr alloc) | |||
69 | } | 70 | } |
70 | else | 71 | else |
71 | { | 72 | { |
72 | MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc); | 73 | MY_ALLOC(Byte, p->Defs, (num + 7) >> 3, alloc) |
73 | MY_ALLOC(UInt32, p->Vals, num, alloc); | 74 | MY_ALLOC(UInt32, p->Vals, num, alloc) |
74 | } | 75 | } |
75 | return SZ_OK; | 76 | return SZ_OK; |
76 | } | 77 | } |
@@ -81,7 +82,7 @@ static void SzBitUi32s_Free(CSzBitUi32s *p, ISzAllocPtr alloc) | |||
81 | ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL; | 82 | ISzAlloc_Free(alloc, p->Vals); p->Vals = NULL; |
82 | } | 83 | } |
83 | 84 | ||
84 | #define SzBitUi64s_Init(p) { (p)->Defs = NULL; (p)->Vals = NULL; } | 85 | #define SzBitUi64s_INIT(p) { (p)->Defs = NULL; (p)->Vals = NULL; } |
85 | 86 | ||
86 | static void SzBitUi64s_Free(CSzBitUi64s *p, ISzAllocPtr alloc) | 87 | static void SzBitUi64s_Free(CSzBitUi64s *p, ISzAllocPtr alloc) |
87 | { | 88 | { |
@@ -96,7 +97,7 @@ static void SzAr_Init(CSzAr *p) | |||
96 | p->NumFolders = 0; | 97 | p->NumFolders = 0; |
97 | 98 | ||
98 | p->PackPositions = NULL; | 99 | p->PackPositions = NULL; |
99 | SzBitUi32s_Init(&p->FolderCRCs); | 100 | SzBitUi32s_INIT(&p->FolderCRCs) |
100 | 101 | ||
101 | p->FoCodersOffsets = NULL; | 102 | p->FoCodersOffsets = NULL; |
102 | p->FoStartPackStreamIndex = NULL; | 103 | p->FoStartPackStreamIndex = NULL; |
@@ -142,11 +143,11 @@ void SzArEx_Init(CSzArEx *p) | |||
142 | p->FileNameOffsets = NULL; | 143 | p->FileNameOffsets = NULL; |
143 | p->FileNames = NULL; | 144 | p->FileNames = NULL; |
144 | 145 | ||
145 | SzBitUi32s_Init(&p->CRCs); | 146 | SzBitUi32s_INIT(&p->CRCs) |
146 | SzBitUi32s_Init(&p->Attribs); | 147 | SzBitUi32s_INIT(&p->Attribs) |
147 | // SzBitUi32s_Init(&p->Parents); | 148 | // SzBitUi32s_INIT(&p->Parents) |
148 | SzBitUi64s_Init(&p->MTime); | 149 | SzBitUi64s_INIT(&p->MTime) |
149 | SzBitUi64s_Init(&p->CTime); | 150 | SzBitUi64s_INIT(&p->CTime) |
150 | } | 151 | } |
151 | 152 | ||
152 | void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc) | 153 | void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc) |
@@ -180,11 +181,20 @@ static int TestSignatureCandidate(const Byte *testBytes) | |||
180 | return 1; | 181 | return 1; |
181 | } | 182 | } |
182 | 183 | ||
183 | #define SzData_Clear(p) { (p)->Data = NULL; (p)->Size = 0; } | 184 | #define SzData_CLEAR(p) { (p)->Data = NULL; (p)->Size = 0; } |
185 | |||
186 | #define SZ_READ_BYTE_SD_NOCHECK(_sd_, dest) \ | ||
187 | (_sd_)->Size--; dest = *(_sd_)->Data++; | ||
188 | |||
189 | #define SZ_READ_BYTE_SD(_sd_, dest) \ | ||
190 | if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; \ | ||
191 | SZ_READ_BYTE_SD_NOCHECK(_sd_, dest) | ||
184 | 192 | ||
185 | #define SZ_READ_BYTE_SD(_sd_, dest) if ((_sd_)->Size == 0) return SZ_ERROR_ARCHIVE; (_sd_)->Size--; dest = *(_sd_)->Data++; | ||
186 | #define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest) | 193 | #define SZ_READ_BYTE(dest) SZ_READ_BYTE_SD(sd, dest) |
187 | #define SZ_READ_BYTE_2(dest) if (sd.Size == 0) return SZ_ERROR_ARCHIVE; sd.Size--; dest = *sd.Data++; | 194 | |
195 | #define SZ_READ_BYTE_2(dest) \ | ||
196 | if (sd.Size == 0) return SZ_ERROR_ARCHIVE; \ | ||
197 | sd.Size--; dest = *sd.Data++; | ||
188 | 198 | ||
189 | #define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); } | 199 | #define SKIP_DATA(sd, size) { sd->Size -= (size_t)(size); sd->Data += (size_t)(size); } |
190 | #define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); } | 200 | #define SKIP_DATA2(sd, size) { sd.Size -= (size_t)(size); sd.Data += (size_t)(size); } |
@@ -192,25 +202,25 @@ static int TestSignatureCandidate(const Byte *testBytes) | |||
192 | #define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \ | 202 | #define SZ_READ_32(dest) if (sd.Size < 4) return SZ_ERROR_ARCHIVE; \ |
193 | dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4); | 203 | dest = GetUi32(sd.Data); SKIP_DATA2(sd, 4); |
194 | 204 | ||
195 | static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value) | 205 | static Z7_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value) |
196 | { | 206 | { |
197 | Byte firstByte, mask; | 207 | Byte firstByte, mask; |
198 | unsigned i; | 208 | unsigned i; |
199 | UInt32 v; | 209 | UInt32 v; |
200 | 210 | ||
201 | SZ_READ_BYTE(firstByte); | 211 | SZ_READ_BYTE(firstByte) |
202 | if ((firstByte & 0x80) == 0) | 212 | if ((firstByte & 0x80) == 0) |
203 | { | 213 | { |
204 | *value = firstByte; | 214 | *value = firstByte; |
205 | return SZ_OK; | 215 | return SZ_OK; |
206 | } | 216 | } |
207 | SZ_READ_BYTE(v); | 217 | SZ_READ_BYTE(v) |
208 | if ((firstByte & 0x40) == 0) | 218 | if ((firstByte & 0x40) == 0) |
209 | { | 219 | { |
210 | *value = (((UInt32)firstByte & 0x3F) << 8) | v; | 220 | *value = (((UInt32)firstByte & 0x3F) << 8) | v; |
211 | return SZ_OK; | 221 | return SZ_OK; |
212 | } | 222 | } |
213 | SZ_READ_BYTE(mask); | 223 | SZ_READ_BYTE(mask) |
214 | *value = v | ((UInt32)mask << 8); | 224 | *value = v | ((UInt32)mask << 8); |
215 | mask = 0x20; | 225 | mask = 0x20; |
216 | for (i = 2; i < 8; i++) | 226 | for (i = 2; i < 8; i++) |
@@ -218,11 +228,11 @@ static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value) | |||
218 | Byte b; | 228 | Byte b; |
219 | if ((firstByte & mask) == 0) | 229 | if ((firstByte & mask) == 0) |
220 | { | 230 | { |
221 | UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1); | 231 | const UInt64 highPart = (unsigned)firstByte & (unsigned)(mask - 1); |
222 | *value |= (highPart << (8 * i)); | 232 | *value |= (highPart << (8 * i)); |
223 | return SZ_OK; | 233 | return SZ_OK; |
224 | } | 234 | } |
225 | SZ_READ_BYTE(b); | 235 | SZ_READ_BYTE(b) |
226 | *value |= ((UInt64)b << (8 * i)); | 236 | *value |= ((UInt64)b << (8 * i)); |
227 | mask >>= 1; | 237 | mask >>= 1; |
228 | } | 238 | } |
@@ -230,7 +240,7 @@ static MY_NO_INLINE SRes ReadNumber(CSzData *sd, UInt64 *value) | |||
230 | } | 240 | } |
231 | 241 | ||
232 | 242 | ||
233 | static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value) | 243 | static Z7_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value) |
234 | { | 244 | { |
235 | Byte firstByte; | 245 | Byte firstByte; |
236 | UInt64 value64; | 246 | UInt64 value64; |
@@ -244,7 +254,7 @@ static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value) | |||
244 | sd->Size--; | 254 | sd->Size--; |
245 | return SZ_OK; | 255 | return SZ_OK; |
246 | } | 256 | } |
247 | RINOK(ReadNumber(sd, &value64)); | 257 | RINOK(ReadNumber(sd, &value64)) |
248 | if (value64 >= (UInt32)0x80000000 - 1) | 258 | if (value64 >= (UInt32)0x80000000 - 1) |
249 | return SZ_ERROR_UNSUPPORTED; | 259 | return SZ_ERROR_UNSUPPORTED; |
250 | if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4))) | 260 | if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 4))) |
@@ -258,10 +268,10 @@ static MY_NO_INLINE SRes SzReadNumber32(CSzData *sd, UInt32 *value) | |||
258 | static SRes SkipData(CSzData *sd) | 268 | static SRes SkipData(CSzData *sd) |
259 | { | 269 | { |
260 | UInt64 size; | 270 | UInt64 size; |
261 | RINOK(ReadNumber(sd, &size)); | 271 | RINOK(ReadNumber(sd, &size)) |
262 | if (size > sd->Size) | 272 | if (size > sd->Size) |
263 | return SZ_ERROR_ARCHIVE; | 273 | return SZ_ERROR_ARCHIVE; |
264 | SKIP_DATA(sd, size); | 274 | SKIP_DATA(sd, size) |
265 | return SZ_OK; | 275 | return SZ_OK; |
266 | } | 276 | } |
267 | 277 | ||
@@ -270,22 +280,22 @@ static SRes WaitId(CSzData *sd, UInt32 id) | |||
270 | for (;;) | 280 | for (;;) |
271 | { | 281 | { |
272 | UInt64 type; | 282 | UInt64 type; |
273 | RINOK(ReadID(sd, &type)); | 283 | RINOK(ReadID(sd, &type)) |
274 | if (type == id) | 284 | if (type == id) |
275 | return SZ_OK; | 285 | return SZ_OK; |
276 | if (type == k7zIdEnd) | 286 | if (type == k7zIdEnd) |
277 | return SZ_ERROR_ARCHIVE; | 287 | return SZ_ERROR_ARCHIVE; |
278 | RINOK(SkipData(sd)); | 288 | RINOK(SkipData(sd)) |
279 | } | 289 | } |
280 | } | 290 | } |
281 | 291 | ||
282 | static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v) | 292 | static SRes RememberBitVector(CSzData *sd, UInt32 numItems, const Byte **v) |
283 | { | 293 | { |
284 | UInt32 numBytes = (numItems + 7) >> 3; | 294 | const UInt32 numBytes = (numItems + 7) >> 3; |
285 | if (numBytes > sd->Size) | 295 | if (numBytes > sd->Size) |
286 | return SZ_ERROR_ARCHIVE; | 296 | return SZ_ERROR_ARCHIVE; |
287 | *v = sd->Data; | 297 | *v = sd->Data; |
288 | SKIP_DATA(sd, numBytes); | 298 | SKIP_DATA(sd, numBytes) |
289 | return SZ_OK; | 299 | return SZ_OK; |
290 | } | 300 | } |
291 | 301 | ||
@@ -307,48 +317,48 @@ static UInt32 CountDefinedBits(const Byte *bits, UInt32 numItems) | |||
307 | return sum; | 317 | return sum; |
308 | } | 318 | } |
309 | 319 | ||
310 | static MY_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAllocPtr alloc) | 320 | static Z7_NO_INLINE SRes ReadBitVector(CSzData *sd, UInt32 numItems, Byte **v, ISzAllocPtr alloc) |
311 | { | 321 | { |
312 | Byte allAreDefined; | 322 | Byte allAreDefined; |
313 | Byte *v2; | 323 | Byte *v2; |
314 | UInt32 numBytes = (numItems + 7) >> 3; | 324 | const UInt32 numBytes = (numItems + 7) >> 3; |
315 | *v = NULL; | 325 | *v = NULL; |
316 | SZ_READ_BYTE(allAreDefined); | 326 | SZ_READ_BYTE(allAreDefined) |
317 | if (numBytes == 0) | 327 | if (numBytes == 0) |
318 | return SZ_OK; | 328 | return SZ_OK; |
319 | if (allAreDefined == 0) | 329 | if (allAreDefined == 0) |
320 | { | 330 | { |
321 | if (numBytes > sd->Size) | 331 | if (numBytes > sd->Size) |
322 | return SZ_ERROR_ARCHIVE; | 332 | return SZ_ERROR_ARCHIVE; |
323 | MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc); | 333 | MY_ALLOC_AND_CPY(*v, numBytes, sd->Data, alloc) |
324 | SKIP_DATA(sd, numBytes); | 334 | SKIP_DATA(sd, numBytes) |
325 | return SZ_OK; | 335 | return SZ_OK; |
326 | } | 336 | } |
327 | MY_ALLOC(Byte, *v, numBytes, alloc); | 337 | MY_ALLOC(Byte, *v, numBytes, alloc) |
328 | v2 = *v; | 338 | v2 = *v; |
329 | memset(v2, 0xFF, (size_t)numBytes); | 339 | memset(v2, 0xFF, (size_t)numBytes); |
330 | { | 340 | { |
331 | unsigned numBits = (unsigned)numItems & 7; | 341 | const unsigned numBits = (unsigned)numItems & 7; |
332 | if (numBits != 0) | 342 | if (numBits != 0) |
333 | v2[(size_t)numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits)); | 343 | v2[(size_t)numBytes - 1] = (Byte)((((UInt32)1 << numBits) - 1) << (8 - numBits)); |
334 | } | 344 | } |
335 | return SZ_OK; | 345 | return SZ_OK; |
336 | } | 346 | } |
337 | 347 | ||
338 | static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) | 348 | static Z7_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) |
339 | { | 349 | { |
340 | UInt32 i; | 350 | UInt32 i; |
341 | CSzData sd; | 351 | CSzData sd; |
342 | UInt32 *vals; | 352 | UInt32 *vals; |
343 | const Byte *defs; | 353 | const Byte *defs; |
344 | MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc); | 354 | MY_ALLOC_ZE(UInt32, crcs->Vals, numItems, alloc) |
345 | sd = *sd2; | 355 | sd = *sd2; |
346 | defs = crcs->Defs; | 356 | defs = crcs->Defs; |
347 | vals = crcs->Vals; | 357 | vals = crcs->Vals; |
348 | for (i = 0; i < numItems; i++) | 358 | for (i = 0; i < numItems; i++) |
349 | if (SzBitArray_Check(defs, i)) | 359 | if (SzBitArray_Check(defs, i)) |
350 | { | 360 | { |
351 | SZ_READ_32(vals[i]); | 361 | SZ_READ_32(vals[i]) |
352 | } | 362 | } |
353 | else | 363 | else |
354 | vals[i] = 0; | 364 | vals[i] = 0; |
@@ -359,7 +369,7 @@ static MY_NO_INLINE SRes ReadUi32s(CSzData *sd2, UInt32 numItems, CSzBitUi32s *c | |||
359 | static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) | 369 | static SRes ReadBitUi32s(CSzData *sd, UInt32 numItems, CSzBitUi32s *crcs, ISzAllocPtr alloc) |
360 | { | 370 | { |
361 | SzBitUi32s_Free(crcs, alloc); | 371 | SzBitUi32s_Free(crcs, alloc); |
362 | RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc)); | 372 | RINOK(ReadBitVector(sd, numItems, &crcs->Defs, alloc)) |
363 | return ReadUi32s(sd, numItems, crcs, alloc); | 373 | return ReadUi32s(sd, numItems, crcs, alloc); |
364 | } | 374 | } |
365 | 375 | ||
@@ -367,36 +377,36 @@ static SRes SkipBitUi32s(CSzData *sd, UInt32 numItems) | |||
367 | { | 377 | { |
368 | Byte allAreDefined; | 378 | Byte allAreDefined; |
369 | UInt32 numDefined = numItems; | 379 | UInt32 numDefined = numItems; |
370 | SZ_READ_BYTE(allAreDefined); | 380 | SZ_READ_BYTE(allAreDefined) |
371 | if (!allAreDefined) | 381 | if (!allAreDefined) |
372 | { | 382 | { |
373 | size_t numBytes = (numItems + 7) >> 3; | 383 | const size_t numBytes = (numItems + 7) >> 3; |
374 | if (numBytes > sd->Size) | 384 | if (numBytes > sd->Size) |
375 | return SZ_ERROR_ARCHIVE; | 385 | return SZ_ERROR_ARCHIVE; |
376 | numDefined = CountDefinedBits(sd->Data, numItems); | 386 | numDefined = CountDefinedBits(sd->Data, numItems); |
377 | SKIP_DATA(sd, numBytes); | 387 | SKIP_DATA(sd, numBytes) |
378 | } | 388 | } |
379 | if (numDefined > (sd->Size >> 2)) | 389 | if (numDefined > (sd->Size >> 2)) |
380 | return SZ_ERROR_ARCHIVE; | 390 | return SZ_ERROR_ARCHIVE; |
381 | SKIP_DATA(sd, (size_t)numDefined * 4); | 391 | SKIP_DATA(sd, (size_t)numDefined * 4) |
382 | return SZ_OK; | 392 | return SZ_OK; |
383 | } | 393 | } |
384 | 394 | ||
385 | static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc) | 395 | static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc) |
386 | { | 396 | { |
387 | RINOK(SzReadNumber32(sd, &p->NumPackStreams)); | 397 | RINOK(SzReadNumber32(sd, &p->NumPackStreams)) |
388 | 398 | ||
389 | RINOK(WaitId(sd, k7zIdSize)); | 399 | RINOK(WaitId(sd, k7zIdSize)) |
390 | MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc); | 400 | MY_ALLOC(UInt64, p->PackPositions, (size_t)p->NumPackStreams + 1, alloc) |
391 | { | 401 | { |
392 | UInt64 sum = 0; | 402 | UInt64 sum = 0; |
393 | UInt32 i; | 403 | UInt32 i; |
394 | UInt32 numPackStreams = p->NumPackStreams; | 404 | const UInt32 numPackStreams = p->NumPackStreams; |
395 | for (i = 0; i < numPackStreams; i++) | 405 | for (i = 0; i < numPackStreams; i++) |
396 | { | 406 | { |
397 | UInt64 packSize; | 407 | UInt64 packSize; |
398 | p->PackPositions[i] = sum; | 408 | p->PackPositions[i] = sum; |
399 | RINOK(ReadNumber(sd, &packSize)); | 409 | RINOK(ReadNumber(sd, &packSize)) |
400 | sum += packSize; | 410 | sum += packSize; |
401 | if (sum < packSize) | 411 | if (sum < packSize) |
402 | return SZ_ERROR_ARCHIVE; | 412 | return SZ_ERROR_ARCHIVE; |
@@ -407,16 +417,16 @@ static SRes ReadPackInfo(CSzAr *p, CSzData *sd, ISzAllocPtr alloc) | |||
407 | for (;;) | 417 | for (;;) |
408 | { | 418 | { |
409 | UInt64 type; | 419 | UInt64 type; |
410 | RINOK(ReadID(sd, &type)); | 420 | RINOK(ReadID(sd, &type)) |
411 | if (type == k7zIdEnd) | 421 | if (type == k7zIdEnd) |
412 | return SZ_OK; | 422 | return SZ_OK; |
413 | if (type == k7zIdCRC) | 423 | if (type == k7zIdCRC) |
414 | { | 424 | { |
415 | /* CRC of packed streams is unused now */ | 425 | /* CRC of packed streams is unused now */ |
416 | RINOK(SkipBitUi32s(sd, p->NumPackStreams)); | 426 | RINOK(SkipBitUi32s(sd, p->NumPackStreams)) |
417 | continue; | 427 | continue; |
418 | } | 428 | } |
419 | RINOK(SkipData(sd)); | 429 | RINOK(SkipData(sd)) |
420 | } | 430 | } |
421 | } | 431 | } |
422 | 432 | ||
@@ -442,7 +452,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
442 | f->NumPackStreams = 0; | 452 | f->NumPackStreams = 0; |
443 | f->UnpackStream = 0; | 453 | f->UnpackStream = 0; |
444 | 454 | ||
445 | RINOK(SzReadNumber32(sd, &numCoders)); | 455 | RINOK(SzReadNumber32(sd, &numCoders)) |
446 | if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX) | 456 | if (numCoders == 0 || numCoders > SZ_NUM_CODERS_IN_FOLDER_MAX) |
447 | return SZ_ERROR_UNSUPPORTED; | 457 | return SZ_ERROR_UNSUPPORTED; |
448 | 458 | ||
@@ -453,7 +463,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
453 | unsigned idSize, j; | 463 | unsigned idSize, j; |
454 | UInt64 id; | 464 | UInt64 id; |
455 | 465 | ||
456 | SZ_READ_BYTE(mainByte); | 466 | SZ_READ_BYTE(mainByte) |
457 | if ((mainByte & 0xC0) != 0) | 467 | if ((mainByte & 0xC0) != 0) |
458 | return SZ_ERROR_UNSUPPORTED; | 468 | return SZ_ERROR_UNSUPPORTED; |
459 | 469 | ||
@@ -481,12 +491,12 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
481 | { | 491 | { |
482 | UInt32 numStreams; | 492 | UInt32 numStreams; |
483 | 493 | ||
484 | RINOK(SzReadNumber32(sd, &numStreams)); | 494 | RINOK(SzReadNumber32(sd, &numStreams)) |
485 | if (numStreams > k_NumCodersStreams_in_Folder_MAX) | 495 | if (numStreams > k_NumCodersStreams_in_Folder_MAX) |
486 | return SZ_ERROR_UNSUPPORTED; | 496 | return SZ_ERROR_UNSUPPORTED; |
487 | coder->NumStreams = (Byte)numStreams; | 497 | coder->NumStreams = (Byte)numStreams; |
488 | 498 | ||
489 | RINOK(SzReadNumber32(sd, &numStreams)); | 499 | RINOK(SzReadNumber32(sd, &numStreams)) |
490 | if (numStreams != 1) | 500 | if (numStreams != 1) |
491 | return SZ_ERROR_UNSUPPORTED; | 501 | return SZ_ERROR_UNSUPPORTED; |
492 | } | 502 | } |
@@ -499,7 +509,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
499 | if ((mainByte & 0x20) != 0) | 509 | if ((mainByte & 0x20) != 0) |
500 | { | 510 | { |
501 | UInt32 propsSize = 0; | 511 | UInt32 propsSize = 0; |
502 | RINOK(SzReadNumber32(sd, &propsSize)); | 512 | RINOK(SzReadNumber32(sd, &propsSize)) |
503 | if (propsSize > sd->Size) | 513 | if (propsSize > sd->Size) |
504 | return SZ_ERROR_ARCHIVE; | 514 | return SZ_ERROR_ARCHIVE; |
505 | if (propsSize >= 0x80) | 515 | if (propsSize >= 0x80) |
@@ -549,12 +559,12 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
549 | { | 559 | { |
550 | CSzBond *bp = f->Bonds + i; | 560 | CSzBond *bp = f->Bonds + i; |
551 | 561 | ||
552 | RINOK(SzReadNumber32(sd, &bp->InIndex)); | 562 | RINOK(SzReadNumber32(sd, &bp->InIndex)) |
553 | if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex]) | 563 | if (bp->InIndex >= numInStreams || streamUsed[bp->InIndex]) |
554 | return SZ_ERROR_ARCHIVE; | 564 | return SZ_ERROR_ARCHIVE; |
555 | streamUsed[bp->InIndex] = True; | 565 | streamUsed[bp->InIndex] = True; |
556 | 566 | ||
557 | RINOK(SzReadNumber32(sd, &bp->OutIndex)); | 567 | RINOK(SzReadNumber32(sd, &bp->OutIndex)) |
558 | if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex]) | 568 | if (bp->OutIndex >= numCoders || coderUsed[bp->OutIndex]) |
559 | return SZ_ERROR_ARCHIVE; | 569 | return SZ_ERROR_ARCHIVE; |
560 | coderUsed[bp->OutIndex] = True; | 570 | coderUsed[bp->OutIndex] = True; |
@@ -584,7 +594,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
584 | for (i = 0; i < numPackStreams; i++) | 594 | for (i = 0; i < numPackStreams; i++) |
585 | { | 595 | { |
586 | UInt32 index; | 596 | UInt32 index; |
587 | RINOK(SzReadNumber32(sd, &index)); | 597 | RINOK(SzReadNumber32(sd, &index)) |
588 | if (index >= numInStreams || streamUsed[index]) | 598 | if (index >= numInStreams || streamUsed[index]) |
589 | return SZ_ERROR_ARCHIVE; | 599 | return SZ_ERROR_ARCHIVE; |
590 | streamUsed[index] = True; | 600 | streamUsed[index] = True; |
@@ -598,7 +608,7 @@ SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd) | |||
598 | } | 608 | } |
599 | 609 | ||
600 | 610 | ||
601 | static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num) | 611 | static Z7_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num) |
602 | { | 612 | { |
603 | CSzData sd; | 613 | CSzData sd; |
604 | sd = *sd2; | 614 | sd = *sd2; |
@@ -606,7 +616,7 @@ static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num) | |||
606 | { | 616 | { |
607 | Byte firstByte, mask; | 617 | Byte firstByte, mask; |
608 | unsigned i; | 618 | unsigned i; |
609 | SZ_READ_BYTE_2(firstByte); | 619 | SZ_READ_BYTE_2(firstByte) |
610 | if ((firstByte & 0x80) == 0) | 620 | if ((firstByte & 0x80) == 0) |
611 | continue; | 621 | continue; |
612 | if ((firstByte & 0x40) == 0) | 622 | if ((firstByte & 0x40) == 0) |
@@ -622,7 +632,7 @@ static MY_NO_INLINE SRes SkipNumbers(CSzData *sd2, UInt32 num) | |||
622 | mask >>= 1; | 632 | mask >>= 1; |
623 | if (i > sd.Size) | 633 | if (i > sd.Size) |
624 | return SZ_ERROR_ARCHIVE; | 634 | return SZ_ERROR_ARCHIVE; |
625 | SKIP_DATA2(sd, i); | 635 | SKIP_DATA2(sd, i) |
626 | } | 636 | } |
627 | *sd2 = sd; | 637 | *sd2 = sd; |
628 | return SZ_OK; | 638 | return SZ_OK; |
@@ -645,30 +655,30 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
645 | const Byte *startBufPtr; | 655 | const Byte *startBufPtr; |
646 | Byte external; | 656 | Byte external; |
647 | 657 | ||
648 | RINOK(WaitId(sd2, k7zIdFolder)); | 658 | RINOK(WaitId(sd2, k7zIdFolder)) |
649 | 659 | ||
650 | RINOK(SzReadNumber32(sd2, &numFolders)); | 660 | RINOK(SzReadNumber32(sd2, &numFolders)) |
651 | if (numFolders > numFoldersMax) | 661 | if (numFolders > numFoldersMax) |
652 | return SZ_ERROR_UNSUPPORTED; | 662 | return SZ_ERROR_UNSUPPORTED; |
653 | p->NumFolders = numFolders; | 663 | p->NumFolders = numFolders; |
654 | 664 | ||
655 | SZ_READ_BYTE_SD(sd2, external); | 665 | SZ_READ_BYTE_SD(sd2, external) |
656 | if (external == 0) | 666 | if (external == 0) |
657 | sd = *sd2; | 667 | sd = *sd2; |
658 | else | 668 | else |
659 | { | 669 | { |
660 | UInt32 index; | 670 | UInt32 index; |
661 | RINOK(SzReadNumber32(sd2, &index)); | 671 | RINOK(SzReadNumber32(sd2, &index)) |
662 | if (index >= numTempBufs) | 672 | if (index >= numTempBufs) |
663 | return SZ_ERROR_ARCHIVE; | 673 | return SZ_ERROR_ARCHIVE; |
664 | sd.Data = tempBufs[index].data; | 674 | sd.Data = tempBufs[index].data; |
665 | sd.Size = tempBufs[index].size; | 675 | sd.Size = tempBufs[index].size; |
666 | } | 676 | } |
667 | 677 | ||
668 | MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc); | 678 | MY_ALLOC(size_t, p->FoCodersOffsets, (size_t)numFolders + 1, alloc) |
669 | MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc); | 679 | MY_ALLOC(UInt32, p->FoStartPackStreamIndex, (size_t)numFolders + 1, alloc) |
670 | MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc); | 680 | MY_ALLOC(UInt32, p->FoToCoderUnpackSizes, (size_t)numFolders + 1, alloc) |
671 | MY_ALLOC_ZE(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc); | 681 | MY_ALLOC_ZE(Byte, p->FoToMainUnpackSizeIndex, (size_t)numFolders, alloc) |
672 | 682 | ||
673 | startBufPtr = sd.Data; | 683 | startBufPtr = sd.Data; |
674 | 684 | ||
@@ -681,7 +691,7 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
681 | 691 | ||
682 | p->FoCodersOffsets[fo] = (size_t)(sd.Data - startBufPtr); | 692 | p->FoCodersOffsets[fo] = (size_t)(sd.Data - startBufPtr); |
683 | 693 | ||
684 | RINOK(SzReadNumber32(&sd, &numCoders)); | 694 | RINOK(SzReadNumber32(&sd, &numCoders)) |
685 | if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX) | 695 | if (numCoders == 0 || numCoders > k_Scan_NumCoders_MAX) |
686 | return SZ_ERROR_UNSUPPORTED; | 696 | return SZ_ERROR_UNSUPPORTED; |
687 | 697 | ||
@@ -691,7 +701,7 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
691 | unsigned idSize; | 701 | unsigned idSize; |
692 | UInt32 coderInStreams; | 702 | UInt32 coderInStreams; |
693 | 703 | ||
694 | SZ_READ_BYTE_2(mainByte); | 704 | SZ_READ_BYTE_2(mainByte) |
695 | if ((mainByte & 0xC0) != 0) | 705 | if ((mainByte & 0xC0) != 0) |
696 | return SZ_ERROR_UNSUPPORTED; | 706 | return SZ_ERROR_UNSUPPORTED; |
697 | idSize = (mainByte & 0xF); | 707 | idSize = (mainByte & 0xF); |
@@ -699,15 +709,15 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
699 | return SZ_ERROR_UNSUPPORTED; | 709 | return SZ_ERROR_UNSUPPORTED; |
700 | if (idSize > sd.Size) | 710 | if (idSize > sd.Size) |
701 | return SZ_ERROR_ARCHIVE; | 711 | return SZ_ERROR_ARCHIVE; |
702 | SKIP_DATA2(sd, idSize); | 712 | SKIP_DATA2(sd, idSize) |
703 | 713 | ||
704 | coderInStreams = 1; | 714 | coderInStreams = 1; |
705 | 715 | ||
706 | if ((mainByte & 0x10) != 0) | 716 | if ((mainByte & 0x10) != 0) |
707 | { | 717 | { |
708 | UInt32 coderOutStreams; | 718 | UInt32 coderOutStreams; |
709 | RINOK(SzReadNumber32(&sd, &coderInStreams)); | 719 | RINOK(SzReadNumber32(&sd, &coderInStreams)) |
710 | RINOK(SzReadNumber32(&sd, &coderOutStreams)); | 720 | RINOK(SzReadNumber32(&sd, &coderOutStreams)) |
711 | if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1) | 721 | if (coderInStreams > k_Scan_NumCodersStreams_in_Folder_MAX || coderOutStreams != 1) |
712 | return SZ_ERROR_UNSUPPORTED; | 722 | return SZ_ERROR_UNSUPPORTED; |
713 | } | 723 | } |
@@ -717,10 +727,10 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
717 | if ((mainByte & 0x20) != 0) | 727 | if ((mainByte & 0x20) != 0) |
718 | { | 728 | { |
719 | UInt32 propsSize; | 729 | UInt32 propsSize; |
720 | RINOK(SzReadNumber32(&sd, &propsSize)); | 730 | RINOK(SzReadNumber32(&sd, &propsSize)) |
721 | if (propsSize > sd.Size) | 731 | if (propsSize > sd.Size) |
722 | return SZ_ERROR_ARCHIVE; | 732 | return SZ_ERROR_ARCHIVE; |
723 | SKIP_DATA2(sd, propsSize); | 733 | SKIP_DATA2(sd, propsSize) |
724 | } | 734 | } |
725 | } | 735 | } |
726 | 736 | ||
@@ -734,7 +744,7 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
734 | Byte coderUsed[k_Scan_NumCoders_MAX]; | 744 | Byte coderUsed[k_Scan_NumCoders_MAX]; |
735 | 745 | ||
736 | UInt32 i; | 746 | UInt32 i; |
737 | UInt32 numBonds = numCoders - 1; | 747 | const UInt32 numBonds = numCoders - 1; |
738 | if (numInStreams < numBonds) | 748 | if (numInStreams < numBonds) |
739 | return SZ_ERROR_ARCHIVE; | 749 | return SZ_ERROR_ARCHIVE; |
740 | 750 | ||
@@ -750,12 +760,12 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
750 | { | 760 | { |
751 | UInt32 index; | 761 | UInt32 index; |
752 | 762 | ||
753 | RINOK(SzReadNumber32(&sd, &index)); | 763 | RINOK(SzReadNumber32(&sd, &index)) |
754 | if (index >= numInStreams || streamUsed[index]) | 764 | if (index >= numInStreams || streamUsed[index]) |
755 | return SZ_ERROR_ARCHIVE; | 765 | return SZ_ERROR_ARCHIVE; |
756 | streamUsed[index] = True; | 766 | streamUsed[index] = True; |
757 | 767 | ||
758 | RINOK(SzReadNumber32(&sd, &index)); | 768 | RINOK(SzReadNumber32(&sd, &index)) |
759 | if (index >= numCoders || coderUsed[index]) | 769 | if (index >= numCoders || coderUsed[index]) |
760 | return SZ_ERROR_ARCHIVE; | 770 | return SZ_ERROR_ARCHIVE; |
761 | coderUsed[index] = True; | 771 | coderUsed[index] = True; |
@@ -767,7 +777,7 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
767 | for (i = 0; i < numPackStreams; i++) | 777 | for (i = 0; i < numPackStreams; i++) |
768 | { | 778 | { |
769 | UInt32 index; | 779 | UInt32 index; |
770 | RINOK(SzReadNumber32(&sd, &index)); | 780 | RINOK(SzReadNumber32(&sd, &index)) |
771 | if (index >= numInStreams || streamUsed[index]) | 781 | if (index >= numInStreams || streamUsed[index]) |
772 | return SZ_ERROR_ARCHIVE; | 782 | return SZ_ERROR_ARCHIVE; |
773 | streamUsed[index] = True; | 783 | streamUsed[index] = True; |
@@ -802,7 +812,7 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
802 | const size_t dataSize = (size_t)(sd.Data - startBufPtr); | 812 | const size_t dataSize = (size_t)(sd.Data - startBufPtr); |
803 | p->FoStartPackStreamIndex[fo] = packStreamIndex; | 813 | p->FoStartPackStreamIndex[fo] = packStreamIndex; |
804 | p->FoCodersOffsets[fo] = dataSize; | 814 | p->FoCodersOffsets[fo] = dataSize; |
805 | MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc); | 815 | MY_ALLOC_ZE_AND_CPY(p->CodersData, dataSize, startBufPtr, alloc) |
806 | } | 816 | } |
807 | 817 | ||
808 | if (external != 0) | 818 | if (external != 0) |
@@ -812,21 +822,21 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
812 | sd = *sd2; | 822 | sd = *sd2; |
813 | } | 823 | } |
814 | 824 | ||
815 | RINOK(WaitId(&sd, k7zIdCodersUnpackSize)); | 825 | RINOK(WaitId(&sd, k7zIdCodersUnpackSize)) |
816 | 826 | ||
817 | MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc); | 827 | MY_ALLOC_ZE(UInt64, p->CoderUnpackSizes, (size_t)numCodersOutStreams, alloc) |
818 | { | 828 | { |
819 | UInt32 i; | 829 | UInt32 i; |
820 | for (i = 0; i < numCodersOutStreams; i++) | 830 | for (i = 0; i < numCodersOutStreams; i++) |
821 | { | 831 | { |
822 | RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i)); | 832 | RINOK(ReadNumber(&sd, p->CoderUnpackSizes + i)) |
823 | } | 833 | } |
824 | } | 834 | } |
825 | 835 | ||
826 | for (;;) | 836 | for (;;) |
827 | { | 837 | { |
828 | UInt64 type; | 838 | UInt64 type; |
829 | RINOK(ReadID(&sd, &type)); | 839 | RINOK(ReadID(&sd, &type)) |
830 | if (type == k7zIdEnd) | 840 | if (type == k7zIdEnd) |
831 | { | 841 | { |
832 | *sd2 = sd; | 842 | *sd2 = sd; |
@@ -834,10 +844,10 @@ static SRes ReadUnpackInfo(CSzAr *p, | |||
834 | } | 844 | } |
835 | if (type == k7zIdCRC) | 845 | if (type == k7zIdCRC) |
836 | { | 846 | { |
837 | RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc)); | 847 | RINOK(ReadBitUi32s(&sd, numFolders, &p->FolderCRCs, alloc)) |
838 | continue; | 848 | continue; |
839 | } | 849 | } |
840 | RINOK(SkipData(&sd)); | 850 | RINOK(SkipData(&sd)) |
841 | } | 851 | } |
842 | } | 852 | } |
843 | 853 | ||
@@ -862,13 +872,13 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi) | |||
862 | { | 872 | { |
863 | UInt64 type = 0; | 873 | UInt64 type = 0; |
864 | UInt32 numSubDigests = 0; | 874 | UInt32 numSubDigests = 0; |
865 | UInt32 numFolders = p->NumFolders; | 875 | const UInt32 numFolders = p->NumFolders; |
866 | UInt32 numUnpackStreams = numFolders; | 876 | UInt32 numUnpackStreams = numFolders; |
867 | UInt32 numUnpackSizesInData = 0; | 877 | UInt32 numUnpackSizesInData = 0; |
868 | 878 | ||
869 | for (;;) | 879 | for (;;) |
870 | { | 880 | { |
871 | RINOK(ReadID(sd, &type)); | 881 | RINOK(ReadID(sd, &type)) |
872 | if (type == k7zIdNumUnpackStream) | 882 | if (type == k7zIdNumUnpackStream) |
873 | { | 883 | { |
874 | UInt32 i; | 884 | UInt32 i; |
@@ -878,7 +888,7 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi) | |||
878 | for (i = 0; i < numFolders; i++) | 888 | for (i = 0; i < numFolders; i++) |
879 | { | 889 | { |
880 | UInt32 numStreams; | 890 | UInt32 numStreams; |
881 | RINOK(SzReadNumber32(sd, &numStreams)); | 891 | RINOK(SzReadNumber32(sd, &numStreams)) |
882 | if (numUnpackStreams > numUnpackStreams + numStreams) | 892 | if (numUnpackStreams > numUnpackStreams + numStreams) |
883 | return SZ_ERROR_UNSUPPORTED; | 893 | return SZ_ERROR_UNSUPPORTED; |
884 | numUnpackStreams += numStreams; | 894 | numUnpackStreams += numStreams; |
@@ -892,7 +902,7 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi) | |||
892 | } | 902 | } |
893 | if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd) | 903 | if (type == k7zIdCRC || type == k7zIdSize || type == k7zIdEnd) |
894 | break; | 904 | break; |
895 | RINOK(SkipData(sd)); | 905 | RINOK(SkipData(sd)) |
896 | } | 906 | } |
897 | 907 | ||
898 | if (!ssi->sdNumSubStreams.Data) | 908 | if (!ssi->sdNumSubStreams.Data) |
@@ -908,9 +918,9 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi) | |||
908 | if (type == k7zIdSize) | 918 | if (type == k7zIdSize) |
909 | { | 919 | { |
910 | ssi->sdSizes.Data = sd->Data; | 920 | ssi->sdSizes.Data = sd->Data; |
911 | RINOK(SkipNumbers(sd, numUnpackSizesInData)); | 921 | RINOK(SkipNumbers(sd, numUnpackSizesInData)) |
912 | ssi->sdSizes.Size = (size_t)(sd->Data - ssi->sdSizes.Data); | 922 | ssi->sdSizes.Size = (size_t)(sd->Data - ssi->sdSizes.Data); |
913 | RINOK(ReadID(sd, &type)); | 923 | RINOK(ReadID(sd, &type)) |
914 | } | 924 | } |
915 | 925 | ||
916 | for (;;) | 926 | for (;;) |
@@ -920,14 +930,14 @@ static SRes ReadSubStreamsInfo(CSzAr *p, CSzData *sd, CSubStreamInfo *ssi) | |||
920 | if (type == k7zIdCRC) | 930 | if (type == k7zIdCRC) |
921 | { | 931 | { |
922 | ssi->sdCRCs.Data = sd->Data; | 932 | ssi->sdCRCs.Data = sd->Data; |
923 | RINOK(SkipBitUi32s(sd, numSubDigests)); | 933 | RINOK(SkipBitUi32s(sd, numSubDigests)) |
924 | ssi->sdCRCs.Size = (size_t)(sd->Data - ssi->sdCRCs.Data); | 934 | ssi->sdCRCs.Size = (size_t)(sd->Data - ssi->sdCRCs.Data); |
925 | } | 935 | } |
926 | else | 936 | else |
927 | { | 937 | { |
928 | RINOK(SkipData(sd)); | 938 | RINOK(SkipData(sd)) |
929 | } | 939 | } |
930 | RINOK(ReadID(sd, &type)); | 940 | RINOK(ReadID(sd, &type)) |
931 | } | 941 | } |
932 | } | 942 | } |
933 | 943 | ||
@@ -940,31 +950,31 @@ static SRes SzReadStreamsInfo(CSzAr *p, | |||
940 | { | 950 | { |
941 | UInt64 type; | 951 | UInt64 type; |
942 | 952 | ||
943 | SzData_Clear(&ssi->sdSizes); | 953 | SzData_CLEAR(&ssi->sdSizes) |
944 | SzData_Clear(&ssi->sdCRCs); | 954 | SzData_CLEAR(&ssi->sdCRCs) |
945 | SzData_Clear(&ssi->sdNumSubStreams); | 955 | SzData_CLEAR(&ssi->sdNumSubStreams) |
946 | 956 | ||
947 | *dataOffset = 0; | 957 | *dataOffset = 0; |
948 | RINOK(ReadID(sd, &type)); | 958 | RINOK(ReadID(sd, &type)) |
949 | if (type == k7zIdPackInfo) | 959 | if (type == k7zIdPackInfo) |
950 | { | 960 | { |
951 | RINOK(ReadNumber(sd, dataOffset)); | 961 | RINOK(ReadNumber(sd, dataOffset)) |
952 | if (*dataOffset > p->RangeLimit) | 962 | if (*dataOffset > p->RangeLimit) |
953 | return SZ_ERROR_ARCHIVE; | 963 | return SZ_ERROR_ARCHIVE; |
954 | RINOK(ReadPackInfo(p, sd, alloc)); | 964 | RINOK(ReadPackInfo(p, sd, alloc)) |
955 | if (p->PackPositions[p->NumPackStreams] > p->RangeLimit - *dataOffset) | 965 | if (p->PackPositions[p->NumPackStreams] > p->RangeLimit - *dataOffset) |
956 | return SZ_ERROR_ARCHIVE; | 966 | return SZ_ERROR_ARCHIVE; |
957 | RINOK(ReadID(sd, &type)); | 967 | RINOK(ReadID(sd, &type)) |
958 | } | 968 | } |
959 | if (type == k7zIdUnpackInfo) | 969 | if (type == k7zIdUnpackInfo) |
960 | { | 970 | { |
961 | RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc)); | 971 | RINOK(ReadUnpackInfo(p, sd, numFoldersMax, tempBufs, numTempBufs, alloc)) |
962 | RINOK(ReadID(sd, &type)); | 972 | RINOK(ReadID(sd, &type)) |
963 | } | 973 | } |
964 | if (type == k7zIdSubStreamsInfo) | 974 | if (type == k7zIdSubStreamsInfo) |
965 | { | 975 | { |
966 | RINOK(ReadSubStreamsInfo(p, sd, ssi)); | 976 | RINOK(ReadSubStreamsInfo(p, sd, ssi)) |
967 | RINOK(ReadID(sd, &type)); | 977 | RINOK(ReadID(sd, &type)) |
968 | } | 978 | } |
969 | else | 979 | else |
970 | { | 980 | { |
@@ -976,7 +986,7 @@ static SRes SzReadStreamsInfo(CSzAr *p, | |||
976 | } | 986 | } |
977 | 987 | ||
978 | static SRes SzReadAndDecodePackedStreams( | 988 | static SRes SzReadAndDecodePackedStreams( |
979 | ILookInStream *inStream, | 989 | ILookInStreamPtr inStream, |
980 | CSzData *sd, | 990 | CSzData *sd, |
981 | CBuf *tempBufs, | 991 | CBuf *tempBufs, |
982 | UInt32 numFoldersMax, | 992 | UInt32 numFoldersMax, |
@@ -988,7 +998,7 @@ static SRes SzReadAndDecodePackedStreams( | |||
988 | UInt32 fo; | 998 | UInt32 fo; |
989 | CSubStreamInfo ssi; | 999 | CSubStreamInfo ssi; |
990 | 1000 | ||
991 | RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp)); | 1001 | RINOK(SzReadStreamsInfo(p, sd, numFoldersMax, NULL, 0, &dataStartPos, &ssi, allocTemp)) |
992 | 1002 | ||
993 | dataStartPos += baseOffset; | 1003 | dataStartPos += baseOffset; |
994 | if (p->NumFolders == 0) | 1004 | if (p->NumFolders == 0) |
@@ -1000,7 +1010,7 @@ static SRes SzReadAndDecodePackedStreams( | |||
1000 | for (fo = 0; fo < p->NumFolders; fo++) | 1010 | for (fo = 0; fo < p->NumFolders; fo++) |
1001 | { | 1011 | { |
1002 | CBuf *tempBuf = tempBufs + fo; | 1012 | CBuf *tempBuf = tempBufs + fo; |
1003 | UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo); | 1013 | const UInt64 unpackSize = SzAr_GetFolderUnpackSize(p, fo); |
1004 | if ((size_t)unpackSize != unpackSize) | 1014 | if ((size_t)unpackSize != unpackSize) |
1005 | return SZ_ERROR_MEM; | 1015 | return SZ_ERROR_MEM; |
1006 | if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp)) | 1016 | if (!Buf_Create(tempBuf, (size_t)unpackSize, allocTemp)) |
@@ -1010,8 +1020,8 @@ static SRes SzReadAndDecodePackedStreams( | |||
1010 | for (fo = 0; fo < p->NumFolders; fo++) | 1020 | for (fo = 0; fo < p->NumFolders; fo++) |
1011 | { | 1021 | { |
1012 | const CBuf *tempBuf = tempBufs + fo; | 1022 | const CBuf *tempBuf = tempBufs + fo; |
1013 | RINOK(LookInStream_SeekTo(inStream, dataStartPos)); | 1023 | RINOK(LookInStream_SeekTo(inStream, dataStartPos)) |
1014 | RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp)); | 1024 | RINOK(SzAr_DecodeFolder(p, fo, inStream, dataStartPos, tempBuf->data, tempBuf->size, allocTemp)) |
1015 | } | 1025 | } |
1016 | 1026 | ||
1017 | return SZ_OK; | 1027 | return SZ_OK; |
@@ -1046,7 +1056,7 @@ static SRes SzReadFileNames(const Byte *data, size_t size, UInt32 numFiles, size | |||
1046 | return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE; | 1056 | return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE; |
1047 | } | 1057 | } |
1048 | 1058 | ||
1049 | static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, | 1059 | static Z7_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, |
1050 | CSzData *sd2, | 1060 | CSzData *sd2, |
1051 | const CBuf *tempBufs, UInt32 numTempBufs, | 1061 | const CBuf *tempBufs, UInt32 numTempBufs, |
1052 | ISzAllocPtr alloc) | 1062 | ISzAllocPtr alloc) |
@@ -1057,22 +1067,22 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, | |||
1057 | Byte *defs; | 1067 | Byte *defs; |
1058 | Byte external; | 1068 | Byte external; |
1059 | 1069 | ||
1060 | RINOK(ReadBitVector(sd2, num, &p->Defs, alloc)); | 1070 | RINOK(ReadBitVector(sd2, num, &p->Defs, alloc)) |
1061 | 1071 | ||
1062 | SZ_READ_BYTE_SD(sd2, external); | 1072 | SZ_READ_BYTE_SD(sd2, external) |
1063 | if (external == 0) | 1073 | if (external == 0) |
1064 | sd = *sd2; | 1074 | sd = *sd2; |
1065 | else | 1075 | else |
1066 | { | 1076 | { |
1067 | UInt32 index; | 1077 | UInt32 index; |
1068 | RINOK(SzReadNumber32(sd2, &index)); | 1078 | RINOK(SzReadNumber32(sd2, &index)) |
1069 | if (index >= numTempBufs) | 1079 | if (index >= numTempBufs) |
1070 | return SZ_ERROR_ARCHIVE; | 1080 | return SZ_ERROR_ARCHIVE; |
1071 | sd.Data = tempBufs[index].data; | 1081 | sd.Data = tempBufs[index].data; |
1072 | sd.Size = tempBufs[index].size; | 1082 | sd.Size = tempBufs[index].size; |
1073 | } | 1083 | } |
1074 | 1084 | ||
1075 | MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc); | 1085 | MY_ALLOC_ZE(CNtfsFileTime, p->Vals, num, alloc) |
1076 | vals = p->Vals; | 1086 | vals = p->Vals; |
1077 | defs = p->Defs; | 1087 | defs = p->Defs; |
1078 | for (i = 0; i < num; i++) | 1088 | for (i = 0; i < num; i++) |
@@ -1082,7 +1092,7 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, | |||
1082 | return SZ_ERROR_ARCHIVE; | 1092 | return SZ_ERROR_ARCHIVE; |
1083 | vals[i].Low = GetUi32(sd.Data); | 1093 | vals[i].Low = GetUi32(sd.Data); |
1084 | vals[i].High = GetUi32(sd.Data + 4); | 1094 | vals[i].High = GetUi32(sd.Data + 4); |
1085 | SKIP_DATA2(sd, 8); | 1095 | SKIP_DATA2(sd, 8) |
1086 | } | 1096 | } |
1087 | else | 1097 | else |
1088 | vals[i].High = vals[i].Low = 0; | 1098 | vals[i].High = vals[i].Low = 0; |
@@ -1100,7 +1110,7 @@ static MY_NO_INLINE SRes ReadTime(CSzBitUi64s *p, UInt32 num, | |||
1100 | static SRes SzReadHeader2( | 1110 | static SRes SzReadHeader2( |
1101 | CSzArEx *p, /* allocMain */ | 1111 | CSzArEx *p, /* allocMain */ |
1102 | CSzData *sd, | 1112 | CSzData *sd, |
1103 | ILookInStream *inStream, | 1113 | ILookInStreamPtr inStream, |
1104 | CBuf *tempBufs, UInt32 *numTempBufs, | 1114 | CBuf *tempBufs, UInt32 *numTempBufs, |
1105 | ISzAllocPtr allocMain, | 1115 | ISzAllocPtr allocMain, |
1106 | ISzAllocPtr allocTemp | 1116 | ISzAllocPtr allocTemp |
@@ -1111,26 +1121,26 @@ static SRes SzReadHeader2( | |||
1111 | { | 1121 | { |
1112 | UInt64 type; | 1122 | UInt64 type; |
1113 | 1123 | ||
1114 | SzData_Clear(&ssi.sdSizes); | 1124 | SzData_CLEAR(&ssi.sdSizes) |
1115 | SzData_Clear(&ssi.sdCRCs); | 1125 | SzData_CLEAR(&ssi.sdCRCs) |
1116 | SzData_Clear(&ssi.sdNumSubStreams); | 1126 | SzData_CLEAR(&ssi.sdNumSubStreams) |
1117 | 1127 | ||
1118 | ssi.NumSubDigests = 0; | 1128 | ssi.NumSubDigests = 0; |
1119 | ssi.NumTotalSubStreams = 0; | 1129 | ssi.NumTotalSubStreams = 0; |
1120 | 1130 | ||
1121 | RINOK(ReadID(sd, &type)); | 1131 | RINOK(ReadID(sd, &type)) |
1122 | 1132 | ||
1123 | if (type == k7zIdArchiveProperties) | 1133 | if (type == k7zIdArchiveProperties) |
1124 | { | 1134 | { |
1125 | for (;;) | 1135 | for (;;) |
1126 | { | 1136 | { |
1127 | UInt64 type2; | 1137 | UInt64 type2; |
1128 | RINOK(ReadID(sd, &type2)); | 1138 | RINOK(ReadID(sd, &type2)) |
1129 | if (type2 == k7zIdEnd) | 1139 | if (type2 == k7zIdEnd) |
1130 | break; | 1140 | break; |
1131 | RINOK(SkipData(sd)); | 1141 | RINOK(SkipData(sd)) |
1132 | } | 1142 | } |
1133 | RINOK(ReadID(sd, &type)); | 1143 | RINOK(ReadID(sd, &type)) |
1134 | } | 1144 | } |
1135 | 1145 | ||
1136 | if (type == k7zIdAdditionalStreamsInfo) | 1146 | if (type == k7zIdAdditionalStreamsInfo) |
@@ -1148,15 +1158,15 @@ static SRes SzReadHeader2( | |||
1148 | 1158 | ||
1149 | if (res != SZ_OK) | 1159 | if (res != SZ_OK) |
1150 | return res; | 1160 | return res; |
1151 | RINOK(ReadID(sd, &type)); | 1161 | RINOK(ReadID(sd, &type)) |
1152 | } | 1162 | } |
1153 | 1163 | ||
1154 | if (type == k7zIdMainStreamsInfo) | 1164 | if (type == k7zIdMainStreamsInfo) |
1155 | { | 1165 | { |
1156 | RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs, | 1166 | RINOK(SzReadStreamsInfo(&p->db, sd, (UInt32)1 << 30, tempBufs, *numTempBufs, |
1157 | &p->dataPos, &ssi, allocMain)); | 1167 | &p->dataPos, &ssi, allocMain)) |
1158 | p->dataPos += p->startPosAfterHeader; | 1168 | p->dataPos += p->startPosAfterHeader; |
1159 | RINOK(ReadID(sd, &type)); | 1169 | RINOK(ReadID(sd, &type)) |
1160 | } | 1170 | } |
1161 | 1171 | ||
1162 | if (type == k7zIdEnd) | 1172 | if (type == k7zIdEnd) |
@@ -1174,23 +1184,23 @@ static SRes SzReadHeader2( | |||
1174 | const Byte *emptyStreams = NULL; | 1184 | const Byte *emptyStreams = NULL; |
1175 | const Byte *emptyFiles = NULL; | 1185 | const Byte *emptyFiles = NULL; |
1176 | 1186 | ||
1177 | RINOK(SzReadNumber32(sd, &numFiles)); | 1187 | RINOK(SzReadNumber32(sd, &numFiles)) |
1178 | p->NumFiles = numFiles; | 1188 | p->NumFiles = numFiles; |
1179 | 1189 | ||
1180 | for (;;) | 1190 | for (;;) |
1181 | { | 1191 | { |
1182 | UInt64 type; | 1192 | UInt64 type; |
1183 | UInt64 size; | 1193 | UInt64 size; |
1184 | RINOK(ReadID(sd, &type)); | 1194 | RINOK(ReadID(sd, &type)) |
1185 | if (type == k7zIdEnd) | 1195 | if (type == k7zIdEnd) |
1186 | break; | 1196 | break; |
1187 | RINOK(ReadNumber(sd, &size)); | 1197 | RINOK(ReadNumber(sd, &size)) |
1188 | if (size > sd->Size) | 1198 | if (size > sd->Size) |
1189 | return SZ_ERROR_ARCHIVE; | 1199 | return SZ_ERROR_ARCHIVE; |
1190 | 1200 | ||
1191 | if (type >= ((UInt32)1 << 8)) | 1201 | if (type >= ((UInt32)1 << 8)) |
1192 | { | 1202 | { |
1193 | SKIP_DATA(sd, size); | 1203 | SKIP_DATA(sd, size) |
1194 | } | 1204 | } |
1195 | else switch ((unsigned)type) | 1205 | else switch ((unsigned)type) |
1196 | { | 1206 | { |
@@ -1200,7 +1210,7 @@ static SRes SzReadHeader2( | |||
1200 | const Byte *namesData; | 1210 | const Byte *namesData; |
1201 | Byte external; | 1211 | Byte external; |
1202 | 1212 | ||
1203 | SZ_READ_BYTE(external); | 1213 | SZ_READ_BYTE(external) |
1204 | if (external == 0) | 1214 | if (external == 0) |
1205 | { | 1215 | { |
1206 | namesSize = (size_t)size - 1; | 1216 | namesSize = (size_t)size - 1; |
@@ -1209,7 +1219,7 @@ static SRes SzReadHeader2( | |||
1209 | else | 1219 | else |
1210 | { | 1220 | { |
1211 | UInt32 index; | 1221 | UInt32 index; |
1212 | RINOK(SzReadNumber32(sd, &index)); | 1222 | RINOK(SzReadNumber32(sd, &index)) |
1213 | if (index >= *numTempBufs) | 1223 | if (index >= *numTempBufs) |
1214 | return SZ_ERROR_ARCHIVE; | 1224 | return SZ_ERROR_ARCHIVE; |
1215 | namesData = (tempBufs)[index].data; | 1225 | namesData = (tempBufs)[index].data; |
@@ -1218,25 +1228,25 @@ static SRes SzReadHeader2( | |||
1218 | 1228 | ||
1219 | if ((namesSize & 1) != 0) | 1229 | if ((namesSize & 1) != 0) |
1220 | return SZ_ERROR_ARCHIVE; | 1230 | return SZ_ERROR_ARCHIVE; |
1221 | MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain); | 1231 | MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain) |
1222 | MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain); | 1232 | MY_ALLOC_ZE_AND_CPY(p->FileNames, namesSize, namesData, allocMain) |
1223 | RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets)) | 1233 | RINOK(SzReadFileNames(p->FileNames, namesSize, numFiles, p->FileNameOffsets)) |
1224 | if (external == 0) | 1234 | if (external == 0) |
1225 | { | 1235 | { |
1226 | SKIP_DATA(sd, namesSize); | 1236 | SKIP_DATA(sd, namesSize) |
1227 | } | 1237 | } |
1228 | break; | 1238 | break; |
1229 | } | 1239 | } |
1230 | case k7zIdEmptyStream: | 1240 | case k7zIdEmptyStream: |
1231 | { | 1241 | { |
1232 | RINOK(RememberBitVector(sd, numFiles, &emptyStreams)); | 1242 | RINOK(RememberBitVector(sd, numFiles, &emptyStreams)) |
1233 | numEmptyStreams = CountDefinedBits(emptyStreams, numFiles); | 1243 | numEmptyStreams = CountDefinedBits(emptyStreams, numFiles); |
1234 | emptyFiles = NULL; | 1244 | emptyFiles = NULL; |
1235 | break; | 1245 | break; |
1236 | } | 1246 | } |
1237 | case k7zIdEmptyFile: | 1247 | case k7zIdEmptyFile: |
1238 | { | 1248 | { |
1239 | RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles)); | 1249 | RINOK(RememberBitVector(sd, numEmptyStreams, &emptyFiles)) |
1240 | break; | 1250 | break; |
1241 | } | 1251 | } |
1242 | case k7zIdWinAttrib: | 1252 | case k7zIdWinAttrib: |
@@ -1245,22 +1255,22 @@ static SRes SzReadHeader2( | |||
1245 | CSzData sdSwitch; | 1255 | CSzData sdSwitch; |
1246 | CSzData *sdPtr; | 1256 | CSzData *sdPtr; |
1247 | SzBitUi32s_Free(&p->Attribs, allocMain); | 1257 | SzBitUi32s_Free(&p->Attribs, allocMain); |
1248 | RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain)); | 1258 | RINOK(ReadBitVector(sd, numFiles, &p->Attribs.Defs, allocMain)) |
1249 | 1259 | ||
1250 | SZ_READ_BYTE(external); | 1260 | SZ_READ_BYTE(external) |
1251 | if (external == 0) | 1261 | if (external == 0) |
1252 | sdPtr = sd; | 1262 | sdPtr = sd; |
1253 | else | 1263 | else |
1254 | { | 1264 | { |
1255 | UInt32 index; | 1265 | UInt32 index; |
1256 | RINOK(SzReadNumber32(sd, &index)); | 1266 | RINOK(SzReadNumber32(sd, &index)) |
1257 | if (index >= *numTempBufs) | 1267 | if (index >= *numTempBufs) |
1258 | return SZ_ERROR_ARCHIVE; | 1268 | return SZ_ERROR_ARCHIVE; |
1259 | sdSwitch.Data = (tempBufs)[index].data; | 1269 | sdSwitch.Data = (tempBufs)[index].data; |
1260 | sdSwitch.Size = (tempBufs)[index].size; | 1270 | sdSwitch.Size = (tempBufs)[index].size; |
1261 | sdPtr = &sdSwitch; | 1271 | sdPtr = &sdSwitch; |
1262 | } | 1272 | } |
1263 | RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain)); | 1273 | RINOK(ReadUi32s(sdPtr, numFiles, &p->Attribs, allocMain)) |
1264 | break; | 1274 | break; |
1265 | } | 1275 | } |
1266 | /* | 1276 | /* |
@@ -1273,11 +1283,11 @@ static SRes SzReadHeader2( | |||
1273 | break; | 1283 | break; |
1274 | } | 1284 | } |
1275 | */ | 1285 | */ |
1276 | case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break; | 1286 | case k7zIdMTime: RINOK(ReadTime(&p->MTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)) break; |
1277 | case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)); break; | 1287 | case k7zIdCTime: RINOK(ReadTime(&p->CTime, numFiles, sd, tempBufs, *numTempBufs, allocMain)) break; |
1278 | default: | 1288 | default: |
1279 | { | 1289 | { |
1280 | SKIP_DATA(sd, size); | 1290 | SKIP_DATA(sd, size) |
1281 | } | 1291 | } |
1282 | } | 1292 | } |
1283 | } | 1293 | } |
@@ -1288,10 +1298,10 @@ static SRes SzReadHeader2( | |||
1288 | for (;;) | 1298 | for (;;) |
1289 | { | 1299 | { |
1290 | UInt64 type; | 1300 | UInt64 type; |
1291 | RINOK(ReadID(sd, &type)); | 1301 | RINOK(ReadID(sd, &type)) |
1292 | if (type == k7zIdEnd) | 1302 | if (type == k7zIdEnd) |
1293 | break; | 1303 | break; |
1294 | RINOK(SkipData(sd)); | 1304 | RINOK(SkipData(sd)) |
1295 | } | 1305 | } |
1296 | 1306 | ||
1297 | { | 1307 | { |
@@ -1303,40 +1313,37 @@ static SRes SzReadHeader2( | |||
1303 | UInt64 unpackPos = 0; | 1313 | UInt64 unpackPos = 0; |
1304 | const Byte *digestsDefs = NULL; | 1314 | const Byte *digestsDefs = NULL; |
1305 | const Byte *digestsVals = NULL; | 1315 | const Byte *digestsVals = NULL; |
1306 | UInt32 digestsValsIndex = 0; | 1316 | UInt32 digestIndex = 0; |
1307 | UInt32 digestIndex; | ||
1308 | Byte allDigestsDefined = 0; | ||
1309 | Byte isDirMask = 0; | 1317 | Byte isDirMask = 0; |
1310 | Byte crcMask = 0; | 1318 | Byte crcMask = 0; |
1311 | Byte mask = 0x80; | 1319 | Byte mask = 0x80; |
1312 | 1320 | ||
1313 | MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain); | 1321 | MY_ALLOC(UInt32, p->FolderToFile, p->db.NumFolders + 1, allocMain) |
1314 | MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain); | 1322 | MY_ALLOC_ZE(UInt32, p->FileToFolder, p->NumFiles, allocMain) |
1315 | MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain); | 1323 | MY_ALLOC(UInt64, p->UnpackPositions, p->NumFiles + 1, allocMain) |
1316 | MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain); | 1324 | MY_ALLOC_ZE(Byte, p->IsDirs, (p->NumFiles + 7) >> 3, allocMain) |
1317 | 1325 | ||
1318 | RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain)); | 1326 | RINOK(SzBitUi32s_Alloc(&p->CRCs, p->NumFiles, allocMain)) |
1319 | 1327 | ||
1320 | if (ssi.sdCRCs.Size != 0) | 1328 | if (ssi.sdCRCs.Size != 0) |
1321 | { | 1329 | { |
1322 | SZ_READ_BYTE_SD(&ssi.sdCRCs, allDigestsDefined); | 1330 | Byte allDigestsDefined = 0; |
1331 | SZ_READ_BYTE_SD_NOCHECK(&ssi.sdCRCs, allDigestsDefined) | ||
1323 | if (allDigestsDefined) | 1332 | if (allDigestsDefined) |
1324 | digestsVals = ssi.sdCRCs.Data; | 1333 | digestsVals = ssi.sdCRCs.Data; |
1325 | else | 1334 | else |
1326 | { | 1335 | { |
1327 | size_t numBytes = (ssi.NumSubDigests + 7) >> 3; | 1336 | const size_t numBytes = (ssi.NumSubDigests + 7) >> 3; |
1328 | digestsDefs = ssi.sdCRCs.Data; | 1337 | digestsDefs = ssi.sdCRCs.Data; |
1329 | digestsVals = digestsDefs + numBytes; | 1338 | digestsVals = digestsDefs + numBytes; |
1330 | } | 1339 | } |
1331 | } | 1340 | } |
1332 | 1341 | ||
1333 | digestIndex = 0; | ||
1334 | |||
1335 | for (i = 0; i < numFiles; i++, mask >>= 1) | 1342 | for (i = 0; i < numFiles; i++, mask >>= 1) |
1336 | { | 1343 | { |
1337 | if (mask == 0) | 1344 | if (mask == 0) |
1338 | { | 1345 | { |
1339 | UInt32 byteIndex = (i - 1) >> 3; | 1346 | const UInt32 byteIndex = (i - 1) >> 3; |
1340 | p->IsDirs[byteIndex] = isDirMask; | 1347 | p->IsDirs[byteIndex] = isDirMask; |
1341 | p->CRCs.Defs[byteIndex] = crcMask; | 1348 | p->CRCs.Defs[byteIndex] = crcMask; |
1342 | isDirMask = 0; | 1349 | isDirMask = 0; |
@@ -1374,18 +1381,17 @@ static SRes SzReadHeader2( | |||
1374 | numSubStreams = 1; | 1381 | numSubStreams = 1; |
1375 | if (ssi.sdNumSubStreams.Data) | 1382 | if (ssi.sdNumSubStreams.Data) |
1376 | { | 1383 | { |
1377 | RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)); | 1384 | RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)) |
1378 | } | 1385 | } |
1379 | remSubStreams = numSubStreams; | 1386 | remSubStreams = numSubStreams; |
1380 | if (numSubStreams != 0) | 1387 | if (numSubStreams != 0) |
1381 | break; | 1388 | break; |
1382 | { | 1389 | { |
1383 | UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); | 1390 | const UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); |
1384 | unpackPos += folderUnpackSize; | 1391 | unpackPos += folderUnpackSize; |
1385 | if (unpackPos < folderUnpackSize) | 1392 | if (unpackPos < folderUnpackSize) |
1386 | return SZ_ERROR_ARCHIVE; | 1393 | return SZ_ERROR_ARCHIVE; |
1387 | } | 1394 | } |
1388 | |||
1389 | folderIndex++; | 1395 | folderIndex++; |
1390 | } | 1396 | } |
1391 | } | 1397 | } |
@@ -1397,47 +1403,44 @@ static SRes SzReadHeader2( | |||
1397 | 1403 | ||
1398 | if (--remSubStreams == 0) | 1404 | if (--remSubStreams == 0) |
1399 | { | 1405 | { |
1400 | UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); | 1406 | const UInt64 folderUnpackSize = SzAr_GetFolderUnpackSize(&p->db, folderIndex); |
1401 | UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]]; | 1407 | const UInt64 startFolderUnpackPos = p->UnpackPositions[p->FolderToFile[folderIndex]]; |
1402 | if (folderUnpackSize < unpackPos - startFolderUnpackPos) | 1408 | if (folderUnpackSize < unpackPos - startFolderUnpackPos) |
1403 | return SZ_ERROR_ARCHIVE; | 1409 | return SZ_ERROR_ARCHIVE; |
1404 | unpackPos = startFolderUnpackPos + folderUnpackSize; | 1410 | unpackPos = startFolderUnpackPos + folderUnpackSize; |
1405 | if (unpackPos < folderUnpackSize) | 1411 | if (unpackPos < folderUnpackSize) |
1406 | return SZ_ERROR_ARCHIVE; | 1412 | return SZ_ERROR_ARCHIVE; |
1407 | 1413 | ||
1408 | if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, i)) | 1414 | if (numSubStreams == 1 && SzBitWithVals_Check(&p->db.FolderCRCs, folderIndex)) |
1409 | { | 1415 | { |
1410 | p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex]; | 1416 | p->CRCs.Vals[i] = p->db.FolderCRCs.Vals[folderIndex]; |
1411 | crcMask |= mask; | 1417 | crcMask |= mask; |
1412 | } | 1418 | } |
1413 | else if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex))) | ||
1414 | { | ||
1415 | p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4); | ||
1416 | digestsValsIndex++; | ||
1417 | crcMask |= mask; | ||
1418 | } | ||
1419 | |||
1420 | folderIndex++; | 1419 | folderIndex++; |
1421 | } | 1420 | } |
1422 | else | 1421 | else |
1423 | { | 1422 | { |
1424 | UInt64 v; | 1423 | UInt64 v; |
1425 | RINOK(ReadNumber(&ssi.sdSizes, &v)); | 1424 | RINOK(ReadNumber(&ssi.sdSizes, &v)) |
1426 | unpackPos += v; | 1425 | unpackPos += v; |
1427 | if (unpackPos < v) | 1426 | if (unpackPos < v) |
1428 | return SZ_ERROR_ARCHIVE; | 1427 | return SZ_ERROR_ARCHIVE; |
1429 | if (allDigestsDefined || (digestsDefs && SzBitArray_Check(digestsDefs, digestIndex))) | 1428 | } |
1429 | if ((crcMask & mask) == 0 && digestsVals) | ||
1430 | { | ||
1431 | if (!digestsDefs || SzBitArray_Check(digestsDefs, digestIndex)) | ||
1430 | { | 1432 | { |
1431 | p->CRCs.Vals[i] = GetUi32(digestsVals + (size_t)digestsValsIndex * 4); | 1433 | p->CRCs.Vals[i] = GetUi32(digestsVals); |
1432 | digestsValsIndex++; | 1434 | digestsVals += 4; |
1433 | crcMask |= mask; | 1435 | crcMask |= mask; |
1434 | } | 1436 | } |
1437 | digestIndex++; | ||
1435 | } | 1438 | } |
1436 | } | 1439 | } |
1437 | 1440 | ||
1438 | if (mask != 0x80) | 1441 | if (mask != 0x80) |
1439 | { | 1442 | { |
1440 | UInt32 byteIndex = (i - 1) >> 3; | 1443 | const UInt32 byteIndex = (i - 1) >> 3; |
1441 | p->IsDirs[byteIndex] = isDirMask; | 1444 | p->IsDirs[byteIndex] = isDirMask; |
1442 | p->CRCs.Defs[byteIndex] = crcMask; | 1445 | p->CRCs.Defs[byteIndex] = crcMask; |
1443 | } | 1446 | } |
@@ -1454,7 +1457,7 @@ static SRes SzReadHeader2( | |||
1454 | break; | 1457 | break; |
1455 | if (!ssi.sdNumSubStreams.Data) | 1458 | if (!ssi.sdNumSubStreams.Data) |
1456 | return SZ_ERROR_ARCHIVE; | 1459 | return SZ_ERROR_ARCHIVE; |
1457 | RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)); | 1460 | RINOK(SzReadNumber32(&ssi.sdNumSubStreams, &numSubStreams)) |
1458 | if (numSubStreams != 0) | 1461 | if (numSubStreams != 0) |
1459 | return SZ_ERROR_ARCHIVE; | 1462 | return SZ_ERROR_ARCHIVE; |
1460 | /* | 1463 | /* |
@@ -1479,7 +1482,7 @@ static SRes SzReadHeader2( | |||
1479 | static SRes SzReadHeader( | 1482 | static SRes SzReadHeader( |
1480 | CSzArEx *p, | 1483 | CSzArEx *p, |
1481 | CSzData *sd, | 1484 | CSzData *sd, |
1482 | ILookInStream *inStream, | 1485 | ILookInStreamPtr inStream, |
1483 | ISzAllocPtr allocMain, | 1486 | ISzAllocPtr allocMain, |
1484 | ISzAllocPtr allocTemp) | 1487 | ISzAllocPtr allocTemp) |
1485 | { | 1488 | { |
@@ -1498,7 +1501,7 @@ static SRes SzReadHeader( | |||
1498 | for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++) | 1501 | for (i = 0; i < NUM_ADDITIONAL_STREAMS_MAX; i++) |
1499 | Buf_Free(tempBufs + i, allocTemp); | 1502 | Buf_Free(tempBufs + i, allocTemp); |
1500 | 1503 | ||
1501 | RINOK(res); | 1504 | RINOK(res) |
1502 | 1505 | ||
1503 | if (sd->Size != 0) | 1506 | if (sd->Size != 0) |
1504 | return SZ_ERROR_FAIL; | 1507 | return SZ_ERROR_FAIL; |
@@ -1508,7 +1511,7 @@ static SRes SzReadHeader( | |||
1508 | 1511 | ||
1509 | static SRes SzArEx_Open2( | 1512 | static SRes SzArEx_Open2( |
1510 | CSzArEx *p, | 1513 | CSzArEx *p, |
1511 | ILookInStream *inStream, | 1514 | ILookInStreamPtr inStream, |
1512 | ISzAllocPtr allocMain, | 1515 | ISzAllocPtr allocMain, |
1513 | ISzAllocPtr allocTemp) | 1516 | ISzAllocPtr allocTemp) |
1514 | { | 1517 | { |
@@ -1521,9 +1524,9 @@ static SRes SzArEx_Open2( | |||
1521 | SRes res; | 1524 | SRes res; |
1522 | 1525 | ||
1523 | startArcPos = 0; | 1526 | startArcPos = 0; |
1524 | RINOK(ILookInStream_Seek(inStream, &startArcPos, SZ_SEEK_CUR)); | 1527 | RINOK(ILookInStream_Seek(inStream, &startArcPos, SZ_SEEK_CUR)) |
1525 | 1528 | ||
1526 | RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE)); | 1529 | RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE)) |
1527 | 1530 | ||
1528 | if (!TestSignatureCandidate(header)) | 1531 | if (!TestSignatureCandidate(header)) |
1529 | return SZ_ERROR_NO_ARCHIVE; | 1532 | return SZ_ERROR_NO_ARCHIVE; |
@@ -1552,14 +1555,14 @@ static SRes SzArEx_Open2( | |||
1552 | 1555 | ||
1553 | { | 1556 | { |
1554 | Int64 pos = 0; | 1557 | Int64 pos = 0; |
1555 | RINOK(ILookInStream_Seek(inStream, &pos, SZ_SEEK_END)); | 1558 | RINOK(ILookInStream_Seek(inStream, &pos, SZ_SEEK_END)) |
1556 | if ((UInt64)pos < (UInt64)startArcPos + nextHeaderOffset || | 1559 | if ((UInt64)pos < (UInt64)startArcPos + nextHeaderOffset || |
1557 | (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset || | 1560 | (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset || |
1558 | (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize) | 1561 | (UInt64)pos < (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize) |
1559 | return SZ_ERROR_INPUT_EOF; | 1562 | return SZ_ERROR_INPUT_EOF; |
1560 | } | 1563 | } |
1561 | 1564 | ||
1562 | RINOK(LookInStream_SeekTo(inStream, (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset)); | 1565 | RINOK(LookInStream_SeekTo(inStream, (UInt64)startArcPos + k7zStartHeaderSize + nextHeaderOffset)) |
1563 | 1566 | ||
1564 | if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp)) | 1567 | if (!Buf_Create(&buf, nextHeaderSizeT, allocTemp)) |
1565 | return SZ_ERROR_MEM; | 1568 | return SZ_ERROR_MEM; |
@@ -1634,10 +1637,10 @@ static SRes SzArEx_Open2( | |||
1634 | } | 1637 | } |
1635 | 1638 | ||
1636 | 1639 | ||
1637 | SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, | 1640 | SRes SzArEx_Open(CSzArEx *p, ILookInStreamPtr inStream, |
1638 | ISzAllocPtr allocMain, ISzAllocPtr allocTemp) | 1641 | ISzAllocPtr allocMain, ISzAllocPtr allocTemp) |
1639 | { | 1642 | { |
1640 | SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp); | 1643 | const SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp); |
1641 | if (res != SZ_OK) | 1644 | if (res != SZ_OK) |
1642 | SzArEx_Free(p, allocMain); | 1645 | SzArEx_Free(p, allocMain); |
1643 | return res; | 1646 | return res; |
@@ -1646,7 +1649,7 @@ SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, | |||
1646 | 1649 | ||
1647 | SRes SzArEx_Extract( | 1650 | SRes SzArEx_Extract( |
1648 | const CSzArEx *p, | 1651 | const CSzArEx *p, |
1649 | ILookInStream *inStream, | 1652 | ILookInStreamPtr inStream, |
1650 | UInt32 fileIndex, | 1653 | UInt32 fileIndex, |
1651 | UInt32 *blockIndex, | 1654 | UInt32 *blockIndex, |
1652 | Byte **tempBuf, | 1655 | Byte **tempBuf, |
@@ -1656,7 +1659,7 @@ SRes SzArEx_Extract( | |||
1656 | ISzAllocPtr allocMain, | 1659 | ISzAllocPtr allocMain, |
1657 | ISzAllocPtr allocTemp) | 1660 | ISzAllocPtr allocTemp) |
1658 | { | 1661 | { |
1659 | UInt32 folderIndex = p->FileToFolder[fileIndex]; | 1662 | const UInt32 folderIndex = p->FileToFolder[fileIndex]; |
1660 | SRes res = SZ_OK; | 1663 | SRes res = SZ_OK; |
1661 | 1664 | ||
1662 | *offset = 0; | 1665 | *offset = 0; |
@@ -1673,13 +1676,13 @@ SRes SzArEx_Extract( | |||
1673 | 1676 | ||
1674 | if (*tempBuf == NULL || *blockIndex != folderIndex) | 1677 | if (*tempBuf == NULL || *blockIndex != folderIndex) |
1675 | { | 1678 | { |
1676 | UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex); | 1679 | const UInt64 unpackSizeSpec = SzAr_GetFolderUnpackSize(&p->db, folderIndex); |
1677 | /* | 1680 | /* |
1678 | UInt64 unpackSizeSpec = | 1681 | UInt64 unpackSizeSpec = |
1679 | p->UnpackPositions[p->FolderToFile[(size_t)folderIndex + 1]] - | 1682 | p->UnpackPositions[p->FolderToFile[(size_t)folderIndex + 1]] - |
1680 | p->UnpackPositions[p->FolderToFile[folderIndex]]; | 1683 | p->UnpackPositions[p->FolderToFile[folderIndex]]; |
1681 | */ | 1684 | */ |
1682 | size_t unpackSize = (size_t)unpackSizeSpec; | 1685 | const size_t unpackSize = (size_t)unpackSizeSpec; |
1683 | 1686 | ||
1684 | if (unpackSize != unpackSizeSpec) | 1687 | if (unpackSize != unpackSizeSpec) |
1685 | return SZ_ERROR_MEM; | 1688 | return SZ_ERROR_MEM; |
@@ -1707,7 +1710,7 @@ SRes SzArEx_Extract( | |||
1707 | 1710 | ||
1708 | if (res == SZ_OK) | 1711 | if (res == SZ_OK) |
1709 | { | 1712 | { |
1710 | UInt64 unpackPos = p->UnpackPositions[fileIndex]; | 1713 | const UInt64 unpackPos = p->UnpackPositions[fileIndex]; |
1711 | *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]); | 1714 | *offset = (size_t)(unpackPos - p->UnpackPositions[p->FolderToFile[folderIndex]]); |
1712 | *outSizeProcessed = (size_t)(p->UnpackPositions[(size_t)fileIndex + 1] - unpackPos); | 1715 | *outSizeProcessed = (size_t)(p->UnpackPositions[(size_t)fileIndex + 1] - unpackPos); |
1713 | if (*offset + *outSizeProcessed > *outBufferSize) | 1716 | if (*offset + *outSizeProcessed > *outBufferSize) |
@@ -1723,8 +1726,8 @@ SRes SzArEx_Extract( | |||
1723 | 1726 | ||
1724 | size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest) | 1727 | size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest) |
1725 | { | 1728 | { |
1726 | size_t offs = p->FileNameOffsets[fileIndex]; | 1729 | const size_t offs = p->FileNameOffsets[fileIndex]; |
1727 | size_t len = p->FileNameOffsets[fileIndex + 1] - offs; | 1730 | const size_t len = p->FileNameOffsets[fileIndex + 1] - offs; |
1728 | if (dest != 0) | 1731 | if (dest != 0) |
1729 | { | 1732 | { |
1730 | size_t i; | 1733 | size_t i; |