Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor output modes to use enum (#489) #929

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions librz/analysis/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,16 +1257,16 @@ static void rz_analysis_class_list_json(RzAnalysis *analysis) {
pj_free(j);
}

RZ_API void rz_analysis_class_list(RzAnalysis *analysis, int mode) {
if (mode == 'j') {
RZ_API void rz_analysis_class_list(RzAnalysis *analysis, RzOutputMode mode) {
if (mode == RZ_OUTPUT_MODE_JSON) {
rz_analysis_class_list_json(analysis);
return;
}

SdbList *classes = rz_analysis_class_get_all(analysis, mode != '*');
SdbList *classes = rz_analysis_class_get_all(analysis, mode != RZ_OUTPUT_MODE_RIZIN);
SdbListIter *iter;
SdbKv *kv;
if (mode == '*') {
if (mode == RZ_OUTPUT_MODE_RIZIN) {
ls_foreach (classes, iter, kv) {
// need to create all classes first, so they can be referenced
rz_cons_printf("ac %s\n", sdbkv_key(kv));
Expand All @@ -1276,7 +1276,7 @@ RZ_API void rz_analysis_class_list(RzAnalysis *analysis, int mode) {
}
} else {
ls_foreach (classes, iter, kv) {
rz_analysis_class_print(analysis, sdbkv_key(kv), mode == 'l');
rz_analysis_class_print(analysis, sdbkv_key(kv), mode == RZ_OUTPUT_MODE_LONG);
}
}
ls_free(classes);
Expand Down
40 changes: 20 additions & 20 deletions librz/analysis/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ RZ_API const char *rz_meta_type_to_string(int type) {
return "# unknown meta # ";
}

RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, int rad, PJ *pj, bool show_full) {
rz_return_if_fail(!(rad == 'j' && !pj)); // rad == 'j' => pj != NULL
RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, RzOutputMode mode, PJ *pj, bool show_full) {
rz_return_if_fail(!(mode == RZ_OUTPUT_MODE_JSON && !pj)); // mode == 'j' => pj != NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also update the commentary or just remove it.

char *pstr, *base64_str;
RzCore *core = a->coreb.core;
bool esc_bslash = core ? core->print->esc_bslash : false;
Expand Down Expand Up @@ -285,8 +285,8 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
pstr = d->str;
}
// rz_str_sanitize (str);
switch (rad) {
case 'j':
switch (mode) {
case RZ_OUTPUT_MODE_JSON:
pj_o(pj);
pj_kn(pj, "offset", start);
pj_ks(pj, "type", rz_meta_type_to_string(d->type));
Expand Down Expand Up @@ -339,7 +339,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
break;
case 0:
case 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would remove 0 and 1.

case '*':
case RZ_OUTPUT_MODE_RIZIN:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to define this if it goes into the default switch case.

Copy link
Contributor Author

@valdaarhun valdaarhun Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I will remove all the three cases.

default:
switch (d->type) {
case RZ_META_TYPE_COMMENT: {
Expand All @@ -348,7 +348,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
if (!s) {
s = strdup(pstr);
}
if (rad) {
if (mode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be if (mode), code doesn't make sense in JSON mode, for example. Lets just use some specific mode here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XVilka Do you mean I should change it to something more specific like if (mode == RZ_OUTPUT_MODE_STANDARD)?

if (!strcmp(type, "CCu")) {
a->cb_printf("%s base64:%s @ 0x%08" PFMT64x "\n",
type, s, start);
Expand All @@ -370,7 +370,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
free(s);
} break;
case RZ_META_TYPE_STRING:
if (rad) {
if (mode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and below.

char cmd[] = "Cs#";
switch (d->subtype) {
case 'a':
Expand Down Expand Up @@ -402,7 +402,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
break;
case RZ_META_TYPE_HIDE:
case RZ_META_TYPE_DATA:
if (rad) {
if (mode) {
a->cb_printf("%s %" PFMT64u " @ 0x%08" PFMT64x "\n",
rz_meta_type_to_string(d->type),
size, start);
Expand All @@ -419,7 +419,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
break;
case RZ_META_TYPE_MAGIC:
case RZ_META_TYPE_FORMAT:
if (rad) {
if (mode) {
a->cb_printf("%s %" PFMT64u " %s @ 0x%08" PFMT64x "\n",
rz_meta_type_to_string(d->type),
size, pstr, start);
Expand All @@ -434,7 +434,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
}
break;
case RZ_META_TYPE_VARTYPE:
if (rad) {
if (mode) {
a->cb_printf("%s %s @ 0x%08" PFMT64x "\n",
rz_meta_type_to_string(d->type), pstr, start);
} else {
Expand All @@ -450,7 +450,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
// TODO: d->size
} break;
default:
if (rad) {
if (mode) {
a->cb_printf("%s %" PFMT64u " 0x%08" PFMT64x " # %s\n",
rz_meta_type_to_string(d->type),
size, start, pstr);
Expand All @@ -470,22 +470,22 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
}
}

RZ_API void rz_meta_print_list_at(RzAnalysis *a, ut64 addr, int rad) {
RZ_API void rz_meta_print_list_at(RzAnalysis *a, ut64 addr, RzOutputMode mode) {
RzPVector *nodes = collect_nodes_at(a, RZ_META_TYPE_ANY, rz_spaces_current(&a->meta_spaces), addr);
if (!nodes) {
return;
}
void **it;
rz_pvector_foreach (nodes, it) {
RzIntervalNode *node = *it;
rz_meta_print(a, node->data, node->start, rz_meta_node_size(node), rad, NULL, true);
rz_meta_print(a, node->data, node->start, rz_meta_node_size(node), mode, NULL, true);
}
rz_pvector_free(nodes);
}

static void print_meta_list(RzAnalysis *a, int type, int rad, ut64 addr) {
static void print_meta_list(RzAnalysis *a, int type, RzOutputMode mode, ut64 addr) {
PJ *pj = NULL;
if (rad == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj = pj_new();
if (!pj) {
return;
Expand All @@ -511,7 +511,7 @@ static void print_meta_list(RzAnalysis *a, int type, int rad, ut64 addr) {
if (fcn && !rz_analysis_function_contains(fcn, node->start)) {
continue;
}
rz_meta_print(a, item, node->start, rz_meta_node_size(node), rad, pj, true);
rz_meta_print(a, item, node->start, rz_meta_node_size(node), mode, pj, true);
}

beach:
Expand All @@ -522,12 +522,12 @@ static void print_meta_list(RzAnalysis *a, int type, int rad, ut64 addr) {
}
}

RZ_API void rz_meta_print_list_all(RzAnalysis *a, int type, int rad) {
print_meta_list(a, type, rad, UT64_MAX);
RZ_API void rz_meta_print_list_all(RzAnalysis *a, int type, RzOutputMode mode) {
print_meta_list(a, type, mode, UT64_MAX);
}

RZ_API void rz_meta_print_list_in_function(RzAnalysis *a, int type, int rad, ut64 addr) {
print_meta_list(a, type, rad, addr);
RZ_API void rz_meta_print_list_in_function(RzAnalysis *a, int type, RzOutputMode mode, ut64 addr) {
print_meta_list(a, type, mode, addr);
}

RZ_API void rz_meta_rebase(RzAnalysis *analysis, ut64 diff) {
Expand Down
8 changes: 4 additions & 4 deletions librz/analysis/rtti.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ RZ_API char *rz_analysis_rtti_demangle_class_name(RzAnalysis *analysis, const ch
return rz_analysis_rtti_itanium_demangle_class_name(&context, name);
}

RZ_API void rz_analysis_rtti_print_at_vtable(RzAnalysis *analysis, ut64 addr, int mode) {
bool use_json = mode == 'j';
RZ_API void rz_analysis_rtti_print_at_vtable(RzAnalysis *analysis, ut64 addr, RzOutputMode mode) {
bool use_json = mode == RZ_OUTPUT_MODE_JSON;
if (use_json) {
rz_cons_print("[");
}
Expand All @@ -33,11 +33,11 @@ RZ_API void rz_analysis_rtti_print_at_vtable(RzAnalysis *analysis, ut64 addr, in
}
}

RZ_API void rz_analysis_rtti_print_all(RzAnalysis *analysis, int mode) {
RZ_API void rz_analysis_rtti_print_all(RzAnalysis *analysis, RzOutputMode mode) {
RVTableContext context;
rz_analysis_vtable_begin(analysis, &context);

bool use_json = mode == 'j';
bool use_json = mode == RZ_OUTPUT_MODE_JSON;
if (use_json) {
rz_cons_print("[");
}
Expand Down
4 changes: 2 additions & 2 deletions librz/analysis/rtti_itanium.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ static void rtti_itanium_type_info_free(void *info) {
}
}

RZ_API bool rz_analysis_rtti_itanium_print_at_vtable(RVTableContext *context, ut64 addr, int mode) {
bool use_json = mode == 'j';
RZ_API bool rz_analysis_rtti_itanium_print_at_vtable(RVTableContext *context, ut64 addr, RzOutputMode mode) {
bool use_json = mode == RZ_OUTPUT_MODE_JSON;
class_type_info *cti = rtti_itanium_type_info_new(context, addr);
if (!cti) {
return false;
Expand Down
22 changes: 11 additions & 11 deletions librz/analysis/rtti_msvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,14 @@ RZ_API char *rz_analysis_rtti_msvc_demangle_class_name(RVTableContext *context,
return ret;
}

RZ_API void rz_analysis_rtti_msvc_print_complete_object_locator(RVTableContext *context, ut64 addr, int mode) {
RZ_API void rz_analysis_rtti_msvc_print_complete_object_locator(RVTableContext *context, ut64 addr, RzOutputMode mode) {
rtti_complete_object_locator col;
if (!rtti_msvc_read_complete_object_locator(context, addr, &col)) {
eprintf("Failed to parse Complete Object Locator at 0x%08" PFMT64x "\n", addr);
return;
}

if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
if (!pj) {
return;
Expand All @@ -436,14 +436,14 @@ RZ_API void rz_analysis_rtti_msvc_print_complete_object_locator(RVTableContext *
}
}

RZ_API void rz_analysis_rtti_msvc_print_type_descriptor(RVTableContext *context, ut64 addr, int mode) {
RZ_API void rz_analysis_rtti_msvc_print_type_descriptor(RVTableContext *context, ut64 addr, RzOutputMode mode) {
rtti_type_descriptor td = { 0 };
if (!rtti_msvc_read_type_descriptor(context, addr, &td)) {
eprintf("Failed to parse Type Descriptor at 0x%08" PFMT64x "\n", addr);
return;
}

if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
if (!pj) {
return;
Expand All @@ -458,14 +458,14 @@ RZ_API void rz_analysis_rtti_msvc_print_type_descriptor(RVTableContext *context,
rtti_type_descriptor_fini(&td);
}

RZ_API void rz_analysis_rtti_msvc_print_class_hierarchy_descriptor(RVTableContext *context, ut64 addr, int mode) {
RZ_API void rz_analysis_rtti_msvc_print_class_hierarchy_descriptor(RVTableContext *context, ut64 addr, RzOutputMode mode) {
rtti_class_hierarchy_descriptor chd;
if (!rtti_msvc_read_class_hierarchy_descriptor(context, addr, &chd)) {
eprintf("Failed to parse Class Hierarchy Descriptor at 0x%08" PFMT64x "\n", addr);
return;
}

if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
if (!pj) {
return;
Expand All @@ -478,14 +478,14 @@ RZ_API void rz_analysis_rtti_msvc_print_class_hierarchy_descriptor(RVTableContex
}
}

RZ_API void rz_analysis_rtti_msvc_print_base_class_descriptor(RVTableContext *context, ut64 addr, int mode) {
RZ_API void rz_analysis_rtti_msvc_print_base_class_descriptor(RVTableContext *context, ut64 addr, RzOutputMode mode) {
rtti_base_class_descriptor bcd;
if (!rtti_msvc_read_base_class_descriptor(context, addr, &bcd)) {
eprintf("Failed to parse Base Class Descriptor at 0x%08" PFMT64x "\n", addr);
return;
}

if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
if (!pj) {
return;
Expand All @@ -498,7 +498,7 @@ RZ_API void rz_analysis_rtti_msvc_print_base_class_descriptor(RVTableContext *co
}
}

static bool rtti_msvc_print_complete_object_locator_recurse(RVTableContext *context, ut64 atAddress, int mode, bool strict) {
static bool rtti_msvc_print_complete_object_locator_recurse(RVTableContext *context, ut64 atAddress, RzOutputMode mode, bool strict) {
ut64 colRefAddr = atAddress - context->word_size;
ut64 colAddr;
if (!context->read_addr(context->analysis, colRefAddr, &colAddr)) {
Expand Down Expand Up @@ -552,7 +552,7 @@ static bool rtti_msvc_print_complete_object_locator_recurse(RVTableContext *cont
}

// print
bool use_json = mode == 'j';
bool use_json = mode == RZ_OUTPUT_MODE_JSON;
PJ *pj = NULL;
if (use_json) {
pj = pj_new();
Expand Down Expand Up @@ -616,7 +616,7 @@ static bool rtti_msvc_print_complete_object_locator_recurse(RVTableContext *cont
return true;
}

RZ_API bool rz_analysis_rtti_msvc_print_at_vtable(RVTableContext *context, ut64 addr, int mode, bool strict) {
RZ_API bool rz_analysis_rtti_msvc_print_at_vtable(RVTableContext *context, ut64 addr, RzOutputMode mode, bool strict) {
return rtti_msvc_print_complete_object_locator_recurse(context, addr, mode, strict);
}

Expand Down
14 changes: 7 additions & 7 deletions librz/analysis/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1175,18 +1175,18 @@ static int regvar_comparator(const RzAnalysisVar *a, const RzAnalysisVar *b) {
return (a && b) ? (a->argnum > b->argnum) - (a->argnum < b->argnum) : 0;
}

RZ_API void rz_analysis_var_list_show(RzAnalysis *analysis, RzAnalysisFunction *fcn, int kind, int mode, PJ *pj) {
RZ_API void rz_analysis_var_list_show(RzAnalysis *analysis, RzAnalysisFunction *fcn, int kind, RzOutputMode mode, PJ *pj) {
RzList *list = rz_analysis_var_list(analysis, fcn, kind);
RzAnalysisVar *var;
RzListIter *iter;
if (!pj && mode == 'j') {
if (!pj && mode == RZ_OUTPUT_MODE_JSON) {
return;
}
if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_a(pj);
}
if (!list) {
if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_end(pj);
}
return;
Expand All @@ -1197,7 +1197,7 @@ RZ_API void rz_analysis_var_list_show(RzAnalysis *analysis, RzAnalysisFunction *
continue;
}
switch (mode) {
case '*':
case RZ_OUTPUT_MODE_RIZIN:
// we can't express all type info here :(
if (kind == RZ_ANALYSIS_VAR_KIND_REG) { // registers
RzRegItem *i = rz_reg_index_get(analysis->reg, var->delta);
Expand All @@ -1216,7 +1216,7 @@ RZ_API void rz_analysis_var_list_show(RzAnalysis *analysis, RzAnalysisFunction *
fcn->addr);
}
break;
case 'j':
case RZ_OUTPUT_MODE_JSON:
switch (var->kind) {
case RZ_ANALYSIS_VAR_KIND_BPV: {
st64 delta = (st64)var->delta + fcn->bp_off;
Expand Down Expand Up @@ -1311,7 +1311,7 @@ RZ_API void rz_analysis_var_list_show(RzAnalysis *analysis, RzAnalysisFunction *
}
}
}
if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_end(pj);
}
rz_list_free(list);
Expand Down
6 changes: 3 additions & 3 deletions librz/analysis/vtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ RZ_API RzList *rz_analysis_vtable_search(RVTableContext *context) {
return vtables;
}

RZ_API void rz_analysis_list_vtables(RzAnalysis *analysis, int rad) {
RZ_API void rz_analysis_list_vtables(RzAnalysis *analysis, RzOutputMode mode) {
RVTableContext context;
rz_analysis_vtable_begin(analysis, &context);

Expand All @@ -296,7 +296,7 @@ RZ_API void rz_analysis_list_vtables(RzAnalysis *analysis, int rad) {

RzList *vtables = rz_analysis_vtable_search(&context);

if (rad == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
PJ *pj = pj_new();
if (!pj) {
return;
Expand All @@ -320,7 +320,7 @@ RZ_API void rz_analysis_list_vtables(RzAnalysis *analysis, int rad) {
pj_end(pj);
rz_cons_println(pj_string(pj));
pj_free(pj);
} else if (rad == '*') {
} else if (mode == RZ_OUTPUT_MODE_RIZIN) {
rz_list_foreach (vtables, vtableIter, table) {
rz_cons_printf("f vtable.0x%08" PFMT64x " %" PFMT64d " @ 0x%08" PFMT64x "\n",
table->saddr,
Expand Down
Loading