]> Git Repo - VerusCoin.git/blame - src/core_read.cpp
Remove debug outs
[VerusCoin.git] / src / core_read.cpp
CommitLineData
f914f1a7 1// Copyright (c) 2009-2014 The Bitcoin Core developers
78253fcb 2// Distributed under the MIT software license, see the accompanying
bc909a7a 3// file COPYING or https://www.opensource.org/licenses/mit-license.php .
ae775b5b 4
ae775b5b 5#include "core_io.h"
611116d4 6
d2270111
LD
7#include "primitives/block.h"
8#include "primitives/transaction.h"
cbd22a50 9#include "script/script.h"
611116d4 10#include "serialize.h"
fa736190 11#include "streams.h"
a10a6e2a 12#include <univalue.h>
cbe39a38 13#include "util.h"
85c579e3
CF
14#include "utilstrencodings.h"
15#include "version.h"
b2aeaa79 16
b2aeaa79
JG
17#include <boost/algorithm/string/classification.hpp>
18#include <boost/algorithm/string/predicate.hpp>
b2aeaa79 19#include <boost/algorithm/string/replace.hpp>
611116d4
PK
20#include <boost/algorithm/string/split.hpp>
21#include <boost/assign/list_of.hpp>
ae775b5b 22
611116d4 23using namespace std;
b2aeaa79 24
db954a65 25CScript ParseScript(const std::string& s)
b2aeaa79
JG
26{
27 CScript result;
28
29 static map<string, opcodetype> mapOpNames;
30
2a584009 31 if (mapOpNames.empty())
b2aeaa79
JG
32 {
33 for (int op = 0; op <= OP_NOP10; op++)
34 {
35 // Allow OP_RESERVED to get into mapOpNames
36 if (op < OP_NOP && op != OP_RESERVED)
37 continue;
38
39 const char* name = GetOpName((opcodetype)op);
40 if (strcmp(name, "OP_UNKNOWN") == 0)
41 continue;
42 string strName(name);
43 mapOpNames[strName] = (opcodetype)op;
44 // Convenience: OP_ADD and just ADD are both recognized:
856e862f 45 boost::algorithm::replace_first(strName, "OP_", "");
b2aeaa79
JG
46 mapOpNames[strName] = (opcodetype)op;
47 }
48 }
49
50 vector<string> words;
856e862f 51 boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
b2aeaa79 52
2a584009 53 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
b2aeaa79 54 {
2a584009 55 if (w->empty())
b2aeaa79
JG
56 {
57 // Empty string, ignore. (boost::split given '' will return one word)
58 }
856e862f
CF
59 else if (all(*w, boost::algorithm::is_digit()) ||
60 (boost::algorithm::starts_with(*w, "-") && all(string(w->begin()+1, w->end()), boost::algorithm::is_digit())))
b2aeaa79
JG
61 {
62 // Number
2a584009 63 int64_t n = atoi64(*w);
b2aeaa79
JG
64 result << n;
65 }
856e862f 66 else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end())))
b2aeaa79
JG
67 {
68 // Raw hex data, inserted NOT pushed onto stack:
2a584009 69 std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end()));
b2aeaa79
JG
70 result.insert(result.end(), raw.begin(), raw.end());
71 }
856e862f 72 else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
b2aeaa79
JG
73 {
74 // Single-quoted string, pushed as data. NOTE: this is poor-man's
75 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
2a584009 76 std::vector<unsigned char> value(w->begin()+1, w->end()-1);
b2aeaa79
JG
77 result << value;
78 }
2a584009 79 else if (mapOpNames.count(*w))
b2aeaa79
JG
80 {
81 // opcode, e.g. OP_ADD or ADD:
2a584009 82 result << mapOpNames[*w];
b2aeaa79
JG
83 }
84 else
85 {
86 throw runtime_error("script parse error");
87 }
88 }
89
90 return result;
91}
ae775b5b
JG
92
93bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
94{
95 if (!IsHex(strHexTx))
96 return false;
97
98 vector<unsigned char> txData(ParseHex(strHexTx));
99 CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
100 try {
101 ssData >> tx;
102 }
27df4123 103 catch (const std::exception&) {
ae775b5b
JG
104 return false;
105 }
106
107 return true;
108}
109
3dcbb9b6
LD
110bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
111{
112 if (!IsHex(strHexBlk))
113 return false;
114
115 std::vector<unsigned char> blockData(ParseHex(strHexBlk));
116 CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
117 try {
118 ssBlock >> block;
119 }
27df4123 120 catch (const std::exception&) {
3dcbb9b6
LD
121 return false;
122 }
123
124 return true;
125}
126
cbe39a38
JG
127uint256 ParseHashUV(const UniValue& v, const string& strName)
128{
129 string strHex;
130 if (v.isStr())
131 strHex = v.getValStr();
7f718139
LD
132 return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
133}
134
135uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
136{
cbe39a38
JG
137 if (!IsHex(strHex)) // Note: IsHex("") is false
138 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
139
140 uint256 result;
141 result.SetHex(strHex);
142 return result;
143}
144
145vector<unsigned char> ParseHexUV(const UniValue& v, const string& strName)
146{
147 string strHex;
148 if (v.isStr())
149 strHex = v.getValStr();
150 if (!IsHex(strHex))
151 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
152 return ParseHex(strHex);
153}
This page took 0.189509 seconds and 4 git commands to generate.