From 0898fda3dbec4dea2c2488d00c9a8a357282453a Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Tue, 2 Jul 2019 02:13:42 +0200 Subject: [PATCH] Fixed warnings related to libtomcrypt's byte definition --- CMakeLists.txt | 1 + src/converters/converter.h | 3 ++- src/misc/base64.cpp | 28 +++++++++++++++++++++++++--- src/misc/base64.h | 24 ++---------------------- src/misc/digest.cpp | 21 +++++++++++++++++++++ src/misc/digest.h | 16 +++++----------- src/protocol/CryptionHandler.h | 3 ++- src/query/Command.h | 1 + 8 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 src/misc/digest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 036ed2c..59a8fbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ set(SOURCE_FILES src/misc/time.cpp src/misc/memtracker.cpp src/misc/base64.cpp + src/misc/digest.cpp #Logger src/log/LogUtils.cpp diff --git a/src/converters/converter.h b/src/converters/converter.h index 2c196a1..b85e461 100644 --- a/src/converters/converter.h +++ b/src/converters/converter.h @@ -81,4 +81,5 @@ namespace ts { return ((class(*)(const std::string&)) ts::converter::from_string)(val); \ }; \ }; \ -} \ No newline at end of file +} +/* DO NOT REMOVE ME (NL warning) */ \ No newline at end of file diff --git a/src/misc/base64.cpp b/src/misc/base64.cpp index d70e954..5d80e2b 100644 --- a/src/misc/base64.cpp +++ b/src/misc/base64.cpp @@ -1,4 +1,26 @@ -// -// Created by wolverindev on 02.07.19. -// +#include "./base64.h" +#include + +std::string base64::decode(const char* input, size_t size) { + auto out = new unsigned char[size]; + if(base64_strict_decode((unsigned char*) input, size, out, (unsigned long*) &size) != CRYPT_OK){ + std::cerr << "Invalid base 64 string '" << input << "'" << std::endl; + return ""; + } + std::string ret((char*) out, size); + delete[] out; + return ret; +} + +std::string base64::encode(const char* input, const unsigned long inputSize) { + auto outlen = static_cast(inputSize + (inputSize / 3.0) + 16); + auto outbuf = new unsigned char[outlen]; //Reserve output memory + if(base64_encode((unsigned char*) input, inputSize, outbuf, &outlen) != CRYPT_OK){ + std::cerr << "Invalid input '" << input << "'" << std::endl; + return ""; + } + std::string ret((char*) outbuf, outlen); + delete[] outbuf; + return ret; +} \ No newline at end of file diff --git a/src/misc/base64.h b/src/misc/base64.h index 53b6a62..241883d 100644 --- a/src/misc/base64.h +++ b/src/misc/base64.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include namespace base64 { @@ -11,17 +10,7 @@ namespace base64 { * @param inputSize The size of the input to decode * @return A Base64-encoded version of the encoded string */ - inline std::string encode(const char* input, const unsigned long inputSize) { - auto outlen = static_cast(inputSize + (inputSize / 3.0) + 16); - auto outbuf = new unsigned char[outlen]; //Reserve output memory - if(base64_encode((unsigned char*) input, inputSize, outbuf, &outlen) != CRYPT_OK){ - std::cerr << "Invalid input '" << input << "'" << std::endl; - return ""; - } - std::string ret((char*) outbuf, outlen); - delete[] outbuf; - return ret; - } + extern std::string encode(const char* input, const unsigned long inputSize); /** * Encodes a given string in Base64 @@ -36,16 +25,7 @@ namespace base64 { * @param input The input string to decode * @return A string (binary) that represents the Base64-decoded data of the input */ - inline std::string decode(const char* input, size_t size) { - auto out = new unsigned char[size]; - if(base64_strict_decode((unsigned char*) input, size, out, (unsigned long*) &size) != CRYPT_OK){ - std::cerr << "Invalid base 64 string '" << input << "'" << std::endl; - return ""; - } - std::string ret((char*) out, size); - delete[] out; - return ret; - } + extern std::string decode(const char* input, size_t size); /** * Decodes a Base64-encoded string. diff --git a/src/misc/digest.cpp b/src/misc/digest.cpp new file mode 100644 index 0000000..879b962 --- /dev/null +++ b/src/misc/digest.cpp @@ -0,0 +1,21 @@ +#include "./digest.h" +#ifdef NO_OPEN_SSL + #include + + #define DECLARE_DIGEST(name, _unused_, digestLength) \ + std::string digest::tomcrypt::name(const std::string& input) { \ + hash_state hash{}; \ + \ + uint8_t buffer[digestLength]; \ + \ + name ##_init(&hash); \ + name ##_process(&hash, (uint8_t*) input.data(), input.length()); \ + name ##_done(&hash, buffer); \ + \ + return std::string((const char*) buffer, digestLength); \ + } + + DECLARE_DIGEST(sha1, SHA1, SHA_DIGEST_LENGTH) + DECLARE_DIGEST(sha256, SHA256, SHA256_DIGEST_LENGTH) + DECLARE_DIGEST(sha512, SHA512, SHA512_DIGEST_LENGTH) +#endif \ No newline at end of file diff --git a/src/misc/digest.h b/src/misc/digest.h index 5441e56..83971fb 100644 --- a/src/misc/digest.h +++ b/src/misc/digest.h @@ -4,24 +4,18 @@ #include #include +#define NO_OPEN_SSL #ifdef NO_OPEN_SSL - #include - #define SHA_DIGEST_LENGTH 20 #define SHA256_DIGEST_LENGTH 32 #define SHA512_DIGEST_LENGTH 64 #define DECLARE_DIGEST(name, _unused_, digestLength) \ + namespace tomcrypt { \ + extern std::string name(const std::string&); \ + } \ inline std::string name(const std::string& input) { \ - hash_state hash{}; \ - \ - uint8_t buffer[digestLength]; \ - \ - name ##_init(&hash); \ - name ##_process(&hash, (uint8_t*) input.data(), input.length()); \ - name ##_done(&hash, buffer); \ - \ - return std::string((const char*) buffer, digestLength); \ + return tomcrypt::name(input); \ } \ \ inline std::string name(const char* input, int64_t length = -1) { \ diff --git a/src/protocol/CryptionHandler.h b/src/protocol/CryptionHandler.h index a19af23..07fbcb0 100644 --- a/src/protocol/CryptionHandler.h +++ b/src/protocol/CryptionHandler.h @@ -2,8 +2,9 @@ #include #include -#include #include "Packet.h" +#include +#undef byte /* the macro byte gets defined by tomcrypt_macros. We have to undefine it */ namespace ts { namespace connection { diff --git a/src/query/Command.h b/src/query/Command.h index 0947312..9921d07 100644 --- a/src/query/Command.h +++ b/src/query/Command.h @@ -1,6 +1,7 @@ #pragma once #ifdef byte + #define byte asdd #ifndef WIN32 #warning The byte macro is already defined! Undefining it! #endif