diff options
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/Common.h | 11 | ||||
-rw-r--r-- | libdevcore/JSON.h | 4 | ||||
-rw-r--r-- | libdevcore/SwarmHash.cpp | 12 | ||||
-rw-r--r-- | libdevcore/SwarmHash.h | 5 | ||||
-rw-r--r-- | libdevcore/UTF8.cpp | 2 | ||||
-rw-r--r-- | libdevcore/UTF8.h | 10 |
6 files changed, 32 insertions, 12 deletions
diff --git a/libdevcore/Common.h b/libdevcore/Common.h index d65cfeac..225f38ac 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -135,6 +135,17 @@ inline u256 s2u(s256 _u) return u256(c_end + _u); } +inline std::ostream& operator<<(std::ostream& os, bytes const& _bytes) +{ + std::ostringstream ss; + ss << std::hex; + std::copy(_bytes.begin(), _bytes.end(), std::ostream_iterator<int>(ss, ",")); + std::string result = ss.str(); + result.pop_back(); + os << "[" + result + "]"; + return os; +} + template <size_t n> inline u256 exp10() { return exp10<n - 1>() * u256(10); diff --git a/libdevcore/JSON.h b/libdevcore/JSON.h index 7876dfb2..0d6e0d2e 100644 --- a/libdevcore/JSON.h +++ b/libdevcore/JSON.h @@ -28,13 +28,13 @@ namespace dev { /// Serialise the JSON object (@a _input) with identation -std::string jsonPrettyPrint(Json::Value const& _input) +inline std::string jsonPrettyPrint(Json::Value const& _input) { return Json::StyledWriter().write(_input); } /// Serialise theJ SON object (@a _input) without identation -std::string jsonCompactPrint(Json::Value const& _input) +inline std::string jsonCompactPrint(Json::Value const& _input) { Json::FastWriter writer; writer.omitEndingLineFeed(); diff --git a/libdevcore/SwarmHash.cpp b/libdevcore/SwarmHash.cpp index e7b844eb..aa98eafd 100644 --- a/libdevcore/SwarmHash.cpp +++ b/libdevcore/SwarmHash.cpp @@ -38,13 +38,14 @@ h256 swarmHashSimple(bytesConstRef _data, size_t _size) return keccak256(toLittleEndian(_size) + _data.toBytes()); } -h256 swarmHashIntermediate(bytes const& _input, size_t _offset, size_t _length) +h256 swarmHashIntermediate(string const& _input, size_t _offset, size_t _length) { + bytesConstRef ref; + bytes innerNodes; if (_length <= 0x1000) - return swarmHashSimple(bytesConstRef(_input.data() + _offset, _length), _length); + ref = bytesConstRef(_input).cropped(_offset, _length); else { - bytes innerNodes; size_t maxRepresentedSize = 0x1000; while (maxRepresentedSize * (0x1000 / 32) < _length) maxRepresentedSize *= (0x1000 / 32); @@ -53,11 +54,12 @@ h256 swarmHashIntermediate(bytes const& _input, size_t _offset, size_t _length) size_t size = std::min(maxRepresentedSize, _length - i); innerNodes += swarmHashIntermediate(_input, _offset + i, size).asBytes(); } - return swarmHashSimple(bytesConstRef(&innerNodes), _length); + ref = bytesConstRef(&innerNodes); } + return swarmHashSimple(ref, _length); } -h256 dev::swarmHash(bytes const& _input) +h256 dev::swarmHash(string const& _input) { return swarmHashIntermediate(_input, 0, _input.size()); } diff --git a/libdevcore/SwarmHash.h b/libdevcore/SwarmHash.h index 925509ab..f474ce11 100644 --- a/libdevcore/SwarmHash.h +++ b/libdevcore/SwarmHash.h @@ -20,12 +20,13 @@ #pragma once #include <libdevcore/FixedHash.h> -#include <libdevcore/Common.h> + +#include <string> namespace dev { /// Compute the "swarm hash" of @a _data -h256 swarmHash(bytes const& _data); +h256 swarmHash(std::string const& _data); } diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp index d742fe66..1c7ed17c 100644 --- a/libdevcore/UTF8.cpp +++ b/libdevcore/UTF8.cpp @@ -28,7 +28,7 @@ namespace dev { -bool validate(std::string const& _input, size_t& _invalidPosition) +bool validateUTF8(std::string const& _input, size_t& _invalidPosition) { const size_t length = _input.length(); bool valid = true; diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h index 3e39273c..753914e3 100644 --- a/libdevcore/UTF8.h +++ b/libdevcore/UTF8.h @@ -29,7 +29,13 @@ namespace dev { /// Validate an input for UTF8 encoding -/// @returns true if it is invalid and the first invalid position in invalidPosition -bool validate(std::string const& _input, size_t& _invalidPosition); +/// @returns false if it is invalid and the first invalid position in invalidPosition +bool validateUTF8(std::string const& _input, size_t& _invalidPosition); + +inline bool validateUTF8(std::string const& _input) +{ + size_t invalidPos; + return validateUTF8(_input, invalidPos); +} } |