-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
170 lines (159 loc) · 4.46 KB
/
parser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/parser.h"
#include "include/asm.h"
#include "include/globals.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* reverse_lines(char* s) {
char* new_string = (char*)calloc(1, MAX_C_CODE_LEN);
int len = strlen(s);
int i;
int last_pos = len;
int pos = 0;
for (i = len - 1; i >= 0; i--) {
if (s[i] == '\n') {
if (last_pos != i + 1) {
int line_len = last_pos - i - 1;
strncpy(new_string + pos, s + i + 1, line_len);
pos += line_len;
new_string[pos] = '\n';
pos++;
}
last_pos = i;
}
}
if (last_pos > 0) {
int line_len = last_pos;
strncpy(new_string + pos, s, line_len);
pos += line_len;
if (last_pos < len && s[last_pos] == '\n') {
new_string[pos] = '\n';
pos++;
}
}
new_string[pos] = 0;
return new_string;
}
void delete_last_char(char* s, char c) {
char* last = strrchr(s, c);
if (last) memmove(last, last + 1, strlen(last));
}
void free_parsed_data(parsed_data* p) {
if (p->name) free(p->name);
if (p->next) free_parsed_data(p->next);
free(p);
}
//todo: add error handling
int replace_section(char* input_str, int start, int end, char* replacement) {
int replacement_len = strlen(replacement);
int section_len = end - start + 1;
int tail_len = strlen(input_str) - end - 1;
memmove(input_str + start + replacement_len, input_str + end + 1, tail_len + 1);
memcpy(input_str + start, replacement, replacement_len);
return replacement_len - section_len;
}
//todo: add error handling
char** replace_instrs(char* instr, parsed_data* parsed, int n){
char** replaced_instrs = (char**)malloc(sizeof(char*)*2);
replaced_instrs[0] = (char*)calloc(1,INSTR_MAX_SIZE);
replaced_instrs[1] = (char*)calloc(1,INSTR_MAX_SIZE);
strcpy(replaced_instrs[0], instr);
strcpy(replaced_instrs[1], instr);
int i = 0;
int offset = 0;
int tmp = 0;
while(parsed) {
uint64_t test_value;
if (parsed->size != 64)
test_value = ((1ULL << parsed->size) - 1) & (~(1ULL << (parsed->size - 1))); // clear_msb(2^size - 1)
else
test_value = 9223372036854775807;
char* test_value_str = (char*)calloc(1, SIZE_MAX_DIGITS);
sprintf(test_value_str, "0x%lx", test_value);
if (i == n) {
uint64_t test_value_mo = test_value - 1;
char* test_value_mo_str = (char*)calloc(1,SIZE_MAX_DIGITS);
sprintf(test_value_mo_str, "0x%lx", test_value_mo);
tmp = replace_section(replaced_instrs[0], offset + parsed->pos, offset + parsed->end, test_value_str);
replace_section(replaced_instrs[1], offset + parsed->pos, offset + parsed->end, test_value_mo_str);
offset += tmp;
free(test_value_mo_str);
}
else {
tmp = replace_section(replaced_instrs[0], offset + parsed->pos, offset + parsed->end, test_value_str);
replace_section(replaced_instrs[1], offset + parsed->pos, offset + parsed->end, test_value_str);
offset += tmp;
}
free(test_value_str);
i++;
parsed = parsed->next;
}
return replaced_instrs;
}
parsed_data* parse(const char* input, int* ll_size) {
parsed_data* variables_ll = (parsed_data*)calloc(1, sizeof(parsed_data));
parsed_data* last_ll;
parsed_data* head = variables_ll;
int pos = 0;
int in_backtick_expr = 0;
int name_part;
int ll_size_loc = 0;
int name_pos = 0;
int need_to_free = 0;
while (input[pos]) {
if (in_backtick_expr) {
if (input[pos] == SPECIAL) {
need_to_free = 1;
variables_ll->end = pos;
variables_ll->next = (parsed_data*)calloc(1, sizeof(parsed_data));
ll_size_loc++;
last_ll = variables_ll;
variables_ll = variables_ll->next;
in_backtick_expr = 0;
pos++;
}
else if (input[pos] == ',') {
(variables_ll->name)[name_pos] = 0;
variables_ll->size = strtol(input+pos+1, NULL, 10);
name_part = 0;
pos++;
}
else if (name_part) {
if (name_pos >= 0xff) return NULL;
(variables_ll->name)[name_pos++] = input[pos++];
}
else {
pos++;
}
}
else {
if (input[pos] == SPECIAL) {
in_backtick_expr = 1;
name_part = 1;
name_pos = 0;
variables_ll->name = (char*)calloc(1,INSTR_MAX_SIZE);
variables_ll->pos = pos;
pos++;
}
else {
pos++;
}
}
}
if (head->name == NULL) {
free(variables_ll);
return NULL;
}
else {
variables_ll->next = NULL;
if (ll_size) *ll_size = ll_size_loc;
if (need_to_free) {
free(variables_ll);
last_ll->next = NULL;
}
return head;
}
}