Ethereum  PoC-8
The C++ Implementation of Ethereum
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 #include "Exceptions.h"
26 
27 using namespace std;
28 using namespace dev;
29 
30 namespace
31 {
32 int fromHexChar(char _i) noexcept
33 {
34  if (_i >= '0' && _i <= '9')
35  return _i - '0';
36  if (_i >= 'a' && _i <= 'f')
37  return _i - 'a' + 10;
38  if (_i >= 'A' && _i <= 'F')
39  return _i - 'A' + 10;
40  return -1;
41 }
42 }
43 
44 bool dev::isHex(string const& _s) noexcept
45 {
46  auto it = _s.begin();
47  if (_s.compare(0, 2, "0x") == 0)
48  it += 2;
49  return std::all_of(it, _s.end(), [](char c){ return fromHexChar(c) != -1; });
50 }
51 
52 std::string dev::escaped(std::string const& _s, bool _all)
53 {
54  static const map<char, char> prettyEscapes{{'\r', 'r'}, {'\n', 'n'}, {'\t', 't'}, {'\v', 'v'}};
55  std::string ret;
56  ret.reserve(_s.size() + 2);
57  ret.push_back('"');
58  for (auto i: _s)
59  if (i == '"' && !_all)
60  ret += "\\\"";
61  else if (i == '\\' && !_all)
62  ret += "\\\\";
63  else if (prettyEscapes.count(i) && !_all)
64  {
65  ret += '\\';
66  ret += prettyEscapes.find(i)->second;
67  }
68  else if (i < ' ' || _all)
69  {
70  ret += "\\x";
71  ret.push_back("0123456789abcdef"[(uint8_t)i / 16]);
72  ret.push_back("0123456789abcdef"[(uint8_t)i % 16]);
73  }
74  else
75  ret.push_back(i);
76  ret.push_back('"');
77  return ret;
78 }
79 
80 
81 bytes dev::fromHex(std::string const& _s, WhenError _throw)
82 {
83  unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
84  std::vector<uint8_t> ret;
85  ret.reserve((_s.size() - s + 1) / 2);
86 
87  if (_s.size() % 2)
88  {
89  int h = fromHexChar(_s[s++]);
90  if (h != -1)
91  ret.push_back(h);
92  else if (_throw == WhenError::Throw)
93  BOOST_THROW_EXCEPTION(BadHexCharacter());
94  else
95  return bytes();
96  }
97  for (unsigned i = s; i < _s.size(); i += 2)
98  {
99  int h = fromHexChar(_s[i]);
100  int l = fromHexChar(_s[i + 1]);
101  if (h != -1 && l != -1)
102  ret.push_back((byte)(h * 16 + l));
103  else if (_throw == WhenError::Throw)
104  BOOST_THROW_EXCEPTION(BadHexCharacter());
105  else
106  return bytes();
107  }
108  return ret;
109 }
110 
112 {
113  std::vector<uint8_t> ret;
114  ret.reserve(_s.size() * 2);
115  for (auto i: _s)
116  {
117  ret.push_back(i / 16);
118  ret.push_back(i % 16);
119  }
120  return ret;
121 }
dev::asNibbles
bytes asNibbles(bytesConstRef const &_s)
Definition: CommonData.cpp:111
dev::vector_ref< byte const >
Exceptions.h
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::vector_ref::size
size_t size() const
Definition: vector_ref.h:53
CommonData.h
dev::escaped
std::string escaped(std::string const &_s, bool _all=true)
Definition: CommonData.cpp:52
std
Definition: FixedHash.h:393
dev
Definition: Address.cpp:21
dev::fromHex
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:81
dev::WhenError
WhenError
Definition: CommonData.h:40
dev::isHex
bool isHex(std::string const &_s) noexcept