This commit is contained in:
Igor Pavlov
2005-05-30 00:00:00 +00:00
committed by Kornel Lesiński
parent 8c1b5c7b7e
commit 3c510ba80b
926 changed files with 40559 additions and 23519 deletions

View File

@@ -1,8 +1,8 @@
// stdafx.h
// StdAfx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#include "../../../Common/MyWindows.h"
#endif
#endif

View File

@@ -8,111 +8,97 @@
namespace NCrypto {
namespace NZip {
/*
const int kBufferSize = 1 << 17;
CBuffer2::CBuffer2():
_buffer(0)
{
_buffer = new BYTE[kBufferSize];
_buffer = new Byte[kBufferSize];
}
CBuffer2::~CBuffer2()
{
delete []_buffer;
}
*/
STDMETHODIMP CEncoder::CryptoSetPassword(const BYTE *data, UINT32 size)
STDMETHODIMP CEncoder::CryptoSetPassword(const Byte *data, UInt32 size)
{
_cipher.SetPassword(data, size);
return S_OK;
}
STDMETHODIMP CEncoder::CryptoSetCRC(UINT32 crc)
STDMETHODIMP CEncoder::CryptoSetCRC(UInt32 crc)
{
_crc = crc;
return S_OK;
}
STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ICompressProgressInfo *progress)
STDMETHODIMP CEncoder::Init()
{
return S_OK;
}
HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream)
{
CRandom random;
random.Init(::GetTickCount());
UINT64 nowPos = 0;
BYTE header[kHeaderSize];
UInt64 nowPos = 0;
Byte header[kHeaderSize];
for (int i = 0; i < kHeaderSize - 2; i++)
{
header[i] = BYTE(random.Generate());
header[i] = Byte(random.Generate());
}
header[kHeaderSize - 1] = BYTE(_crc >> 24);
header[kHeaderSize - 2] = BYTE(_crc >> 16);
header[kHeaderSize - 1] = Byte(_crc >> 24);
header[kHeaderSize - 2] = Byte(_crc >> 16);
UINT32 processedSize;
UInt32 processedSize;
_cipher.EncryptHeader(header);
RINOK(outStream->Write(header, kHeaderSize, &processedSize));
if (processedSize != kHeaderSize)
return E_FAIL;
while(true)
{
if (outSize != NULL && nowPos == *outSize)
return S_OK;
RINOK(inStream->Read(_buffer, kBufferSize, &processedSize));
if (processedSize == 0)
return S_OK;
for (UINT32 i = 0; i < processedSize; i++)
_buffer[i] = _cipher.EncryptByte(_buffer[i]);
UINT32 size = processedSize;
if (outSize != NULL && nowPos + size > *outSize)
size = UINT32(*outSize - nowPos);
RINOK(outStream->Write(_buffer, size, &processedSize));
if (size != processedSize)
return E_FAIL;
nowPos += processedSize;
}
return S_OK;
}
STDMETHODIMP CDecoder::CryptoSetPassword(const BYTE *data, UINT32 size)
STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size)
{
UInt32 i;
for (i = 0; i < size; i++)
data[i] = _cipher.EncryptByte(data[i]);
return i;
}
STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size)
{
_cipher.SetPassword(data, size);
return S_OK;
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ICompressProgressInfo *progress)
HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream)
{
UINT64 nowPos = 0;
if (inSize != NULL && *inSize == 0)
return S_OK;
BYTE header[kHeaderSize];
UINT32 processedSize;
UInt64 nowPos = 0;
Byte header[kHeaderSize];
UInt32 processedSize;
RINOK(inStream->Read(header, kHeaderSize, &processedSize));
if (processedSize != kHeaderSize)
return E_FAIL;
_cipher.DecryptHeader(header);
return S_OK;
}
while(true)
{
if (outSize != NULL && nowPos == *outSize)
return S_OK;
RINOK(inStream->Read(_buffer, kBufferSize, &processedSize));
if (processedSize == 0)
return S_OK;
for (UINT32 i = 0; i < processedSize; i++)
_buffer[i] = _cipher.DecryptByte(_buffer[i]);
UINT32 size = processedSize;
if (outSize != NULL && nowPos + size > *outSize)
size = UINT32(*outSize - nowPos);
RINOK(outStream->Write(_buffer, size, &processedSize));
if (size != processedSize)
return E_FAIL;
nowPos += processedSize;
}
STDMETHODIMP CDecoder::Init()
{
return S_OK;
}
STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size)
{
UInt32 i;
for (i = 0; i < size; i++)
data[i] = _cipher.DecryptByte(data[i]);
return i;
}
}}

View File

@@ -15,56 +15,56 @@
namespace NCrypto {
namespace NZip {
/*
class CBuffer2
{
protected:
BYTE *_buffer;
Byte *_buffer;
public:
CBuffer2();
~CBuffer2();
};
*/
class CEncoder :
public ICompressCoder,
public ICompressFilter,
public ICryptoSetPassword,
public ICryptoSetCRC,
public CMyUnknownImp,
public CBuffer2
public CMyUnknownImp
// public CBuffer2
{
CCipher _cipher;
UINT32 _crc;
UInt32 _crc;
public:
MY_UNKNOWN_IMP2(
ICryptoSetPassword,
ICryptoSetCRC
)
STDMETHOD(Code)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ICompressProgressInfo *progress);
STDMETHOD(Init)();
STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
STDMETHOD(CryptoSetPassword)(const BYTE *data, UINT32 size);
STDMETHOD(CryptoSetCRC)(UINT32 crc);
STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
STDMETHOD(CryptoSetCRC)(UInt32 crc);
HRESULT WriteHeader(ISequentialOutStream *outStream);
};
class CDecoder:
public ICompressCoder,
public ICompressFilter,
public ICryptoSetPassword,
public CMyUnknownImp,
public CBuffer2
public CMyUnknownImp
// public CBuffer2
{
CCipher _cipher;
public:
MY_UNKNOWN_IMP1(ICryptoSetPassword)
STDMETHOD(Code)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, UINT64 const *inSize,
const UINT64 *outSize,
ICompressProgressInfo *progress);
STDMETHOD(Init)();
STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
STDMETHOD(CryptoSetPassword)(const BYTE *data, UINT32 size);
HRESULT ReadHeader(ISequentialInStream *inStream);
STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
};
}}

View File

@@ -3,60 +3,60 @@
#include "StdAfx.h"
#include "ZipCipher.h"
#include "Common/Crc.h"
#include "../../../Common/CRC.h"
namespace NCrypto {
namespace NZip {
inline UINT32 CRC32(UINT32 c, BYTE b)
static inline UInt32 ZipCRC32(UInt32 c, Byte b)
{
return CCRC::Table[(c ^ b) & 0xFF] ^ (c >> 8);
}
void CCipher::UpdateKeys(BYTE b)
void CCipher::UpdateKeys(Byte b)
{
Keys[0] = CRC32(Keys[0], b);
Keys[0] = ZipCRC32(Keys[0], b);
Keys[1] += Keys[0] & 0xff;
Keys[1] = Keys[1] * 134775813L + 1;
Keys[2] = CRC32(Keys[2], Keys[1] >> 24);
Keys[2] = ZipCRC32(Keys[2], Keys[1] >> 24);
}
void CCipher::SetPassword(const BYTE *password, UINT32 passwordLength)
void CCipher::SetPassword(const Byte *password, UInt32 passwordLength)
{
Keys[0] = 305419896L;
Keys[1] = 591751049L;
Keys[2] = 878082192L;
for (UINT32 i = 0; i < passwordLength; i++)
for (UInt32 i = 0; i < passwordLength; i++)
UpdateKeys(password[i]);
}
BYTE CCipher::DecryptByteSpec()
Byte CCipher::DecryptByteSpec()
{
UINT32 temp = Keys[2] | 2;
UInt32 temp = Keys[2] | 2;
return (temp * (temp ^ 1)) >> 8;
}
BYTE CCipher::DecryptByte(BYTE encryptedByte)
Byte CCipher::DecryptByte(Byte encryptedByte)
{
BYTE c = encryptedByte ^ DecryptByteSpec();
Byte c = encryptedByte ^ DecryptByteSpec();
UpdateKeys(c);
return c;
}
BYTE CCipher::EncryptByte(BYTE b)
Byte CCipher::EncryptByte(Byte b)
{
BYTE c = b ^ DecryptByteSpec();
Byte c = b ^ DecryptByteSpec();
UpdateKeys(b);
return c;
}
void CCipher::DecryptHeader(BYTE *buffer)
void CCipher::DecryptHeader(Byte *buffer)
{
for (int i = 0; i < 12; i++)
buffer[i] = DecryptByte(buffer[i]);
}
void CCipher::EncryptHeader(BYTE *buffer)
void CCipher::EncryptHeader(Byte *buffer)
{
for (int i = 0; i < 12; i++)
buffer[i] = EncryptByte(buffer[i]);

View File

@@ -1,7 +1,5 @@
// Crypto/ZipCrypto.h
#pragma once
#ifndef __CRYPTO_ZIP_CRYPTO_H
#define __CRYPTO_ZIP_CRYPTO_H
@@ -11,15 +9,15 @@ namespace NZip {
const int kHeaderSize = 12;
class CCipher
{
UINT32 Keys[3];
void UpdateKeys(BYTE b);
BYTE DecryptByteSpec();
UInt32 Keys[3];
void UpdateKeys(Byte b);
Byte DecryptByteSpec();
public:
void SetPassword(const BYTE *password, UINT32 passwordLength);
BYTE DecryptByte(BYTE encryptedByte);
BYTE EncryptByte(BYTE b);
void DecryptHeader(BYTE *buffer);
void EncryptHeader(BYTE *buffer);
void SetPassword(const Byte *password, UInt32 passwordLength);
Byte DecryptByte(Byte encryptedByte);
Byte EncryptByte(Byte b);
void DecryptHeader(Byte *buffer);
void EncryptHeader(Byte *buffer);
};