| 22 | #include <algorithm> |
| 23 | |
| 24 | CScript ParseScript(const std::string& s) |
| 25 | { |
| 26 | CScript result; |
| 27 | |
| 28 | static std::map<std::string, opcodetype> mapOpNames; |
| 29 | |
| 30 | if (mapOpNames.empty()) |
| 31 | { |
| 32 | for (unsigned int op = 0; op <= MAX_OPCODE; op++) |
| 33 | { |
| 34 | // Allow OP_RESERVED to get into mapOpNames |
| 35 | if (op < OP_NOP && op != OP_RESERVED) |
| 36 | continue; |
| 37 | |
| 38 | const char* name = GetOpName(static_cast<opcodetype>(op)); |
| 39 | if (strcmp(name, "OP_UNKNOWN") == 0) |
| 40 | continue; |
| 41 | std::string strName(name); |
| 42 | mapOpNames[strName] = static_cast<opcodetype>(op); |
| 43 | // Convenience: OP_ADD and just ADD are both recognized: |
| 44 | boost::algorithm::replace_first(strName, "OP_", ""); |
| 45 | mapOpNames[strName] = static_cast<opcodetype>(op); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | std::vector<std::string> words; |
| 50 | boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on); |
| 51 | |
| 52 | for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w) |
| 53 | { |
| 54 | if (w->empty()) |
| 55 | { |
| 56 | // Empty string, ignore. (boost::split given '' will return one word) |
| 57 | } |
| 58 | else if (std::all_of(w->begin(), w->end(), ::IsDigit) || |
| 59 | (w->front() == '-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(), ::IsDigit))) |
| 60 | { |
| 61 | // Number |
| 62 | int64_t n = atoi64(*w); |
| 63 | result << n; |
| 64 | } |
| 65 | else if (w->substr(0,2) == "0x" && w->size() > 2 && IsHex(std::string(w->begin()+2, w->end()))) |
| 66 | { |
| 67 | // Raw hex data, inserted NOT pushed onto stack: |
| 68 | std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end())); |
| 69 | result.insert(result.end(), raw.begin(), raw.end()); |
| 70 | } |
| 71 | else if (w->size() >= 2 && w->front() == '\'' && w->back() == '\'') |
| 72 | { |
| 73 | // Single-quoted string, pushed as data. NOTE: this is poor-man's |
| 74 | // parsing, spaces/tabs/newlines in single-quoted strings won't work. |
| 75 | std::vector<unsigned char> value(w->begin()+1, w->end()-1); |
| 76 | result << value; |
| 77 | } |
| 78 | else if (mapOpNames.count(*w)) |
| 79 | { |
| 80 | // opcode, e.g. OP_ADD or ADD: |
| 81 | result << mapOpNames[*w]; |