Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

(WIP)cpp-ethereum#4598 1)Switch to C++17 style byte. 2)Fixed all compiler … #4672

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion eth/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ KeyPair AccountManager::makeKey() const
{
bool icap = true;
KeyPair k(Secret::random());
while (icap && k.address()[0])
while (icap && to_integer(k.address()[0]))
k = KeyPair(Secret(sha3(k.secret().ref())));
return k;
}
Expand Down
2 changes: 1 addition & 1 deletion ethkey/KeyAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class KeyCLI
KeyPair makeKey() const
{
KeyPair k(Secret::random());
while (m_icap && k.address()[0])
while (m_icap && to_integer(k.address()[0]))
k = KeyPair(Secret(sha3(k.secret().ref())));
return k;
}
Expand Down
12 changes: 6 additions & 6 deletions ethvm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ int main(int argc, char** argv)

if (inputFile == "-")
for (int i = cin.get(); i != -1; i = cin.get())
code.push_back((char)i);
code.push_back((dev::byte)i);
else
code = contents(inputFile);

Expand Down Expand Up @@ -321,14 +321,14 @@ int main(int argc, char** argv)
executive.setResultRecipient(res);
t.forceSender(sender);

unordered_map<byte, pair<unsigned, bigint>> counts;
unordered_map<unsigned char, pair<unsigned, bigint>> counts;
unsigned total = 0;
bigint memTotal;
auto onOp = [&](uint64_t step, uint64_t PC, Instruction inst, bigint m, bigint gasCost, bigint gas, VM* vm, ExtVMFace const* extVM) {
if (mode == Mode::Statistics)
{
counts[(byte)inst].first++;
counts[(byte)inst].second += gasCost;
counts[(unsigned char)inst].first++;
counts[(unsigned char)inst].second += gasCost;
total++;
if (m > 0)
memTotal = m;
Expand Down Expand Up @@ -370,8 +370,8 @@ int main(int argc, char** argv)
cout << "Maximum memory usage: " << memTotal * 32 << " bytes\n";
cout << "Expensive operations:\n";
for (auto const& c: {Instruction::SSTORE, Instruction::SLOAD, Instruction::CALL, Instruction::CREATE, Instruction::CALLCODE, Instruction::DELEGATECALL, Instruction::MSTORE8, Instruction::MSTORE, Instruction::MLOAD, Instruction::SHA3})
if (!!counts[(byte)c].first)
cout << " " << instructionInfo(c).name << " x " << counts[(byte)c].first << " (" << counts[(byte)c].second << " gas)\n";
if (!!counts[(unsigned char)c].first)
cout << " " << instructionInfo(c).name << " x " << counts[(unsigned char)c].first << " (" << counts[(unsigned char)c].second << " gas)\n";
}
else if (mode == Mode::Trace)
cout << st.json(styledJson);
Expand Down
56 changes: 27 additions & 29 deletions libdevcore/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ using namespace dev;

static inline bool is_base64(byte c)
{
return (isalnum(c) || (c == '+') || (c == '/'));
return (isalnum(to_integer(c)) || (to_integer(c) == '+') || (to_integer(c) == '/'));
}

static inline byte find_base64_char_index(byte c)
{
if ('A' <= c && c <= 'Z') return c - 'A';
else if ('a' <= c && c <= 'z') return c - 'a' + 1 + find_base64_char_index('Z');
else if ('0' <= c && c <= '9') return c - '0' + 1 + find_base64_char_index('z');
else if (c == '+') return 1 + find_base64_char_index('9');
else if (c == '/') return 1 + find_base64_char_index('+');
else return 1 + find_base64_char_index('/');
if ('A' <= to_integer(c) && to_integer(c) <= 'Z') return byte(to_integer(c) - 'A');
else if ('a' <= to_integer(c) && to_integer(c) <= 'z') return byte(to_integer(c) - 'a' + 1 + to_integer(find_base64_char_index((byte)'Z')));
else if ('0' <= to_integer(c) && to_integer(c) <= '9') return byte(to_integer(c) - '0' + 1 + to_integer(find_base64_char_index((byte)'z')));
else if (to_integer(c) == '+') return byte(1 + to_integer(find_base64_char_index((byte)'9')));
else if (to_integer(c) == '/') return byte(1 + to_integer(find_base64_char_index((byte)'+')));
else return (byte)(1 + to_integer(find_base64_char_index((byte)'/')));
}

string dev::toBase64(bytesConstRef _in)
Expand All @@ -67,29 +67,29 @@ string dev::toBase64(bytesConstRef _in)
char_array_3[i++] = *(buf++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
char_array_4[0] = (char_array_3[0] & (byte)0xfc) >> 2;
char_array_4[1] = (byte)(to_integer((char_array_3[0] & (byte)0x03) << 4) + to_integer((char_array_3[1] & (byte)0xf0) >> 4));
char_array_4[2] = (byte)(to_integer((char_array_3[1] & (byte)0x0f) << 2) + to_integer((char_array_3[2] & (byte)0xc0) >> 6));
char_array_4[3] = char_array_3[2] & (byte)0x3f;

for (i = 0; i < 4; i++)
ret += base64_chars[char_array_4[i]];
ret += base64_chars[to_integer(char_array_4[i])];
i = 0;
}
}

if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_3[j] = (byte)('\0');

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
char_array_4[0] = (char_array_3[0] & (byte)0xfc) >> 2;
char_array_4[1] = (byte)(to_integer((char_array_3[0] & (byte)0x03) << 4) + to_integer((char_array_3[1] & (byte)0xf0) >> 4));
char_array_4[2] = (byte)(to_integer((char_array_3[1] & (byte)0x0f) << 2) + to_integer((char_array_3[2] & (byte)0xc0) >> 6));
char_array_4[3] = char_array_3[2] & (byte)0x3f;

for (j = 0; j < i + 1; j++)
ret += base64_chars[char_array_4[j]];
ret += base64_chars[to_integer(char_array_4[j])];

while (i++ < 3)
ret += '=';
Expand All @@ -108,18 +108,17 @@ bytes dev::fromBase64(string const& encoded_string)
byte char_array_4[4];
bytes ret;

while (in_len-- && encoded_string[in_] != '=' && is_base64(encoded_string[in_]))
while (in_len-- && encoded_string[in_] != '=' && is_base64((byte)encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_]; in_++;
char_array_4[i++] = (byte)(encoded_string[in_]); in_++;
if (i == 4)
{
for (i = 0; i < 4; i++)
char_array_4[i] = find_base64_char_index(char_array_4[i]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

char_array_3[0] = (byte)(to_integer(char_array_4[0] << 2) + to_integer((char_array_4[1] & (byte)0x30) >> 4));
char_array_3[1] = (byte)(to_integer((char_array_4[1] & (byte)0xf) << 4) + to_integer((char_array_4[2] & (byte)0x3c) >> 2));
char_array_3[2] = (byte)(to_integer((char_array_4[2] & (byte)0x3) << 6) + to_integer(char_array_4[3]));
for (i = 0; (i < 3); i++)
ret.push_back(char_array_3[i]);
i = 0;
Expand All @@ -129,15 +128,14 @@ bytes dev::fromBase64(string const& encoded_string)
if (i)
{
for (j = i; j < 4; j++)
char_array_4[j] = 0;
char_array_4[j] = (byte)0;

for (j = 0; j < 4; j++)
char_array_4[j] = find_base64_char_index(char_array_4[j]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

char_array_3[0] = (byte)(to_integer(char_array_4[0] << 2) + to_integer((char_array_4[1] & (byte)0x30) >> 4));
char_array_3[1] = (byte)(to_integer((char_array_4[1] & (byte)0xf) << 4) + to_integer((char_array_4[2] & (byte)0x3c) >> 2));
char_array_3[2] = (byte)(to_integer((char_array_4[2] & (byte)0x3) << 6) + to_integer(char_array_4[3]));
for (j = 0; j < i - 1; j++)
ret.push_back(char_array_3[j]);
}
Expand Down
65 changes: 63 additions & 2 deletions libdevcore/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
#pragma GCC diagnostic pop
#include "vector_ref.h"

// CryptoPP defines byte in the global namespace, so must we.
using byte = uint8_t;

// Quote a given token stream to turn it into a string.
#define DEV_QUOTED_HELPER(s) #s
Expand Down Expand Up @@ -200,6 +198,69 @@ inline N diff(N const& _a, N const& _b)
return std::max(_a, _b) - std::min(_a, _b);
}

// template <class IntegerType, class = std::enable_if<std::is_integral<IntegerType>::value>>
inline constexpr uint8_t to_integer(byte b) noexcept
{
return static_cast<uint8_t>(b);
}

template <class IntegerType, class = std::enable_if<std::is_integral<IntegerType>::value>>
inline constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
{
return b = byte(static_cast<unsigned char>(b) << shift);
}

template <class IntegerType, class = std::enable_if<std::is_integral<IntegerType>::value>>
inline constexpr byte operator<<(byte b, IntegerType shift) noexcept
{
return byte(static_cast<unsigned char>(b) << shift);
}

template <class IntegerType, class = std::enable_if<std::is_integral<IntegerType>::value>>
inline constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
{
return b = byte(static_cast<unsigned char>(b) >> shift);
}

template <class IntegerType, class = std::enable_if<std::is_integral<IntegerType>::value>>
inline constexpr byte operator>>(byte b, IntegerType shift) noexcept
{
return byte(static_cast<unsigned char>(b) >> shift);
}

inline constexpr byte& operator|=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
}

inline constexpr byte operator|(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
}

inline constexpr byte& operator&=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
}

inline constexpr byte operator&(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
}

inline constexpr byte& operator^=(byte& l, byte r) noexcept
{
return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
}

inline constexpr byte operator^(byte l, byte r) noexcept
{
return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
}

inline constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }


/// RAII utility class whose destructor calls a given function.
class ScopeGuard
{
Expand Down
10 changes: 5 additions & 5 deletions libdevcore/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ std::string dev::randomWord()
bytes dev::fromHex(std::string const& _s, WhenError _throw)
{
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
std::vector<uint8_t> ret;
std::vector<byte> ret;
ret.reserve((_s.size() - s + 1) / 2);

if (_s.size() % 2)
{
int h = fromHexChar(_s[s++]);
if (h != -1)
ret.push_back(h);
ret.push_back((byte)h);
else if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter());
else
Expand All @@ -122,12 +122,12 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)

bytes dev::asNibbles(bytesConstRef const& _s)
{
std::vector<uint8_t> ret;
std::vector<byte> ret;
ret.reserve(_s.size() * 2);
for (auto i: _s)
{
ret.push_back(i / 16);
ret.push_back(i % 16);
ret.push_back((byte)(to_integer(i) / 16));
ret.push_back((byte)(to_integer(i) % 16));
}
return ret;
}
Expand Down
10 changes: 5 additions & 5 deletions libdevcore/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ std::string toHex(Iterator _it, Iterator _end, std::string const& _prefix)
hex.replace(0, off, _prefix);
for (; _it != _end; _it++)
{
hex[off++] = hexdigits[(*_it >> 4) & 0x0f];
hex[off++] = hexdigits[*_it & 0x0f];
hex[off++] = hexdigits[to_integer((byte)(*_it >> 4) & (byte)0x0f)];
hex[off++] = hexdigits[to_integer((byte)*_it & (byte)0x0f)];
}
return hex;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ inline T fromBigEndian(_In const& _bytes)
{
T ret = (T)0;
for (auto i: _bytes)
ret = (T)((ret << 8) | (byte)(typename std::make_unsigned<decltype(i)>::type)i);
ret = (T)((ret << 8) | (typename std::make_unsigned<decltype(i)>::type)i);
return ret;
}

Expand All @@ -157,13 +157,13 @@ inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
int i = 0;
for (T v = _val; v; ++i, v >>= 8) {}
bytes ret(std::max<unsigned>(_min, i), 0);
bytes ret(std::max<unsigned>(_min, i), (byte)0);
toBigEndian(_val, ret);
return ret;
}
inline bytes toCompactBigEndian(byte _val, unsigned _min = 0)
{
return (_min || _val) ? bytes{ _val } : bytes{};
return (_min || to_integer(_val)) ? bytes{ _val } : bytes{};
}

/// Convenience function for toBigEndian.
Expand Down
8 changes: 4 additions & 4 deletions libdevcore/CommonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ string dev::memDump(bytes const& _bytes, unsigned _width, bool _html)
ret << hex << setw(4) << setfill('0') << i << " ";
for (unsigned j = i; j < i + _width; ++j)
if (j < _bytes.size())
if (_bytes[j] >= 32 && _bytes[j] < 127)
if ((char)_bytes[j] == '<' && _html)
if (to_integer(_bytes[j]) >= 32 && to_integer(_bytes[j]) < 127)
if (to_integer(_bytes[j]) == '<' && _html)
ret << "&lt;";
else if ((char)_bytes[j] == '&' && _html)
else if (to_integer(_bytes[j]) == '&' && _html)
ret << "&amp;";
else
ret << (char)_bytes[j];
ret << to_integer(_bytes[j]);
else
ret << '?';
else
Expand Down
2 changes: 1 addition & 1 deletion libdevcore/CommonJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bytes jsToBytes(string const& _s, OnFailed _f)
bytes padded(bytes _b, unsigned _l)
{
while (_b.size() < _l)
_b.insert(_b.begin(), 0);
_b.insert(_b.begin(), (byte)0);
return asBytes(asString(_b).substr(_b.size() - max(_l, _l)));
}

Expand Down