diff --git a/CMakeLists.txt b/CMakeLists.txt index 75cd05d..2ac831f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ include (CheckIncludeFile) set(HEADERS src/3rdparty/align.h src/App.h + src/base/tools/String.h src/common/config/CommonConfig.h src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h @@ -89,6 +90,7 @@ set(HEADERS set(SOURCES src/App.cpp + src/base/tools/String.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp new file mode 100644 index 0000000..adad51e --- /dev/null +++ b/src/base/tools/String.cpp @@ -0,0 +1,42 @@ +/* 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 2016-2018 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/String.h" +#include "rapidjson/document.h" + + +rapidjson::Value xmrig::String::toJSON() const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(StringRef(m_data)); +} + + +rapidjson::Value xmrig::String::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(m_data, doc.GetAllocator()); +} diff --git a/src/base/tools/String.h b/src/base/tools/String.h new file mode 100644 index 0000000..d5f0e84 --- /dev/null +++ b/src/base/tools/String.h @@ -0,0 +1,104 @@ +/* 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 2016-2018 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_STRING_H +#define XMRIG_STRING_H + + +#include +#include +#include + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +/** + * @brief Simple C string wrapper. + * + * 1. I know about std:string. + * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. + * 3. nullptr and JSON conversion supported. + */ +class String +{ +public: + inline String() : m_data(nullptr) {} + inline String(char *str) : m_data(str) {} + inline String(const char *str) : m_data(nullptr) { set(str); } + inline String(const String &other) : m_data(nullptr) { set(other.data()); } + inline String(String &&other) : m_data(other.m_data) { other.m_data = nullptr; } + inline ~String() { free(m_data); } + + + inline bool isEqual(const char *str) const { return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); } + inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; } + + + inline bool isEmpty() const { return size() == 0; } + inline bool isNull() const { return m_data == nullptr; } + inline const char *data() const { return m_data; } + inline size_t size() const { return isNull() ? 0 : strlen(m_data); } + + + inline bool operator!=(const char *str) const { return !isEqual(str); } + inline bool operator!=(const String &str) const { return !isEqual(str.data()); } + inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; } + inline bool operator==(const char *str) const { return isEqual(str); } + inline bool operator==(const String &str) const { return isEqual(str.data()); } + inline String &operator=(char *str) { set(str); return *this; } + inline String &operator=(const char *str) { set(str); return *this; } + inline String &operator=(const String &str) { set(str.data()); return *this; } + inline String &operator=(String &&str) { m_data = str.m_data; str.m_data = nullptr; return *this; } + + rapidjson::Value toJSON() const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + inline void set(const char *str) + { + free(m_data); + + m_data = str != nullptr ? strdup(str) : nullptr; + } + + + inline void set(char *str) + { + free(m_data); + + m_data = str; + } + + + char *m_data; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_STRING_H */ diff --git a/src/common/utils/c_str.h b/src/common/utils/c_str.h index 7ce6375..fe0164b 100644 --- a/src/common/utils/c_str.h +++ b/src/common/utils/c_str.h @@ -21,82 +21,19 @@ * along with this program. If not, see . */ -#ifndef __C_STR_H__ -#define __C_STR_H__ +#ifndef XMRIG_C_STR_H +#define XMRIG_C_STR_H -#include -#include - -#include +#include "base/tools/String.h" namespace xmrig { -/** - * @brief Simple C string wrapper. - * - * 1. I know about std:string. - * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. - */ -class c_str -{ -public: - inline c_str() : m_data(nullptr) {} - inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; } - inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); } - inline c_str(const char *str) : m_data(nullptr) { set(str); } - inline ~c_str() { free(m_data); } - - - inline void set(const char *str) - { - free(m_data); - - m_data = str != nullptr ? strdup(str) : nullptr; - } - - - inline void set(char *str) - { - free(m_data); - - m_data = str; - } - - - inline bool isEqual(const char *str) const - { - return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); - } - - - inline bool contains(const char *str) const - { - return strstr(m_data, str) != nullptr; - } - - - inline bool isNull() const { return m_data == nullptr; } - inline const char *data() const { return m_data; } - inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); } - - - inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); } - inline bool operator!=(const char *str) const { return !isEqual(str); } - inline bool operator==(const c_str &str) const { return isEqual(str.data()); } - inline bool operator==(const char *str) const { return isEqual(str); } - inline c_str &operator=(char *str) { set(str); return *this; } - inline c_str &operator=(const c_str &str) { set(str.data()); return *this; } - inline c_str &operator=(const char *str) { set(str); return *this; } - - -private: - char *m_data; -}; +typedef String c_str; } /* namespace xmrig */ -#endif /* __C_STR_H__ */ +#endif /* XMRIG_C_STR_H */ diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index a12a022..2ad9a7b 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -51,8 +51,8 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value obj(kObjectType); - obj.AddMember("cert", cert() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); - obj.AddMember("key", key() ? Value(StringRef(key())).Move() : Value(kNullType).Move(), allocator); + obj.AddMember("cert", m_cert.toJSON(), allocator); + obj.AddMember("key", m_key.toJSON(), allocator); return obj; } diff --git a/src/proxy/tls/TlsConfig.h b/src/proxy/tls/TlsConfig.h index f1e8143..6a12a2b 100644 --- a/src/proxy/tls/TlsConfig.h +++ b/src/proxy/tls/TlsConfig.h @@ -26,7 +26,7 @@ #define XMRIG_TLSCONFIG_H -#include "common/utils/c_str.h" +#include "base/tools/String.h" #include "rapidjson/fwd.h" @@ -40,7 +40,7 @@ public: TlsConfig(const rapidjson::Value &object); ~TlsConfig(); - inline bool isValid() const { return !m_cert.isNull() && !m_key.isNull(); } + inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); } inline const char *cert() const { return m_cert.data(); } inline const char *key() const { return m_key.data(); } inline void setCert(const char *cert) { m_cert = cert; } @@ -49,8 +49,8 @@ public: rapidjson::Value toJSON(rapidjson::Document &doc) const; private: - c_str m_cert; - c_str m_key; + String m_cert; + String m_key; };