CommonData.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file CommonData.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "CommonData.h"
  19. #include <random>
  20. #if defined(_MSC_VER)
  21. #pragma warning(push)
  22. #pragma warning(disable:4724) // potential mod by 0, line 78 of boost/random/uniform_int_distribution.hpp (boost 1.55)
  23. #endif
  24. #include <boost/random/uniform_int_distribution.hpp>
  25. #if defined(_MSC_VER)
  26. #pragma warning(pop)
  27. #endif
  28. #include "Exceptions.h"
  29. #include "Log.h"
  30. using namespace std;
  31. using namespace dev;
  32. namespace
  33. {
  34. int fromHexChar(char _i) noexcept
  35. {
  36. if (_i >= '0' && _i <= '9')
  37. return _i - '0';
  38. if (_i >= 'a' && _i <= 'f')
  39. return _i - 'a' + 10;
  40. if (_i >= 'A' && _i <= 'F')
  41. return _i - 'A' + 10;
  42. return -1;
  43. }
  44. }
  45. bool dev::isHex(string const& _s) noexcept
  46. {
  47. auto it = _s.begin();
  48. if (_s.compare(0, 2, "0x") == 0)
  49. it += 2;
  50. return std::all_of(it, _s.end(), [](char c){ return fromHexChar(c) != -1; });
  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. std::string dev::randomWord()
  80. {
  81. static std::mt19937_64 s_eng(0);
  82. std::string ret(boost::random::uniform_int_distribution<int>(1, 5)(s_eng), ' ');
  83. char const n[] = "qwertyuiop";//asdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  84. boost::random::uniform_int_distribution<int> d(0, sizeof(n) - 2);
  85. for (char& c: ret)
  86. c = n[d(s_eng)];
  87. return ret;
  88. }
  89. bytes dev::fromHex(std::string const& _s, WhenError _throw)
  90. {
  91. unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
  92. std::vector<uint8_t> ret;
  93. ret.reserve((_s.size() - s + 1) / 2);
  94. if (_s.size() % 2)
  95. {
  96. int h = fromHexChar(_s[s++]);
  97. if (h != -1)
  98. ret.push_back(h);
  99. else if (_throw == WhenError::Throw)
  100. BOOST_THROW_EXCEPTION(BadHexCharacter());
  101. else
  102. return bytes();
  103. }
  104. for (unsigned i = s; i < _s.size(); i += 2)
  105. {
  106. int h = fromHexChar(_s[i]);
  107. int l = fromHexChar(_s[i + 1]);
  108. if (h != -1 && l != -1)
  109. ret.push_back((byte)(h * 16 + l));
  110. else if (_throw == WhenError::Throw)
  111. BOOST_THROW_EXCEPTION(BadHexCharacter());
  112. else
  113. return bytes();
  114. }
  115. return ret;
  116. }
  117. bytes dev::asNibbles(bytesConstRef const& _s)
  118. {
  119. std::vector<uint8_t> ret;
  120. ret.reserve(_s.size() * 2);
  121. for (auto i: _s)
  122. {
  123. ret.push_back(i / 16);
  124. ret.push_back(i % 16);
  125. }
  126. return ret;
  127. }
  128. std::string dev::toString(string32 const& _s)
  129. {
  130. std::string ret;
  131. for (unsigned i = 0; i < 32 && _s[i]; ++i)
  132. ret.push_back(_s[i]);
  133. return ret;
  134. }