📄 zipin.cpp
字号:
{
RINOK(ReadLocalItemAfterCdItem(item));
if (item.HasDescriptor())
{
RINOK(Seek(m_ArchiveInfo.Base + item.GetDataPosition() + item.PackSize));
if (ReadUInt32() != NSignature::kDataDescriptor)
return S_FALSE;
UInt32 crc = ReadUInt32();
UInt64 packSize, unpackSize;
/*
if (IsZip64)
{
packSize = ReadUInt64();
unpackSize = ReadUInt64();
}
else
*/
{
packSize = ReadUInt32();
unpackSize = ReadUInt32();
}
if (crc != item.FileCRC || item.PackSize != packSize || item.UnPackSize != unpackSize)
return S_FALSE;
}
}
catch(...) { return S_FALSE; }
return S_OK;
}
HRESULT CInArchive::ReadCdItem(CItemEx &item)
{
item.FromCentral = true;
const int kBufSize = 42;
Byte p[kBufSize];
SafeReadBytes(p, kBufSize);
item.MadeByVersion.Version = p[0];
item.MadeByVersion.HostOS = p[1];
item.ExtractVersion.Version = p[2];
item.ExtractVersion.HostOS = p[3];
item.Flags = Get16(p + 4);
item.CompressionMethod = Get16(p + 6);
item.Time = Get32(p + 8);
item.FileCRC = Get32(p + 12);
item.PackSize = Get32(p + 16);
item.UnPackSize = Get32(p + 20);
UInt16 headerNameSize = Get16(p + 24);
UInt16 headerExtraSize = Get16(p + 26);
UInt16 headerCommentSize = Get16(p + 28);
UInt32 headerDiskNumberStart = Get16(p + 30);
item.InternalAttributes = Get16(p + 32);
item.ExternalAttributes = Get32(p + 34);
item.LocalHeaderPosition = Get32(p + 38);
item.Name = ReadFileName(headerNameSize);
if (headerExtraSize > 0)
{
ReadExtra(headerExtraSize, item.CentralExtra, item.UnPackSize, item.PackSize,
item.LocalHeaderPosition, headerDiskNumberStart);
}
if (headerDiskNumberStart != 0)
throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
// May be these strings must be deleted
/*
if (item.IsDir())
item.UnPackSize = 0;
*/
ReadBuffer(item.Comment, headerCommentSize);
return S_OK;
}
HRESULT CInArchive::TryEcd64(UInt64 offset, CCdInfo &cdInfo)
{
RINOK(Seek(offset));
const UInt32 kEcd64Size = 56;
Byte buf[kEcd64Size];
if (!ReadBytesAndTestSize(buf, kEcd64Size))
return S_FALSE;
if (GetUInt32(buf) != NSignature::kZip64EndOfCentralDir)
return S_FALSE;
// cdInfo.NumEntries = GetUInt64(buf + 24);
cdInfo.Size = GetUInt64(buf + 40);
cdInfo.Offset = GetUInt64(buf + 48);
return S_OK;
}
HRESULT CInArchive::FindCd(CCdInfo &cdInfo)
{
UInt64 endPosition;
RINOK(m_Stream->Seek(0, STREAM_SEEK_END, &endPosition));
const UInt32 kBufSizeMax = (1 << 16) + kEcdSize + kZip64EcdLocatorSize;
Byte buf[kBufSizeMax];
UInt32 bufSize = (endPosition < kBufSizeMax) ? (UInt32)endPosition : kBufSizeMax;
if (bufSize < kEcdSize)
return S_FALSE;
UInt64 startPosition = endPosition - bufSize;
RINOK(m_Stream->Seek(startPosition, STREAM_SEEK_SET, &m_Position));
if (m_Position != startPosition)
return S_FALSE;
if (!ReadBytesAndTestSize(buf, bufSize))
return S_FALSE;
for (int i = (int)(bufSize - kEcdSize); i >= 0; i--)
{
if (GetUInt32(buf + i) == NSignature::kEndOfCentralDir)
{
if (i >= kZip64EcdLocatorSize)
{
const Byte *locator = buf + i - kZip64EcdLocatorSize;
if (GetUInt32(locator) == NSignature::kZip64EndOfCentralDirLocator)
{
UInt64 ecd64Offset = GetUInt64(locator + 8);
if (TryEcd64(ecd64Offset, cdInfo) == S_OK)
return S_OK;
if (TryEcd64(m_ArchiveInfo.StartPosition + ecd64Offset, cdInfo) == S_OK)
{
m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
return S_OK;
}
}
}
if (GetUInt32(buf + i + 4) == 0)
{
// cdInfo.NumEntries = GetUInt16(buf + i + 10);
cdInfo.Size = GetUInt32(buf + i + 12);
cdInfo.Offset = GetUInt32(buf + i + 16);
UInt64 curPos = endPosition - bufSize + i;
UInt64 cdEnd = cdInfo.Size + cdInfo.Offset;
if (curPos > cdEnd)
m_ArchiveInfo.Base = curPos - cdEnd;
return S_OK;
}
}
}
return S_FALSE;
}
HRESULT CInArchive::TryReadCd(CObjectVector<CItemEx> &items, UInt64 cdOffset, UInt64 cdSize, CProgressVirt *progress)
{
items.Clear();
RINOK(m_Stream->Seek(cdOffset, STREAM_SEEK_SET, &m_Position));
if (m_Position != cdOffset)
return S_FALSE;
while(m_Position - cdOffset < cdSize)
{
if (ReadUInt32() != NSignature::kCentralFileHeader)
return S_FALSE;
CItemEx cdItem;
RINOK(ReadCdItem(cdItem));
items.Add(cdItem);
if (progress && items.Size() % 1000 == 0)
RINOK(progress->SetCompleted(items.Size()));
}
return (m_Position - cdOffset == cdSize) ? S_OK : S_FALSE;
}
HRESULT CInArchive::ReadCd(CObjectVector<CItemEx> &items, UInt64 &cdOffset, UInt64 &cdSize, CProgressVirt *progress)
{
m_ArchiveInfo.Base = 0;
CCdInfo cdInfo;
RINOK(FindCd(cdInfo));
HRESULT res = S_FALSE;
cdSize = cdInfo.Size;
cdOffset = cdInfo.Offset;
res = TryReadCd(items, m_ArchiveInfo.Base + cdOffset, cdSize, progress);
if (res == S_FALSE && m_ArchiveInfo.Base == 0)
{
res = TryReadCd(items, cdInfo.Offset + m_ArchiveInfo.StartPosition, cdSize, progress);
if (res == S_OK)
m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
}
if (!ReadUInt32(m_Signature))
return S_FALSE;
return res;
}
HRESULT CInArchive::ReadLocalsAndCd(CObjectVector<CItemEx> &items, CProgressVirt *progress, UInt64 &cdOffset)
{
items.Clear();
while (m_Signature == NSignature::kLocalFileHeader)
{
// FSeek points to next byte after signature
// NFileHeader::CLocalBlock localHeader;
CItemEx item;
item.LocalHeaderPosition = m_Position - m_StreamStartPosition - 4; // points to signature;
RINOK(ReadLocalItem(item));
item.FromLocal = true;
ReadLocalItemDescriptor(item);
items.Add(item);
if (progress && items.Size() % 100 == 0)
RINOK(progress->SetCompleted(items.Size()));
if (!ReadUInt32(m_Signature))
break;
}
cdOffset = m_Position - 4;
for (int i = 0; i < items.Size(); i++)
{
if (progress && i % 1000 == 0)
RINOK(progress->SetCompleted(items.Size()));
if (m_Signature != NSignature::kCentralFileHeader)
return S_FALSE;
CItemEx cdItem;
RINOK(ReadCdItem(cdItem));
if (i == 0)
{
if (cdItem.LocalHeaderPosition == 0)
m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition;
}
int index;
int left = 0, right = items.Size();
for (;;)
{
if (left >= right)
return S_FALSE;
index = (left + right) / 2;
UInt64 position = items[index].LocalHeaderPosition - m_ArchiveInfo.Base;
if (cdItem.LocalHeaderPosition == position)
break;
if (cdItem.LocalHeaderPosition < position)
right = index;
else
left = index + 1;
}
CItemEx &item = items[index];
item.LocalHeaderPosition = cdItem.LocalHeaderPosition;
item.MadeByVersion = cdItem.MadeByVersion;
item.CentralExtra = cdItem.CentralExtra;
if (
// item.ExtractVersion != cdItem.ExtractVersion ||
item.Flags != cdItem.Flags ||
item.CompressionMethod != cdItem.CompressionMethod ||
// item.Time != cdItem.Time ||
item.FileCRC != cdItem.FileCRC)
return S_FALSE;
if (item.Name.Length() != cdItem.Name.Length() ||
item.PackSize != cdItem.PackSize ||
item.UnPackSize != cdItem.UnPackSize
)
return S_FALSE;
item.Name = cdItem.Name;
item.InternalAttributes = cdItem.InternalAttributes;
item.ExternalAttributes = cdItem.ExternalAttributes;
item.Comment = cdItem.Comment;
item.FromCentral = cdItem.FromCentral;
if (!ReadUInt32(m_Signature))
return S_FALSE;
}
return S_OK;
}
struct CEcd
{
UInt16 thisDiskNumber;
UInt16 startCDDiskNumber;
UInt16 numEntriesInCDOnThisDisk;
UInt16 numEntriesInCD;
UInt32 cdSize;
UInt32 cdStartOffset;
UInt16 commentSize;
void Parse(const Byte *p);
};
void CEcd::Parse(const Byte *p)
{
thisDiskNumber = Get16(p);
startCDDiskNumber = Get16(p + 2);
numEntriesInCDOnThisDisk = Get16(p + 4);
numEntriesInCD = Get16(p + 6);
cdSize = Get32(p + 8);
cdStartOffset = Get32(p + 12);
commentSize = Get16(p + 16);
}
struct CEcd64
{
UInt16 versionMade;
UInt16 versionNeedExtract;
UInt32 thisDiskNumber;
UInt32 startCDDiskNumber;
UInt64 numEntriesInCDOnThisDisk;
UInt64 numEntriesInCD;
UInt64 cdSize;
UInt64 cdStartOffset;
void Parse(const Byte *p);
CEcd64() { memset(this, 0, sizeof(*this)); }
};
void CEcd64::Parse(const Byte *p)
{
versionMade = Get16(p);
versionNeedExtract = Get16(p + 2);
thisDiskNumber = Get32(p + 4);
startCDDiskNumber = Get32(p + 8);
numEntriesInCDOnThisDisk = Get64(p + 12);
numEntriesInCD = Get64(p + 20);
cdSize = Get64(p + 28);
cdStartOffset = Get64(p + 36);
}
#define COPY_ECD_ITEM_16(n) if (!isZip64 || ecd. n != 0xFFFF) ecd64. n = ecd. n;
#define COPY_ECD_ITEM_32(n) if (!isZip64 || ecd. n != 0xFFFFFFFF) ecd64. n = ecd. n;
HRESULT CInArchive::ReadHeaders(CObjectVector<CItemEx> &items, CProgressVirt *progress)
{
// m_Signature must be kLocalFileHeaderSignature or
// kEndOfCentralDirSignature
// m_Position points to next byte after signature
IsZip64 = false;
items.Clear();
UInt64 cdSize, cdStartOffset;
HRESULT res = ReadCd(items, cdStartOffset, cdSize, progress);
if (res != S_FALSE && res != S_OK)
return res;
/*
if (res != S_OK)
return res;
res = S_FALSE;
*/
if (res == S_FALSE)
{
m_ArchiveInfo.Base = 0;
RINOK(m_Stream->Seek(m_ArchiveInfo.StartPosition, STREAM_SEEK_SET, &m_Position));
if (m_Position != m_ArchiveInfo.StartPosition)
return S_FALSE;
if (!ReadUInt32(m_Signature))
return S_FALSE;
RINOK(ReadLocalsAndCd(items, progress, cdStartOffset));
cdSize = (m_Position - 4) - cdStartOffset;
cdStartOffset -= m_ArchiveInfo.Base;
}
CEcd64 ecd64;
bool isZip64 = false;
UInt64 zip64EcdStartOffset = m_Position - 4 - m_ArchiveInfo.Base;
if (m_Signature == NSignature::kZip64EndOfCentralDir)
{
IsZip64 = isZip64 = true;
UInt64 recordSize = ReadUInt64();
const int kBufSize = kZip64EcdSize;
Byte buf[kBufSize];
SafeReadBytes(buf, kBufSize);
ecd64.Parse(buf);
IncreaseRealPosition(recordSize - kZip64EcdSize);
if (!ReadUInt32(m_Signature))
return S_FALSE;
if (ecd64.thisDiskNumber != 0 || ecd64.startCDDiskNumber != 0)
throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
if (ecd64.numEntriesInCDOnThisDisk != items.Size() ||
ecd64.numEntriesInCD != items.Size() ||
ecd64.cdSize != cdSize ||
(ecd64.cdStartOffset != cdStartOffset &&
(!items.IsEmpty())))
return S_FALSE;
}
if (m_Signature == NSignature::kZip64EndOfCentralDirLocator)
{
/* UInt32 startEndCDDiskNumber = */ ReadUInt32();
UInt64 endCDStartOffset = ReadUInt64();
/* UInt32 numberOfDisks = */ ReadUInt32();
if (zip64EcdStartOffset != endCDStartOffset)
return S_FALSE;
if (!ReadUInt32(m_Signature))
return S_FALSE;
}
if (m_Signature != NSignature::kEndOfCentralDir)
return S_FALSE;
const int kBufSize = kEcdSize - 4;
Byte buf[kBufSize];
SafeReadBytes(buf, kBufSize);
CEcd ecd;
ecd.Parse(buf);
COPY_ECD_ITEM_16(thisDiskNumber);
COPY_ECD_ITEM_16(startCDDiskNumber);
COPY_ECD_ITEM_16(numEntriesInCDOnThisDisk);
COPY_ECD_ITEM_16(numEntriesInCD);
COPY_ECD_ITEM_32(cdSize);
COPY_ECD_ITEM_32(cdStartOffset);
ReadBuffer(m_ArchiveInfo.Comment, ecd.commentSize);
if (ecd64.thisDiskNumber != 0 || ecd64.startCDDiskNumber != 0)
throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported);
if ((UInt16)ecd64.numEntriesInCDOnThisDisk != ((UInt16)items.Size()) ||
(UInt16)ecd64.numEntriesInCD != ((UInt16)items.Size()) ||
(UInt32)ecd64.cdSize != (UInt32)cdSize ||
((UInt32)(ecd64.cdStartOffset) != (UInt32)cdStartOffset &&
(!items.IsEmpty())))
return S_FALSE;
return S_OK;
}
ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size)
{
CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
CMyComPtr<ISequentialInStream> stream(streamSpec);
SeekInArchive(m_ArchiveInfo.Base + position);
streamSpec->SetStream(m_Stream);
streamSpec->Init(size);
return stream.Detach();
}
IInStream* CInArchive::CreateStream()
{
CMyComPtr<IInStream> stream = m_Stream;
return stream.Detach();
}
bool CInArchive::SeekInArchive(UInt64 position)
{
UInt64 newPosition;
if (m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition) != S_OK)
return false;
return (newPosition == position);
}
}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -