-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbicol2text.c
219 lines (207 loc) · 4.45 KB
/
bicol2text.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
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opcodes.h"
#define MAX_NAMES 100000
#define MAX_NAME 64
static struct {
uint32_t addr;
uint32_t is_var;
char name[MAX_NAME];
} names[MAX_NAMES];
static int names_size = 0;
static int32_t offset = 0;
static int32_t user_offset = 0;
static int32_t last_op = -1;
static FILE* src;
static FILE* dst;
static uint8_t read8() {
++offset;
return fgetc(src);
}
static uint16_t read16() {
uint16_t a, b;
a = read8();
b = read8();
return a | (b << 8);
}
static uint32_t read32() {
uint32_t a, b;
a = read16();
b = read16();
return a | (b << 16);
}
static void read_string(char *name, int32_t *len) {
int i;
*len = read16() - 1;
if (*len < 0 || *len > MAX_NAME * 10) {
fprintf(stderr, "Bad name length at: %d, of: %d\n", offset, *len);
exit(1);
}
for (i = 0; i < 1 + *len; ++i) {
*name = read8();
++name;
}
}
static void add_word(uint8_t op) {
char name[MAX_NAME * 10];
int32_t len;
if (op == OP_DEFINE || op == OP_DEFUSER) {
memset(&names[names_size], 0, sizeof(names[0]));
read_string(names[names_size].name, &len);
if (op == OP_DEFUSER) {
names[names_size].addr = user_offset++;
} else {
names[names_size].addr = offset;
}
names[names_size].is_var = op == OP_DEFUSER;
++names_size;
} else if (op == OP_STRING || op == OP_COMMENT) {
read_string(name, &len);
} else if (op == OP_DEC || op == OP_HEX) {
read32();
} else if (op == OP_CALL || op == OP_TICK || op == OP_USER) {
read16();
}
}
static void word(uint8_t op) {
char name[MAX_NAME * 10];
int32_t len;
int32_t operand;
int i;
fflush(dst);
if (op == OP_DEC) {
fprintf(dst, " %d", (int32_t) read32());
last_op = op;
return;
}
if (op == OP_HEX) {
fprintf(dst, " $%x", (uint32_t) read32());
last_op = op;
return;
}
if (op == OP_CALL || op == OP_TICK) {
operand = (int16_t) read16();
for (i = 0; i < names_size; ++i) {
if (names[i].addr ==
(int32_t) offset + (int32_t) operand && !names[i].is_var) {
if (op == OP_TICK) {
fprintf(dst, " '");
}
fprintf(dst, " %s", names[i].name);
last_op = op;
return;
}
}
fprintf(dst, " call:%d", operand);
last_op = op;
return;
}
if (op == OP_USER) {
operand = (uint16_t) read16();
for (i = 0; i < names_size; ++i) {
if (names[i].addr == operand && names[i].is_var) {
fprintf(dst, " %s", names[i].name);
last_op = op;
return;
}
}
fprintf(dst, " user:%d", operand);
last_op = op;
return;
}
if (op == OP_STRING) {
fprintf(dst, " s\" ");
read_string(name, &len);
fwrite(name, 1, len, dst);
fprintf(dst, "\"");
last_op = op;
return;
}
if (op == OP_DASH) {
fprintf(dst, "\n --");
last_op = op;
return;
}
if (op == OP_DEFINE) {
read_string(name, &len);
fprintf(dst, "\n: %s", name);
last_op = op;
return;
} else if (op == OP_DEFUSER) {
read_string(name, &len);
fprintf(dst, "\nuser %s", name);
++user_offset;
last_op = op;
return;
} else if (op == OP_COMMENT) {
if(last_op == OP_DEFINE) {
fprintf(dst, " ( ");
} else {
if (last_op != -1) {
fprintf(dst, "\n\n");
}
fprintf(dst, "( ");
}
read_string(name, &len);
fprintf(dst, "%s)", name);
last_op = op;
return;
}
#define V(num, _, wname, code) \
if (op == num) { \
fprintf(dst, " %s", wname); \
last_op = op; \
return; \
}
INSTRUCTIONS(V)
#undef V
fprintf(stderr, "Bad opcode: %d at: %d\n", op, offset);
exit(1);
}
int main(int argc, char *argv[]) {
char name[MAX_NAME];
uint8_t op;
if (argc != 3) {
fprintf(stderr, "Usage: %s <src> <dst>\n", argv[0]);
return 1;
}
strcpy(name, "");
src = fopen(argv[1], "r");
if (!src) {
fprintf(stderr, "Cannot open: %s\n", argv[1]);
return 1;
}
dst = fopen(argv[2], "wb");
if (!dst) {
fprintf(stderr, "Cannot open: %s\n", argv[2]);
return 1;
}
offset = 0;
user_offset = 0;
last_op = -1;
for (;;) {
if (fread(&op, 1, 1, src) != 1) {
break;
}
add_word(op);
offset++;
}
fseek(src, 0, SEEK_SET);
offset = 0;
user_offset = 0;
last_op = -1;
for (;;) {
if (fread(&op, 1, 1, src) != 1) {
break;
}
word(op);
offset++;
}
fprintf(dst, "\n");
fclose(src);
fclose(dst);
return 0;
}