Skip to content

Commit f7d9fef

Browse files
authored
Fix signature for the parse callback, needs implementation cleanup
1 parent b1f3b1a commit f7d9fef

File tree

23 files changed

+209
-204
lines changed

23 files changed

+209
-204
lines changed

libr/arch/p/6502/pseudo.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ typedef enum {
99
NORM = 2,
1010
} ADDR_TYPE;
1111

12-
static int replace(int argc, const char *argv[], char *newstr, ADDR_TYPE type) {
13-
int i, j, k;
12+
static char *replace(int argc, const char *argv[], ADDR_TYPE type) {
13+
int i, j;
1414
struct {
1515
int narg;
1616
const char *op;
@@ -63,44 +63,41 @@ static int replace(int argc, const char *argv[], char *newstr, ADDR_TYPE type) {
6363
{0, "sei", "set_interrupt" },
6464
{1, "jsr", "1()" },
6565
{0, NULL}};
66-
if (!newstr) {
67-
return false;
68-
}
6966

67+
RStrBuf *sb = r_strbuf_new ("");
7068
for (i = 0; ops[i].op; i++) {
7169
if (ops[i].narg) {
7270
if (argc - 1 != ops[i].narg) {
7371
continue;
7472
}
7573
}
7674
if (!strcmp (ops[i].op, argv[0])) {
77-
for (j = k = 0; ops[i].str[j] != '\0'; j++, k++) {
75+
for (j = 0; ops[i].str[j] != '\0'; j++) {
7876
if (isdigit(ops[i].str[j])) {
7977
const char *w = argv[ops[i].str[j] - '0'];
8078
if (w) {
81-
strcpy (newstr + k, w);
82-
k += strlen(w) - 1;
79+
r_strbuf_append (sb, w);
8380
}
8481
} else {
85-
newstr[k] = ops[i].str[j];
82+
const char ch = ops[i].str[j];
83+
r_strbuf_append_n (sb, &ch, 1);
8684
}
8785
}
88-
newstr[k] = '\0';
8986
if (argc == 4 && argv[2][0] == '[') {
90-
strcat (newstr + k, "+");
91-
strcat (newstr + k + 3, argv[2]);
87+
r_strbuf_append (sb, "+");
88+
r_strbuf_append (sb, argv[2]); // wtf+3?
89+
// strcat (newstr + k, "+");
90+
// strcat (newstr + k + 3, argv[2]);
9291
}
93-
return true;
92+
return r_strbuf_drain (sb);
9493
}
9594
}
9695

97-
/* TODO: this is slow */
98-
newstr[0] = '\0';
9996
for (i = 0; i < argc; i++) {
100-
strcat (newstr, argv[i]);
101-
strcat (newstr, (i == 0 || i == argc - 1) ? " " : ",");
97+
r_strbuf_append (sb, argv[i]);
98+
r_strbuf_append (sb, (i == 0 || i == argc - 1) ? " " : ",");
10299
}
103-
return false;
100+
return r_strbuf_drain (sb);
104101
}
105102

106103
static ADDR_TYPE addr_type(const char *str) {
@@ -115,18 +112,20 @@ static ADDR_TYPE addr_type(const char *str) {
115112
return NORM;
116113
}
117114

118-
static bool parse(RAsmPluginSession *s, const char *data, char *str) {
115+
static char *parse(RAsmPluginSession *s, const char *data) {
119116
char w0[256], w1[256], w2[256];
120117
int i, len = strlen (data);
121-
char *buf, *ptr, *optr;
118+
char *ptr, *optr;
122119
ADDR_TYPE atype;
120+
char *str = NULL;
123121

124122
if (len >= sizeof (w0)) {
125-
return false;
123+
return NULL;
126124
}
127125
// malloc can be slow here :?
128-
if (!(buf = malloc (len + 1))) {
129-
return false;
126+
char *buf = malloc (len + 1);
127+
if (!buf) {
128+
return NULL;
130129
}
131130
memcpy (buf, data, len + 1);
132131

@@ -167,12 +166,12 @@ static bool parse(RAsmPluginSession *s, const char *data, char *str) {
167166
nw++;
168167
}
169168
}
170-
replace (nw, wa, str, atype);
169+
str = replace (nw, wa, atype);
171170
}
172171

173172
free (buf);
174173

175-
return true;
174+
return str;
176175
}
177176

178177
RAsmPlugin r_asm_plugin_6502 = {

libr/arch/p/arm/pseudo.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,21 @@ static int replace(int argc, const char *argv[], char *newstr) {
215215
return false;
216216
}
217217

218-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
218+
static char *parse(RAsmPluginSession *aps, const char *data) {
219219
char w0[256], w1[256], w2[256], w3[256], w4[256];
220220
int i, len = strlen (data);
221221
char *buf, *ptr, *optr;
222222

223223
if (len >= sizeof (w0)) {
224-
return false;
224+
return NULL;
225225
}
226226
// malloc can be slow here :?
227227
if (!(buf = malloc (len + 1))) {
228-
return false;
228+
return NULL;
229229
}
230230
memcpy (buf, data, len + 1);
231+
char *str = malloc (strlen (data) + 128);
232+
strcpy (str, data);
231233
if (*buf) {
232234
*w0 = *w1 = *w2 = *w3 = *w4 = '\0';
233235
ptr = strchr (buf, ' ');
@@ -302,7 +304,7 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
302304
}
303305
free (buf);
304306
r_str_fixspaces (str);
305-
return true;
307+
return str;
306308
}
307309

308310
static char *subs_var_string(RParse *p, RAnalVarField *var, char *tstr, const char *oldstr, const char *reg, int delta) {

libr/arch/p/avr/pseudo.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static bool replace(int argc, const char *argv[], char *newstr) {
125125
}
126126

127127
#define WSZ 128
128-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
128+
static char *parse(RAsmPluginSession *aps, const char *data) {
129129
int i, len = strlen (data);
130130
char w0[WSZ];
131131
char w1[WSZ];
@@ -136,11 +136,13 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
136136

137137
// malloc can be slow here :?
138138
if (!(buf = malloc (len + 1))) {
139-
return false;
139+
return NULL;
140140
}
141141
memcpy (buf, data, len + 1);
142142

143143
r_str_trim (buf);
144+
char *str = malloc (len + 128);
145+
strcpy (str, data);
144146
if (*buf) {
145147
w0[0] = '\0';
146148
w1[0] = '\0';
@@ -205,7 +207,7 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
205207
}
206208
}
207209
free (buf);
208-
return true;
210+
return str;
209211
}
210212

211213
RAsmPlugin r_asm_plugin_avr = {

libr/arch/p/bpf/pseudo.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,21 @@ static int tokenize(const char* in, char* out[]) {
6666
return count;
6767
}
6868

69-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
69+
static char *parse(RAsmPluginSession *aps, const char *data) {
7070
int i;
7171
char *argv[MAXARGS] = { NULL, NULL, NULL, NULL };
7272
int argc = tokenize (data, argv);
7373

74+
char *str = malloc (strlen (data) + 128);
75+
strcpy (str, data);
7476
if (!replace (argc, argv, str, BUFSIZE)) {
7577
strcpy (str, data);
7678
}
7779
for (i = 0; i < MAXARGS; i++) {
7880
free (argv[i]);
7981
}
8082
r_str_fixspaces (str);
81-
return true;
83+
return str;
8284
}
8385

8486
RAsmPlugin r_asm_plugin_bpf = {

libr/arch/p/chip8/pseudo.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,25 @@ static int tokenize(const char* in, char* out[]) {
8888
return count;
8989
}
9090

91-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
91+
static char *parse(RAsmPluginSession *aps, const char *data) {
9292
int i;
9393
char *argv[MAXARGS] = { NULL, NULL, NULL, NULL };
9494
int argc = tokenize (data, argv);
95+
// XXX deprecate and make replace return the *
96+
char *str = malloc (BUFSIZE);
97+
r_str_ncpy (str, data, BUFSIZE);
9598

9699
if (!replace (argc, argv, str, BUFSIZE)) {
97100
strcpy (str, data);
98101
}
99102
for (i = 0; i < MAXARGS; i++) {
100103
free (argv[i]);
101104
}
102-
return true;
105+
if (!strcmp (str, data)) {
106+
free (str);
107+
return NULL;
108+
}
109+
return str;
103110
}
104111

105112
RAsmPlugin r_asm_plugin_chip8 = {

libr/arch/p/dalvik/pseudo.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/* radare - LGPL - Copyright 2012-2024 - pancake */
22

3-
#include <r_lib.h>
4-
#include <r_flag.h>
5-
#include <r_anal.h>
63
#include <r_asm.h>
74

8-
static int replace(int argc, const char *argv[], char *newstr) {
5+
static char *replace(int argc, const char *argv[]) {
96
int i, j, k;
107
struct {
118
const char *op;
@@ -183,32 +180,32 @@ static int replace(int argc, const char *argv[], char *newstr) {
183180
{ "mul-double", "1 = 2 * 3" },
184181
{ "move-wide", "1 = 2" },
185182
{ "move-wide/16", "1 = 2" },
183+
{ "return-void", "return" },
186184
{ "return-wide", "return (wide) 1" },
187185
{ "return-object", "return (object) 1" },
188186
// { "sget", "1 = 2[3]" },
189187
{ NULL }
190188
};
191189

190+
RStrBuf *sb = r_strbuf_new ("");
192191
for (i = 0; ops[i].op; i++) {
193192
if (!strcmp (ops[i].op, argv[0])) {
194-
if (newstr) {
195-
for (j = k = 0; ops[i].str[j] != '\0'; j++, k++) {
196-
if (ops[i].str[j] >= '1' && ops[i].str[j] <= '9') {
197-
const char *w = argv[ops[i].str[j] - '0'];
198-
if (w) {
199-
strcpy (newstr + k, w);
200-
k += strlen (w) - 1;
201-
}
202-
} else {
203-
newstr[k] = ops[i].str[j];
193+
for (j = k = 0; ops[i].str[j] != '\0'; j++, k++) {
194+
if (ops[i].str[j] >= '1' && ops[i].str[j] <= '9') {
195+
const char *w = argv[ops[i].str[j] - '0'];
196+
if (w) {
197+
r_strbuf_append (sb, w);
204198
}
199+
} else {
200+
char ch = ops[i].str[j];
201+
r_strbuf_append_n (sb, &ch, 1);
205202
}
206-
newstr[k] = '\0';
207203
}
208-
return true;
204+
return r_strbuf_drain (sb);
209205
}
210206
}
211-
207+
r_strbuf_free (sb);
208+
#if 0
212209
/* TODO: this is slow */
213210
if (newstr) {
214211
newstr[0] = '\0';
@@ -217,8 +214,8 @@ static int replace(int argc, const char *argv[], char *newstr) {
217214
strcat (newstr, (i == 0 || i== argc - 1)?" ":", ");
218215
}
219216
}
220-
221-
return false;
217+
#endif
218+
return NULL;
222219
}
223220

224221
static char *patch(RAsmPluginSession *aps, RAnalOp *aop, const char *op) {
@@ -248,7 +245,7 @@ static char *patch(RAsmPluginSession *aps, RAnalOp *aop, const char *op) {
248245
} \
249246
} while (0)
250247

251-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
248+
static char *parse(RAsmPluginSession *aps, const char *data) {
252249
int i, len = strlen (data);
253250
char *buf, *ptr, *optr, *ptr2;
254251
char w0[64];
@@ -261,8 +258,7 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
261258
|| !strcmp (data, "???")
262259
|| !strcmp (data, "nop")
263260
|| !strcmp (data, "DEPRECATED")) {
264-
str[0] = 0;
265-
return true;
261+
return strdup ("");
266262
}
267263

268264
// malloc can be slow here :?
@@ -273,21 +269,20 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
273269

274270
r_str_trim (buf);
275271

272+
char * str = NULL;
276273
if (*buf) {
277-
w0[0]='\0';
278-
w1[0]='\0';
279-
w2[0]='\0';
280-
w3[0]='\0';
281-
w4[0]='\0';
274+
w0[0] = '\0';
275+
w1[0] = '\0';
276+
w2[0] = '\0';
277+
w3[0] = '\0';
278+
w4[0] = '\0';
282279
ptr = strchr (buf, ' ');
283280
if (!ptr) {
284281
ptr = strchr (buf, '\t');
285282
}
286283
if (ptr) {
287284
*ptr = '\0';
288-
for (ptr++; *ptr == ' '; ptr++) {
289-
;
290-
}
285+
ptr = (char *)r_str_trim_head_ro (ptr + 1);
291286
strncpy (w0, buf, sizeof (w0) - 1);
292287
w0[sizeof (w0)-1] = '\0';
293288
strncpy (w1, ptr, sizeof (w1) - 1);
@@ -343,8 +338,8 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
343338
nw++;
344339
}
345340
}
346-
replace (nw, wa, str);
347-
{
341+
str = replace (nw, wa);
342+
if (str) {
348343
char *p = strdup (str);
349344
p = r_str_replace (p, "+ -", "- ", 0);
350345
#if EXPERIMENTAL_ZERO
@@ -362,13 +357,12 @@ static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
362357
REPLACE ("%s = %s >>", "%s >>=");
363358
REPLACE ("%s = %s <<", "%s <<=");
364359
}
365-
strcpy (str, p);
366-
free (p);
360+
str = p;
367361
}
368362
}
369363
}
370364
free (buf);
371-
return true;
365+
return str;
372366
}
373367

374368
RAsmPlugin r_asm_plugin_dalvik = {

libr/arch/p/evm/pseudo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ static int tokenize(const char* in, char* out[]) {
7070
return count;
7171
}
7272

73-
static bool parse(RAsmPluginSession *aps, const char *data, char *str) {
73+
static char *parse(RAsmPluginSession *aps, const char *data) {
7474
int i;
7575
char *argv[MAXARGS] = { NULL, NULL, NULL, NULL };
7676
int argc = tokenize (data, argv);
77-
77+
char *str = malloc (strlen (data) + 128);
78+
strcpy (str, data);
7879
if (!replace (argc, argv, str, BUFSIZE)) {
7980
strcpy (str, data);
8081
}
81-
8282
for (i = 0; i < MAXARGS; i++) {
8383
free (argv[i]);
8484
}
8585
r_str_fixspaces (str);
86-
return true;
86+
return str;
8787
}
8888

8989
RAsmPlugin r_asm_plugin_evm = {

0 commit comments

Comments
 (0)