📄 zipin.cpp
字号:
// Archive/ZipIn.cpp
#include "StdAfx.h"
#include "ZipIn.h"
#include "Windows/Defs.h"
#include "Common/StringConvert.h"
#include "Common/DynamicBuffer.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/StreamUtils.h"
extern "C"
{
#include "../../../../C/CpuArch.h"
}
#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
#define Get64(p) GetUi64(p)
namespace NArchive {
namespace NZip {
// static const char kEndOfString = '\0';
HRESULT CInArchive::Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
Close();
RINOK(stream->Seek(0, STREAM_SEEK_CUR, &m_StreamStartPosition));
m_Position = m_StreamStartPosition;
RINOK(FindAndReadMarker(stream, searchHeaderSizeLimit));
RINOK(stream->Seek(m_Position, STREAM_SEEK_SET, NULL));
m_Stream = stream;
return S_OK;
}
void CInArchive::Close()
{
m_Stream.Release();
}
HRESULT CInArchive::Seek(UInt64 offset)
{
return m_Stream->Seek(offset, STREAM_SEEK_SET, NULL);
}
//////////////////////////////////////
// Markers
static inline bool TestMarkerCandidate(const Byte *p, UInt32 &value)
{
value = Get32(p);
return
(value == NSignature::kLocalFileHeader) ||
(value == NSignature::kEndOfCentralDir);
}
static const UInt32 kNumMarkerAddtionalBytes = 2;
static inline bool TestMarkerCandidate2(const Byte *p, UInt32 &value)
{
value = Get32(p);
if (value == NSignature::kEndOfCentralDir)
return (Get16(p + 4) == 0);
return (value == NSignature::kLocalFileHeader && p[4] < 128);
}
HRESULT CInArchive::FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
m_ArchiveInfo.Clear();
m_Position = m_StreamStartPosition;
Byte marker[NSignature::kMarkerSize];
RINOK(ReadStream_FALSE(stream, marker, NSignature::kMarkerSize));
m_Position += NSignature::kMarkerSize;
if (TestMarkerCandidate(marker, m_Signature))
return S_OK;
CByteDynamicBuffer dynamicBuffer;
const UInt32 kSearchMarkerBufferSize = 0x10000;
dynamicBuffer.EnsureCapacity(kSearchMarkerBufferSize);
Byte *buffer = dynamicBuffer;
UInt32 numBytesPrev = NSignature::kMarkerSize - 1;
memcpy(buffer, marker + 1, numBytesPrev);
UInt64 curTestPos = m_StreamStartPosition + 1;
for (;;)
{
if (searchHeaderSizeLimit != NULL)
if (curTestPos - m_StreamStartPosition > *searchHeaderSizeLimit)
break;
size_t numReadBytes = kSearchMarkerBufferSize - numBytesPrev;
RINOK(ReadStream(stream, buffer + numBytesPrev, &numReadBytes));
m_Position += numReadBytes;
UInt32 numBytesInBuffer = numBytesPrev + (UInt32)numReadBytes;
const UInt32 kMarker2Size = NSignature::kMarkerSize + kNumMarkerAddtionalBytes;
if (numBytesInBuffer < kMarker2Size)
break;
UInt32 numTests = numBytesInBuffer - kMarker2Size + 1;
for (UInt32 pos = 0; pos < numTests; pos++)
{
if (buffer[pos] != 0x50)
continue;
if (TestMarkerCandidate2(buffer + pos, m_Signature))
{
curTestPos += pos;
m_ArchiveInfo.StartPosition = curTestPos;
m_Position = curTestPos + NSignature::kMarkerSize;
return S_OK;
}
}
curTestPos += numTests;
numBytesPrev = numBytesInBuffer - numTests;
memmove(buffer, buffer + numTests, numBytesPrev);
}
return S_FALSE;
}
HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 *processedSize)
{
size_t realProcessedSize = size;
HRESULT result = ReadStream(m_Stream, data, &realProcessedSize);
if (processedSize != NULL)
*processedSize = (UInt32)realProcessedSize;
m_Position += realProcessedSize;
return result;
}
void CInArchive::IncreaseRealPosition(UInt64 addValue)
{
if (m_Stream->Seek(addValue, STREAM_SEEK_CUR, &m_Position) != S_OK)
throw CInArchiveException(CInArchiveException::kSeekStreamError);
}
bool CInArchive::ReadBytesAndTestSize(void *data, UInt32 size)
{
UInt32 realProcessedSize;
if (ReadBytes(data, size, &realProcessedSize) != S_OK)
throw CInArchiveException(CInArchiveException::kReadStreamError);
return (realProcessedSize == size);
}
void CInArchive::SafeReadBytes(void *data, UInt32 size)
{
if (!ReadBytesAndTestSize(data, size))
throw CInArchiveException(CInArchiveException::kUnexpectedEndOfArchive);
}
void CInArchive::ReadBuffer(CByteBuffer &buffer, UInt32 size)
{
buffer.SetCapacity(size);
if (size > 0)
SafeReadBytes(buffer, size);
}
Byte CInArchive::ReadByte()
{
Byte b;
SafeReadBytes(&b, 1);
return b;
}
UInt16 CInArchive::ReadUInt16()
{
UInt16 value = 0;
for (int i = 0; i < 2; i++)
value |= (((UInt16)ReadByte()) << (8 * i));
return value;
}
UInt32 CInArchive::ReadUInt32()
{
UInt32 value = 0;
for (int i = 0; i < 4; i++)
value |= (((UInt32)ReadByte()) << (8 * i));
return value;
}
UInt64 CInArchive::ReadUInt64()
{
UInt64 value = 0;
for (int i = 0; i < 8; i++)
value |= (((UInt64)ReadByte()) << (8 * i));
return value;
}
bool CInArchive::ReadUInt32(UInt32 &value)
{
value = 0;
for (int i = 0; i < 4; i++)
{
Byte b;
if (!ReadBytesAndTestSize(&b, 1))
return false;
value |= (UInt32(b) << (8 * i));
}
return true;
}
AString CInArchive::ReadFileName(UInt32 nameSize)
{
if (nameSize == 0)
return AString();
char *p = m_NameBuffer.GetBuffer(nameSize);
SafeReadBytes(p, nameSize);
p[nameSize] = 0;
m_NameBuffer.ReleaseBuffer();
return m_NameBuffer;
}
void CInArchive::GetArchiveInfo(CInArchiveInfo &archiveInfo) const
{
archiveInfo = m_ArchiveInfo;
}
/*
void CInArchive::ThrowIncorrectArchiveException()
{
throw CInArchiveException(CInArchiveException::kIncorrectArchive);
}
*/
static UInt32 GetUInt32(const Byte *data)
{
return
((UInt32)(Byte)data[0]) |
(((UInt32)(Byte)data[1]) << 8) |
(((UInt32)(Byte)data[2]) << 16) |
(((UInt32)(Byte)data[3]) << 24);
}
/*
static UInt16 GetUInt16(const Byte *data)
{
return
((UInt16)(Byte)data[0]) |
(((UInt16)(Byte)data[1]) << 8);
}
*/
static UInt64 GetUInt64(const Byte *data)
{
return GetUInt32(data) | ((UInt64)GetUInt32(data + 4) << 32);
}
void CInArchive::ReadExtra(UInt32 extraSize, CExtraBlock &extraBlock,
UInt64 &unpackSize, UInt64 &packSize, UInt64 &localHeaderOffset, UInt32 &diskStartNumber)
{
extraBlock.Clear();
UInt32 remain = extraSize;
while(remain >= 4)
{
CExtraSubBlock subBlock;
subBlock.ID = ReadUInt16();
UInt32 dataSize = ReadUInt16();
remain -= 4;
if (dataSize > remain) // it's bug
dataSize = remain;
if (subBlock.ID == NFileHeader::NExtraID::kZip64)
{
if (unpackSize == 0xFFFFFFFF)
{
if (dataSize < 8)
break;
unpackSize = ReadUInt64();
remain -= 8;
dataSize -= 8;
}
if (packSize == 0xFFFFFFFF)
{
if (dataSize < 8)
break;
packSize = ReadUInt64();
remain -= 8;
dataSize -= 8;
}
if (localHeaderOffset == 0xFFFFFFFF)
{
if (dataSize < 8)
break;
localHeaderOffset = ReadUInt64();
remain -= 8;
dataSize -= 8;
}
if (diskStartNumber == 0xFFFF)
{
if (dataSize < 4)
break;
diskStartNumber = ReadUInt32();
remain -= 4;
dataSize -= 4;
}
for (UInt32 i = 0; i < dataSize; i++)
ReadByte();
}
else
{
ReadBuffer(subBlock.Data, dataSize);
extraBlock.SubBlocks.Add(subBlock);
}
remain -= dataSize;
}
IncreaseRealPosition(remain);
}
HRESULT CInArchive::ReadLocalItem(CItemEx &item)
{
item.ExtractVersion.Version = ReadByte();
item.ExtractVersion.HostOS = ReadByte();
item.Flags = ReadUInt16();
item.CompressionMethod = ReadUInt16();
item.Time = ReadUInt32();
item.FileCRC = ReadUInt32();
item.PackSize = ReadUInt32();
item.UnPackSize = ReadUInt32();
UInt32 fileNameSize = ReadUInt16();
item.LocalExtraSize = ReadUInt16();
item.Name = ReadFileName(fileNameSize);
item.FileHeaderWithNameSize = 4 + NFileHeader::kLocalBlockSize + fileNameSize;
if (item.LocalExtraSize > 0)
{
UInt64 localHeaderOffset = 0;
UInt32 diskStartNumber = 0;
ReadExtra(item.LocalExtraSize, item.LocalExtra, item.UnPackSize, item.PackSize,
localHeaderOffset, diskStartNumber);
}
/*
if (item.IsDir())
item.UnPackSize = 0; // check It
*/
return S_OK;
}
HRESULT CInArchive::ReadLocalItemAfterCdItem(CItemEx &item)
{
if (item.FromLocal)
return S_OK;
try
{
RINOK(Seek(m_ArchiveInfo.Base + item.LocalHeaderPosition));
CItemEx localItem;
if (ReadUInt32() != NSignature::kLocalFileHeader)
return S_FALSE;
RINOK(ReadLocalItem(localItem));
if (item.Flags != localItem.Flags)
{
if (
(item.CompressionMethod != NFileHeader::NCompressionMethod::kDeflated ||
(item.Flags & 0x7FF9) != (localItem.Flags & 0x7FF9)) &&
(item.CompressionMethod != NFileHeader::NCompressionMethod::kStored ||
(item.Flags & 0x7FFF) != (localItem.Flags & 0x7FFF)) &&
(item.CompressionMethod != NFileHeader::NCompressionMethod::kImploded ||
(item.Flags & 0x7FFF) != (localItem.Flags & 0x7FFF))
)
return S_FALSE;
}
if (item.CompressionMethod != localItem.CompressionMethod ||
// item.Time != localItem.Time ||
(!localItem.HasDescriptor() &&
(
item.FileCRC != localItem.FileCRC ||
item.PackSize != localItem.PackSize ||
item.UnPackSize != localItem.UnPackSize
)
) ||
item.Name.Length() != localItem.Name.Length()
)
return S_FALSE;
item.FileHeaderWithNameSize = localItem.FileHeaderWithNameSize;
item.LocalExtraSize = localItem.LocalExtraSize;
item.LocalExtra = localItem.LocalExtra;
item.FromLocal = true;
}
catch(...) { return S_FALSE; }
return S_OK;
}
HRESULT CInArchive::ReadLocalItemDescriptor(CItemEx &item)
{
if (item.HasDescriptor())
{
const int kBufferSize = (1 << 12);
Byte buffer[kBufferSize];
UInt32 numBytesInBuffer = 0;
UInt32 packedSize = 0;
bool descriptorWasFound = false;
for (;;)
{
UInt32 processedSize;
RINOK(ReadBytes(buffer + numBytesInBuffer, kBufferSize - numBytesInBuffer, &processedSize));
numBytesInBuffer += processedSize;
if (numBytesInBuffer < NFileHeader::kDataDescriptorSize)
return S_FALSE;
UInt32 i;
for (i = 0; i <= numBytesInBuffer - NFileHeader::kDataDescriptorSize; i++)
{
// descriptorSignature field is Info-ZIP's extension
// to Zip specification.
UInt32 descriptorSignature = GetUInt32(buffer + i);
// !!!! It must be fixed for Zip64 archives
UInt32 descriptorPackSize = GetUInt32(buffer + i + 8);
if (descriptorSignature== NSignature::kDataDescriptor && descriptorPackSize == packedSize + i)
{
descriptorWasFound = true;
item.FileCRC = GetUInt32(buffer + i + 4);
item.PackSize = descriptorPackSize;
item.UnPackSize = GetUInt32(buffer + i + 12);
IncreaseRealPosition(Int64(Int32(0 - (numBytesInBuffer - i - NFileHeader::kDataDescriptorSize))));
break;
}
}
if (descriptorWasFound)
break;
packedSize += i;
int j;
for (j = 0; i < numBytesInBuffer; i++, j++)
buffer[j] = buffer[i];
numBytesInBuffer = j;
}
}
else
IncreaseRealPosition(item.PackSize);
return S_OK;
}
HRESULT CInArchive::ReadLocalItemAfterCdItemFull(CItemEx &item)
{
if (item.FromLocal)
return S_OK;
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -