You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core_read.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2009-2014 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "core_io.h"
  5. #include "core.h"
  6. #include "script/script.h"
  7. #include "serialize.h"
  8. #include "univalue/univalue.h"
  9. #include "util.h"
  10. #include "utilstrencodings.h"
  11. #include "version.h"
  12. #include <boost/algorithm/string/classification.hpp>
  13. #include <boost/algorithm/string/predicate.hpp>
  14. #include <boost/algorithm/string/replace.hpp>
  15. #include <boost/algorithm/string/split.hpp>
  16. #include <boost/assign/list_of.hpp>
  17. using namespace boost;
  18. using namespace boost::algorithm;
  19. using namespace std;
  20. CScript ParseScript(std::string s)
  21. {
  22. CScript result;
  23. static map<string, opcodetype> mapOpNames;
  24. if (mapOpNames.empty())
  25. {
  26. for (int op = 0; op <= OP_NOP10; op++)
  27. {
  28. // Allow OP_RESERVED to get into mapOpNames
  29. if (op < OP_NOP && op != OP_RESERVED)
  30. continue;
  31. const char* name = GetOpName((opcodetype)op);
  32. if (strcmp(name, "OP_UNKNOWN") == 0)
  33. continue;
  34. string strName(name);
  35. mapOpNames[strName] = (opcodetype)op;
  36. // Convenience: OP_ADD and just ADD are both recognized:
  37. replace_first(strName, "OP_", "");
  38. mapOpNames[strName] = (opcodetype)op;
  39. }
  40. }
  41. vector<string> words;
  42. split(words, s, is_any_of(" \t\n"), token_compress_on);
  43. for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
  44. {
  45. if (w->empty())
  46. {
  47. // Empty string, ignore. (boost::split given '' will return one word)
  48. }
  49. else if (all(*w, is_digit()) ||
  50. (starts_with(*w, "-") && all(string(w->begin()+1, w->end()), is_digit())))
  51. {
  52. // Number
  53. int64_t n = atoi64(*w);
  54. result << n;
  55. }
  56. else if (starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end())))
  57. {
  58. // Raw hex data, inserted NOT pushed onto stack:
  59. std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end()));
  60. result.insert(result.end(), raw.begin(), raw.end());
  61. }
  62. else if (w->size() >= 2 && starts_with(*w, "'") && ends_with(*w, "'"))
  63. {
  64. // Single-quoted string, pushed as data. NOTE: this is poor-man's
  65. // parsing, spaces/tabs/newlines in single-quoted strings won't work.
  66. std::vector<unsigned char> value(w->begin()+1, w->end()-1);
  67. result << value;
  68. }
  69. else if (mapOpNames.count(*w))
  70. {
  71. // opcode, e.g. OP_ADD or ADD:
  72. result << mapOpNames[*w];
  73. }
  74. else
  75. {
  76. throw runtime_error("script parse error");
  77. }
  78. }
  79. return result;
  80. }
  81. bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
  82. {
  83. if (!IsHex(strHexTx))
  84. return false;
  85. vector<unsigned char> txData(ParseHex(strHexTx));
  86. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
  87. try {
  88. ssData >> tx;
  89. }
  90. catch (const std::exception &) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. uint256 ParseHashUV(const UniValue& v, const string& strName)
  96. {
  97. string strHex;
  98. if (v.isStr())
  99. strHex = v.getValStr();
  100. if (!IsHex(strHex)) // Note: IsHex("") is false
  101. throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
  102. uint256 result;
  103. result.SetHex(strHex);
  104. return result;
  105. }
  106. vector<unsigned char> ParseHexUV(const UniValue& v, const string& strName)
  107. {
  108. string strHex;
  109. if (v.isStr())
  110. strHex = v.getValStr();
  111. if (!IsHex(strHex))
  112. throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
  113. return ParseHex(strHex);
  114. }