diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b095f549..86edd95cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,7 @@ else() endif() endif() -add_definitions(-DXMRIG_MINER_PROJECT) +add_definitions(-DXMRIG_MINER_PROJECT -DXMRIG_JSON_SINGLE_LINE_ARRAY) add_definitions(-D__STDC_FORMAT_MACROS -DUNICODE) find_package(UV REQUIRED) diff --git a/src/base/io/json/Json.cpp b/src/base/io/json/Json.cpp index a9be7dc3e..99a087e9c 100644 --- a/src/base/io/json/Json.cpp +++ b/src/base/io/json/Json.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -29,6 +23,7 @@ #include #include +#include namespace xmrig { @@ -119,6 +114,21 @@ const rapidjson::Value &xmrig::Json::getValue(const rapidjson::Value &obj, const } +double xmrig::Json::getDouble(const rapidjson::Value &obj, const char *key, double defaultValue) +{ + if (isEmpty(obj)) { + return defaultValue; + } + + auto i = obj.FindMember(key); + if (i != obj.MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) { + return i->value.GetDouble(); + } + + return defaultValue; +} + + int xmrig::Json::getInt(const rapidjson::Value &obj, const char *key, int defaultValue) { if (isEmpty(obj)) { @@ -149,6 +159,25 @@ int64_t xmrig::Json::getInt64(const rapidjson::Value &obj, const char *key, int6 } +xmrig::String xmrig::Json::getString(const rapidjson::Value &obj, const char *key, size_t maxSize) +{ + if (isEmpty(obj)) { + return {}; + } + + auto i = obj.FindMember(key); + if (i == obj.MemberEnd() || !i->value.IsString()) { + return {}; + } + + if (maxSize == 0 || i->value.GetStringLength() <= maxSize) { + return i->value.GetString(); + } + + return { i->value.GetString(), maxSize }; +} + + uint64_t xmrig::Json::getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue) { if (isEmpty(obj)) { @@ -222,6 +251,11 @@ bool xmrig::Json::convertOffset(std::istream &ifs, size_t offset, size_t &line, } +xmrig::JsonReader::JsonReader() : + m_obj(kNullValue) +{} + + bool xmrig::JsonReader::isEmpty() const { return Json::isEmpty(m_obj); diff --git a/src/base/io/json/Json.h b/src/base/io/json/Json.h index 683eb308a..e5d0fcc6d 100644 --- a/src/base/io/json/Json.h +++ b/src/base/io/json/Json.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -26,13 +20,10 @@ #define XMRIG_JSON_H -#include "3rdparty/rapidjson/fwd.h" #include "base/kernel/interfaces/IJsonReader.h" #include -#include -#include namespace xmrig { @@ -47,8 +38,10 @@ public: static const rapidjson::Value &getArray(const rapidjson::Value &obj, const char *key); static const rapidjson::Value &getObject(const rapidjson::Value &obj, const char *key); static const rapidjson::Value &getValue(const rapidjson::Value &obj, const char *key); + static double getDouble(const rapidjson::Value &obj, const char *key, double defaultValue = 0); static int getInt(const rapidjson::Value &obj, const char *key, int defaultValue = 0); static int64_t getInt64(const rapidjson::Value &obj, const char *key, int64_t defaultValue = 0); + static String getString(const rapidjson::Value &obj, const char *key, size_t maxSize); static uint64_t getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue = 0); static unsigned getUint(const rapidjson::Value &obj, const char *key, unsigned defaultValue = 0); @@ -66,6 +59,7 @@ private: class JsonReader : public IJsonReader { public: + JsonReader(); inline JsonReader(const rapidjson::Value &obj) : m_obj(obj) {} inline bool getBool(const char *key, bool defaultValue = false) const override { return Json::getBool(m_obj, key, defaultValue); } @@ -73,8 +67,11 @@ public: inline const rapidjson::Value &getArray(const char *key) const override { return Json::getArray(m_obj, key); } inline const rapidjson::Value &getObject(const char *key) const override { return Json::getObject(m_obj, key); } inline const rapidjson::Value &getValue(const char *key) const override { return Json::getValue(m_obj, key); } + inline const rapidjson::Value &object() const override { return m_obj; } + inline double getDouble(const char *key, double defaultValue = 0) const override { return Json::getDouble(m_obj, key, defaultValue); } inline int getInt(const char *key, int defaultValue = 0) const override { return Json::getInt(m_obj, key, defaultValue); } inline int64_t getInt64(const char *key, int64_t defaultValue = 0) const override { return Json::getInt64(m_obj, key, defaultValue); } + inline String getString(const char *key, size_t maxSize) const override { return Json::getString(m_obj, key, maxSize); } inline uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override { return Json::getUint64(m_obj, key, defaultValue); } inline unsigned getUint(const char *key, unsigned defaultValue = 0) const override { return Json::getUint(m_obj, key, defaultValue); } diff --git a/src/base/io/json/JsonChain.cpp b/src/base/io/json/JsonChain.cpp index dff619e8f..0a4a1857d 100644 --- a/src/base/io/json/JsonChain.cpp +++ b/src/base/io/json/JsonChain.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -181,6 +175,28 @@ const rapidjson::Value &xmrig::JsonChain::getValue(const char *key) const } + +const rapidjson::Value &xmrig::JsonChain::object() const +{ + assert(false); + + return m_chain.back(); +} + + +double xmrig::JsonChain::getDouble(const char *key, double defaultValue) const +{ + for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { + auto i = it->FindMember(key); + if (i != it->MemberEnd() && (i->value.IsDouble() || i->value.IsLosslessDouble())) { + return i->value.GetDouble(); + } + } + + return defaultValue; +} + + int xmrig::JsonChain::getInt(const char *key, int defaultValue) const { for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { @@ -207,6 +223,24 @@ int64_t xmrig::JsonChain::getInt64(const char *key, int64_t defaultValue) const } + +xmrig::String xmrig::JsonChain::getString(const char *key, size_t maxSize) const +{ + for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { + auto i = it->FindMember(key); + if (i != it->MemberEnd() && i->value.IsString()) { + if (maxSize == 0 || i->value.GetStringLength() <= maxSize) { + return i->value.GetString(); + } + + return { i->value.GetString(), maxSize }; + } + } + + return {}; +} + + uint64_t xmrig::JsonChain::getUint64(const char *key, uint64_t defaultValue) const { for (auto it = m_chain.rbegin(); it != m_chain.rend(); ++it) { diff --git a/src/base/io/json/JsonChain.h b/src/base/io/json/JsonChain.h index 90f307422..d7fc2f05a 100644 --- a/src/base/io/json/JsonChain.h +++ b/src/base/io/json/JsonChain.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -59,8 +53,11 @@ protected: const rapidjson::Value &getArray(const char *key) const override; const rapidjson::Value &getObject(const char *key) const override; const rapidjson::Value &getValue(const char *key) const override; + const rapidjson::Value &object() const override; + double getDouble(const char *key, double defaultValue = 0) const override; int getInt(const char *key, int defaultValue = 0) const override; int64_t getInt64(const char *key, int64_t defaultValue = 0) const override; + String getString(const char *key, size_t maxSize) const override; uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const override; unsigned getUint(const char *key, unsigned defaultValue = 0) const override; diff --git a/src/base/io/json/JsonRequest.cpp b/src/base/io/json/JsonRequest.cpp index cacbdc809..2bfdda001 100644 --- a/src/base/io/json/JsonRequest.cpp +++ b/src/base/io/json/JsonRequest.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -30,16 +24,33 @@ namespace xmrig { -static const char *k2_0 = "2.0"; -static const char *kId = "id"; -static const char *kJsonRPC = "jsonrpc"; -static const char *kMethod = "method"; -const char *JsonRequest::kParams = "params"; +const char *JsonRequest::k2_0 = "2.0"; +const char *JsonRequest::kId = "id"; +const char *JsonRequest::kJsonRPC = "jsonrpc"; +const char *JsonRequest::kMethod = "method"; +const char *JsonRequest::kOK = "OK"; +const char *JsonRequest::kParams = "params"; +const char *JsonRequest::kResult = "result"; +const char *JsonRequest::kStatus = "status"; + +const char *JsonRequest::kParseError = "parse error"; +const char *JsonRequest::kInvalidRequest = "invalid request"; +const char *JsonRequest::kMethodNotFound = "method not found"; +const char *JsonRequest::kInvalidParams = "invalid params"; +const char *JsonRequest::kInternalError = "internal error"; + +static uint64_t nextId = 0; } // namespace xmrig +rapidjson::Document xmrig::JsonRequest::create(const char *method) +{ + return create(++nextId, method); +} + + rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method) { using namespace rapidjson; @@ -54,7 +65,13 @@ rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method) } -void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms) +uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, const char *method, rapidjson::Value ¶ms) +{ + return create(doc, ++nextId, method, params); +} + + +uint64_t xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms) { using namespace rapidjson; auto &allocator = doc.GetAllocator(); @@ -63,4 +80,6 @@ void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char doc.AddMember(StringRef(kJsonRPC), StringRef(k2_0), allocator); doc.AddMember(StringRef(kMethod), StringRef(method), allocator); doc.AddMember(StringRef(kParams), params, allocator); + + return id; } diff --git a/src/base/io/json/JsonRequest.h b/src/base/io/json/JsonRequest.h index 88dbbad65..21451a43e 100644 --- a/src/base/io/json/JsonRequest.h +++ b/src/base/io/json/JsonRequest.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -35,10 +29,31 @@ namespace xmrig { class JsonRequest { public: + static const char *k2_0; + static const char *kId; + static const char *kJsonRPC; + static const char *kMethod; + static const char *kOK; static const char *kParams; + static const char *kResult; + static const char *kStatus; + static const char *kParseError; + static const char *kInvalidRequest; + static const char *kMethodNotFound; + static const char *kInvalidParams; + static const char *kInternalError; + + constexpr static int kParseErrorCode = -32700; + constexpr static int kInvalidRequestCode = -32600; + constexpr static int kMethodNotFoundCode = -32601; + constexpr static int kInvalidParamsCode = -32602; + constexpr static int kInternalErrorCode = -32603; + + static rapidjson::Document create(const char *method); static rapidjson::Document create(int64_t id, const char *method); - static void create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms); + static uint64_t create(rapidjson::Document &doc, const char *method, rapidjson::Value ¶ms); + static uint64_t create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value ¶ms); }; diff --git a/src/base/io/json/Json_unix.cpp b/src/base/io/json/Json_unix.cpp index 328dda344..0116b81f4 100644 --- a/src/base/io/json/Json_unix.cpp +++ b/src/base/io/json/Json_unix.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -43,7 +37,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) rapidjson::IStreamWrapper isw(ifs); doc.ParseStream(isw); - return !doc.HasParseError() && doc.IsObject(); + return !doc.HasParseError() && (doc.IsObject() || doc.IsArray()); } @@ -56,7 +50,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) rapidjson::OStreamWrapper osw(ofs); rapidjson::PrettyWriter writer(osw); - writer.SetFormatOptions(rapidjson::kFormatSingleLineArray); + +# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY + writer.SetFormatOptions(kFormatSingleLineArray); +# endif doc.Accept(writer); diff --git a/src/base/io/json/Json_win.cpp b/src/base/io/json/Json_win.cpp index 87c79c247..cd7cf5844 100644 --- a/src/base/io/json/Json_win.cpp +++ b/src/base/io/json/Json_win.cpp @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -96,7 +90,7 @@ bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) IStreamWrapper isw(ifs); doc.ParseStream(isw); - return !doc.HasParseError() && doc.IsObject(); + return !doc.HasParseError() && (doc.IsObject() || doc.IsArray()); } @@ -127,7 +121,10 @@ bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) OStreamWrapper osw(ofs); PrettyWriter writer(osw); + +# ifdef XMRIG_JSON_SINGLE_LINE_ARRAY writer.SetFormatOptions(kFormatSingleLineArray); +# endif doc.Accept(writer); diff --git a/src/base/kernel/interfaces/IJsonReader.h b/src/base/kernel/interfaces/IJsonReader.h index b545514db..044a291c0 100644 --- a/src/base/kernel/interfaces/IJsonReader.h +++ b/src/base/kernel/interfaces/IJsonReader.h @@ -1,12 +1,6 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 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 @@ -27,6 +21,8 @@ #include "3rdparty/rapidjson/fwd.h" +#include "base/tools/Object.h" +#include "base/tools/String.h" namespace xmrig { @@ -35,7 +31,10 @@ namespace xmrig { class IJsonReader { public: - virtual ~IJsonReader() = default; + XMRIG_DISABLE_COPY_MOVE(IJsonReader) + + IJsonReader() = default; + virtual ~IJsonReader() = default; virtual bool getBool(const char *key, bool defaultValue = false) const = 0; virtual bool isEmpty() const = 0; @@ -43,8 +42,11 @@ public: virtual const rapidjson::Value &getArray(const char *key) const = 0; virtual const rapidjson::Value &getObject(const char *key) const = 0; virtual const rapidjson::Value &getValue(const char *key) const = 0; + virtual const rapidjson::Value &object() const = 0; + virtual double getDouble(const char *key, double defaultValue = 0) const = 0; virtual int getInt(const char *key, int defaultValue = 0) const = 0; virtual int64_t getInt64(const char *key, int64_t defaultValue = 0) const = 0; + virtual String getString(const char *key, size_t maxSize) const = 0; virtual uint64_t getUint64(const char *key, uint64_t defaultValue = 0) const = 0; virtual unsigned getUint(const char *key, unsigned defaultValue = 0) const = 0; };