2019-07-03 19:37:55 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-07-03 19:37:55 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
|
|
|
|
2019-08-14 19:05:26 +00:00
|
|
|
#include <test/libsolidity/util/ContractABIUtils.h>
|
2019-07-21 13:58:12 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
#include <liblangutil/Common.h>
|
|
|
|
|
2020-07-13 18:05:34 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/StringUtils.h>
|
2019-08-07 10:15:10 +00:00
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
2019-07-10 15:11:03 +00:00
|
|
|
#include <iomanip>
|
2019-07-03 19:37:55 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <regex>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace solidity;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2019-07-03 19:37:55 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
bytes BytesUtils::alignLeft(bytes _bytes)
|
|
|
|
{
|
|
|
|
soltestAssert(_bytes.size() <= 32, "");
|
|
|
|
size_t size = _bytes.size();
|
|
|
|
return std::move(_bytes) + bytes(32 - size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes BytesUtils::alignRight(bytes _bytes)
|
|
|
|
{
|
|
|
|
soltestAssert(_bytes.size() <= 32, "");
|
|
|
|
return bytes(32 - _bytes.size(), 0) + std::move(_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes BytesUtils::applyAlign(
|
|
|
|
Parameter::Alignment _alignment,
|
|
|
|
ABIType& _abiType,
|
|
|
|
bytes _bytes
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (_alignment != Parameter::Alignment::None)
|
|
|
|
_abiType.alignDeclared = true;
|
|
|
|
|
|
|
|
switch (_alignment)
|
|
|
|
{
|
|
|
|
case Parameter::Alignment::Left:
|
|
|
|
_abiType.align = ABIType::AlignLeft;
|
|
|
|
return alignLeft(std::move(_bytes));
|
|
|
|
case Parameter::Alignment::Right:
|
|
|
|
default:
|
|
|
|
_abiType.align = ABIType::AlignRight;
|
|
|
|
return alignRight(std::move(_bytes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
bytes BytesUtils::convertBoolean(string const& _literal)
|
|
|
|
{
|
|
|
|
if (_literal == "true")
|
|
|
|
return bytes{true};
|
|
|
|
else if (_literal == "false")
|
|
|
|
return bytes{false};
|
|
|
|
else
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Boolean literal invalid."));
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes BytesUtils::convertNumber(string const& _literal)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return toCompactBigEndian(u256{_literal});
|
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Number encoding invalid."));
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes BytesUtils::convertHexNumber(string const& _literal)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-07-12 11:58:31 +00:00
|
|
|
return fromHex(_literal);
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("Hex number encoding invalid."));
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes BytesUtils::convertString(string const& _literal)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return asBytes(_literal);
|
|
|
|
}
|
|
|
|
catch (std::exception const&)
|
|
|
|
{
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(TestParserError("String encoding invalid."));
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
string BytesUtils::formatUnsigned(bytes const& _bytes)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
soltestAssert(!_bytes.empty() && _bytes.size() <= 32, "");
|
2019-07-03 19:37:55 +00:00
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
return fromBigEndian<u256>(_bytes).str();
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
string BytesUtils::formatSigned(bytes const& _bytes)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
soltestAssert(!_bytes.empty() && _bytes.size() <= 32, "");
|
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
if (*_bytes.begin() & 0x80)
|
|
|
|
os << u2s(fromBigEndian<u256>(_bytes));
|
|
|
|
else
|
|
|
|
os << fromBigEndian<u256>(_bytes);
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
string BytesUtils::formatBoolean(bytes const& _bytes)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
u256 result = fromBigEndian<u256>(_bytes);
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
os << "false";
|
|
|
|
else if (result == 1)
|
|
|
|
os << "true";
|
|
|
|
else
|
|
|
|
os << result;
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:18:16 +00:00
|
|
|
string BytesUtils::formatHex(bytes const& _bytes, bool _shorten)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
2019-07-12 11:58:31 +00:00
|
|
|
soltestAssert(!_bytes.empty() && _bytes.size() <= 32, "");
|
2019-08-07 10:15:10 +00:00
|
|
|
u256 value = fromBigEndian<u256>(_bytes);
|
2020-01-15 17:18:16 +00:00
|
|
|
string output = toCompactHexWithPrefix(value);
|
2019-07-12 11:58:31 +00:00
|
|
|
|
2020-01-15 17:18:16 +00:00
|
|
|
if (_shorten)
|
|
|
|
return output.substr(0, output.size() - countRightPaddedZeros(_bytes) * 2);
|
|
|
|
return output;
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 13:58:12 +00:00
|
|
|
string BytesUtils::formatHexString(bytes const& _bytes)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
|
|
|
os << "hex\"" << toHex(_bytes) << "\"";
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2019-07-21 14:58:48 +00:00
|
|
|
string BytesUtils::formatString(bytes const& _bytes, size_t _cutOff)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
|
|
|
os << "\"";
|
2019-07-10 15:11:03 +00:00
|
|
|
for (size_t i = 0; i < min(_cutOff, _bytes.size()); ++i)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
2019-07-10 15:11:03 +00:00
|
|
|
auto const v = _bytes[i];
|
|
|
|
switch (v)
|
2019-07-03 19:37:55 +00:00
|
|
|
{
|
2019-07-10 15:11:03 +00:00
|
|
|
case '\0':
|
|
|
|
os << "\\0";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
os << "\\n";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint(v))
|
|
|
|
os << v;
|
|
|
|
else
|
2020-07-13 18:05:34 +00:00
|
|
|
os << "\\x" << toHex(v);
|
2019-07-03 19:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
os << "\"";
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
2019-07-21 14:15:02 +00:00
|
|
|
|
2019-08-14 19:05:26 +00:00
|
|
|
string BytesUtils::formatRawBytes(
|
|
|
|
bytes const& _bytes,
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::test::ParameterList const& _parameters,
|
2019-08-14 19:05:26 +00:00
|
|
|
string _linePrefix)
|
2019-07-21 14:15:02 +00:00
|
|
|
{
|
|
|
|
stringstream os;
|
2019-09-11 13:45:53 +00:00
|
|
|
ParameterList parameters;
|
2019-07-21 14:15:02 +00:00
|
|
|
auto it = _bytes.begin();
|
2019-08-07 09:24:02 +00:00
|
|
|
|
2019-09-11 13:45:53 +00:00
|
|
|
if (_bytes.size() != ContractABIUtils::encodingSize(_parameters))
|
2020-10-13 10:55:46 +00:00
|
|
|
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
|
2019-09-11 13:45:53 +00:00
|
|
|
else
|
|
|
|
parameters = _parameters;
|
|
|
|
|
|
|
|
for (auto const& parameter: parameters)
|
2019-07-21 14:15:02 +00:00
|
|
|
{
|
2019-08-14 19:05:26 +00:00
|
|
|
bytes byteRange{it, it + static_cast<long>(parameter.abiType.size)};
|
2019-07-21 14:15:02 +00:00
|
|
|
|
2020-02-26 20:37:52 +00:00
|
|
|
os << _linePrefix << byteRange;
|
2019-09-11 13:45:53 +00:00
|
|
|
if (¶meter != ¶meters.back())
|
2019-07-21 14:15:02 +00:00
|
|
|
os << endl;
|
2019-08-14 19:05:26 +00:00
|
|
|
|
|
|
|
it += static_cast<long>(parameter.abiType.size);
|
2019-07-21 14:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
2019-07-21 14:58:48 +00:00
|
|
|
|
|
|
|
string BytesUtils::formatBytes(
|
|
|
|
bytes const& _bytes,
|
|
|
|
ABIType const& _abiType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
|
|
|
switch (_abiType.type)
|
|
|
|
{
|
|
|
|
case ABIType::UnsignedDec:
|
|
|
|
// Check if the detected type was wrong and if this could
|
|
|
|
// be signed. If an unsigned was detected in the expectations,
|
|
|
|
// but the actual result returned a signed, it would be formatted
|
|
|
|
// incorrectly.
|
|
|
|
if (*_bytes.begin() & 0x80)
|
|
|
|
os << formatSigned(_bytes);
|
|
|
|
else
|
2020-05-19 04:50:19 +00:00
|
|
|
{
|
|
|
|
std::string decimal(formatUnsigned(_bytes));
|
|
|
|
std::string hexadecimal(formatHex(_bytes));
|
|
|
|
unsigned int value = u256(_bytes).convert_to<unsigned int>();
|
|
|
|
if (value < 0x10)
|
|
|
|
os << decimal;
|
|
|
|
else if (value >= 0x10 && value <= 0xff) {
|
|
|
|
os << hexadecimal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto entropy = [](std::string const& str) -> double {
|
|
|
|
double result = 0;
|
2019-12-12 23:39:29 +00:00
|
|
|
map<char, double> frequencies;
|
2020-05-19 04:50:19 +00:00
|
|
|
for (char c: str)
|
|
|
|
frequencies[c]++;
|
|
|
|
for (auto p: frequencies)
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
double freq = p.second / double(str.length());
|
|
|
|
result -= freq * (log(freq) / log(2.0));
|
2020-05-19 04:50:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
if (entropy(decimal) < entropy(hexadecimal.substr(2, hexadecimal.length())))
|
|
|
|
os << decimal;
|
|
|
|
else
|
|
|
|
os << hexadecimal;
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 14:58:48 +00:00
|
|
|
break;
|
|
|
|
case ABIType::SignedDec:
|
|
|
|
os << formatSigned(_bytes);
|
|
|
|
break;
|
|
|
|
case ABIType::Boolean:
|
|
|
|
os << formatBoolean(_bytes);
|
|
|
|
break;
|
|
|
|
case ABIType::Hex:
|
2020-01-15 17:18:16 +00:00
|
|
|
os << formatHex(_bytes, _abiType.alignDeclared);
|
2019-07-21 14:58:48 +00:00
|
|
|
break;
|
|
|
|
case ABIType::HexString:
|
|
|
|
os << formatHexString(_bytes);
|
|
|
|
break;
|
|
|
|
case ABIType::String:
|
2019-10-15 10:49:28 +00:00
|
|
|
os << formatString(_bytes, _bytes.size() - countRightPaddedZeros(_bytes));
|
2019-07-21 14:58:48 +00:00
|
|
|
break;
|
|
|
|
case ABIType::Failure:
|
|
|
|
break;
|
|
|
|
case ABIType::None:
|
|
|
|
break;
|
|
|
|
}
|
2020-01-15 17:18:16 +00:00
|
|
|
|
|
|
|
if (_abiType.alignDeclared)
|
|
|
|
return (_abiType.align == ABIType::AlignLeft ? "left(" : "right(") + os.str() + ")";
|
2019-07-21 14:58:48 +00:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
string BytesUtils::formatBytesRange(
|
|
|
|
bytes _bytes,
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::test::ParameterList const& _parameters,
|
2019-07-21 14:58:48 +00:00
|
|
|
bool _highlight
|
|
|
|
)
|
|
|
|
{
|
|
|
|
stringstream os;
|
2019-09-11 13:45:53 +00:00
|
|
|
ParameterList parameters;
|
2019-07-21 14:58:48 +00:00
|
|
|
auto it = _bytes.begin();
|
|
|
|
|
2019-09-11 13:45:53 +00:00
|
|
|
if (_bytes.size() != ContractABIUtils::encodingSize(_parameters))
|
2020-10-13 10:55:46 +00:00
|
|
|
parameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
|
2019-09-11 13:45:53 +00:00
|
|
|
else
|
|
|
|
parameters = _parameters;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto const& parameter: parameters)
|
2019-07-21 14:58:48 +00:00
|
|
|
{
|
|
|
|
bytes byteRange{it, it + static_cast<long>(parameter.abiType.size)};
|
|
|
|
|
|
|
|
if (!parameter.matchesBytes(byteRange))
|
|
|
|
AnsiColorized(
|
|
|
|
os,
|
|
|
|
_highlight,
|
2019-12-23 15:50:30 +00:00
|
|
|
{util::formatting::RED_BACKGROUND}
|
2019-07-21 14:58:48 +00:00
|
|
|
) << formatBytes(byteRange, parameter.abiType);
|
|
|
|
else
|
|
|
|
os << parameter.rawString;
|
|
|
|
|
2019-09-11 13:45:53 +00:00
|
|
|
if (¶meter != ¶meters.back())
|
2019-07-21 14:58:48 +00:00
|
|
|
os << ", ";
|
2019-08-07 09:24:02 +00:00
|
|
|
|
|
|
|
it += static_cast<long>(parameter.abiType.size);
|
2019-07-21 14:58:48 +00:00
|
|
|
}
|
2019-08-07 09:24:02 +00:00
|
|
|
|
2019-07-21 14:58:48 +00:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2019-10-15 10:49:28 +00:00
|
|
|
size_t BytesUtils::countRightPaddedZeros(bytes const& _bytes)
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
return static_cast<size_t>(find_if(
|
2019-10-15 10:49:28 +00:00
|
|
|
_bytes.rbegin(),
|
|
|
|
_bytes.rend(),
|
|
|
|
[](uint8_t b) { return b != '\0'; }
|
2020-06-02 13:42:46 +00:00
|
|
|
) - _bytes.rbegin());
|
2019-10-15 10:49:28 +00:00
|
|
|
}
|
|
|
|
|