4.44 beta

This commit is contained in:
Igor Pavlov
2007-01-20 00:00:00 +00:00
committed by Kornel Lesiński
parent 804edc5756
commit d9666cf046
1331 changed files with 10535 additions and 13791 deletions

View File

@@ -0,0 +1,142 @@
// DLLExports.cpp
#include "StdAfx.h"
#include "Common/MyInitGuid.h"
#include "Common/ComTry.h"
#include "Windows/PropVariant.h"
#include "../../ICoder.h"
#include "../../IPassword.h"
#include "../Common/CodecsPath.h"
// {23170F69-40C1-278B-0601-010000000000}
DEFINE_GUID(CLSID_CCrypto_AES128_Decoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00);
#include "RarHandler.h"
HINSTANCE g_hInstance;
#ifndef _UNICODE
bool g_IsNT = false;
static bool IsItWindowsNT()
{
OSVERSIONINFO versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
if (!::GetVersionEx(&versionInfo))
return false;
return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
#endif
void GetCryptoFolderPrefix(TCHAR *path)
{
CSysString s = GetCodecsFolderPrefix();
lstrcpy(path, s);
}
// {23170F69-40C1-278B-0403-010000000000}
DEFINE_GUID(CLSID_CCompressRar15Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00);
// {23170F69-40C1-278B-0403-020000000000}
DEFINE_GUID(CLSID_CCompressRar20Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00);
// {23170F69-40C1-278B-0403-030000000000}
DEFINE_GUID(CLSID_CCompressRar29Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00);
/*
// {23170F69-40C1-278B-06F1-0302000000000}
DEFINE_GUID(CLSID_CCryptoRar20Decoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0xF1, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00);
// {23170F69-40C1-278B-06F1-0303000000000}
DEFINE_GUID(CLSID_CCryptoRar29Decoder,
0x23170F69, 0x40C1, 0x278B, 0x06, 0xF1, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00);
*/
// {23170F69-40C1-278A-1000-000110030000}
DEFINE_GUID(CLSID_CRarHandler,
0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x03, 0x00, 0x00);
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInstance;
#ifndef _UNICODE
g_IsNT = IsItWindowsNT();
#endif
}
return TRUE;
}
STDAPI CreateObject(
const GUID *classID,
const GUID *interfaceID,
void **outObject)
{
COM_TRY_BEGIN
*outObject = 0;
if (*classID != CLSID_CRarHandler)
return CLASS_E_CLASSNOTAVAILABLE;
int needIn = *interfaceID == IID_IInArchive;
if (needIn)
{
NArchive::NRar::CHandler *temp = new NArchive::NRar::CHandler;
if (needIn)
{
CMyComPtr<IInArchive> inArchive = (IInArchive *)temp;
*outObject = inArchive.Detach();
}
else
{
CMyComPtr<IOutArchive> outArchive = (IOutArchive *)temp;
*outObject = outArchive.Detach();
}
}
else
return E_NOINTERFACE;
COM_TRY_END
return S_OK;
}
STDAPI GetHandlerProperty(PROPID propID, PROPVARIANT *value)
{
NWindows::NCOM::CPropVariant propVariant;
switch(propID)
{
case NArchive::kName:
propVariant = L"Rar";
break;
case NArchive::kClassID:
{
if ((value->bstrVal = ::SysAllocStringByteLen(
(const char *)&CLSID_CRarHandler, sizeof(GUID))) != 0)
value->vt = VT_BSTR;
return S_OK;
}
case NArchive::kExtension:
propVariant = L"rar";
break;
case NArchive::kUpdate:
propVariant = false;
break;
case NArchive::kKeepName:
propVariant = false;
break;
case NArchive::kStartSignature:
{
if ((value->bstrVal = ::SysAllocStringByteLen(
(const char *)NArchive::NRar::NHeader::kMarker,
NArchive::NRar::NHeader::kMarkerSize)) != 0)
value->vt = VT_BSTR;
return S_OK;
}
}
propVariant.Detach(value);
return S_OK;
}

459
CPP/7zip/Archive/Rar/Rar.dsp Executable file
View File

@@ -0,0 +1,459 @@
# Microsoft Developer Studio Project File - Name="Rar" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=Rar - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "Rar.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "Rar.mak" CFG="Rar - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "Rar - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "Rar - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "Rar - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 1
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "RAR_EXPORTS" /YX /FD /c
# ADD CPP /nologo /Gz /MD /W3 /GX /O1 /I "..\..\..\\" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "RAR_EXPORTS" /Yu"StdAfx.h" /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x419 /d "NDEBUG"
# ADD RSC /l 0x419 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"C:\Program Files\7-ZIP\Formats\rar.dll" /opt:NOWIN98
# SUBTRACT LINK32 /pdb:none
!ELSEIF "$(CFG)" == "Rar - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 1
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "RAR_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /Gz /MTd /W3 /Gm /GX /ZI /Od /I "..\..\..\\" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "RAR_EXPORTS" /Yu"StdAfx.h" /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x419 /d "_DEBUG"
# ADD RSC /l 0x419 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"C:\Program Files\7-ZIP\Formats\rar.dll" /pdbtype:sept
!ENDIF
# Begin Target
# Name "Rar - Win32 Release"
# Name "Rar - Win32 Debug"
# Begin Group "Spec"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\Archive.def
# End Source File
# Begin Source File
SOURCE=.\DllExports.cpp
# End Source File
# Begin Source File
SOURCE=.\resource.rc
# End Source File
# Begin Source File
SOURCE=.\StdAfx.cpp
# ADD CPP /Yc"StdAfx.h"
# End Source File
# Begin Source File
SOURCE=.\StdAfx.h
# End Source File
# End Group
# Begin Group "Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\Common\Alloc.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Alloc.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Buffer.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\CRC.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\CRC.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\DynamicBuffer.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\IntToString.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\IntToString.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\NewHandler.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\NewHandler.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\String.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\String.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\StringConvert.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\StringConvert.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Types.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\UTFConvert.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\UTFConvert.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Vector.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Vector.h
# End Source File
# End Group
# Begin Group "Archive Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\Common\CodecsPath.cpp
# End Source File
# Begin Source File
SOURCE=..\Common\CodecsPath.h
# End Source File
# Begin Source File
SOURCE=..\Common\CoderLoader.h
# End Source File
# Begin Source File
SOURCE=..\Common\FilterCoder.cpp
# End Source File
# Begin Source File
SOURCE=..\Common\FilterCoder.h
# End Source File
# Begin Source File
SOURCE=..\Common\IArchiveHandler.h
# End Source File
# Begin Source File
SOURCE=..\Common\InStreamWithCRC.cpp
# End Source File
# Begin Source File
SOURCE=..\Common\InStreamWithCRC.h
# End Source File
# Begin Source File
SOURCE=..\Common\OutStreamWithCRC.cpp
# End Source File
# Begin Source File
SOURCE=..\Common\OutStreamWithCRC.h
# End Source File
# End Group
# Begin Group "Windows"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\Windows\DLL.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\DLL.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\FileFind.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\FileFind.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Handle.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\PropVariant.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\PropVariant.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Synchronization.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Synchronization.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Thread.h
# End Source File
# End Group
# Begin Group "Compress"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Compress\Copy\CopyCoder.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Compress\Copy\CopyCoder.h
# End Source File
# End Group
# Begin Group "Engine"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\RarHandler.cpp
# End Source File
# Begin Source File
SOURCE=.\RarHandler.h
# End Source File
# Begin Source File
SOURCE=.\RarHeader.cpp
# End Source File
# Begin Source File
SOURCE=.\RarHeader.h
# End Source File
# Begin Source File
SOURCE=.\RarIn.cpp
# End Source File
# Begin Source File
SOURCE=.\RarIn.h
# End Source File
# Begin Source File
SOURCE=.\RarItem.cpp
# End Source File
# Begin Source File
SOURCE=.\RarItem.h
# End Source File
# Begin Source File
SOURCE=.\RarVolumeInStream.cpp
# End Source File
# Begin Source File
SOURCE=.\RarVolumeInStream.h
# End Source File
# End Group
# Begin Group "Crypto"
# PROP Default_Filter ""
# Begin Group "Rar29"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Crypto\RarAES\RarAES.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Crypto\RarAES\RarAES.h
# End Source File
# End Group
# Begin Group "Rar20"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Crypto\Rar20\Rar20Cipher.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Crypto\Rar20\Rar20Cipher.h
# End Source File
# Begin Source File
SOURCE=..\..\Crypto\Rar20\Rar20Crypto.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Crypto\Rar20\Rar20Crypto.h
# End Source File
# End Group
# Begin Group "Hash"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Crypto\Hash\Sha1.cpp
!IF "$(CFG)" == "Rar - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Rar - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\Crypto\Hash\Sha1.h
# End Source File
# End Group
# End Group
# Begin Group "7-zip Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Common\LimitedStreams.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\LimitedStreams.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\ProgressUtils.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\ProgressUtils.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\StreamObjects.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\StreamObjects.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\StreamUtils.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\StreamUtils.h
# End Source File
# End Group
# Begin Group "7z"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\7z\7zMethodID.cpp
# End Source File
# Begin Source File
SOURCE=..\7z\7zMethodID.h
# End Source File
# Begin Source File
SOURCE=..\7z\7zMethods.cpp
# End Source File
# Begin Source File
SOURCE=..\7z\7zMethods.h
# End Source File
# End Group
# Begin Source File
SOURCE=.\Rar.ico
# End Source File
# End Target
# End Project

29
CPP/7zip/Archive/Rar/Rar.dsw Executable file
View File

@@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "Rar"=.\Rar.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@@ -0,0 +1,941 @@
// RarHandler.cpp
#include "StdAfx.h"
#include "RarHandler.h"
#include "Common/StringConvert.h"
#include "Common/ComTry.h"
#include "Common/IntToString.h"
#include "Windows/PropVariant.h"
#include "Windows/Time.h"
#include "../../IPassword.h"
#include "../../Common//ProgressUtils.h"
#include "../../Compress/Copy/CopyCoder.h"
#include "../../Crypto/Rar20/Rar20Cipher.h"
#include "../../Crypto/RarAES/RarAES.h"
#include "../Common/OutStreamWithCRC.h"
#include "../Common/CoderLoader.h"
#include "../Common/CodecsPath.h"
#include "../Common/FilterCoder.h"
#include "../Common/ItemNameUtils.h"
#include "../7z/7zMethods.h"
using namespace NWindows;
using namespace NTime;
// {23170F69-40C1-278B-0403-010000000000}
DEFINE_GUID(CLSID_CCompressRar15Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00);
// {23170F69-40C1-278B-0403-020000000000}
DEFINE_GUID(CLSID_CCompressRar20Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00);
// {23170F69-40C1-278B-0403-030000000000}
DEFINE_GUID(CLSID_CCompressRar29Decoder,
0x23170F69, 0x40C1, 0x278B, 0x04, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00);
namespace NArchive {
namespace NRar {
static const wchar_t *kHostOS[] =
{
L"MS DOS",
L"OS/2",
L"Win32",
L"Unix",
L"Mac OS",
L"BeOS"
};
static const int kNumHostOSes = sizeof(kHostOS) / sizeof(kHostOS[0]);
static const wchar_t *kUnknownOS = L"Unknown";
enum // PropID
{
kpidUnPackVersion = kpidUserDefined
};
STATPROPSTG kProperties[] =
{
{ NULL, kpidPath, VT_BSTR},
{ NULL, kpidIsFolder, VT_BOOL},
{ NULL, kpidSize, VT_UI8},
{ NULL, kpidPackedSize, VT_UI8},
{ NULL, kpidLastWriteTime, VT_FILETIME},
{ NULL, kpidCreationTime, VT_FILETIME},
{ NULL, kpidLastAccessTime, VT_FILETIME},
{ NULL, kpidAttributes, VT_UI4},
{ NULL, kpidEncrypted, VT_BOOL},
{ NULL, kpidSolid, VT_BOOL},
{ NULL, kpidCommented, VT_BOOL},
{ NULL, kpidSplitBefore, VT_BOOL},
{ NULL, kpidSplitAfter, VT_BOOL},
{ NULL, kpidCRC, VT_UI4},
{ NULL, kpidHostOS, VT_BSTR},
{ NULL, kpidMethod, VT_BSTR}
// { NULL, kpidDictionarySize, VT_UI4},
// { L"UnPack Version", kpidUnPackVersion, VT_UI1}
};
STATPROPSTG kArchiveProperties[] =
{
{ NULL, kpidSolid, VT_BOOL},
{ NULL, kpidCommented, VT_BOOL},
};
UInt64 CHandler::GetPackSize(int refIndex) const
{
const CRefItem &refItem = _refItems[refIndex];
UInt64 totalPackSize = 0;
for (int i = 0; i < refItem.NumItems; i++)
totalPackSize += _items[refItem.ItemIndex + i].PackSize;
return totalPackSize;
}
STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant propVariant;
switch(propID)
{
case kpidSolid:
propVariant = _archiveInfo.IsSolid();
break;
case kpidCommented:
propVariant = _archiveInfo.IsCommented();
break;
}
propVariant.Detach(value);
return S_OK;
COM_TRY_END
}
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;
*name = 0;
return S_OK;
}
STDMETHODIMP CHandler::GetNumberOfArchiveProperties(UInt32 *numProperties)
{
*numProperties = sizeof(kArchiveProperties) / sizeof(kArchiveProperties[0]);
return S_OK;
}
STDMETHODIMP CHandler::GetArchivePropertyInfo(UInt32 index,
BSTR *name, PROPID *propID, VARTYPE *varType)
{
if(index >= sizeof(kArchiveProperties) / sizeof(kArchiveProperties[0]))
return E_INVALIDARG;
const STATPROPSTG &srcItem = kArchiveProperties[index];
*propID = srcItem.propid;
*varType = srcItem.vt;
*name = 0;
return S_OK;
}
STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
{
*numItems = _refItems.Size();
return S_OK;
}
static bool RarTimeToFileTime(const CRarTime &rarTime, FILETIME &result)
{
if (!DosTimeToFileTime(rarTime.DosTime, result))
return false;
UInt64 value = (((UInt64)result.dwHighDateTime) << 32) + result.dwLowDateTime;
value += (UInt64)rarTime.LowSecond * 10000000;
value += ((UInt64)rarTime.SubTime[2] << 16) +
((UInt64)rarTime.SubTime[1] << 8) +
((UInt64)rarTime.SubTime[0]);
result.dwLowDateTime = (DWORD)value;
result.dwHighDateTime = DWORD(value >> 32);
return true;
}
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant propVariant;
const CRefItem &refItem = _refItems[index];
const CItemEx &item = _items[refItem.ItemIndex];
switch(propID)
{
case kpidPath:
{
UString u;
if (item.HasUnicodeName() && !item.UnicodeName.IsEmpty())
u = item.UnicodeName;
else
u = MultiByteToUnicodeString(item.Name, CP_OEMCP);
propVariant = (const wchar_t *)NItemName::WinNameToOSName(u);
break;
}
case kpidIsFolder:
propVariant = item.IsDirectory();
break;
case kpidSize:
propVariant = item.UnPackSize;
break;
case kpidPackedSize:
{
propVariant = GetPackSize(index);
break;
}
case kpidLastWriteTime:
{
FILETIME localFileTime, utcFileTime;
if (RarTimeToFileTime(item.LastWriteTime, localFileTime))
{
if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime))
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
}
else
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
propVariant = utcFileTime;
break;
}
case kpidCreationTime:
{
if (item.IsCreationTimeDefined)
{
FILETIME localFileTime, utcFileTime;
if (RarTimeToFileTime(item.CreationTime, localFileTime))
{
if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime))
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
}
else
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
propVariant = utcFileTime;
}
break;
}
case kpidLastAccessTime:
{
if (item.IsLastAccessTimeDefined)
{
FILETIME localFileTime, utcFileTime;
if (RarTimeToFileTime(item.LastAccessTime, localFileTime))
{
if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime))
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
}
else
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
propVariant = utcFileTime;
}
break;
}
case kpidAttributes:
propVariant = item.GetWinAttributes();
break;
case kpidEncrypted:
propVariant = item.IsEncrypted();
break;
case kpidSolid:
propVariant = IsSolid(index);
break;
case kpidCommented:
propVariant = item.IsCommented();
break;
case kpidSplitBefore:
propVariant = item.IsSplitBefore();
break;
case kpidSplitAfter:
propVariant = _items[refItem.ItemIndex + refItem.NumItems - 1].IsSplitAfter();
break;
/*
case kpidDictionarySize:
if (!item.IsDirectory())
propVariant = UInt32(0x10000 << item.GetDictSize());
break;
*/
case kpidCRC:
{
const CItemEx &lastItem =
_items[refItem.ItemIndex + refItem.NumItems - 1];
if (lastItem.IsSplitAfter())
propVariant = item.FileCRC;
else
propVariant = lastItem.FileCRC;
break;
}
case kpidUnPackVersion:
propVariant = item.UnPackVersion;
break;
case kpidMethod:
{
UString method;
if (item.Method >= Byte('0') && item.Method <= Byte('5'))
{
method = L"m";
wchar_t temp[32];
ConvertUInt64ToString(item.Method - Byte('0'), temp);
method += temp;
if (!item.IsDirectory())
{
method += L":";
ConvertUInt64ToString(16 + item.GetDictSize(), temp);
method += temp;
}
}
else
{
wchar_t temp[32];
ConvertUInt64ToString(item.Method, temp);
method += temp;
}
propVariant = method;
break;
}
case kpidHostOS:
propVariant = (item.HostOS < kNumHostOSes) ?
(kHostOS[item.HostOS]) : kUnknownOS;
break;
}
propVariant.Detach(value);
return S_OK;
COM_TRY_END
}
class CVolumeName
{
bool _first;
bool _newStyle;
UString _unchangedPart;
UString _changedPart;
UString _afterPart;
public:
CVolumeName(): _newStyle(true) {};
bool InitName(const UString &name, bool newStyle)
{
_first = true;
_newStyle = newStyle;
int dotPos = name.ReverseFind('.');
UString basePart = name;
if (dotPos >= 0)
{
UString ext = name.Mid(dotPos + 1);
if (ext.CompareNoCase(L"RAR")==0 ||
ext.CompareNoCase(L"EXE") == 0)
{
_afterPart = L".rar";
basePart = name.Left(dotPos);
}
}
if (!_newStyle)
{
_afterPart.Empty();
_unchangedPart = basePart + UString(L".");
_changedPart = L"r00";
return true;;
}
int numLetters = 1;
if (basePart.Right(numLetters) == L"1")
{
while (numLetters < basePart.Length())
{
if (basePart[basePart.Length() - numLetters - 1] != '0')
break;
numLetters++;
}
}
else
return false;
_unchangedPart = basePart.Left(basePart.Length() - numLetters);
_changedPart = basePart.Right(numLetters);
return true;
}
UString GetNextName()
{
UString newName;
if (_newStyle || !_first)
{
int i;
int numLetters = _changedPart.Length();
for (i = numLetters - 1; i >= 0; i--)
{
wchar_t c = _changedPart[i];
if (c == L'9')
{
c = L'0';
newName = c + newName;
if (i == 0)
newName = UString(L'1') + newName;
continue;
}
c++;
newName = UString(c) + newName;
i--;
for (; i >= 0; i--)
newName = _changedPart[i] + newName;
break;
}
_changedPart = newName;
}
_first = false;
return _unchangedPart + _changedPart + _afterPart;
}
};
STDMETHODIMP CHandler::Open(IInStream *stream,
const UInt64 *maxCheckStartPosition,
IArchiveOpenCallback *openArchiveCallback)
{
COM_TRY_BEGIN
Close();
try
{
CMyComPtr<IArchiveOpenVolumeCallback> openVolumeCallback;
CMyComPtr<ICryptoGetTextPassword> getTextPassword;
CMyComPtr<IArchiveOpenCallback> openArchiveCallbackWrap = openArchiveCallback;
CVolumeName seqName;
if (openArchiveCallback != NULL)
{
openArchiveCallbackWrap.QueryInterface(IID_IArchiveOpenVolumeCallback, &openVolumeCallback);
RINOK(openArchiveCallback->SetTotal(NULL, NULL));
UInt64 numFiles = _items.Size();
RINOK(openArchiveCallback->SetCompleted(&numFiles, NULL));
openArchiveCallbackWrap.QueryInterface(IID_ICryptoGetTextPassword, &getTextPassword);
}
for (;;)
{
CMyComPtr<IInStream> inStream;
if (!_archives.IsEmpty())
{
if (!openVolumeCallback)
break;
if(_archives.Size() == 1)
{
if (!_archiveInfo.IsVolume())
break;
UString baseName;
{
NCOM::CPropVariant propVariant;
RINOK(openVolumeCallback->GetProperty(kpidName, &propVariant));
if (propVariant.vt != VT_BSTR)
break;
baseName = propVariant.bstrVal;
}
seqName.InitName(baseName, _archiveInfo.HaveNewVolumeName());
}
UString fullName = seqName.GetNextName();
HRESULT result = openVolumeCallback->GetStream(fullName, &inStream);
if (result == S_FALSE)
break;
if (result != S_OK)
return result;
if (!stream)
break;
}
else
inStream = stream;
NArchive::NRar::CInArchive archive;
if(!archive.Open(inStream, maxCheckStartPosition))
return S_FALSE;
if (_archives.IsEmpty())
archive.GetArchiveInfo(_archiveInfo);
CItemEx item;
for (;;)
{
HRESULT result = archive.GetNextItem(item, getTextPassword);
if (result == S_FALSE)
break;
RINOK(result);
if (item.IgnoreItem())
continue;
bool needAdd = true;
if (item.IsSplitBefore())
{
if (!_refItems.IsEmpty())
{
CRefItem &refItem = _refItems.Back();
refItem.NumItems++;
needAdd = false;
}
}
if (needAdd)
{
CRefItem refItem;
refItem.ItemIndex = _items.Size();
refItem.NumItems = 1;
refItem.VolumeIndex = _archives.Size();
_refItems.Add(refItem);
}
_items.Add(item);
if (openArchiveCallback != NULL)
{
UInt64 numFiles = _items.Size();
RINOK(openArchiveCallback->SetCompleted(&numFiles, NULL));
}
}
_archives.Add(archive);
}
}
catch(...)
{
return S_FALSE;
}
return S_OK;
COM_TRY_END
}
STDMETHODIMP CHandler::Close()
{
COM_TRY_BEGIN
_refItems.Clear();
_items.Clear();
_archives.Clear();
return S_OK;
COM_TRY_END
}
struct CMethodItem
{
Byte RarUnPackVersion;
CMyComPtr<ICompressCoder> Coder;
};
STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems,
Int32 _aTestMode, IArchiveExtractCallback *_anExtractCallback)
{
COM_TRY_BEGIN
CMyComPtr<ICryptoGetTextPassword> getTextPassword;
bool testMode = (_aTestMode != 0);
CMyComPtr<IArchiveExtractCallback> extractCallback = _anExtractCallback;
UInt64 censoredTotalUnPacked = 0,
// censoredTotalPacked = 0,
importantTotalUnPacked = 0;
// importantTotalPacked = 0;
bool allFilesMode = (numItems == UInt32(-1));
if (allFilesMode)
numItems = _refItems.Size();
if(numItems == 0)
return S_OK;
int lastIndex = 0;
CRecordVector<int> importantIndexes;
CRecordVector<bool> extractStatuses;
for(UInt32 t = 0; t < numItems; t++)
{
int index = allFilesMode ? t : indices[t];
const CRefItem &refItem = _refItems[index];
const CItemEx &item = _items[refItem.ItemIndex];
censoredTotalUnPacked += item.UnPackSize;
// censoredTotalPacked += item.PackSize;
int j;
for(j = lastIndex; j <= index; j++)
// if(!_items[_refItems[j].ItemIndex].IsSolid())
if(!IsSolid(j))
lastIndex = j;
for(j = lastIndex; j <= index; j++)
{
const CRefItem &refItem = _refItems[j];
const CItemEx &item = _items[refItem.ItemIndex];
// const CItemEx &item = _items[j];
importantTotalUnPacked += item.UnPackSize;
// importantTotalPacked += item.PackSize;
importantIndexes.Add(j);
extractStatuses.Add(j == index);
}
lastIndex = index + 1;
}
extractCallback->SetTotal(importantTotalUnPacked);
UInt64 currentImportantTotalUnPacked = 0;
UInt64 currentImportantTotalPacked = 0;
UInt64 currentUnPackSize, currentPackSize;
/*
CSysString path = GetCodecsFolderPrefix() + TEXT("Rar29.dll");
TCHAR compressLibPath[MAX_PATH + 64];
if (!GetCompressFolderPrefix(compressLibPath))
return ::GetLastError();
lstrcat(compressLibPath, TEXT("Rar29.dll"));
*/
N7z::LoadMethodMap();
CCoderLibraries libraries;
CObjectVector<CMethodItem> methodItems;
/*
CCoderLibrary compressLib;
CMyComPtr<ICompressCoder> decoder15;
CMyComPtr<ICompressCoder> decoder20;
CMyComPtr<ICompressCoder> decoder29;
*/
NCompress::CCopyCoder *copyCoderSpec = NULL;
CMyComPtr<ICompressCoder> copyCoder;
// CCoderMixer *mixerCoderSpec;
// CMyComPtr<ICompressCoder> mixerCoder;
// bool mixerCoderStoreMethod;
// int mixerCryptoVersion;
CFilterCoder *filterStreamSpec = new CFilterCoder;
CMyComPtr<ISequentialInStream> filterStream = filterStreamSpec;
NCrypto::NRar20::CDecoder *rar20CryptoDecoderSpec = NULL;
CMyComPtr<ICompressFilter> rar20CryptoDecoder;
NCrypto::NRar29::CDecoder *rar29CryptoDecoderSpec = NULL;
CMyComPtr<ICompressFilter> rar29CryptoDecoder;
CFolderInStream *folderInStreamSpec = NULL;
CMyComPtr<ISequentialInStream> folderInStream;
for(int i = 0; i < importantIndexes.Size(); i++,
currentImportantTotalUnPacked += currentUnPackSize,
currentImportantTotalPacked += currentPackSize)
{
RINOK(extractCallback->SetCompleted(
&currentImportantTotalUnPacked));
CMyComPtr<ISequentialOutStream> realOutStream;
Int32 askMode;
if(extractStatuses[i])
askMode = testMode ? NArchive::NExtract::NAskMode::kTest :
NArchive::NExtract::NAskMode::kExtract;
else
askMode = NArchive::NExtract::NAskMode::kSkip;
UInt32 index = importantIndexes[i];
const CRefItem &refItem = _refItems[index];
const CItemEx &item = _items[refItem.ItemIndex];
currentUnPackSize = item.UnPackSize;
currentPackSize = GetPackSize(index);
if(item.IgnoreItem())
continue;
RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
if(item.IsDirectory())
{
RINOK(extractCallback->PrepareOperation(askMode));
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
continue;
}
bool mustBeProcessedAnywhere = false;
if(i < importantIndexes.Size() - 1)
{
// const CRefItem &nextRefItem = _refItems[importantIndexes[i + 1]];
// const CItemEx &nextItemInfo = _items[nextRefItem.ItemIndex];
// mustBeProcessedAnywhere = nextItemInfo.IsSolid();
mustBeProcessedAnywhere = IsSolid(importantIndexes[i + 1]);
}
if (!mustBeProcessedAnywhere && !testMode && !realOutStream)
continue;
if (!realOutStream && !testMode)
askMode = NArchive::NExtract::NAskMode::kSkip;
RINOK(extractCallback->PrepareOperation(askMode));
COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC;
CMyComPtr<ISequentialOutStream> outStream(outStreamSpec);
outStreamSpec->SetStream(realOutStream);
outStreamSpec->Init();
realOutStream.Release();
UInt64 packedPos = currentImportantTotalPacked;
UInt64 unpackedPos = currentImportantTotalUnPacked;
/*
for (int partIndex = 0; partIndex < 1; partIndex++)
{
CMyComPtr<ISequentialInStream> inStream;
// item redefinition
const CItemEx &item = _items[refItem.ItemIndex + partIndex];
NArchive::NRar::CInArchive &archive = _archives[refItem.VolumeIndex + partIndex];
inStream.Attach(archive.CreateLimitedStream(item.GetDataPosition(),
item.PackSize));
*/
if (!folderInStream)
{
folderInStreamSpec = new CFolderInStream;
folderInStream = folderInStreamSpec;
}
folderInStreamSpec->Init(&_archives, &_items, refItem);
CLocalProgress *localProgressSpec = new CLocalProgress;
CMyComPtr<ICompressProgressInfo> progress = localProgressSpec;
localProgressSpec->Init(extractCallback, false);
CLocalCompressProgressInfo *localCompressProgressSpec =
new CLocalCompressProgressInfo;
CMyComPtr<ICompressProgressInfo> compressProgress = localCompressProgressSpec;
localCompressProgressSpec->Init(progress,
&packedPos,
&unpackedPos);
UInt64 packSize = currentPackSize;
// packedPos += item.PackSize;
// unpackedPos += 0;
CMyComPtr<ISequentialInStream> inStream;
if (item.IsEncrypted())
{
CMyComPtr<ICryptoSetPassword> cryptoSetPassword;
if (item.UnPackVersion >= 29)
{
if (!rar29CryptoDecoder)
{
rar29CryptoDecoderSpec = new NCrypto::NRar29::CDecoder;
rar29CryptoDecoder = rar29CryptoDecoderSpec;
// RINOK(rar29CryptoDecoder.CoCreateInstance(CLSID_CCryptoRar29Decoder));
}
rar29CryptoDecoderSpec->SetRar350Mode(item.UnPackVersion < 36);
CMyComPtr<ICompressSetDecoderProperties2> cryptoProperties;
RINOK(rar29CryptoDecoder.QueryInterface(IID_ICompressSetDecoderProperties2,
&cryptoProperties));
RINOK(cryptoProperties->SetDecoderProperties2(item.Salt, item.HasSalt() ? sizeof(item.Salt) : 0));
filterStreamSpec->Filter = rar29CryptoDecoder;
}
else if (item.UnPackVersion >= 20)
{
if (!rar20CryptoDecoder)
{
rar20CryptoDecoderSpec = new NCrypto::NRar20::CDecoder;
rar20CryptoDecoder = rar20CryptoDecoderSpec;
// RINOK(rar20CryptoDecoder.CoCreateInstance(CLSID_CCryptoRar20Decoder));
}
filterStreamSpec->Filter = rar20CryptoDecoder;
}
else
{
outStream.Release();
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod));
continue;
}
RINOK(filterStreamSpec->Filter.QueryInterface(IID_ICryptoSetPassword,
&cryptoSetPassword));
if (!getTextPassword)
extractCallback.QueryInterface(IID_ICryptoGetTextPassword,
&getTextPassword);
if (getTextPassword)
{
CMyComBSTR password;
RINOK(getTextPassword->CryptoGetTextPassword(&password));
if (item.UnPackVersion >= 29)
{
CByteBuffer buffer;
UString unicodePassword(password);
const UInt32 sizeInBytes = unicodePassword.Length() * 2;
buffer.SetCapacity(sizeInBytes);
for (int i = 0; i < unicodePassword.Length(); i++)
{
wchar_t c = unicodePassword[i];
((Byte *)buffer)[i * 2] = (Byte)c;
((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8);
}
RINOK(cryptoSetPassword->CryptoSetPassword(
(const Byte *)buffer, sizeInBytes));
}
else
{
AString oemPassword = UnicodeStringToMultiByte(
(const wchar_t *)password, CP_OEMCP);
RINOK(cryptoSetPassword->CryptoSetPassword(
(const Byte *)(const char *)oemPassword, oemPassword.Length()));
}
}
else
{
RINOK(cryptoSetPassword->CryptoSetPassword(0, 0));
}
filterStreamSpec->SetInStream(folderInStream);
inStream = filterStream;
}
else
{
inStream = folderInStream;
}
CMyComPtr<ICompressCoder> commonCoder;
switch(item.Method)
{
case '0':
{
if(copyCoderSpec == NULL)
{
copyCoderSpec = new NCompress::CCopyCoder;
copyCoder = copyCoderSpec;
}
commonCoder = copyCoder;
break;
}
case '1':
case '2':
case '3':
case '4':
case '5':
{
/*
if (item.UnPackVersion >= 29)
{
outStream.Release();
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod));
continue;
}
*/
int m;
for (m = 0; m < methodItems.Size(); m++)
if (methodItems[m].RarUnPackVersion == item.UnPackVersion)
break;
if (m == methodItems.Size())
{
CMethodItem mi;
mi.RarUnPackVersion = item.UnPackVersion;
N7z::CMethodID methodID = { { 0x04, 0x03 } , 3 };
Byte myID;
if (item.UnPackVersion < 20)
myID = 1;
else if (item.UnPackVersion < 29)
myID = 2;
else
myID = 3;
methodID.ID[2] = myID;
N7z::CMethodInfo methodInfo;
if (!N7z::GetMethodInfo(methodID, methodInfo))
{
RINOK(extractCallback->SetOperationResult(
NArchive::NExtract::NOperationResult::kUnSupportedMethod));
continue;
}
RINOK(libraries.CreateCoder(methodInfo.FilePath,
methodInfo.Decoder, &mi.Coder));
m = methodItems.Add(mi);
}
CMyComPtr<ICompressCoder> decoder = methodItems[m].Coder;
CMyComPtr<ICompressSetDecoderProperties2> compressSetDecoderProperties;
RINOK(decoder.QueryInterface(IID_ICompressSetDecoderProperties2,
&compressSetDecoderProperties));
Byte isSolid = (Byte)((IsSolid(index) || item.IsSplitBefore()) ? 1: 0);
RINOK(compressSetDecoderProperties->SetDecoderProperties2(&isSolid, 1));
commonCoder = decoder;
break;
}
default:
outStream.Release();
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod));
continue;
}
HRESULT result = commonCoder->Code(inStream, outStream,
&packSize, &item.UnPackSize, compressProgress);
if (item.IsEncrypted())
filterStreamSpec->ReleaseInStream();
if (result == S_FALSE)
{
outStream.Release();
RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kDataError));
continue;
}
if (result != S_OK)
return result;
/*
if (refItem.NumItems == 1 &&
!item.IsSplitBefore() && !item.IsSplitAfter())
*/
{
const CItemEx &lastItem = _items[refItem.ItemIndex + refItem.NumItems - 1];
bool crcOK = outStreamSpec->GetCRC() == lastItem.FileCRC;
outStream.Release();
RINOK(extractCallback->SetOperationResult(crcOK ? NArchive::NExtract::NOperationResult::kOK:
NArchive::NExtract::NOperationResult::kCRCError));
}
/*
else
{
bool crcOK = true;
for (int partIndex = 0; partIndex < refItem.NumItems; partIndex++)
{
const CItemEx &item = _items[refItem.ItemIndex + partIndex];
if (item.FileCRC != folderInStreamSpec->CRCs[partIndex])
{
crcOK = false;
break;
}
}
RINOK(extractCallback->SetOperationResult(crcOK ? NArchive::NExtract::NOperationResult::kOK:
NArchive::NExtract::NOperationResult::kCRCError));
}
*/
}
return S_OK;
COM_TRY_END
}
/*
STDMETHODIMP CHandler::ExtractAllItems(Int32 testMode,
IArchiveExtractCallback *extractCallback)
{
COM_TRY_BEGIN
CRecordVector<UInt32> indices;
indices.Reserve(_refItems.Size());
for(int i = 0; i < _refItems.Size(); i++)
indices.Add(i);
return Extract(&indices.Front(), _refItems.Size(), testMode, extractCallback);
COM_TRY_END
}
*/
}}

View File

@@ -0,0 +1,63 @@
// Rar/Handler.h
#ifndef __RAR_HANDLER_H
#define __RAR_HANDLER_H
#include "../IArchive.h"
#include "RarIn.h"
#include "RarVolumeInStream.h"
namespace NArchive {
namespace NRar {
class CHandler:
public IInArchive,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP
STDMETHOD(Open)(IInStream *aStream,
const UInt64 *aMaxCheckStartPosition,
IArchiveOpenCallback *anOpenArchiveCallback);
STDMETHOD(Close)();
STDMETHOD(GetNumberOfItems)(UInt32 *numItems);
STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value);
STDMETHOD(Extract)(const UInt32* indices, UInt32 numItems,
Int32 testMode, IArchiveExtractCallback *anExtractCallback);
STDMETHOD(GetArchiveProperty)(PROPID propID, PROPVARIANT *value);
STDMETHOD(GetNumberOfProperties)(UInt32 *numProperties);
STDMETHOD(GetPropertyInfo)(UInt32 index,
BSTR *name, PROPID *propID, VARTYPE *varType);
STDMETHOD(GetNumberOfArchiveProperties)(UInt32 *numProperties);
STDMETHOD(GetArchivePropertyInfo)(UInt32 index,
BSTR *name, PROPID *propID, VARTYPE *varType);
private:
CRecordVector<CRefItem> _refItems;
CObjectVector<CItemEx> _items;
CObjectVector<CInArchive> _archives;
NArchive::NRar::CInArchiveInfo _archiveInfo;
UInt64 GetPackSize(int refIndex) const;
// NArchive::NRar::CInArchive _archive;
bool IsSolid(int refIndex)
{
const CItemEx &item = _items[_refItems[refIndex].ItemIndex];
if (item.UnPackVersion < 20)
{
if (_archiveInfo.IsSolid())
return (refIndex > 0);
return false;
}
return item.IsSolid();
}
};
}}
#endif

View File

@@ -0,0 +1,21 @@
// Archive/Rar/Headers.cpp
#include "StdAfx.h"
#include "RarHeader.h"
namespace NArchive{
namespace NRar{
namespace NHeader{
Byte kMarker[kMarkerSize] = {0x52 + 1, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00};
class CMarkerInitializer
{
public:
CMarkerInitializer() { kMarker[0]--; };
};
static CMarkerInitializer markerInitializer;
}}}

224
CPP/7zip/Archive/Rar/RarHeader.h Executable file
View File

@@ -0,0 +1,224 @@
// Archive/RarHeader.h
#ifndef __ARCHIVE_RAR_HEADER_H
#define __ARCHIVE_RAR_HEADER_H
#include "Common/Types.h"
namespace NArchive{
namespace NRar{
namespace NHeader{
const int kMarkerSize = 7;
extern Byte kMarker[kMarkerSize];
const int kArchiveSolid = 0x1;
namespace NBlockType
{
enum EBlockType
{
kMarker = 0x72,
kArchiveHeader = 0x73,
kFileHeader = 0x74,
kCommentHeader = 0x75,
kOldAuthenticity = 0x76,
kSubBlock = 0x77,
kRecoveryRecord = 0x78,
kAuthenticity = 0x79,
kEndOfArchive = 0x7B // Is not safe
};
}
namespace NArchive
{
const UInt16 kVolume = 1;
const UInt16 kComment = 2;
const UInt16 kLock = 4;
const UInt16 kSolid = 8;
const UInt16 kNewVolName = 0x10; // ('volname.partN.rar')
const UInt16 kAuthenticity = 0x20;
const UInt16 kRecovery = 0x40;
const UInt16 kBlockEncryption = 0x80;
const UInt16 kFirstVolume = 0x100; // (set only by RAR 3.0 and later)
const UInt16 kEncryptVer = 0x200; // RAR 3.6 there is EncryptVer Byte in End of MainHeader
const int kHeaderSizeMin = 7;
struct CBlock
{
UInt16 CRC;
Byte Type;
UInt16 Flags;
UInt16 Size;
UInt16 Reserved1;
UInt32 Reserved2;
// UInt16 GetRealCRC() const;
};
const int kArchiveHeaderSize = 13;
const int kBlockHeadersAreEncrypted = 0x80;
struct CHeader360: public CBlock
{
Byte EncryptVersion;
bool IsEncrypted() const { return (Flags & NHeader::NArchive::kBlockEncryption) != 0; }
bool IsThereEncryptVer() const { return (Flags & NHeader::NArchive::kEncryptVer) != 0; }
bool IsEncryptOld() const { return (!IsThereEncryptVer() || EncryptVersion < 36); }
UInt32 GetBaseSize() const { return kArchiveHeaderSize + (IsEncryptOld() ? 0 : 1); }
};
}
namespace NFile
{
const int kSplitBefore = 1 << 0;
const int kSplitAfter = 1 << 1;
const int kEncrypted = 1 << 2;
const int kComment = 1 << 3;
const int kSolid = 1 << 4;
const int kDictBitStart = 5;
const int kNumDictBits = 3;
const int kDictMask = (1 << kNumDictBits) - 1;
const int kDictDirectoryValue = 0x7;
const int kSize64Bits = 1 << 8;
const int kUnicodeName = 1 << 9;
const int kSalt = 1 << 10;
const int kOldVersion = 1 << 11;
const int kExtTime = 1 << 12;
// const int kExtFlags = 1 << 13;
// const int kSkipIfUnknown = 1 << 14;
const int kLongBlock = 1 << 15;
/*
struct CBlock
{
// UInt16 HeadCRC;
// Byte Type;
// UInt16 Flags;
// UInt16 HeadSize;
UInt32 PackSize;
UInt32 UnPackSize;
Byte HostOS;
UInt32 FileCRC;
UInt32 Time;
Byte UnPackVersion;
Byte Method;
UInt16 NameSize;
UInt32 Attributes;
};
*/
/*
struct CBlock32
{
UInt16 HeadCRC;
Byte Type;
UInt16 Flags;
UInt16 HeadSize;
UInt32 PackSize;
UInt32 UnPackSize;
Byte HostOS;
UInt32 FileCRC;
UInt32 Time;
Byte UnPackVersion;
Byte Method;
UInt16 NameSize;
UInt32 Attributes;
UInt16 GetRealCRC(const void *aName, UInt32 aNameSize,
bool anExtraDataDefined = false, Byte *anExtraData = 0) const;
};
struct CBlock64
{
UInt16 HeadCRC;
Byte Type;
UInt16 Flags;
UInt16 HeadSize;
UInt32 PackSizeLow;
UInt32 UnPackSizeLow;
Byte HostOS;
UInt32 FileCRC;
UInt32 Time;
Byte UnPackVersion;
Byte Method;
UInt16 NameSize;
UInt32 Attributes;
UInt32 PackSizeHigh;
UInt32 UnPackSizeHigh;
UInt16 GetRealCRC(const void *aName, UInt32 aNameSize) const;
};
*/
const int kLabelFileAttribute = 0x08;
const int kWinFileDirectoryAttributeMask = 0x10;
enum CHostOS
{
kHostMSDOS = 0,
kHostOS2 = 1,
kHostWin32 = 2,
kHostUnix = 3,
kHostMacOS = 4,
kHostBeOS = 5
};
}
namespace NBlock
{
const UInt16 kLongBlock = 1 << 15;
struct CBlock
{
UInt16 CRC;
Byte Type;
UInt16 Flags;
UInt16 HeadSize;
// UInt32 DataSize;
};
}
/*
struct CSubBlock
{
UInt16 HeadCRC;
Byte HeadType;
UInt16 Flags;
UInt16 HeadSize;
UInt32 DataSize;
UInt16 SubType;
Byte Level; // Reserved : Must be 0
};
struct CCommentBlock
{
UInt16 HeadCRC;
Byte HeadType;
UInt16 Flags;
UInt16 HeadSize;
UInt16 UnpSize;
Byte UnpVer;
Byte Method;
UInt16 CommCRC;
};
struct CProtectHeader
{
UInt16 HeadCRC;
Byte HeadType;
UInt16 Flags;
UInt16 HeadSize;
UInt32 DataSize;
Byte Version;
UInt16 RecSectors;
UInt32 TotalBlocks;
Byte Mark[8];
};
*/
}}}
#endif

535
CPP/7zip/Archive/Rar/RarIn.cpp Executable file
View File

@@ -0,0 +1,535 @@
// Archive/RarIn.cpp
#include "StdAfx.h"
#include "Common/StringConvert.h"
#include "Common/CRC.h"
#include "Common/UTFConvert.h"
#include "RarIn.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/StreamUtils.h"
namespace NArchive {
namespace NRar {
static const char kEndOfString = '\0';
void CInArchive::ThrowExceptionWithCode(
CInArchiveException::CCauseType cause)
{
throw CInArchiveException(cause);
}
bool CInArchive::Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit)
{
m_CryptoMode = false;
if(inStream->Seek(0, STREAM_SEEK_CUR, &m_StreamStartPosition) != S_OK)
return false;
m_Position = m_StreamStartPosition;
m_Stream = inStream;
if (ReadMarkerAndArchiveHeader(searchHeaderSizeLimit))
return true;
m_Stream.Release();
return false;
}
void CInArchive::Close()
{
m_Stream.Release();
}
static inline bool TestMarkerCandidate(const void *aTestBytes)
{
for (UInt32 i = 0; i < NHeader::kMarkerSize; i++)
if (((const Byte *)aTestBytes)[i] != NHeader::kMarker[i])
return false;
return true;
}
bool CInArchive::FindAndReadMarker(const UInt64 *searchHeaderSizeLimit)
{
// if (m_Length < NHeader::kMarkerSize)
// return false;
m_ArchiveStartPosition = 0;
m_Position = m_StreamStartPosition;
if(m_Stream->Seek(m_StreamStartPosition, STREAM_SEEK_SET, NULL) != S_OK)
return false;
Byte marker[NHeader::kMarkerSize];
UInt32 processedSize;
ReadBytes(marker, NHeader::kMarkerSize, &processedSize);
if(processedSize != NHeader::kMarkerSize)
return false;
if (TestMarkerCandidate(marker))
return true;
CByteDynamicBuffer dynamicBuffer;
static const UInt32 kSearchMarkerBufferSize = 0x10000;
dynamicBuffer.EnsureCapacity(kSearchMarkerBufferSize);
Byte *buffer = dynamicBuffer;
UInt32 numBytesPrev = NHeader::kMarkerSize - 1;
memmove(buffer, marker + 1, numBytesPrev);
UInt64 curTestPos = m_StreamStartPosition + 1;
for (;;)
{
if (searchHeaderSizeLimit != NULL)
if (curTestPos - m_StreamStartPosition > *searchHeaderSizeLimit)
break;
UInt32 numReadBytes = kSearchMarkerBufferSize - numBytesPrev;
ReadBytes(buffer + numBytesPrev, numReadBytes, &processedSize);
UInt32 numBytesInBuffer = numBytesPrev + processedSize;
if (numBytesInBuffer < NHeader::kMarkerSize)
break;
UInt32 numTests = numBytesInBuffer - NHeader::kMarkerSize + 1;
for(UInt32 pos = 0; pos < numTests; pos++, curTestPos++)
{
if (TestMarkerCandidate(buffer + pos))
{
m_ArchiveStartPosition = curTestPos;
m_Position = curTestPos + NHeader::kMarkerSize;
if(m_Stream->Seek(m_Position, STREAM_SEEK_SET, NULL) != S_OK)
return false;
return true;
}
}
numBytesPrev = numBytesInBuffer - numTests;
memmove(buffer, buffer + numTests, numBytesPrev);
}
return false;
}
void CInArchive::ThrowUnexpectedEndOfArchiveException()
{
ThrowExceptionWithCode(CInArchiveException::kUnexpectedEndOfArchive);
}
bool CInArchive::ReadBytesAndTestSize(void *data, UInt32 size)
{
if (m_CryptoMode)
{
const Byte *bufData = (const Byte *)m_DecryptedData;
UInt32 bufSize = m_DecryptedDataSize;
UInt32 i;
for (i = 0; i < size && m_CryptoPos < bufSize; i++)
((Byte *)data)[i] = bufData[m_CryptoPos++];
return (i == size);
}
UInt32 processedSize;
ReadStream(m_Stream, data, size, &processedSize);
return (processedSize == size);
}
void CInArchive::ReadBytesAndTestResult(void *data, UInt32 size)
{
if(!ReadBytesAndTestSize(data,size))
ThrowUnexpectedEndOfArchiveException();
}
HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 *processedSize)
{
UInt32 realProcessedSize;
HRESULT result = ReadStream(m_Stream, data, size, &realProcessedSize);
if(processedSize != NULL)
*processedSize = realProcessedSize;
AddToSeekValue(realProcessedSize);
return result;
}
bool CInArchive::ReadMarkerAndArchiveHeader(const UInt64 *searchHeaderSizeLimit)
{
if (!FindAndReadMarker(searchHeaderSizeLimit))
return false;
Byte buf[NHeader::NArchive::kArchiveHeaderSize];
UInt32 processedSize;
ReadBytes(buf, sizeof(buf), &processedSize);
if (processedSize != sizeof(buf))
return false;
m_CurData = buf;
m_CurPos = 0;
m_PosLimit = sizeof(buf);
m_ArchiveHeader.CRC = ReadUInt16();
m_ArchiveHeader.Type = ReadByte();
m_ArchiveHeader.Flags = ReadUInt16();
m_ArchiveHeader.Size = ReadUInt16();
m_ArchiveHeader.Reserved1 = ReadUInt16();
m_ArchiveHeader.Reserved2 = ReadUInt32();
m_ArchiveHeader.EncryptVersion = 0;
CCRC crc;
crc.UpdateByte(m_ArchiveHeader.Type);
crc.UpdateUInt16(m_ArchiveHeader.Flags);
crc.UpdateUInt16(m_ArchiveHeader.Size);
crc.UpdateUInt16(m_ArchiveHeader.Reserved1);
crc.UpdateUInt32(m_ArchiveHeader.Reserved2);
if (m_ArchiveHeader.IsThereEncryptVer() && m_ArchiveHeader.Size > NHeader::NArchive::kArchiveHeaderSize)
{
ReadBytes(&m_ArchiveHeader.EncryptVersion, 1, &processedSize);
if (processedSize != 1)
return false;
crc.UpdateByte(m_ArchiveHeader.EncryptVersion);
}
if(m_ArchiveHeader.CRC != (crc.GetDigest() & 0xFFFF))
ThrowExceptionWithCode(CInArchiveException::kArchiveHeaderCRCError);
if (m_ArchiveHeader.Type != NHeader::NBlockType::kArchiveHeader)
return false;
m_ArchiveCommentPosition = m_Position;
m_SeekOnArchiveComment = true;
return true;
}
void CInArchive::SkipArchiveComment()
{
if (!m_SeekOnArchiveComment)
return;
AddToSeekValue(m_ArchiveHeader.Size - m_ArchiveHeader.GetBaseSize());
m_SeekOnArchiveComment = false;
}
void CInArchive::GetArchiveInfo(CInArchiveInfo &archiveInfo) const
{
archiveInfo.StartPosition = m_ArchiveStartPosition;
archiveInfo.Flags = m_ArchiveHeader.Flags;
archiveInfo.CommentPosition = m_ArchiveCommentPosition;
archiveInfo.CommentSize = (UInt16)(m_ArchiveHeader.Size - NHeader::NArchive::kArchiveHeaderSize);
}
static void DecodeUnicodeFileName(const char *name, const Byte *encName,
int encSize, wchar_t *unicodeName, int maxDecSize)
{
int encPos = 0;
int decPos = 0;
int flagBits = 0;
Byte flags = 0;
Byte highByte = encName[encPos++];
while (encPos < encSize && decPos < maxDecSize)
{
if (flagBits == 0)
{
flags = encName[encPos++];
flagBits = 8;
}
switch(flags >> 6)
{
case 0:
unicodeName[decPos++] = encName[encPos++];
break;
case 1:
unicodeName[decPos++] = (wchar_t)(encName[encPos++] + (highByte << 8));
break;
case 2:
unicodeName[decPos++] = (wchar_t)(encName[encPos] + (encName[encPos + 1] << 8));
encPos += 2;
break;
case 3:
{
int length = encName[encPos++];
if (length & 0x80)
{
Byte correction = encName[encPos++];
for (length = (length & 0x7f) + 2;
length > 0 && decPos < maxDecSize; length--, decPos++)
unicodeName[decPos] = (wchar_t)(((name[decPos] + correction) & 0xff) + (highByte << 8));
}
else
for (length += 2; length > 0 && decPos < maxDecSize; length--, decPos++)
unicodeName[decPos] = name[decPos];
}
break;
}
flags <<= 2;
flagBits -= 2;
}
unicodeName[decPos < maxDecSize ? decPos : maxDecSize - 1] = 0;
}
void CInArchive::ReadName(CItemEx &item, int nameSize)
{
item.UnicodeName.Empty();
if (nameSize > 0)
{
m_NameBuffer.EnsureCapacity(nameSize + 1);
char *buffer = (char *)m_NameBuffer;
for (int i = 0; i < nameSize; i++)
buffer[i] = ReadByte();
int mainLen;
for (mainLen = 0; mainLen < nameSize; mainLen++)
if (buffer[mainLen] == '\0')
break;
buffer[mainLen] = '\0';
item.Name = buffer;
if(item.HasUnicodeName())
{
if(mainLen < nameSize)
{
int unicodeNameSizeMax = MyMin(nameSize, (0x400));
_unicodeNameBuffer.EnsureCapacity(unicodeNameSizeMax + 1);
DecodeUnicodeFileName(buffer, (const Byte *)buffer + mainLen + 1,
nameSize - (mainLen + 1), _unicodeNameBuffer, unicodeNameSizeMax);
item.UnicodeName = _unicodeNameBuffer;
}
else if (!ConvertUTF8ToUnicode(item.Name, item.UnicodeName))
item.UnicodeName.Empty();
}
}
else
item.Name.Empty();
}
Byte CInArchive::ReadByte()
{
if (m_CurPos >= m_PosLimit)
throw CInArchiveException(CInArchiveException::kIncorrectArchive);
return m_CurData[m_CurPos++];
}
UInt16 CInArchive::ReadUInt16()
{
UInt16 value = 0;
for (int i = 0; i < 2; i++)
{
Byte b = ReadByte();
value |= (UInt16(b) << (8 * i));
}
return value;
}
UInt32 CInArchive::ReadUInt32()
{
UInt32 value = 0;
for (int i = 0; i < 4; i++)
{
Byte b = ReadByte();
value |= (UInt32(b) << (8 * i));
}
return value;
}
void CInArchive::ReadTime(Byte mask, CRarTime &rarTime)
{
rarTime.LowSecond = (Byte)(((mask & 4) != 0) ? 1 : 0);
int numDigits = (mask & 3);
rarTime.SubTime[0] = rarTime.SubTime[1] = rarTime.SubTime[2] = 0;
for (int i = 0; i < numDigits; i++)
rarTime.SubTime[3 - numDigits + i] = ReadByte();
}
void CInArchive::ReadHeaderReal(CItemEx &item)
{
item.Flags = m_BlockHeader.Flags;
item.PackSize = ReadUInt32();
item.UnPackSize = ReadUInt32();
item.HostOS = ReadByte();
item.FileCRC = ReadUInt32();
item.LastWriteTime.DosTime = ReadUInt32();
item.UnPackVersion = ReadByte();
item.Method = ReadByte();
int nameSize = ReadUInt16();
item.Attributes = ReadUInt32();
item.LastWriteTime.LowSecond = 0;
item.LastWriteTime.SubTime[0] =
item.LastWriteTime.SubTime[1] =
item.LastWriteTime.SubTime[2] = 0;
if((item.Flags & NHeader::NFile::kSize64Bits) != 0)
{
item.PackSize |= ((UInt64)ReadUInt32() << 32);
item.UnPackSize |= ((UInt64)ReadUInt32() << 32);
}
ReadName(item, nameSize);
if (item.HasSalt())
for (int i = 0; i < sizeof(item.Salt); i++)
item.Salt[i] = ReadByte();
// some rar archives have HasExtTime flag without field.
if (m_CurPos < m_PosLimit && item.HasExtTime())
{
Byte accessMask = (Byte)(ReadByte() >> 4);
Byte b = ReadByte();
Byte modifMask = (Byte)(b >> 4);
Byte createMask = (Byte)(b & 0xF);
if ((modifMask & 8) != 0)
ReadTime(modifMask, item.LastWriteTime);
item.IsCreationTimeDefined = ((createMask & 8) != 0);
if (item.IsCreationTimeDefined)
{
item.CreationTime.DosTime = ReadUInt32();
ReadTime(createMask, item.CreationTime);
}
item.IsLastAccessTimeDefined = ((accessMask & 8) != 0);
if (item.IsLastAccessTimeDefined)
{
item.LastAccessTime.DosTime = ReadUInt32();
ReadTime(accessMask, item.LastAccessTime);
}
}
UInt16 fileHeaderWithNameSize = (UInt16)m_CurPos;
item.Position = m_Position;
item.MainPartSize = fileHeaderWithNameSize;
item.CommentSize = (UInt16)(m_BlockHeader.HeadSize - fileHeaderWithNameSize);
if (m_CryptoMode)
item.AlignSize = (UInt16)((16 - ((m_BlockHeader.HeadSize) & 0xF)) & 0xF);
else
item.AlignSize = 0;
AddToSeekValue(m_BlockHeader.HeadSize);
}
void CInArchive::AddToSeekValue(UInt64 addValue)
{
m_Position += addValue;
}
HRESULT CInArchive::GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword)
{
if (m_SeekOnArchiveComment)
SkipArchiveComment();
for (;;)
{
if(!SeekInArchive(m_Position))
return S_FALSE;
if (!m_CryptoMode && (m_ArchiveHeader.Flags &
NHeader::NArchive::kBlockHeadersAreEncrypted) != 0)
{
m_CryptoMode = false;
if (getTextPassword == 0)
return S_FALSE;
if(!SeekInArchive(m_Position))
return S_FALSE;
if (!m_RarAES)
{
m_RarAESSpec = new NCrypto::NRar29::CDecoder;
m_RarAES = m_RarAESSpec;
}
m_RarAESSpec->SetRar350Mode(m_ArchiveHeader.IsEncryptOld());
// Salt
const UInt32 kSaltSize = 8;
Byte salt[kSaltSize];
if(!ReadBytesAndTestSize(salt, kSaltSize))
return false;
m_Position += kSaltSize;
RINOK(m_RarAESSpec->SetDecoderProperties2(salt, kSaltSize))
// Password
CMyComBSTR password;
RINOK(getTextPassword->CryptoGetTextPassword(&password))
UString unicodePassword(password);
CByteBuffer buffer;
const UInt32 sizeInBytes = unicodePassword.Length() * 2;
buffer.SetCapacity(sizeInBytes);
for (int i = 0; i < unicodePassword.Length(); i++)
{
wchar_t c = unicodePassword[i];
((Byte *)buffer)[i * 2] = (Byte)c;
((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8);
}
RINOK(m_RarAESSpec->CryptoSetPassword((const Byte *)buffer, sizeInBytes));
const UInt32 kDecryptedBufferSize = (1 << 12);
if (m_DecryptedData.GetCapacity() == 0)
{
m_DecryptedData.SetCapacity(kDecryptedBufferSize);
}
RINOK(m_RarAES->Init());
RINOK(ReadStream(m_Stream, (Byte *)m_DecryptedData, kDecryptedBufferSize, &m_DecryptedDataSize));
m_DecryptedDataSize = m_RarAES->Filter((Byte *)m_DecryptedData, m_DecryptedDataSize);
m_CryptoMode = true;
m_CryptoPos = 0;
}
m_FileHeaderData.EnsureCapacity(7);
if(!ReadBytesAndTestSize((Byte *)m_FileHeaderData, 7))
return S_FALSE;
m_CurData = (Byte *)m_FileHeaderData;
m_CurPos = 0;
m_PosLimit = 7;
m_BlockHeader.CRC = ReadUInt16();
m_BlockHeader.Type = ReadByte();
m_BlockHeader.Flags = ReadUInt16();
m_BlockHeader.HeadSize = ReadUInt16();
if (m_BlockHeader.HeadSize < 7)
ThrowExceptionWithCode(CInArchiveException::kIncorrectArchive);
if (m_BlockHeader.Type == NHeader::NBlockType::kEndOfArchive)
return S_FALSE;
if (m_BlockHeader.Type == NHeader::NBlockType::kFileHeader)
{
m_FileHeaderData.EnsureCapacity(m_BlockHeader.HeadSize);
m_CurData = (Byte *)m_FileHeaderData;
m_PosLimit = m_BlockHeader.HeadSize;
ReadBytesAndTestResult(m_CurData + m_CurPos, m_BlockHeader.HeadSize - 7);
ReadHeaderReal(item);
if ((CCRC::CalculateDigest(m_CurData + 2,
m_BlockHeader.HeadSize - item.CommentSize - 2) & 0xFFFF) != m_BlockHeader.CRC)
ThrowExceptionWithCode(CInArchiveException::kFileHeaderCRCError);
FinishCryptoBlock();
m_CryptoMode = false;
SeekInArchive(m_Position); // Move Position to compressed Data;
AddToSeekValue(item.PackSize); // m_Position points to next header;
return S_OK;
}
if (m_CryptoMode && m_BlockHeader.HeadSize > (1 << 12))
return E_FAIL; // it's for bad passwords
if ((m_BlockHeader.Flags & NHeader::NBlock::kLongBlock) != 0)
{
m_FileHeaderData.EnsureCapacity(7 + 4);
m_CurData = (Byte *)m_FileHeaderData;
ReadBytesAndTestResult(m_CurData + m_CurPos, 4); // test it
m_PosLimit = 7 + 4;
UInt32 dataSize = ReadUInt32();
AddToSeekValue(dataSize);
if (m_CryptoMode && dataSize > (1 << 27))
return E_FAIL; // it's for bad passwords
m_CryptoPos = m_BlockHeader.HeadSize;
}
else
m_CryptoPos = 0;
AddToSeekValue(m_BlockHeader.HeadSize);
FinishCryptoBlock();
m_CryptoMode = false;
}
}
void CInArchive::DirectGetBytes(void *data, UInt32 size)
{
ReadStream(m_Stream, data, size, NULL);
}
bool CInArchive::SeekInArchive(UInt64 position)
{
UInt64 newPosition;
m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition);
return newPosition == position;
}
ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size)
{
CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
CMyComPtr<ISequentialInStream> inStream(streamSpec);
SeekInArchive(position);
streamSpec->SetStream(m_Stream);
streamSpec->Init(size);
return inStream.Detach();
}
}}

124
CPP/7zip/Archive/Rar/RarIn.h Executable file
View File

@@ -0,0 +1,124 @@
// RarIn.h
#ifndef __ARCHIVE_RAR_IN_H
#define __ARCHIVE_RAR_IN_H
#include "Common/DynamicBuffer.h"
#include "Common/Exception.h"
#include "Common/MyCom.h"
#include "../../IStream.h"
#include "../../ICoder.h"
#include "../../Common/StreamObjects.h"
#include "../../Crypto/RarAES/RarAES.h"
#include "RarHeader.h"
#include "RarItem.h"
namespace NArchive {
namespace NRar {
class CInArchiveException
{
public:
enum CCauseType
{
kUnexpectedEndOfArchive = 0,
kArchiveHeaderCRCError,
kFileHeaderCRCError,
kIncorrectArchive,
}
Cause;
CInArchiveException(CCauseType cause) : Cause(cause) {}
};
class CInArchiveInfo
{
public:
UInt64 StartPosition;
WORD Flags;
UInt64 CommentPosition;
WORD CommentSize;
bool IsSolid() const { return (Flags & NHeader::NArchive::kSolid) != 0; }
bool IsCommented() const { return (Flags & NHeader::NArchive::kComment) != 0; }
bool IsVolume() const { return (Flags & NHeader::NArchive::kVolume) != 0; }
bool HaveNewVolumeName() const { return (Flags & NHeader::NArchive::kNewVolName) != 0; }
bool IsEncrypted() const { return (Flags & NHeader::NArchive::kBlockEncryption) != 0; }
};
class CInArchive
{
CMyComPtr<IInStream> m_Stream;
UInt64 m_StreamStartPosition;
UInt64 m_Position;
UInt64 m_ArchiveStartPosition;
NHeader::NArchive::CHeader360 m_ArchiveHeader;
CDynamicBuffer<char> m_NameBuffer;
CDynamicBuffer<wchar_t> _unicodeNameBuffer;
bool m_SeekOnArchiveComment;
UInt64 m_ArchiveCommentPosition;
void ReadName(CItemEx &item, int nameSize);
void ReadHeaderReal(CItemEx &item);
HRESULT ReadBytes(void *data, UInt32 size, UInt32 *aProcessedSize);
bool ReadBytesAndTestSize(void *data, UInt32 size);
void ReadBytesAndTestResult(void *data, UInt32 size);
bool FindAndReadMarker(const UInt64 *searchHeaderSizeLimit);
void ThrowExceptionWithCode(CInArchiveException::CCauseType cause);
void ThrowUnexpectedEndOfArchiveException();
void AddToSeekValue(UInt64 addValue);
protected:
CDynamicBuffer<Byte> m_FileHeaderData;
NHeader::NBlock::CBlock m_BlockHeader;
NCrypto::NRar29::CDecoder *m_RarAESSpec;
CMyComPtr<ICompressFilter> m_RarAES;
Byte *m_CurData; // it must point to start of Rar::Block
UInt32 m_CurPos;
UInt32 m_PosLimit;
Byte ReadByte();
UInt16 ReadUInt16();
UInt32 ReadUInt32();
void ReadTime(Byte mask, CRarTime &rarTime);
CBuffer<Byte> m_DecryptedData;
UInt32 m_DecryptedDataSize;
bool m_CryptoMode;
UInt32 m_CryptoPos;
void FinishCryptoBlock()
{
if (m_CryptoMode)
while ((m_CryptoPos & 0xF) != 0)
{
m_CryptoPos++;
m_Position++;
}
}
bool ReadMarkerAndArchiveHeader(const UInt64 *searchHeaderSizeLimit);
public:
bool Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit);
void Close();
HRESULT GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword);
void SkipArchiveComment();
void GetArchiveInfo(CInArchiveInfo &archiveInfo) const;
void DirectGetBytes(void *data, UInt32 size);
bool SeekInArchive(UInt64 position);
ISequentialInStream *CreateLimitedStream(UInt64 position, UInt64 size);
};
}}
#endif

118
CPP/7zip/Archive/Rar/RarItem.cpp Executable file
View File

@@ -0,0 +1,118 @@
// RarItem.cpp
#include "StdAfx.h"
#include "RarItem.h"
#include "RarHeader.h"
namespace NArchive{
namespace NRar{
bool CItem::IsEncrypted() const
{ return (Flags & NHeader::NFile::kEncrypted) != 0; }
bool CItem::IsSolid() const
{ return (Flags & NHeader::NFile::kSolid) != 0; }
bool CItem::IsCommented() const
{ return (Flags & NHeader::NFile::kComment) != 0; }
bool CItem::IsSplitBefore() const
{ return (Flags & NHeader::NFile::kSplitBefore) != 0; }
bool CItem::IsSplitAfter() const
{ return (Flags & NHeader::NFile::kSplitAfter) != 0; }
bool CItem::HasSalt() const
{ return (Flags & NHeader::NFile::kSalt) != 0; }
bool CItem::HasExtTime() const
{ return (Flags & NHeader::NFile::kExtTime) != 0; }
bool CItem::HasUnicodeName() const
{ return (Flags & NHeader::NFile::kUnicodeName) != 0; }
bool CItem::IsOldVersion() const
{ return (Flags & NHeader::NFile::kOldVersion) != 0; }
bool CItem::IgnoreItem() const
{
switch(HostOS)
{
case NHeader::NFile::kHostMSDOS:
case NHeader::NFile::kHostOS2:
case NHeader::NFile::kHostWin32:
return ((Attributes & NHeader::NFile::kLabelFileAttribute) != 0);
}
return false;
}
UInt32 CItem::GetDictSize() const
{ return (Flags >> NHeader::NFile::kDictBitStart) & NHeader::NFile::kDictMask; }
bool CItem::IsDirectory() const
{
if (GetDictSize() == NHeader::NFile::kDictDirectoryValue)
return true;
switch(HostOS)
{
case NHeader::NFile::kHostMSDOS:
case NHeader::NFile::kHostOS2:
case NHeader::NFile::kHostWin32:
if ((Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
return true;
}
return false;
}
UInt32 CItem::GetWinAttributes() const
{
UInt32 winAttributes;
switch(HostOS)
{
case NHeader::NFile::kHostMSDOS:
case NHeader::NFile::kHostOS2:
case NHeader::NFile::kHostWin32:
winAttributes = Attributes;
break;
default:
winAttributes = 0; // must be converted from unix value;;
}
if (IsDirectory()) // test it;
winAttributes |= NHeader::NFile::kWinFileDirectoryAttributeMask;
return winAttributes;
}
void CItem::ClearFlags()
{ Flags = 0; }
void CItem::SetFlagBits(int startBitNumber, int numBits, int value)
{
UInt16 mask = (UInt16)(((1 << numBits) - 1) << startBitNumber);
Flags &= ~mask;
Flags |= value << startBitNumber;
}
void CItem::SetBitMask(int bitMask, bool enable)
{
if(enable)
Flags |= bitMask;
else
Flags &= ~bitMask;
}
void CItem::SetDictSize(UInt32 size)
{
SetFlagBits(NHeader::NFile::kDictBitStart, NHeader::NFile::kNumDictBits, (size & NHeader::NFile::kDictMask));
}
void CItem::SetAsDirectory(bool directory)
{
if (directory)
SetDictSize(NHeader::NFile::kDictDirectoryValue);
}
void CItem::SetEncrypted(bool encrypted)
{ SetBitMask(NHeader::NFile::kEncrypted, encrypted); }
void CItem::SetSolid(bool solid)
{ SetBitMask(NHeader::NFile::kSolid, solid); }
void CItem::SetCommented(bool commented)
{ SetBitMask(NHeader::NFile::kComment, commented); }
void CItem::SetSplitBefore(bool splitBefore)
{ SetBitMask(NHeader::NFile::kSplitBefore, splitBefore); }
void CItem::SetSplitAfter(bool splitAfter)
{ SetBitMask(NHeader::NFile::kSplitAfter, splitAfter); }
}}

91
CPP/7zip/Archive/Rar/RarItem.h Executable file
View File

@@ -0,0 +1,91 @@
// RarItem.h
#ifndef __ARCHIVE_RAR_ITEM_H
#define __ARCHIVE_RAR_ITEM_H
#include "Common/Types.h"
#include "Common/String.h"
namespace NArchive{
namespace NRar{
struct CRarTime
{
UInt32 DosTime;
Byte LowSecond;
Byte SubTime[3];
};
class CItem
{
public:
UInt16 Flags;
UInt64 PackSize;
UInt64 UnPackSize;
Byte HostOS;
UInt32 FileCRC;
CRarTime CreationTime;
CRarTime LastWriteTime;
CRarTime LastAccessTime;
bool IsCreationTimeDefined;
// bool IsLastWriteTimeDefined;
bool IsLastAccessTimeDefined;
Byte UnPackVersion;
Byte Method;
UInt32 Attributes;
AString Name;
UString UnicodeName;
Byte Salt[8];
bool IsEncrypted() const;
bool IsSolid() const;
bool IsCommented() const;
bool IsSplitBefore() const;
bool IsSplitAfter() const;
bool HasSalt() const;
bool HasExtTime() const;
bool HasUnicodeName() const;
bool IsOldVersion() const;
UInt32 GetDictSize() const;
bool IsDirectory() const;
bool IgnoreItem() const;
UInt32 GetWinAttributes() const;
CItem(): IsCreationTimeDefined(false), IsLastAccessTimeDefined(false) {}
private:
void SetFlagBits(int startBitNumber, int numBits, int value);
void SetBitMask(int bitMask, bool enable);
public:
void ClearFlags();
void SetDictSize(UInt32 size);
void SetAsDirectory(bool directory);
void SetEncrypted(bool encrypted);
void SetSolid(bool solid);
void SetCommented(bool commented);
void SetSplitBefore(bool splitBefore);
void SetSplitAfter(bool splitAfter);
};
class CItemEx: public CItem
{
public:
UInt64 Position;
UInt16 MainPartSize;
UInt16 CommentSize;
UInt16 AlignSize;
UInt64 GetFullSize() const { return MainPartSize + CommentSize + AlignSize + PackSize; };
// DWORD GetHeaderWithCommentSize() const { return MainPartSize + CommentSize; };
UInt64 GetCommentPosition() const { return Position + MainPartSize; };
UInt64 GetDataPosition() const { return GetCommentPosition() + CommentSize + AlignSize; };
};
}}
#endif

View File

@@ -0,0 +1,79 @@
// RarVolumeInStream.cpp
#include "StdAfx.h"
#include "RarVolumeInStream.h"
#include "Windows/Defs.h"
#include "Common/Defs.h"
namespace NArchive {
namespace NRar {
void CFolderInStream::Init(
CObjectVector<CInArchive> *archives,
const CObjectVector<CItemEx> *items,
const CRefItem &refItem)
{
_archives = archives;
_items = items;
_refItem = refItem;
_curIndex = 0;
CRCs.Clear();
_fileIsOpen = false;
}
HRESULT CFolderInStream::OpenStream()
{
while (_curIndex < _refItem.NumItems)
{
const CItemEx &item = (*_items)[_refItem.ItemIndex + _curIndex];
_stream.Attach((*_archives)[_refItem.VolumeIndex + _curIndex].
CreateLimitedStream(item.GetDataPosition(), item.PackSize));
_curIndex++;
_fileIsOpen = true;
_crc.Init();
return S_OK;
}
return S_OK;
}
HRESULT CFolderInStream::CloseStream()
{
CRCs.Add(_crc.GetDigest());
_stream.Release();
_fileIsOpen = false;
return S_OK;
}
STDMETHODIMP CFolderInStream::Read(void *data, UInt32 size, UInt32 *processedSize)
{
UInt32 realProcessedSize = 0;
while ((_curIndex < _refItem.NumItems || _fileIsOpen) && size > 0)
{
if (_fileIsOpen)
{
UInt32 localProcessedSize;
RINOK(_stream->Read(
((Byte *)data) + realProcessedSize, size, &localProcessedSize));
_crc.Update(((Byte *)data) + realProcessedSize, localProcessedSize);
if (localProcessedSize == 0)
{
RINOK(CloseStream());
continue;
}
realProcessedSize += localProcessedSize;
size -= localProcessedSize;
break;
}
else
{
RINOK(OpenStream());
}
}
if (processedSize != 0)
*processedSize = realProcessedSize;
return S_OK;
}
}}

View File

@@ -0,0 +1,50 @@
// RarVolumeInStream.h
#ifndef __RAR_VOLUME_IN_STREAM_H
#define __RAR_VOLUME_IN_STREAM_H
#include "../../IStream.h"
#include "Common/CRC.h"
#include "RarIn.h"
namespace NArchive {
namespace NRar {
struct CRefItem
{
int VolumeIndex;
int ItemIndex;
int NumItems;
};
class CFolderInStream:
public ISequentialInStream,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
private:
CObjectVector<CInArchive> *_archives;
const CObjectVector<CItemEx> *_items;
CRefItem _refItem;
int _curIndex;
CCRC _crc;
bool _fileIsOpen;
CMyComPtr<ISequentialInStream> _stream;
HRESULT OpenStream();
HRESULT CloseStream();
public:
void Init(CObjectVector<CInArchive> *archives,
const CObjectVector<CItemEx> *items,
const CRefItem &refItem);
CRecordVector<UInt32> CRCs;
};
}}
#endif

View File

@@ -0,0 +1,3 @@
// StdAfx.cpp
#include "StdAfx.h"

8
CPP/7zip/Archive/Rar/StdAfx.h Executable file
View File

@@ -0,0 +1,8 @@
// StdAfx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include "../../../Common/MyWindows.h"
#endif

90
CPP/7zip/Archive/Rar/makefile Executable file
View File

@@ -0,0 +1,90 @@
PROG = rar.dll
DEF_FILE = ../Archive.def
CFLAGS = $(CFLAGS) -I ../../../
LIBS = $(LIBS) oleaut32.lib user32.lib
RAR_OBJS = \
$O\DllExports.obj \
$O\RarHandler.obj \
$O\RarHeader.obj \
$O\RarIn.obj \
$O\RarItem.obj \
$O\RarVolumeInStream.obj \
COMMON_OBJS = \
$O\Alloc.obj \
$O\CRC.obj \
$O\IntToString.obj \
$O\NewHandler.obj \
$O\String.obj \
$O\StringConvert.obj \
$O\UTFConvert.obj \
$O\Vector.obj \
WIN_OBJS = \
$O\DLL.obj \
$O\FileFind.obj \
$O\PropVariant.obj \
7ZIP_COMMON_OBJS = \
$O\LimitedStreams.obj \
$O\ProgressUtils.obj \
$O\StreamObjects.obj \
$O\StreamUtils.obj \
AR_COMMON_OBJS = \
$O\CodecsPath.obj \
$O\FilterCoder.obj \
$O\InStreamWithCRC.obj \
$O\OutStreamWithCRC.obj \
7Z_OBJS = \
$O\7zMethodID.obj \
$O\7zMethods.obj \
CRYPTO_HASH_OBJS = \
$O\Sha1.obj \
CRYPTO_RAR20_OBJS = \
$O\Rar20Cipher.obj \
$O\Rar20Crypto.obj \
CRYPTO_RARAES_OBJS = \
$O\RarAES.obj \
OBJS = \
$O\StdAfx.obj \
$(RAR_OBJS) \
$(COMMON_OBJS) \
$(WIN_OBJS) \
$(7ZIP_COMMON_OBJS) \
$(AR_COMMON_OBJS) \
$(7Z_OBJS) \
$(CRYPTO_HASH_OBJS) \
$(CRYPTO_RAR20_OBJS) \
$(CRYPTO_RARAES_OBJS) \
$O\CopyCoder.obj \
$O\resource.res
!include "../../../Build.mak"
$(RAR_OBJS): $(*B).cpp
$(COMPL)
$(COMMON_OBJS): ../../../Common/$(*B).cpp
$(COMPL)
$(WIN_OBJS): ../../../Windows/$(*B).cpp
$(COMPL)
$(7ZIP_COMMON_OBJS): ../../Common/$(*B).cpp
$(COMPL)
$(AR_COMMON_OBJS): ../Common/$(*B).cpp
$(COMPL)
$(7Z_OBJS): ../7z/$(*B).cpp
$(COMPL)
$(CRYPTO_HASH_OBJS): ../../Crypto/Hash/$(*B).cpp
$(COMPL_O2)
$(CRYPTO_RAR20_OBJS): ../../Crypto/Rar20/$(*B).cpp
$(COMPL)
$(CRYPTO_RARAES_OBJS): ../../Crypto/RarAES/$(*B).cpp
$(COMPL)
$O\CopyCoder.obj: ../../Compress/Copy/$(*B).cpp
$(COMPL)

BIN
CPP/7zip/Archive/Rar/rar.ico Executable file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@@ -0,0 +1,5 @@
#include "../../MyVersionInfo.rc"
MY_VERSION_INFO_DLL("Rar Plugin", "rar")
101 ICON "rar.ico"