Skip to content

Commit 0338c43

Browse files
committed
Deprecate RParse.parse, make RParse.instruction into RParse.pseudo
1 parent cbdf085 commit 0338c43

File tree

6 files changed

+59
-50
lines changed

6 files changed

+59
-50
lines changed

libr/asm/asm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ R_API int r_asm_disassemble(RAsm *a, RAnalOp *op, const ut8 *buf, int len) {
409409
}
410410
}
411411
if (a->ofilter) {
412-
char *newtext = r_parse_instruction (a->ofilter, op->mnemonic);
412+
char *newtext = r_parse_pseudo (a->ofilter, op->mnemonic);
413413
if (newtext) {
414414
r_anal_op_set_mnemonic (op, op->addr, newtext);
415415
}
@@ -535,7 +535,7 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
535535
}
536536
ret = op.size;
537537
if (a->ofilter) {
538-
char *newtext = r_parse_instruction (a->ofilter, op.mnemonic);
538+
char *newtext = r_parse_pseudo (a->ofilter, op.mnemonic);
539539
if (newtext) {
540540
free (op.mnemonic);
541541
op.mnemonic = newtext;
@@ -564,8 +564,11 @@ R_API RAsmCode* r_asm_mdisassemble_hexstr(RAsm *a, RParse *p, const char *hexstr
564564
}
565565
RAsmCode *ret = r_asm_mdisassemble (a, buf, (ut64)len);
566566
if (ret && p) {
567-
// XXX this can crash the output buffer
568-
r_parse_parse (p, ret->assembly, ret->assembly);
567+
char *res = r_parse_pseudo (p, ret->assembly);
568+
if (res) {
569+
free (ret->assembly);
570+
ret->assembly = res;
571+
}
569572
}
570573
free (buf);
571574
return ret;

libr/asm/parse.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ typedef struct r_parse_session_t {
2727
char *retleave_asm;
2828
} RParseSession;
2929

30+
#endif
31+
3032
R_API RParseSession *r_parse_new_session(RParse *p, const char *name) {
3133
if (r_parse_use (p, name)) {
3234
RParseSession *ps = R_NEW0 (RParseSession);
@@ -36,7 +38,8 @@ R_API RParseSession *r_parse_new_session(RParse *p, const char *name) {
3638
}
3739
return NULL;
3840
}
39-
#endif
41+
42+
// R_API bool r_parse_session_
4043

4144
R_API RParse *r_parse_new(void) {
4245
RParse *p = R_NEW0 (RParse);
@@ -151,31 +154,19 @@ R_API bool r_parse_use(RParse *p, const char *name) {
151154
return true;
152155
}
153156

154-
// data is input disasm, str is output pseudo
155-
// TODO: refactoring, this should return char * instead
156-
// like parseHeap()
157-
R_API char *r_parse_instruction(RParse *p, const char *data) {
157+
R_API char *r_parse_pseudo(RParse *p, const char *data) {
158158
R_RETURN_VAL_IF_FAIL (p && data, false);
159159
char *str = malloc (32 + strlen (data) * 2);
160160
strcpy (str, data);
161-
bool bres = (p && p->cur && p->cur->parse)
162-
? p->cur->parse (p, data, str) : false;
161+
RParsePluginParse parse = R_UNWRAP3 (p, cur, parse);
162+
bool bres = parse? parse (p, data, str) : false;
163163
if (bres) {
164164
return str;
165165
}
166166
free (str);
167167
return NULL;
168168
}
169169

170-
// TODO deprecate in R2_600 because r_parse_instruction is better
171-
// TODO worst api name ever
172-
R_API bool r_parse_parse(RParse *p, const char *data, char *str) {
173-
R_RETURN_VAL_IF_FAIL (p && data && str, false);
174-
RParsePluginParse parse = R_UNWRAP3 (p, cur, parse);
175-
return (*data && parse)? parse (p, data, str): false;
176-
// causes pdc to be empty, we need that parser to be doing sthg
177-
}
178-
179170
R_API char *r_parse_immtrim(RParse *p, const char *_opstr) {
180171
if (R_STR_ISEMPTY (_opstr)) {
181172
return NULL;

libr/core/cmd_anal.inc.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,9 +2508,8 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int
25082508
pj_ks (pj, "disasm", strsub);
25092509
// apply pseudo if needed
25102510
{
2511-
char *pseudo = calloc (128 + strlen (strsub), 3);
2512-
r_parse_parse (core->parser, strsub, pseudo);
2513-
if (pseudo && *pseudo) {
2511+
char *pseudo = r_parse_pseudo (core->parser, strsub);
2512+
if (R_STR_ISNOTEMPTY (pseudo)) {
25142513
pj_ks (pj, "pseudo", pseudo);
25152514
}
25162515
free (pseudo);
@@ -2712,9 +2711,8 @@ static void core_anal_bytes(RCore *core, const ut8 *buf, int len, int nops, int
27122711
}
27132712
printline ("disasm", "%s\n", disasm);
27142713
{
2715-
char *pseudo = calloc (128 + strlen (disasm), 3);
2716-
r_parse_parse (core->parser, disasm, pseudo);
2717-
if (pseudo && *pseudo) {
2714+
char *pseudo = r_parse_pseudo (core->parser, disasm);
2715+
if (R_STR_ISNOTEMPTY (pseudo)) {
27182716
printline ("pseudo", "%s\n", pseudo);
27192717
}
27202718
free (pseudo);

libr/core/disasm.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,9 @@ static void ds_build_op_str(RDisasmState *ds, bool print_color) {
12131213
}
12141214
}
12151215
if (ds->pseudo) {
1216-
if (r_parse_parse (core->parser, ds->opstr, ds->str)) {
1216+
char *res = r_parse_pseudo (core->parser, ds->opstr);
1217+
if (res) {
1218+
r_str_ncpy (ds->str, res, sizeof (ds->str));
12171219
R_LOG_DEBUG ("asm.parse.pseudo (%s) -> (%s)", ds->opstr, ds->str);
12181220
if (R_STR_ISNOTEMPTY (ds->str)) {
12191221
free (ds->opstr);
@@ -3061,9 +3063,12 @@ static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len) {
30613063
if (!str) {
30623064
str = ds->str;
30633065
}
3064-
r_parse_parse (core->parser, str, ds->str);
3065-
free (ds->opstr);
3066-
ds->opstr = strdup (ds->str);
3066+
char *res = r_parse_pseudo (core->parser, str);
3067+
if (res) {
3068+
r_str_ncpy (ds->str, res, sizeof (ds->str));
3069+
free (ds->opstr);
3070+
ds->opstr = strdup (ds->str);
3071+
}
30673072
#else
30683073
char *str = ds->opstr? ds->opstr: ds->str;
30693074
char *s = r_parse_instruction (core->parser, str);
@@ -6488,11 +6493,11 @@ R_API int r_core_print_disasm(RCore *core, ut64 addr, ut8 *buf, int len, int cou
64886493
}
64896494
if (!ds->show_cmt_right) {
64906495
if (ds->show_cmt_pseudo) {
6491-
char *opstr = malloc (32 + strlen (ds->analop.mnemonic));
6492-
strcpy (opstr, ds->analop.mnemonic);
6493-
r_parse_parse (core->parser, opstr, opstr);
6494-
ds_comment (ds, true, "%s", opstr);
6495-
free (opstr);
6496+
char *res = r_parse_pseudo (core->parser, ds->analop.mnemonic);
6497+
if (res) {
6498+
ds_comment (ds, true, "%s", res);
6499+
free (res);
6500+
}
64966501
}
64976502
if (ds->show_cmt_esil) {
64986503
const char *esil = R_STRBUF_SAFEGET (&ds->analop.esil);
@@ -6818,11 +6823,11 @@ R_API int r_core_print_disasm(RCore *core, ut64 addr, ut8 *buf, int len, int cou
68186823
ds_cdiv_optimization (ds);
68196824
if ((ds->show_comments || ds->show_cmt_user) && ds->show_cmt_right) {
68206825
if (ds->show_cmt_pseudo) {
6821-
char *opstr = malloc (32 + strlen (ds->analop.mnemonic));
6822-
strcpy (opstr, ds->analop.mnemonic);
6823-
r_parse_parse (core->parser, opstr, opstr);
6824-
ds_comment (ds, true, "%s", opstr);
6825-
free (opstr);
6826+
char *res = r_parse_pseudo (core->parser, ds->analop.mnemonic);
6827+
if (res) {
6828+
ds_comment (ds, true, "%s", res);
6829+
free (res);
6830+
}
68266831
}
68276832
if (ds->show_cmt_esil) {
68286833
const char *esil = R_STRBUF_SAFEGET (&ds->analop.esil);
@@ -7379,7 +7384,9 @@ R_IPI int r_core_print_disasm_json_ipi(RCore *core, ut64 addr, ut8 *buf, int nb_
73797384
r_anal_op (core->anal, &ds->analop, at, buf + i, nb_bytes - i, R_ARCH_OP_MASK_ALL);
73807385

73817386
if (ds->pseudo) {
7382-
r_parse_parse (core->parser, opstr, opstr);
7387+
char *res = r_parse_pseudo (core->parser, opstr);
7388+
r_str_ncpy (opstr, res, sizeof (opstr));
7389+
free (res);
73837390
}
73847391

73857392
// f = r_anal_get_fcn_in (core->anal, at,

libr/include/r_parse.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ typedef struct r_parse_t {
2828
int maxflagnamelen;
2929
int minval;
3030
char *retleave_asm;
31-
struct r_parse_plugin_t *cur;
32-
// RAnal *anal; // weak anal ref XXX do not use. use analb.anal
31+
struct r_parse_plugin_t *cur; // XXX move into session
3332
RList *parsers;
3433
RAnalVarList varlist;
3534
st64 (*get_ptr_at)(RAnalFunction *fcn, st64 delta, ut64 addr);
@@ -38,7 +37,7 @@ typedef struct r_parse_t {
3837
RAnalBind analb;
3938
RFlagGetAtAddr flag_get; // XXX
4039
RAnalLabelAt label_get;
41-
} RParse; // TODO rename to RAsmParseState
40+
} RParse;
4241

4342
typedef int (*RParsePluginParse)(RParse *p, const char *data, char *str);
4443
typedef int (*RParsePluginFilter)(RParse *p, ut64 addr, RFlag *f, char *data, char *str, int len, bool big_endian);
@@ -55,6 +54,12 @@ typedef struct r_parse_plugin_t {
5554
RParsePluginSubvar subvar;
5655
} RParsePlugin;
5756

57+
typedef struct r_parse_session_t {
58+
RParse *p;
59+
RParsePlugin *cur;
60+
void *data;
61+
} RParseSession;
62+
5863
#ifdef R_API
5964

6065
/* lifecycle */
@@ -67,9 +72,14 @@ R_API bool r_parse_plugin_add(RParse *p, RParsePlugin *plugin);
6772
R_API bool r_parse_plugin_remove(RParse *p, RParsePlugin *plugin);
6873
R_API bool r_parse_use(RParse *p, const char *name);
6974

75+
#define R_PARSE_FILTER_IMMTRIM 1
76+
#define R_PARSE_FILTER_SUBVAR 2
77+
#define R_PARSE_FILTER_FILTER 4
78+
#define R_PARSE_FILTER_INSTRUCTION 8
79+
7080
/* action */
71-
R_API bool r_parse_parse(RParse *p, const char *data, char *str);
72-
R_API char *r_parse_instruction(RParse *p, const char *data);
81+
// DEPRECATED R_API bool r_parse_parse(RParse *p, const char *data, char *str);
82+
R_API char *r_parse_pseudo(RParse *p, const char *data);
7383
R_API bool r_parse_filter(RParse *p, ut64 addr, RFlag *f, RAnalHint *hint, char *data, char *str, int len, bool big_endian);
7484
R_API bool r_parse_subvar(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
7585
R_API char *r_parse_immtrim(RParse *p, const char *opstr);

test/db/cmd/cmd_pd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ EXPECT=<<EOF
7171
EOF
7272
RUN
7373

74-
NAME=pd pseudo=1 cmtright=0
74+
NAME=pd pseudo=1 cmt.right=0
7575
FILE=malloc://128
7676
CMDS=<<EOF
7777
wx 4889e5
7878
e asm.arch=x86
7979
e asm.bits=64
8080
e asm.pseudo=1
81-
e asm.cmtright =0
81+
e asm.cmt.right =0
8282
pd 1
8383
EOF
8484
EXPECT=<<EOF
@@ -216,9 +216,9 @@ e asm.bits=64
216216
wx 31ed
217217
e asm.pseudo=true
218218
e asm.comments=true
219-
e asm.cmtright=true
219+
e asm.cmt.right=true
220220
pd 1
221-
e asm.cmtright=false
221+
e asm.cmt.right=false
222222
pd 1
223223
EOF
224224
EXPECT=<<EOF

0 commit comments

Comments
 (0)