Skip to content

Commit

Permalink
Move format processing to RzTypes from RzUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Mar 24, 2021
1 parent 87fe420 commit 6b48fe9
Show file tree
Hide file tree
Showing 21 changed files with 818 additions and 752 deletions.
2 changes: 0 additions & 2 deletions librz/analysis/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2235,5 +2235,3 @@ RZ_API RzList *rz_analysis_types_from_fcn(RzAnalysis *analysis, RzAnalysisFuncti
rz_list_free(type_used);
return uniq;
}


2 changes: 0 additions & 2 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -7603,5 +7603,3 @@ RZ_API void rz_core_analysis_cc_init(RzCore *core) {
free(dbpath);
free(dbhomepath);
}


13 changes: 8 additions & 5 deletions librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ RZ_API void rz_core_bin_export_info(RzCore *core, int mode) {
free(error_msg);
}
if (out) {
rz_analysis_save_parsed_type(core->analysis, out);
rz_type_save_parsed_type(core->analysis->type, out);
free(out);
}
}
Expand All @@ -250,7 +250,7 @@ RZ_API void rz_core_bin_export_info(RzCore *core, int mode) {
if (IS_MODE_RZCMD(mode)) {
rz_cons_printf("pf.%s %s\n", flagname, v);
} else if (IS_MODE_SET(mode)) {
sdb_set(core->print->formats, flagname, v, 0);
rz_type_format_set(core->analysis->type, flagname, v);
}
}
free(dup);
Expand All @@ -265,7 +265,7 @@ RZ_API void rz_core_bin_export_info(RzCore *core, int mode) {
offset = strdup("0");
}
flagname = dup;
int fmtsize = rz_print_format_struct_size(core->print, v, 0, 0);
int fmtsize = rz_type_format_struct_size(core->analysis->type, v, 0, 0);
char *offset_key = rz_str_newf("%s.offset", flagname);
const char *off = sdb_const_get(db, offset_key, 0);
free(offset_key);
Expand All @@ -277,11 +277,14 @@ RZ_API void rz_core_bin_export_info(RzCore *core, int mode) {
ut8 *buf = malloc(fmtsize);
if (buf) {
rz_io_read_at(core->io, addr, buf, fmtsize);
int res = rz_print_format(core->print, addr, buf,
char *format = rz_type_format_data(core->analysis->type, core->print, addr, buf,
fmtsize, v, 0, NULL, NULL);
free(buf);
if (res < 0) {
if (!format) {
eprintf("Warning: Cannot register invalid format (%s)\n", v);
} else {
rz_cons_print(format);
free(format);
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions librz/core/cmd_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ static int cmd_meta_others(RzCore *core, const char *input) {
if (p) {
p = (char *)rz_str_trim_head_ro(p);
if (*p == '.') {
const char *realformat = rz_type_format_byname(core->print, p + 1);
const char *realformat = rz_type_format_byname(core->analysis->type, p + 1);
if (realformat) {
p = (char *)realformat;
} else {
Expand All @@ -759,7 +759,7 @@ static int cmd_meta_others(RzCore *core, const char *input) {
}
}
if (n < 1) {
n = rz_type_format_struct_size(core->print, p, 0, 0);
n = rz_type_format_struct_size(core->analysis->type, p, 0, 0);
if (n < 1) {
eprintf("Warning: Cannot resolve struct size for '%s'\n", p);
n = 32; //
Expand All @@ -769,10 +769,13 @@ static int cmd_meta_others(RzCore *core, const char *input) {
if (n > core->blocksize) {
n = core->blocksize;
}
int r = rz_type_format(core->print, addr, core->block,
char *format = rz_type_format_data(core->analysis->type, core->print, addr, core->block,
n, p, 0, NULL, NULL);
if (r < 0) {
if (!format) {
n = -1;
} else {
rz_cons_print(format);
free(format);
}
} else {
eprintf("Usage: Cf [size] [pf-format-string]\n");
Expand Down
59 changes: 38 additions & 21 deletions librz/core/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "rz_core.h"
#include "rz_config.h"
#include "rz_util.h"
#include "rz_type.h"
#include "rz_types.h"
#include <limits.h>

Expand Down Expand Up @@ -1603,9 +1604,9 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
_input += 2;
if (*_input == '.') {
_input++;
val = sdb_get(core->print->formats, _input, NULL);
val = rz_type_format_get(core->analysis->type, _input);
if (val) {
rz_cons_printf("%d\n", rz_print_format_struct_size(core->print, val, mode, 0));
rz_cons_printf("%d\n", rz_type_format_struct_size(core->analysis->type, val, mode, 0));
} else {
eprintf("Struct %s not defined\nUsage: pfs.struct_name | pfs format\n", _input);
}
Expand All @@ -1614,7 +1615,7 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
_input++;
}
if (*_input) {
rz_cons_printf("%d\n", rz_print_format_struct_size(core->print, _input, mode, 0));
rz_cons_printf("%d\n", rz_type_format_struct_size(core->analysis->type, _input, mode, 0));
} else {
eprintf("Struct %s not defined\nUsage: pfs.struct_name | pfs format\n", _input);
}
Expand All @@ -1640,7 +1641,7 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
}
} else {
const char *struct_name = rz_str_trim_head_ro(_input);
const char *val = sdb_get(core->print->formats, struct_name, NULL);
const char *val = rz_type_format_get(core->analysis->type, struct_name);
if (val) {
rz_cons_printf("%s\n", val);
} else {
Expand Down Expand Up @@ -1743,19 +1744,19 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
core->print->num = core->num;
/* print all stored format */
if (!input[1] || !input[2]) { // "pf."
SdbListIter *iter;
SdbKv *kv;
SdbList *sdbls = sdb_foreach_list(core->print->formats, true);
ls_foreach (sdbls, iter, kv) {
rz_cons_printf("pf.%s %s\n", sdbkv_key(kv), sdbkv_value(kv));
RzListIter *iter;
char *fmt = NULL;
RzList *fmtl = rz_type_format_all(core->analysis->type);
rz_list_foreach (fmtl, iter, fmt) {
rz_cons_printf("pf.%s\n", fmt);
}
rz_list_free(fmtl);
/* delete a format */
} else if (input[1] && input[2] == '-') { // "pf-"
if (input[3] == '*') { // "pf-*"
sdb_free(core->print->formats);
core->print->formats = sdb_new0();
rz_type_format_purge(core->analysis->type);
} else { // "pf-xxx"
sdb_unset(core->print->formats, input + 3, 0);
rz_type_format_delete(core->analysis->type, input + 3);
}
} else {
char *name = strdup(input + (input[1] ? 2 : 1));
Expand All @@ -1776,15 +1777,15 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
eprintf("Struct or fields name can not contain dot symbol (.)\n");
} else {
// pf.foo=xxx
sdb_set(core->print->formats, name, space, 0);
rz_type_format_set(core->analysis->type, name, space);
}
free(name);
free(input);
return;
}

if (!strchr(name, '.') &&
!sdb_get(core->print->formats, name, NULL)) {
!rz_type_format_get(core->analysis->type, name)) {
eprintf("Cannot find '%s' format.\n", name);
free(name);
free(input);
Expand All @@ -1802,9 +1803,9 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
/* Load format from name into fmt to get the size */
/* This make sure the whole structure will be printed */
const char *fmt = NULL;
fmt = sdb_get(core->print->formats, name, NULL);
fmt = rz_type_format_get(core->analysis->type, name);
if (fmt) {
int size = rz_print_format_struct_size(core->print, fmt, mode, 0) + 10;
int size = rz_type_format_struct_size(core->analysis->type, fmt, mode, 0) + 10;
if (size > core->blocksize) {
rz_core_block_size(core, size);
}
Expand All @@ -1816,22 +1817,34 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
if (eq) { // Write mode (pf.field=value)
*eq++ = 0;
mode = RZ_PRINT_MUSTSET;
rz_print_format(core->print, core->offset,
char *format = rz_type_format_data(core->analysis->type, core->print, core->offset,
core->block, core->blocksize, name, mode, eq, dot);
if (format) {
rz_cons_print(format);
free(format);
}
} else {
rz_print_format(core->print, core->offset,
char *format = rz_type_format_data(core->analysis->type, core->print, core->offset,
core->block, core->blocksize, name, mode, NULL, dot);
if (format) {
rz_cons_print(format);
free(format);
}
}
} else {
rz_print_format(core->print, core->offset,
char *format = rz_type_format_data(core->analysis->type, core->print, core->offset,
core->block, core->blocksize, name, mode, NULL, NULL);
if (format) {
rz_cons_print(format);
free(format);
}
}
free(name);
}
} else {
/* This make sure the structure will be printed entirely */
const char *fmt = rz_str_trim_head_ro(input + 1);
int struct_sz = rz_print_format_struct_size(core->print, fmt, mode, 0);
int struct_sz = rz_type_format_struct_size(core->analysis->type, fmt, mode, 0);
int size = RZ_MAX(core->blocksize, struct_sz);
ut8 *buf = calloc(1, size);
if (!buf) {
Expand All @@ -1854,8 +1867,12 @@ static void cmd_print_format(RzCore *core, const char *_input, const ut8 *block,
}
free(args);
if (syntax_ok) {
rz_print_format(core->print, core->offset,
char *format = rz_type_format_data(core->analysis->type, core->print, core->offset,
buf, size, fmt, mode, NULL, NULL);
if (format) {
rz_cons_print(format);
free(format);
}
}
free(buf);
}
Expand Down
13 changes: 8 additions & 5 deletions librz/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ static void autocompleteFilename(RzLineCompletion *completion, RzLineBuffer *buf
static int autocomplete_pfele(RzCore *core, RzLineCompletion *completion, char *key, char *pfx, int idx, char *ptr) {
int i, ret = 0;
int len = strlen(ptr);
char *fmt = sdb_get(core->print->formats, key, NULL);
char *fmt = rz_type_format_get(core->analysis->type, key);
if (fmt) {
int nargs = rz_str_word_set0_stack(fmt);
if (nargs > 1) {
Expand Down Expand Up @@ -1666,6 +1666,7 @@ RZ_API void rz_core_autocomplete(RZ_NULLABLE RzCore *core, RzLineCompletion *com
ADDARG("gui.alt_background")
ADDARG("gui.border")
}
/*
} else if (!strncmp(buf->data, "pf.", 3) || !strncmp(buf->data, "pf*.", 4) || !strncmp(buf->data, "pfd.", 4) || !strncmp(buf->data, "pfv.", 4) || !strncmp(buf->data, "pfj.", 4)) {
char pfx[2];
int chr = (buf->data[2] == '.') ? 3 : 4;
Expand All @@ -1675,10 +1676,10 @@ RZ_API void rz_core_autocomplete(RZ_NULLABLE RzCore *core, RzLineCompletion *com
} else {
*pfx = 0;
}
SdbList *sls = sdb_foreach_list(core->print->formats, false);
SdbListIter *iter;
SdbKv *kv;
ls_foreach (sls, iter, kv) {
// FIXME: FORMATS
RzListIter *iter;
RzList *fmtl = rz_type_format_all(core->analysis->type);
rz_list_foreach (fmtl, iter, kv) {
int len = strlen(buf->data + chr);
int minlen = RZ_MIN(len, strlen(sdbkv_key(kv)));
if (!len || !strncmp(buf->data + chr, sdbkv_key(kv), minlen)) {
Expand All @@ -1693,6 +1694,7 @@ RZ_API void rz_core_autocomplete(RZ_NULLABLE RzCore *core, RzLineCompletion *com
}
}
}
*/
} else if ((!strncmp(buf->data, "afvn ", 5)) || (!strncmp(buf->data, "afan ", 5))) {
RzAnalysisFunction *fcn = rz_analysis_get_fcn_in(core->analysis, core->offset, 0);
RzList *vars;
Expand Down Expand Up @@ -2544,6 +2546,7 @@ RZ_API bool rz_core_init(RzCore *core) {
rz_io_bind(core->io, &(core->search->iob));
rz_io_bind(core->io, &(core->print->iob));
rz_io_bind(core->io, &(core->analysis->iob));
rz_io_bind(core->io, &(core->analysis->type->iob));
rz_io_bind(core->io, &(core->bin->iob));
rz_flag_bind(core->flags, &(core->analysis->flb));
core->analysis->flg_class_set = core_flg_class_set;
Expand Down
1 change: 0 additions & 1 deletion librz/core/ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,4 +1098,3 @@ RZ_IPI void rz_types_open_editor(RzCore *core, const char *typename) {
}
free(str);
}

6 changes: 5 additions & 1 deletion librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3123,8 +3123,12 @@ static bool ds_print_meta_infos(RDisasmState *ds, ut8 *buf, int len, int idx, in
case RZ_META_TYPE_FORMAT: {
rz_cons_printf("pf %s # size=%" PFMT64d "\n", mi->str, mi_size);
int len_before = rz_cons_get_buffer_len();
rz_print_format(core->print, ds->at, buf + idx,
char *format = rz_type_format_data(core->analysis->type, core->print, ds->at, buf + idx,
len - idx, mi->str, RZ_PRINT_MUSTSEE, NULL, NULL);
if (format) {
rz_cons_print(format);
free(format);
}
int len_after = rz_cons_get_buffer_len();
const char *cons_buf = rz_cons_get_buffer();
if (len_after > len_before && buf && cons_buf[len_after - 1] == '\n') {
Expand Down
2 changes: 1 addition & 1 deletion librz/core/vmenus.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ RZ_API int rz_core_visual_types(RzCore *core) {
}
} break;
case 'd':
rz_analysis_remove_parsed_type(core->analysis, vt.curname);
rz_type_remove_parsed_type(core->analysis->type, vt.curname);
break;
case '-':
rz_types_open_editor(core, NULL);
Expand Down
22 changes: 19 additions & 3 deletions librz/include/rz_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#define RZ_TYPE_H

#include <rz_types.h>
#include <rz_util/rz_num.h>
#include <rz_util/rz_print.h>
#include <rz_bind.h>
#include <rz_io.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -16,12 +20,16 @@ typedef struct rz_type_target_t {
const char *cpu;
int bits;
const char *os;
bool big_endian;
} RzTypeTarget;

typedef struct rz_type_t {
void *user;
Sdb *sdb_types;
Sdb *formats; // for `pf` formats
RzTypeTarget *target;
RNum *num;
RzIOBind iob; // for RzIO in formats
} RzType;

typedef struct rz_type_enum_case_t {
Expand Down Expand Up @@ -132,9 +140,18 @@ RZ_API bool rz_type_unlink(RzType *t, ut64 addr);
RZ_API bool rz_type_unlink_all(RzType *t);
RZ_API bool rz_type_link_offset(RzType *t, const char *val, ut64 addr);

// Type formats (`tp` and `pf` commands)
RZ_API const char *rz_type_format_get(RzType *t, const char *name);
RZ_API void rz_type_format_set(RzType *t, const char *name, const char *fmt);
RZ_API RZ_OWN RzList *rz_type_format_all(RzType *t);
RZ_API void rz_type_format_delete(RzType *t, const char *name);
RZ_API void rz_type_format_purge(RzType *t);

RZ_API char *rz_type_format(RzType *type, const char *t);
RZ_API int rz_type_format_struct_size(RzPrint *p, const char *f, int mode, int n);
RZ_API const char *rz_type_format_byname(RzPrint *p, const char *name);
RZ_API int rz_type_format_struct_size(RzType *t, const char *f, int mode, int n);
RZ_API const char *rz_type_format_byname(RzType *t, const char *name);
RZ_API char *rz_type_format_data(RzType *t, RzPrint *p, ut64 seek, const ut8 *b, const int len,
const char *formatname, int mode, const char *setval, char *ofield);

// Function prototypes api
RZ_API bool rz_type_func_exist(RzType *t, const char *func_name);
Expand All @@ -159,7 +176,6 @@ RZ_API RzList *rz_type_links(RzType *type);
RZ_API void rz_serialize_types_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzType *types);
RZ_API bool rz_serialize_types_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzType *types, RZ_NULLABLE RzSerializeResultInfo *res);


#endif

#ifdef __cplusplus
Expand Down
5 changes: 1 addition & 4 deletions librz/include/rz_util/rz_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ typedef struct rz_print_t {
RzPrintHasRefs hasrefs;
RzPrintCommentCallback get_comments;
RzPrintSectionGet get_section_name;
Sdb *formats;
Sdb *sdb_types;
RzCons *cons;
RzConsBind consbind;
Expand Down Expand Up @@ -176,9 +175,7 @@ RZ_API void rz_print_code(RzPrint *p, ut64 addr, const ut8 *buf, int len, char l
#define RZ_PRINT_DOT (1 << 7)
#define RZ_PRINT_QUIET (1 << 8)
#define RZ_PRINT_STRUCT (1 << 9)
RZ_API int rz_print_format_struct_size(RzPrint *p, const char *format, int mode, int n);
RZ_API int rz_print_format(RzPrint *p, ut64 seek, const ut8 *buf, const int len, const char *fmt, int elem, const char *setval, char *field);
RZ_API const char *rz_print_format_byname(RzPrint *p, const char *name);

RZ_API void rz_print_offset(RzPrint *p, ut64 off, int invert, int opt, int dec, int delta, const char *label);
RZ_API void rz_print_offset_sg(RzPrint *p, ut64 off, int invert, int offseg, int seggrn, int offdec, int delta, const char *label);
#define RZ_PRINT_STRING_WIDE 1
Expand Down
1 change: 0 additions & 1 deletion librz/type/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,3 @@ RZ_API void rz_type_save_base_type(const RzType *t, const RzBaseType *type) {
break;
}
}

Loading

0 comments on commit 6b48fe9

Please sign in to comment.