Fabcoin Core  0.16.2
P2P Digital Currency
CommonData.cpp
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 */
22 #include "CommonData.h"
23 #include <random>
24 
25 #if defined(_MSC_VER)
26 #pragma warning(push)
27 #pragma warning(disable:4724) // potential mod by 0, line 78 of boost/random/uniform_int_distribution.hpp (boost 1.55)
28 #endif
29 #include <boost/random/uniform_int_distribution.hpp>
30 #if defined(_MSC_VER)
31 #pragma warning(pop)
32 #endif
33 #include "Exceptions.h"
34 #include "Log.h"
35 using namespace std;
36 using namespace dev;
37 
38 namespace
39 {
40 int fromHexChar(char _i) noexcept
41 {
42  if (_i >= '0' && _i <= '9')
43  return _i - '0';
44  if (_i >= 'a' && _i <= 'f')
45  return _i - 'a' + 10;
46  if (_i >= 'A' && _i <= 'F')
47  return _i - 'A' + 10;
48  return -1;
49 }
50 }
51 
52 bool dev::isHex(string const& _s) noexcept
53 {
54  auto it = _s.begin();
55  if (_s.compare(0, 2, "0x") == 0)
56  it += 2;
57  return std::all_of(it, _s.end(), [](char c){ return fromHexChar(c) != -1; });
58 }
59 
60 std::string dev::escaped(std::string const& _s, bool _all)
61 {
62  static const map<char, char> prettyEscapes{{'\r', 'r'}, {'\n', 'n'}, {'\t', 't'}, {'\v', 'v'}};
63  std::string ret;
64  ret.reserve(_s.size() + 2);
65  ret.push_back('"');
66  for (auto i: _s)
67  if (i == '"' && !_all)
68  ret += "\\\"";
69  else if (i == '\\' && !_all)
70  ret += "\\\\";
71  else if (prettyEscapes.count(i) && !_all)
72  {
73  ret += '\\';
74  ret += prettyEscapes.find(i)->second;
75  }
76  else if (i < ' ' || _all)
77  {
78  ret += "\\x";
79  ret.push_back("0123456789abcdef"[(uint8_t)i / 16]);
80  ret.push_back("0123456789abcdef"[(uint8_t)i % 16]);
81  }
82  else
83  ret.push_back(i);
84  ret.push_back('"');
85  return ret;
86 }
87 
88 std::string dev::randomWord()
89 {
90  static std::mt19937_64 s_eng(0);
91  std::string ret(boost::random::uniform_int_distribution<int>(1, 5)(s_eng), ' ');
92  char const n[] = "qwertyuiop";//asdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
93  boost::random::uniform_int_distribution<int> d(0, sizeof(n) - 2);
94  for (char& c: ret)
95  c = n[d(s_eng)];
96  return ret;
97 }
98 
99 bytes dev::fromHex(std::string const& _s, WhenError _throw)
100 {
101  unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
102  std::vector<uint8_t> ret;
103  ret.reserve((_s.size() - s + 1) / 2);
104 
105  if (_s.size() % 2)
106  {
107  int h = fromHexChar(_s[s++]);
108  if (h != -1)
109  ret.push_back(h);
110  else if (_throw == WhenError::Throw)
111  BOOST_THROW_EXCEPTION(BadHexCharacter());
112  else
113  return bytes();
114  }
115  for (unsigned i = s; i < _s.size(); i += 2)
116  {
117  int h = fromHexChar(_s[i]);
118  int l = fromHexChar(_s[i + 1]);
119  if (h != -1 && l != -1)
120  ret.push_back((byte)(h * 16 + l));
121  else if (_throw == WhenError::Throw)
122  BOOST_THROW_EXCEPTION(BadHexCharacter());
123  else
124  return bytes();
125  }
126  return ret;
127 }
128 
130 {
131  std::vector<uint8_t> ret;
132  ret.reserve(_s.size() * 2);
133  for (auto i: _s)
134  {
135  ret.push_back(i / 16);
136  ret.push_back(i % 16);
137  }
138  return ret;
139 }
140 
141 std::string dev::toString(string32 const& _s)
142 {
143  std::string ret;
144  for (unsigned i = 0; i < 32 && _s[i]; ++i)
145  ret.push_back(_s[i]);
146  return ret;
147 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
#define h(i)
Definition: sha.cpp:736
std::string randomWord()
Creates a random, printable, word.
Definition: CommonData.cpp:88
#define c(i)
std::hash for asio::adress
Definition: Common.h:323
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
WhenError
Definition: CommonData.h:39
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
Definition: CommonData.cpp:60
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
bool isHex(std::string const &_s) noexcept
std::vector< byte > bytes
Definition: Common.h:75
bytes asNibbles(bytesConstRef const &_s)
Definition: CommonData.cpp:129
size_t size() const
Definition: vector_ref.h:55
std::array< char, 32 > string32
Definition: Common.h:150
#define d(i)
Definition: sha.cpp:732