diff --git a/ethkey/KeyAux.h b/ethkey/KeyAux.h index d2ec13b2a..e4310df30 100644 --- a/ethkey/KeyAux.h +++ b/ethkey/KeyAux.h @@ -221,26 +221,26 @@ public: break; } case OperationMode::ImportBare: - for (string const& i: m_inputs) + for (string const& input: m_inputs) { h128 u; bytes b; - b = fromHex(i); + b = fromHex(input); if (b.size() != 32) { - std::string s = contentsString(i); + std::string s = contentsString(input); b = fromHex(s); if (b.size() != 32) - u = store.importKey(i); + u = store.importKey(input); } if (!u && b.size() == 32) u = store.importSecret(b, lockPassword(toAddress(Secret(b)).abridged())); if (!u) { - cerr << "Cannot import " << i << " not a file or secret." << endl; + cerr << "Cannot import " << input << " not a file or secret." << endl; continue; } - cout << "Successfully imported " << i << " as " << toUUID(u); + cout << "Successfully imported " << input << " as " << toUUID(u); } break; case OperationMode::InspectBare: diff --git a/libdevcore/Common.h b/libdevcore/Common.h index 1ee83c794..c6ed25223 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -113,25 +113,27 @@ static const u256 Invalid256 = ~(u256)0; static const bytes NullBytes; static const std::map EmptyMapU256U256; +/// Interprets @a _u as a two's complement signed number and returns the resulting s256. inline s256 u2s(u256 _u) { - static const bigint c_end = (bigint)1 << 256; - static const u256 c_send = (u256)1 << 255; - if (_u < c_send) - return (s256)_u; - else - return (s256)-(c_end - _u); + static const bigint c_end = bigint(1) << 256; + if (boost::multiprecision::bit_test(_u, 255)) + return s256(-(c_end - _u)); + else + return s256(_u); } +/// @returns the two's complement signed representation of the signed number _u. inline u256 s2u(s256 _u) { - static const bigint c_end = (bigint)1 << 256; + static const bigint c_end = bigint(1) << 256; if (_u >= 0) - return (u256)_u; + return u256(_u); else - return (u256)(c_end + _u); + return u256(c_end + _u); } +/// @returns the smallest n >= 0 such that (1 << n) >= _x inline unsigned int toLog2(u256 _x) { unsigned ret; @@ -139,6 +141,7 @@ inline unsigned int toLog2(u256 _x) return ret; } +/// @returns the absolute distance between _a and _b. template inline N diff(N const& _a, N const& _b) { diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index 2d6333f26..ef178965f 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -93,7 +93,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw) if (h != -1) ret.push_back(h); else if (_throw == WhenError::Throw) - throw BadHexCharacter(); + BOOST_THROW_EXCEPTION(BadHexCharacter()); else return bytes(); } @@ -104,7 +104,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw) if (h != -1 && l != -1) ret.push_back((byte)(h * 16 + l)); else if (_throw == WhenError::Throw) - throw BadHexCharacter(); + BOOST_THROW_EXCEPTION(BadHexCharacter()); else return bytes(); } diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index ddc00e09f..f4d3cf65d 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -68,11 +68,6 @@ int fromHex(char _i, WhenError _throw); /// If _throw = ThrowType::DontThrow, it replaces bad hex characters with 0's, otherwise it will throw an exception. bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow); -#if 0 -std::string toBase58(bytesConstRef _data); -bytes fromBase58(std::string const& _s); -#endif - /// Converts byte array to a string containing the same (binary) data. Unless /// the byte array happens to contain ASCII data, this won't be printable. inline std::string asString(bytes const& _b) diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index 9538ca55f..03b22bfc5 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -64,59 +64,35 @@ string dev::memDump(bytes const& _bytes, unsigned _width, bool _html) return ret.str(); } -// Don't forget to delete[] later. -bytesRef dev::contentsNew(std::string const& _file, bytesRef _dest) +template +inline _T contentsGeneric(std::string const& _file) { + _T ret; + size_t const c_elementSize = sizeof(typename _T::value_type); std::ifstream is(_file, std::ifstream::binary); if (!is) - return bytesRef(); + return ret; + // get length of file: - is.seekg (0, is.end); + is.seekg(0, is.end); streamoff length = is.tellg(); - if (length == 0) // return early, MSVC does not like reading 0 bytes - return bytesRef(); - if (!_dest.empty() && _dest.size() != (unsigned)length) - return bytesRef(); - is.seekg (0, is.beg); - bytesRef ret = _dest.empty() ? bytesRef(new byte[length], length) : _dest; - is.read((char*)ret.data(), length); - is.close(); + if (length == 0) + return ret; // do not read empty file (MSVC does not like it) + is.seekg(0, is.beg); + + ret.resize((length + c_elementSize - 1) / c_elementSize); + is.read(const_cast(reinterpret_cast(ret.data())), length); return ret; } -bytes dev::contents(std::string const& _file) +bytes dev::contents(string const& _file) { - std::ifstream is(_file, std::ifstream::binary); - if (!is) - return bytes(); - // get length of file: - is.seekg (0, is.end); - streamoff length = is.tellg(); - if (length == 0) // return early, MSVC does not like reading 0 bytes - return bytes(); - is.seekg (0, is.beg); - bytes ret(length); - is.read((char*)ret.data(), length); - is.close(); - return ret; + return contentsGeneric(_file); } -string dev::contentsString(std::string const& _file) +string dev::contentsString(string const& _file) { - std::ifstream is(_file, std::ifstream::binary); - if (!is) - return string(); - // get length of file: - is.seekg (0, is.end); - streamoff length = is.tellg(); - if (length == 0) // return early, MSVC does not like reading 0 bytes - return string(); - is.seekg (0, is.beg); - string ret; - ret.resize(length); - is.read((char*)ret.data(), length); - is.close(); - return ret; + return contentsGeneric(_file); } void dev::writeFile(std::string const& _file, bytesConstRef _data) diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h index 46a8b80bc..c8aa830ff 100644 --- a/libdevcore/CommonIO.h +++ b/libdevcore/CommonIO.h @@ -42,10 +42,14 @@ namespace dev { +/// Requests the user to enter a password on the console. std::string getPassword(std::string const& _prompt); -/// Retrieve and returns the contents of the given file. If the file doesn't exist or isn't readable, returns an empty bytes. +/// Retrieve and returns the contents of the given file. +/// If the file doesn't exist or isn't readable, returns an empty container / bytes. bytes contents(std::string const& _file); +/// Retrieve and returns the contents of the given file as a std::string. +/// If the file doesn't exist or isn't readable, returns an empty container / bytes. std::string contentsString(std::string const& _file); /// Retrieve and returns the allocated contents of the given file; if @_dest is given, don't allocate, use it directly. /// If the file doesn't exist or isn't readable, returns bytesRef(). Don't forget to delete [] the returned value's data when finished. diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index 1e7766434..b50e316d3 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -196,7 +196,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) { if (_t.size() != 2) error(); - m_asm.append(CodeFragment::compile(asString(contents(firstAsString())), _s).m_asm); + m_asm.append(CodeFragment::compile(contentsString(firstAsString()), _s).m_asm); } else if (us == "SET") { diff --git a/libtestutils/Common.cpp b/libtestutils/Common.cpp index cff21d464..8f23685c1 100644 --- a/libtestutils/Common.cpp +++ b/libtestutils/Common.cpp @@ -59,7 +59,7 @@ Json::Value dev::test::loadJsonFromFile(std::string const& _path) { Json::Reader reader; Json::Value result; - string s = asString(dev::contents(_path)); + string s = dev::contentsString(_path); if (!s.length()) ctest << "Contents of " + _path + " is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?"; else diff --git a/lllc/main.cpp b/lllc/main.cpp index 1a44ee950..e1a61f49a 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -95,7 +95,7 @@ int main(int argc, char** argv) } } else - src = asString(contents(infile)); + src = contentsString(infile); vector errors; if (src.empty()) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index e65c602ab..ec2a90033 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -401,7 +401,7 @@ bool CommandLineInterface::processInput() continue; } - m_sourceCodes[infile] = asString(dev::contents(infile)); + m_sourceCodes[infile] = dev::contentsString(infile); } m_compiler.reset(new CompilerStack(m_args["add-std"].as())); diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index 733ccb6d0..bca0528ff 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -598,7 +598,7 @@ void userDefinedTest(std::function doTests) { cnote << "Testing user defined test: " << filename; json_spirit::mValue v; - string s = asString(contents(filename)); + string s = contentsString(filename); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + filename + " is empty. "); json_spirit::read_string(s, v); json_spirit::mObject oSingleTest; diff --git a/test/libdevcore/rlp.cpp b/test/libdevcore/rlp.cpp index 41362d569..86780cfa6 100644 --- a/test/libdevcore/rlp.cpp +++ b/test/libdevcore/rlp.cpp @@ -67,7 +67,7 @@ namespace dev string testPath = getTestPath(); testPath += "/BasicTests"; - string s = asString(contents(testPath + "/rlptest.json")); + string s = contentsString(testPath + "/rlptest.json"); BOOST_REQUIRE_MESSAGE( s.length() > 0, "Contents of 'rlptest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); diff --git a/test/libdevcrypto/SecretStore.cpp b/test/libdevcrypto/SecretStore.cpp index 1f927db5d..43c201edc 100644 --- a/test/libdevcrypto/SecretStore.cpp +++ b/test/libdevcrypto/SecretStore.cpp @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(basic_tests) cnote << "Testing Key Store..."; js::mValue v; - string s = asString(contents(testPath + "/basic_tests.json")); + string s = contentsString(testPath + "/basic_tests.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'KeyStoreTests/basic_tests.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) diff --git a/test/libdevcrypto/hexPrefix.cpp b/test/libdevcrypto/hexPrefix.cpp index 53e0d3dbd..4d89ec594 100644 --- a/test/libdevcrypto/hexPrefix.cpp +++ b/test/libdevcrypto/hexPrefix.cpp @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(hexPrefix_test) cnote << "Testing Hex-Prefix-Encode..."; js::mValue v; - string s = asString(contents(testPath + "/hexencodetest.json")); + string s = contentsString(testPath + "/hexencodetest.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content from 'hexencodetest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) diff --git a/test/libdevcrypto/trie.cpp b/test/libdevcrypto/trie.cpp index b5d8662dc..daa3cc181 100644 --- a/test/libdevcrypto/trie.cpp +++ b/test/libdevcrypto/trie.cpp @@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(hex_encoded_securetrie_test) cnote << "Testing Secure Trie..."; js::mValue v; - string s = asString(contents(testPath + "/hex_encoded_securetrie_test.json")); + string s = contentsString(testPath + "/hex_encoded_securetrie_test.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'hex_encoded_securetrie_test.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder) cnote << "Testing Trie..."; js::mValue v; - string s = asString(contents(testPath + "/trieanyorder.json")); + string s = contentsString(testPath + "/trieanyorder.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered) cnote << "Testing Trie..."; js::mValue v; - string s = asString(contents(testPath + "/trietest.json")); + string s = contentsString(testPath + "/trietest.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); diff --git a/test/libethcore/dagger.cpp b/test/libethcore/dagger.cpp index 59770373d..1743882e5 100644 --- a/test/libethcore/dagger.cpp +++ b/test/libethcore/dagger.cpp @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(basic_test) cnote << "Testing Proof of Work..."; js::mValue v; - string s = asString(contents(testPath + "/ethash_tests.json")); + string s = contentsString(testPath + "/ethash_tests.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'ethash_tests.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) diff --git a/test/libethereum/genesis.cpp b/test/libethereum/genesis.cpp index 4633a0617..02ff4517a 100644 --- a/test/libethereum/genesis.cpp +++ b/test/libethereum/genesis.cpp @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(genesis_tests) cnote << "Testing Genesis block..."; js::mValue v; - string s = asString(contents(testPath + "/genesishashestest.json")); + string s = contentsString(testPath + "/genesishashestest.json"); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'genesishashestest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v);