-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
69 lines (54 loc) · 1.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "opcodes.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#include <boost/multiprecision/cpp_int.hpp>
#define OCT_DIGITS "01234567"
#define DEC_DIGITS "0123456789"
#define HEX_DIGITS "0123456789ABCDEFabcdef"
using namespace std;
using namespace boost::multiprecision;
bool is_integer(const string& s)
{
if (s.substr(0, 2) == "0x")
return !s.empty() && s.substr(2).find_first_not_of(HEX_DIGITS) == string::npos;
else if (s[0] == '0')
return !s.empty() && s.find_first_not_of(OCT_DIGITS) == string::npos;
else
return !s.empty() && s.find_first_not_of(DEC_DIGITS) == string::npos;
}
int main()
{
ifstream in("in", ios::binary);
ofstream out("out");
vector<string> t;
cpp_int n;
vector<unsigned char> nv;
string ns;
uint32_t nbytes;
for (string ins; in >> ins;) {
if (is_integer(ins)) {
n = cpp_int(ins);
export_bits(n, back_inserter(nv), 8);
ns = string(nv.begin(), nv.end());
nbytes = nv.size();
if (n == 0)
out << opcodes["OP_0"];
else if (n <= 16)
out << nv[0] + 0x50;
else if (nbytes <= 0x4b)
out << (char*) &nbytes << ns;
else if (nbytes <= 0xff)
out << opcodes["OP_PUSHDATA1"] << (char*) &nbytes << ns;
else if (nbytes <= 0xffff)
out << opcodes["OP_PUSHDATA2"] << (char*) &nbytes << ns;
else if (nbytes <= 0xffffffff)
out << opcodes["OP_PUSHDATA4"] << (char*) &nbytes << ns;
} else {
out << opcodes[ins];
}
}
return (0);
}