mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-06 17:15:00 -06:00
4.52 beta
This commit is contained in:
committed by
Kornel Lesiński
parent
d14d4dcdef
commit
33ccab7e72
@@ -17,7 +17,6 @@ class CCabBlockInStream:
|
||||
Byte *_buffer;
|
||||
UInt32 _pos;
|
||||
UInt32 _size;
|
||||
int _align;
|
||||
|
||||
public:
|
||||
UInt32 TotalPackSize;
|
||||
@@ -25,24 +24,13 @@ public:
|
||||
bool DataError;
|
||||
bool MsZip;
|
||||
|
||||
CCabBlockInStream(): _buffer(0), ReservedSize(0), MsZip(false), DataError(false), _align(0), TotalPackSize(0) {}
|
||||
CCabBlockInStream(): _buffer(0), ReservedSize(0), MsZip(false), DataError(false), TotalPackSize(0) {}
|
||||
~CCabBlockInStream();
|
||||
bool Create();
|
||||
void SetStream(ISequentialInStream *stream) { _stream = stream; }
|
||||
|
||||
void InitForNewFolder()
|
||||
{
|
||||
_align = 0;
|
||||
TotalPackSize = 0;
|
||||
}
|
||||
|
||||
void InitForNewBlock()
|
||||
{
|
||||
_size = 0;
|
||||
_align = (_align + (int)TotalPackSize) & 1;
|
||||
}
|
||||
|
||||
int GetAlign() const { return _align; }
|
||||
void InitForNewFolder() { TotalPackSize = 0; }
|
||||
void InitForNewBlock() { _size = 0; }
|
||||
|
||||
MY_UNKNOWN_IMP
|
||||
|
||||
|
||||
@@ -769,7 +769,7 @@ STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems,
|
||||
res = deflateDecoder->Code(cabBlockInStream, outStream, NULL, &unpackRemain, NULL);
|
||||
break;
|
||||
case NHeader::NCompressionMethodMajor::kLZX:
|
||||
lzxDecoderSpec->SetKeepHistory(keepHistory, cabBlockInStreamSpec->GetAlign());
|
||||
lzxDecoderSpec->SetKeepHistory(keepHistory);
|
||||
res = lzxDecoder->Code(cabBlockInStream, outStream, NULL, &unpackRemain, NULL);
|
||||
break;
|
||||
case NHeader::NCompressionMethodMajor::kQuantum:
|
||||
|
||||
@@ -262,7 +262,7 @@ public:
|
||||
const CFilesDatabase *database,
|
||||
IArchiveExtractCallback *extractCallback,
|
||||
bool testMode);
|
||||
HRESULT FlushCorrupted();
|
||||
HRESULT FlushCorrupted(UInt64 maxSize);
|
||||
};
|
||||
|
||||
void CChmFolderOutStream::Init(
|
||||
@@ -356,7 +356,7 @@ HRESULT CChmFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *proce
|
||||
else
|
||||
{
|
||||
if (m_CurrentIndex >= m_NumFiles)
|
||||
return E_FAIL;
|
||||
return E_FAIL;
|
||||
int fullIndex = m_StartIndex + m_CurrentIndex;
|
||||
m_RemainFileSize = m_Database->GetFileSize(fullIndex);
|
||||
UInt64 fileOffset = m_Database->GetFileOffset(fullIndex);
|
||||
@@ -390,17 +390,21 @@ STDMETHODIMP CChmFolderOutStream::Write(const void *data, UInt32 size, UInt32 *p
|
||||
return Write2(data, size, processedSize, true);
|
||||
}
|
||||
|
||||
HRESULT CChmFolderOutStream::FlushCorrupted()
|
||||
HRESULT CChmFolderOutStream::FlushCorrupted(UInt64 maxSize)
|
||||
{
|
||||
const UInt32 kBufferSize = (1 << 10);
|
||||
Byte buffer[kBufferSize];
|
||||
for (int i = 0; i < kBufferSize; i++)
|
||||
buffer[i] = 0;
|
||||
while(m_PosInFolder < m_FolderSize)
|
||||
if (maxSize > m_FolderSize)
|
||||
maxSize = m_FolderSize;
|
||||
while (m_PosInFolder < maxSize)
|
||||
{
|
||||
UInt32 size = (UInt32)MyMin(m_FolderSize - m_PosInFolder, (UInt64)kBufferSize);
|
||||
UInt32 size = (UInt32)MyMin(maxSize - m_PosInFolder, (UInt64)kBufferSize);
|
||||
UInt32 processedSizeLocal = 0;
|
||||
RINOK(Write2(buffer, size, &processedSizeLocal, false));
|
||||
if (processedSizeLocal == 0)
|
||||
return S_OK;
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
@@ -688,24 +692,28 @@ STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems,
|
||||
UInt64 bCur = startBlock + b;
|
||||
if (bCur >= rt.ResetOffsets.Size())
|
||||
return E_FAIL;
|
||||
UInt64 startOffset = rt.ResetOffsets[(int)startBlock];
|
||||
UInt64 offset = rt.ResetOffsets[(int)bCur];
|
||||
UInt64 compressedSize;
|
||||
rt.GetCompressedSizeOfBlock(bCur, compressedSize);
|
||||
UInt64 rem = finishPos - chmFolderOutStream->m_PosInSection;
|
||||
if (rem > rt.BlockSize)
|
||||
rem = rt.BlockSize;
|
||||
// const UInt64 *offsets = (const UInt64 *)&rt.ResetOffsets.Front();
|
||||
RINOK(m_Stream->Seek(compressedPos + offset, STREAM_SEEK_SET, NULL));
|
||||
streamSpec->SetStream(m_Stream);
|
||||
streamSpec->Init(compressedSize);
|
||||
lzxDecoderSpec->SetKeepHistory(b > 0, (int)((offset - startOffset) & 1));
|
||||
RINOK(lzxDecoder->Code(inStream, outStream, NULL, &rem, NULL));
|
||||
lzxDecoderSpec->SetKeepHistory(b > 0);
|
||||
HRESULT res = lzxDecoder->Code(inStream, outStream, NULL, &rem, NULL);
|
||||
if (res != S_OK)
|
||||
{
|
||||
if (res != S_FALSE)
|
||||
return res;
|
||||
throw 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
RINOK(chmFolderOutStream->FlushCorrupted());
|
||||
RINOK(chmFolderOutStream->FlushCorrupted(unPackSize));
|
||||
}
|
||||
currentTotalSize += folderSize;
|
||||
if (folderIndex == lastFolderIndex)
|
||||
|
||||
264
CPP/7zip/Archive/Com/ComHandler.cpp
Executable file
264
CPP/7zip/Archive/Com/ComHandler.cpp
Executable file
@@ -0,0 +1,264 @@
|
||||
// ComHandler.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "Common/ComTry.h"
|
||||
#include "Windows/PropVariant.h"
|
||||
#include "../../Common/StreamUtils.h"
|
||||
#include "ComHandler.h"
|
||||
|
||||
namespace NArchive {
|
||||
namespace NCom {
|
||||
|
||||
STATPROPSTG kProperties[] =
|
||||
{
|
||||
{ NULL, kpidPath, VT_BSTR},
|
||||
{ NULL, kpidIsFolder, VT_BOOL},
|
||||
{ NULL, kpidSize, VT_UI8},
|
||||
// { NULL, kpidAttributes, VT_UI4},
|
||||
{ NULL, kpidPackedSize, VT_UI8},
|
||||
{ NULL, kpidCreationTime, VT_FILETIME},
|
||||
{ NULL, kpidLastWriteTime, VT_FILETIME}
|
||||
};
|
||||
|
||||
STDMETHODIMP CHandler::GetArchiveProperty(PROPID /* propID */, PROPVARIANT *value)
|
||||
{
|
||||
value->vt = VT_EMPTY;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetNumberOfProperties(UInt32 *numProperties)
|
||||
{
|
||||
*numProperties = sizeof(kProperties) / sizeof(kProperties[0]);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetPropertyInfo(UInt32 index,
|
||||
BSTR *name, PROPID *propID, VARTYPE *varType)
|
||||
{
|
||||
if(index >= sizeof(kProperties) / sizeof(kProperties[0]))
|
||||
return E_INVALIDARG;
|
||||
const STATPROPSTG &srcItem = kProperties[index];
|
||||
*propID = srcItem.propid;
|
||||
*varType = srcItem.vt;
|
||||
if (srcItem.lpwstrName == 0)
|
||||
*name = 0;
|
||||
else
|
||||
*name = ::SysAllocString(srcItem.lpwstrName);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetNumberOfArchiveProperties(UInt32 *numProperties)
|
||||
{
|
||||
*numProperties = 0;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetArchivePropertyInfo(UInt32 /* index */,
|
||||
BSTR * /* name */, PROPID * /* propID */, VARTYPE * /* varType */)
|
||||
{
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
NWindows::NCOM::CPropVariant propVariant;
|
||||
const CRef &ref = _db.Refs[index];
|
||||
const CItem &item = _db.Items[ref.Did];
|
||||
|
||||
switch(propID)
|
||||
{
|
||||
case kpidPath:
|
||||
{
|
||||
UString name = _db.GetItemPath(index);
|
||||
propVariant = name;
|
||||
break;
|
||||
}
|
||||
case kpidIsFolder:
|
||||
propVariant = item.IsDir();
|
||||
break;
|
||||
case kpidCreationTime:
|
||||
propVariant = item.CreationTime;
|
||||
break;
|
||||
case kpidLastWriteTime:
|
||||
propVariant = item.LastWriteTime;
|
||||
break;
|
||||
/*
|
||||
case kpidAttributes:
|
||||
propVariant = item.Falgs;
|
||||
break;
|
||||
*/
|
||||
case kpidPackedSize:
|
||||
if (!item.IsDir())
|
||||
{
|
||||
int numBits = _db.IsLargeStream(item.Size) ?
|
||||
_db.SectorSizeBits :
|
||||
_db.MiniSectorSizeBits;
|
||||
propVariant = (item.Size + ((UInt32)1 << numBits) - 1) >> numBits << numBits;
|
||||
break;
|
||||
}
|
||||
case kpidSize:
|
||||
if (!item.IsDir())
|
||||
propVariant = (UInt64)item.Size;
|
||||
break;
|
||||
}
|
||||
propVariant.Detach(value);
|
||||
return S_OK;
|
||||
COM_TRY_END
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::Open(IInStream *inStream,
|
||||
const UInt64 * /* maxCheckStartPosition */,
|
||||
IArchiveOpenCallback * /* openArchiveCallback */)
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
Close();
|
||||
try
|
||||
{
|
||||
if (OpenArchive(inStream, _db) != S_OK)
|
||||
return S_FALSE;
|
||||
_stream = inStream;
|
||||
}
|
||||
catch(...) { return S_FALSE; }
|
||||
return S_OK;
|
||||
COM_TRY_END
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::Close()
|
||||
{
|
||||
_db.Clear();
|
||||
_stream.Release();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems,
|
||||
Int32 _aTestMode, IArchiveExtractCallback *extractCallback)
|
||||
{
|
||||
COM_TRY_BEGIN
|
||||
bool testMode = (_aTestMode != 0);
|
||||
bool allFilesMode = (numItems == UInt32(-1));
|
||||
if (allFilesMode)
|
||||
numItems = _db.Refs.Size();
|
||||
if (numItems == 0)
|
||||
return S_OK;
|
||||
UInt32 i;
|
||||
UInt64 totalSize = 0;
|
||||
for(i = 0; i < numItems; i++)
|
||||
{
|
||||
const CItem &item = _db.Items[_db.Refs[allFilesMode ? i : indices[i]].Did];
|
||||
if (!item.IsDir())
|
||||
totalSize += item.Size;
|
||||
}
|
||||
RINOK(extractCallback->SetTotal(totalSize));
|
||||
|
||||
UInt64 currentTotalSize = 0, currentItemSize = 0;
|
||||
|
||||
CByteBuffer sect;
|
||||
sect.SetCapacity((UInt32)1 << _db.SectorSizeBits);
|
||||
|
||||
for (i = 0; i < numItems; i++, currentTotalSize += currentItemSize)
|
||||
{
|
||||
RINOK(extractCallback->SetCompleted(¤tTotalSize));
|
||||
Int32 index = allFilesMode ? i : indices[i];
|
||||
const CItem &item = _db.Items[_db.Refs[index].Did];
|
||||
currentItemSize = 0;
|
||||
if (!item.IsDir())
|
||||
currentItemSize = item.Size;
|
||||
|
||||
CMyComPtr<ISequentialOutStream> realOutStream;
|
||||
Int32 askMode = testMode ?
|
||||
NArchive::NExtract::NAskMode::kTest :
|
||||
NArchive::NExtract::NAskMode::kExtract;
|
||||
RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
|
||||
|
||||
if (item.IsDir())
|
||||
{
|
||||
RINOK(extractCallback->PrepareOperation(askMode));
|
||||
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
|
||||
continue;
|
||||
}
|
||||
if (!testMode && (!realOutStream))
|
||||
continue;
|
||||
RINOK(extractCallback->PrepareOperation(askMode));
|
||||
Int32 res = NArchive::NExtract::NOperationResult::kDataError;
|
||||
{
|
||||
UInt32 sid = item.Sid;
|
||||
UInt64 prev = 0;
|
||||
for (UInt64 pos = 0;;)
|
||||
{
|
||||
if (sid == NFatID::kEndOfChain)
|
||||
{
|
||||
if (pos != item.Size)
|
||||
break;
|
||||
res = NArchive::NExtract::NOperationResult::kOK;
|
||||
break;
|
||||
}
|
||||
if (pos >= item.Size)
|
||||
break;
|
||||
|
||||
UInt64 offset;
|
||||
UInt32 size;
|
||||
|
||||
if (_db.IsLargeStream(item.Size))
|
||||
{
|
||||
if (pos - prev > (1 << 20))
|
||||
{
|
||||
UInt64 processed = currentTotalSize + pos;
|
||||
RINOK(extractCallback->SetCompleted(&processed));
|
||||
prev = pos;
|
||||
}
|
||||
size = 1 << _db.SectorSizeBits;
|
||||
offset = ((UInt64)sid + 1) << _db.SectorSizeBits;
|
||||
if (sid >= _db.FatSize)
|
||||
break;
|
||||
sid = _db.Fat[sid];
|
||||
}
|
||||
else
|
||||
{
|
||||
int subBits = (_db.SectorSizeBits - _db.MiniSectorSizeBits);
|
||||
UInt32 fid = sid >> subBits;
|
||||
if (fid >= _db.NumSectorsInMiniStream)
|
||||
break;
|
||||
size = 1 << _db.MiniSectorSizeBits;
|
||||
offset = (((UInt64)_db.MiniSids[fid] + 1) << _db.SectorSizeBits) +
|
||||
((sid & ((1 << subBits) - 1)) << _db.MiniSectorSizeBits);
|
||||
if (sid >= _db.MatSize)
|
||||
break;
|
||||
sid = _db.Mat[sid];
|
||||
}
|
||||
|
||||
// last sector can be smaller than sector size (it can contain requied data only).
|
||||
UInt64 rem = item.Size - pos;
|
||||
if (size > rem)
|
||||
size = (UInt32)rem;
|
||||
|
||||
RINOK(_stream->Seek(offset, STREAM_SEEK_SET, NULL));
|
||||
UInt32 realProcessedSize;
|
||||
RINOK(ReadStream(_stream, sect, size, &realProcessedSize));
|
||||
if (realProcessedSize != size)
|
||||
break;
|
||||
|
||||
if (realOutStream)
|
||||
{
|
||||
RINOK(WriteStream(realOutStream, sect, size, &realProcessedSize));
|
||||
if (realProcessedSize != size)
|
||||
break;
|
||||
}
|
||||
pos += size;
|
||||
}
|
||||
}
|
||||
realOutStream.Release();
|
||||
RINOK(extractCallback->SetOperationResult(res));
|
||||
}
|
||||
return S_OK;
|
||||
COM_TRY_END
|
||||
}
|
||||
|
||||
STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
|
||||
{
|
||||
*numItems = _db.Refs.Size();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
}}
|
||||
28
CPP/7zip/Archive/Com/ComHandler.h
Executable file
28
CPP/7zip/Archive/Com/ComHandler.h
Executable file
@@ -0,0 +1,28 @@
|
||||
// ComHandler.h
|
||||
|
||||
#ifndef __ARCHIVE_COM_HANDLER_H
|
||||
#define __ARCHIVE_COM_HANDLER_H
|
||||
|
||||
#include "Common/MyCom.h"
|
||||
#include "../IArchive.h"
|
||||
#include "ComIn.h"
|
||||
|
||||
namespace NArchive {
|
||||
namespace NCom {
|
||||
|
||||
class CHandler:
|
||||
public IInArchive,
|
||||
public CMyUnknownImp
|
||||
{
|
||||
public:
|
||||
MY_UNKNOWN_IMP1(IInArchive)
|
||||
INTERFACE_IInArchive(;)
|
||||
|
||||
private:
|
||||
CMyComPtr<IInStream> _stream;
|
||||
CDatabase _db;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
371
CPP/7zip/Archive/Com/ComIn.cpp
Executable file
371
CPP/7zip/Archive/Com/ComIn.cpp
Executable file
@@ -0,0 +1,371 @@
|
||||
// Archive/ComIn.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../../../../C/Alloc.h"
|
||||
}
|
||||
|
||||
#include "Common/MyCom.h"
|
||||
#include "../../Common/StreamUtils.h"
|
||||
#include "Common/IntToString.h"
|
||||
|
||||
#include "ComIn.h"
|
||||
|
||||
namespace NArchive{
|
||||
namespace NCom{
|
||||
|
||||
static const UInt32 kSignatureSize = 8;
|
||||
static const Byte kSignature[kSignatureSize] = { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 };
|
||||
|
||||
static HRESULT ReadBytes(ISequentialInStream *inStream, void *data, UInt32 size)
|
||||
{
|
||||
UInt32 realProcessedSize;
|
||||
RINOK(ReadStream(inStream, data, size, &realProcessedSize));
|
||||
return (realProcessedSize == size) ? S_OK : S_FALSE;
|
||||
}
|
||||
|
||||
#ifdef LITTLE_ENDIAN_UNALIGN
|
||||
#define GetUi16(p) (*(const UInt16 *)(p))
|
||||
#define GetUi32(p) (*(const UInt32 *)(p))
|
||||
#else
|
||||
#define GetUi16(p) ((p)[0] | ((UInt16)(p)[1] << 8))
|
||||
#define GetUi32(p) ((p)[0] | ((UInt32)(p)[1] << 8) | ((UInt32)(p)[2] << 16) | ((UInt32)(p)[3] << 24))
|
||||
#endif
|
||||
|
||||
|
||||
void CUInt32Buf::Free()
|
||||
{
|
||||
MyFree(_buf);
|
||||
_buf = 0;
|
||||
}
|
||||
|
||||
bool CUInt32Buf::Allocate(UInt32 numItems)
|
||||
{
|
||||
Free();
|
||||
if (numItems == 0)
|
||||
return true;
|
||||
size_t newSize = (size_t)numItems * sizeof(UInt32);
|
||||
if (newSize / sizeof(UInt32) != numItems)
|
||||
return false;
|
||||
_buf = (UInt32 *)MyAlloc(newSize);
|
||||
return (_buf != 0);
|
||||
}
|
||||
|
||||
static HRESULT ReadSector(IInStream *inStream, Byte *buf, int sectorSizeBits, UInt32 sid)
|
||||
{
|
||||
RINOK(inStream->Seek((((UInt64)sid + 1) << sectorSizeBits), STREAM_SEEK_SET, NULL));
|
||||
return ReadBytes(inStream, buf, (UInt32)1 << sectorSizeBits);
|
||||
}
|
||||
|
||||
static HRESULT ReadIDs(IInStream *inStream, Byte *buf, int sectorSizeBits, UInt32 sid, UInt32 *dest)
|
||||
{
|
||||
RINOK(ReadSector(inStream, buf, sectorSizeBits, sid));
|
||||
UInt32 sectorSize = (UInt32)1 << sectorSizeBits;
|
||||
for (UInt32 t = 0; t < sectorSize; t += 4)
|
||||
*dest++ = GetUi32(buf + t);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static void GetFileTimeFromMem(const Byte *p, FILETIME *ft)
|
||||
{
|
||||
ft->dwLowDateTime = GetUi32(p);
|
||||
ft->dwHighDateTime = GetUi32(p + 4);
|
||||
}
|
||||
|
||||
static void ReadItem(Byte *p, CItem &item, bool mode64bit)
|
||||
{
|
||||
memcpy(item.Name, p, 64);
|
||||
// item.NameSize = GetUi16(p + 64);
|
||||
item.Type = p[66];
|
||||
item.LeftDid = GetUi32(p + 68);
|
||||
item.RightDid = GetUi32(p + 72);
|
||||
item.SonDid = GetUi32(p + 76);
|
||||
// item.Flags = GetUi32(p + 96);
|
||||
GetFileTimeFromMem(p + 100, &item.CreationTime);
|
||||
GetFileTimeFromMem(p + 108, &item.LastWriteTime);
|
||||
item.Sid = GetUi32(p + 116);
|
||||
item.Size = GetUi32(p + 120);
|
||||
if (mode64bit)
|
||||
item.Size |= ((UInt64)GetUi32(p + 124) << 32);
|
||||
}
|
||||
|
||||
static const UInt32 kNoDid = 0xFFFFFFFF;
|
||||
|
||||
HRESULT CDatabase::AddNode(int parent, UInt32 did)
|
||||
{
|
||||
if (did == kNoDid)
|
||||
return S_OK;
|
||||
if (did >= (UInt32)Items.Size())
|
||||
return S_FALSE;
|
||||
const CItem &item = Items[did];
|
||||
if (item.IsEmpty())
|
||||
return S_FALSE;
|
||||
CRef ref;
|
||||
ref.Parent = parent;
|
||||
ref.Did = did;
|
||||
int index = Refs.Add(ref);
|
||||
if (Refs.Size() > Items.Size())
|
||||
return S_FALSE;
|
||||
RINOK(AddNode(parent, item.LeftDid));
|
||||
RINOK(AddNode(parent, item.RightDid));
|
||||
if (item.IsDir())
|
||||
{
|
||||
RINOK(AddNode(index, item.SonDid));
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static const char kCharOpenBracket = '[';
|
||||
static const char kCharCloseBracket = ']';
|
||||
|
||||
UString DWORDToString(UInt32 val)
|
||||
{
|
||||
wchar_t buf[32];
|
||||
ConvertUInt64ToString(val, buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static UString CompoundNameToFileName(const UString &s)
|
||||
{
|
||||
UString res;
|
||||
for (int i = 0; i < s.Length(); i++)
|
||||
{
|
||||
wchar_t c = s[i];
|
||||
if (c < 0x20)
|
||||
{
|
||||
res += kCharOpenBracket;
|
||||
res += DWORDToString(c);
|
||||
res += kCharCloseBracket;
|
||||
}
|
||||
else
|
||||
res += c;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static char g_MsiChars[] =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._";
|
||||
|
||||
static const wchar_t *kMsi_ID = L""; // L"{msi}";
|
||||
|
||||
static const int kMsiNumBits = 6;
|
||||
static const UInt32 kMsiNumChars = 1 << kMsiNumBits;
|
||||
static const UInt32 kMsiCharMask = kMsiNumChars - 1;
|
||||
static const UInt32 kMsiStartUnicodeChar = 0x3800;
|
||||
static const UInt32 kMsiUnicodeRange = kMsiNumChars * (kMsiNumChars + 1);
|
||||
|
||||
bool CompoundMsiNameToFileName(const UString &name, UString &resultName)
|
||||
{
|
||||
resultName.Empty();
|
||||
for (int i = 0; i < name.Length(); i++)
|
||||
{
|
||||
wchar_t c = name[i];
|
||||
if (c < kMsiStartUnicodeChar || c > kMsiStartUnicodeChar + kMsiUnicodeRange)
|
||||
return false;
|
||||
if (i == 0)
|
||||
resultName += kMsi_ID;
|
||||
c -= kMsiStartUnicodeChar;
|
||||
|
||||
UInt32 c0 = c & kMsiCharMask;
|
||||
UInt32 c1 = c >> kMsiNumBits;
|
||||
|
||||
if (c1 <= kMsiNumChars)
|
||||
{
|
||||
resultName += (wchar_t)g_MsiChars[c0];
|
||||
if (c1 == kMsiNumChars)
|
||||
break;
|
||||
resultName += (wchar_t)g_MsiChars[c1];
|
||||
}
|
||||
else
|
||||
resultName += L'!';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static UString ConvertName(const Byte *p)
|
||||
{
|
||||
UString s;
|
||||
for (int i = 0; i < kNameSizeMax; i += 2)
|
||||
{
|
||||
wchar_t c = (p[i] | (wchar_t)p[i + 1] << 8);
|
||||
if (c == 0)
|
||||
break;
|
||||
s += c;
|
||||
}
|
||||
UString msiName;
|
||||
if (CompoundMsiNameToFileName(s, msiName))
|
||||
return msiName;
|
||||
return CompoundNameToFileName(s);
|
||||
}
|
||||
|
||||
UString CDatabase::GetItemPath(UInt32 index) const
|
||||
{
|
||||
UString s;
|
||||
while (index != kNoDid)
|
||||
{
|
||||
const CRef &ref = Refs[index];
|
||||
const CItem &item = Items[ref.Did];
|
||||
if (!s.IsEmpty())
|
||||
s = (UString)WCHAR_PATH_SEPARATOR + s;
|
||||
s = ConvertName(item.Name) + s;
|
||||
index = ref.Parent;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
HRESULT OpenArchive(IInStream *inStream, CDatabase &db)
|
||||
{
|
||||
static const UInt32 kHeaderSize = 512;
|
||||
Byte p[kHeaderSize];
|
||||
RINOK(ReadBytes(inStream, p, kHeaderSize));
|
||||
if (memcmp(p, kSignature, kSignatureSize) != 0)
|
||||
return S_FALSE;
|
||||
UInt16 majorVer = GetUi16(p + 0x1A);
|
||||
if (majorVer > 4)
|
||||
return S_FALSE;
|
||||
if (GetUi16(p + 0x1C) != 0xFFFE)
|
||||
return S_FALSE;
|
||||
UInt16 sectorSizeBits = GetUi16(p + 0x1E);
|
||||
bool mode64bit = (sectorSizeBits >= 12);
|
||||
UInt16 miniSectorSizeBits = GetUi16(p + 0x20);
|
||||
db.SectorSizeBits = sectorSizeBits;
|
||||
db.MiniSectorSizeBits = miniSectorSizeBits;
|
||||
|
||||
if (sectorSizeBits > 28 || miniSectorSizeBits > 28 ||
|
||||
sectorSizeBits < 7 || miniSectorSizeBits < 2 || miniSectorSizeBits > sectorSizeBits)
|
||||
return S_FALSE;
|
||||
UInt32 numSectorsForFAT = GetUi32(p + 0x2C);
|
||||
db.LongStreamMinSize = GetUi32(p + 0x38);
|
||||
|
||||
UInt32 sectSize = (UInt32)1 << (int)(sectorSizeBits);
|
||||
|
||||
CByteBuffer sect;
|
||||
sect.SetCapacity(sectSize);
|
||||
|
||||
int ssb2 = (int)(sectorSizeBits - 2);
|
||||
UInt32 numSidsInSec = (UInt32)1 << ssb2;
|
||||
UInt32 numFatItems = numSectorsForFAT << ssb2;
|
||||
if ((numFatItems >> ssb2) != numSectorsForFAT)
|
||||
return S_FALSE;
|
||||
db.FatSize = numFatItems;
|
||||
|
||||
{
|
||||
CUInt32Buf bat;
|
||||
UInt32 numSectorsForBat = GetUi32(p + 0x48);
|
||||
const UInt32 kNumHeaderBatItems = 109;
|
||||
UInt32 numBatItems = kNumHeaderBatItems + (numSectorsForBat << ssb2);
|
||||
if (numBatItems < kNumHeaderBatItems || ((numBatItems - kNumHeaderBatItems) >> ssb2) != numSectorsForBat)
|
||||
return S_FALSE;
|
||||
if (!bat.Allocate(numBatItems))
|
||||
return S_FALSE;
|
||||
UInt32 i;
|
||||
for (i = 0; i < kNumHeaderBatItems; i++)
|
||||
bat[i] = GetUi32(p + 0x4c + i * 4);
|
||||
UInt32 sid = GetUi32(p + 0x44);
|
||||
for (UInt32 s = 0; s < numSectorsForBat; s++)
|
||||
{
|
||||
RINOK(ReadIDs(inStream, sect, sectorSizeBits, sid, bat + i));
|
||||
i += numSidsInSec - 1;
|
||||
sid = bat[i];
|
||||
}
|
||||
numBatItems = i;
|
||||
|
||||
if (!db.Fat.Allocate(numFatItems))
|
||||
return S_FALSE;
|
||||
UInt32 j = 0;
|
||||
|
||||
for (i = 0; i < numFatItems; j++, i += numSidsInSec)
|
||||
{
|
||||
if (j >= numBatItems)
|
||||
return S_FALSE;
|
||||
RINOK(ReadIDs(inStream, sect, sectorSizeBits, bat[j], db.Fat + i));
|
||||
}
|
||||
}
|
||||
|
||||
UInt32 numMatItems;
|
||||
{
|
||||
UInt32 numSectorsForMat = GetUi32(p + 0x40);
|
||||
numMatItems = (UInt32)numSectorsForMat << ssb2;
|
||||
if ((numMatItems >> ssb2) != numSectorsForMat)
|
||||
return S_FALSE;
|
||||
if (!db.Mat.Allocate(numMatItems))
|
||||
return S_FALSE;
|
||||
UInt32 i;
|
||||
UInt32 sid = GetUi32(p + 0x3C);
|
||||
for (i = 0; i < numMatItems; i += numSidsInSec)
|
||||
{
|
||||
RINOK(ReadIDs(inStream, sect, sectorSizeBits, sid, db.Mat + i));
|
||||
if (sid >= numFatItems)
|
||||
return S_FALSE;
|
||||
sid = db.Fat[sid];
|
||||
}
|
||||
if (sid != NFatID::kEndOfChain)
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
{
|
||||
UInt32 sid = GetUi32(p + 0x30);
|
||||
for (;;)
|
||||
{
|
||||
if (sid >= numFatItems)
|
||||
return S_FALSE;
|
||||
RINOK(ReadSector(inStream, sect, sectorSizeBits, sid));
|
||||
for (UInt32 i = 0; i < sectSize; i += 128)
|
||||
{
|
||||
CItem item;
|
||||
ReadItem(sect + i, item, mode64bit);
|
||||
db.Items.Add(item);
|
||||
}
|
||||
sid = db.Fat[sid];
|
||||
if (sid == NFatID::kEndOfChain)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CItem root = db.Items[0];
|
||||
|
||||
{
|
||||
UInt32 numSectorsInMiniStream;
|
||||
{
|
||||
UInt64 numSatSects64 = (root.Size + sectSize - 1) >> sectorSizeBits;
|
||||
if (numSatSects64 > NFatID::kMaxValue)
|
||||
return S_FALSE;
|
||||
numSectorsInMiniStream = (UInt32)numSatSects64;
|
||||
}
|
||||
db.NumSectorsInMiniStream = numSectorsInMiniStream;
|
||||
if (!db.MiniSids.Allocate(numSectorsInMiniStream))
|
||||
return S_FALSE;
|
||||
{
|
||||
UInt64 matSize64 = (root.Size + (1 << miniSectorSizeBits) - 1) >> miniSectorSizeBits;
|
||||
if (matSize64 > NFatID::kMaxValue)
|
||||
return S_FALSE;
|
||||
db.MatSize = (UInt32)matSize64;
|
||||
if (numMatItems < db.MatSize)
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
|
||||
UInt32 sid = root.Sid;
|
||||
for (UInt32 i = 0; ; i++)
|
||||
{
|
||||
if (sid == NFatID::kEndOfChain)
|
||||
{
|
||||
if (i != numSectorsInMiniStream)
|
||||
return S_FALSE;
|
||||
break;
|
||||
}
|
||||
if (i >= numSectorsInMiniStream)
|
||||
return S_FALSE;
|
||||
db.MiniSids[i] = sid;
|
||||
if (sid >= numFatItems)
|
||||
return S_FALSE;
|
||||
sid = db.Fat[sid];
|
||||
}
|
||||
}
|
||||
|
||||
return db.AddNode(-1, root.SonDid);
|
||||
}
|
||||
|
||||
}}
|
||||
106
CPP/7zip/Archive/Com/ComIn.h
Executable file
106
CPP/7zip/Archive/Com/ComIn.h
Executable file
@@ -0,0 +1,106 @@
|
||||
// Archive/ComIn.h
|
||||
|
||||
#ifndef __ARCHIVE_COM_IN_H
|
||||
#define __ARCHIVE_COM_IN_H
|
||||
|
||||
#include "Common/MyString.h"
|
||||
#include "Common/Buffer.h"
|
||||
|
||||
namespace NArchive {
|
||||
namespace NCom {
|
||||
|
||||
struct CUInt32Buf
|
||||
{
|
||||
UInt32 *_buf;
|
||||
public:
|
||||
CUInt32Buf(): _buf(0) {}
|
||||
~CUInt32Buf() { Free(); }
|
||||
void Free();
|
||||
bool Allocate(UInt32 numItems);
|
||||
operator UInt32 *() const { return _buf; };
|
||||
};
|
||||
|
||||
namespace NFatID
|
||||
{
|
||||
const UInt32 kFree = 0xFFFFFFFF;
|
||||
const UInt32 kEndOfChain = 0xFFFFFFFE;
|
||||
const UInt32 kFatSector = 0xFFFFFFFD;
|
||||
const UInt32 kMatSector = 0xFFFFFFFC;
|
||||
const UInt32 kMaxValue = 0xFFFFFFFA;
|
||||
}
|
||||
|
||||
namespace NItemType
|
||||
{
|
||||
const Byte kEmpty = 0;
|
||||
const Byte kStorage = 1;
|
||||
const Byte kStream = 2;
|
||||
const Byte kLockBytes = 3;
|
||||
const Byte kProperty = 4;
|
||||
const Byte kRootStorage = 5;
|
||||
}
|
||||
|
||||
const UInt32 kNameSizeMax = 64;
|
||||
|
||||
struct CItem
|
||||
{
|
||||
Byte Name[kNameSizeMax];
|
||||
// UInt16 NameSize;
|
||||
// UInt32 Flags;
|
||||
FILETIME CreationTime;
|
||||
FILETIME LastWriteTime;
|
||||
UInt64 Size;
|
||||
UInt32 LeftDid;
|
||||
UInt32 RightDid;
|
||||
UInt32 SonDid;
|
||||
UInt32 Sid;
|
||||
Byte Type;
|
||||
|
||||
bool IsEmpty() const { return Type == NItemType::kEmpty; }
|
||||
bool IsDir() const { return Type == NItemType::kStorage || Type == NItemType::kRootStorage; }
|
||||
};
|
||||
|
||||
struct CRef
|
||||
{
|
||||
int Parent;
|
||||
UInt32 Did;
|
||||
};
|
||||
|
||||
class CDatabase
|
||||
{
|
||||
public:
|
||||
HRESULT AddNode(int parent, UInt32 did);
|
||||
|
||||
CUInt32Buf Fat;
|
||||
UInt32 FatSize;
|
||||
|
||||
CUInt32Buf MiniSids;
|
||||
UInt32 NumSectorsInMiniStream;
|
||||
|
||||
CUInt32Buf Mat;
|
||||
UInt32 MatSize;
|
||||
|
||||
CObjectVector<CItem> Items;
|
||||
CRecordVector<CRef> Refs;
|
||||
|
||||
UInt32 LongStreamMinSize;
|
||||
int SectorSizeBits;
|
||||
int MiniSectorSizeBits;
|
||||
|
||||
void Clear()
|
||||
{
|
||||
Fat.Free();
|
||||
MiniSids.Free();
|
||||
Mat.Free();
|
||||
Items.Clear();
|
||||
Refs.Clear();
|
||||
}
|
||||
|
||||
bool IsLargeStream(UInt64 size) { return size >= LongStreamMinSize; }
|
||||
UString GetItemPath(UInt32 index) const;
|
||||
};
|
||||
|
||||
HRESULT OpenArchive(IInStream *inStream, CDatabase &database);
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
13
CPP/7zip/Archive/Com/ComRegister.cpp
Executable file
13
CPP/7zip/Archive/Com/ComRegister.cpp
Executable file
@@ -0,0 +1,13 @@
|
||||
// ComRegister.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "../../Common/RegisterArc.h"
|
||||
|
||||
#include "ComHandler.h"
|
||||
static IInArchive *CreateArc() { return new NArchive::NCom::CHandler; }
|
||||
|
||||
static CArcInfo g_ArcInfo =
|
||||
{ L"Com", L"msi doc xls ppt", 0, 0xE5, { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }, 8, false, CreateArc, 0 };
|
||||
|
||||
REGISTER_ARC(Com)
|
||||
@@ -123,7 +123,7 @@ HRESULT CUnpacker::Unpack(IInStream *inStream, const CResource &resource,
|
||||
if (outProcessed + outSize > resource.UnpackSize)
|
||||
outSize = (UInt32)(resource.UnpackSize - outProcessed);
|
||||
UInt64 outSize64 = outSize;
|
||||
lzxDecoderSpec->SetKeepHistory(false, 0);
|
||||
lzxDecoderSpec->SetKeepHistory(false);
|
||||
ICompressCoder *coder = (inSize == outSize) ? copyCoder : lzxDecoder;
|
||||
RINOK(coder->Code(limitedStreamSpec, outStream, NULL, &outSize64, NULL));
|
||||
outProcessed += outSize;
|
||||
|
||||
Reference in New Issue
Block a user