Dogecoin Core  1.14.2
P2P Digital Currency
core_read.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "core_io.h"
6 
7 #include "primitives/block.h"
9 #include "script/script.h"
10 #include "serialize.h"
11 #include "streams.h"
12 #include <univalue.h>
13 #include "util.h"
14 #include "utilstrencodings.h"
15 #include "version.h"
16 
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/predicate.hpp>
19 #include <boost/algorithm/string/replace.hpp>
20 #include <boost/algorithm/string/split.hpp>
21 #include <boost/assign/list_of.hpp>
22 
23 CScript ParseScript(const std::string& s)
24 {
25  CScript result;
26 
27  static std::map<std::string, opcodetype> mapOpNames;
28 
29  if (mapOpNames.empty())
30  {
31  for (int op = 0; op <= OP_NOP10; op++)
32  {
33  // Allow OP_RESERVED to get into mapOpNames
34  if (op < OP_NOP && op != OP_RESERVED)
35  continue;
36 
37  const char* name = GetOpName((opcodetype)op);
38  if (strcmp(name, "OP_UNKNOWN") == 0)
39  continue;
40  std::string strName(name);
41  mapOpNames[strName] = (opcodetype)op;
42  // Convenience: OP_ADD and just ADD are both recognized:
43  boost::algorithm::replace_first(strName, "OP_", "");
44  mapOpNames[strName] = (opcodetype)op;
45  }
46  }
47 
48  std::vector<std::string> words;
49  boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
50 
51  for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
52  {
53  if (w->empty())
54  {
55  // Empty string, ignore. (boost::split given '' will return one word)
56  }
57  else if (all(*w, boost::algorithm::is_digit()) ||
58  (boost::algorithm::starts_with(*w, "-") && all(std::string(w->begin()+1, w->end()), boost::algorithm::is_digit())))
59  {
60  // Number
61  int64_t n = atoi64(*w);
62  result << n;
63  }
64  else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end())))
65  {
66  // Raw hex data, inserted NOT pushed onto stack:
67  std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
68  result.insert(result.end(), raw.begin(), raw.end());
69  }
70  else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
71  {
72  // Single-quoted string, pushed as data. NOTE: this is poor-man's
73  // parsing, spaces/tabs/newlines in single-quoted strings won't work.
74  std::vector<unsigned char> value(w->begin()+1, w->end()-1);
75  result << value;
76  }
77  else if (mapOpNames.count(*w))
78  {
79  // opcode, e.g. OP_ADD or ADD:
80  result << mapOpNames[*w];
81  }
82  else
83  {
84  throw std::runtime_error("script parse error");
85  }
86  }
87 
88  return result;
89 }
90 
91 bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx, bool fTryNoWitness)
92 {
93  if (!IsHex(strHexTx))
94  return false;
95 
96  std::vector<unsigned char> txData(ParseHex(strHexTx));
97 
98  if (fTryNoWitness) {
99  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
100  try {
101  ssData >> tx;
102  if (ssData.eof()) {
103  return true;
104  }
105  }
106  catch (const std::exception&) {
107  // Fall through.
108  }
109  }
110 
111  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
112  try {
113  ssData >> tx;
114  if (!ssData.empty())
115  return false;
116  }
117  catch (const std::exception&) {
118  return false;
119  }
120 
121  return true;
122 }
123 
124 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
125 {
126  if (!IsHex(strHexBlk))
127  return false;
128 
129  std::vector<unsigned char> blockData(ParseHex(strHexBlk));
130  CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
131  try {
132  ssBlock >> block;
133  }
134  catch (const std::exception&) {
135  return false;
136  }
137 
138  return true;
139 }
140 
141 uint256 ParseHashUV(const UniValue& v, const std::string& strName)
142 {
143  std::string strHex;
144  if (v.isStr())
145  strHex = v.getValStr();
146  return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
147 }
148 
149 uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
150 {
151  if (!IsHex(strHex)) // Note: IsHex("") is false
152  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
153 
154  uint256 result;
155  result.SetHex(strHex);
156  return result;
157 }
158 
159 std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
160 {
161  std::string strHex;
162  if (v.isStr())
163  strHex = v.getValStr();
164  if (!IsHex(strHex))
165  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
166  return ParseHex(strHex);
167 }
Definition: block.h:67
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
bool empty() const
Definition: streams.h:238
bool eof() const
Definition: streams.h:333
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:377
const std::string & getValStr() const
Definition: univalue.h:66
bool isStr() const
Definition: univalue.h:81
void SetHex(const char *psz)
Definition: uint256.cpp:30
iterator end()
Definition: prevector.h:291
iterator insert(iterator pos, const T &value)
Definition: prevector.h:342
256-bit opaque blob.
Definition: uint256.h:123
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:23
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:159
uint256 ParseHashStr(const std::string &strHex, const std::string &strName)
Definition: core_read.cpp:149
uint256 ParseHashUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:141
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
Definition: core_read.cpp:124
bool DecodeHexTx(CMutableTransaction &tx, const std::string &strHexTx, bool fTryNoWitness)
Definition: core_read.cpp:91
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:13
opcodetype
Script opcodes.
Definition: script.h:45
@ OP_NOP10
Definition: script.h:178
@ OP_NOP
Definition: script.h:73
@ OP_RESERVED
Definition: script.h:53
@ SER_NETWORK
Definition: serialize.h:146
A mutable version of CTransaction.
Definition: transaction.h:413
int64_t atoi64(const char *psz)
bool IsHex(const string &str)
vector< unsigned char > ParseHex(const char *psz)