From a9b90c9a07678bad11d3acc9c98564c4ffcda603 Mon Sep 17 00:00:00 2001 From: Viste Date: Tue, 25 Aug 2020 13:52:22 +0300 Subject: [PATCH] refactor(Core): remove ace_autoptr, cleanup (#3276) --- src/common/Cryptography/BigNumber.cpp | 12 ++++---- src/common/Cryptography/BigNumber.h | 7 +++-- src/common/Cryptography/HMACSHA1.cpp | 12 ++++---- src/common/Cryptography/HMACSHA1.h | 6 ++-- src/common/Cryptography/OpenSSLCrypto.cpp | 36 +++++++++-------------- src/common/Cryptography/OpenSSLCrypto.h | 12 +++++++- src/server/game/Server/WorldSocket.cpp | 3 +- 7 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp index 539378f0e..b72acbbbe 100644 --- a/src/common/Cryptography/BigNumber.cpp +++ b/src/common/Cryptography/BigNumber.cpp @@ -10,7 +10,6 @@ #include #include #include -#include BigNumber::BigNumber() : _bn(BN_new()) @@ -158,23 +157,24 @@ bool BigNumber::isZero() const return BN_is_zero(_bn); } -ACE_Auto_Array_Ptr BigNumber::AsByteArray(int32 minSize, bool littleEndian) +std::unique_ptr BigNumber::AsByteArray(int32 minSize, bool littleEndian) { - int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes(); + int numBytes = GetNumBytes(); + int length = (minSize >= numBytes) ? minSize : numBytes; uint8* array = new uint8[length]; // If we need more bytes than length of BigNumber set the rest to 0 - if (length > GetNumBytes()) + if (length > numBytes) memset((void*)array, 0, length); - BN_bn2bin(_bn, (unsigned char *)array); + BN_bn2bin(_bn, (unsigned char*)array); // openssl's BN stores data internally in big endian format, reverse if little endian desired if (littleEndian) std::reverse(array, array + length); - ACE_Auto_Array_Ptr ret(array); + std::unique_ptr ret(array); return ret; } diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h index f9abda682..5bab4d958 100644 --- a/src/common/Cryptography/BigNumber.h +++ b/src/common/Cryptography/BigNumber.h @@ -8,7 +8,10 @@ #define _AUTH_BIGNUMBER_H #include "Define.h" -#include +#include "Errors.h" +#include +#include +#include struct bignum_st; @@ -75,7 +78,7 @@ class BigNumber uint32 AsDword(); - ACE_Auto_Array_Ptr AsByteArray(int32 minSize = 0, bool littleEndian = true); + std::unique_ptr AsByteArray(int32 minSize = 0, bool littleEndian = true); char * AsHexStr() const; char * AsDecStr() const; diff --git a/src/common/Cryptography/HMACSHA1.cpp b/src/common/Cryptography/HMACSHA1.cpp index 5a973af92..d396a4125 100644 --- a/src/common/Cryptography/HMACSHA1.cpp +++ b/src/common/Cryptography/HMACSHA1.cpp @@ -6,7 +6,8 @@ #include "HMACSHA1.h" #include "BigNumber.h" -#include "Common.h" +#include "Errors.h" +#include #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L HMAC_CTX* HMAC_CTX_new() @@ -23,8 +24,7 @@ void HMAC_CTX_free(HMAC_CTX* ctx) } #endif - -HmacHash::HmacHash(uint32 len, uint8 *seed) +HmacHash::HmacHash(uint32 len, uint8* seed) { m_ctx = HMAC_CTX_new(); HMAC_Init_ex(m_ctx, seed, len, EVP_sha1(), nullptr); @@ -36,14 +36,14 @@ HmacHash::~HmacHash() HMAC_CTX_free(m_ctx); } -void HmacHash::UpdateData(const std::string &str) +void HmacHash::UpdateData(std::string const& str) { HMAC_Update(m_ctx, reinterpret_cast(str.c_str()), str.length()); } -void HmacHash::UpdateData(const uint8* data, size_t len) +void HmacHash::UpdateData(uint8 const* data, size_t len) { - HMAC_Update(m_ctx, data, len); + HMAC_Update(m_ctx, data, len); } void HmacHash::Finalize() diff --git a/src/common/Cryptography/HMACSHA1.h b/src/common/Cryptography/HMACSHA1.h index 51f45ea9a..45e0b219d 100644 --- a/src/common/Cryptography/HMACSHA1.h +++ b/src/common/Cryptography/HMACSHA1.h @@ -19,10 +19,10 @@ class BigNumber; class HmacHash { public: - HmacHash(uint32 len, uint8 *seed); + HmacHash(uint32 len, uint8* seed); ~HmacHash(); - void UpdateData(const std::string &str); - void UpdateData(const uint8* data, size_t len); + void UpdateData(std::string const& str); + void UpdateData(uint8 const* data, size_t len); void Finalize(); uint8* ComputeHash(BigNumber* bn); uint8* GetDigest() { return m_digest; } diff --git a/src/common/Cryptography/OpenSSLCrypto.cpp b/src/common/Cryptography/OpenSSLCrypto.cpp index 97545ceb6..ddd08fa80 100644 --- a/src/common/Cryptography/OpenSSLCrypto.cpp +++ b/src/common/Cryptography/OpenSSLCrypto.cpp @@ -6,52 +6,44 @@ #include #include -#include + +#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x1010000fL #include -#include - -std::vector cryptoLocks; - -static void lockingCallback(int mode, int type, const char* /*file*/, int /*line*/) +#include +#include +std::vector cryptoLocks; +static void lockingCallback(int mode, int type, char const* /*file*/, int /*line*/) { if (mode & CRYPTO_LOCK) - cryptoLocks[type]->acquire(); + cryptoLocks[type]->lock(); else - cryptoLocks[type]->release(); + cryptoLocks[type]->unlock(); } - static void threadIdCallback(CRYPTO_THREADID * id) { (void)id; -/// ACE_thread_t turns out to be a struct under Mac OS. -#ifndef __APPLE__ - CRYPTO_THREADID_set_numeric(id, ACE_Thread::self()); -#else - CRYPTO_THREADID_set_pointer(id, ACE_Thread::self()); -#endif + CRYPTO_THREADID_set_numeric(id, std::hash()(std::this_thread::get_id())); } - void OpenSSLCrypto::threadsSetup() { cryptoLocks.resize(CRYPTO_num_locks()); for(int i = 0 ; i < CRYPTO_num_locks(); ++i) { - cryptoLocks[i] = new ACE_Thread_Mutex(); + cryptoLocks[i] = new std::mutex(); } (void)&threadIdCallback; CRYPTO_THREADID_set_callback(threadIdCallback); - (void)&lockingCallback; CRYPTO_set_locking_callback(lockingCallback); } - void OpenSSLCrypto::threadsCleanup() { - CRYPTO_set_locking_callback(NULL); - CRYPTO_THREADID_set_callback(NULL); + CRYPTO_set_locking_callback(nullptr); + CRYPTO_THREADID_set_callback(nullptr); for(int i = 0 ; i < CRYPTO_num_locks(); ++i) { delete cryptoLocks[i]; } cryptoLocks.resize(0); -} \ No newline at end of file +} +#endif diff --git a/src/common/Cryptography/OpenSSLCrypto.h b/src/common/Cryptography/OpenSSLCrypto.h index cb584612c..c2c782a3f 100644 --- a/src/common/Cryptography/OpenSSLCrypto.h +++ b/src/common/Cryptography/OpenSSLCrypto.h @@ -7,16 +7,26 @@ #ifndef OPENSSL_CRYPTO_H #define OPENSSL_CRYPTO_H +#include "Define.h" +#include + /** * A group of functions which setup openssl crypto module to work properly in multithreaded enviroment * If not setup properly - it will crash */ namespace OpenSSLCrypto { + +#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x1010000fL /// Needs to be called before threads using openssl are spawned void threadsSetup(); /// Needs to be called after threads using openssl are despawned void threadsCleanup(); +#else + void threadsSetup() { }; + void threadsCleanup() { }; +#endif + } -#endif \ No newline at end of file +#endif diff --git a/src/server/game/Server/WorldSocket.cpp b/src/server/game/Server/WorldSocket.cpp index 62bbfe1c9..d8528e8be 100644 --- a/src/server/game/Server/WorldSocket.cpp +++ b/src/server/game/Server/WorldSocket.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include "WorldSocket.h" #include "Common.h" @@ -660,7 +659,7 @@ int WorldSocket::ProcessIncoming(WorldPacket* new_pct) ACE_ASSERT (new_pct); // manage memory ;) - ACE_Auto_Ptr aptr (new_pct); + std::unique_ptr aptr (new_pct); const uint16 opcode = new_pct->GetOpcode();