From be3b47d0d504a3409ce66bd77bb8c0acff87c4f5 Mon Sep 17 00:00:00 2001 From: kh1 Date: Thu, 15 Mar 2012 14:53:47 +0100 Subject: Reorganize the tree, have better ifw.pri. Shadow build support. Change-Id: I01fb12537f863ed0744979973c7e4153889cc5cb Reviewed-by: Tim Jenssen --- src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp (limited to 'src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp') diff --git a/src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp b/src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp new file mode 100644 index 000000000..baaaf98e3 --- /dev/null +++ b/src/libs/7zip/unix/CPP/7zip/Crypto/ZipCrypto.cpp @@ -0,0 +1,88 @@ +// Crypto/ZipCrypto.cpp + +#include "StdAfx.h" + +#include "../../../C/7zCrc.h" + +#include "../Common/StreamUtils.h" + +#include "RandGen.h" +#include "ZipCrypto.h" + +namespace NCrypto { +namespace NZip { + +void CCipher::UpdateKeys(Byte b) +{ + Keys[0] = CRC_UPDATE_BYTE(Keys[0], b); + Keys[1] = (Keys[1] + (Keys[0] & 0xFF)) * 0x8088405 + 1; + Keys[2] = CRC_UPDATE_BYTE(Keys[2], (Byte)(Keys[1] >> 24)); +} + +STDMETHODIMP CCipher::CryptoSetPassword(const Byte *password, UInt32 passwordLen) +{ + Keys[0] = 0x12345678; + Keys[1] = 0x23456789; + Keys[2] = 0x34567890; + UInt32 i; + for (i = 0; i < passwordLen; i++) + UpdateKeys(password[i]); + for (i = 0; i < 3; i++) + Keys2[i] = Keys[i]; + return S_OK; +} + +STDMETHODIMP CCipher::Init() +{ + return S_OK; +} + +Byte CCipher::DecryptByteSpec() +{ + UInt32 temp = Keys[2] | 2; + return (Byte)((temp * (temp ^ 1)) >> 8); +} + +HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream, UInt32 crc) +{ + Byte h[kHeaderSize]; + g_RandomGenerator.Generate(h, kHeaderSize - 2); + h[kHeaderSize - 1] = (Byte)(crc >> 24); + h[kHeaderSize - 2] = (Byte)(crc >> 16); + RestoreKeys(); + Filter(h, kHeaderSize); + return WriteStream(outStream, h, kHeaderSize); +} + +STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size) +{ + for (UInt32 i = 0; i < size; i++) + { + Byte b = data[i]; + data[i] = (Byte)(b ^ DecryptByteSpec());; + UpdateKeys(b); + } + return size; +} + +HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) +{ + Byte h[kHeaderSize]; + RINOK(ReadStream_FAIL(inStream, h, kHeaderSize)); + RestoreKeys(); + Filter(h, kHeaderSize); + return S_OK; +} + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + for (UInt32 i = 0; i < size; i++) + { + Byte c = (Byte)(data[i] ^ DecryptByteSpec()); + UpdateKeys(c); + data[i] = c; + } + return size; +} + +}} -- cgit v1.2.3