1 // Archive/UdfIn.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../../../C/CpuArch.h"
6
7 #include "../../Common/StreamUtils.h"
8
9 #include "UdfIn.h"
10
11 #define Get16(p) GetUi16(p)
12 #define Get32(p) GetUi32(p)
13 #define Get64(p) GetUi64(p)
14
15 namespace NArchive {
16 namespace NUdf {
17
18 const int kNumPartitionsMax = 64;
19 const int kNumLogVolumesMax = 64;
20 const int kNumRecureseLevelsMax = 1 << 10;
21 const int kNumItemsMax = 1 << 27;
22 const int kNumFilesMax = 1 << 28;
23 const int kNumRefsMax = 1 << 28;
24 const UInt32 kNumExtentsMax = (UInt32)1 << 30;
25 const UInt64 kFileNameLengthTotalMax = (UInt64)1 << 33;
26 const UInt64 kInlineExtentsSizeMax = (UInt64)1 << 33;
27
28 void MY_FAST_CALL Crc16GenerateTable(void);
29
30 #define CRC16_INIT_VAL 0
31 #define CRC16_GET_DIGEST(crc) (crc)
32 #define CRC16_UPDATE_BYTE(crc, b) (g_Crc16Table[(((crc) >> 8) ^ (b)) & 0xFF] ^ ((crc) << 8))
33
34 #define kCrc16Poly 0x1021
35 UInt16 g_Crc16Table[256];
36
Crc16GenerateTable(void)37 void MY_FAST_CALL Crc16GenerateTable(void)
38 {
39 UInt32 i;
40 for (i = 0; i < 256; i++)
41 {
42 UInt32 r = (i << 8);
43 for (int j = 8; j > 0; j--)
44 r = ((r & 0x8000) ? ((r << 1) ^ kCrc16Poly) : (r << 1)) & 0xFFFF;
45 g_Crc16Table[i] = (UInt16)r;
46 }
47 }
48
Crc16_Update(UInt16 v,const void * data,size_t size)49 UInt16 MY_FAST_CALL Crc16_Update(UInt16 v, const void *data, size_t size)
50 {
51 const Byte *p = (const Byte *)data;
52 for (; size > 0 ; size--, p++)
53 v = CRC16_UPDATE_BYTE(v, *p);
54 return v;
55 }
56
Crc16Calc(const void * data,size_t size)57 UInt16 MY_FAST_CALL Crc16Calc(const void *data, size_t size)
58 {
59 return Crc16_Update(CRC16_INIT_VAL, data, size);
60 }
61
CCrc16TableInitNArchive::NUdf::CCrc16TableInit62 struct CCrc16TableInit { CCrc16TableInit() { Crc16GenerateTable(); } } g_Crc16TableInit;
63
Parse(const Byte * buf)64 void CDString128::Parse(const Byte *buf) { memcpy(Data, buf, sizeof(Data)); }
65
Parse(const Byte * p,unsigned size)66 void CDString::Parse(const Byte *p, unsigned size)
67 {
68 Data.SetCapacity(size);
69 memcpy(Data, p, size);
70 }
71
ParseDString(const Byte * data,int size)72 static UString ParseDString(const Byte *data, int size)
73 {
74 UString res;
75 wchar_t *p;
76 if (size > 0)
77 {
78 Byte type = data[0];
79 if (type == 8)
80 {
81 p = res.GetBuffer((int)size + 1);
82 for (int i = 1; i < size; i++)
83 {
84 wchar_t c = data[i];
85 if (c == 0)
86 break;
87 *p++ = c;
88 }
89 }
90 else if (type == 16)
91 {
92 p = res.GetBuffer((int)size / 2 + 1);
93 for (int i = 1; i + 2 <= size; i += 2)
94 {
95 wchar_t c = ((wchar_t)data[i] << 8) | data[i + 1];
96 if (c == 0)
97 break;
98 *p++ = c;
99 }
100 }
101 else
102 return L"[unknow]";
103 *p++ = 0;
104 res.ReleaseBuffer();
105 }
106 return res;
107 }
108
GetString() const109 UString CDString:: GetString() const { return ParseDString(Data, (int)Data.GetCapacity()); }
GetString() const110 UString CDString128::GetString() const
111 {
112 int size = Data[sizeof(Data) - 1];
113 return ParseDString(Data, MyMin(size, (int)(sizeof(Data) - 1)));
114 }
115
Parse(const Byte * buf)116 void CTime::Parse(const Byte *buf) { memcpy(Data, buf, sizeof(Data)); }
117
118 /*
119 void CRegId::Parse(const Byte *buf)
120 {
121 Flags = buf[0];
122 memcpy(Id, buf + 1, sizeof(Id));
123 memcpy(Suffix, buf + 24, sizeof(Suffix));
124 }
125 */
126
127 // ECMA 3/7.1
128
129 struct CExtent
130 {
131 UInt32 Len;
132 UInt32 Pos;
133
134 void Parse(const Byte *buf);
135 };
136
Parse(const Byte * buf)137 void CExtent::Parse(const Byte *buf)
138 {
139 Len = Get32(buf);
140 Pos = Get32(buf + 4);
141 }
142
143 // ECMA 3/7.2
144
145 struct CTag
146 {
147 UInt16 Id;
148 UInt16 Version;
149 // Byte Checksum;
150 // UInt16 SerialNumber;
151 // UInt16 Crc;
152 // UInt16 CrcLen;
153 // UInt32 TagLocation;
154
155 HRESULT Parse(const Byte *buf, size_t size);
156 };
157
Parse(const Byte * buf,size_t size)158 HRESULT CTag::Parse(const Byte *buf, size_t size)
159 {
160 if (size < 16)
161 return S_FALSE;
162 Byte sum = 0;
163 int i;
164 for (i = 0; i < 4; i++) sum = sum + buf[i];
165 for (i = 5; i < 16; i++) sum = sum + buf[i];
166 if (sum != buf[4] || buf[5] != 0) return S_FALSE;
167
168 Id = Get16(buf);
169 Version = Get16(buf + 2);
170 // SerialNumber = Get16(buf + 6);
171 UInt16 crc = Get16(buf + 8);
172 UInt16 crcLen = Get16(buf + 10);
173 // TagLocation = Get32(buf + 12);
174
175 if (size >= 16 + (size_t)crcLen)
176 if (crc == Crc16Calc(buf + 16, crcLen))
177 return S_OK;
178 return S_FALSE;
179 }
180
181 // ECMA 3/7.2.1
182
183 enum EDescriptorType
184 {
185 DESC_TYPE_SpoaringTable = 0, // UDF
186 DESC_TYPE_PrimVol = 1,
187 DESC_TYPE_AnchorVolPtr = 2,
188 DESC_TYPE_VolPtr = 3,
189 DESC_TYPE_ImplUseVol = 4,
190 DESC_TYPE_Partition = 5,
191 DESC_TYPE_LogicalVol = 6,
192 DESC_TYPE_UnallocSpace = 7,
193 DESC_TYPE_Terminating = 8,
194 DESC_TYPE_LogicalVolIntegrity = 9,
195 DESC_TYPE_FileSet = 256,
196 DESC_TYPE_FileId = 257,
197 DESC_TYPE_AllocationExtent = 258,
198 DESC_TYPE_Indirect = 259,
199 DESC_TYPE_Terminal = 260,
200 DESC_TYPE_File = 261,
201 DESC_TYPE_ExtendedAttrHeader = 262,
202 DESC_TYPE_UnallocatedSpace = 263,
203 DESC_TYPE_SpaceBitmap = 264,
204 DESC_TYPE_PartitionIntegrity = 265,
205 DESC_TYPE_ExtendedFile = 266,
206 };
207
208
Parse(const Byte * buf)209 void CLogBlockAddr::Parse(const Byte *buf)
210 {
211 Pos = Get32(buf);
212 PartitionRef = Get16(buf + 4);
213 }
214
Parse(const Byte * buf)215 void CShortAllocDesc::Parse(const Byte *buf)
216 {
217 Len = Get32(buf);
218 Pos = Get32(buf + 4);
219 }
220
221 /*
222 void CADImpUse::Parse(const Byte *buf)
223 {
224 Flags = Get16(buf);
225 UdfUniqueId = Get32(buf + 2);
226 }
227 */
228
Parse(const Byte * buf)229 void CLongAllocDesc::Parse(const Byte *buf)
230 {
231 Len = Get32(buf);
232 Location.Parse(buf + 4);
233 // memcpy(ImplUse, buf + 10, sizeof(ImplUse));
234 // adImpUse.Parse(ImplUse);
235 }
236
CheckExtent(int volIndex,int partitionRef,UInt32 blockPos,UInt32 len) const237 bool CInArchive::CheckExtent(int volIndex, int partitionRef, UInt32 blockPos, UInt32 len) const
238 {
239 const CLogVol &vol = LogVols[volIndex];
240 const CPartition &partition = Partitions[vol.PartitionMaps[partitionRef].PartitionIndex];
241 UInt64 offset = ((UInt64)partition.Pos << SecLogSize) + (UInt64)blockPos * vol.BlockSize;
242 return (offset + len) <= (((UInt64)partition.Pos + partition.Len) << SecLogSize);
243 }
244
CheckItemExtents(int volIndex,const CItem & item) const245 bool CInArchive::CheckItemExtents(int volIndex, const CItem &item) const
246 {
247 for (int i = 0; i < item.Extents.Size(); i++)
248 {
249 const CMyExtent &e = item.Extents[i];
250 if (!CheckExtent(volIndex, e.PartitionRef, e.Pos, e.GetLen()))
251 return false;
252 }
253 return true;
254 }
255
Read(int volIndex,int partitionRef,UInt32 blockPos,UInt32 len,Byte * buf)256 HRESULT CInArchive::Read(int volIndex, int partitionRef, UInt32 blockPos, UInt32 len, Byte *buf)
257 {
258 if (!CheckExtent(volIndex, partitionRef, blockPos, len))
259 return S_FALSE;
260 const CLogVol &vol = LogVols[volIndex];
261 const CPartition &partition = Partitions[vol.PartitionMaps[partitionRef].PartitionIndex];
262 RINOK(_stream->Seek(((UInt64)partition.Pos << SecLogSize) +
263 (UInt64)blockPos * vol.BlockSize, STREAM_SEEK_SET, NULL));
264 return ReadStream_FALSE(_stream, buf, len);
265 }
266
Read(int volIndex,const CLongAllocDesc & lad,Byte * buf)267 HRESULT CInArchive::Read(int volIndex, const CLongAllocDesc &lad, Byte *buf)
268 {
269 return Read(volIndex, lad.Location.PartitionRef, lad.Location.Pos, lad.GetLen(), (Byte *)buf);
270 }
271
ReadFromFile(int volIndex,const CItem & item,CByteBuffer & buf)272 HRESULT CInArchive::ReadFromFile(int volIndex, const CItem &item, CByteBuffer &buf)
273 {
274 if (item.Size >= (UInt32)1 << 30)
275 return S_FALSE;
276 if (item.IsInline)
277 {
278 buf = item.InlineData;
279 return S_OK;
280 }
281 buf.SetCapacity((size_t)item.Size);
282 size_t pos = 0;
283 for (int i = 0; i < item.Extents.Size(); i++)
284 {
285 const CMyExtent &e = item.Extents[i];
286 UInt32 len = e.GetLen();
287 RINOK(Read(volIndex, e.PartitionRef, e.Pos, len, (Byte *)buf + pos));
288 pos += len;
289 }
290 return S_OK;
291 }
292
293
Parse(const Byte * p)294 void CIcbTag::Parse(const Byte *p)
295 {
296 // PriorDirectNum = Get32(p);
297 // StrategyType = Get16(p + 4);
298 // StrategyParam = Get16(p + 6);
299 // MaxNumOfEntries = Get16(p + 8);
300 FileType = p[11];
301 // ParentIcb.Parse(p + 12);
302 Flags = Get16(p + 18);
303 }
304
Parse(const Byte * p)305 void CItem::Parse(const Byte *p)
306 {
307 // Uid = Get32(p + 36);
308 // Gid = Get32(p + 40);
309 // Permissions = Get32(p + 44);
310 // FileLinkCount = Get16(p + 48);
311 // RecordFormat = p[50];
312 // RecordDisplayAttr = p[51];
313 // RecordLen = Get32(p + 52);
314 Size = Get64(p + 56);
315 NumLogBlockRecorded = Get64(p + 64);
316 ATime.Parse(p + 72);
317 MTime.Parse(p + 84);
318 // AttrtTime.Parse(p + 96);
319 // CheckPoint = Get32(p + 108);
320 // ExtendedAttrIcb.Parse(p + 112);
321 // ImplId.Parse(p + 128);
322 // UniqueId = Get64(p + 160);
323 }
324
325 // 4/14.4
326 struct CFileId
327 {
328 // UInt16 FileVersion;
329 Byte FileCharacteristics;
330 // CByteBuffer ImplUse;
331 CDString Id;
332 CLongAllocDesc Icb;
333
IsItLinkParentNArchive::NUdf::CFileId334 bool IsItLinkParent() const { return (FileCharacteristics & FILEID_CHARACS_Parent) != 0; }
335 HRESULT Parse(const Byte *p, size_t size, size_t &processed);
336 };
337
Parse(const Byte * p,size_t size,size_t & processed)338 HRESULT CFileId::Parse(const Byte *p, size_t size, size_t &processed)
339 {
340 processed = 0;
341 if (size < 38)
342 return S_FALSE;
343 CTag tag;
344 RINOK(tag.Parse(p, size));
345 if (tag.Id != DESC_TYPE_FileId)
346 return S_FALSE;
347 // FileVersion = Get16(p + 16);
348 FileCharacteristics = p[18];
349 unsigned idLen = p[19];
350 Icb.Parse(p + 20);
351 unsigned impLen = Get16(p + 36);
352 if (size < 38 + idLen + impLen)
353 return S_FALSE;
354 // ImplUse.SetCapacity(impLen);
355 processed = 38;
356 // memcpy(ImplUse, p + processed, impLen);
357 processed += impLen;
358 Id.Parse(p + processed, idLen);
359 processed += idLen;
360 for (;(processed & 3) != 0; processed++)
361 if (p[processed] != 0)
362 return S_FALSE;
363 return (processed <= size) ? S_OK : S_FALSE;
364 }
365
ReadFileItem(int volIndex,int fsIndex,const CLongAllocDesc & lad,int numRecurseAllowed)366 HRESULT CInArchive::ReadFileItem(int volIndex, int fsIndex, const CLongAllocDesc &lad, int numRecurseAllowed)
367 {
368 if (Files.Size() % 100 == 0)
369 RINOK(_progress->SetCompleted(Files.Size(), _processedProgressBytes));
370 if (numRecurseAllowed-- == 0)
371 return S_FALSE;
372 CFile &file = Files.Back();
373 const CLogVol &vol = LogVols[volIndex];
374 CPartition &partition = Partitions[vol.PartitionMaps[lad.Location.PartitionRef].PartitionIndex];
375
376 UInt32 key = lad.Location.Pos;
377 UInt32 value;
378 const UInt32 kRecursedErrorValue = (UInt32)(Int32)-1;
379 if (partition.Map.Find(key, value))
380 {
381 if (value == kRecursedErrorValue)
382 return S_FALSE;
383 file.ItemIndex = value;
384 }
385 else
386 {
387 value = Items.Size();
388 file.ItemIndex = (int)value;
389 if (partition.Map.Set(key, kRecursedErrorValue))
390 return S_FALSE;
391 RINOK(ReadItem(volIndex, fsIndex, lad, numRecurseAllowed));
392 if (!partition.Map.Set(key, value))
393 return S_FALSE;
394 }
395 return S_OK;
396 }
397
ReadItem(int volIndex,int fsIndex,const CLongAllocDesc & lad,int numRecurseAllowed)398 HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &lad, int numRecurseAllowed)
399 {
400 if (Items.Size() > kNumItemsMax)
401 return S_FALSE;
402 Items.Add(CItem());
403 CItem &item = Items.Back();
404
405 const CLogVol &vol = LogVols[volIndex];
406
407 if (lad.GetLen() != vol.BlockSize)
408 return S_FALSE;
409
410 CByteBuffer buf;
411 size_t size = lad.GetLen();
412 buf.SetCapacity(size);
413 RINOK(Read(volIndex, lad, buf));
414
415 CTag tag;
416 const Byte *p = buf;
417 RINOK(tag.Parse(p, size));
418 if (tag.Id != DESC_TYPE_File)
419 return S_FALSE;
420
421 item.IcbTag.Parse(p + 16);
422 if (item.IcbTag.FileType != ICB_FILE_TYPE_DIR &&
423 item.IcbTag.FileType != ICB_FILE_TYPE_FILE)
424 return S_FALSE;
425
426 item.Parse(p);
427
428 _processedProgressBytes += (UInt64)item.NumLogBlockRecorded * vol.BlockSize + size;
429
430 UInt32 extendedAttrLen = Get32(p + 168);
431 UInt32 allocDescriptorsLen = Get32(p + 172);
432
433 if ((extendedAttrLen & 3) != 0)
434 return S_FALSE;
435 int pos = 176;
436 if (extendedAttrLen > size - pos)
437 return S_FALSE;
438 /*
439 if (extendedAttrLen != 16)
440 {
441 if (extendedAttrLen < 24)
442 return S_FALSE;
443 CTag attrTag;
444 RINOK(attrTag.Parse(p + pos, size));
445 if (attrTag.Id != DESC_TYPE_ExtendedAttrHeader)
446 return S_FALSE;
447 // UInt32 implAttrLocation = Get32(p + pos + 16);
448 // UInt32 applicationlAttrLocation = Get32(p + pos + 20);
449 }
450 */
451 pos += extendedAttrLen;
452
453 int desctType = item.IcbTag.GetDescriptorType();
454 if (allocDescriptorsLen > size - pos)
455 return S_FALSE;
456 if (desctType == ICB_DESC_TYPE_INLINE)
457 {
458 item.IsInline = true;
459 item.InlineData.SetCapacity(allocDescriptorsLen);
460 memcpy(item.InlineData, p + pos, allocDescriptorsLen);
461 }
462 else
463 {
464 item.IsInline = false;
465 if (desctType != ICB_DESC_TYPE_SHORT && desctType != ICB_DESC_TYPE_LONG)
466 return S_FALSE;
467 for (UInt32 i = 0; i < allocDescriptorsLen;)
468 {
469 CMyExtent e;
470 if (desctType == ICB_DESC_TYPE_SHORT)
471 {
472 if (i + 8 > allocDescriptorsLen)
473 return S_FALSE;
474 CShortAllocDesc sad;
475 sad.Parse(p + pos + i);
476 e.Pos = sad.Pos;
477 e.Len = sad.Len;
478 e.PartitionRef = lad.Location.PartitionRef;
479 i += 8;
480 }
481 else
482 {
483 if (i + 16 > allocDescriptorsLen)
484 return S_FALSE;
485 CLongAllocDesc ladNew;
486 ladNew.Parse(p + pos + i);
487 e.Pos = ladNew.Location.Pos;
488 e.PartitionRef = ladNew.Location.PartitionRef;
489 e.Len = ladNew.Len;
490 i += 16;
491 }
492 item.Extents.Add(e);
493 }
494 }
495
496 if (item.IcbTag.IsDir())
497 {
498 if (!item.CheckChunkSizes() || !CheckItemExtents(volIndex, item))
499 return S_FALSE;
500 CByteBuffer buf;
501 RINOK(ReadFromFile(volIndex, item, buf));
502 item.Size = 0;
503 item.Extents.ClearAndFree();
504 item.InlineData.Free();
505
506 const Byte *p = buf;
507 size = buf.GetCapacity();
508 size_t processedTotal = 0;
509 for (; processedTotal < size;)
510 {
511 size_t processedCur;
512 CFileId fileId;
513 RINOK(fileId.Parse(p + processedTotal, size - processedTotal, processedCur));
514 if (!fileId.IsItLinkParent())
515 {
516 CFile file;
517 // file.FileVersion = fileId.FileVersion;
518 // file.FileCharacteristics = fileId.FileCharacteristics;
519 // file.ImplUse = fileId.ImplUse;
520 file.Id = fileId.Id;
521
522 _fileNameLengthTotal += file.Id.Data.GetCapacity();
523 if (_fileNameLengthTotal > kFileNameLengthTotalMax)
524 return S_FALSE;
525
526 item.SubFiles.Add(Files.Size());
527 if (Files.Size() > kNumFilesMax)
528 return S_FALSE;
529 Files.Add(file);
530 RINOK(ReadFileItem(volIndex, fsIndex, fileId.Icb, numRecurseAllowed));
531 }
532 processedTotal += processedCur;
533 }
534 }
535 else
536 {
537 if ((UInt32)item.Extents.Size() > kNumExtentsMax - _numExtents)
538 return S_FALSE;
539 _numExtents += item.Extents.Size();
540
541 if (item.InlineData.GetCapacity() > kInlineExtentsSizeMax - _inlineExtentsSize)
542 return S_FALSE;
543 _inlineExtentsSize += item.InlineData.GetCapacity();
544 }
545
546 return S_OK;
547 }
548
FillRefs(CFileSet & fs,int fileIndex,int parent,int numRecurseAllowed)549 HRESULT CInArchive::FillRefs(CFileSet &fs, int fileIndex, int parent, int numRecurseAllowed)
550 {
551 if (_numRefs % 10000 == 0)
552 {
553 RINOK(_progress->SetCompleted());
554 }
555 if (numRecurseAllowed-- == 0)
556 return S_FALSE;
557 if (_numRefs >= kNumRefsMax)
558 return S_FALSE;
559 _numRefs++;
560 CRef ref;
561 ref.FileIndex = fileIndex;
562 ref.Parent = parent;
563 parent = fs.Refs.Size();
564 fs.Refs.Add(ref);
565 const CItem &item = Items[Files[fileIndex].ItemIndex];
566 for (int i = 0; i < item.SubFiles.Size(); i++)
567 {
568 RINOK(FillRefs(fs, item.SubFiles[i], parent, numRecurseAllowed));
569 }
570 return S_OK;
571 }
572
Open2()573 HRESULT CInArchive::Open2()
574 {
575 Clear();
576
577 UInt64 fileSize;
578 RINOK(_stream->Seek(0, STREAM_SEEK_END, &fileSize));
579
580 // Some UDFs contain additional 2 KB of zeros, so we also check 12, corrected to 11.
581 const int kSecLogSizeMax = 12;
582 Byte buf[1 << kSecLogSizeMax];
583 Byte kSizesLog[] = { 11, 8, 12 };
584
585 for (int i = 0;; i++)
586 {
587 if (i == sizeof(kSizesLog) / sizeof(kSizesLog[0]))
588 return S_FALSE;
589 SecLogSize = kSizesLog[i];
590 Int32 bufSize = 1 << SecLogSize;
591 if (bufSize > fileSize)
592 return S_FALSE;
593 RINOK(_stream->Seek(-bufSize, STREAM_SEEK_END, NULL));
594 RINOK(ReadStream_FALSE(_stream, buf, bufSize));
595 CTag tag;
596 if (tag.Parse(buf, bufSize) == S_OK)
597 if (tag.Id == DESC_TYPE_AnchorVolPtr)
598 break;
599 }
600 if (SecLogSize == 12)
601 SecLogSize = 11;
602
603 CExtent extentVDS;
604 extentVDS.Parse(buf + 16);
605
606 for (UInt32 location = extentVDS.Pos; ; location++)
607 {
608 size_t bufSize = 1 << SecLogSize;
609 size_t pos = 0;
610 RINOK(_stream->Seek((UInt64)location << SecLogSize, STREAM_SEEK_SET, NULL));
611 RINOK(ReadStream_FALSE(_stream, buf, bufSize));
612 CTag tag;
613 RINOK(tag.Parse(buf + pos, bufSize - pos));
614 if (tag.Id == DESC_TYPE_Terminating)
615 break;
616 if (tag.Id == DESC_TYPE_Partition)
617 {
618 if (Partitions.Size() >= kNumPartitionsMax)
619 return S_FALSE;
620 CPartition partition;
621 // UInt32 volDescSeqNumer = Get32(buf + 16);
622 // partition.Flags = Get16(buf + 20);
623 partition.Number = Get16(buf + 22);
624 // partition.ContentsId.Parse(buf + 24);
625
626 // memcpy(partition.ContentsUse, buf + 56, sizeof(partition.ContentsUse));
627 // ContentsUse is Partition Header Description.
628
629 // partition.AccessType = Get32(buf + 184);
630 partition.Pos = Get32(buf + 188);
631 partition.Len = Get32(buf + 192);
632 // partition.ImplId.Parse(buf + 196);
633 // memcpy(partition.ImplUse, buf + 228, sizeof(partition.ImplUse));
634
635 Partitions.Add(partition);
636 }
637 else if (tag.Id == DESC_TYPE_LogicalVol)
638 {
639 if (LogVols.Size() >= kNumLogVolumesMax)
640 return S_FALSE;
641 CLogVol vol;
642 vol.Id.Parse(buf + 84);
643 vol.BlockSize = Get32(buf + 212);
644 // vol.DomainId.Parse(buf + 216);
645
646 if (vol.BlockSize < 512 || vol.BlockSize > ((UInt32)1 << 30))
647 return S_FALSE;
648
649 // memcpy(vol.ContentsUse, buf + 248, sizeof(vol.ContentsUse));
650 vol.FileSetLocation.Parse(buf + 248);
651
652 // UInt32 mapTableLength = Get32(buf + 264);
653 UInt32 numPartitionMaps = Get32(buf + 268);
654 if (numPartitionMaps > kNumPartitionsMax)
655 return S_FALSE;
656 // vol.ImplId.Parse(buf + 272);
657 // memcpy(vol.ImplUse, buf + 128, sizeof(vol.ImplUse));
658 size_t pos = 440;
659 for (UInt32 i = 0; i < numPartitionMaps; i++)
660 {
661 if (pos + 2 > bufSize)
662 return S_FALSE;
663 CPartitionMap pm;
664 pm.Type = buf[pos];
665 // pm.Length = buf[pos + 1];
666 Byte len = buf[pos + 1];
667
668 if (pos + len > bufSize)
669 return S_FALSE;
670
671 // memcpy(pm.Data, buf + pos + 2, pm.Length - 2);
672 if (pm.Type == 1)
673 {
674 if (pos + 6 > bufSize)
675 return S_FALSE;
676 // pm.VolSeqNumber = Get16(buf + pos + 2);
677 pm.PartitionNumber = Get16(buf + pos + 4);
678 }
679 else
680 return S_FALSE;
681 pos += len;
682 vol.PartitionMaps.Add(pm);
683 }
684 LogVols.Add(vol);
685 }
686 }
687
688 UInt64 totalSize = 0;
689
690 int volIndex;
691 for (volIndex = 0; volIndex < LogVols.Size(); volIndex++)
692 {
693 CLogVol &vol = LogVols[volIndex];
694 for (int pmIndex = 0; pmIndex < vol.PartitionMaps.Size(); pmIndex++)
695 {
696 CPartitionMap &pm = vol.PartitionMaps[pmIndex];
697 int i;
698 for (i = 0; i < Partitions.Size(); i++)
699 {
700 CPartition &part = Partitions[i];
701 if (part.Number == pm.PartitionNumber)
702 {
703 if (part.VolIndex >= 0)
704 return S_FALSE;
705 pm.PartitionIndex = i;
706 part.VolIndex = volIndex;
707
708 totalSize += (UInt64)part.Len << SecLogSize;
709 break;
710 }
711 }
712 if (i == Partitions.Size())
713 return S_FALSE;
714 }
715 }
716
717 RINOK(_progress->SetTotal(totalSize));
718
719 for (volIndex = 0; volIndex < LogVols.Size(); volIndex++)
720 {
721 CLogVol &vol = LogVols[volIndex];
722
723 CLongAllocDesc nextExtent = vol.FileSetLocation;
724 // while (nextExtent.ExtentLen != 0)
725 // for (int i = 0; i < 1; i++)
726 {
727 if (nextExtent.GetLen() < 512)
728 return S_FALSE;
729 CByteBuffer buf;
730 buf.SetCapacity(nextExtent.GetLen());
731 RINOK(Read(volIndex, nextExtent, buf));
732 const Byte *p = buf;
733 size_t size = nextExtent.GetLen();
734
735 CTag tag;
736 RINOK(tag.Parse(p, size));
737 if (tag.Id != DESC_TYPE_FileSet)
738 return S_FALSE;
739
740 CFileSet fs;
741 fs.RecodringTime.Parse(p + 16);
742 // fs.InterchangeLevel = Get16(p + 18);
743 // fs.MaxInterchangeLevel = Get16(p + 20);
744 // fs.FileSetNumber = Get32(p + 40);
745 // fs.FileSetDescNumber = Get32(p + 44);
746
747 // fs.Id.Parse(p + 304);
748 // fs.CopyrightId.Parse(p + 336);
749 // fs.AbstractId.Parse(p + 368);
750
751 fs.RootDirICB.Parse(p + 400);
752 // fs.DomainId.Parse(p + 416);
753
754 // fs.SystemStreamDirICB.Parse(p + 464);
755
756 vol.FileSets.Add(fs);
757
758 // nextExtent.Parse(p + 448);
759 }
760
761 for (int fsIndex = 0; fsIndex < vol.FileSets.Size(); fsIndex++)
762 {
763 CFileSet &fs = vol.FileSets[fsIndex];
764 int fileIndex = Files.Size();
765 Files.Add(CFile());
766 RINOK(ReadFileItem(volIndex, fsIndex, fs.RootDirICB, kNumRecureseLevelsMax));
767 RINOK(FillRefs(fs, fileIndex, -1, kNumRecureseLevelsMax));
768 }
769 }
770
771 return S_OK;
772 }
773
Open(IInStream * inStream,CProgressVirt * progress)774 HRESULT CInArchive::Open(IInStream *inStream, CProgressVirt *progress)
775 {
776 _progress = progress;
777 _stream = inStream;
778 HRESULT res;
779 try { res = Open2(); }
780 catch(...) { Clear(); res = S_FALSE; }
781 _stream.Release();
782 return res;
783 }
784
Clear()785 void CInArchive::Clear()
786 {
787 Partitions.Clear();
788 LogVols.Clear();
789 Items.Clear();
790 Files.Clear();
791 _fileNameLengthTotal = 0;
792 _numRefs = 0;
793 _numExtents = 0;
794 _inlineExtentsSize = 0;
795 _processedProgressBytes = 0;
796 }
797
GetComment() const798 UString CInArchive::GetComment() const
799 {
800 UString res;
801 for (int i = 0; i < LogVols.Size(); i++)
802 {
803 if (i > 0)
804 res += L" ";
805 res += LogVols[i].GetName();
806 }
807 return res;
808 }
809
GetSpecName(const UString & name)810 static UString GetSpecName(const UString &name)
811 {
812 UString name2 = name;
813 name2.Trim();
814 if (name2.IsEmpty())
815 {
816 /*
817 wchar_t s[32];
818 ConvertUInt64ToString(id, s);
819 return L"[" + (UString)s + L"]";
820 */
821 return L"[]";
822 }
823 return name;
824 }
825
UpdateWithName(UString & res,const UString & addString)826 static void UpdateWithName(UString &res, const UString &addString)
827 {
828 if (res.IsEmpty())
829 res = addString;
830 else
831 res = addString + WCHAR_PATH_SEPARATOR + res;
832 }
833
GetItemPath(int volIndex,int fsIndex,int refIndex,bool showVolName,bool showFsName) const834 UString CInArchive::GetItemPath(int volIndex, int fsIndex, int refIndex,
835 bool showVolName, bool showFsName) const
836 {
837 // showVolName = true;
838 const CLogVol &vol = LogVols[volIndex];
839 const CFileSet &fs = vol.FileSets[fsIndex];
840
841 UString name;
842
843 for (;;)
844 {
845 const CRef &ref = fs.Refs[refIndex];
846 refIndex = ref.Parent;
847 if (refIndex < 0)
848 break;
849 UpdateWithName(name, GetSpecName(Files[ref.FileIndex].GetName()));
850 }
851
852 if (showFsName)
853 {
854 wchar_t s[32];
855 ConvertUInt64ToString(fsIndex, s);
856 UString newName = L"File Set ";
857 newName += s;
858 UpdateWithName(name, newName);
859 }
860
861 if (showVolName)
862 {
863 wchar_t s[32];
864 ConvertUInt64ToString(volIndex, s);
865 UString newName = s;
866 UString newName2 = vol.GetName();
867 if (newName2.IsEmpty())
868 newName2 = L"Volume";
869 newName += L'-';
870 newName += newName2;
871 UpdateWithName(name, newName);
872 }
873 return name;
874 }
875
876 }}
877