diff --git a/Builds/CMake/RippledCore.cmake b/Builds/CMake/RippledCore.cmake index 40b1df199..461de2f86 100644 --- a/Builds/CMake/RippledCore.cmake +++ b/Builds/CMake/RippledCore.cmake @@ -272,15 +272,6 @@ install ( DESTINATION include/ripple/beast/core) install ( FILES - src/ripple/beast/crypto/detail/mac_facade.h - src/ripple/beast/crypto/detail/ripemd_context.h - src/ripple/beast/crypto/detail/sha2_context.h - src/ripple/beast/crypto/ripemd.h - src/ripple/beast/crypto/sha2.h - DESTINATION include/ripple/beast/crypto) -install ( - FILES - src/ripple/beast/hash/endian.h src/ripple/beast/hash/hash_append.h src/ripple/beast/hash/meta.h src/ripple/beast/hash/uhash.h @@ -891,7 +882,6 @@ target_sources (rippled PRIVATE src/test/protocol/Seed_test.cpp src/test/protocol/SeqProxy_test.cpp src/test/protocol/TER_test.cpp - src/test/protocol/digest_test.cpp src/test/protocol/types_test.cpp #[===============================[ test sources: diff --git a/Builds/CMake/RippledInterface.cmake b/Builds/CMake/RippledInterface.cmake index e0c0c1e5c..28a531246 100644 --- a/Builds/CMake/RippledInterface.cmake +++ b/Builds/CMake/RippledInterface.cmake @@ -16,9 +16,6 @@ target_compile_definitions (opts BOOST_BEAST_ALLOW_DEPRECATED BOOST_FILESYSTEM_DEPRECATED > - $<$: - USE_BEAST_HASHER - > $<$:BEAST_NO_UNIT_TEST_INLINE=1> $<$:BEAST_DONT_AUTOLINK_TO_WIN32_LIBRARIES=1> $<$:RIPPLE_SINGLE_IO_SERVICE_THREAD=1>) diff --git a/bin/ci/test.sh b/bin/ci/test.sh index 6162f37aa..11615d732 100755 --- a/bin/ci/test.sh +++ b/bin/ci/test.sh @@ -24,7 +24,6 @@ declare -a manual_tests=( 'ripple.consensus.ByzantineFailureSim' 'ripple.consensus.DistributedValidators' 'ripple.consensus.ScaleFreeSim' - 'ripple.ripple_data.digest' 'ripple.tx.CrossingLimits' 'ripple.tx.FindOversizeCross' 'ripple.tx.Offer_manual' diff --git a/bin/ci/ubuntu/build-and-test.sh b/bin/ci/ubuntu/build-and-test.sh index ea83e7686..1f9da09b8 100755 --- a/bin/ci/ubuntu/build-and-test.sh +++ b/bin/ci/ubuntu/build-and-test.sh @@ -137,7 +137,6 @@ else # descending execution time (longest running tests at top) declare -a manual_tests=( 'ripple.ripple_data.reduce_relay_simulate' - 'ripple.ripple_data.digest' 'ripple.tx.Offer_manual' 'ripple.tx.CrossingLimits' 'ripple.tx.PlumpBook' diff --git a/src/ripple/app/consensus/RCLCxPeerPos.h b/src/ripple/app/consensus/RCLCxPeerPos.h index 83456a101..9d448aac4 100644 --- a/src/ripple/app/consensus/RCLCxPeerPos.h +++ b/src/ripple/app/consensus/RCLCxPeerPos.h @@ -113,12 +113,6 @@ private: Slice const& signature, uint256 const& suppress, Proposal&& proposal); - - static char const* - getCountedObjectName() - { - return "RCLCxPeerPos::Data"; - } }; std::shared_ptr data_; diff --git a/src/ripple/app/ledger/AcceptedLedgerTx.cpp b/src/ripple/app/ledger/AcceptedLedgerTx.cpp index 4c0150d4a..c92ebffe0 100644 --- a/src/ripple/app/ledger/AcceptedLedgerTx.cpp +++ b/src/ripple/app/ledger/AcceptedLedgerTx.cpp @@ -74,7 +74,7 @@ std::string AcceptedLedgerTx::getEscMeta() const { assert(!mRawMeta.empty()); - return sqlEscape(mRawMeta); + return sqlBlobLiteral(mRawMeta); } void diff --git a/src/ripple/app/ledger/InboundLedger.h b/src/ripple/app/ledger/InboundLedger.h index 6688d0dc3..4ab66b6a6 100644 --- a/src/ripple/app/ledger/InboundLedger.h +++ b/src/ripple/app/ledger/InboundLedger.h @@ -38,12 +38,6 @@ class InboundLedger final : public PeerSet, public: using clock_type = beast::abstract_clock; - static char const* - getCountedObjectName() - { - return "InboundLedger"; - } - using PeerDataPairType = std::pair, std::shared_ptr>; diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 15b4583c7..e52aebeb4 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -1173,13 +1173,13 @@ loadLedgerHelper(std::string const& sqlSuffix, Application& app, bool acquire) uint256 prevHash{}, accountHash{}, transHash{}; if (sLedgerHash) - ledgerHash.SetHexExact(*sLedgerHash); + (void)ledgerHash.parseHex(*sLedgerHash); if (sPrevHash) - prevHash.SetHexExact(*sPrevHash); + (void)prevHash.parseHex(*sPrevHash); if (sAccountHash) - accountHash.SetHexExact(*sAccountHash); + (void)accountHash.parseHex(*sAccountHash); if (sTransHash) - transHash.SetHexExact(*sTransHash); + (void)transHash.parseHex(*sTransHash); using time_point = NetClock::time_point; using duration = NetClock::duration; @@ -1266,7 +1266,7 @@ getHashByIndex(std::uint32_t ledgerIndex, Application& app) std::string sql = "SELECT LedgerHash FROM Ledgers INDEXED BY SeqLedger WHERE LedgerSeq='"; - sql.append(beast::lexicalCastThrow(ledgerIndex)); + sql.append(std::to_string(ledgerIndex)); sql.append("';"); std::string hash; @@ -1284,7 +1284,7 @@ getHashByIndex(std::uint32_t ledgerIndex, Application& app) return ret; } - ret.SetHexExact(hash); + (void)ret.parseHex(hash); return ret; } @@ -1310,10 +1310,7 @@ getHashesByIndex( return false; } - ledgerHash.SetHexExact(*lhO); - parentHash.SetHexExact(*phO); - - return true; + return ledgerHash.parseHex(*lhO) && parentHash.parseHex(*phO); } std::map> @@ -1323,9 +1320,9 @@ getHashesByIndex(std::uint32_t minSeq, std::uint32_t maxSeq, Application& app) std::string sql = "SELECT LedgerSeq,LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq >= "; - sql.append(beast::lexicalCastThrow(minSeq)); + sql.append(std::to_string(minSeq)); sql.append(" AND LedgerSeq <= "); - sql.append(beast::lexicalCastThrow(maxSeq)); + sql.append(std::to_string(maxSeq)); sql.append(";"); auto db = app.getLedgerDB().checkoutDb(); @@ -1341,9 +1338,9 @@ getHashesByIndex(std::uint32_t minSeq, std::uint32_t maxSeq, Application& app) { std::pair& hashes = ret[rangeCheckedCast(ls)]; - hashes.first.SetHexExact(lh); + (void)hashes.first.parseHex(lh); if (ph) - hashes.second.SetHexExact(*ph); + (void)hashes.second.parseHex(*ph); else hashes.second.zero(); if (!ph) diff --git a/src/ripple/app/ledger/Ledger.h b/src/ripple/app/ledger/Ledger.h index 2dff59380..1e3eb791d 100644 --- a/src/ripple/app/ledger/Ledger.h +++ b/src/ripple/app/ledger/Ledger.h @@ -80,12 +80,6 @@ class Ledger final : public std::enable_shared_from_this, public CountedObject { public: - static char const* - getCountedObjectName() - { - return "Ledger"; - } - Ledger(Ledger const&) = delete; Ledger& operator=(Ledger const&) = delete; diff --git a/src/ripple/app/ledger/impl/InboundLedgers.cpp b/src/ripple/app/ledger/impl/InboundLedgers.cpp index 98a6cc895..b7116aba2 100644 --- a/src/ripple/app/ledger/impl/InboundLedgers.cpp +++ b/src/ripple/app/ledger/impl/InboundLedgers.cpp @@ -309,8 +309,7 @@ public: for (auto const& it : mRecentFailures) { if (it.second > 1) - ret[beast::lexicalCastThrow(it.second)] - [jss::failed] = true; + ret[std::to_string(it.second)][jss::failed] = true; else ret[to_string(it.first)][jss::failed] = true; } diff --git a/src/ripple/app/ledger/impl/TransactionAcquire.h b/src/ripple/app/ledger/impl/TransactionAcquire.h index be93a86a6..c1119c4ac 100644 --- a/src/ripple/app/ledger/impl/TransactionAcquire.h +++ b/src/ripple/app/ledger/impl/TransactionAcquire.h @@ -34,15 +34,8 @@ class TransactionAcquire final public CountedObject { public: - static char const* - getCountedObjectName() - { - return "TransactionAcquire"; - } - using pointer = std::shared_ptr; -public: TransactionAcquire(Application& app, uint256 const& hash); ~TransactionAcquire() = default; diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 411dfaf52..04828557c 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -1878,7 +1878,7 @@ ApplicationImp::loadLedgerFromFile(std::string const& name) uint256 uIndex; - if (!uIndex.SetHex(entry[jss::index].asString())) + if (!uIndex.parseHex(entry[jss::index].asString())) { JLOG(m_journal.fatal()) << "Invalid entry in ledger"; return nullptr; @@ -1940,7 +1940,7 @@ ApplicationImp::loadOldLedger( { uint256 hash; - if (hash.SetHex(ledgerID)) + if (hash.parseHex(ledgerID)) { loadLedger = loadByHash(hash, *this); diff --git a/src/ripple/app/misc/HashRouter.h b/src/ripple/app/misc/HashRouter.h index 76cf4431e..ba2f9436d 100644 --- a/src/ripple/app/misc/HashRouter.h +++ b/src/ripple/app/misc/HashRouter.h @@ -61,12 +61,6 @@ private: class Entry : public CountedObject { public: - static char const* - getCountedObjectName() - { - return "HashRouterEntry"; - } - Entry() { } diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index bc4bdd9d8..3d4ff057e 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -2229,8 +2229,7 @@ NetworkOPsImp::transactionsSQL( boost::format("SELECT %s FROM AccountTransactions " "WHERE Account = '%s' %s %s LIMIT %u, %u;") % selection % app_.accountIDCache().toBase58(account) % maxClause % - minClause % beast::lexicalCastThrow(offset) % - beast::lexicalCastThrow(numberOfResults)); + minClause % offset % numberOfResults); else sql = boost::str( boost::format( @@ -2244,8 +2243,7 @@ NetworkOPsImp::transactionsSQL( selection % app_.accountIDCache().toBase58(account) % maxClause % minClause % (descending ? "DESC" : "ASC") % (descending ? "DESC" : "ASC") % (descending ? "DESC" : "ASC") % - beast::lexicalCastThrow(offset) % - beast::lexicalCastThrow(numberOfResults)); + offset % numberOfResults); JLOG(m_journal.trace()) << "txSQL query: " << sql; return sql; } diff --git a/src/ripple/app/misc/Transaction.h b/src/ripple/app/misc/Transaction.h index 4dbe2f06b..79da8b224 100644 --- a/src/ripple/app/misc/Transaction.h +++ b/src/ripple/app/misc/Transaction.h @@ -61,12 +61,6 @@ class Transaction : public std::enable_shared_from_this, public CountedObject { public: - static char const* - getCountedObjectName() - { - return "Transaction"; - } - using pointer = std::shared_ptr; using ref = const pointer&; diff --git a/src/ripple/app/misc/impl/AmendmentTable.cpp b/src/ripple/app/misc/impl/AmendmentTable.cpp index 2f29a2f57..68ad92b20 100644 --- a/src/ripple/app/misc/impl/AmendmentTable.cpp +++ b/src/ripple/app/misc/impl/AmendmentTable.cpp @@ -56,7 +56,7 @@ parseSection(Section const& section) uint256 id; - if (!id.SetHexExact(match[1])) + if (!id.parseHex(match[1])) Throw( "Invalid amendment ID '" + match[1] + "' in [" + section.name() + "]"); diff --git a/src/ripple/app/misc/impl/Manifest.cpp b/src/ripple/app/misc/impl/Manifest.cpp index e69c5c49d..aa2f99e01 100644 --- a/src/ripple/app/misc/impl/Manifest.cpp +++ b/src/ripple/app/misc/impl/Manifest.cpp @@ -22,12 +22,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -235,7 +235,7 @@ loadValidatorToken(std::vector const& blob) })); for (auto const& line : blob) - tokenStr += beast::rfc2616::trim(line); + tokenStr += boost::algorithm::trim_copy(line); tokenStr = base64_decode(tokenStr); @@ -509,7 +509,7 @@ ManifestCache::load( })); for (auto const& line : configRevocation) - revocationStr += beast::rfc2616::trim(line); + revocationStr += boost::algorithm::trim_copy(line); auto mo = deserializeManifest(base64_decode(revocationStr)); diff --git a/src/ripple/app/paths/PathRequest.h b/src/ripple/app/paths/PathRequest.h index e0b9f37cd..4cba34fba 100644 --- a/src/ripple/app/paths/PathRequest.h +++ b/src/ripple/app/paths/PathRequest.h @@ -49,12 +49,6 @@ class PathRequest : public std::enable_shared_from_this, public CountedObject { public: - static char const* - getCountedObjectName() - { - return "PathRequest"; - } - using wptr = std::weak_ptr; using pointer = std::shared_ptr; using ref = const pointer&; @@ -112,8 +106,6 @@ public: private: bool isValid(std::shared_ptr const& crCache); - void - setValid(); std::unique_ptr const& getPathFinder( diff --git a/src/ripple/basics/CountedObject.h b/src/ripple/basics/CountedObject.h index cf57b2a30..a5bccf89e 100644 --- a/src/ripple/basics/CountedObject.h +++ b/src/ripple/basics/CountedObject.h @@ -21,6 +21,7 @@ #define RIPPLE_BASICS_COUNTEDOBJECT_H_INCLUDED #include +#include #include #include #include @@ -45,47 +46,60 @@ public: @internal */ - class CounterBase + class Counter { public: - CounterBase() noexcept; + Counter(std::string name) noexcept : name_(std::move(name)), count_(0) + { + // Insert ourselves at the front of the lock-free linked list + CountedObjects& instance = CountedObjects::getInstance(); + Counter* head; - virtual ~CounterBase() noexcept; + do + { + head = instance.m_head.load(); + next_ = head; + } while (instance.m_head.exchange(this) != head); + + ++instance.m_count; + } + + ~Counter() noexcept = default; int increment() noexcept { - return ++m_count; + return ++count_; } int decrement() noexcept { - return --m_count; + return --count_; } int getCount() const noexcept { - return m_count.load(); + return count_.load(); } - CounterBase* + Counter* getNext() const noexcept { - return m_next; + return next_; } - virtual char const* - getName() const = 0; + std::string const& + getName() const noexcept + { + return name_; + } private: - virtual void - checkPureVirtual() const = 0; - - protected: - std::atomic m_count; - CounterBase* m_next; + std::string const name_; + std::atomic count_; + Counter* next_; }; private: @@ -94,7 +108,7 @@ private: private: std::atomic m_count; - std::atomic m_head; + std::atomic m_head; }; //------------------------------------------------------------------------------ @@ -109,6 +123,14 @@ private: template class CountedObject { +private: + static auto& + getCounter() noexcept + { + static CountedObjects::Counter c{beast::type_name()}; + return c; + } + public: CountedObject() noexcept { @@ -127,35 +149,6 @@ public: { getCounter().decrement(); } - -private: - class Counter : public CountedObjects::CounterBase - { - public: - Counter() noexcept - { - } - - char const* - getName() const override - { - return Object::getCountedObjectName(); - } - - void - checkPureVirtual() const override - { - } - }; - -private: - static Counter& - getCounter() noexcept - { - static_assert(std::is_nothrow_constructible{}, ""); - static Counter c; - return c; - } }; } // namespace ripple diff --git a/src/ripple/basics/StringUtilities.h b/src/ripple/basics/StringUtilities.h index 9cedbbe17..759d40736 100644 --- a/src/ripple/basics/StringUtilities.h +++ b/src/ripple/basics/StringUtilities.h @@ -30,42 +30,19 @@ #include namespace ripple { -inline static std::string -sqlEscape(std::string const& strSrc) -{ - static boost::format f("X'%s'"); - return str(boost::format(f) % strHex(strSrc)); -} -inline static std::string -sqlEscape(Blob const& vecSrc) -{ - size_t size = vecSrc.size(); +/** Format arbitrary binary data as an SQLite "blob literal". - if (size == 0) - return "X''"; + In SQLite, blob literals must be encoded when used in a query. Per + https://sqlite.org/lang_expr.html#literal_values_constants_ they are + encoded as string literals containing hexadecimal data and preceded + by a single 'X' character. - std::string j(size * 2 + 3, 0); - - unsigned char* oPtr = reinterpret_cast(&*j.begin()); - const unsigned char* iPtr = &vecSrc[0]; - - *oPtr++ = 'X'; - *oPtr++ = '\''; - - for (int i = size; i != 0; --i) - { - unsigned char c = *iPtr++; - *oPtr++ = charHex(c >> 4); - *oPtr++ = charHex(c & 15); - } - - *oPtr++ = '\''; - return j; -} - -uint64_t -uintFromHex(std::string const& strSrc); + @param blob An arbitrary blob of binary data + @return The input, encoded as a blob literal. + */ +std::string +sqlBlobLiteral(Blob const& blob); template boost::optional diff --git a/src/ripple/basics/base_uint.h b/src/ripple/basics/base_uint.h index bc23a5825..3ada512b5 100644 --- a/src/ripple/basics/base_uint.h +++ b/src/ripple/basics/base_uint.h @@ -56,8 +56,19 @@ struct is_contiguous_container< } // namespace detail -// This class stores its values internally in big-endian form +/** Integers of any length that is a multiple of 32-bits + @note This class stores its values internally in big-endian + form and that internal representation is part of the + binary protocol of the XRP Ledger and cannot be changed + arbitrarily without causing breakage. + + @tparam Bits The number of bits this integer should have; must + be at least 64 and a multiple of 32. + @tparam Tag An arbitrary type that functions as a tag and allows + the instantiation of "distinct" types that the same + number of bits. + */ template class base_uint { @@ -362,99 +373,53 @@ public: } /** Parse a hex string into a base_uint - The string must contain exactly bytes * 2 hex characters and must not - have any leading or trailing whitespace. - */ - bool - SetHexExact(const char* psz) + + The input must be precisely `2 * bytes` hexadecimal characters + long, with one exception: the value '0'. + + @param sv A null-terminated string of hexadecimal characters + @return true if the input was parsed properly; false otherwise. + */ + [[nodiscard]] bool + parseHex(std::string_view sv) { - unsigned char* pOut = begin(); - - for (int i = 0; i < sizeof(data_); ++i) + if (sv == "0") { - auto hi = charUnHex(*psz++); - if (hi == -1) - return false; - - auto lo = charUnHex(*psz++); - if (lo == -1) - return false; - - *pOut++ = (hi << 4) | lo; + zero(); + return true; } - // We've consumed exactly as many bytes as we needed at this point - // so we should be at the end of the string. - return (*psz == 0); - } + if (sv.size() != bytes * 2) + return false; - /** Parse a hex string into a base_uint - The input can be: - - shorter than the full hex representation by not including leading - zeroes. - - longer than the full hex representation in which case leading - bytes are discarded. + auto out = data(); - When finished parsing, the string must be fully consumed with only a - null terminator remaining. + auto in = sv.begin(); - When bStrict is false, the parsing is done in non-strict mode, and, if - present, leading whitespace and the 0x prefix will be skipped. - */ - bool - SetHex(const char* psz, bool bStrict = false) - { - // Find beginning. - auto pBegin = reinterpret_cast(psz); - // skip leading spaces - if (!bStrict) - while (isspace(*pBegin)) - pBegin++; - - // skip 0x - if (!bStrict && pBegin[0] == '0' && tolower(pBegin[1]) == 'x') - pBegin += 2; - - // Find end. - auto pEnd = pBegin; - while (charUnHex(*pEnd) != -1) - pEnd++; - - // Take only last digits of over long string. - if ((unsigned int)(pEnd - pBegin) > 2 * size()) - pBegin = pEnd - 2 * size(); - - unsigned char* pOut = end() - ((pEnd - pBegin + 1) / 2); - - *this = beast::zero; - - if ((pEnd - pBegin) & 1) - *pOut++ = charUnHex(*pBegin++); - - while (pBegin != pEnd) + while (in != sv.end()) { - auto cHigh = charUnHex(*pBegin++); - auto cLow = pBegin == pEnd ? 0 : charUnHex(*pBegin++); + auto const hi = charUnHex(*in++); + auto const lo = charUnHex(*in++); - if (cHigh == -1 || cLow == -1) + if (hi == -1 || lo == -1) return false; - *pOut++ = (cHigh << 4) | cLow; + *out++ = static_cast((hi << 4) + lo); } - return !*pEnd; + return true; } - bool - SetHex(std::string const& str, bool bStrict = false) + [[nodiscard]] bool + parseHex(const char* str) { - return SetHex(str.c_str(), bStrict); + return parseHex(std::string_view{str}); } - bool - SetHexExact(std::string const& str) + [[nodiscard]] bool + parseHex(std::string const& str) { - return SetHexExact(str.c_str()); + return parseHex(std::string_view{str}); } constexpr static std::size_t @@ -602,31 +567,6 @@ to_string(base_uint const& a) return strHex(a.cbegin(), a.cend()); } -// Function templates that return a base_uint given text in hexadecimal. -// Invoke like: -// auto i = from_hex_text("AAAAA"); -template -auto -from_hex_text(char const* text) -> std::enable_if_t< - std::is_same>::value, - T> -{ - T ret; - ret.SetHex(text); - return ret; -} - -template -auto -from_hex_text(std::string const& text) -> std::enable_if_t< - std::is_same>::value, - T> -{ - T ret; - ret.SetHex(text); - return ret; -} - template inline std::ostream& operator<<(std::ostream& out, base_uint const& u) diff --git a/src/ripple/basics/impl/CountedObject.cpp b/src/ripple/basics/impl/CountedObject.cpp index da42bf5b4..5b2509163 100644 --- a/src/ripple/basics/impl/CountedObject.cpp +++ b/src/ripple/basics/impl/CountedObject.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include namespace ripple { @@ -41,53 +42,17 @@ CountedObjects::getCounts(int minimumThreshold) const // When other operations are concurrent, the count // might be temporarily less than the actual count. - int const count = m_count.load(); + counts.reserve(m_count.load()); - counts.reserve(count); - - CounterBase* counter = m_head.load(); - - while (counter != nullptr) + for (auto* ctr = m_head.load(); ctr != nullptr; ctr = ctr->getNext()) { - if (counter->getCount() >= minimumThreshold) - { - Entry entry; - - entry.first = counter->getName(); - entry.second = counter->getCount(); - - counts.push_back(entry); - } - - counter = counter->getNext(); + if (ctr->getCount() >= minimumThreshold) + counts.emplace_back(ctr->getName(), ctr->getCount()); } + std::sort(counts.begin(), counts.end()); + return counts; } -//------------------------------------------------------------------------------ - -CountedObjects::CounterBase::CounterBase() noexcept : m_count(0) -{ - // Insert ourselves at the front of the lock-free linked list - - CountedObjects& instance = CountedObjects::getInstance(); - CounterBase* head; - - do - { - head = instance.m_head.load(); - m_next = head; - } while (instance.m_head.exchange(this) != head); - - ++instance.m_count; -} - -CountedObjects::CounterBase::~CounterBase() noexcept -{ - // VFALCO NOTE If the counters are destroyed before the singleton, - // undefined behavior will result if the singleton's member - // functions are called. -} - } // namespace ripple diff --git a/src/ripple/basics/impl/StringUtilities.cpp b/src/ripple/basics/impl/StringUtilities.cpp index d76bd79b6..346e94c75 100644 --- a/src/ripple/basics/impl/StringUtilities.cpp +++ b/src/ripple/basics/impl/StringUtilities.cpp @@ -26,29 +26,21 @@ #include #include #include -#include namespace ripple { -uint64_t -uintFromHex(std::string const& strSrc) +std::string +sqlBlobLiteral(Blob const& blob) { - uint64_t uValue(0); + std::string j; - if (strSrc.size() > 16) - Throw("overlong 64-bit value"); + j.reserve(blob.size() * 2 + 3); + j.push_back('X'); + j.push_back('\''); + boost::algorithm::hex(blob.begin(), blob.end(), std::back_inserter(j)); + j.push_back('\''); - for (auto c : strSrc) - { - int ret = charUnHex(c); - - if (ret == -1) - Throw("invalid hex digit"); - - uValue = (uValue << 4) | ret; - } - - return uValue; + return j; } bool diff --git a/src/ripple/basics/impl/strHex.cpp b/src/ripple/basics/impl/strHex.cpp index 66aa8949f..084493af5 100644 --- a/src/ripple/basics/impl/strHex.cpp +++ b/src/ripple/basics/impl/strHex.cpp @@ -18,37 +18,30 @@ //============================================================================== #include -#include -#include +#include namespace ripple { int charUnHex(unsigned char c) { - struct HexTab - { - int hex[256]; + static constexpr std::array const xtab = []() { + std::array t{}; - HexTab() - { - std::fill(std::begin(hex), std::end(hex), -1); - for (int i = 0; i < 10; ++i) - hex['0' + i] = i; - for (int i = 0; i < 6; ++i) - { - hex['A' + i] = 10 + i; - hex['a' + i] = 10 + i; - } - } - int - operator[](unsigned char c) const - { - return hex[c]; - } - }; + for (auto& x : t) + x = -1; - static HexTab xtab; + for (int i = 0; i < 10; ++i) + t['0' + i] = i; + + for (int i = 0; i < 6; ++i) + { + t['A' + i] = 10 + i; + t['a' + i] = 10 + i; + } + + return t; + }(); return xtab[c]; } diff --git a/src/ripple/basics/strHex.h b/src/ripple/basics/strHex.h index f3618a359..e48ea9215 100644 --- a/src/ripple/basics/strHex.h +++ b/src/ripple/basics/strHex.h @@ -17,36 +17,14 @@ */ //============================================================================== -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. - #ifndef RIPPLE_BASICS_STRHEX_H_INCLUDED #define RIPPLE_BASICS_STRHEX_H_INCLUDED -#include -#include - #include #include namespace ripple { -/** Converts an integer to the corresponding hex digit - @param iDigit 0-15 inclusive - @return a character from '0'-'9' or 'A'-'F'. -*/ -inline char -charHex(unsigned int digit) -{ - static char const xtab[] = "0123456789ABCDEF"; - - assert(digit < 16); - - return xtab[digit]; -} - /** @{ */ /** Converts a hex digit to the corresponding integer @param cDigit one of '0'-'9', 'A'-'F' or 'a'-'f' @@ -84,15 +62,6 @@ strHex(T const& from) return strHex(from.begin(), from.end()); } -inline std::string -strHex(const std::uint64_t uiHost) -{ - uint64_t uBig = boost::endian::native_to_big(uiHost); - - auto const begin = (unsigned char*)&uBig; - auto const end = begin + sizeof(uBig); - return strHex(begin, end); -} } // namespace ripple #endif diff --git a/src/ripple/beast/asio/io_latency_probe.h b/src/ripple/beast/asio/io_latency_probe.h index b18fb823e..bbde13af6 100644 --- a/src/ripple/beast/asio/io_latency_probe.h +++ b/src/ripple/beast/asio/io_latency_probe.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -137,12 +136,7 @@ private: } if (wait) -#ifdef BOOST_NO_CXX11_LAMBDAS - while (m_count != 0) - m_cond.wait(lock); -#else m_cond.wait(lock, [this] { return this->m_count == 0; }); -#endif } void diff --git a/src/ripple/beast/crypto/detail/mac_facade.h b/src/ripple/beast/crypto/detail/mac_facade.h deleted file mode 100644 index f58c2fa23..000000000 --- a/src/ripple/beast/crypto/detail/mac_facade.h +++ /dev/null @@ -1,82 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_CRYPTO_MAC_FACADE_H_INCLUDED -#define BEAST_CRYPTO_MAC_FACADE_H_INCLUDED - -#include -#include -#include -#include - -namespace beast { -namespace detail { - -// Message Authentication Code (MAC) facade -template -class mac_facade -{ -private: - Context ctx_; - -public: - static beast::endian const endian = beast::endian::native; - - static std::size_t const digest_size = Context::digest_size; - - using result_type = std::array; - - mac_facade() noexcept - { - init(ctx_); - } - - ~mac_facade() - { - erase(std::integral_constant{}); - } - - void - operator()(void const* data, std::size_t size) noexcept - { - update(ctx_, data, size); - } - - explicit operator result_type() noexcept - { - result_type digest; - finish(ctx_, &digest[0]); - return digest; - } - -private: - inline void erase(std::false_type) noexcept - { - } - - inline void erase(std::true_type) noexcept - { - secure_erase(&ctx_, sizeof(ctx_)); - } -}; - -} // namespace detail -} // namespace beast - -#endif diff --git a/src/ripple/beast/crypto/detail/ripemd_context.h b/src/ripple/beast/crypto/detail/ripemd_context.h deleted file mode 100644 index 8eb7a573d..000000000 --- a/src/ripple/beast/crypto/detail/ripemd_context.h +++ /dev/null @@ -1,449 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_CRYPTO_RIPEMD_CONTEXT_H_INCLUDED -#define BEAST_CRYPTO_RIPEMD_CONTEXT_H_INCLUDED - -#include -#include -#include - -namespace beast { -namespace detail { - -// Based on -// https://code.google.com/p/blockchain/source/browse/trunk/RIPEMD160.cpp -/* - Copyright (c) Katholieke Universiteit Leuven - 1996 All Rights Reserved - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to - deal in the Software without restriction, including without limitation the - rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - sell copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - IN THE SOFTWARE. -*/ -// This implementation has been modified from the -// original. It has been updated for C++11. - -struct ripemd160_context -{ - explicit ripemd160_context() = default; - - static unsigned int const block_size = 64; - static unsigned int const digest_size = 20; - - unsigned int tot_len; - unsigned int len; - unsigned char block[256]; - std::uint32_t h[5]; -}; - -// ROL(x, n) cyclically rotates x over n bits to the left -// x must be of an unsigned 32 bits type and 0 <= n < 32. -#define BEAST_RIPEMD_ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) - -// the five basic functions F(), G() and H() -#define BEAST_RIPEMD_F(x, y, z) ((x) ^ (y) ^ (z)) -#define BEAST_RIPEMD_G(x, y, z) (((x) & (y)) | (~(x) & (z))) -#define BEAST_RIPEMD_H(x, y, z) (((x) | ~(y)) ^ (z)) -#define BEAST_RIPEMD_I(x, y, z) (((x) & (z)) | ((y) & ~(z))) -#define BEAST_RIPEMD_J(x, y, z) ((x) ^ ((y) | ~(z))) - -// the ten basic operations FF() through III() -#define BEAST_RIPEMD_FF(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_GG(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x5a827999UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_HH(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6ed9eba1UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_II(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x8f1bbcdcUL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_JJ(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0xa953fd4eUL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_FFF(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_GGG(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x7a6d76e9UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_HHH(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6d703ef3UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_III(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x5c4dd124UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } -#define BEAST_RIPEMD_JJJ(a, b, c, d, e, x, s) \ - { \ - (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0x50a28be6UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); \ - } - -template -void -ripemd_load(std::array& X, unsigned char const* p) -{ - for (int i = 0; i < 16; ++i) - { - X[i] = ((std::uint32_t) * ((p) + 3) << 24) | - ((std::uint32_t) * ((p) + 2) << 16) | - ((std::uint32_t) * ((p) + 1) << 8) | ((std::uint32_t) * (p)); - p += 4; - } -} - -template -void -ripemd_compress( - ripemd160_context& ctx, - std::array& X) noexcept -{ - std::uint32_t aa = ctx.h[0]; - std::uint32_t bb = ctx.h[1]; - std::uint32_t cc = ctx.h[2]; - std::uint32_t dd = ctx.h[3]; - std::uint32_t ee = ctx.h[4]; - std::uint32_t aaa = ctx.h[0]; - std::uint32_t bbb = ctx.h[1]; - std::uint32_t ccc = ctx.h[2]; - std::uint32_t ddd = ctx.h[3]; - std::uint32_t eee = ctx.h[4]; - - // round 1 - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[0], 11); - BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[1], 14); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[2], 15); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[3], 12); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[4], 5); - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[5], 8); - BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[6], 7); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[7], 9); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[8], 11); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[9], 13); - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[10], 14); - BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[11], 15); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[12], 6); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[13], 7); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[14], 9); - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[15], 8); - - // round 2 - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[7], 7); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[4], 6); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[13], 8); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[1], 13); - BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[10], 11); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[6], 9); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[15], 7); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[3], 15); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[12], 7); - BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[0], 12); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[9], 15); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[5], 9); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[2], 11); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[14], 7); - BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[11], 13); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[8], 12); - - // round 3 - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[3], 11); - BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[10], 13); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[14], 6); - BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[4], 7); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[9], 14); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[15], 9); - BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[8], 13); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[1], 15); - BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[2], 14); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[7], 8); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[0], 13); - BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[6], 6); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[13], 5); - BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[11], 12); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[5], 7); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[12], 5); - - // round 4 - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[1], 11); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[9], 12); - BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[11], 14); - BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[10], 15); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[0], 14); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[8], 15); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[12], 9); - BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[4], 8); - BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[13], 9); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[3], 14); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[7], 5); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[15], 6); - BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[14], 8); - BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[5], 6); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[6], 5); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[2], 12); - - // round 5 - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[4], 9); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[0], 15); - BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[5], 5); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[9], 11); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[7], 6); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[12], 8); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[2], 13); - BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[10], 12); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[14], 5); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[1], 12); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[3], 13); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[8], 14); - BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[11], 11); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[6], 8); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[15], 5); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[13], 6); - - // parallel round 1 - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[5], 8); - BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[7], 9); - BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[0], 11); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[9], 13); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[2], 15); - BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[4], 5); - BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[6], 7); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); - BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[8], 11); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[1], 14); - BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[3], 12); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); - - // parallel round 2 - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[6], 9); - BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[11], 13); - BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[3], 15); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[7], 7); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[0], 12); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[13], 8); - BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[5], 9); - BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[10], 11); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[14], 7); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[15], 7); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[8], 12); - BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[12], 7); - BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[4], 6); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[9], 15); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[1], 13); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[2], 11); - - // parallel round 3 - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[5], 7); - BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[1], 15); - BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[3], 11); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[7], 8); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[6], 6); - BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[9], 14); - BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[8], 13); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[2], 14); - BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); - BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[0], 13); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[4], 7); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); - - // parallel round 4 - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[8], 15); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[6], 5); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[4], 8); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[1], 11); - BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[3], 14); - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[0], 14); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[5], 6); - BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[2], 12); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[9], 12); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[7], 5); - BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); - - // parallel round 5 - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[12], 8); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[15], 5); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[10], 12); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[4], 9); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[1], 12); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[5], 5); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[8], 14); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[7], 6); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[6], 8); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[2], 13); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[13], 6); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[14], 5); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[0], 15); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[3], 13); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[9], 11); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[11], 11); - - // combine results - ddd += cc + ctx.h[1]; // final result for h[0] - ctx.h[1] = ctx.h[2] + dd + eee; - ctx.h[2] = ctx.h[3] + ee + aaa; - ctx.h[3] = ctx.h[4] + aa + bbb; - ctx.h[4] = ctx.h[0] + bb + ccc; - ctx.h[0] = ddd; -} - -template -void -init(ripemd160_context& ctx) noexcept -{ - ctx.len = 0; - ctx.tot_len = 0; - ctx.h[0] = 0x67452301UL; - ctx.h[1] = 0xefcdab89UL; - ctx.h[2] = 0x98badcfeUL; - ctx.h[3] = 0x10325476UL; - ctx.h[4] = 0xc3d2e1f0UL; -} - -template -void -update(ripemd160_context& ctx, void const* message, std::size_t size) noexcept -{ - auto const pm = reinterpret_cast(message); - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char* shifted_message; - tmp_len = ripemd160_context::block_size - ctx.len; - rem_len = size < tmp_len ? size : tmp_len; - std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < ripemd160_context::block_size) - { - ctx.len += size; - return; - } - new_len = size - rem_len; - block_nb = new_len / ripemd160_context::block_size; - shifted_message = pm + rem_len; - std::array X; - ripemd_load(X, ctx.block); - ripemd_compress(ctx, X); - for (int i = 0; i < block_nb; ++i) - { - ripemd_load(X, shifted_message + i * ripemd160_context::block_size); - ripemd_compress(ctx, X); - } - rem_len = new_len % ripemd160_context::block_size; - std::memcpy( - ctx.block, - &shifted_message[block_nb * ripemd160_context::block_size], - rem_len); - ctx.len = rem_len; - ctx.tot_len += (block_nb + 1) * ripemd160_context::block_size; -} - -template -void -finish(ripemd160_context& ctx, void* digest) noexcept -{ - std::array X; - X.fill(0); - // put leftovers into X - auto p = &ctx.block[0]; - // uint8_t i goes into word X[i div 4] at pos. 8*(i mod 4) - for (int i = 0; i < ctx.len; ++i) - X[i >> 2] ^= (std::uint32_t)*p++ << (8 * (i & 3)); - ctx.tot_len += ctx.len; - // append the bit m_n == 1 - X[(ctx.tot_len >> 2) & 15] ^= (uint32_t)1 << (8 * (ctx.tot_len & 3) + 7); - // length goes to next block? - if ((ctx.tot_len & 63) > 55) - { - ripemd_compress(ctx, X); - X.fill(0); - } - // append length in bits*/ - X[14] = ctx.tot_len << 3; - X[15] = (ctx.tot_len >> 29) | (0 << 3); - ripemd_compress(ctx, X); - - std::uint8_t* pd = reinterpret_cast(digest); - for (std::uint32_t i = 0; i < 20; i += 4) - { - pd[i] = (std::uint8_t)(ctx.h[i >> 2]); // implicit cast to uint8_t - pd[i + 1] = (std::uint8_t)(ctx.h[i >> 2] >> 8); // extracts the 8 least - pd[i + 2] = (std::uint8_t)(ctx.h[i >> 2] >> 16); // significant bits. - pd[i + 3] = (std::uint8_t)(ctx.h[i >> 2] >> 24); - } -} - -} // namespace detail -} // namespace beast - -#endif diff --git a/src/ripple/beast/crypto/detail/sha2_context.h b/src/ripple/beast/crypto/detail/sha2_context.h deleted file mode 100644 index 6fc8018fc..000000000 --- a/src/ripple/beast/crypto/detail/sha2_context.h +++ /dev/null @@ -1,419 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_CRYPTO_SHA512_CONTEXT_H_INCLUDED -#define BEAST_CRYPTO_SHA512_CONTEXT_H_INCLUDED - -#include -#include - -namespace beast { -namespace detail { - -// Based on https://github.com/ogay/sha2 -// This implementation has been modified from the -// original. It has been updated for C++11. - -/* - * Updated to C++, zedwood.com 2012 - * Based on Olivier Gay's version - * See Modified BSD License below: - * - * FIPS 180-2 SHA-224/256/384/512 implementation - * Issue date: 04/30/2005 - * http://www.ouah.org/ogay/sha2/ - * - * Copyright (C) 2005, 2007 Olivier Gay - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -struct sha256_context -{ - explicit sha256_context() = default; - - static unsigned int const block_size = 64; - static unsigned int const digest_size = 32; - - unsigned int tot_len; - unsigned int len; - unsigned char block[2 * block_size]; - std::uint32_t h[8]; -}; - -struct sha512_context -{ - explicit sha512_context() = default; - - static unsigned int const block_size = 128; - static unsigned int const digest_size = 64; - - unsigned int tot_len; - unsigned int len; - unsigned char block[2 * block_size]; - std::uint64_t h[8]; -}; - -#define BEAST_SHA2_SHFR(x, n) (x >> n) -#define BEAST_SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) -#define BEAST_SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) -#define BEAST_SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) -#define BEAST_SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) -#define BEAST_SHA256_F1(x) \ - (BEAST_SHA2_ROTR(x, 2) ^ BEAST_SHA2_ROTR(x, 13) ^ BEAST_SHA2_ROTR(x, 22)) -#define BEAST_SHA256_F2(x) \ - (BEAST_SHA2_ROTR(x, 6) ^ BEAST_SHA2_ROTR(x, 11) ^ BEAST_SHA2_ROTR(x, 25)) -#define BEAST_SHA256_F3(x) \ - (BEAST_SHA2_ROTR(x, 7) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_SHFR(x, 3)) -#define BEAST_SHA256_F4(x) \ - (BEAST_SHA2_ROTR(x, 17) ^ BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_SHFR(x, 10)) -#define BEAST_SHA512_F1(x) \ - (BEAST_SHA2_ROTR(x, 28) ^ BEAST_SHA2_ROTR(x, 34) ^ BEAST_SHA2_ROTR(x, 39)) -#define BEAST_SHA512_F2(x) \ - (BEAST_SHA2_ROTR(x, 14) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_ROTR(x, 41)) -#define BEAST_SHA512_F3(x) \ - (BEAST_SHA2_ROTR(x, 1) ^ BEAST_SHA2_ROTR(x, 8) ^ BEAST_SHA2_SHFR(x, 7)) -#define BEAST_SHA512_F4(x) \ - (BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_ROTR(x, 61) ^ BEAST_SHA2_SHFR(x, 6)) -#define BEAST_SHA2_PACK32(str, x) \ - { \ - *(x) = ((std::uint32_t) * ((str) + 3)) | \ - ((std::uint32_t) * ((str) + 2) << 8) | \ - ((std::uint32_t) * ((str) + 1) << 16) | \ - ((std::uint32_t) * ((str) + 0) << 24); \ - } -#define BEAST_SHA2_UNPACK32(x, str) \ - { \ - *((str) + 3) = (std::uint8_t)((x)); \ - *((str) + 2) = (std::uint8_t)((x) >> 8); \ - *((str) + 1) = (std::uint8_t)((x) >> 16); \ - *((str) + 0) = (std::uint8_t)((x) >> 24); \ - } -#define BEAST_SHA2_PACK64(str, x) \ - { \ - *(x) = ((std::uint64_t) * ((str) + 7)) | \ - ((std::uint64_t) * ((str) + 6) << 8) | \ - ((std::uint64_t) * ((str) + 5) << 16) | \ - ((std::uint64_t) * ((str) + 4) << 24) | \ - ((std::uint64_t) * ((str) + 3) << 32) | \ - ((std::uint64_t) * ((str) + 2) << 40) | \ - ((std::uint64_t) * ((str) + 1) << 48) | \ - ((std::uint64_t) * ((str) + 0) << 56); \ - } -#define BEAST_SHA2_UNPACK64(x, str) \ - { \ - *((str) + 7) = (std::uint8_t)((x)); \ - *((str) + 6) = (std::uint8_t)((x) >> 8); \ - *((str) + 5) = (std::uint8_t)((x) >> 16); \ - *((str) + 4) = (std::uint8_t)((x) >> 24); \ - *((str) + 3) = (std::uint8_t)((x) >> 32); \ - *((str) + 2) = (std::uint8_t)((x) >> 40); \ - *((str) + 1) = (std::uint8_t)((x) >> 48); \ - *((str) + 0) = (std::uint8_t)((x) >> 56); \ - } - -//------------------------------------------------------------------------------ - -// SHA256 - -template -void -sha256_transform( - sha256_context& ctx, - unsigned char const* message, - unsigned int block_nb) noexcept -{ - static unsigned long long const K[64] = { - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, - 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, - 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, - 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, - 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - std::uint32_t w[64]; - std::uint32_t wv[8]; - std::uint32_t t1, t2; - unsigned char const* sub_block; - int i, j; - for (i = 0; i < (int)block_nb; i++) - { - sub_block = message + (i << 6); - for (j = 0; j < 16; j++) - BEAST_SHA2_PACK32(&sub_block[j << 2], &w[j]); - for (j = 16; j < 64; j++) - w[j] = BEAST_SHA256_F4(w[j - 2]) + w[j - 7] + - BEAST_SHA256_F3(w[j - 15]) + w[j - 16]; - for (j = 0; j < 8; j++) - wv[j] = ctx.h[j]; - for (j = 0; j < 64; j++) - { - t1 = wv[7] + BEAST_SHA256_F2(wv[4]) + - BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + K[j] + w[j]; - t2 = BEAST_SHA256_F1(wv[0]) + BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - for (j = 0; j < 8; j++) - ctx.h[j] += wv[j]; - } -} - -template -void -init(sha256_context& ctx) noexcept -{ - ctx.len = 0; - ctx.tot_len = 0; - ctx.h[0] = 0x6a09e667; - ctx.h[1] = 0xbb67ae85; - ctx.h[2] = 0x3c6ef372; - ctx.h[3] = 0xa54ff53a; - ctx.h[4] = 0x510e527f; - ctx.h[5] = 0x9b05688c; - ctx.h[6] = 0x1f83d9ab; - ctx.h[7] = 0x5be0cd19; -} - -template -void -update(sha256_context& ctx, void const* message, std::size_t size) noexcept -{ - auto const pm = reinterpret_cast(message); - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char* shifted_message; - tmp_len = sha256_context::block_size - ctx.len; - rem_len = size < tmp_len ? size : tmp_len; - std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < sha256_context::block_size) - { - ctx.len += size; - return; - } - new_len = size - rem_len; - block_nb = new_len / sha256_context::block_size; - shifted_message = pm + rem_len; - sha256_transform(ctx, ctx.block, 1); - sha256_transform(ctx, shifted_message, block_nb); - rem_len = new_len % sha256_context::block_size; - std::memcpy(ctx.block, &shifted_message[block_nb << 6], rem_len); - ctx.len = rem_len; - ctx.tot_len += (block_nb + 1) << 6; -} - -template -void -finish(sha256_context& ctx, void* digest) noexcept -{ - auto const pd = reinterpret_cast(digest); - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - int i; - block_nb = - (1 + - ((sha256_context::block_size - 9) < - (ctx.len % sha256_context::block_size))); - len_b = (ctx.tot_len + ctx.len) << 3; - pm_len = block_nb << 6; - std::memset(ctx.block + ctx.len, 0, pm_len - ctx.len); - ctx.block[ctx.len] = 0x80; - BEAST_SHA2_UNPACK32(len_b, ctx.block + pm_len - 4); - sha256_transform(ctx, ctx.block, block_nb); - for (i = 0; i < 8; i++) - BEAST_SHA2_UNPACK32(ctx.h[i], &pd[i << 2]); -} - -//------------------------------------------------------------------------------ - -// SHA512 - -template -void -sha512_transform( - sha512_context& ctx, - unsigned char const* message, - unsigned int block_nb) noexcept -{ - static unsigned long long const K[80] = { - 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, - 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, - 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, - 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, - 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, - 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, - 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, - 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, - 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, - 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, - 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, - 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, - 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, - 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, - 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; - - std::uint64_t w[80]; - std::uint64_t wv[8]; - std::uint64_t t1, t2; - unsigned char const* sub_block; - int i, j; - for (i = 0; i < (int)block_nb; i++) - { - sub_block = message + (i << 7); - for (j = 0; j < 16; j++) - BEAST_SHA2_PACK64(&sub_block[j << 3], &w[j]); - for (j = 16; j < 80; j++) - w[j] = BEAST_SHA512_F4(w[j - 2]) + w[j - 7] + - BEAST_SHA512_F3(w[j - 15]) + w[j - 16]; - for (j = 0; j < 8; j++) - wv[j] = ctx.h[j]; - for (j = 0; j < 80; j++) - { - t1 = wv[7] + BEAST_SHA512_F2(wv[4]) + - BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + K[j] + w[j]; - t2 = BEAST_SHA512_F1(wv[0]) + BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); - wv[7] = wv[6]; - wv[6] = wv[5]; - wv[5] = wv[4]; - wv[4] = wv[3] + t1; - wv[3] = wv[2]; - wv[2] = wv[1]; - wv[1] = wv[0]; - wv[0] = t1 + t2; - } - for (j = 0; j < 8; j++) - ctx.h[j] += wv[j]; - } -} - -template -void -init(sha512_context& ctx) noexcept -{ - ctx.len = 0; - ctx.tot_len = 0; - ctx.h[0] = 0x6a09e667f3bcc908ULL; - ctx.h[1] = 0xbb67ae8584caa73bULL; - ctx.h[2] = 0x3c6ef372fe94f82bULL; - ctx.h[3] = 0xa54ff53a5f1d36f1ULL; - ctx.h[4] = 0x510e527fade682d1ULL; - ctx.h[5] = 0x9b05688c2b3e6c1fULL; - ctx.h[6] = 0x1f83d9abfb41bd6bULL; - ctx.h[7] = 0x5be0cd19137e2179ULL; -} - -template -void -update(sha512_context& ctx, void const* message, std::size_t size) noexcept -{ - auto const pm = reinterpret_cast(message); - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char* shifted_message; - tmp_len = sha512_context::block_size - ctx.len; - rem_len = size < tmp_len ? size : tmp_len; - std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < sha512_context::block_size) - { - ctx.len += size; - return; - } - new_len = size - rem_len; - block_nb = new_len / sha512_context::block_size; - shifted_message = pm + rem_len; - sha512_transform(ctx, ctx.block, 1); - sha512_transform(ctx, shifted_message, block_nb); - rem_len = new_len % sha512_context::block_size; - std::memcpy(ctx.block, &shifted_message[block_nb << 7], rem_len); - ctx.len = rem_len; - ctx.tot_len += (block_nb + 1) << 7; -} - -template -void -finish(sha512_context& ctx, void* digest) noexcept -{ - auto const pd = reinterpret_cast(digest); - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; - int i; - block_nb = 1 + - ((sha512_context::block_size - 17) < - (ctx.len % sha512_context::block_size)); - len_b = (ctx.tot_len + ctx.len) << 3; - pm_len = block_nb << 7; - std::memset(ctx.block + ctx.len, 0, pm_len - ctx.len); - ctx.block[ctx.len] = 0x80; - BEAST_SHA2_UNPACK32(len_b, ctx.block + pm_len - 4); - sha512_transform(ctx, ctx.block, block_nb); - for (i = 0; i < 8; i++) - BEAST_SHA2_UNPACK64(ctx.h[i], &pd[i << 3]); -} - -} // namespace detail -} // namespace beast - -#endif diff --git a/src/ripple/beast/crypto/ripemd.h b/src/ripple/beast/crypto/ripemd.h deleted file mode 100644 index 3111deee8..000000000 --- a/src/ripple/beast/crypto/ripemd.h +++ /dev/null @@ -1,35 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_CRYPTO_RIPEMD_H_INCLUDED -#define BEAST_CRYPTO_RIPEMD_H_INCLUDED - -#include -#include - -namespace beast { - -using ripemd160_hasher = detail::mac_facade; - -// secure version -using ripemd160_hasher_s = detail::mac_facade; - -} // namespace beast - -#endif diff --git a/src/ripple/beast/crypto/sha2.h b/src/ripple/beast/crypto/sha2.h deleted file mode 100644 index c7cd187a5..000000000 --- a/src/ripple/beast/crypto/sha2.h +++ /dev/null @@ -1,40 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_CRYPTO_SHA2_H_INCLUDED -#define BEAST_CRYPTO_SHA2_H_INCLUDED - -#include -#include - -namespace beast { - -using sha256_hasher = detail::mac_facade; - -// secure version -using sha256_hasher_s = detail::mac_facade; - -using sha512_hasher = detail::mac_facade; - -// secure version -using sha512_hasher_s = detail::mac_facade; - -} // namespace beast - -#endif diff --git a/src/ripple/beast/hash/endian.h b/src/ripple/beast/hash/endian.h deleted file mode 100644 index cda8f4b0d..000000000 --- a/src/ripple/beast/hash/endian.h +++ /dev/null @@ -1,54 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Howard Hinnant , - Vinnie Falco #include #include +#include #include #include #include @@ -77,7 +77,10 @@ inline void maybe_reverse_bytes(T& t, Hasher&) { maybe_reverse_bytes( - t, std::integral_constant{}); + t, + std::integral_constant< + bool, + Hasher::endian != boost::endian::order::native>{}); } } // namespace detail @@ -182,7 +185,8 @@ struct is_contiguously_hashable : public std::integral_constant< bool, is_uniquely_represented::value && - (sizeof(T) == 1 || HashAlgorithm::endian == endian::native)> + (sizeof(T) == 1 || + HashAlgorithm::endian == boost::endian::order::native)> { explicit is_contiguously_hashable() = default; }; @@ -192,7 +196,8 @@ struct is_contiguously_hashable : public std::integral_constant< bool, is_uniquely_represented::value && - (sizeof(T) == 1 || HashAlgorithm::endian == endian::native)> + (sizeof(T) == 1 || + HashAlgorithm::endian == boost::endian::order::native)> { explicit is_contiguously_hashable() = default; }; diff --git a/src/ripple/beast/hash/xxhasher.h b/src/ripple/beast/hash/xxhasher.h index d6f6335ac..512c451a0 100644 --- a/src/ripple/beast/hash/xxhasher.h +++ b/src/ripple/beast/hash/xxhasher.h @@ -20,8 +20,8 @@ #ifndef BEAST_HASH_XXHASHER_H_INCLUDED #define BEAST_HASH_XXHASHER_H_INCLUDED -#include #include +#include #include #include @@ -38,7 +38,7 @@ private: public: using result_type = std::size_t; - static beast::endian const endian = beast::endian::native; + static constexpr auto const endian = boost::endian::order::native; xxhasher() noexcept { diff --git a/src/ripple/beast/rfc2616.h b/src/ripple/beast/rfc2616.h index bb176b944..deac23550 100644 --- a/src/ripple/beast/rfc2616.h +++ b/src/ripple/beast/rfc2616.h @@ -29,7 +29,6 @@ #include #include #include -#include // for std::tie, remove ASAP #include #include @@ -38,27 +37,6 @@ namespace rfc2616 { namespace detail { -/* Routines for performing RFC2616 compliance. - RFC2616: - Hypertext Transfer Protocol -- HTTP/1.1 - http://www.w3.org/Protocols/rfc2616/rfc2616 -*/ - -struct ci_equal_pred -{ - explicit ci_equal_pred() = default; - - bool - operator()(char c1, char c2) - { - // VFALCO TODO Use a table lookup here - return std::tolower(static_cast(c1)) == - std::tolower(static_cast(c2)); - } -}; - -} // namespace detail - /** Returns `true` if `c` is linear white space. This excludes the CRLF sequence allowed for line continuations. @@ -86,57 +64,6 @@ is_white(char c) return false; } -/** Returns `true` if `c` is a control character. */ -inline bool -is_control(char c) -{ - return c <= 31 || c >= 127; -} - -/** Returns `true` if `c` is a separator. */ -inline bool -is_separator(char c) -{ - // VFALCO Could use a static table - switch (c) - { - case '(': - case ')': - case '<': - case '>': - case '@': - case ',': - case ';': - case ':': - case '\\': - case '"': - case '{': - case '}': - case ' ': - case '\t': - return true; - }; - return false; -} - -/** Returns `true` if `c` is a character. */ -inline bool -is_char(char c) -{ -#ifdef __CHAR_UNSIGNED__ /* -funsigned-char */ - return c >= 0 && c <= 127; -#else - return c >= 0; -#endif -} - -template -FwdIter -trim_left(FwdIter first, FwdIter last) -{ - return std::find_if_not(first, last, is_white); -} - template FwdIter trim_right(FwdIter first, FwdIter last) @@ -152,34 +79,6 @@ trim_right(FwdIter first, FwdIter last) return first; } -template -void -trim_right_in_place(std::basic_string& s) -{ - s.resize(std::distance(s.begin(), trim_right(s.begin(), s.end()))); -} - -template -std::pair -trim(FwdIter first, FwdIter last) -{ - first = trim_left(first, last); - last = trim_right(first, last); - return std::make_pair(first, last); -} - -template -String -trim(String const& s) -{ - using std::begin; - using std::end; - auto first = begin(s); - auto last = end(s); - std::tie(first, last) = trim(first, last); - return {first, last}; -} - template String trim_right(String const& s) @@ -192,11 +91,7 @@ trim_right(String const& s) return {first, last}; } -inline std::string -trim(std::string const& s) -{ - return trim(s); -} +} // namespace detail /** Parse a character sequence of values separated by commas. Double quotes and escape sequences will be converted. Excess white @@ -214,8 +109,11 @@ template < Result split(FwdIt first, FwdIt last, Char delim) { - Result result; + using namespace detail; using string = typename Result::value_type; + + Result result; + FwdIt iter = first; string e; while (iter != last) @@ -297,179 +195,6 @@ split_commas(boost::beast::string_view const& s) return split_commas(s.begin(), s.end()); } -//------------------------------------------------------------------------------ - -/** Iterates through a comma separated list. - - Meets the requirements of ForwardIterator. - - List defined in rfc2616 2.1. - - @note Values returned may contain backslash escapes. -*/ -class list_iterator -{ - using iter_type = boost::string_ref::const_iterator; - - iter_type it_; - iter_type end_; - boost::string_ref value_; - -public: - using value_type = boost::string_ref; - using pointer = value_type const*; - using reference = value_type const&; - using difference_type = std::ptrdiff_t; - using iterator_category = std::forward_iterator_tag; - - list_iterator(iter_type begin, iter_type end) : it_(begin), end_(end) - { - if (it_ != end_) - increment(); - } - - bool - operator==(list_iterator const& other) const - { - return other.it_ == it_ && other.end_ == end_ && - other.value_.size() == value_.size(); - } - - bool - operator!=(list_iterator const& other) const - { - return !(*this == other); - } - - reference - operator*() const - { - return value_; - } - - pointer - operator->() const - { - return &*(*this); - } - - list_iterator& - operator++() - { - increment(); - return *this; - } - - list_iterator - operator++(int) - { - auto temp = *this; - ++(*this); - return temp; - } - -private: - template - void - increment(); -}; - -template -void -list_iterator::increment() -{ - value_.clear(); - while (it_ != end_) - { - if (*it_ == '"') - { - // quoted-string - ++it_; - if (it_ == end_) - return; - if (*it_ != '"') - { - auto start = it_; - for (;;) - { - ++it_; - if (it_ == end_) - { - value_ = boost::string_ref( - &*start, std::distance(start, it_)); - return; - } - if (*it_ == '"') - { - value_ = boost::string_ref( - &*start, std::distance(start, it_)); - ++it_; - return; - } - } - } - ++it_; - } - else if (*it_ == ',') - { - it_++; - continue; - } - else if (is_lws(*it_)) - { - ++it_; - continue; - } - else - { - auto start = it_; - for (;;) - { - ++it_; - if (it_ == end_ || *it_ == ',' || is_lws(*it_)) - { - value_ = - boost::string_ref(&*start, std::distance(start, it_)); - return; - } - } - } - } -} - -/** Returns true if two strings are equal. - - A case-insensitive comparison is used. -*/ -inline bool -ci_equal(boost::string_ref s1, boost::string_ref s2) -{ - return boost::range::equal(s1, s2, detail::ci_equal_pred{}); -} - -/** Returns a range representing the list. */ -inline boost::iterator_range -make_list(boost::string_ref const& field) -{ - return boost::iterator_range{ - list_iterator{field.begin(), field.end()}, - list_iterator{field.end(), field.end()}}; -} - -/** Returns true if the specified token exists in the list. - - A case-insensitive comparison is used. -*/ -template -bool -token_in_list(boost::string_ref const& value, boost::string_ref const& token) -{ - for (auto const& item : make_list(value)) - if (ci_equal(item, token)) - return true; - return false; -} - template bool is_keep_alive(boost::beast::http::message const& m) diff --git a/src/ripple/consensus/Consensus.h b/src/ripple/consensus/Consensus.h index 5f88033ec..4b64cfbda 100644 --- a/src/ripple/consensus/Consensus.h +++ b/src/ripple/consensus/Consensus.h @@ -738,8 +738,7 @@ Consensus::peerProposalInternal( if (deadNodes_.find(peerID) != deadNodes_.end()) { - using std::to_string; - JLOG(j_.info()) << "Position from dead node: " << to_string(peerID); + JLOG(j_.info()) << "Position from dead node: " << peerID; return false; } @@ -758,9 +757,7 @@ Consensus::peerProposalInternal( if (newPeerProp.isBowOut()) { - using std::to_string; - - JLOG(j_.info()) << "Peer bows out: " << to_string(peerID); + JLOG(j_.info()) << "Peer " << peerID << " bows out"; if (result_) { for (auto& it : result_->disputes) @@ -1559,10 +1556,7 @@ Consensus::haveConsensus() } else { - using std::to_string; - - JLOG(j_.debug()) << to_string(nodeId) << " has " - << to_string(peerProp.position()); + JLOG(j_.debug()) << nodeId << " has " << peerProp.position(); ++disagree; } } diff --git a/src/ripple/json/impl/json_value.cpp b/src/ripple/json/impl/json_value.cpp index aaf8a23b2..024ad2c88 100644 --- a/src/ripple/json/impl/json_value.cpp +++ b/src/ripple/json/impl/json_value.cpp @@ -480,13 +480,13 @@ Value::asString() const return value_.bool_ ? "true" : "false"; case intValue: - return beast::lexicalCastThrow(value_.int_); + return std::to_string(value_.int_); case uintValue: - return beast::lexicalCastThrow(value_.uint_); + return std::to_string(value_.uint_); case realValue: - return beast::lexicalCastThrow(value_.real_); + return std::to_string(value_.real_); case arrayValue: case objectValue: diff --git a/src/ripple/ledger/detail/ReadViewFwdRange.h b/src/ripple/ledger/detail/ReadViewFwdRange.h index 3e7408568..af00944cf 100644 --- a/src/ripple/ledger/detail/ReadViewFwdRange.h +++ b/src/ripple/ledger/detail/ReadViewFwdRange.h @@ -141,10 +141,6 @@ public: ReadViewFwdRange& operator=(ReadViewFwdRange const&) = default; - // VFALCO Otherwise causes errors on clang - // private: - // friend class ReadView; - explicit ReadViewFwdRange(ReadView const& view) : view_(&view) { } diff --git a/src/ripple/net/InfoSub.h b/src/ripple/net/InfoSub.h index aaf6293d2..b1ef6e25b 100644 --- a/src/ripple/net/InfoSub.h +++ b/src/ripple/net/InfoSub.h @@ -40,12 +40,6 @@ class PathRequest; class InfoSub : public CountedObject { public: - static char const* - getCountedObjectName() - { - return "InfoSub"; - } - using pointer = std::shared_ptr; // VFALCO TODO Standardize on the names of weak / strong pointer type diff --git a/src/ripple/net/impl/HTTPClient.cpp b/src/ripple/net/impl/HTTPClient.cpp index 79779065c..c9f136cb0 100644 --- a/src/ripple/net/impl/HTTPClient.cpp +++ b/src/ripple/net/impl/HTTPClient.cpp @@ -144,7 +144,7 @@ public: auto query = std::make_shared( mDeqSites[0], - beast::lexicalCast(mPort), + std::to_string(mPort), boost::asio::ip::resolver_query_base::numeric_service); mQuery = query; diff --git a/src/ripple/net/impl/RPCCall.cpp b/src/ripple/net/impl/RPCCall.cpp index 815ccf0ac..9a79515a2 100644 --- a/src/ripple/net/impl/RPCCall.cpp +++ b/src/ripple/net/impl/RPCCall.cpp @@ -818,7 +818,7 @@ private: { // verify the channel id is a valid 256 bit number uint256 channelId; - if (!channelId.SetHexExact(jvParams[index].asString())) + if (!channelId.parseHex(jvParams[index].asString())) return rpcError(rpcCHANNEL_MALFORMED); jvRequest[jss::channel_id] = to_string(channelId); index++; @@ -850,7 +850,7 @@ private: { // verify the channel id is a valid 256 bit number uint256 channelId; - if (!channelId.SetHexExact(jvParams[1u].asString())) + if (!channelId.parseHex(jvParams[1u].asString())) return rpcError(rpcCHANNEL_MALFORMED); } jvRequest[jss::channel_id] = jvParams[1u].asString(); diff --git a/src/ripple/nodestore/NodeObject.h b/src/ripple/nodestore/NodeObject.h index 7c3316993..2bd73d8de 100644 --- a/src/ripple/nodestore/NodeObject.h +++ b/src/ripple/nodestore/NodeObject.h @@ -48,12 +48,6 @@ enum NodeObjectType : std::uint32_t { class NodeObject : public CountedObject { public: - static char const* - getCountedObjectName() - { - return "NodeObject"; - } - static constexpr std::size_t keyBytes = 32; private: diff --git a/src/ripple/nodestore/backend/RocksDBFactory.cpp b/src/ripple/nodestore/backend/RocksDBFactory.cpp index 4fc36b7e1..04b969990 100644 --- a/src/ripple/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBFactory.cpp @@ -376,8 +376,7 @@ public: { // Uh oh, corrupted data! JLOG(m_journal.fatal()) - << "Corrupt NodeObject #" - << from_hex_text(it->key().data()); + << "Corrupt NodeObject #" << it->key().ToString(true); } } else diff --git a/src/ripple/nodestore/impl/Shard.cpp b/src/ripple/nodestore/impl/Shard.cpp index a7f49bebe..6032bf3e8 100644 --- a/src/ripple/nodestore/impl/Shard.cpp +++ b/src/ripple/nodestore/impl/Shard.cpp @@ -671,7 +671,7 @@ Shard::finalize( if (!sHash) return fail("missing LastLedgerHash"); - if (!hash.SetHexExact(*sHash) || hash.isZero()) + if (!hash.parseHex(*sHash) || hash.isZero()) return fail("invalid LastLedgerHash"); if (blobPresent != soci::i_ok) @@ -1159,7 +1159,7 @@ Shard::storeSQLite( session << (STTx::getMetaSQLInsertReplaceHeader() + item.first->getMetaSQL( - ledgerSeq, sqlEscape(s.modData())) + + ledgerSeq, sqlBlobLiteral(s.modData())) + ';'); } } diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index ae373801a..7a2834dd4 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -135,7 +135,7 @@ PeerImp::run() auto parseLedgerHash = [](std::string const& value) -> boost::optional { - if (uint256 ret; ret.SetHexExact(value)) + if (uint256 ret; ret.parseHex(value)) return ret; if (auto const s = base64_decode(value); s.size() == uint256::size()) diff --git a/src/ripple/protocol/AccountID.h b/src/ripple/protocol/AccountID.h index f8393bc26..e105084c8 100644 --- a/src/ripple/protocol/AccountID.h +++ b/src/ripple/protocol/AccountID.h @@ -57,29 +57,6 @@ template <> boost::optional parseBase58(std::string const& s); -// Compatibility with legacy code -bool -deprecatedParseBase58(AccountID& account, Json::Value const& jv); - -/** Parse AccountID from hexadecimal string - - If the string is not exactly 40 - hex digits, boost::none is returned. - - @return boost::none if a parse error occurs -*/ -template <> -boost::optional -parseHex(std::string const& s); - -/** Parse AccountID from hex or checked base58 string. - - @return boost::none if a parse error occurs -*/ -template <> -boost::optional -parseHexOrBase58(std::string const& s); - /** Compute AccountID from public key. The account ID is computed as the 160-bit hash of the diff --git a/src/ripple/protocol/STArray.h b/src/ripple/protocol/STArray.h index ad62b5af6..12c698649 100644 --- a/src/ripple/protocol/STArray.h +++ b/src/ripple/protocol/STArray.h @@ -33,12 +33,6 @@ private: list_type v_; public: - static char const* - getCountedObjectName() - { - return "STArray"; - } - using size_type = list_type::size_type; using iterator = list_type::iterator; using const_iterator = list_type::const_iterator; diff --git a/src/ripple/protocol/STBitString.h b/src/ripple/protocol/STBitString.h index a4a847d0b..06ace27d9 100644 --- a/src/ripple/protocol/STBitString.h +++ b/src/ripple/protocol/STBitString.h @@ -45,16 +45,6 @@ public: { } - STBitString(SField const& n, const char* v) : STBase(n) - { - value_.SetHex(v); - } - - STBitString(SField const& n, std::string const& v) : STBase(n) - { - value_.SetHex(v); - } - STBitString(SerialIter& sit, SField const& name) : STBitString(name, sit.getBitString()) { diff --git a/src/ripple/protocol/STLedgerEntry.h b/src/ripple/protocol/STLedgerEntry.h index 1ddb01220..0a0e3a805 100644 --- a/src/ripple/protocol/STLedgerEntry.h +++ b/src/ripple/protocol/STLedgerEntry.h @@ -32,12 +32,6 @@ class STLedgerEntry final : public STObject, public CountedObject friend Invariants_test; // this test wants access to the private type_ public: - static char const* - getCountedObjectName() - { - return "STLedgerEntry"; - } - using pointer = std::shared_ptr; using ref = const std::shared_ptr&; diff --git a/src/ripple/protocol/STObject.h b/src/ripple/protocol/STObject.h index 4ac67ded5..ad63dcc39 100644 --- a/src/ripple/protocol/STObject.h +++ b/src/ripple/protocol/STObject.h @@ -260,12 +260,6 @@ public: using std::runtime_error::runtime_error; }; - static char const* - getCountedObjectName() - { - return "STObject"; - } - STObject(STObject&&); STObject(STObject const&) = default; STObject(const SOTemplate& type, SField const& name); diff --git a/src/ripple/protocol/STTx.h b/src/ripple/protocol/STTx.h index cecbd2c0a..0d90b2324 100644 --- a/src/ripple/protocol/STTx.h +++ b/src/ripple/protocol/STTx.h @@ -42,12 +42,6 @@ enum TxnSql : char { class STTx final : public STObject, public CountedObject { public: - static char const* - getCountedObjectName() - { - return "STTx"; - } - static std::size_t const minMultiSigners = 1; static std::size_t const maxMultiSigners = 8; diff --git a/src/ripple/protocol/STValidation.h b/src/ripple/protocol/STValidation.h index be1537ad0..866d1e785 100644 --- a/src/ripple/protocol/STValidation.h +++ b/src/ripple/protocol/STValidation.h @@ -43,12 +43,6 @@ constexpr std::uint32_t vfFullyCanonicalSig = 0x80000000; class STValidation final : public STObject, public CountedObject { public: - static char const* - getCountedObjectName() - { - return "STValidation"; - } - /** Construct a STValidation from a peer. Construct a STValidation from serialized data previously shared by a diff --git a/src/ripple/protocol/Serializer.h b/src/ripple/protocol/Serializer.h index 812517f25..e51e1bb14 100644 --- a/src/ripple/protocol/Serializer.h +++ b/src/ripple/protocol/Serializer.h @@ -411,15 +411,18 @@ template base_uint SerialIter::getBitString() { - base_uint u; auto const n = Bits / 8; + if (remain_ < n) Throw("invalid SerialIter getBitString"); - std::memcpy(u.begin(), p_, n); + + auto const x = p_; + p_ += n; used_ += n; remain_ -= n; - return u; + + return base_uint::fromVoid(x); } } // namespace ripple diff --git a/src/ripple/protocol/digest.h b/src/ripple/protocol/digest.h index bd6190f18..6507057dc 100644 --- a/src/ripple/protocol/digest.h +++ b/src/ripple/protocol/digest.h @@ -21,21 +21,20 @@ #define RIPPLE_PROTOCOL_DIGEST_H_INCLUDED #include -#include -#include -#include +#include +#include #include #include namespace ripple { -/* Message digest functions used in the Ripple Protocol +/** Message digest functions used in the codebase - Modeled to meet the requirements of `Hasher` in the - `hash_append` interface, currently in proposal: + @note These are modeled to meet the requirements of `Hasher` in the + `hash_append` interface, discussed in proposal: - N3980 "Types Don't Know #" - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.html + N3980 "Types Don't Know #" + http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.html */ //------------------------------------------------------------------------------ @@ -47,7 +46,7 @@ namespace ripple { struct openssl_ripemd160_hasher { public: - static beast::endian const endian = beast::endian::native; + static constexpr auto const endian = boost::endian::order::native; using result_type = std::array; @@ -69,7 +68,7 @@ private: struct openssl_sha512_hasher { public: - static beast::endian const endian = beast::endian::native; + static constexpr auto const endian = boost::endian::order::native; using result_type = std::array; @@ -91,7 +90,7 @@ private: struct openssl_sha256_hasher { public: - static beast::endian const endian = beast::endian::native; + static constexpr auto const endian = boost::endian::order::native; using result_type = std::array; @@ -108,17 +107,9 @@ private: //------------------------------------------------------------------------------ -// Aliases to choose the correct digest implementation - -#if USE_BEAST_HASHER -using ripemd160_hasher = beast::ripemd160_hasher; -using sha256_hasher = beast::sha256_hasher; -using sha512_hasher = beast::sha512_hasher; -#else using ripemd160_hasher = openssl_ripemd160_hasher; using sha256_hasher = openssl_sha256_hasher; using sha512_hasher = openssl_sha512_hasher; -#endif //------------------------------------------------------------------------------ @@ -143,7 +134,7 @@ private: sha256_hasher h_; public: - static beast::endian const endian = beast::endian::native; + static constexpr auto const endian = boost::endian::order::native; using result_type = std::array; @@ -178,7 +169,7 @@ private: sha512_hasher h_; public: - static beast::endian const endian = beast::endian::big; + static constexpr auto const endian = boost::endian::order::big; using result_type = uint256; @@ -196,9 +187,7 @@ public: explicit operator result_type() noexcept { auto const digest = sha512_hasher::result_type(h_); - result_type result; - std::copy(digest.begin(), digest.begin() + 32, result.begin()); - return result; + return result_type::fromVoid(digest.data()); } private: diff --git a/src/ripple/protocol/impl/AccountID.cpp b/src/ripple/protocol/impl/AccountID.cpp index 471935135..5f9b7a1b7 100644 --- a/src/ripple/protocol/impl/AccountID.cpp +++ b/src/ripple/protocol/impl/AccountID.cpp @@ -36,47 +36,9 @@ boost::optional parseBase58(std::string const& s) { auto const result = decodeBase58Token(s, TokenType::AccountID); - if (result.empty()) + if (result.size() != AccountID::bytes) return boost::none; - AccountID id; - if (result.size() != id.size()) - return boost::none; - std::memcpy(id.data(), result.data(), result.size()); - return id; -} - -bool -deprecatedParseBase58(AccountID& account, Json::Value const& jv) -{ - if (!jv.isString()) - return false; - auto const result = parseBase58(jv.asString()); - if (!result) - return false; - account = *result; - return true; -} - -template <> -boost::optional -parseHex(std::string const& s) -{ - if (s.size() != 40) - return boost::none; - AccountID id; - if (!id.SetHex(s, true)) - return boost::none; - return id; -} - -template <> -boost::optional -parseHexOrBase58(std::string const& s) -{ - auto result = parseHex(s); - if (!result) - result = parseBase58(s); - return result; + return AccountID{result}; } //------------------------------------------------------------------------------ @@ -117,13 +79,11 @@ parseHexOrBase58(std::string const& s) AccountID calcAccountID(PublicKey const& pk) { + static_assert(AccountID::bytes == sizeof(ripesha_hasher::result_type)); + ripesha_hasher rsh; rsh(pk.data(), pk.size()); - auto const d = static_cast(rsh); - AccountID id; - static_assert(sizeof(d) == id.size(), ""); - std::memcpy(id.data(), d.data(), d.size()); - return id; + return AccountID{static_cast(rsh)}; } AccountID const& @@ -143,11 +103,8 @@ noAccount() bool to_issuer(AccountID& issuer, std::string const& s) { - if (s.size() == (160 / 4)) - { - issuer.SetHex(s); + if (issuer.parseHex(s)) return true; - } auto const account = parseBase58(s); if (!account) return false; diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index 20174d978..02148a5f1 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -96,8 +96,13 @@ getBookBase(Book const& book) uint256 getQualityNext(uint256 const& uBase) { - static uint256 const uNext(from_hex_text("10000000000000000")); - return uBase + uNext; + static uint256 const nextq = []() { + uint256 x; + (void)x.parseHex( + "0000000000000000000000000000000000000000000000010000000000000000"); + return x; + }(); + return uBase + nextq; } std::uint64_t diff --git a/src/ripple/protocol/impl/PublicKey.cpp b/src/ripple/protocol/impl/PublicKey.cpp index 4dfdc312b..0469c6ed6 100644 --- a/src/ripple/protocol/impl/PublicKey.cpp +++ b/src/ripple/protocol/impl/PublicKey.cpp @@ -298,13 +298,11 @@ verify( NodeID calcNodeID(PublicKey const& pk) { + static_assert(NodeID::bytes == sizeof(ripesha_hasher::result_type)); + ripesha_hasher h; h(pk.data(), pk.size()); - auto const digest = static_cast(h); - static_assert(NodeID::bytes == sizeof(ripesha_hasher::result_type), ""); - NodeID result; - std::memcpy(result.data(), digest.data(), digest.size()); - return result; + return NodeID{static_cast(h)}; } } // namespace ripple diff --git a/src/ripple/protocol/impl/STBase.cpp b/src/ripple/protocol/impl/STBase.cpp index 2c7d4b4f7..f447b1fe2 100644 --- a/src/ripple/protocol/impl/STBase.cpp +++ b/src/ripple/protocol/impl/STBase.cpp @@ -36,16 +36,6 @@ STBase::STBase(SField const& n) : fName(&n) STBase& STBase::operator=(const STBase& t) { - if ((t.fName != fName) && fName->isUseful() && t.fName->isUseful()) - { - // VFALCO We shouldn't be logging at this low a level - /* - WriteLog ((t.getSType () == STI_AMOUNT) ? lsTRACE : lsWARNING, STBase) - // This is common for amounts - << "Caution: " << t.fName->getName () << " not replacing " << - fName->getName (); - */ - } if (!fName->isUseful()) fName = t.fName; return *this; @@ -110,12 +100,7 @@ bool STBase::isEquivalent(const STBase& t) const { assert(getSType() == STI_NOTPRESENT); - if (t.getSType() == STI_NOTPRESENT) - return true; - // VFALCO We shouldn't be logging at this low a level - // WriteLog (lsDEBUG, STBase) << "notEquiv " << getFullText() << " not - // STI_NOTPRESENT"; - return false; + return t.getSType() == STI_NOTPRESENT; } bool diff --git a/src/ripple/protocol/impl/STInteger.cpp b/src/ripple/protocol/impl/STInteger.cpp index dc580388d..2b154e369 100644 --- a/src/ripple/protocol/impl/STInteger.cpp +++ b/src/ripple/protocol/impl/STInteger.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace ripple { @@ -56,7 +57,7 @@ STUInt8::getText() const << "Unknown result code in metadata: " << value_; } - return beast::lexicalCastThrow(value_); + return std::to_string(value_); } template <> @@ -113,7 +114,7 @@ STUInt16::getText() const return item->getName(); } - return beast::lexicalCastThrow(value_); + return std::to_string(value_); } template <> @@ -159,7 +160,7 @@ template <> std::string STUInt32::getText() const { - return beast::lexicalCastThrow(value_); + return std::to_string(value_); } template <> @@ -187,13 +188,17 @@ template <> std::string STUInt64::getText() const { - return beast::lexicalCastThrow(value_); + return std::to_string(value_); } template <> Json::Value STUInt64::getJson(JsonOptions) const { - return strHex(value_); + std::string str(16, 0); + auto ret = std::to_chars(str.data(), str.data() + str.size(), value_, 16); + assert(ret.ec == std::errc()); + str.resize(std::distance(str.data(), ret.ptr)); + return str; } } // namespace ripple diff --git a/src/ripple/protocol/impl/STLedgerEntry.cpp b/src/ripple/protocol/impl/STLedgerEntry.cpp index 6211e3e7f..9c0ac3f51 100644 --- a/src/ripple/protocol/impl/STLedgerEntry.cpp +++ b/src/ripple/protocol/impl/STLedgerEntry.cpp @@ -25,22 +25,29 @@ #include #include #include +#include namespace ripple { STLedgerEntry::STLedgerEntry(Keylet const& k) : STObject(sfLedgerEntry), key_(k.key), type_(k.type) { - if (!(0u <= type_ && - type_ <= std::min( - std::numeric_limits::max(), - std::numeric_limits< - std::underlying_type_t>::max()))) + // The on-ledger representation of a key type is a 16-bit unsigned integer + // but the LedgerEntryType enum has a larger range (including negative + // values), so catch obviously wrong values: + constexpr auto const minValidValue = + static_cast(std::numeric_limits::min()); + + constexpr auto const maxValidValue = + static_cast(std::numeric_limits::max()); + + if (type_ < minValidValue || type_ > maxValidValue) Throw("invalid ledger entry type: out of range"); + auto const format = LedgerFormats::getInstance().findByType(type_); if (format == nullptr) - Throw("invalid ledger entry type"); + Throw("unknown ledger entry type"); set(format->getSOTemplate()); diff --git a/src/ripple/protocol/impl/STParsedJSON.cpp b/src/ripple/protocol/impl/STParsedJSON.cpp index 7baad35a9..da5e53b33 100644 --- a/src/ripple/protocol/impl/STParsedJSON.cpp +++ b/src/ripple/protocol/impl/STParsedJSON.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include namespace ripple { @@ -403,8 +404,17 @@ parseLeaf( { if (value.isString()) { - ret = detail::make_stvar( - field, uintFromHex(value.asString())); + auto const str = value.asString(); + + std::uint64_t val; + + auto [p, ec] = std::from_chars( + str.data(), str.data() + str.size(), val, 16); + + if (ec != std::errc() || (p != str.data() + str.size())) + Throw("invalid data"); + + ret = detail::make_stvar(field, val); } else if (value.isInt()) { @@ -430,71 +440,77 @@ parseLeaf( break; - case STI_HASH128: - try + case STI_HASH128: { + if (!value.isString()) { - if (value.isString()) - { - ret = - detail::make_stvar(field, value.asString()); - } - else - { - error = bad_type(json_name, fieldName); - return ret; - } - } - catch (std::exception const&) - { - error = invalid_data(json_name, fieldName); + error = bad_type(json_name, fieldName); return ret; } - break; + uint128 num; - case STI_HASH160: - try + if (auto const s = value.asString(); !num.parseHex(s)) { - if (value.isString()) + if (!s.empty()) { - ret = - detail::make_stvar(field, value.asString()); - } - else - { - error = bad_type(json_name, fieldName); + error = invalid_data(json_name, fieldName); return ret; } + + num.zero(); } - catch (std::exception const&) + + ret = detail::make_stvar(field, num); + break; + } + + case STI_HASH160: { + if (!value.isString()) { - error = invalid_data(json_name, fieldName); + error = bad_type(json_name, fieldName); return ret; } - break; + uint160 num; - case STI_HASH256: - try + if (auto const s = value.asString(); !num.parseHex(s)) { - if (value.isString()) + if (!s.empty()) { - ret = - detail::make_stvar(field, value.asString()); - } - else - { - error = bad_type(json_name, fieldName); + error = invalid_data(json_name, fieldName); return ret; } + + num.zero(); } - catch (std::exception const&) + + ret = detail::make_stvar(field, num); + break; + } + + case STI_HASH256: { + if (!value.isString()) { - error = invalid_data(json_name, fieldName); + error = bad_type(json_name, fieldName); return ret; } + uint256 num; + + if (auto const s = value.asString(); !num.parseHex(s)) + { + if (!s.empty()) + { + error = invalid_data(json_name, fieldName); + return ret; + } + + num.zero(); + } + + ret = detail::make_stvar(field, num); break; + } case STI_VL: if (!value.isString()) @@ -550,7 +566,8 @@ parseLeaf( for (Json::UInt i = 0; value.isValidIndex(i); ++i) { uint256 s; - s.SetHex(value[i].asString()); + if (!s.parseHex(value[i].asString())) + Throw("invalid data"); tail.push_back(s); } ret = detail::make_stvar(std::move(tail)); @@ -623,7 +640,7 @@ parseLeaf( // If we have what looks like a 160-bit hex value, // we set it, otherwise, we assume it's an AccountID - if (!uAccount.SetHexExact(account.asString())) + if (!uAccount.parseHex(account.asString())) { auto const a = parseBase58(account.asString()); @@ -649,7 +666,7 @@ parseLeaf( hasCurrency = true; - if (!uCurrency.SetHexExact(currency.asString())) + if (!uCurrency.parseHex(currency.asString())) { if (!to_currency( uCurrency, currency.asString())) @@ -670,7 +687,7 @@ parseLeaf( return ret; } - if (!uIssuer.SetHexExact(issuer.asString())) + if (!uIssuer.parseHex(issuer.asString())) { auto const a = parseBase58(issuer.asString()); @@ -711,14 +728,14 @@ parseLeaf( try { - // VFALCO This needs careful auditing - auto const account = parseHexOrBase58(strValue); - if (!account) - { - error = invalid_data(json_name, fieldName); - return ret; - } - ret = detail::make_stvar(field, *account); + if (AccountID account; account.parseHex(strValue)) + return detail::make_stvar(field, account); + + if (auto result = parseBase58(strValue)) + return detail::make_stvar(field, *result); + + error = invalid_data(json_name, fieldName); + return ret; } catch (std::exception const&) { diff --git a/src/ripple/protocol/impl/STPathSet.cpp b/src/ripple/protocol/impl/STPathSet.cpp index 2825c7cfb..141fad329 100644 --- a/src/ripple/protocol/impl/STPathSet.cpp +++ b/src/ripple/protocol/impl/STPathSet.cpp @@ -156,7 +156,6 @@ Json::Value STPath::getJson(JsonOptions) const auto const iType = it.getNodeType(); elem[jss::type] = iType; - elem[jss::type_hex] = strHex(iType); if (iType & STPathElement::typeAccount) elem[jss::account] = to_string(it.getAccountID()); diff --git a/src/ripple/protocol/impl/STTx.cpp b/src/ripple/protocol/impl/STTx.cpp index 14bffddea..6e1b86f1f 100644 --- a/src/ripple/protocol/impl/STTx.cpp +++ b/src/ripple/protocol/impl/STTx.cpp @@ -259,7 +259,7 @@ STTx::getMetaSQL( { static boost::format bfTrans( "('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)"); - std::string rTxn = sqlEscape(rawTxn.peekData()); + std::string rTxn = sqlBlobLiteral(rawTxn.peekData()); auto format = TxFormats::getInstance().findByType(tx_type_); assert(format != nullptr); diff --git a/src/ripple/protocol/impl/Seed.cpp b/src/ripple/protocol/impl/Seed.cpp index 02c1ad2e3..f38556c2e 100644 --- a/src/ripple/protocol/impl/Seed.cpp +++ b/src/ripple/protocol/impl/Seed.cpp @@ -104,7 +104,7 @@ parseGenericSeed(std::string const& str) { uint128 seed; - if (seed.SetHexExact(str)) + if (seed.parseHex(str)) return Seed{Slice(seed.data(), seed.size())}; } diff --git a/src/ripple/protocol/impl/UintTypes.cpp b/src/ripple/protocol/impl/UintTypes.cpp index 698555b9a..39e25a813 100644 --- a/src/ripple/protocol/impl/UintTypes.cpp +++ b/src/ripple/protocol/impl/UintTypes.cpp @@ -55,8 +55,11 @@ to_string(Currency const& currency) if (currency == noCurrency()) return "1"; - static Currency const sIsoBits( - from_hex_text("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF")); + static Currency const sIsoBits = []() { + Currency c; + (void)c.parseHex("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF"); + return c; + }(); if ((currency & sIsoBits).isZero()) { @@ -105,7 +108,7 @@ to_currency(Currency& currency, std::string const& code) return true; } - return currency.SetHexExact(code); + return currency.parseHex(code); } Currency diff --git a/src/ripple/protocol/tokens.h b/src/ripple/protocol/tokens.h index 1da8ca993..d507c2542 100644 --- a/src/ripple/protocol/tokens.h +++ b/src/ripple/protocol/tokens.h @@ -45,14 +45,6 @@ template boost::optional parseBase58(TokenType type, std::string const& s); -template -boost::optional -parseHex(std::string const& s); - -template -boost::optional -parseHexOrBase58(std::string const& s); - /** Encode data in Base58Check format using XRPL alphabet For details on the format see diff --git a/src/ripple/rpc/handlers/AccountChannels.cpp b/src/ripple/rpc/handlers/AccountChannels.cpp index b1c85cc80..d222e4c72 100644 --- a/src/ripple/rpc/handlers/AccountChannels.cpp +++ b/src/ripple/rpc/handlers/AccountChannels.cpp @@ -121,7 +121,7 @@ doAccountChannels(RPC::JsonContext& context) if (!marker.isString()) return RPC::expected_field_error(jss::marker, "string"); - if (!startAfter.SetHex(marker.asString())) + if (!startAfter.parseHex(marker.asString())) { return rpcError(rpcINVALID_PARAMS); } diff --git a/src/ripple/rpc/handlers/AccountLines.cpp b/src/ripple/rpc/handlers/AccountLines.cpp index 947b0a3be..2e1dac1d7 100644 --- a/src/ripple/rpc/handlers/AccountLines.cpp +++ b/src/ripple/rpc/handlers/AccountLines.cpp @@ -139,7 +139,9 @@ doAccountLines(RPC::JsonContext& context) if (!marker.isString()) return RPC::expected_field_error(jss::marker, "string"); - startAfter.SetHex(marker.asString()); + if (!startAfter.parseHex(marker.asString())) + return rpcError(rpcINVALID_PARAMS); + auto const sleLine = ledger->read({ltRIPPLE_STATE, startAfter}); if (!sleLine) diff --git a/src/ripple/rpc/handlers/AccountObjects.cpp b/src/ripple/rpc/handlers/AccountObjects.cpp index 464031e93..1783c89da 100644 --- a/src/ripple/rpc/handlers/AccountObjects.cpp +++ b/src/ripple/rpc/handlers/AccountObjects.cpp @@ -134,13 +134,13 @@ doAccountObjects(RPC::JsonContext& context) if (!std::getline(ss, s, ',')) return RPC::invalid_field_error(jss::marker); - if (!dirIndex.SetHex(s)) + if (!dirIndex.parseHex(s)) return RPC::invalid_field_error(jss::marker); if (!std::getline(ss, s, ',')) return RPC::invalid_field_error(jss::marker); - if (!entryIndex.SetHex(s)) + if (!entryIndex.parseHex(s)) return RPC::invalid_field_error(jss::marker); } diff --git a/src/ripple/rpc/handlers/AccountOffers.cpp b/src/ripple/rpc/handlers/AccountOffers.cpp index 983641729..063d08743 100644 --- a/src/ripple/rpc/handlers/AccountOffers.cpp +++ b/src/ripple/rpc/handlers/AccountOffers.cpp @@ -101,7 +101,9 @@ doAccountOffers(RPC::JsonContext& context) if (!marker.isString()) return RPC::expected_field_error(jss::marker, "string"); - startAfter.SetHex(marker.asString()); + if (!startAfter.parseHex(marker.asString())) + return rpcError(rpcINVALID_PARAMS); + auto const sleOffer = ledger->read({ltOFFER, startAfter}); if (!sleOffer || accountID != sleOffer->getAccountID(sfAccount)) diff --git a/src/ripple/rpc/handlers/AccountTx.cpp b/src/ripple/rpc/handlers/AccountTx.cpp index ffcbe145f..104f55535 100644 --- a/src/ripple/rpc/handlers/AccountTx.cpp +++ b/src/ripple/rpc/handlers/AccountTx.cpp @@ -165,7 +165,7 @@ parseLedgerArgs(Json::Value const& params) } LedgerHash hash; - if (!hash.SetHex(hashValue.asString())) + if (!hash.parseHex(hashValue.asString())) { RPC::Status status{rpcINVALID_PARAMS, "ledgerHashMalformed"}; status.inject(response); diff --git a/src/ripple/rpc/handlers/CanDelete.cpp b/src/ripple/rpc/handlers/CanDelete.cpp index d302a2f1b..56015375a 100644 --- a/src/ripple/rpc/handlers/CanDelete.cpp +++ b/src/ripple/rpc/handlers/CanDelete.cpp @@ -71,13 +71,9 @@ doCanDelete(RPC::JsonContext& context) if (!canDeleteSeq) return RPC::make_error(rpcNOT_READY); } - else if ( - canDeleteStr.size() == 64 && - canDeleteStr.find_first_not_of("0123456789abcdef") == - std::string::npos) + else if (uint256 lh; lh.parseHex(canDeleteStr)) { - auto ledger = context.ledgerMaster.getLedgerByHash( - from_hex_text(canDeleteStr)); + auto ledger = context.ledgerMaster.getLedgerByHash(lh); if (!ledger) return RPC::make_error(rpcLGR_NOT_FOUND, "ledgerNotFound"); diff --git a/src/ripple/rpc/handlers/Feature1.cpp b/src/ripple/rpc/handlers/Feature1.cpp index 47a284f30..086d6acd2 100644 --- a/src/ripple/rpc/handlers/Feature1.cpp +++ b/src/ripple/rpc/handlers/Feature1.cpp @@ -60,8 +60,7 @@ doFeature(RPC::JsonContext& context) auto feature = table.find(context.params[jss::feature].asString()); - if (!feature && - !feature.SetHexExact(context.params[jss::feature].asString())) + if (!feature && !feature.parseHex(context.params[jss::feature].asString())) return rpcError(rpcBAD_FEATURE); if (context.params.isMember(jss::vetoed)) diff --git a/src/ripple/rpc/handlers/LedgerData.cpp b/src/ripple/rpc/handlers/LedgerData.cpp index 9bad14854..4cb0b4d46 100644 --- a/src/ripple/rpc/handlers/LedgerData.cpp +++ b/src/ripple/rpc/handlers/LedgerData.cpp @@ -55,7 +55,7 @@ doLedgerData(RPC::JsonContext& context) if (isMarker) { Json::Value const& jMarker = params[jss::marker]; - if (!(jMarker.isString() && key.SetHex(jMarker.asString()))) + if (!(jMarker.isString() && key.parseHex(jMarker.asString()))) return RPC::expected_field_error(jss::marker, "valid"); } diff --git a/src/ripple/rpc/handlers/LedgerEntry.cpp b/src/ripple/rpc/handlers/LedgerEntry.cpp index 6d0fbbdb0..933c61f23 100644 --- a/src/ripple/rpc/handlers/LedgerEntry.cpp +++ b/src/ripple/rpc/handlers/LedgerEntry.cpp @@ -50,7 +50,11 @@ doLedgerEntry(RPC::JsonContext& context) if (context.params.isMember(jss::index)) { - uNodeIndex.SetHex(context.params[jss::index].asString()); + if (!uNodeIndex.parseHex(context.params[jss::index].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if (context.params.isMember(jss::account_root)) { @@ -65,7 +69,12 @@ doLedgerEntry(RPC::JsonContext& context) else if (context.params.isMember(jss::check)) { expectedType = ltCHECK; - uNodeIndex.SetHex(context.params[jss::check].asString()); + + if (!uNodeIndex.parseHex(context.params[jss::check].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if (context.params.isMember(jss::deposit_preauth)) { @@ -74,7 +83,7 @@ doLedgerEntry(RPC::JsonContext& context) if (!context.params[jss::deposit_preauth].isObject()) { if (!context.params[jss::deposit_preauth].isString() || - !uNodeIndex.SetHex( + !uNodeIndex.parseHex( context.params[jss::deposit_preauth].asString())) { uNodeIndex = beast::zero; @@ -115,7 +124,11 @@ doLedgerEntry(RPC::JsonContext& context) } else if (!context.params[jss::directory].isObject()) { - uNodeIndex.SetHex(context.params[jss::directory].asString()); + if (!uNodeIndex.parseHex(context.params[jss::directory].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if ( context.params[jss::directory].isMember(jss::sub_index) && @@ -132,18 +145,22 @@ doLedgerEntry(RPC::JsonContext& context) if (context.params[jss::directory].isMember(jss::dir_root)) { + uint256 uDirRoot; + if (context.params[jss::directory].isMember(jss::owner)) { // May not specify both dir_root and owner. jvResult[jss::error] = "malformedRequest"; } + else if (!uDirRoot.parseHex( + context.params[jss::directory][jss::dir_root] + .asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } else { - uint256 uDirRoot; - uDirRoot.SetHex( - context.params[jss::directory][jss::dir_root] - .asString()); - uNodeIndex = keylet::page(uDirRoot, uSubIndex).key; } } @@ -173,7 +190,11 @@ doLedgerEntry(RPC::JsonContext& context) expectedType = ltESCROW; if (!context.params[jss::escrow].isObject()) { - uNodeIndex.SetHex(context.params[jss::escrow].asString()); + if (!uNodeIndex.parseHex(context.params[jss::escrow].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if ( !context.params[jss::escrow].isMember(jss::owner) || @@ -200,7 +221,11 @@ doLedgerEntry(RPC::JsonContext& context) expectedType = ltOFFER; if (!context.params[jss::offer].isObject()) { - uNodeIndex.SetHex(context.params[jss::offer].asString()); + if (!uNodeIndex.parseHex(context.params[jss::offer].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if ( !context.params[jss::offer].isMember(jss::account) || @@ -225,7 +250,13 @@ doLedgerEntry(RPC::JsonContext& context) else if (context.params.isMember(jss::payment_channel)) { expectedType = ltPAYCHAN; - uNodeIndex.SetHex(context.params[jss::payment_channel].asString()); + + if (!uNodeIndex.parseHex( + context.params[jss::payment_channel].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if (context.params.isMember(jss::ripple_state)) { @@ -271,7 +302,11 @@ doLedgerEntry(RPC::JsonContext& context) expectedType = ltTICKET; if (!context.params[jss::ticket].isObject()) { - uNodeIndex.SetHex(context.params[jss::ticket].asString()); + if (!uNodeIndex.parseHex(context.params[jss::ticket].asString())) + { + uNodeIndex = beast::zero; + jvResult[jss::error] = "malformedRequest"; + } } else if ( !context.params[jss::ticket].isMember(jss::account) || diff --git a/src/ripple/rpc/handlers/LedgerRequest.cpp b/src/ripple/rpc/handlers/LedgerRequest.cpp index 5d2d11b00..dfb9d099f 100644 --- a/src/ripple/rpc/handlers/LedgerRequest.cpp +++ b/src/ripple/rpc/handlers/LedgerRequest.cpp @@ -55,7 +55,7 @@ doLedgerRequest(RPC::JsonContext& context) if (hasHash) { auto const& jsonHash = context.params[jss::ledger_hash]; - if (!jsonHash.isString() || !ledgerHash.SetHex(jsonHash.asString())) + if (!jsonHash.isString() || !ledgerHash.parseHex(jsonHash.asString())) return RPC::invalid_field_error(jss::ledger_hash); } else diff --git a/src/ripple/rpc/handlers/PayChanClaim.cpp b/src/ripple/rpc/handlers/PayChanClaim.cpp index ddc09f8aa..9c46893dd 100644 --- a/src/ripple/rpc/handlers/PayChanClaim.cpp +++ b/src/ripple/rpc/handlers/PayChanClaim.cpp @@ -60,7 +60,7 @@ doChannelAuthorize(RPC::JsonContext& context) return result; uint256 channelId; - if (!channelId.SetHexExact(params[jss::channel_id].asString())) + if (!channelId.parseHex(params[jss::channel_id].asString())) return rpcError(rpcCHANNEL_MALFORMED); boost::optional const optDrops = @@ -122,7 +122,7 @@ doChannelVerify(RPC::JsonContext& context) } uint256 channelId; - if (!channelId.SetHexExact(params[jss::channel_id].asString())) + if (!channelId.parseHex(params[jss::channel_id].asString())) return rpcError(rpcCHANNEL_MALFORMED); boost::optional const optDrops = diff --git a/src/ripple/rpc/handlers/TransactionEntry.cpp b/src/ripple/rpc/handlers/TransactionEntry.cpp index 3731b6063..83c4d18a2 100644 --- a/src/ripple/rpc/handlers/TransactionEntry.cpp +++ b/src/ripple/rpc/handlers/TransactionEntry.cpp @@ -57,7 +57,11 @@ doTransactionEntry(RPC::JsonContext& context) uint256 uTransID; // XXX Relying on trusted WSS client. Would be better to have a strict // routine, returning success or failure. - uTransID.SetHex(context.params[jss::tx_hash].asString()); + if (!uTransID.parseHex(context.params[jss::tx_hash].asString())) + { + jvResult[jss::error] = "malformedRequest"; + return jvResult; + } auto [sttx, stobj] = lpLedger->txRead(uTransID); if (!sttx) diff --git a/src/ripple/rpc/handlers/Tx.cpp b/src/ripple/rpc/handlers/Tx.cpp index e5ab4b8dd..9db86b1e1 100644 --- a/src/ripple/rpc/handlers/Tx.cpp +++ b/src/ripple/rpc/handlers/Tx.cpp @@ -37,20 +37,6 @@ namespace ripple { // transaction: // } -static bool -isHexTxID(std::string const& txid) -{ - if (txid.size() != 64) - return false; - - auto const ret = - std::find_if(txid.begin(), txid.end(), [](std::string::value_type c) { - return !std::isxdigit(static_cast(c)); - }); - - return (ret == txid.end()); -} - static bool isValidated(LedgerMaster& ledgerMaster, std::uint32_t seq, uint256 const& hash) { @@ -326,12 +312,10 @@ doTxJson(RPC::JsonContext& context) if (!context.params.isMember(jss::transaction)) return rpcError(rpcINVALID_PARAMS); - std::string txHash = context.params[jss::transaction].asString(); - if (!isHexTxID(txHash)) - return rpcError(rpcNOT_IMPL); - TxArgs args; - args.hash = from_hex_text(txHash); + + if (!args.hash.parseHex(context.params[jss::transaction].asString())) + return rpcError(rpcNOT_IMPL); args.binary = context.params.isMember(jss::binary) && context.params[jss::binary].asBool(); diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index 6a415477a..4aac4e6a5 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -221,7 +221,7 @@ ledgerFromRequest(T& ledger, JsonContext& context) return {rpcINVALID_PARAMS, "ledgerHashNotString"}; uint256 ledgerHash; - if (!ledgerHash.SetHex(hashValue.asString())) + if (!ledgerHash.parseHex(hashValue.asString())) return {rpcINVALID_PARAMS, "ledgerHashMalformed"}; return getLedger(ledger, ledgerHash, context); } @@ -646,7 +646,7 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error) { uint128 s; - if (s.SetHexExact(fieldContents)) + if (s.parseHex(fieldContents)) seed.emplace(Slice(s.data(), s.size())); } diff --git a/src/ripple/shamap/impl/SHAMapTreeNode.cpp b/src/ripple/shamap/impl/SHAMapTreeNode.cpp index acc268ac2..e05f9c3dd 100644 --- a/src/ripple/shamap/impl/SHAMapTreeNode.cpp +++ b/src/ripple/shamap/impl/SHAMapTreeNode.cpp @@ -513,7 +513,7 @@ SHAMapTreeNode::getString(const SHAMapNodeID& id) const ret += "\n Hash="; ret += to_string(mHash); ret += "/"; - ret += beast::lexicalCast(mItem->size()); + ret += std::to_string(mItem->size()); return ret; } diff --git a/src/test/app/Manifest_test.cpp b/src/test/app/Manifest_test.cpp index 18460ce36..8dbc92ed7 100644 --- a/src/test/app/Manifest_test.cpp +++ b/src/test/app/Manifest_test.cpp @@ -602,14 +602,29 @@ public: std::uint32_t sequence = 0; // public key with invalid type - auto const ret = strUnHex( - "9930E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD02" - "0"); - auto const badKey = Slice{ret->data(), ret->size()}; + std::array const badKey{ + 0x99, 0x30, 0xE7, 0xFC, 0x9D, 0x56, 0xBB, 0x25, 0xD6, 0x89, 0x3B, + 0xA3, 0xF3, 0x17, 0xAE, 0x5B, 0xCF, 0x33, 0xB3, 0x29, 0x1B, 0xD6, + 0x3D, 0xB3, 0x26, 0x54, 0xA3, 0x13, 0x22, 0x2F, 0x7F, 0xD0, 0x20}; - // short public key - auto const retShort = strUnHex("0330"); - auto const shortKey = Slice{retShort->data(), retShort->size()}; + // Short public key: + std::array const shortKey{ + 0x03, + 0x30, + 0xE7, + 0xFC, + 0x9D, + 0x56, + 0xBB, + 0x25, + 0xD6, + 0x89, + 0x3B, + 0xA3, + 0xF3, + 0x17, + 0xAE, + 0x5B}; auto toString = [](STObject const& st) { Serializer s; @@ -749,13 +764,13 @@ public: { // reject invalid public key type auto badSt = st; - badSt[sfPublicKey] = badKey; + badSt[sfPublicKey] = makeSlice(badKey); BEAST_EXPECT(!deserializeManifest(toString(badSt))); } { // reject short public key auto badSt = st; - badSt[sfPublicKey] = shortKey; + badSt[sfPublicKey] = makeSlice(shortKey); BEAST_EXPECT(!deserializeManifest(toString(badSt))); } { @@ -767,13 +782,13 @@ public: { // reject invalid signing public key type auto badSt = st; - badSt[sfSigningPubKey] = badKey; + badSt[sfSigningPubKey] = makeSlice(badKey); BEAST_EXPECT(!deserializeManifest(toString(badSt))); } { // reject short signing public key auto badSt = st; - badSt[sfSigningPubKey] = shortKey; + badSt[sfSigningPubKey] = makeSlice(shortKey); BEAST_EXPECT(!deserializeManifest(toString(badSt))); } { diff --git a/src/test/app/Path_test.cpp b/src/test/app/Path_test.cpp index 17e15c950..1889a790e 100644 --- a/src/test/app/Path_test.cpp +++ b/src/test/app/Path_test.cpp @@ -802,7 +802,7 @@ public: "issuer" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "value" : "1000" }, - "HighNode" : "0000000000000000", + "HighNode" : "0", "HighQualityIn" : 2000, "HighQualityOut" : 1400000000, "LedgerEntryType" : "RippleState", @@ -811,7 +811,7 @@ public: "issuer" : "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn", "value" : "0" }, - "LowNode" : "0000000000000000" + "LowNode" : "0" })", jv); @@ -847,14 +847,14 @@ public: "issuer" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "value" : "1000" }, - "HighNode" : "0000000000000000", + "HighNode" : "0", "LedgerEntryType" : "RippleState", "LowLimit" : { "currency" : "USD", "issuer" : "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn", "value" : "1000" }, - "LowNode" : "0000000000000000" + "LowNode" : "0" })", jv); @@ -900,7 +900,7 @@ public: "issuer" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "value" : "0" }, - "HighNode" : "0000000000000000", + "HighNode" : "0", "LedgerEntryType" : "RippleState", "LowLimit" : { @@ -908,7 +908,7 @@ public: "issuer" : "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn", "value" : "0" }, - "LowNode" : "0000000000000000" + "LowNode" : "0" })", jv); diff --git a/src/test/app/TrustAndBalance_test.cpp b/src/test/app/TrustAndBalance_test.cpp index 5e07f1110..72ab5eb43 100644 --- a/src/test/app/TrustAndBalance_test.cpp +++ b/src/test/app/TrustAndBalance_test.cpp @@ -454,30 +454,24 @@ class TrustAndBalance_test : public beast::unit_test::suite jvs[jss::streams].append("transactions"); BEAST_EXPECT(wsc->invoke("subscribe", jvs)[jss::status] == "success"); + char const* invoiceid = + "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"; + Json::Value jv; auto tx = env.jt( pay(env.master, alice, XRP(10000)), - json(sfInvoiceID.fieldName, "DEADBEEF")); + json(sfInvoiceID.fieldName, invoiceid)); jv[jss::tx_blob] = strHex(tx.stx->getSerializer().slice()); auto jrr = wsc->invoke("submit", jv)[jss::result]; BEAST_EXPECT(jrr[jss::status] == "success"); - BEAST_EXPECT( - jrr[jss::tx_json][sfInvoiceID.fieldName] == - "0000000000000000" - "0000000000000000" - "0000000000000000" - "00000000DEADBEEF"); + BEAST_EXPECT(jrr[jss::tx_json][sfInvoiceID.fieldName] == invoiceid); env.close(); using namespace std::chrono_literals; - BEAST_EXPECT(wsc->findMsg(2s, [](auto const& jval) { + BEAST_EXPECT(wsc->findMsg(2s, [invoiceid](auto const& jval) { auto const& t = jval[jss::transaction]; return t[jss::TransactionType] == jss::Payment && - t[sfInvoiceID.fieldName] == - "0000000000000000" - "0000000000000000" - "0000000000000000" - "00000000DEADBEEF"; + t[sfInvoiceID.fieldName] == invoiceid; })); BEAST_EXPECT(wsc->invoke("unsubscribe", jv)[jss::status] == "success"); diff --git a/src/test/basics/base_uint_test.cpp b/src/test/basics/base_uint_test.cpp index ab327bc28..2ac8bbe8f 100644 --- a/src/test/basics/base_uint_test.cpp +++ b/src/test/basics/base_uint_test.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include @@ -34,9 +34,10 @@ namespace test { template struct nonhash { + static constexpr auto const endian = boost::endian::order::big; static constexpr std::size_t WIDTH = Bits / 8; + std::array data_; - static beast::endian const endian = beast::endian::big; nonhash() = default; @@ -62,6 +63,8 @@ struct base_uint_test : beast::unit_test::suite void run() override { + testcase("base_uint: general purpose tests"); + static_assert( !std::is_constructible>::value, ""); static_assert( @@ -151,59 +154,47 @@ struct base_uint_test : beast::unit_test::suite BEAST_EXPECT(uset.size() == 4); - // SetHex tests... - test96 fromHex; - BEAST_EXPECT(fromHex.SetHexExact(to_string(u))); - BEAST_EXPECT(fromHex == u); - fromHex = z; + test96 tmp; + BEAST_EXPECT(tmp.parseHex(to_string(u))); + BEAST_EXPECT(tmp == u); + tmp = z; // fails with extra char - BEAST_EXPECT(!fromHex.SetHexExact("A" + to_string(u))); - fromHex = z; + BEAST_EXPECT(!tmp.parseHex("A" + to_string(u))); + tmp = z; // fails with extra char at end - BEAST_EXPECT(!fromHex.SetHexExact(to_string(u) + "A")); - // NOTE: the value fromHex is actually correctly parsed - // in this case, but that is an implementation detail and - // not guaranteed, thus we don't check the value here. - fromHex = z; + BEAST_EXPECT(!tmp.parseHex(to_string(u) + "A")); - BEAST_EXPECT(fromHex.SetHex(to_string(u))); - BEAST_EXPECT(fromHex == u); - fromHex = z; + // fails with a non-hex character at some point in the string: + tmp = z; - // leading space/0x allowed if not strict - BEAST_EXPECT(fromHex.SetHex(" 0x" + to_string(u))); - BEAST_EXPECT(fromHex == u); - fromHex = z; + for (std::size_t i = 0; i != 24; ++i) + { + std::string x = to_string(z); + x[i] = ('G' + (i % 10)); + BEAST_EXPECT(!tmp.parseHex(x)); + } - // other leading chars also allowed (ignored) - BEAST_EXPECT(fromHex.SetHex("FEFEFE" + to_string(u))); - BEAST_EXPECT(fromHex == u); - fromHex = z; + // Walking 1s: + for (std::size_t i = 0; i != 24; ++i) + { + std::string s1 = "000000000000000000000000"; + s1[i] = '1'; - // invalid hex chars should fail (0 replaced with Z here) - BEAST_EXPECT(!fromHex.SetHex( - boost::algorithm::replace_all_copy(to_string(u), "0", "Z"))); - fromHex = z; + BEAST_EXPECT(tmp.parseHex(s1)); + BEAST_EXPECT(to_string(tmp) == s1); + } - BEAST_EXPECT(fromHex.SetHex(to_string(u), true)); - BEAST_EXPECT(fromHex == u); - fromHex = z; + // Walking 0s: + for (std::size_t i = 0; i != 24; ++i) + { + std::string s1 = "111111111111111111111111"; + s1[i] = '0'; - // strict mode fails with leading chars - BEAST_EXPECT(!fromHex.SetHex(" 0x" + to_string(u), true)); - fromHex = z; - - // SetHex ignores extra leading hexits, so the parsed value - // is still correct for the following case (strict or non-strict) - BEAST_EXPECT(fromHex.SetHex("DEAD" + to_string(u), true)); - BEAST_EXPECT(fromHex == u); - fromHex = z; - - BEAST_EXPECT(fromHex.SetHex("DEAD" + to_string(u), false)); - BEAST_EXPECT(fromHex == u); - fromHex = z; + BEAST_EXPECT(tmp.parseHex(s1)); + BEAST_EXPECT(to_string(tmp) == s1); + } } }; diff --git a/src/test/conditions/PreimageSha256_test.cpp b/src/test/conditions/PreimageSha256_test.cpp index d9df96226..cf4bc8c7c 100644 --- a/src/test/conditions/PreimageSha256_test.cpp +++ b/src/test/conditions/PreimageSha256_test.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -37,29 +38,9 @@ class PreimageSha256_test : public beast::unit_test::suite inline Buffer hexblob(std::string const& s) { - std::vector x; - x.reserve(s.size() / 2); - - auto iter = s.cbegin(); - - while (iter != s.cend()) - { - int cHigh = charUnHex(*iter++); - - if (cHigh < 0) - return {}; - - int cLow = charUnHex(*iter++); - - if (cLow < 0) - return {}; - - x.push_back( - static_cast(cHigh << 4) | - static_cast(cLow)); - } - - return {x.data(), x.size()}; + auto blob = strUnHex(s); + BEAST_EXPECT(blob); + return {blob->data(), blob->size()}; } void diff --git a/src/test/jtx/impl/Env.cpp b/src/test/jtx/impl/Env.cpp index 855dfe7bb..a8c5aca49 100644 --- a/src/test/jtx/impl/Env.cpp +++ b/src/test/jtx/impl/Env.cpp @@ -327,7 +327,9 @@ Env::sign_and_submit(JTx const& jt, Json::Value params) params[jss::tx_json] = jt.jv; jr = client().invoke("submit", params); } - txid_.SetHex(jr[jss::result][jss::tx_json][jss::hash].asString()); + + if (!txid_.parseHex(jr[jss::result][jss::tx_json][jss::hash].asString())) + txid_.zero(); std::tie(ter_, didApply) = parseResult(jr); diff --git a/src/test/ledger/Directory_test.cpp b/src/test/ledger/Directory_test.cpp index 39d319ad9..183e82d9a 100644 --- a/src/test/ledger/Directory_test.cpp +++ b/src/test/ledger/Directory_test.cpp @@ -326,11 +326,11 @@ struct Directory_test : public beast::unit_test::suite env.close(); uint256 base; - base.SetHex( + (void)base.parseHex( "fb71c9aa3310141da4b01d6c744a98286af2d72ab5448d5adc0910ca0c910880"); uint256 item; - item.SetHex( + (void)item.parseHex( "bad0f021aa3b2f6754a8fe82a5779730aa0bbbab82f17201ef24900efc2c7312"); { diff --git a/src/test/overlay/compression_test.cpp b/src/test/overlay/compression_test.cpp index f5f0817a9..041659b37 100644 --- a/src/test/overlay/compression_test.cpp +++ b/src/test/overlay/compression_test.cpp @@ -203,18 +203,11 @@ public: env.trust(bob["USD"](fund), alice); env.close(); - auto toBinary = [](std::string const& text) { - std::string binary; - for (size_t i = 0; i < text.size(); ++i) - { - unsigned int c = charUnHex(text[i]); - c = c << 4; - ++i; - c = c | charUnHex(text[i]); - binary.push_back(c); - } - - return binary; + auto toBinary = [this](std::string const& text) { + auto blob = strUnHex(text); + BEAST_EXPECT(blob); + return std::string{ + reinterpret_cast(blob->data()), blob->size()}; }; std::string usdTxBlob = ""; diff --git a/src/test/protocol/STAmount_test.cpp b/src/test/protocol/STAmount_test.cpp index 10c4e1d4a..bd3e96694 100644 --- a/src/test/protocol/STAmount_test.cpp +++ b/src/test/protocol/STAmount_test.cpp @@ -306,9 +306,6 @@ public: const std::string cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000"; unexpected(!to_currency(c, cur), "create custom currency"); unexpected(to_string(c) != cur, "check custom currency"); - unexpected( - c != Currency(from_hex_text(cur)), - "check custom currency"); } //-------------------------------------------------------------------------- diff --git a/src/test/protocol/digest_test.cpp b/src/test/protocol/digest_test.cpp deleted file mode 100644 index 7c82b5e76..000000000 --- a/src/test/protocol/digest_test.cpp +++ /dev/null @@ -1,160 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of rippled: https://github.com/ripple/rippled - Copyright (c) 2012, 2013 Ripple Labs Inc. - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ripple { - -class digest_test : public beast::unit_test::suite -{ - std::vector dataset1; - - template - void - test(char const* name) - { - using namespace std::chrono; - - // Prime the cache - for (int i = 0; i != 4; i++) - { - for (auto const& x : dataset1) - { - Hasher h; - h(x.data(), x.size()); - (void)static_cast(h); - } - } - - std::array results; - - for (auto& result : results) - { - auto const start = high_resolution_clock::now(); - - for (auto const& x : dataset1) - { - Hasher h; - h(x.data(), x.size()); - (void)static_cast(h); - } - - auto const d = high_resolution_clock::now() - start; - - result = d; - } - - log << " " << name << ":" << '\n'; - - auto const sum = - std::accumulate(results.begin(), results.end(), nanoseconds{0}); - { - auto s = duration_cast(sum); - auto ms = duration_cast(sum) - s; - log << " Total Time = " << s.count() << "." << ms.count() - << " seconds" << std::endl; - } - - auto const mean = sum / results.size(); - { - auto s = duration_cast(mean); - auto ms = duration_cast(mean) - s; - log << " Mean Time = " << s.count() << "." << ms.count() - << " seconds" << std::endl; - } - - std::vector diff(results.size()); - std::transform( - results.begin(), - results.end(), - diff.begin(), - [&mean](nanoseconds trial) { return (trial - mean).count(); }); - auto const sq_sum = - std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0); - { - nanoseconds const stddev{static_cast( - std::sqrt(sq_sum / results.size()))}; - auto s = duration_cast(stddev); - auto ms = duration_cast(stddev) - s; - log << " Std Dev = " << s.count() << "." << ms.count() - << " seconds" << std::endl; - } - } - -public: - digest_test() - { - beast::xor_shift_engine g(19207813); - std::array buf; - - for (int i = 0; i < 1000000; i++) - { - beast::rngfill(buf.data(), buf.size(), g); - dataset1.push_back(uint256{buf}); - } - } - - void - testSHA512() - { - testcase("SHA512"); - test("OpenSSL"); - test("Beast"); - pass(); - } - - void - testSHA256() - { - testcase("SHA256"); - test("OpenSSL"); - test("Beast"); - pass(); - } - - void - testRIPEMD160() - { - testcase("RIPEMD160"); - test("OpenSSL"); - test("Beast"); - pass(); - } - - void - run() override - { - testSHA512(); - testSHA256(); - testRIPEMD160(); - } -}; - -BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(digest, ripple_data, ripple, 20); - -} // namespace ripple diff --git a/src/test/rpc/AccountObjects_test.cpp b/src/test/rpc/AccountObjects_test.cpp index 8f77b6ce9..70c70e697 100644 --- a/src/test/rpc/AccountObjects_test.cpp +++ b/src/test/rpc/AccountObjects_test.cpp @@ -34,10 +34,10 @@ static char const* bobs_account_objects[] = { R"json({ "Account" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "BookDirectory" : "50AD0A9E54D2B381288D535EB724E4275FFBF41580D28A925D038D7EA4C68000", - "BookNode" : "0000000000000000", + "BookNode" : "0", "Flags" : 65536, "LedgerEntryType" : "Offer", - "OwnerNode" : "0000000000000000", + "OwnerNode" : "0", "Sequence" : 6, "TakerGets" : { "currency" : "USD", @@ -59,14 +59,14 @@ static char const* bobs_account_objects[] = { "issuer" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "value" : "1000" }, - "HighNode" : "0000000000000000", + "HighNode" : "0", "LedgerEntryType" : "RippleState", "LowLimit" : { "currency" : "USD", "issuer" : "r9cZvwKU3zzuZK9JFovGg1JC5n7QiqNL8L", "value" : "0" }, - "LowNode" : "0000000000000000", + "LowNode" : "0", "index" : "D13183BCFFC9AAC9F96AEBB5F66E4A652AD1F5D10273AEB615478302BEBFD4A4" })json", R"json({ @@ -81,23 +81,23 @@ static char const* bobs_account_objects[] = { "issuer" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "value" : "1000" }, - "HighNode" : "0000000000000000", + "HighNode" : "0", "LedgerEntryType" : "RippleState", "LowLimit" : { "currency" : "USD", "issuer" : "r32rQHyesiTtdWFU7UJVtff4nCR5SHCbJW", "value" : "0" }, - "LowNode" : "0000000000000000", + "LowNode" : "0", "index" : "D89BC239086183EB9458C396E643795C1134963E6550E682A190A5F021766D43" })json", R"json({ "Account" : "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK", "BookDirectory" : "B025997A323F5C3E03DDF1334471F5984ABDE31C59D463525D038D7EA4C68000", - "BookNode" : "0000000000000000", + "BookNode" : "0", "Flags" : 65536, "LedgerEntryType" : "Offer", - "OwnerNode" : "0000000000000000", + "OwnerNode" : "0", "Sequence" : 7, "TakerGets" : { "currency" : "USD", @@ -287,9 +287,6 @@ public: auto& aobj = resp[jss::result][jss::account_objects][i]; aobj.removeMember("PreviousTxnID"); aobj.removeMember("PreviousTxnLgrSeq"); - - if (aobj != bobj[i]) - std::cout << "Fail at " << i << ": " << aobj << std::endl; BEAST_EXPECT(aobj == bobj[i]); } } diff --git a/src/test/rpc/AccountSet_test.cpp b/src/test/rpc/AccountSet_test.cpp index b9745f3ec..a3fe45e77 100644 --- a/src/test/rpc/AccountSet_test.cpp +++ b/src/test/rpc/AccountSet_test.cpp @@ -258,11 +258,11 @@ public: env.fund(XRP(10000), alice); auto jt = noop(alice); - uint256 somehash = from_hex_text( - "9633ec8af54f16b5286db1d7b519ef49eefc050c0c8ac4384f1d88acd1bfdf05"); - jt[sfWalletLocator.fieldName] = to_string(somehash); + std::string const locator = + "9633EC8AF54F16B5286DB1D7B519EF49EEFC050C0C8AC4384F1D88ACD1BFDF05"; + jt[sfWalletLocator.fieldName] = locator; env(jt); - BEAST_EXPECT((*env.le(alice))[sfWalletLocator] == somehash); + BEAST_EXPECT(to_string((*env.le(alice))[sfWalletLocator]) == locator); jt[sfWalletLocator.fieldName] = ""; env(jt); @@ -280,11 +280,10 @@ public: env.fund(XRP(10000), alice); auto jt = noop(alice); - uint128 somehash = - from_hex_text("fff680681c2f5e6095324e2e08838f221a72ab4f"); - jt[sfEmailHash.fieldName] = to_string(somehash); + std::string const mh("5F31A79367DC3137FADA860C05742EE6"); + jt[sfEmailHash.fieldName] = mh; env(jt); - BEAST_EXPECT((*env.le(alice))[sfEmailHash] == somehash); + BEAST_EXPECT(to_string((*env.le(alice))[sfEmailHash]) == mh); jt[sfEmailHash.fieldName] = ""; env(jt); diff --git a/src/test/rpc/Book_test.cpp b/src/test/rpc/Book_test.cpp index 548f0b6eb..a2ebc21cb 100644 --- a/src/test/rpc/Book_test.cpp +++ b/src/test/rpc/Book_test.cpp @@ -1054,10 +1054,10 @@ public: BEAST_EXPECT( jrOffer[sfBookDirectory.fieldName] == getBookDir(env, XRP, USD.issue())); - BEAST_EXPECT(jrOffer[sfBookNode.fieldName] == "0000000000000000"); + BEAST_EXPECT(jrOffer[sfBookNode.fieldName] == "0"); BEAST_EXPECT(jrOffer[jss::Flags] == 0); BEAST_EXPECT(jrOffer[sfLedgerEntryType.fieldName] == jss::Offer); - BEAST_EXPECT(jrOffer[sfOwnerNode.fieldName] == "0000000000000000"); + BEAST_EXPECT(jrOffer[sfOwnerNode.fieldName] == "0"); BEAST_EXPECT(jrOffer[sfSequence.fieldName] == 5); BEAST_EXPECT( jrOffer[jss::TakerGets] == @@ -1110,10 +1110,10 @@ public: BEAST_EXPECT( jrNextOffer[sfBookDirectory.fieldName] == getBookDir(env, XRP, USD.issue())); - BEAST_EXPECT(jrNextOffer[sfBookNode.fieldName] == "0000000000000000"); + BEAST_EXPECT(jrNextOffer[sfBookNode.fieldName] == "0"); BEAST_EXPECT(jrNextOffer[jss::Flags] == 0); BEAST_EXPECT(jrNextOffer[sfLedgerEntryType.fieldName] == jss::Offer); - BEAST_EXPECT(jrNextOffer[sfOwnerNode.fieldName] == "0000000000000000"); + BEAST_EXPECT(jrNextOffer[sfOwnerNode.fieldName] == "0"); BEAST_EXPECT(jrNextOffer[sfSequence.fieldName] == 5); BEAST_EXPECT( jrNextOffer[jss::TakerGets] == diff --git a/src/test/rpc/Feature_test.cpp b/src/test/rpc/Feature_test.cpp index 5773f7566..89b976395 100644 --- a/src/test/rpc/Feature_test.cpp +++ b/src/test/rpc/Feature_test.cpp @@ -129,7 +129,7 @@ class Feature_test : public beast::unit_test::suite ++it) { uint256 id; - id.SetHexExact(it.key().asString().c_str()); + (void)id.parseHex(it.key().asString().c_str()); if (!BEAST_EXPECT((*it).isMember(jss::name))) return; bool expectEnabled = env.app().getAmendmentTable().isEnabled(id); diff --git a/src/test/rpc/LedgerRPC_test.cpp b/src/test/rpc/LedgerRPC_test.cpp index b58d21359..d0070857d 100644 --- a/src/test/rpc/LedgerRPC_test.cpp +++ b/src/test/rpc/LedgerRPC_test.cpp @@ -249,10 +249,10 @@ class LedgerRPC_test : public beast::unit_test::suite Env env{*this, envconfig(no_admin)}; - env.close(); + // env.close(); Json::Value jvParams; - jvParams[jss::ledger_index] = 3u; + jvParams[jss::ledger_index] = 1u; jvParams[jss::full] = true; auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result]; @@ -1345,9 +1345,8 @@ class LedgerRPC_test : public beast::unit_test::suite "json", "ledger", boost::lexical_cast(jvParams))[jss::result]; - BEAST_EXPECT(jrr.isMember(jss::ledger)); - BEAST_EXPECT(jrr.isMember(jss::ledger_hash)); - BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "3"); + BEAST_EXPECT(jrr[jss::error] == "invalidParams"); + BEAST_EXPECT(jrr[jss::error_message] == "ledgerHashMalformed"); // request with non-string ledger_hash jvParams[jss::ledger_hash] = 2; diff --git a/src/test/rpc/Submit_test.cpp b/src/test/rpc/Submit_test.cpp index bd8047372..252e1c32a 100644 --- a/src/test/rpc/Submit_test.cpp +++ b/src/test/rpc/Submit_test.cpp @@ -17,6 +17,7 @@ */ //============================================================================== +#include #include #include #include @@ -73,18 +74,11 @@ public: env.trust(bob["USD"](TestData::fund), alice); env.close(); - auto toBinary = [](std::string const& text) { - std::string binary; - for (size_t i = 0; i < text.size(); ++i) - { - unsigned int c = charUnHex(text[i]); - c = c << 4; - ++i; - c = c | charUnHex(text[i]); - binary.push_back(c); - } - - return binary; + auto toBinary = [this](std::string const& text) { + auto blob = strUnHex(text); + BEAST_EXPECT(blob); + return std::string{ + reinterpret_cast(blob->data()), blob->size()}; }; // use a websocket client to fill transaction blobs diff --git a/src/test/rpc/TransactionEntry_test.cpp b/src/test/rpc/TransactionEntry_test.cpp index 1d81355c9..a477a6248 100644 --- a/src/test/rpc/TransactionEntry_test.cpp +++ b/src/test/rpc/TransactionEntry_test.cpp @@ -66,7 +66,7 @@ class TransactionEntry_test : public beast::unit_test::suite auto const result = env.client().invoke("transaction_entry", params)[jss::result]; BEAST_EXPECT(!result[jss::ledger_hash].asString().empty()); - BEAST_EXPECT(result[jss::error] == "transactionNotFound"); + BEAST_EXPECT(result[jss::error] == "malformedRequest"); BEAST_EXPECT(result[jss::status] == "error"); } diff --git a/src/test/shamap/SHAMap_test.cpp b/src/test/shamap/SHAMap_test.cpp index 49d5d5638..6b1d5c6d6 100644 --- a/src/test/shamap/SHAMap_test.cpp +++ b/src/test/shamap/SHAMap_test.cpp @@ -143,15 +143,15 @@ public: // h3 and h4 differ only in the leaf, same terminal node (level 19) uint256 h1, h2, h3, h4, h5; - h1.SetHex( + (void)h1.parseHex( "092891fe4ef6cee585fdc6fda0e09eb4d386363158ec3321b8123e5a772c6ca7"); - h2.SetHex( + (void)h2.parseHex( "436ccbac3347baa1f1e53baeef1f43334da88f1f6d70d963b833afd6dfa289fe"); - h3.SetHex( + (void)h3.parseHex( "b92891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8"); - h4.SetHex( + (void)h4.parseHex( "b92891fe4ef6cee585fdc6fda2e09eb4d386363158ec3321b8123e5a772c6ca8"); - h5.SetHex( + (void)h5.parseHex( "a92891fe4ef6cee585fdc6fda0e09eb4d386363158ec3321b8123e5a772c6ca7"); SHAMap sMap(SHAMapType::FREE, f); @@ -223,54 +223,54 @@ public: testcase("build/tear unbacked"); { std::vector keys(8); - keys[0].SetHex( + (void)keys[0].parseHex( "b92891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[1].SetHex( + (void)keys[1].parseHex( "b92881fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[2].SetHex( + (void)keys[2].parseHex( "b92691fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[3].SetHex( + (void)keys[3].parseHex( "b92791fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[4].SetHex( + (void)keys[4].parseHex( "b91891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[5].SetHex( + (void)keys[5].parseHex( "b99891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[6].SetHex( + (void)keys[6].parseHex( "f22891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[7].SetHex( + (void)keys[7].parseHex( "292891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); std::vector hashes(8); - hashes[0].SetHex( + (void)hashes[0].parseHex( "B7387CFEA0465759ADC718E8C42B52D2309D179B326E239EB5075C64B6281F" "7F"); - hashes[1].SetHex( + (void)hashes[1].parseHex( "FBC195A9592A54AB44010274163CB6BA95F497EC5BA0A8831845467FB2ECE2" "66"); - hashes[2].SetHex( + (void)hashes[2].parseHex( "4E7D2684B65DFD48937FFB775E20175C43AF0C94066F7D5679F51AE756795B" "75"); - hashes[3].SetHex( + (void)hashes[3].parseHex( "7A2F312EB203695FFD164E038E281839EEF06A1B99BFC263F3CECC6C74F93E" "07"); - hashes[4].SetHex( + (void)hashes[4].parseHex( "395A6691A372387A703FB0F2C6D2C405DAF307D0817F8F0E207596462B0E3A" "3E"); - hashes[5].SetHex( + (void)hashes[5].parseHex( "D044C0A696DE3169CC70AE216A1564D69DE96582865796142CE7D98A84D9DD" "E4"); - hashes[6].SetHex( + (void)hashes[6].parseHex( "76DCC77C4027309B5A91AD164083264D70B77B5E43E08AEDA5EBF943611436" "15"); - hashes[7].SetHex( + (void)hashes[7].parseHex( "DF4220E93ADC6F5569063A01B4DC79F8DB9553B6A3222ADE23DEA02BBE7230" "E5"); @@ -302,28 +302,28 @@ public: { std::vector keys(8); - keys[0].SetHex( + (void)keys[0].parseHex( "f22891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[1].SetHex( + (void)keys[1].parseHex( "b99891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[2].SetHex( + (void)keys[2].parseHex( "b92891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[3].SetHex( + (void)keys[3].parseHex( "b92881fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[4].SetHex( + (void)keys[4].parseHex( "b92791fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[5].SetHex( + (void)keys[5].parseHex( "b92691fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[6].SetHex( + (void)keys[6].parseHex( "b91891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8"); - keys[7].SetHex( + (void)keys[7].parseHex( "292891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6c" "a8");