Fabcoin Core  0.16.2
P2P Digital Currency
CommonJS.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #pragma once
25 
26 #include <string>
27 #include "FixedHash.h"
28 #include "CommonData.h"
29 #include "CommonIO.h"
30 
31 namespace dev
32 {
33 
34 template <unsigned S> std::string toJS(FixedHash<S> const& _h)
35 {
36  return "0x" + toHex(_h.ref());
37 }
38 
39 template <unsigned N> std::string toJS(boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> const& _n)
40 {
41  std::string h = toHex(toCompactBigEndian(_n, 1));
42  // remove first 0, if it is necessary;
43  std::string res = h[0] != '0' ? h : h.substr(1);
44  return "0x" + res;
45 }
46 
47 inline std::string toJS(bytes const& _n, std::size_t _padding = 0)
48 {
49  bytes n = _n;
50  n.resize(std::max<unsigned>(n.size(), _padding));
51  return "0x" + toHex(n);
52 }
53 
54 template<unsigned T> std::string toJS(SecureFixedHash<T> const& _i)
55 {
56  std::stringstream stream;
57  stream << "0x" << _i.makeInsecure().hex();
58  return stream.str();
59 }
60 
61 template<typename T> std::string toJS(T const& _i)
62 {
63  std::stringstream stream;
64  stream << "0x" << std::hex << _i;
65  return stream.str();
66 }
67 
68 enum class OnFailed { InterpretRaw, Empty, Throw };
69 
72 bytes jsToBytes(std::string const& _s, OnFailed _f = OnFailed::Empty);
74 bytes padded(bytes _b, unsigned _l);
76 bytes paddedRight(bytes _b, unsigned _l);
78 bytes unpadded(bytes _s);
82 std::string fromRaw(h256 _n);
83 
84 template <unsigned N> FixedHash<N> jsToFixed(std::string const& _s)
85 {
86  if (_s.substr(0, 2) == "0x")
87  // Hex
88  return FixedHash<N>(_s.substr(2 + std::max<unsigned>(N * 2, _s.size() - 2) - N * 2));
89  else if (_s.find_first_not_of("0123456789") == std::string::npos)
90  // Decimal
91  return (typename FixedHash<N>::Arith)(_s);
92  else
93  // Binary
94  return FixedHash<N>(); // FAIL
95 }
96 
97 template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> jsToInt(std::string const& _s)
98 {
99  if (_s.substr(0, 2) == "0x")
100  // Hex
101  return fromBigEndian<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>>(fromHex(_s.substr(2)));
102  else if (_s.find_first_not_of("0123456789") == std::string::npos)
103  // Decimal
104  return boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>(_s);
105  else
106  // Binary
107  return 0; // FAIL
108 }
109 
110 inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); }
111 
115 inline int jsToInt(std::string const& _s)
116 {
117  int ret = 0;
118  DEV_IGNORE_EXCEPTIONS(ret = std::stoi(_s, nullptr, 0));
119  return ret;
120 }
121 
122 inline std::string jsToDecimal(std::string const& _s)
123 {
124  return toString(jsToU256(_s));
125 }
126 
127 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
std::string jsToDecimal(std::string const &_s)
Definition: CommonJS.h:122
#define T(i, x)
#define h(i)
Definition: sha.cpp:736
OnFailed
Definition: CommonJS.h:68
FixedHash< N > jsToFixed(std::string const &_s)
Definition: CommonJS.h:84
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> Arith
The corresponding arithmetic type.
Definition: FixedHash.h:51
bytes unpadded(bytes _b)
Removing all trailing &#39;0&#39;. Returns empty array if input contains only &#39;0&#39; char.
Definition: CommonJS.cpp:60
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
bytesRef ref()
Definition: FixedHash.h:133
FixedHash< T > const & makeInsecure() const
Definition: FixedHash.h:253
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor). If it can&#39;t be interpreted as an ASCII string, empty string is returned.
Definition: CommonJS.cpp:81
std::vector< byte > bytes
Definition: Common.h:75
bytes toCompactBigEndian(T _val, unsigned _min=0)
Convenience function for toBigEndian.
Definition: CommonData.h:141
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bytes paddedRight(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the back of _b until it is of length _l.
Definition: CommonJS.cpp:54
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
bytes padded(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the front of _b until it is of length _l.
Definition: CommonJS.cpp:47
T fromBigEndian(_In const &_bytes)
Converts a big-endian byte-stream represented on a templated collection to a templated integer value...
Definition: CommonData.h:124
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Convert a string representation of a number to an int String can be a normal decimal number...
Definition: CommonJS.h:97
bytes unpadLeft(bytes _b)
Remove all 0 byte on the head of _s.
Definition: CommonJS.cpp:67
u256 jsToU256(std::string const &_s)
Definition: CommonJS.h:110
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63
std::string hex() const
Definition: FixedHash.h:130