This commit is contained in:
Igor Pavlov
2014-11-23 00:00:00 +00:00
committed by Kornel Lesiński
parent 83f8ddcc5b
commit f08f4dcc3c
1158 changed files with 76451 additions and 35082 deletions

3
CPP/7zip/Archive/Udf/StdAfx.h Executable file → Normal file
View File

@@ -3,7 +3,6 @@
#ifndef __STDAFX_H
#define __STDAFX_H
#include "../../../Common/MyWindows.h"
#include "../../../Common/NewHandler.h"
#include "../../../Common/Common.h"
#endif

170
CPP/7zip/Archive/Udf/UdfHandler.cpp Executable file → Normal file
View File

@@ -2,13 +2,14 @@
#include "StdAfx.h"
#include "Common/ComTry.h"
#include "../../../Common/ComTry.h"
#include "Windows/PropVariant.h"
#include "Windows/Time.h"
#include "../../../Windows/PropVariant.h"
#include "../../../Windows/TimeUtils.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/ProgressUtils.h"
#include "../../Common/RegisterArc.h"
#include "../../Common/StreamObjects.h"
#include "../../Compress/CopyCoder.h"
@@ -18,7 +19,7 @@
namespace NArchive {
namespace NUdf {
void UdfTimeToFileTime(const CTime &t, NWindows::NCOM::CPropVariant &prop)
static void UdfTimeToFileTime(const CTime &t, NWindows::NCOM::CPropVariant &prop)
{
UInt64 numSecs;
const Byte *d = t.Data;
@@ -33,21 +34,21 @@ void UdfTimeToFileTime(const CTime &t, NWindows::NCOM::CPropVariant &prop)
prop = ft;
}
static STATPROPSTG kProps[] =
static const Byte kProps[] =
{
{ NULL, kpidPath, VT_BSTR},
{ NULL, kpidIsDir, VT_BOOL},
{ NULL, kpidSize, VT_UI8},
{ NULL, kpidPackSize, VT_UI8},
{ NULL, kpidMTime, VT_FILETIME},
{ NULL, kpidATime, VT_FILETIME}
kpidPath,
kpidIsDir,
kpidSize,
kpidPackSize,
kpidMTime,
kpidATime
};
static STATPROPSTG kArcProps[] =
static const Byte kArcProps[] =
{
{ NULL, kpidComment, VT_BSTR},
{ NULL, kpidClusterSize, VT_UI4},
{ NULL, kpidCTime, VT_FILETIME}
kpidComment,
kpidClusterSize,
kpidCTime
};
IMP_IInArchive_Props
@@ -59,6 +60,8 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
NWindows::NCOM::CPropVariant prop;
switch(propID)
{
case kpidPhySize: prop = _archive.PhySize; break;
case kpidComment:
{
UString comment = _archive.GetComment();
@@ -71,7 +74,7 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
if (_archive.LogVols.Size() > 0)
{
UInt32 blockSize = _archive.LogVols[0].BlockSize;
int i;
unsigned i;
for (i = 1; i < _archive.LogVols.Size(); i++)
if (_archive.LogVols[i].BlockSize != blockSize)
break;
@@ -88,6 +91,16 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
UdfTimeToFileTime(vol.FileSets[0].RecodringTime, prop);
}
break;
case kpidErrorFlags:
{
UInt32 v = 0;
if (!_archive.IsArc) v |= kpv_ErrorFlags_IsNotArc;
if (_archive.Unsupported) v |= kpv_ErrorFlags_UnsupportedFeature;
if (_archive.UnexpectedEnd) v |= kpv_ErrorFlags_UnexpectedEnd;
prop = v;
break;
}
}
prop.Detach(value);
return S_OK;
@@ -127,9 +140,7 @@ HRESULT CProgressImp::SetCompleted()
return S_OK;
}
STDMETHODIMP CHandler::Open(IInStream *stream,
const UInt64 * /* maxCheckStartPosition */,
IArchiveOpenCallback *callback)
STDMETHODIMP CHandler::Open(IInStream *stream, const UInt64 *, IArchiveOpenCallback *callback)
{
COM_TRY_BEGIN
{
@@ -137,14 +148,14 @@ STDMETHODIMP CHandler::Open(IInStream *stream,
CProgressImp progressImp(callback);
RINOK(_archive.Open(stream, &progressImp));
bool showVolName = (_archive.LogVols.Size() > 1);
for (int volIndex = 0; volIndex < _archive.LogVols.Size(); volIndex++)
FOR_VECTOR (volIndex, _archive.LogVols)
{
const CLogVol &vol = _archive.LogVols[volIndex];
bool showFileSetName = (vol.FileSets.Size() > 1);
for (int fsIndex = 0; fsIndex < vol.FileSets.Size(); fsIndex++)
FOR_VECTOR (fsIndex, vol.FileSets)
{
const CFileSet &fs = vol.FileSets[fsIndex];
for (int i = ((showVolName || showFileSetName) ? 0 : 1); i < fs.Refs.Size(); i++)
for (unsigned i = ((showVolName || showFileSetName) ? 0 : 1); i < fs.Refs.Size(); i++)
{
CRef2 ref2;
ref2.Vol = volIndex;
@@ -184,7 +195,7 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
const CRef &ref = vol.FileSets[ref2.Fs].Refs[ref2.Ref];
const CFile &file = _archive.Files[ref.FileIndex];
const CItem &item = _archive.Items[file.ItemIndex];
switch(propID)
switch (propID)
{
case kpidPath: prop = _archive.GetItemPath(ref2.Vol, ref2.Fs, ref2.Ref,
_archive.LogVols.Size() > 1, vol.FileSets.Size() > 1); break;
@@ -200,99 +211,6 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
COM_TRY_END
}
struct CSeekExtent
{
UInt64 Phy;
UInt64 Virt;
};
class CExtentsStream:
public IInStream,
public CMyUnknownImp
{
UInt64 _phyPos;
UInt64 _virtPos;
bool _needStartSeek;
HRESULT SeekToPhys() { return Stream->Seek(_phyPos, STREAM_SEEK_SET, NULL); }
public:
CMyComPtr<IInStream> Stream;
CRecordVector<CSeekExtent> Extents;
MY_UNKNOWN_IMP1(IInStream)
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition);
void ReleaseStream() { Stream.Release(); }
void Init()
{
_virtPos = 0;
_phyPos = 0;
_needStartSeek = true;
}
};
STDMETHODIMP CExtentsStream::Read(void *data, UInt32 size, UInt32 *processedSize)
{
if (processedSize)
*processedSize = 0;
if (size > 0)
{
UInt64 totalSize = Extents.Back().Virt;
if (_virtPos >= totalSize)
return (_virtPos == totalSize) ? S_OK : E_FAIL;
int left = 0, right = Extents.Size() - 1;
for (;;)
{
int mid = (left + right) / 2;
if (mid == left)
break;
if (_virtPos < Extents[mid].Virt)
right = mid;
else
left = mid;
}
const CSeekExtent &extent = Extents[left];
UInt64 phyPos = extent.Phy + (_virtPos - extent.Virt);
if (_needStartSeek || _phyPos != phyPos)
{
_needStartSeek = false;
_phyPos = phyPos;
RINOK(SeekToPhys());
}
UInt64 rem = Extents[left + 1].Virt - _virtPos;
if (size > rem)
size = (UInt32)rem;
HRESULT res = Stream->Read(data, size, &size);
_phyPos += size;
_virtPos += size;
if (processedSize)
*processedSize = size;
return res;
}
return S_OK;
}
STDMETHODIMP CExtentsStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
{
switch(seekOrigin)
{
case STREAM_SEEK_SET: _virtPos = offset; break;
case STREAM_SEEK_CUR: _virtPos += offset; break;
case STREAM_SEEK_END: _virtPos = Extents.Back().Virt + offset; break;
default: return STG_E_INVALIDFUNCTION;
}
if (newPosition)
*newPosition = _virtPos;
return S_OK;
}
STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
{
*stream = 0;
@@ -325,7 +243,7 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
extentStreamSpec->Stream = _inStream;
UInt64 virtOffset = 0;
for (int extentIndex = 0; extentIndex < item.Extents.Size(); extentIndex++)
FOR_VECTOR (extentIndex, item.Extents)
{
const CMyExtent &extent = item.Extents[extentIndex];
UInt32 len = extent.GetLen();
@@ -363,7 +281,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
Int32 testMode, IArchiveExtractCallback *extractCallback)
{
COM_TRY_BEGIN
bool allFilesMode = (numItems == (UInt32)-1);
bool allFilesMode = (numItems == (UInt32)(Int32)-1);
if (allFilesMode)
numItems = _refs2.Size();
if (numItems == 0)
@@ -431,7 +349,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
CMyComPtr<ISequentialInStream> udfInStream;
HRESULT res = GetStream(index, &udfInStream);
if (res == E_NOTIMPL)
opRes = NExtract::NOperationResult::kUnSupportedMethod;
opRes = NExtract::NOperationResult::kUnsupportedMethod;
else if (res != S_OK)
opRes = NExtract::NOperationResult::kDataError;
else
@@ -448,4 +366,18 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
COM_TRY_END
}
IMP_CreateArcIn
static const UInt32 kIsoStartPos = 0x8000;
static CArcInfo g_ArcInfo =
{ "Udf", "udf iso img", 0, 0xE0,
// 5, { 0, 'N', 'S', 'R', '0' },
6, { 1, 'C', 'D', '0', '0', '1' },
kIsoStartPos,
NArcInfoFlags::kStartOpen,
CreateArc, NULL, IsArc_Udf };
REGISTER_ARC(Udf)
}}

11
CPP/7zip/Archive/Udf/UdfHandler.h Executable file → Normal file
View File

@@ -1,9 +1,10 @@
// Udf/Handler.h
// UdfHandler.h
#ifndef __UDF_HANDLER_H
#define __UDF_HANDLER_H
#include "Common/MyCom.h"
#include "../../../Common/MyCom.h"
#include "../IArchive.h"
#include "UdfIn.h"
@@ -13,9 +14,9 @@ namespace NUdf {
struct CRef2
{
int Vol;
int Fs;
int Ref;
unsigned Vol;
unsigned Fs;
unsigned Ref;
};
class CHandler:

393
CPP/7zip/Archive/Udf/UdfIn.cpp Executable file → Normal file
View File

@@ -2,12 +2,25 @@
#include "StdAfx.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#endif
#include "../../../../C/CpuArch.h"
#include "../../Common/RegisterArc.h"
#include "../../Common/StreamUtils.h"
#include "UdfIn.h"
#ifdef SHOW_DEBUG_INFO
#define PRF(x) x
#else
#define PRF(x)
#endif
#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
#define Get64(p) GetUi64(p)
@@ -15,24 +28,22 @@
namespace NArchive {
namespace NUdf {
const int kNumPartitionsMax = 64;
const int kNumLogVolumesMax = 64;
const int kNumRecureseLevelsMax = 1 << 10;
const int kNumItemsMax = 1 << 27;
const int kNumFilesMax = 1 << 28;
const int kNumRefsMax = 1 << 28;
const UInt32 kNumExtentsMax = (UInt32)1 << 30;
const UInt64 kFileNameLengthTotalMax = (UInt64)1 << 33;
const UInt64 kInlineExtentsSizeMax = (UInt64)1 << 33;
void MY_FAST_CALL Crc16GenerateTable(void);
static const unsigned kNumPartitionsMax = 64;
static const unsigned kNumLogVolumesMax = 64;
static const unsigned kNumRecursionLevelsMax = 1 << 10;
static const unsigned kNumItemsMax = 1 << 27;
static const unsigned kNumFilesMax = 1 << 28;
static const unsigned kNumRefsMax = 1 << 28;
static const UInt32 kNumExtentsMax = (UInt32)1 << 30;
static const UInt64 kFileNameLengthTotalMax = (UInt64)1 << 33;
static const UInt64 kInlineExtentsSizeMax = (UInt64)1 << 33;
#define CRC16_INIT_VAL 0
#define CRC16_GET_DIGEST(crc) (crc)
#define CRC16_UPDATE_BYTE(crc, b) (g_Crc16Table[(((crc) >> 8) ^ (b)) & 0xFF] ^ ((crc) << 8))
#define CRC16_UPDATE_BYTE(crc, b) ((UInt16)(g_Crc16Table[(((crc) >> 8) ^ (b)) & 0xFF] ^ ((crc) << 8)))
#define kCrc16Poly 0x1021
UInt16 g_Crc16Table[256];
static UInt16 g_Crc16Table[256];
void MY_FAST_CALL Crc16GenerateTable(void)
{
@@ -61,15 +72,14 @@ UInt16 MY_FAST_CALL Crc16Calc(const void *data, size_t size)
struct CCrc16TableInit { CCrc16TableInit() { Crc16GenerateTable(); } } g_Crc16TableInit;
void CDString128::Parse(const Byte *buf) { memcpy(Data, buf, sizeof(Data)); }
void CDString::Parse(const Byte *p, unsigned size)
{
Data.SetCapacity(size);
memcpy(Data, p, size);
Data.CopyFrom(p, size);
}
static UString ParseDString(const Byte *data, int size)
static UString ParseDString(const Byte *data, unsigned size)
{
UString res;
wchar_t *p;
@@ -78,8 +88,8 @@ static UString ParseDString(const Byte *data, int size)
Byte type = data[0];
if (type == 8)
{
p = res.GetBuffer((int)size + 1);
for (int i = 1; i < size; i++)
p = res.GetBuffer(size);
for (unsigned i = 1; i < size; i++)
{
wchar_t c = data[i];
if (c == 0)
@@ -89,10 +99,10 @@ static UString ParseDString(const Byte *data, int size)
}
else if (type == 16)
{
p = res.GetBuffer((int)size / 2 + 1);
for (int i = 1; i + 2 <= size; i += 2)
p = res.GetBuffer(size / 2);
for (unsigned i = 1; i + 2 <= size; i += 2)
{
wchar_t c = ((wchar_t)data[i] << 8) | data[i + 1];
wchar_t c = GetBe16(data + i);
if (c == 0)
break;
*p++ = c;
@@ -106,13 +116,14 @@ static UString ParseDString(const Byte *data, int size)
return res;
}
UString CDString:: GetString() const { return ParseDString(Data, (int)Data.GetCapacity()); }
UString CDString128::GetString() const
{
int size = Data[sizeof(Data) - 1];
return ParseDString(Data, MyMin(size, (int)(sizeof(Data) - 1)));
unsigned size = Data[sizeof(Data) - 1];
return ParseDString(Data, MyMin(size, (unsigned)(sizeof(Data) - 1)));
}
UString CDString::GetString() const { return ParseDString(Data, (unsigned)Data.Size()); }
void CTime::Parse(const Byte *buf) { memcpy(Data, buf, sizeof(Data)); }
/*
@@ -161,8 +172,8 @@ HRESULT CTag::Parse(const Byte *buf, size_t size)
return S_FALSE;
Byte sum = 0;
int i;
for (i = 0; i < 4; i++) sum = sum + buf[i];
for (i = 5; i < 16; i++) sum = sum + buf[i];
for (i = 0; i < 4; i++) sum = (Byte)(sum + buf[i]);
for (i = 5; i < 16; i++) sum = (Byte)(sum + buf[i]);
if (sum != buf[4] || buf[5] != 0) return S_FALSE;
Id = Get16(buf);
@@ -182,27 +193,27 @@ HRESULT CTag::Parse(const Byte *buf, size_t size)
enum EDescriptorType
{
DESC_TYPE_SpoaringTable = 0, // UDF
DESC_TYPE_PrimVol = 1,
DESC_TYPE_AnchorVolPtr = 2,
DESC_TYPE_VolPtr = 3,
DESC_TYPE_ImplUseVol = 4,
DESC_TYPE_Partition = 5,
DESC_TYPE_LogicalVol = 6,
DESC_TYPE_UnallocSpace = 7,
DESC_TYPE_Terminating = 8,
DESC_TYPE_SpoaringTable = 0, // UDF
DESC_TYPE_PrimVol = 1,
DESC_TYPE_AnchorVolPtr = 2,
DESC_TYPE_VolPtr = 3,
DESC_TYPE_ImplUseVol = 4,
DESC_TYPE_Partition = 5,
DESC_TYPE_LogicalVol = 6,
DESC_TYPE_UnallocSpace = 7,
DESC_TYPE_Terminating = 8,
DESC_TYPE_LogicalVolIntegrity = 9,
DESC_TYPE_FileSet = 256,
DESC_TYPE_FileId = 257,
DESC_TYPE_AllocationExtent = 258,
DESC_TYPE_Indirect = 259,
DESC_TYPE_Terminal = 260,
DESC_TYPE_File = 261,
DESC_TYPE_ExtendedAttrHeader = 262,
DESC_TYPE_UnallocatedSpace = 263,
DESC_TYPE_SpaceBitmap = 264,
DESC_TYPE_PartitionIntegrity = 265,
DESC_TYPE_ExtendedFile = 266
DESC_TYPE_FileSet = 256,
DESC_TYPE_FileId = 257,
DESC_TYPE_AllocationExtent = 258,
DESC_TYPE_Indirect = 259,
DESC_TYPE_Terminal = 260,
DESC_TYPE_File = 261,
DESC_TYPE_ExtendedAttrHeader = 262,
DESC_TYPE_UnallocatedSpace = 263,
DESC_TYPE_SpaceBitmap = 264,
DESC_TYPE_PartitionIntegrity = 265,
DESC_TYPE_ExtendedFile = 266
};
@@ -237,6 +248,8 @@ void CLongAllocDesc::Parse(const Byte *buf)
bool CInArchive::CheckExtent(int volIndex, int partitionRef, UInt32 blockPos, UInt32 len) const
{
const CLogVol &vol = LogVols[volIndex];
if (partitionRef >= (int)vol.PartitionMaps.Size())
return false;
const CPartition &partition = Partitions[vol.PartitionMaps[partitionRef].PartitionIndex];
UInt64 offset = ((UInt64)partition.Pos << SecLogSize) + (UInt64)blockPos * vol.BlockSize;
return (offset + len) <= (((UInt64)partition.Pos + partition.Len) << SecLogSize);
@@ -244,7 +257,7 @@ bool CInArchive::CheckExtent(int volIndex, int partitionRef, UInt32 blockPos, UI
bool CInArchive::CheckItemExtents(int volIndex, const CItem &item) const
{
for (int i = 0; i < item.Extents.Size(); i++)
FOR_VECTOR (i, item.Extents)
{
const CMyExtent &e = item.Extents[i];
if (!CheckExtent(volIndex, e.PartitionRef, e.Pos, e.GetLen()))
@@ -259,9 +272,14 @@ HRESULT CInArchive::Read(int volIndex, int partitionRef, UInt32 blockPos, UInt32
return S_FALSE;
const CLogVol &vol = LogVols[volIndex];
const CPartition &partition = Partitions[vol.PartitionMaps[partitionRef].PartitionIndex];
RINOK(_stream->Seek(((UInt64)partition.Pos << SecLogSize) +
(UInt64)blockPos * vol.BlockSize, STREAM_SEEK_SET, NULL));
return ReadStream_FALSE(_stream, buf, len);
UInt64 offset = ((UInt64)partition.Pos << SecLogSize) + (UInt64)blockPos * vol.BlockSize;
RINOK(_stream->Seek(offset, STREAM_SEEK_SET, NULL));
HRESULT res = ReadStream_FALSE(_stream, buf, len);
if (res == S_FALSE && offset + len > FileSize)
UnexpectedEnd = true;
RINOK(res);
UpdatePhySize(offset + len);
return S_OK;
}
HRESULT CInArchive::Read(int volIndex, const CLongAllocDesc &lad, Byte *buf)
@@ -278,9 +296,9 @@ HRESULT CInArchive::ReadFromFile(int volIndex, const CItem &item, CByteBuffer &b
buf = item.InlineData;
return S_OK;
}
buf.SetCapacity((size_t)item.Size);
buf.Alloc((size_t)item.Size);
size_t pos = 0;
for (int i = 0; i < item.Extents.Size(); i++)
FOR_VECTOR (i, item.Extents)
{
const CMyExtent &e = item.Extents[i];
UInt32 len = e.GetLen();
@@ -407,9 +425,8 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
if (lad.GetLen() != vol.BlockSize)
return S_FALSE;
CByteBuffer buf;
size_t size = lad.GetLen();
buf.SetCapacity(size);
CByteBuffer buf(size);
RINOK(Read(volIndex, lad, buf));
CTag tag;
@@ -456,8 +473,7 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
if (desctType == ICB_DESC_TYPE_INLINE)
{
item.IsInline = true;
item.InlineData.SetCapacity(allocDescriptorsLen);
memcpy(item.InlineData, p + pos, allocDescriptorsLen);
item.InlineData.CopyFrom(p + pos, allocDescriptorsLen);
}
else
{
@@ -504,7 +520,7 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
item.InlineData.Free();
const Byte *p = buf;
size = buf.GetCapacity();
size = buf.Size();
size_t processedTotal = 0;
for (; processedTotal < size;)
{
@@ -519,7 +535,7 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
// file.ImplUse = fileId.ImplUse;
file.Id = fileId.Id;
_fileNameLengthTotal += file.Id.Data.GetCapacity();
_fileNameLengthTotal += file.Id.Data.Size();
if (_fileNameLengthTotal > kFileNameLengthTotalMax)
return S_FALSE;
@@ -538,9 +554,9 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
return S_FALSE;
_numExtents += item.Extents.Size();
if (item.InlineData.GetCapacity() > kInlineExtentsSizeMax - _inlineExtentsSize)
if (item.InlineData.Size() > kInlineExtentsSizeMax - _inlineExtentsSize)
return S_FALSE;
_inlineExtentsSize += item.InlineData.GetCapacity();
_inlineExtentsSize += item.InlineData.Size();
}
return S_OK;
@@ -548,7 +564,7 @@ HRESULT CInArchive::ReadItem(int volIndex, int fsIndex, const CLongAllocDesc &la
HRESULT CInArchive::FillRefs(CFileSet &fs, int fileIndex, int parent, int numRecurseAllowed)
{
if (_numRefs % 10000 == 0)
if ((_numRefs & 0xFFF) == 0)
{
RINOK(_progress->SetCompleted());
}
@@ -563,23 +579,47 @@ HRESULT CInArchive::FillRefs(CFileSet &fs, int fileIndex, int parent, int numRec
parent = fs.Refs.Size();
fs.Refs.Add(ref);
const CItem &item = Items[Files[fileIndex].ItemIndex];
for (int i = 0; i < item.SubFiles.Size(); i++)
FOR_VECTOR (i, item.SubFiles)
{
RINOK(FillRefs(fs, item.SubFiles[i], parent, numRecurseAllowed));
}
return S_OK;
}
API_FUNC_IsArc IsArc_Udf(const Byte *p, size_t size)
{
UInt32 res = k_IsArc_Res_NO;
unsigned SecLogSize;
for (SecLogSize = 11;; SecLogSize -= 3)
{
if (SecLogSize < 8)
return res;
UInt32 offset = (UInt32)256 << SecLogSize;
size_t bufSize = 1 << SecLogSize;
if (offset + bufSize > size)
res = k_IsArc_Res_NEED_MORE;
else
{
CTag tag;
if (tag.Parse(p + offset, bufSize) == S_OK)
if (tag.Id == DESC_TYPE_AnchorVolPtr)
return k_IsArc_Res_YES;
}
}
}
HRESULT CInArchive::Open2()
{
Clear();
UInt64 fileSize;
RINOK(_stream->Seek(0, STREAM_SEEK_END, &fileSize));
FileSize = fileSize;
// Some UDFs contain additional pad zeros (2 KB).
// Seek to STREAM_SEEK_END for direct DVD reading can return 8 KB more, so we check last 16 KB.
// And when we read last block, result read size can be smaller than required size.
UInt64 fileSize;
RINOK(_stream->Seek(0, STREAM_SEEK_END, &fileSize));
/*
const size_t kBufSize = 1 << 14;
Byte buf[kBufSize];
size_t readSize = (fileSize < kBufSize) ? (size_t)fileSize : kBufSize;
@@ -598,22 +638,69 @@ HRESULT CInArchive::Open2()
if (tag.Id == DESC_TYPE_AnchorVolPtr)
break;
}
PhySize = fileSize;
CExtent extentVDS;
extentVDS.Parse(buf + i + 16);
*/
const size_t kBufSize = 1 << 11;
Byte buf[kBufSize];
for (SecLogSize = 11;; SecLogSize -= 3)
{
if (SecLogSize < 8)
return S_FALSE;
UInt32 offset = (UInt32)256 << SecLogSize;
if (offset >= fileSize)
continue;
RINOK(_stream->Seek(offset, STREAM_SEEK_SET, NULL));
size_t bufSize = 1 << SecLogSize;
size_t readSize = bufSize;
RINOK(ReadStream(_stream, buf, &readSize));
if (readSize == bufSize)
{
CTag tag;
if (tag.Parse(buf, readSize) == S_OK)
if (tag.Id == DESC_TYPE_AnchorVolPtr)
break;
}
}
PhySize = (UInt32)(256 + 1) << SecLogSize;
IsArc = true;
for (UInt32 location = extentVDS.Pos; ; location++)
CExtent extentVDS;
extentVDS.Parse(buf + 16);
{
CExtent extentVDS2;
extentVDS2.Parse(buf + 24);
UpdatePhySize(((UInt64)extentVDS.Pos << SecLogSize) + extentVDS.Len);
UpdatePhySize(((UInt64)extentVDS2.Pos << SecLogSize) + extentVDS2.Len);
}
for (UInt32 location = 0; ; location++)
{
size_t bufSize = 1 << SecLogSize;
size_t pos = 0;
RINOK(_stream->Seek((UInt64)location << SecLogSize, STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(_stream, buf, bufSize));
if (((UInt64)(location + 1) << SecLogSize) > extentVDS.Len)
return S_FALSE;
UInt64 offs = (UInt64)(extentVDS.Pos + location) << SecLogSize;
RINOK(_stream->Seek(offs, STREAM_SEEK_SET, NULL));
HRESULT res = ReadStream_FALSE(_stream, buf, bufSize);
if (res == S_FALSE && offs + bufSize > FileSize)
UnexpectedEnd = true;
RINOK(res);
CTag tag;
RINOK(tag.Parse(buf + pos, bufSize - pos));
if (tag.Id == DESC_TYPE_Terminating)
break;
if (tag.Id == DESC_TYPE_Partition)
{
// Partition Descriptor
// ECMA 167 3/10.5
// UDF / 2.2.14
if (Partitions.Size() >= kNumPartitionsMax)
return S_FALSE;
CPartition partition;
@@ -631,10 +718,15 @@ HRESULT CInArchive::Open2()
// partition.ImplId.Parse(buf + 196);
// memcpy(partition.ImplUse, buf + 228, sizeof(partition.ImplUse));
PRF(printf("\nPartition number = %2d pos = %d len = %d", partition.Number, partition.Pos, partition.Len));
Partitions.Add(partition);
}
else if (tag.Id == DESC_TYPE_LogicalVol)
{
/* Logical Volume Descriptor
ECMA 3/10.6
UDF 2.60 2.2.4 */
if (LogVols.Size() >= kNumLogVolumesMax)
return S_FALSE;
CLogVol vol;
@@ -647,6 +739,8 @@ HRESULT CInArchive::Open2()
// memcpy(vol.ContentsUse, buf + 248, sizeof(vol.ContentsUse));
vol.FileSetLocation.Parse(buf + 248);
/* the extent in which the first File Set Descriptor Sequence
of the logical volume is recorded */
// UInt32 mapTableLength = Get32(buf + 264);
UInt32 numPartitionMaps = Get32(buf + 268);
@@ -654,6 +748,8 @@ HRESULT CInArchive::Open2()
return S_FALSE;
// vol.ImplId.Parse(buf + 272);
// memcpy(vol.ImplUse, buf + 128, sizeof(vol.ImplUse));
PRF(printf("\nLogicalVol numPartitionMaps = %2d", numPartitionMaps));
size_t pos = 440;
for (UInt32 i = 0; i < numPartitionMaps; i++)
{
@@ -670,10 +766,33 @@ HRESULT CInArchive::Open2()
// memcpy(pm.Data, buf + pos + 2, pm.Length - 2);
if (pm.Type == 1)
{
if (pos + 6 > bufSize)
if (len != 6) // < 6
return S_FALSE;
// pm.VolSeqNumber = Get16(buf + pos + 2);
pm.PartitionNumber = Get16(buf + pos + 4);
PRF(printf("\nPartitionMap type 1 PartitionNumber = %2d", pm.PartitionNumber));
}
else if (pm.Type == 2)
{
if (len != 64)
return S_FALSE;
/* ECMA 10.7.3 / Type 2 Partition Map
62 bytes: Partition Identifier. */
/* UDF 2.6
2.2.8 Virtual Partition Map
This is an extension of ECMA 167 to expand its scope to include
sequentially written media (eg. CD-R). This extension is for a
Partition Map entry to describe a virtual space. */
// It's not implemented still.
if (Get16(buf + pos + 2) != 0)
return S_FALSE;
// pm.VolSeqNumber = Get16(buf + pos + 36);
pm.PartitionNumber = Get16(buf + pos + 38);
PRF(printf("\nPartitionMap type 2 PartitionNumber = %2d", pm.PartitionNumber));
// Unsupported = true;
return S_FALSE;
}
else
return S_FALSE;
@@ -686,21 +805,26 @@ HRESULT CInArchive::Open2()
UInt64 totalSize = 0;
int volIndex;
unsigned volIndex;
for (volIndex = 0; volIndex < LogVols.Size(); volIndex++)
{
CLogVol &vol = LogVols[volIndex];
for (int pmIndex = 0; pmIndex < vol.PartitionMaps.Size(); pmIndex++)
FOR_VECTOR (pmIndex, vol.PartitionMaps)
{
CPartitionMap &pm = vol.PartitionMaps[pmIndex];
int i;
unsigned i;
for (i = 0; i < Partitions.Size(); i++)
{
CPartition &part = Partitions[i];
if (part.Number == pm.PartitionNumber)
{
if (part.VolIndex >= 0)
return S_FALSE;
{
// it's for 2.60. Fix it
if (part.VolIndex != (int)volIndex)
return S_FALSE;
// return S_FALSE;
}
pm.PartitionIndex = i;
part.VolIndex = volIndex;
@@ -715,27 +839,39 @@ HRESULT CInArchive::Open2()
RINOK(_progress->SetTotal(totalSize));
PRF(printf("\n Read files"));
for (volIndex = 0; volIndex < LogVols.Size(); volIndex++)
{
CLogVol &vol = LogVols[volIndex];
PRF(printf("\nLogVol %2d", volIndex));
CLongAllocDesc nextExtent = vol.FileSetLocation;
// while (nextExtent.ExtentLen != 0)
// for (int i = 0; i < 1; i++)
{
if (nextExtent.GetLen() < 512)
return S_FALSE;
CByteBuffer buf;
buf.SetCapacity(nextExtent.GetLen());
CByteBuffer buf(nextExtent.GetLen());
RINOK(Read(volIndex, nextExtent, buf));
const Byte *p = buf;
size_t size = nextExtent.GetLen();
CTag tag;
RINOK(tag.Parse(p, size));
if (tag.Id == DESC_TYPE_ExtendedFile)
{
// ECMA 4 / 14.17
// 2.60 ??
return S_FALSE;
}
if (tag.Id != DESC_TYPE_FileSet)
return S_FALSE;
PRF(printf("\n FileSet", volIndex));
CFileSet fs;
fs.RecodringTime.Parse(p + 16);
// fs.InterchangeLevel = Get16(p + 18);
@@ -757,16 +893,76 @@ HRESULT CInArchive::Open2()
// nextExtent.Parse(p + 448);
}
for (int fsIndex = 0; fsIndex < vol.FileSets.Size(); fsIndex++)
FOR_VECTOR (fsIndex, vol.FileSets)
{
CFileSet &fs = vol.FileSets[fsIndex];
int fileIndex = Files.Size();
Files.Add(CFile());
RINOK(ReadFileItem(volIndex, fsIndex, fs.RootDirICB, kNumRecureseLevelsMax));
RINOK(FillRefs(fs, fileIndex, -1, kNumRecureseLevelsMax));
unsigned fileIndex = Files.Size();
Files.AddNew();
RINOK(ReadFileItem(volIndex, fsIndex, fs.RootDirICB, kNumRecursionLevelsMax));
RINOK(FillRefs(fs, fileIndex, -1, kNumRecursionLevelsMax));
}
}
for (volIndex = 0; volIndex < LogVols.Size(); volIndex++)
{
const CLogVol &vol = LogVols[volIndex];
// bool showFileSetName = (vol.FileSets.Size() > 1);
FOR_VECTOR (fsIndex, vol.FileSets)
{
const CFileSet &fs = vol.FileSets[fsIndex];
for (unsigned i =
// ((showVolName || showFileSetName) ? 0 : 1)
0; i < fs.Refs.Size(); i++)
{
const CRef &ref = vol.FileSets[fsIndex].Refs[i];
const CFile &file = Files[ref.FileIndex];
const CItem &item = Items[file.ItemIndex];
UInt64 size = item.Size;
if (!item.IsRecAndAlloc() || !item.CheckChunkSizes() || !CheckItemExtents(volIndex, item))
continue;
FOR_VECTOR (extentIndex, item.Extents)
{
const CMyExtent &extent = item.Extents[extentIndex];
UInt32 len = extent.GetLen();
if (len == 0)
continue;
if (size < len)
break;
int partitionIndex = vol.PartitionMaps[extent.PartitionRef].PartitionIndex;
UInt32 logBlockNumber = extent.Pos;
const CPartition &partition = Partitions[partitionIndex];
UInt64 offset = ((UInt64)partition.Pos << SecLogSize) +
(UInt64)logBlockNumber * vol.BlockSize;
UpdatePhySize(offset + len);
}
}
}
}
{
UInt32 secMask = ((UInt32)1 << SecLogSize) - 1;
PhySize = (PhySize + secMask) & ~(UInt64)secMask;
}
if (PhySize < fileSize)
{
RINOK(_stream->Seek(PhySize, STREAM_SEEK_SET, NULL));
size_t bufSize = 1 << SecLogSize;
size_t readSize = bufSize;
RINOK(ReadStream(_stream, buf, &readSize));
if (readSize == bufSize)
{
CTag tag;
if (tag.Parse(buf, readSize) == S_OK)
if (tag.Id == DESC_TYPE_AnchorVolPtr)
{
PhySize += bufSize;
}
}
}
return S_OK;
}
@@ -774,15 +970,38 @@ HRESULT CInArchive::Open(IInStream *inStream, CProgressVirt *progress)
{
_progress = progress;
_stream = inStream;
HRESULT res = Open2();
if (res == S_FALSE && IsArc && !UnexpectedEnd)
Unsupported = true;
return res;
/*
HRESULT res;
try { res = Open2(); }
catch(...) { Clear(); res = S_FALSE; }
try
{
res = Open2();
}
catch(...)
{
// Clear();
// res = S_FALSE;
_stream.Release();
throw;
}
_stream.Release();
return res;
*/
}
void CInArchive::Clear()
{
IsArc = false;
Unsupported = false;
UnexpectedEnd = false;
PhySize = 0;
FileSize = 0;
Partitions.Clear();
LogVols.Clear();
Items.Clear();
@@ -797,7 +1016,7 @@ void CInArchive::Clear()
UString CInArchive::GetComment() const
{
UString res;
for (int i = 0; i < LogVols.Size(); i++)
FOR_VECTOR (i, LogVols)
{
if (i > 0)
res += L" ";
@@ -827,7 +1046,7 @@ static void UpdateWithName(UString &res, const UString &addString)
if (res.IsEmpty())
res = addString;
else
res = addString + WCHAR_PATH_SEPARATOR + res;
res.Insert(0, addString + WCHAR_PATH_SEPARATOR);
}
UString CInArchive::GetItemPath(int volIndex, int fsIndex, int refIndex,
@@ -851,7 +1070,7 @@ UString CInArchive::GetItemPath(int volIndex, int fsIndex, int refIndex,
if (showFsName)
{
wchar_t s[32];
ConvertUInt64ToString(fsIndex, s);
ConvertUInt32ToString(fsIndex, s);
UString newName = L"File Set ";
newName += s;
UpdateWithName(name, newName);
@@ -860,7 +1079,7 @@ UString CInArchive::GetItemPath(int volIndex, int fsIndex, int refIndex,
if (showVolName)
{
wchar_t s[32];
ConvertUInt64ToString(volIndex, s);
ConvertUInt32ToString(volIndex, s);
UString newName = s;
UString newName2 = vol.GetName();
if (newName2.IsEmpty())

54
CPP/7zip/Archive/Udf/UdfIn.h Executable file → Normal file
View File

@@ -3,11 +3,11 @@
#ifndef __ARCHIVE_UDF_IN_H
#define __ARCHIVE_UDF_IN_H
#include "Common/MyCom.h"
#include "Common/IntToString.h"
#include "Common/Buffer.h"
#include "Common/MyString.h"
#include "Common/MyMap.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/MyBuffer.h"
#include "../../../Common/MyCom.h"
#include "../../../Common/MyMap.h"
#include "../../../Common/MyString.h"
#include "../../IStream.h"
@@ -22,6 +22,7 @@ namespace NUdf {
struct CDString32
{
Byte Data[32];
void Parse(const Byte *buf);
// UString GetString() const;
};
@@ -30,13 +31,15 @@ struct CDString32
struct CDString128
{
Byte Data[128];
void Parse(const Byte *buf);
void Parse(const Byte *buf) { memcpy(Data, buf, sizeof(Data)); }
UString GetString() const;
};
struct CDString
{
CByteBuffer Data;
void Parse(const Byte *p, unsigned size);
UString GetString() const;
};
@@ -52,12 +55,12 @@ struct CTime
bool IsLocal() const { return GetType() == 1; }
int GetMinutesOffset() const
{
int t = (Data[0] | ((UInt16)Data[1] << 8)) & 0xFFF;
int t = (Data[0] | ((unsigned)Data[1] << 8)) & 0xFFF;
if ((t >> 11) != 0)
t -= (1 << 12);
return (t > (60 * 24) || t < -(60 * 24)) ? 0 : t;
}
unsigned GetYear() const { return (Data[2] | ((UInt16)Data[3] << 8)); }
unsigned GetYear() const { return (Data[2] | ((unsigned)Data[3] << 8)); }
void Parse(const Byte *buf);
};
@@ -208,8 +211,9 @@ struct CFile
// CByteBuffer ImplUse;
CDString Id;
CFile(): /* FileVersion(0), FileCharacteristics(0), */ ItemIndex(-1) {}
int ItemIndex;
CFile(): /* FileVersion(0), FileCharacteristics(0), */ ItemIndex(-1) {}
UString GetName() const { return Id.GetString(); }
};
@@ -217,7 +221,7 @@ struct CMyExtent
{
UInt32 Pos;
UInt32 Len;
int PartitionRef;
unsigned PartitionRef;
UInt32 GetLen() const { return Len & 0x3FFFFFFF; }
UInt32 GetType() const { return Len >> 30; }
@@ -254,7 +258,7 @@ struct CItem
bool IsRecAndAlloc() const
{
for (int i = 0; i < Extents.Size(); i++)
FOR_VECTOR (i, Extents)
if (!Extents[i].IsRecAndAlloc())
return false;
return true;
@@ -263,9 +267,9 @@ struct CItem
UInt64 GetChunksSumSize() const
{
if (IsInline)
return InlineData.GetCapacity();
return InlineData.Size();
UInt64 size = 0;
for (int i = 0; i < Extents.Size(); i++)
FOR_VECTOR (i, Extents)
size += Extents[i].GetLen();
return size;
}
@@ -331,7 +335,7 @@ struct CProgressVirt
class CInArchive
{
CMyComPtr<IInStream> _stream;
IInStream *_stream;
CProgressVirt *_progress;
HRESULT Read(int volIndex, int partitionRef, UInt32 blockPos, UInt32 len, Byte *buf);
@@ -351,17 +355,29 @@ class CInArchive
UInt32 _numExtents;
UInt64 _inlineExtentsSize;
bool CheckExtent(int volIndex, int partitionRef, UInt32 blockPos, UInt32 len) const;
public:
HRESULT Open(IInStream *inStream, CProgressVirt *progress);
void Clear();
public:
CObjectVector<CPartition> Partitions;
CObjectVector<CLogVol> LogVols;
CObjectVector<CItem> Items;
CObjectVector<CFile> Files;
int SecLogSize;
unsigned SecLogSize;
UInt64 PhySize;
UInt64 FileSize;
bool IsArc;
bool Unsupported;
bool UnexpectedEnd;
void UpdatePhySize(UInt64 val)
{
if (PhySize < val)
PhySize = val;
}
HRESULT Open(IInStream *inStream, CProgressVirt *progress);
void Clear();
UString GetComment() const;
UString GetItemPath(int volIndex, int fsIndex, int refIndex,
@@ -370,6 +386,8 @@ public:
bool CheckItemExtents(int volIndex, const CItem &item) const;
};
API_FUNC_IsArc IsArc_Udf(const Byte *p, size_t size);
}}
#endif

6
CPP/7zip/Archive/Udf/UdfRegister.cpp Executable file → Normal file
View File

@@ -5,9 +5,3 @@
#include "../../Common/RegisterArc.h"
#include "UdfHandler.h"
static IInArchive *CreateArc() { return new NArchive::NUdf::CHandler; }
static CArcInfo g_ArcInfo =
{ L"Udf", L"iso img", 0, 0xE0, { 0, 'N', 'S', 'R', '0' }, 5, false, CreateArc, 0 };
REGISTER_ARC(Udf)