| 23 | using namespace std; |
| 24 | |
| 25 | CScript ParseScript(std::string s) |
| 26 | { |
| 27 | CScript result; |
| 28 | |
| 29 | static map<string, opcodetype> mapOpNames; |
| 30 | |
| 31 | if (mapOpNames.empty()) |
| 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: |
| 45 | boost::algorithm::replace_first(strName, "OP_", ""); |
| 46 | mapOpNames[strName] = (opcodetype)op; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | vector<string> words; |
| 51 | boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on); |
| 52 | |
| 53 | for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w) |
| 54 | { |
| 55 | if (w->empty()) |
| 56 | { |
| 57 | // Empty string, ignore. (boost::split given '' will return one word) |
| 58 | } |
| 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()))) |
| 61 | { |
| 62 | // Number |
| 63 | int64_t n = atoi64(*w); |
| 64 | result << n; |
| 65 | } |
| 66 | else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end()))) |
| 67 | { |
| 68 | // Raw hex data, inserted NOT pushed onto stack: |
| 69 | std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end())); |
| 70 | result.insert(result.end(), raw.begin(), raw.end()); |
| 71 | } |
| 72 | else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'")) |
| 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. |
| 76 | std::vector<unsigned char> value(w->begin()+1, w->end()-1); |
| 77 | result << value; |
| 78 | } |
| 79 | else if (mapOpNames.count(*w)) |
| 80 | { |
| 81 | // opcode, e.g. OP_ADD or ADD: |
| 82 | result << mapOpNames[*w]; |