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

118
CPP/7zip/Archive/Chm/ChmHandler.cpp Executable file → Normal file
View File

@@ -2,17 +2,17 @@
#include "StdAfx.h"
#include "Common/ComTry.h"
#include "Common/Defs.h"
#include "Common/StringConvert.h"
#include "Common/UTFConvert.h"
#include "../../../Common/ComTry.h"
#include "../../../Common/StringConvert.h"
#include "../../../Common/UTFConvert.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/StreamUtils.h"
#include "../../Common/RegisterArc.h"
#include "../../Compress/CopyCoder.h"
#include "../../Compress/LzxDecoder.h"
@@ -38,44 +38,45 @@ enum
#endif
STATPROPSTG kProps[] =
static const Byte kProps[] =
{
{ NULL, kpidPath, VT_BSTR},
{ NULL, kpidSize, VT_UI8},
{ NULL, kpidMethod, VT_BSTR},
{ NULL, kpidBlock, VT_UI4}
kpidPath,
kpidSize,
kpidMethod,
kpidBlock
#ifdef _CHM_DETAILS
,
{ L"Section", kpidSection, VT_UI4},
{ NULL, kpidOffset, VT_UI4}
L"Section", kpidSection,
kpidOffset
#endif
};
STATPROPSTG kArcProps[] =
/*
static const Byte kArcProps[] =
{
{ NULL, kpidNumBlocks, VT_UI8}
// kpidNumBlocks,
};
*/
IMP_IInArchive_Props
IMP_IInArchive_ArcProps_NO
/*
IMP_IInArchive_ArcProps
IMP_IInArchive_ArcProps_NO_Table
STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant prop;
switch(propID)
// COM_TRY_BEGIN
NCOM::CPropVariant prop;
switch (propID)
{
/*
case kpidNumBlocks:
{
UInt64 numBlocks = 0;
for (int i = 0; i < m_Database.Sections.Size(); i++)
FOR_VECTOR(i, m_Database.Sections)
{
const CSectionInfo &s = m_Database.Sections[i];
for (int j = 0; j < s.Methods.Size(); j++)
FOR_VECTOR(j, s.Methods)
{
const CMethodInfo &m = s.Methods[j];
if (m.IsLzx())
@@ -85,23 +86,27 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
prop = numBlocks;
break;
}
*/
case kpidOffset: prop = m_Database.StartPosition; break;
case kpidPhySize: prop = m_Database.PhySize; break;
case kpidErrorFlags: prop = m_ErrorFlags; break;
}
prop.Detach(value);
return S_OK;
COM_TRY_END
// COM_TRY_END
}
*/
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant prop;
NCOM::CPropVariant prop;
if (m_Database.NewFormat)
{
switch(propID)
{
case kpidSize:
prop = (UInt64)m_Database.NewFormatString.Length();
prop = (UInt64)m_Database.NewFormatString.Len();
break;
}
prop.Detach(value);
@@ -113,7 +118,7 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *va
else
entryIndex = m_Database.Indices[index];
const CItem &item = m_Database.Items[entryIndex];
switch(propID)
switch (propID)
{
case kpidPath:
{
@@ -122,7 +127,7 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *va
{
if (!m_Database.LowLevel)
{
if (us.Length() > 1)
if (us.Len() > 1)
if (us[0] == L'/')
us.Delete(0);
}
@@ -160,6 +165,7 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *va
COM_TRY_END
}
/*
class CProgressImp: public CProgressVirt
{
CMyComPtr<IArchiveOpenCallback> _callback;
@@ -182,18 +188,25 @@ STDMETHODIMP CProgressImp::SetCompleted(const UInt64 *numFiles)
return _callback->SetCompleted(numFiles, NULL);
return S_OK;
}
*/
STDMETHODIMP CHandler::Open(IInStream *inStream,
const UInt64 *maxCheckStartPosition,
IArchiveOpenCallback * /* openArchiveCallback */)
{
COM_TRY_BEGIN
m_Stream.Release();
Close();
try
{
CInArchive archive;
CInArchive archive(_help2);
// CProgressImp progressImp(openArchiveCallback);
RINOK(archive.Open(inStream, maxCheckStartPosition, m_Database));
HRESULT res = archive.Open(inStream, maxCheckStartPosition, m_Database);
if (!archive.IsArc) m_ErrorFlags |= kpv_ErrorFlags_IsNotArc;
if (archive.HeadersError) m_ErrorFlags |= kpv_ErrorFlags_HeadersError;
if (archive.UnexpectedEnd) m_ErrorFlags |= kpv_ErrorFlags_UnexpectedEnd;
if (archive.UnsupportedFeature) m_ErrorFlags |= kpv_ErrorFlags_UnsupportedFeature;
RINOK(res);
/*
if (m_Database.LowLevel)
return S_FALSE;
@@ -210,6 +223,7 @@ STDMETHODIMP CHandler::Open(IInStream *inStream,
STDMETHODIMP CHandler::Close()
{
m_ErrorFlags = 0;
m_Database.Clear();
m_Stream.Release();
return S_OK;
@@ -402,7 +416,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
Int32 testModeSpec, IArchiveExtractCallback *extractCallback)
{
COM_TRY_BEGIN
bool allFilesMode = (numItems == (UInt32)-1);
bool allFilesMode = (numItems == (UInt32)(Int32)-1);
if (allFilesMode)
numItems = m_Database.NewFormat ? 1:
@@ -432,7 +446,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
UInt64 currentItemSize = 0;
UInt64 totalSize = 0;
if (m_Database.NewFormat)
totalSize = m_Database.NewFormatString.Length();
totalSize = m_Database.NewFormatString.Len();
else
for (i = 0; i < numItems; i++)
totalSize += m_Database.Items[allFilesMode ? i : indices[i]].Size;
@@ -460,7 +474,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
continue;
if (!testMode)
{
UInt32 size = m_Database.NewFormatString.Length();
UInt32 size = m_Database.NewFormatString.Len();
RINOK(WriteStream(realOutStream, (const char *)m_Database.NewFormatString, size));
}
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kOK));
@@ -475,7 +489,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
RINOK(extractCallback->PrepareOperation(askMode));
if (item.Section != 0)
{
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kUnSupportedMethod));
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kUnsupportedMethod));
continue;
}
@@ -589,7 +603,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
if (!testMode && !realOutStream)
continue;
RINOK(extractCallback->PrepareOperation(askMode));
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kUnSupportedMethod));
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kUnsupportedMethod));
continue;
}
@@ -718,4 +732,32 @@ STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
return S_OK;
}
namespace NChm {
IMP_CreateArcIn_2(CHandler(false))
static CArcInfo g_ArcInfo =
{ "Chm", "chm chi chq chw", 0, 0xE9,
12, { 'I', 'T', 'S', 'F', 3, 0, 0, 0, 0x60, 0, 0, 0 },
0,
0,
CreateArc };
REGISTER_ARC(Chm)
}
namespace NHxs {
IMP_CreateArcIn_2(CHandler(true))
static CArcInfo g_ArcInfo =
{ "Hxs", "hxs hxi hxr hxq hxw lit", 0, 0xCE,
16, { 'I', 'T', 'O', 'L', 'I', 'T', 'L', 'S', 1, 0, 0, 0, 0x28, 0, 0, 0 },
0,
NArcInfoFlags::kFindSignature,
CreateArc };
REGISTER_ARC(Hxs)
}
}}

8
CPP/7zip/Archive/Chm/ChmHandler.h Executable file → Normal file
View File

@@ -3,8 +3,10 @@
#ifndef __ARCHIVE_CHM_HANDLER_H
#define __ARCHIVE_CHM_HANDLER_H
#include "Common/MyCom.h"
#include "../../../Common/MyCom.h"
#include "../IArchive.h"
#include "ChmIn.h"
namespace NArchive {
@@ -19,9 +21,13 @@ public:
INTERFACE_IInArchive(;)
bool _help2;
CHandler(bool help2): _help2(help2) {}
private:
CFilesDatabase m_Database;
CMyComPtr<IInStream> m_Stream;
UInt32 m_ErrorFlags;
};
}}

View File

@@ -1,24 +0,0 @@
// Archive/Chm/Header.h
#include "StdAfx.h"
#include "ChmHeader.h"
namespace NArchive{
namespace NChm{
namespace NHeader{
UInt32 kItsfSignature = 0x46535449 + 1;
UInt32 kItolSignature = 0x4C4F5449 + 1;
static class CSignatureInitializer
{
public:
CSignatureInitializer()
{
kItsfSignature--;
kItolSignature--;
}
}g_SignatureInitializer;
}}}

View File

@@ -1,28 +0,0 @@
// Archive/Chm/Header.h
#ifndef __ARCHIVE_CHM_HEADER_H
#define __ARCHIVE_CHM_HEADER_H
#include "Common/Types.h"
namespace NArchive {
namespace NChm {
namespace NHeader{
const UInt32 kItspSignature = 0x50535449;
const UInt32 kPmglSignature = 0x4C474D50;
const UInt32 kLzxcSignature = 0x43585A4C;
const UInt32 kIfcmSignature = 0x4D434649;
const UInt32 kAollSignature = 0x4C4C4F41;
const UInt32 kCaolSignature = 0x4C4F4143;
extern UInt32 kItsfSignature;
extern UInt32 kItolSignature;
const UInt32 kItlsSignature = 0x534C5449;
UInt64 inline GetHxsSignature() { return ((UInt64)kItlsSignature << 32) | kItolSignature; }
}}}
#endif

277
CPP/7zip/Archive/Chm/ChmIn.cpp Executable file → Normal file
View File

@@ -2,8 +2,10 @@
#include "StdAfx.h"
#include "Common/IntToString.h"
#include "Common/UTFConvert.h"
// #include <stdio.h>
#include "../../../Common/IntToString.h"
#include "../../../Common/UTFConvert.h"
#include "../../Common/LimitedStreams.h"
@@ -12,6 +14,21 @@
namespace NArchive {
namespace NChm {
static const UInt32 kSignature_ITSP = 0x50535449;
static const UInt32 kSignature_PMGL = 0x4C474D50;
static const UInt32 kSignature_LZXC = 0x43585A4C;
static const UInt32 kSignature_IFCM = 0x4D434649;
static const UInt32 kSignature_AOLL = 0x4C4C4F41;
static const UInt32 kSignature_CAOL = 0x4C4F4143;
static const UInt32 kSignature_ITSF = 0x46535449;
static const UInt32 kSignature_ITOL = 0x4C4F5449;
static const UInt32 kSignature_ITLS = 0x534C5449;
struct CEnexpectedEndException {};
struct CHeaderErrorException {};
// define CHM_LOW, if you want to see low level items
// #define CHM_LOW
@@ -31,9 +48,9 @@ static bool AreGuidsEqual(REFGUID g1, REFGUID g2)
return true;
}
static char GetHex(Byte value)
static char GetHex(unsigned v)
{
return (char)((value < 10) ? ('0' + value) : ('A' + (value - 10)));
return (char)((v < 10) ? ('0' + v) : ('A' + (v - 10)));
}
static void PrintByte(Byte b, AString &s)
@@ -103,10 +120,10 @@ UString CMethodInfo::GetName() const
else
{
s2 = GetGuidString();
if (ControlData.GetCapacity() > 0)
if (ControlData.Size() > 0)
{
s2 += ':';
for (size_t i = 0; i < ControlData.GetCapacity(); i++)
for (size_t i = 0; i < ControlData.Size(); i++)
PrintByte(ControlData[i], s2);
}
}
@@ -132,7 +149,7 @@ UString CSectionInfo::GetMethodName() const
s += temp;
s += L": ";
}
for (int i = 0; i < Methods.Size(); i++)
FOR_VECTOR (i, Methods)
{
if (i != 0)
s += L' ';
@@ -145,7 +162,7 @@ Byte CInArchive::ReadByte()
{
Byte b;
if (!_inBuffer.ReadByte(b))
throw 1;
throw CEnexpectedEndException();
return b;
}
@@ -163,32 +180,32 @@ void CInArchive::ReadBytes(Byte *data, UInt32 size)
UInt16 CInArchive::ReadUInt16()
{
UInt16 value = 0;
UInt16 val = 0;
for (int i = 0; i < 2; i++)
value |= ((UInt16)(ReadByte()) << (8 * i));
return value;
val |= ((UInt16)(ReadByte()) << (8 * i));
return val;
}
UInt32 CInArchive::ReadUInt32()
{
UInt32 value = 0;
UInt32 val = 0;
for (int i = 0; i < 4; i++)
value |= ((UInt32)(ReadByte()) << (8 * i));
return value;
val |= ((UInt32)(ReadByte()) << (8 * i));
return val;
}
UInt64 CInArchive::ReadUInt64()
{
UInt64 value = 0;
UInt64 val = 0;
for (int i = 0; i < 8; i++)
value |= ((UInt64)(ReadByte()) << (8 * i));
return value;
val |= ((UInt64)(ReadByte()) << (8 * i));
return val;
}
UInt64 CInArchive::ReadEncInt()
{
UInt64 val = 0;;
for (int i = 0; i < 10; i++)
UInt64 val = 0;
for (int i = 0; i < 9; i++)
{
Byte b = ReadByte();
val |= (b & 0x7F);
@@ -196,7 +213,7 @@ UInt64 CInArchive::ReadEncInt()
return val;
val <<= 7;
}
throw 1;
throw CHeaderErrorException();
}
void CInArchive::ReadGUID(GUID &g)
@@ -207,10 +224,10 @@ void CInArchive::ReadGUID(GUID &g)
ReadBytes(g.Data4, 8);
}
void CInArchive::ReadString(int size, AString &s)
void CInArchive::ReadString(unsigned size, AString &s)
{
s.Empty();
while(size-- != 0)
while (size-- != 0)
{
char c = (char)ReadByte();
if (c == 0)
@@ -222,10 +239,10 @@ void CInArchive::ReadString(int size, AString &s)
}
}
void CInArchive::ReadUString(int size, UString &s)
void CInArchive::ReadUString(unsigned size, UString &s)
{
s.Empty();
while(size-- != 0)
while (size-- != 0)
{
wchar_t c = ReadUInt16();
if (c == 0)
@@ -244,6 +261,7 @@ HRESULT CInArchive::ReadChunk(IInStream *inStream, UInt64 pos, UInt64 size)
CMyComPtr<ISequentialInStream> limitedStream(streamSpec);
streamSpec->SetStream(inStream);
streamSpec->Init(size);
m_InStreamRef = limitedStream;
_inBuffer.SetStream(limitedStream);
_inBuffer.Init();
return S_OK;
@@ -252,10 +270,10 @@ HRESULT CInArchive::ReadChunk(IInStream *inStream, UInt64 pos, UInt64 size)
HRESULT CInArchive::ReadDirEntry(CDatabase &database)
{
CItem item;
UInt64 nameLength = ReadEncInt();
if (nameLength == 0 || nameLength >= 0x10000000)
UInt64 nameLen = ReadEncInt();
if (nameLen == 0 || nameLen > (1 << 13))
return S_FALSE;
ReadString((int)nameLength, item.Name);
ReadString((unsigned)nameLen, item.Name);
item.Section = ReadEncInt();
item.Offset = ReadEncInt();
item.Size = ReadEncInt();
@@ -268,9 +286,14 @@ HRESULT CInArchive::OpenChm(IInStream *inStream, CDatabase &database)
UInt32 headerSize = ReadUInt32();
if (headerSize != 0x60)
return S_FALSE;
database.PhySize = headerSize;
UInt32 unknown1 = ReadUInt32();
if (unknown1 != 0 && unknown1 != 1) // it's 0 in one .sll file
return S_FALSE;
IsArc = true;
/* UInt32 timeStamp = */ ReadUInt32();
// Considered as a big-endian DWORD, it appears to contain seconds (MSB) and
// fractional seconds (second byte).
@@ -280,37 +303,39 @@ HRESULT CInArchive::OpenChm(IInStream *inStream, CDatabase &database)
GUID g;
ReadGUID(g); // {7C01FD10-7BAA-11D0-9E0C-00A0-C922-E6EC}
ReadGUID(g); // {7C01FD11-7BAA-11D0-9E0C-00A0-C922-E6EC}
const int kNumSections = 2;
const unsigned kNumSections = 2;
UInt64 sectionOffsets[kNumSections];
UInt64 sectionSizes[kNumSections];
int i;
unsigned i;
for (i = 0; i < kNumSections; i++)
{
sectionOffsets[i] = ReadUInt64();
sectionSizes[i] = ReadUInt64();
UInt64 end = sectionOffsets[i] + sectionSizes[i];
database.UpdatePhySize(end);
}
// if (chmVersion == 3)
database.ContentOffset = ReadUInt64();
/*
else
database.ContentOffset = _startPosition + 0x58
database.ContentOffset = database.StartPosition + 0x58
*/
/*
// Section 0
ReadChunk(inStream, sectionOffsets[0], sectionSizes[0]);
if (sectionSizes[0] != 0x18)
if (sectionSizes[0] < 0x18)
return S_FALSE;
if (ReadUInt32() != 0x01FE)
return S_FALSE;
ReadUInt32(); // unknown: 01FE
ReadUInt32(); // unknown: 0
UInt64 fileSize = ReadUInt64();
database.UpdatePhySize(fileSize);
ReadUInt32(); // unknown: 0
ReadUInt32(); // unknown: 0
*/
// Section 1: The Directory Listing
ReadChunk(inStream, sectionOffsets[1], sectionSizes[1]);
if (ReadUInt32() != NHeader::kItspSignature)
if (ReadUInt32() != kSignature_ITSP)
return S_FALSE;
if (ReadUInt32() != 1) // version
return S_FALSE;
@@ -340,13 +365,13 @@ HRESULT CInArchive::OpenChm(IInStream *inStream, CDatabase &database)
for (UInt32 ci = 0; ci < numDirChunks; ci++)
{
UInt64 chunkPos = _inBuffer.GetProcessedSize();
if (ReadUInt32() == NHeader::kPmglSignature)
if (ReadUInt32() == kSignature_PMGL)
{
// The quickref area is written backwards from the end of the chunk.
// One quickref entry exists for every n entries in the file, where n
// is calculated as 1 + (1 << quickref density). So for density = 2, n = 5.
UInt32 quickrefLength = ReadUInt32(); // Length of free space and/or quickref area at end of directory chunk
UInt32 quickrefLength = ReadUInt32(); // Len of free space and/or quickref area at end of directory chunk
if (quickrefLength > dirChunkSize || quickrefLength < 2)
return S_FALSE;
ReadUInt32(); // Always 0
@@ -354,7 +379,7 @@ HRESULT CInArchive::OpenChm(IInStream *inStream, CDatabase &database)
// directory in sequence (-1 if this is the first listing chunk)
ReadUInt32(); // Chunk number of next listing chunk when reading
// directory in sequence (-1 if this is the last listing chunk)
int numItems = 0;
unsigned numItems = 0;
for (;;)
{
UInt64 offset = _inBuffer.GetProcessedSize() - chunkPos;
@@ -383,10 +408,13 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
if (ReadUInt32() != 0x28) // Location of header section table
return S_FALSE;
UInt32 numHeaderSections = ReadUInt32();
const int kNumHeaderSectionsMax = 5;
const unsigned kNumHeaderSectionsMax = 5;
if (numHeaderSections != kNumHeaderSectionsMax)
return S_FALSE;
ReadUInt32(); // Length of post-header table
IsArc = true;
ReadUInt32(); // Len of post-header table
GUID g;
ReadGUID(g); // {0A9007C1-4076-11D3-8789-0000F8105754}
@@ -398,6 +426,8 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
{
sectionOffsets[i] = ReadUInt64();
sectionSizes[i] = ReadUInt64();
UInt64 end = sectionOffsets[i] + sectionSizes[i];
database.UpdatePhySize(end);
}
// Post-Header
@@ -436,11 +466,11 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
ReadUInt32(); // $20000 (Same as field following chunk size in directory index)
ReadUInt64(); // 0 (unknown)
if (ReadUInt32() != NHeader::kCaolSignature)
if (ReadUInt32() != kSignature_CAOL)
return S_FALSE;
if (ReadUInt32() != 2) // (Most likely a version number)
return S_FALSE;
UInt32 caolLength = ReadUInt32(); // $50 (Length of the CAOL section, which includes the ITSF section)
UInt32 caolLength = ReadUInt32(); // $50 (Len of the CAOL section, which includes the ITSF section)
if (caolLength >= 0x2C)
{
/* UInt32 c7 = */ ReadUInt16(); // Unknown. Remains the same when identical files are built.
@@ -458,13 +488,15 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
ReadUInt32(); // 0 (Unknown)
if (caolLength == 0x2C)
{
database.ContentOffset = 0;
// fprintf(stdout, "\n !!!NewFormat\n");
// fflush(stdout);
database.ContentOffset = 0; // maybe we must add database.StartPosition here?
database.NewFormat = true;
}
else if (caolLength == 0x50)
{
ReadUInt32(); // 0 (Unknown)
if (ReadUInt32() != NHeader::kItsfSignature)
if (ReadUInt32() != kSignature_ITSF)
return S_FALSE;
if (ReadUInt32() != 4) // $4 (Version number -- CHM uses 3)
return S_FALSE;
@@ -473,7 +505,7 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
UInt32 unknown = ReadUInt32();
if (unknown != 0 && unknown != 1) // = 0 for some HxW files, 1 in other cases;
return S_FALSE;
database.ContentOffset = _startPosition + ReadUInt64();
database.ContentOffset = database.StartPosition + ReadUInt64();
/* UInt32 timeStamp = */ ReadUInt32();
// A timestamp of some sort.
// Considered as a big-endian DWORD, it appears to contain
@@ -486,21 +518,21 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
return S_FALSE;
}
/*
// Section 0
ReadChunk(inStream, _startPosition + sectionOffsets[0], sectionSizes[0]);
if (sectionSizes[0] != 0x18)
ReadChunk(inStream, database.StartPosition + sectionOffsets[0], sectionSizes[0]);
if (sectionSizes[0] < 0x18)
return S_FALSE;
if (ReadUInt32() != 0x01FE)
return S_FALSE;
ReadUInt32(); // unknown: 01FE
ReadUInt32(); // unknown: 0
UInt64 fileSize = ReadUInt64();
database.UpdatePhySize(fileSize);
ReadUInt32(); // unknown: 0
ReadUInt32(); // unknown: 0
*/
// Section 1: The Directory Listing
ReadChunk(inStream, _startPosition + sectionOffsets[1], sectionSizes[1]);
if (ReadUInt32() != NHeader::kIfcmSignature)
ReadChunk(inStream, database.StartPosition + sectionOffsets[1], sectionSizes[1]);
if (ReadUInt32() != kSignature_IFCM)
return S_FALSE;
if (ReadUInt32() != 1) // (probably a version number)
return S_FALSE;
@@ -516,9 +548,9 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
for (UInt32 ci = 0; ci < numDirChunks; ci++)
{
UInt64 chunkPos = _inBuffer.GetProcessedSize();
if (ReadUInt32() == NHeader::kAollSignature)
if (ReadUInt32() == kSignature_AOLL)
{
UInt32 quickrefLength = ReadUInt32(); // Length of quickref area at end of directory chunk
UInt32 quickrefLength = ReadUInt32(); // Len of quickref area at end of directory chunk
if (quickrefLength > dirChunkSize || quickrefLength < 2)
return S_FALSE;
ReadUInt64(); // Directory chunk number
@@ -533,7 +565,7 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
ReadUInt32(); // 1 (unknown -- other values have also been seen here)
ReadUInt32(); // 0 (unknown)
int numItems = 0;
unsigned numItems = 0;
for (;;)
{
UInt64 offset = _inBuffer.GetProcessedSize() - chunkPos;
@@ -544,11 +576,11 @@ HRESULT CInArchive::OpenHelp2(IInStream *inStream, CDatabase &database)
break;
if (database.NewFormat)
{
UInt16 nameLength = ReadUInt16();
if (nameLength == 0)
UInt16 nameLen = ReadUInt16();
if (nameLen == 0)
return S_FALSE;
UString name;
ReadUString((int)nameLength, name);
ReadUString((unsigned)nameLen, name);
AString s;
ConvertUnicodeToUTF8(name, s);
Byte b = ReadByte();
@@ -637,10 +669,10 @@ static int CompareFiles(const int *p1, const int *p2, void *param)
void CFilesDatabase::SetIndices()
{
for (int i = 0; i < Items.Size(); i++)
FOR_VECTOR (i, Items)
{
const CItem &item = Items[i];
if (item.IsUserItem() && item.Name.Length() != 1)
if (item.IsUserItem() && item.Name.Len() != 1)
Indices.Add(i);
}
}
@@ -654,7 +686,7 @@ bool CFilesDatabase::Check()
{
UInt64 maxPos = 0;
UInt64 prevSection = 0;
for(int i = 0; i < Indices.Size(); i++)
FOR_VECTOR (i, Indices)
{
const CItem &item = Items[Indices[i]];
if (item.Section == 0 || item.IsDir())
@@ -684,9 +716,9 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
for (int i = 0; i < numSections; i++)
{
CSectionInfo section;
UInt16 nameLength = ReadUInt16();
UInt16 nameLen = ReadUInt16();
UString name;
ReadUString(nameLength, name);
ReadUString(nameLen, name);
if (ReadUInt16() != 0)
return S_FALSE;
if (!ConvertUnicodeToUTF8(name, section.Name))
@@ -695,7 +727,7 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
}
}
int i;
unsigned i;
for (i = 1; i < database.Sections.Size(); i++)
{
CSectionInfo &section = database.Sections[i];
@@ -736,7 +768,7 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
{
// Control Data
RINOK(DecompressStream(inStream, database, sectionPrefix + kControlData));
for (int mi = 0; mi < section.Methods.Size(); mi++)
FOR_VECTOR (mi, section.Methods)
{
CMethodInfo &method = section.Methods[mi];
UInt32 numDWORDS = ReadUInt32();
@@ -744,7 +776,7 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
{
if (numDWORDS < 5)
return S_FALSE;
if (ReadUInt32() != NHeader::kLzxcSignature)
if (ReadUInt32() != kSignature_LZXC)
return S_FALSE;
CLzxInfo &li = method.LzxInfo;
li.Version = ReadUInt32();
@@ -778,7 +810,7 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
else
{
UInt32 numBytes = numDWORDS * 4;
method.ControlData.SetCapacity(numBytes);
method.ControlData.Alloc(numBytes);
ReadBytes(method.ControlData, numBytes);
}
}
@@ -791,7 +823,7 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
}
// read ResetTable for LZX
for (int mi = 0; mi < section.Methods.Size(); mi++)
FOR_VECTOR (mi, section.Methods)
{
CMethodInfo &method = section.Methods[mi];
if (method.IsLzx())
@@ -819,16 +851,16 @@ HRESULT CInArchive::OpenHighLevel(IInStream *inStream, CFilesDatabase &database)
UInt32 numEntries = ReadUInt32();
if (ReadUInt32() != 8) // Size of table entry (bytes)
return S_FALSE;
if (ReadUInt32() != 0x28) // Length of table header
if (ReadUInt32() != 0x28) // Len of table header
return S_FALSE;
rt.UncompressedSize = ReadUInt64();
rt.CompressedSize = ReadUInt64();
rt.BlockSize = ReadUInt64(); // 0x8000 block size for locations below
if (rt.BlockSize != 0x8000)
return S_FALSE;
rt.ResetOffsets.Reserve(numEntries);
rt.ResetOffsets.ClearAndReserve(numEntries);
for (UInt32 i = 0; i < numEntries; i++)
rt.ResetOffsets.Add(ReadUInt64());
rt.ResetOffsets.AddInReserved(ReadUInt64());
}
}
}
@@ -843,77 +875,91 @@ HRESULT CInArchive::Open2(IInStream *inStream,
const UInt64 *searchHeaderSizeLimit,
CFilesDatabase &database)
{
IsArc = false;
HeadersError = false;
UnexpectedEnd = false;
UnsupportedFeature = false;
database.Clear();
RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &_startPosition));
database.Help2Format = false;
database.Help2Format = _help2;
const UInt32 chmVersion = 3;
RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &database.StartPosition));
if (!_inBuffer.Create(1 << 14))
return E_OUTOFMEMORY;
_inBuffer.SetStream(inStream);
_inBuffer.Init();
if (_help2)
{
if (!_inBuffer.Create(1 << 14))
return E_OUTOFMEMORY;
_inBuffer.SetStream(inStream);
_inBuffer.Init();
UInt64 value = 0;
const int kSignatureSize = 8;
UInt64 hxsSignature = NHeader::GetHxsSignature();
UInt64 chmSignature = ((UInt64)chmVersion << 32)| NHeader::kItsfSignature;
UInt64 signature = ((UInt64)kSignature_ITLS << 32)| kSignature_ITOL;
UInt64 limit = 1 << 18;
if (searchHeaderSizeLimit)
if (limit > *searchHeaderSizeLimit)
limit = *searchHeaderSizeLimit;
UInt64 val = 0;
for (;;)
{
Byte b;
if (!_inBuffer.ReadByte(b))
return S_FALSE;
value >>= 8;
value |= ((UInt64)b) << ((kSignatureSize - 1) * 8);
val >>= 8;
val |= ((UInt64)b) << ((kSignatureSize - 1) * 8);
if (_inBuffer.GetProcessedSize() >= kSignatureSize)
{
if (value == chmSignature)
if (val == signature)
break;
if (value == hxsSignature)
{
database.Help2Format = true;
break;
}
if (_inBuffer.GetProcessedSize() > limit)
return S_FALSE;
}
}
_startPosition += _inBuffer.GetProcessedSize() - kSignatureSize;
}
if (database.Help2Format)
{
database.StartPosition += _inBuffer.GetProcessedSize() - kSignatureSize;
RINOK(OpenHelp2(inStream, database));
if (database.NewFormat)
return S_OK;
}
else
{
if (ReadUInt32() != kSignature_ITSF)
return S_FALSE;
if (ReadUInt32() != chmVersion)
return S_FALSE;
RINOK(OpenChm(inStream, database));
}
#ifndef CHM_LOW
try
{
HRESULT res = OpenHighLevel(inStream, database);
if (res == S_FALSE)
try
{
HRESULT res = OpenHighLevel(inStream, database);
if (res == S_FALSE)
{
UnsupportedFeature = true;
database.HighLevelClear();
return S_OK;
}
RINOK(res);
database.LowLevel = false;
}
catch(...)
{
database.HighLevelClear();
return S_OK;
throw;
}
RINOK(res);
database.LowLevel = false;
}
catch(...)
{
return S_OK;
}
// catch(const CInBufferException &e) { return e.ErrorCode; }
catch(CEnexpectedEndException &) { UnexpectedEnd = true; }
catch(CHeaderErrorException &) { HeadersError = true; }
catch(...) { throw; }
#endif
return S_OK;
}
@@ -923,15 +969,22 @@ HRESULT CInArchive::Open(IInStream *inStream,
{
try
{
HRESULT res = Open2(inStream, searchHeaderSizeLimit, database);
_inBuffer.ReleaseStream();
return res;
}
catch(...)
{
_inBuffer.ReleaseStream();
throw;
try
{
HRESULT res = Open2(inStream, searchHeaderSizeLimit, database);
m_InStreamRef.Release();
return res;
}
catch(...)
{
m_InStreamRef.Release();
throw;
}
}
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(CEnexpectedEndException &) { UnexpectedEnd = true; }
catch(CHeaderErrorException &) { HeadersError = true; }
return S_FALSE;
}
}}

46
CPP/7zip/Archive/Chm/ChmIn.h Executable file → Normal file
View File

@@ -3,13 +3,12 @@
#ifndef __ARCHIVE_CHM_IN_H
#define __ARCHIVE_CHM_IN_H
#include "Common/Buffer.h"
#include "Common/MyString.h"
#include "../../../Common/MyBuffer.h"
#include "../../../Common/MyString.h"
#include "../../IStream.h"
#include "../../Common/InBuffer.h"
#include "ChmHeader.h"
#include "../../Common/InBuffer.h"
namespace NArchive {
namespace NChm {
@@ -23,21 +22,21 @@ struct CItem
bool IsFormatRelatedItem() const
{
if (Name.Length() < 2)
if (Name.Len() < 2)
return false;
return Name[0] == ':' && Name[1] == ':';
}
bool IsUserItem() const
{
if (Name.Length() < 2)
if (Name.Len() < 2)
return false;
return Name[0] == '/';
}
bool IsDir() const
{
if (Name.Length() == 0)
if (Name.Len() == 0)
return false;
return (Name.Back() == '/');
}
@@ -45,15 +44,19 @@ struct CItem
struct CDatabase
{
UInt64 StartPosition;
UInt64 ContentOffset;
CObjectVector<CItem> Items;
AString NewFormatString;
bool Help2Format;
bool NewFormat;
UInt64 PhySize;
void UpdatePhySize(UInt64 v) { if (PhySize < v) PhySize = v; }
int FindItem(const AString &name) const
{
for (int i = 0; i < Items.Size(); i++)
FOR_VECTOR (i, Items)
if (Items[i].Name == name)
return i;
return -1;
@@ -65,6 +68,8 @@ struct CDatabase
NewFormatString.Empty();
Help2Format = false;
Items.Clear();
StartPosition = 0;
PhySize = 0;
}
};
@@ -74,15 +79,16 @@ struct CResetTable
UInt64 CompressedSize;
UInt64 BlockSize;
CRecordVector<UInt64> ResetOffsets;
bool GetCompressedSizeOfBlocks(UInt64 blockIndex, UInt32 numBlocks, UInt64 &size) const
{
if (blockIndex >= ResetOffsets.Size())
return false;
UInt64 startPos = ResetOffsets[(int)blockIndex];
UInt64 startPos = ResetOffsets[(unsigned)blockIndex];
if (blockIndex + numBlocks >= ResetOffsets.Size())
size = CompressedSize - startPos;
else
size = ResetOffsets[(int)(blockIndex + numBlocks)] - startPos;
size = ResetOffsets[(unsigned)(blockIndex + numBlocks)] - startPos;
return true;
}
bool GetCompressedSizeOfBlock(UInt64 blockIndex, UInt64 &size) const
@@ -107,7 +113,7 @@ struct CLzxInfo
{
if (Version == 2 || Version == 3)
{
for (int i = 0; i <= 31; i++)
for (unsigned i = 0; i <= 31; i++)
if (((UInt32)1 << i) >= WindowSize)
return 15 + i;
}
@@ -123,7 +129,7 @@ struct CLzxInfo
UInt64 blockIndex = GetBlockIndexFromFolderIndex(folderIndex);
if (blockIndex >= ResetTable.ResetOffsets.Size())
return false;
offset = ResetTable.ResetOffsets[(int)blockIndex];
offset = ResetTable.ResetOffsets[(unsigned)blockIndex];
return true;
}
bool GetCompressedSizeOfFolder(UInt64 folderIndex, UInt64 &size) const
@@ -202,18 +208,21 @@ public:
bool Check();
};
/*
class CProgressVirt
{
public:
STDMETHOD(SetTotal)(const UInt64 *numFiles) PURE;
STDMETHOD(SetCompleted)(const UInt64 *numFiles) PURE;
};
*/
class CInArchive
{
UInt64 _startPosition;
CMyComPtr<ISequentialInStream> m_InStreamRef;
::CInBuffer _inBuffer;
UInt64 _chunkSize;
bool _help2;
Byte ReadByte();
void ReadBytes(Byte *data, UInt32 size);
@@ -222,8 +231,8 @@ class CInArchive
UInt32 ReadUInt32();
UInt64 ReadUInt64();
UInt64 ReadEncInt();
void ReadString(int size, AString &s);
void ReadUString(int size, UString &s);
void ReadString(unsigned size, AString &s);
void ReadUString(unsigned size, UString &s);
void ReadGUID(GUID &g);
HRESULT ReadChunk(IInStream *inStream, UInt64 pos, UInt64 size);
@@ -232,6 +241,13 @@ class CInArchive
HRESULT DecompressStream(IInStream *inStream, const CDatabase &database, const AString &name);
public:
bool IsArc;
bool HeadersError;
bool UnexpectedEnd;
bool UnsupportedFeature;
CInArchive(bool help2) { _help2 = help2; }
HRESULT OpenChm(IInStream *inStream, CDatabase &database);
HRESULT OpenHelp2(IInStream *inStream, CDatabase &database);
HRESULT OpenHighLevel(IInStream *inStream, CFilesDatabase &database);

View File

@@ -1,13 +0,0 @@
// ChmRegister.cpp
#include "StdAfx.h"
#include "../../Common/RegisterArc.h"
#include "ChmHandler.h"
static IInArchive *CreateArc() { return new NArchive::NChm::CHandler; }
static CArcInfo g_ArcInfo =
{ L"Chm", L"chm chi chq chw hxs hxi hxr hxq hxw lit", 0, 0xE9, { 'I', 'T', 'S', 'F' }, 4, false, CreateArc, 0 };
REGISTER_ARC(Chm)

2
CPP/7zip/Archive/Chm/StdAfx.h Executable file → Normal file
View File

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