diff --git a/src/abi/abiDecode.cpp b/src/abi/abiDecode.cpp index 8cac9eb..dffb288 100644 --- a/src/abi/abiDecode.cpp +++ b/src/abi/abiDecode.cpp @@ -137,4 +137,55 @@ bool iotex::abi::decode::decodeBool(const char data[64]) { // Bool is encoded as uint8. With the value of 1 for true and the value of 0 for false. return data[63] == '1'; -} \ No newline at end of file +} + +iotex::ResultCode iotex::abi::decode::decodeStaticBytes(const char* pData, size_t bytesSize, uint8_t out[]) +{ + // Bytes are left aliged and padded to the right with 0. + ResultCode res = signer.str2hex(pData, out, bytesSize, bytesSize*2); + return res; +} + +iotex::ResultCode iotex::abi::decode::decodeDynamicBytes(const char* pData, std::vector& out, bool includesHeader) +{ + // Cheack size is enough to contain at least the header/size. + if (strlen(pData) < wordStrLen) { return ResultCode::ERROR_BAD_PARAMETER; } + + const char* pBytesSize = pData; + if (includesHeader) + { + // Cheack size is enough to contain at least the header + offset + if (strlen(pData) < wordStrLen * 2) { return ResultCode::ERROR_BAD_PARAMETER; } + + uint64_t offset = decodeUint64(pData); + pBytesSize += (offset*2); + } + size_t bytesCount = getDynamicArraySize(pBytesSize); + + // Validate the size is enought to contain all the bytes. + size_t minimumSize = wordStrLen; // 1st word - Number of bytes + minimumSize += (bytesCount / 32) * wordStrLen; // Full words + minimumSize += (bytesCount % 32) * 2; + if (strlen(pBytesSize) < minimumSize) { return ResultCode::SUCCESS; } + + // Move the pointer to the data. + const char* pByte = pBytesSize + wordStrLen; + + out.reserve(bytesCount); + for (size_t i=0; i& out, bool includesHeader = true); + + +size_t getDynamicArraySize(const char* pData); iotex::ResultCode decodeUintGeneric(const char* pData, size_t uintSize, uint64_t* out); iotex::ResultCode decodeIntGeneric(const char* pData, size_t uintSize, int64_t* out);