diff --git a/revision.txt b/revision.txt index f5c8955..8f92bfd 100644 --- a/revision.txt +++ b/revision.txt @@ -1 +1 @@ -32 +35 diff --git a/src/RpcDecoder.cpp b/src/RpcDecoder.cpp index 800f6fe..7cbe742 100644 --- a/src/RpcDecoder.cpp +++ b/src/RpcDecoder.cpp @@ -29,6 +29,7 @@ */ #include "RpcDecoder.h" +#include "Math.h" namespace Flows { @@ -163,143 +164,186 @@ VariableType RpcDecoder::decodeType(std::vector& packet, uint32_t& posi std::shared_ptr RpcDecoder::decodeParameter(std::vector& packet, uint32_t& position) { - VariableType type = decodeType(packet, position); - std::shared_ptr variable = std::make_shared(type); - if(variable->type == VariableType::tVoid) - { - //Nothing - } - else if(type == VariableType::tString || type == VariableType::tBase64) - { - variable->stringValue = _decoder->decodeString(packet, position); - } - else if(type == VariableType::tInteger) - { - variable->integerValue = _decoder->decodeInteger(packet, position); - variable->integerValue64 = variable->integerValue; - } - else if(type == VariableType::tInteger64) - { - variable->integerValue64 = _decoder->decodeInteger64(packet, position); - variable->integerValue = (int32_t)variable->integerValue64; - } - else if(type == VariableType::tFloat) - { - variable->floatValue = _decoder->decodeFloat(packet, position); - } - else if(type == VariableType::tBoolean) - { - variable->booleanValue = _decoder->decodeBoolean(packet, position); - } - else if(type == VariableType::tBinary) - { - variable->binaryValue = _decoder->decodeBinary(packet, position); - } - else if(type == VariableType::tArray) - { - variable->arrayValue = decodeArray(packet, position); - } - else if(type == VariableType::tStruct) - { - variable->structValue = decodeStruct(packet, position); - if(variable->structValue->size() == 2 && variable->structValue->find("faultCode") != variable->structValue->end() && variable->structValue->find("faultString") != variable->structValue->end()) - { - variable->errorStruct = true; - } - } - return variable; + VariableType type = decodeType(packet, position); + std::shared_ptr variable = std::make_shared(type); + if(type == VariableType::tVoid) + { + //Nothing + } + else if(type == VariableType::tString || type == VariableType::tBase64) + { + variable->stringValue = _decoder->decodeString(packet, position); + variable->integerValue64 = Math::getNumber64(variable->stringValue); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = !variable->stringValue.empty() && variable->stringValue != "0" && variable->stringValue != "false" && variable->stringValue != "f"; + + } + else if(type == VariableType::tInteger) + { + variable->integerValue = _decoder->decodeInteger(packet, position); + variable->integerValue64 = variable->integerValue; + variable->booleanValue = (bool)variable->integerValue; + variable->floatValue = variable->integerValue; + } + else if(type == VariableType::tInteger64) + { + variable->integerValue64 = _decoder->decodeInteger64(packet, position); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = (bool)variable->integerValue64; + variable->floatValue = variable->integerValue64; + } + else if(type == VariableType::tFloat) + { + variable->floatValue = _decoder->decodeFloat(packet, position); + variable->integerValue = (int32_t)std::lround(variable->floatValue); + variable->integerValue64 = std::llround(variable->floatValue); + variable->booleanValue = (bool)variable->floatValue; + } + else if(type == VariableType::tBoolean) + { + variable->booleanValue = _decoder->decodeBoolean(packet, position); + variable->integerValue = (int32_t)variable->booleanValue; + variable->integerValue64 = (int64_t)variable->booleanValue; + } + else if(type == VariableType::tBinary) + { + variable->binaryValue = _decoder->decodeBinary(packet, position); + } + else if(type == VariableType::tArray) + { + variable->arrayValue = decodeArray(packet, position); + } + else if(type == VariableType::tStruct) + { + variable->structValue = decodeStruct(packet, position); + if(variable->structValue->size() == 2 && variable->structValue->find("faultCode") != variable->structValue->end() && variable->structValue->find("faultString") != variable->structValue->end()) + { + variable->errorStruct = true; + } + } + return variable; } std::shared_ptr RpcDecoder::decodeParameter(std::vector& packet, uint32_t& position) { - VariableType type = decodeType(packet, position); - std::shared_ptr variable = std::make_shared(type); - if(variable->type == VariableType::tVoid) - { - //Nothing - } - else if(type == VariableType::tString || type == VariableType::tBase64) - { - variable->stringValue = _decoder->decodeString(packet, position); - } - else if(type == VariableType::tInteger) - { - variable->integerValue = _decoder->decodeInteger(packet, position); - variable->integerValue64 = variable->integerValue; - } - else if(type == VariableType::tInteger64) - { - variable->integerValue64 = _decoder->decodeInteger64(packet, position); - variable->integerValue = (int32_t)variable->integerValue64; - } - else if(type == VariableType::tFloat) - { - variable->floatValue = _decoder->decodeFloat(packet, position); - } - else if(type == VariableType::tBoolean) - { - variable->booleanValue = _decoder->decodeBoolean(packet, position); - } - else if(type == VariableType::tBinary) - { - variable->binaryValue = _decoder->decodeBinary(packet, position); - } - else if(type == VariableType::tArray) - { - variable->arrayValue = decodeArray(packet, position); - } - else if(type == VariableType::tStruct) - { - variable->structValue = decodeStruct(packet, position); - if(variable->structValue->size() == 2 && variable->structValue->find("faultCode") != variable->structValue->end() && variable->structValue->find("faultString") != variable->structValue->end()) - { - variable->errorStruct = true; - } - } - return variable; + VariableType type = decodeType(packet, position); + std::shared_ptr variable = std::make_shared(type); + if(type == VariableType::tVoid) + { + //Nothing + } + else if(type == VariableType::tString || type == VariableType::tBase64) + { + variable->stringValue = _decoder->decodeString(packet, position); + variable->integerValue64 = Math::getNumber64(variable->stringValue); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = !variable->stringValue.empty() && variable->stringValue != "0" && variable->stringValue != "false" && variable->stringValue != "f"; + + } + else if(type == VariableType::tInteger) + { + variable->integerValue = _decoder->decodeInteger(packet, position); + variable->integerValue64 = variable->integerValue; + variable->booleanValue = (bool)variable->integerValue; + variable->floatValue = variable->integerValue; + } + else if(type == VariableType::tInteger64) + { + variable->integerValue64 = _decoder->decodeInteger64(packet, position); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = (bool)variable->integerValue64; + variable->floatValue = variable->integerValue64; + } + else if(type == VariableType::tFloat) + { + variable->floatValue = _decoder->decodeFloat(packet, position); + variable->integerValue = (int32_t)std::lround(variable->floatValue); + variable->integerValue64 = std::llround(variable->floatValue); + variable->booleanValue = (bool)variable->floatValue; + } + else if(type == VariableType::tBoolean) + { + variable->booleanValue = _decoder->decodeBoolean(packet, position); + variable->integerValue = (int32_t)variable->booleanValue; + variable->integerValue64 = (int64_t)variable->booleanValue; + } + else if(type == VariableType::tBinary) + { + variable->binaryValue = _decoder->decodeBinary(packet, position); + } + else if(type == VariableType::tArray) + { + variable->arrayValue = decodeArray(packet, position); + } + else if(type == VariableType::tStruct) + { + variable->structValue = decodeStruct(packet, position); + if(variable->structValue->size() == 2 && variable->structValue->find("faultCode") != variable->structValue->end() && variable->structValue->find("faultString") != variable->structValue->end()) + { + variable->errorStruct = true; + } + } + return variable; } void RpcDecoder::decodeParameter(PVariable& variable, uint32_t& position) { - variable->type = decodeType(variable->binaryValue, position); - if(variable->type == VariableType::tVoid) - { - //Nothing - } - else if(variable->type == VariableType::tString || variable->type == VariableType::tBase64) - { - variable->stringValue = _decoder->decodeString(variable->binaryValue, position); - } - else if(variable->type == VariableType::tInteger) - { - variable->integerValue = _decoder->decodeInteger(variable->binaryValue, position); - variable->integerValue64 = variable->integerValue; - } - else if(variable->type == VariableType::tInteger64) - { - variable->integerValue64 = _decoder->decodeInteger64(variable->binaryValue, position); - variable->integerValue = (int32_t)variable->integerValue64; - } - else if(variable->type == VariableType::tFloat) - { - variable->floatValue = _decoder->decodeFloat(variable->binaryValue, position); - } - else if(variable->type == VariableType::tBoolean) - { - variable->booleanValue = _decoder->decodeBoolean(variable->binaryValue, position); - } - else if(variable->type == VariableType::tBinary) - { - variable->binaryValue = _decoder->decodeBinary(variable->binaryValue, position); - } - else if(variable->type == VariableType::tArray) - { - variable->arrayValue = decodeArray(variable->binaryValue, position); - } - else if(variable->type == VariableType::tStruct) - { - variable->structValue = decodeStruct(variable->binaryValue, position); - } + variable->type = decodeType(variable->binaryValue, position); + if(variable->type == VariableType::tVoid) + { + //Nothing + } + else if(variable->type == VariableType::tString || variable->type == VariableType::tBase64) + { + variable->stringValue = _decoder->decodeString(variable->binaryValue, position); + variable->integerValue64 = Math::getNumber64(variable->stringValue); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = !variable->stringValue.empty() && variable->stringValue != "0" && variable->stringValue != "false" && variable->stringValue != "f"; + + } + else if(variable->type == VariableType::tInteger) + { + variable->integerValue = _decoder->decodeInteger(variable->binaryValue, position); + variable->integerValue64 = variable->integerValue; + variable->booleanValue = (bool)variable->integerValue; + variable->floatValue = variable->integerValue; + } + else if(variable->type == VariableType::tInteger64) + { + variable->integerValue64 = _decoder->decodeInteger64(variable->binaryValue, position); + variable->integerValue = (int32_t)variable->integerValue64; + variable->booleanValue = (bool)variable->integerValue64; + variable->floatValue = variable->integerValue64; + } + else if(variable->type == VariableType::tFloat) + { + variable->floatValue = _decoder->decodeFloat(variable->binaryValue, position); + variable->integerValue = (int32_t)std::lround(variable->floatValue); + variable->integerValue64 = std::llround(variable->floatValue); + variable->booleanValue = (bool)variable->floatValue; + } + else if(variable->type == VariableType::tBoolean) + { + variable->booleanValue = _decoder->decodeBoolean(variable->binaryValue, position); + variable->integerValue = (int32_t)variable->booleanValue; + variable->integerValue64 = (int64_t)variable->booleanValue; + } + else if(variable->type == VariableType::tBinary) + { + variable->binaryValue = _decoder->decodeBinary(variable->binaryValue, position); + } + else if(variable->type == VariableType::tArray) + { + variable->arrayValue = decodeArray(variable->binaryValue, position); + } + else if(variable->type == VariableType::tStruct) + { + variable->structValue = decodeStruct(variable->binaryValue, position); + if(variable->structValue->size() == 2 && variable->structValue->find("faultCode") != variable->structValue->end() && variable->structValue->find("faultString") != variable->structValue->end()) + { + variable->errorStruct = true; + } + } } PArray RpcDecoder::decodeArray(std::vector& packet, uint32_t& position) diff --git a/src/Variable.cpp b/src/Variable.cpp index 60c332d..fe9af3c 100755 --- a/src/Variable.cpp +++ b/src/Variable.cpp @@ -30,21 +30,16 @@ #include "Variable.h" #include "HelperFunctions.h" +#include "Math.h" namespace Flows { -Variable::~Variable() -{ -} - -std::shared_ptr Variable::createError(int32_t faultCode, std::string faultString) +Variable::Variable() { - std::shared_ptr error = std::make_shared(VariableType::tStruct); - error->errorStruct = true; - error->structValue->insert(StructElement("faultCode", std::make_shared(faultCode))); - error->structValue->insert(StructElement("faultString", std::make_shared(faultString))); - return error; + type = VariableType::tVoid; + arrayValue = std::make_shared(); + structValue = std::make_shared(); } Variable::Variable(Variable const& rhs) @@ -71,6 +66,146 @@ Variable::Variable(Variable const& rhs) } } +Variable::Variable(VariableType variableType) : Variable() +{ + type = variableType; + if(type == VariableType::tVariant) type = VariableType::tVoid; +} + +Variable::Variable(uint8_t integer) : Variable() +{ + type = VariableType::tInteger; + integerValue = (int32_t)integer; + integerValue64 = (int64_t)integer; + floatValue = (double)integer; + booleanValue = (bool)integer; +} + +Variable::Variable(int32_t integer) : Variable() +{ + type = VariableType::tInteger; + integerValue = (int32_t)integer; + integerValue64 = (int64_t)integer; + floatValue = (double)integer; + booleanValue = (bool)integer; +} + +Variable::Variable(uint32_t integer): Variable() +{ + type = VariableType::tInteger; + integerValue = (int32_t)integer; + integerValue64 = (int64_t)integer; + floatValue = (double)integer; + booleanValue = (bool)integer; +} + +Variable::Variable(int64_t integer) : Variable() +{ + type = VariableType::tInteger64; + integerValue = (int32_t)integer; + integerValue64 = (int64_t)integer; + floatValue = (double)integer; + booleanValue = (bool)integer; +} + +Variable::Variable(uint64_t integer) : Variable() +{ + type = VariableType::tInteger64; + integerValue = (int32_t)integer; + integerValue64 = (int64_t)integer; + floatValue = (double)integer; + booleanValue = (bool)integer; +} + +Variable::Variable(const std::string& string) : Variable() +{ + type = VariableType::tString; + stringValue = string; + integerValue64 = Math::getNumber64(stringValue); + integerValue = (int32_t)integerValue64; + booleanValue = !stringValue.empty() && stringValue != "0" && stringValue != "false" && stringValue != "f"; +} + +Variable::Variable(const char* string) : Variable(std::string(string)) +{ +} + +Variable::Variable(bool boolean) : Variable() +{ + type = VariableType::tBoolean; + booleanValue = boolean; + integerValue = booleanValue; + integerValue64 = booleanValue; +} + +Variable::Variable(double floatVal) : Variable() +{ + type = VariableType::tFloat; + floatValue = floatVal; + integerValue = (int32_t)std::lround(floatVal); + integerValue64 = std::llround(floatVal); + booleanValue = (bool)floatVal; +} + +Variable::Variable(const PArray& arrayVal) : Variable() +{ + type = VariableType::tArray; + arrayValue = arrayVal; +} + +Variable::Variable(const std::vector& arrayVal) : Variable() +{ + type = VariableType::tArray; + arrayValue->reserve(arrayVal.size()); + for(auto& element : arrayVal) + { + arrayValue->push_back(std::make_shared(element)); + } +} + +Variable::Variable(const PStruct& structVal) : Variable() +{ + type = VariableType::tStruct; + structValue = structVal; +} + +Variable::Variable(const std::vector& binaryVal) : Variable() +{ + type = VariableType::tBinary; + binaryValue = binaryVal; +} + +Variable::Variable(const uint8_t* binaryVal, size_t binaryValSize) : Variable() +{ + type = VariableType::tBinary; + binaryValue = std::vector(binaryVal, binaryVal + binaryValSize); +} + +Variable::Variable(const std::vector& binaryVal) : Variable() +{ + type = VariableType::tBinary; binaryValue.clear(); + binaryValue.insert(binaryValue.end(), binaryVal.begin(), binaryVal.end()); +} + +Variable::Variable(const char* binaryVal, size_t binaryValSize) : Variable() +{ + type = VariableType::tBinary; + binaryValue = std::vector(binaryVal, binaryVal + binaryValSize); +} + +Variable::~Variable() +{ +} + +std::shared_ptr Variable::createError(int32_t faultCode, std::string faultString) +{ + std::shared_ptr error = std::make_shared(VariableType::tStruct); + error->errorStruct = true; + error->structValue->insert(StructElement("faultCode", std::make_shared(faultCode))); + error->structValue->insert(StructElement("faultString", std::make_shared(faultString))); + return error; +} + Variable& Variable::operator=(const Variable& rhs) { if(&rhs == this) return *this; @@ -229,7 +364,7 @@ Variable::operator Variable::bool_type() const result = !arrayValue->empty(); break; case VariableType::tBase64: - result = !stringValue.empty() && stringValue != "0"; + result = !stringValue.empty(); break; case VariableType::tBinary: result = !binaryValue.empty(); @@ -246,7 +381,7 @@ Variable::operator Variable::bool_type() const result = (bool)integerValue64; break; case VariableType::tString: - result = !stringValue.empty() && stringValue != "0"; + result = !stringValue.empty() && stringValue != "0" && stringValue != "false" && stringValue != "f"; break; case VariableType::tStruct: result = !structValue->empty(); diff --git a/src/Variable.h b/src/Variable.h index e1f3373..8335150 100755 --- a/src/Variable.h +++ b/src/Variable.h @@ -37,6 +37,7 @@ #include #include #include +#include namespace Flows { @@ -90,23 +91,25 @@ class Variable PStruct structValue; std::vector binaryValue; - Variable() { type = VariableType::tVoid; arrayValue = PArray(new Array()); structValue = PStruct(new Struct()); } + Variable(); Variable(Variable const& rhs); - Variable(VariableType variableType) : Variable() { type = variableType; if(type == VariableType::tVariant) type = VariableType::tVoid; } - Variable(uint8_t integer) : Variable() { type = VariableType::tInteger; integerValue = (int32_t)integer; integerValue64 = (int64_t)integer; } - Variable(int32_t integer) : Variable() { type = VariableType::tInteger; integerValue = (int32_t)integer; integerValue64 = (int64_t)integer; } - Variable(uint32_t integer) : Variable() { type = VariableType::tInteger; integerValue = (int32_t)integer; integerValue64 = (int64_t)integer; } - Variable(int64_t integer) : Variable() { type = VariableType::tInteger64; integerValue = (int32_t)integer; integerValue64 = (int64_t)integer; } - Variable(uint64_t integer) : Variable() { type = VariableType::tInteger64; integerValue = (int32_t)integer; integerValue64 = (int64_t)integer; } - Variable(std::string string) : Variable() { type = VariableType::tString; stringValue = string; } - Variable(const char* string) : Variable() { type = VariableType::tString; stringValue = std::string(string); } - Variable(bool boolean) : Variable() { type = VariableType::tBoolean; booleanValue = boolean; } - Variable(double floatVal) : Variable() { type = VariableType::tFloat; floatValue = floatVal; } - Variable(PArray arrayVal) : Variable() { type = VariableType::tArray; arrayValue = arrayVal; } - Variable(std::vector& arrayVal) : Variable() { type = VariableType::tArray; arrayValue->reserve(arrayVal.size()); for(std::vector::iterator i = arrayVal.begin(); i != arrayVal.end(); ++i) arrayValue->push_back(PVariable(new Variable(*i))); } - Variable(PStruct structVal) : Variable() { type = VariableType::tStruct; structValue = structVal; } - Variable(std::vector& binaryVal) : Variable() { type = VariableType::tBinary; binaryValue = binaryVal; } - Variable(std::vector& binaryVal) : Variable() { type = VariableType::tBinary; binaryValue.clear(); binaryValue.insert(binaryValue.end(), binaryVal.begin(), binaryVal.end()); } + explicit Variable(VariableType variableType); + explicit Variable(uint8_t integer); + explicit Variable(int32_t integer); + explicit Variable(uint32_t integer); + explicit Variable(int64_t integer); + explicit Variable(uint64_t integer); + explicit Variable(const std::string& string); + explicit Variable(const char* string); + explicit Variable(bool boolean); + explicit Variable(double floatVal); + explicit Variable(const PArray& arrayVal); + explicit Variable(const std::vector& arrayVal); + explicit Variable(const PStruct& structVal); + explicit Variable(const std::vector& binaryVal); + explicit Variable(const uint8_t* binaryVal, size_t binaryValSize); + explicit Variable(const std::vector& binaryVal); + explicit Variable(const char* binaryVal, size_t binaryValSize); virtual ~Variable(); static PVariable createError(int32_t faultCode, std::string faultString); std::string print(bool stdout = false, bool stderr = false, bool oneLine = false);