-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
265 lines (213 loc) · 5.81 KB
/
main.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "stack.h"
#include "script.h"
#include "alloc.h"
#include "util.h"
extern int yyparse();
extern FILE* yyin;
char * g_reader_buf;
char * g_reader_buf_top;
struct stack g_reader_stack;
u32 g_reader_buf_cap;
int yy_scan_string(const char *);
void yyerror(const char* s);
#define streq(a,b) strcmp(a,b) == 0
void parse_error(char* err) {
fprintf(stderr, "[btcs] parse error: %s\n", err);
exit(1);
}
#define COMPILE_SHOW_SCRIPT (1 << 0)
#define COMPILE_SHOW_SCRIPT_HEX (1 << 1)
#define COMPILE_SHOW_STACK (1 << 2)
#define COMPILE_SHOW_STACK_HEX (1 << 3)
#define COMPILE_SHOW_RESULTS (1 << 4)
#define COMPILE_SHOW_LABELS (1 << 5)
#define COMPILE_SHOW_ALL 0x3F
static int compile(int compile_options, int argc, const char *argv[])
{
yyin = stdin;
struct result result;
/* size_t size; */
size_t bufsize = MAX_STACK_SIZE * MAX_STACK_SIZE;
int i;
int compiled_len;
u8 *buf = (u8*)malloc(bufsize);
struct stack tmp_stack;
alloc_arenas();
stack_init(&tmp_stack);
stack_init(&g_reader_stack);
if (argc > 1) {
for (i = 1; i < argc; ++i) {
yy_scan_string(argv[i]);
yyparse();
}
}
else {
do {
yyparse();
} while(!feof(yyin));
}
bool nol = (compile_options & COMPILE_SHOW_LABELS) == 0;
/* size = g_reader_buf_top - g_reader_buf; */
if (compile_options & COMPILE_SHOW_SCRIPT) {
if (compile_options & COMPILE_SHOW_LABELS)
printf("script ");
script_print_vals(&g_reader_stack);
}
script_serialize(&g_reader_stack, buf, bufsize, &compiled_len);
script_eval(buf, compiled_len, &tmp_stack, &result);
if (compile_options & COMPILE_SHOW_SCRIPT_HEX) {
if (compile_options & COMPILE_SHOW_LABELS)
printf("script_hex ");
for(i = 0; i < compiled_len; ++i)
printf("%02x", buf[i]);
printf("\n");
}
if (compile_options & COMPILE_SHOW_STACK) {
if (compile_options & COMPILE_SHOW_LABELS)
printf("stack ");
script_print_vals(&tmp_stack);
}
stack_serialize(&tmp_stack, buf, bufsize, &compiled_len);
if (compile_options & COMPILE_SHOW_STACK_HEX) {
if (compile_options & COMPILE_SHOW_LABELS)
printf("stack_hex ");
for(i = 0; i < compiled_len; ++i)
printf("%02x", buf[i]);
printf("\n");
}
if (compile_options & COMPILE_SHOW_RESULTS) {
if (compile_options & COMPILE_SHOW_LABELS)
printf("results ");
if (result.error)
printf("error:%d:%s:%s", result.op_count, result.error,
op_name(result.last_op));
else
printf("success");
printf("\n");
}
stack_free(&g_reader_stack);
stack_free(&tmp_stack);
free_arenas(0);
return !!result.error;
}
static void fail(int err, const char *msg)
{
fprintf(stderr, "error: %s\n", msg);
exit(err);
}
static int decompile(const char *str, size_t *strlen, bool abbrev_data)
{
static u8 buf[10000];
hex_decode(str, strlen, buf, sizeof(buf));
if (*strlen % 2 != 0)
return 0;
size_t nbytes = *strlen / 2;
script_print(buf, nbytes, abbrev_data);
return 1;
}
static void usage()
{
fprintf(stderr, "usage: btcs [OPTIONS] <script>\n\n");
fprintf(stderr, " OPTIONS\n\n");
fprintf(stderr, " -d,--decompile decompile a base16 string to bitcoin script\n");
exit(1);
}
static u32 count_bits(u32 n)
{
u32 count = 0;
while (n) {
n &= (n-1) ;
count++;
}
return count;
}
int main(int argc, const char *argv[])
{
static u8 buf[20000];
bool is_decompile = false;
const char *input = NULL;
size_t written;
bool abbrev_data = false;
int compile_options = 0;
int last_opt = 0;
int hide_labels = -1;
if (argc == 2 && (streq(argv[1], "-h") || streq(argv[1], "--help")))
usage();
for (int i = 1; i < argc; i++) {
if (streq(argv[i], "-d") || streq(argv[i], "--decompile"))
is_decompile = true;
else if (streq(argv[i], "-sh") || streq(argv[i], "--script-hex"))
compile_options |= COMPILE_SHOW_SCRIPT_HEX;
else if (streq(argv[i], "-s") || streq(argv[i], "--script"))
compile_options |= COMPILE_SHOW_SCRIPT;
else if (streq(argv[i], "-t") || streq(argv[i], "--stack"))
compile_options |= COMPILE_SHOW_STACK;
else if (streq(argv[i], "-th") || streq(argv[i], "--stack-hex"))
compile_options |= COMPILE_SHOW_STACK_HEX;
else if (streq(argv[i], "-r") || streq(argv[i], "--results"))
compile_options |= COMPILE_SHOW_RESULTS;
else if (streq(argv[i], "-l") || streq(argv[i], "--hide-labels"))
hide_labels = 1;
else if (streq(argv[i], "+l") || streq(argv[i], "--show-labels"))
hide_labels = 0;
else if (streq(argv[i], "-a") || streq(argv[i], "--abbreviate-data"))
abbrev_data = true;
else {
last_opt = i-1;
input = argv[i];
break;
}
}
if (is_decompile) {
// we have stdin
char *line = NULL;
ssize_t len = 0, prevlen;
size_t n;
int ok;
bool failed = false;
if (input == NULL) {
while ((len = getline(&line, &n, stdin)) != -1) {
len--;
prevlen = len;
ok = decompile(line, (size_t*)&len, abbrev_data);
printf("%.*s\n", (int)(prevlen - len), line+len);
if (!ok) {
failed = true;
fprintf(stderr, "failed to decompile\n");
}
}
}
else {
ok = read_arg_or_stdin(input, buf, sizeof(buf), &written);
if (!ok)
fail(4, "failed to read input arg (too big?)");
ok = decompile((const char *)buf, &written, abbrev_data);
printf("\n");
if (!ok) {
fprintf(stderr, "failed to decompile\n");
failed = true;
}
}
if (failed)
exit(5);
}
else {
if (!compile_options)
compile_options = COMPILE_SHOW_ALL;
if (hide_labels == -1)
hide_labels = count_bits(compile_options) == 1;
if (hide_labels)
compile_options &= ~COMPILE_SHOW_LABELS;
else
compile_options |= COMPILE_SHOW_LABELS;
exit( compile(compile_options, argc - last_opt, argv + last_opt) );
}
}
void yyerror(const char* s) {
fprintf(stderr, "Parse error: %s\n", s);
exit(1);
}