aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore
diff options
context:
space:
mode:
Diffstat (limited to 'libdevcore')
-rw-r--r--libdevcore/Common.h11
-rw-r--r--libdevcore/CommonData.cpp3
-rw-r--r--libdevcore/JSON.h44
-rw-r--r--libdevcore/SwarmHash.cpp65
-rw-r--r--libdevcore/SwarmHash.h32
-rw-r--r--libdevcore/UTF8.cpp2
-rw-r--r--libdevcore/UTF8.h10
7 files changed, 161 insertions, 6 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/CommonData.cpp b/libdevcore/CommonData.cpp
index b27271cf..062d1b29 100644
--- a/libdevcore/CommonData.cpp
+++ b/libdevcore/CommonData.cpp
@@ -20,9 +20,6 @@
*/
#include "CommonData.h"
-#if defined(_MSC_VER)
-#pragma warning(pop)
-#endif
#include "Exceptions.h"
using namespace std;
using namespace dev;
diff --git a/libdevcore/JSON.h b/libdevcore/JSON.h
new file mode 100644
index 00000000..9f7d9a03
--- /dev/null
+++ b/libdevcore/JSON.h
@@ -0,0 +1,44 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ cpp-ethereum is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file JSON.h
+ * @date 2016
+ *
+ * JSON related helpers
+ */
+
+#pragma once
+
+#include <json/json.h>
+
+namespace dev
+{
+
+/// Serialise the JSON object (@a _input) with indentation
+inline std::string jsonPrettyPrint(Json::Value const& _input)
+{
+ return Json::StyledWriter().write(_input);
+}
+
+/// Serialise the JSON object (@a _input) without indentation
+inline std::string jsonCompactPrint(Json::Value const& _input)
+{
+ Json::FastWriter writer;
+ writer.omitEndingLineFeed();
+ return writer.write(_input);
+}
+
+}
diff --git a/libdevcore/SwarmHash.cpp b/libdevcore/SwarmHash.cpp
new file mode 100644
index 00000000..aa98eafd
--- /dev/null
+++ b/libdevcore/SwarmHash.cpp
@@ -0,0 +1,65 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ cpp-ethereum is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file SwarmHash.cpp
+ */
+
+#include <libdevcore/SwarmHash.h>
+
+#include <libdevcore/SHA3.h>
+
+using namespace std;
+using namespace dev;
+
+
+bytes toLittleEndian(size_t _size)
+{
+ bytes encoded(8);
+ for (size_t i = 0; i < 8; ++i)
+ encoded[i] = (_size >> (8 * i)) & 0xff;
+ return encoded;
+}
+
+h256 swarmHashSimple(bytesConstRef _data, size_t _size)
+{
+ return keccak256(toLittleEndian(_size) + _data.toBytes());
+}
+
+h256 swarmHashIntermediate(string const& _input, size_t _offset, size_t _length)
+{
+ bytesConstRef ref;
+ bytes innerNodes;
+ if (_length <= 0x1000)
+ ref = bytesConstRef(_input).cropped(_offset, _length);
+ else
+ {
+ size_t maxRepresentedSize = 0x1000;
+ while (maxRepresentedSize * (0x1000 / 32) < _length)
+ maxRepresentedSize *= (0x1000 / 32);
+ for (size_t i = 0; i < _length; i += maxRepresentedSize)
+ {
+ size_t size = std::min(maxRepresentedSize, _length - i);
+ innerNodes += swarmHashIntermediate(_input, _offset + i, size).asBytes();
+ }
+ ref = bytesConstRef(&innerNodes);
+ }
+ return swarmHashSimple(ref, _length);
+}
+
+h256 dev::swarmHash(string const& _input)
+{
+ return swarmHashIntermediate(_input, 0, _input.size());
+}
diff --git a/libdevcore/SwarmHash.h b/libdevcore/SwarmHash.h
new file mode 100644
index 00000000..f474ce11
--- /dev/null
+++ b/libdevcore/SwarmHash.h
@@ -0,0 +1,32 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ cpp-ethereum is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/** @file SwarmHash.h
+ */
+
+#pragma once
+
+#include <libdevcore/FixedHash.h>
+
+#include <string>
+
+namespace dev
+{
+
+/// Compute the "swarm hash" of @a _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);
+}
}