diff --git a/CMakeLists.txt b/CMakeLists.txt index f83045890..81ad18f6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ set(HEADERS src/base/net/stratum/SubmitResult.h src/base/net/tools/Storage.h src/base/tools/Arguments.h + src/base/tools/Buffer.h src/base/tools/Chrono.h src/base/tools/Handle.h src/base/tools/String.h @@ -123,6 +124,7 @@ set(SOURCES src/base/net/stratum/strategies/SinglePoolStrategy.cpp src/base/net/stratum/SubmitResult.cpp src/base/tools/Arguments.cpp + src/base/tools/Buffer.cpp src/base/tools/Handle.cpp src/base/tools/String.cpp src/common/config/CommonConfig.cpp diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index ff99c0475..5f3b1f634 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -34,7 +34,7 @@ #include "api/ApiRouter.h" -#include "base/net/stratum/Job.h" +#include "base/tools/Buffer.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" #include "common/cpu/Cpu.h" @@ -174,7 +174,7 @@ void ApiRouter::genId(const char *id) memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, strlen(APP_KIND)); xmrig::keccak(input, inSize, hash); - xmrig::Job::toHex(hash, 8, m_id); + xmrig::Buffer::toHex(hash, 8, m_id); delete [] input; break; diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index cffbe5432..5b330311b 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -39,6 +39,7 @@ #include "base/kernel/interfaces/IClientListener.h" #include "base/net/stratum/Client.h" +#include "base/tools/Buffer.h" #include "base/tools/Chrono.h" #include "common/log/Log.h" #include "net/JobResult.h" @@ -232,10 +233,10 @@ int64_t xmrig::Client::submit(const JobResult &result) char *nonce = m_sendBuf; char *data = m_sendBuf + 16; - Job::toHex(reinterpret_cast(&result.nonce), 4, nonce); + Buffer::toHex(reinterpret_cast(&result.nonce), 4, nonce); nonce[8] = '\0'; - Job::toHex(result.result, 32, data); + Buffer::toHex(result.result, 32, data); data[64] = '\0'; # endif diff --git a/src/base/net/stratum/Job.cpp b/src/base/net/stratum/Job.cpp index c9902f49d..f8239459c 100644 --- a/src/base/net/stratum/Job.cpp +++ b/src/base/net/stratum/Job.cpp @@ -29,33 +29,7 @@ #include "base/net/stratum/Job.h" - - -unsigned char hf_hex2bin(char c, bool &err) -{ - if (c >= '0' && c <= '9') { - return c - '0'; - } - else if (c >= 'a' && c <= 'f') { - return c - 'a' + 0xA; - } - else if (c >= 'A' && c <= 'F') { - return c - 'A' + 0xA; - } - - err = true; - return 0; -} - - -char hf_bin2hex(unsigned char c) -{ - if (c <= 0x9) { - return '0' + c; - } - - return 'a' - 0xA + c; -} +#include "base/tools/Buffer.h" xmrig::Job::Job() : @@ -115,7 +89,7 @@ bool xmrig::Job::setBlob(const char *blob) return false; } - if (!fromHex(blob, (int) m_size * 2, m_blob)) { + if (!Buffer::fromHex(blob, m_size * 2, m_blob)) { return false; } @@ -167,7 +141,7 @@ bool xmrig::Job::setTarget(const char *target) char str[8]; memcpy(str, target, len); - if (!fromHex(str, 8, reinterpret_cast(&tmp)) || tmp == 0) { + if (!Buffer::fromHex(str, 8, reinterpret_cast(&tmp)) || tmp == 0) { return false; } @@ -178,7 +152,7 @@ bool xmrig::Job::setTarget(const char *target) char str[16]; memcpy(str, target, len); - if (!fromHex(str, 16, reinterpret_cast(&m_target)) || m_target == 0) { + if (!Buffer::fromHex(str, 16, reinterpret_cast(&m_target)) || m_target == 0) { return false; } } @@ -212,40 +186,6 @@ void xmrig::Job::setHeight(uint64_t height) } -bool xmrig::Job::fromHex(const char* in, unsigned int len, unsigned char* out) -{ - bool error = false; - for (unsigned int i = 0; i < len; i += 2) { - out[i / 2] = (hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error); - - if (error) { - return false; - } - } - return true; -} - - -void xmrig::Job::toHex(const unsigned char* in, unsigned int len, char* out) -{ - for (unsigned int i = 0; i < len; i++) { - out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4); - out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); - } -} - - -#ifdef APP_DEBUG -char *xmrig::Job::toHex(const unsigned char* in, unsigned int len) -{ - char *out = new char[len * 2 + 1](); - toHex(in, len, out); - - return out; -} -#endif - - xmrig::Variant xmrig::Job::variant() const { switch (m_algorithm.algo()) { diff --git a/src/base/net/stratum/Job.h b/src/base/net/stratum/Job.h index 878274e8c..8fc3d7097 100644 --- a/src/base/net/stratum/Job.h +++ b/src/base/net/stratum/Job.h @@ -84,14 +84,8 @@ public: inline const char *rawTarget() const { return m_rawTarget; } # endif - static bool fromHex(const char* in, unsigned int len, unsigned char* out); static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast(blob + 39); } static inline uint64_t toDiff(uint64_t target) { return 0xFFFFFFFFFFFFFFFFULL / target; } - static void toHex(const unsigned char* in, unsigned int len, char* out); - -# ifdef APP_DEBUG - static char *toHex(const unsigned char* in, unsigned int len); -# endif inline bool operator==(const Job &other) const { return isEqual(other); } inline bool operator!=(const Job &other) const { return !isEqual(other); } diff --git a/src/base/net/stratum/Tls.cpp b/src/base/net/stratum/Tls.cpp index 04aeae92c..e8948f34f 100644 --- a/src/base/net/stratum/Tls.cpp +++ b/src/base/net/stratum/Tls.cpp @@ -29,6 +29,7 @@ #include "base/net/stratum/Client.h" #include "base/net/stratum/Tls.h" +#include "base/tools/Buffer.h" #include "common/log/Log.h" @@ -183,7 +184,7 @@ bool xmrig::Client::Tls::verifyFingerprint(X509 *cert) return false; } - Job::toHex(md, 32, m_fingerprint); + Buffer::toHex(md, 32, m_fingerprint); const char *fingerprint = m_client->m_pool.fingerprint(); return fingerprint == nullptr || strncasecmp(m_fingerprint, fingerprint, 64) == 0; diff --git a/src/base/tools/Buffer.cpp b/src/base/tools/Buffer.cpp new file mode 100644 index 000000000..05a68cb4c --- /dev/null +++ b/src/base/tools/Buffer.cpp @@ -0,0 +1,201 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/tools/Buffer.h" + + +static inline uint8_t hf_hex2bin(uint8_t c, bool &err) +{ + if (c >= '0' && c <= '9') { + return c - '0'; + } + else if (c >= 'a' && c <= 'f') { + return c - 'a' + 0xA; + } + else if (c >= 'A' && c <= 'F') { + return c - 'A' + 0xA; + } + + err = true; + return 0; +} + + +static inline uint8_t hf_bin2hex(uint8_t c) +{ + if (c <= 0x9) { + return '0' + c; + } + + return 'a' - 0xA + c; +} + + +xmrig::Buffer::Buffer() : + m_data(nullptr), + m_size(0) +{ +} + + +xmrig::Buffer::Buffer(Buffer &&other) : + m_data(other.m_data), + m_size(other.m_size) +{ + other.m_data = nullptr; + other.m_size = 0; +} + + +xmrig::Buffer::Buffer(const Buffer &other) +{ + copy(other.data(), other.size()); +} + + +xmrig::Buffer::Buffer(const char *data, size_t size) +{ + copy(data, size); +} + + +xmrig::Buffer::Buffer(size_t size) : + m_size(size) +{ + m_data = new char[size](); +} + + +xmrig::Buffer::~Buffer() +{ + delete [] m_data; +} + + +void xmrig::Buffer::from(const char *data, size_t size) +{ + if (m_size > 0) { + if (m_size == size) { + memcpy(m_data, data, m_size); + + return; + } + + delete [] m_data; + } + + copy(data, size); +} + + +xmrig::Buffer xmrig::Buffer::allocUnsafe(size_t size) +{ + Buffer buf; + buf.m_size = size; + buf.m_data = new char[size]; + + return buf; +} + + +bool xmrig::Buffer::fromHex(const uint8_t *in, size_t size, uint8_t *out) +{ + bool error = false; + for (size_t i = 0; i < size; i += 2) { + out[i / 2] = static_cast((hf_hex2bin(in[i], error) << 4) | hf_hex2bin(in[i + 1], error)); + + if (error) { + return false; + } + } + + return true; +} + + +xmrig::Buffer xmrig::Buffer::fromHex(const char *data, size_t size) +{ + if (data == nullptr || size % 2 != 0) { + return Buffer(); + } + + Buffer buf(size / 2); + fromHex(data, size, buf.data()); + + return buf; +} + + +void xmrig::Buffer::toHex(const uint8_t *in, size_t size, uint8_t *out) +{ + for (size_t i = 0; i < size; i++) { + out[i * 2] = hf_bin2hex((in[i] & 0xF0) >> 4); + out[i * 2 + 1] = hf_bin2hex(in[i] & 0x0F); + } +} + + +xmrig::String xmrig::Buffer::toHex(const uint8_t *in, size_t size) +{ + return Buffer(reinterpret_cast(in), size).toHex(); +} + + +xmrig::String xmrig::Buffer::toHex() const +{ + if (m_size == 0) { + return String(); + } + + char *buf = new char[m_size * 2 + 1]; + buf[m_size * 2] = '\0'; + + toHex(m_data, m_size, buf); + + return String(buf); +} + + +void xmrig::Buffer::copy(const char *data, size_t size) +{ + m_data = new char[size]; + m_size = size; + + memcpy(m_data, data, m_size); +} + + +void xmrig::Buffer::move(Buffer &&other) +{ + if (m_size > 0) { + delete [] m_data; + } + + m_data = other.m_data; + m_size = other.m_size; + + other.m_data = nullptr; + other.m_size = 0; +} diff --git a/src/base/tools/Buffer.h b/src/base/tools/Buffer.h new file mode 100644 index 000000000..6b720357b --- /dev/null +++ b/src/base/tools/Buffer.h @@ -0,0 +1,88 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_BUFFER_H +#define XMRIG_BUFFER_H + + +#include "base/tools/String.h" + + +namespace xmrig { + + +class Buffer +{ +public: + Buffer(); + Buffer(Buffer &&other); + Buffer(const Buffer &other); + Buffer(const char *data, size_t size); + Buffer(size_t size); + ~Buffer(); + + + inline char *data() { return m_data; } + inline const char *data() const { return m_data; } + inline size_t size() const { return m_size; } + inline void from(const Buffer &other) { from(other.data(), other.size()); } + + + void from(const char *data, size_t size); + + + inline Buffer &operator=(const Buffer &other) { from(other); return *this; } + inline Buffer &operator=(Buffer &&other) { move(std::move(other)); return *this; } + + + static Buffer allocUnsafe(size_t size); + static inline Buffer alloc(size_t size) { return Buffer(size); } + + + inline static bool fromHex(const char *in, size_t size, char *out) { return fromHex(reinterpret_cast(in), size, reinterpret_cast(out)); } + inline static bool fromHex(const char *in, size_t size, uint8_t *out) { return fromHex(reinterpret_cast(in), size, out); } + inline static Buffer fromHex(const char *data) { return fromHex(data, strlen(data)); } + inline static Buffer fromHex(const String &str) { return fromHex(str.data(), str.size()); } + inline static void toHex(const char *in, size_t size, char *out) { return toHex(reinterpret_cast(in), size, reinterpret_cast(out)); } + inline static void toHex(const uint8_t *in, size_t size, char *out) { return toHex(in, size, reinterpret_cast(out)); } + + static bool fromHex(const uint8_t *in, size_t size, uint8_t *out); + static Buffer fromHex(const char *data, size_t size); + static String toHex(const uint8_t *in, size_t size); + static void toHex(const uint8_t *in, size_t size, uint8_t *out); + String toHex() const; + +private: + void copy(const char *data, size_t size); + void move(Buffer &&other); + + char *m_data; + size_t m_size; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_BUFFER_H */ diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index f056d498c..ba544c4b6 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -27,6 +27,7 @@ #include "base/net/stratum/Job.h" #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" +#include "base/tools/Buffer.h" #include "common/crypto/keccak.h" #include "common/Platform.h" #include "common/xmrig.h" @@ -51,7 +52,7 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS char userId[65] = { 0 }; keccak(reinterpret_cast(user), strlen(user), hash); - Job::toHex(hash, 32, userId); + Buffer::toHex(hash, 32, userId); # ifndef XMRIG_NO_TLS m_pools.push_back(Pool("donate.ssl.xmrig.com", 443, userId, nullptr, false, true, true));