Rizin
unix-like reverse engineering framework and cli tools
rz_print.h
Go to the documentation of this file.
1 #ifndef RZ_PRINT_H
2 #define RZ_PRINT_H
3 
4 #include "rz_types.h"
5 #include "rz_cons.h"
6 #include "rz_bind.h"
7 #include "rz_io.h"
8 #include "rz_reg.h"
9 #include <rz_util/rz_strbuf.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #define RZ_PRINT_FLAGS_COLOR 0x00000001
16 #define RZ_PRINT_FLAGS_ADDRMOD 0x00000002
17 #define RZ_PRINT_FLAGS_CURSOR 0x00000004
18 #define RZ_PRINT_FLAGS_HEADER 0x00000008
19 #define RZ_PRINT_FLAGS_SPARSE 0x00000010
20 #define RZ_PRINT_FLAGS_SEGOFF 0x00000020
21 #define RZ_PRINT_FLAGS_OFFSET 0x00000040
22 #define RZ_PRINT_FLAGS_REFS 0x00000080
23 #define RZ_PRINT_FLAGS_DIFFOUT 0x00000100 /* only show different rows in `cc` hexdiffing */
24 #define RZ_PRINT_FLAGS_ADDRDEC 0x00000200
25 #define RZ_PRINT_FLAGS_COMMENT 0x00000400
26 #define RZ_PRINT_FLAGS_COMPACT 0x00000800
27 #define RZ_PRINT_FLAGS_NONHEX 0x00001000
28 #define RZ_PRINT_FLAGS_SECSUB 0x00002000
29 #define RZ_PRINT_FLAGS_RAINBOW 0x00004000
30 #define RZ_PRINT_FLAGS_HDROFF 0x00008000
31 #define RZ_PRINT_FLAGS_STYLE 0x00010000
32 #define RZ_PRINT_FLAGS_NONASCII 0x00020000
33 #define RZ_PRINT_FLAGS_ALIGN 0x00040000
34 #define RZ_PRINT_FLAGS_UNALLOC 0x00080000
35 #define RZ_PRINT_FLAGS_BGFILL 0x00100000
36 #define RZ_PRINT_FLAGS_SECTION 0x00200000
37 
38 typedef const char *(*RzPrintNameCallback)(void *user, ut64 addr);
39 typedef int (*RzPrintSizeCallback)(void *user, ut64 addr);
40 typedef char *(*RzPrintCommentCallback)(void *user, ut64 addr);
41 typedef const char *(*RzPrintSectionGet)(void *user, ut64 addr);
42 typedef const char *(*RzPrintColorFor)(void *user, ut64 addr, bool verbose);
43 typedef char *(*RzPrintHasRefs)(void *user, ut64 addr, int mode);
44 
45 typedef enum {
46  RZ_ASM_TOKEN_UNKNOWN = 0, //< Does not fit to any token below.
47  RZ_ASM_TOKEN_MNEMONIC, //< Asm mnemonics like: mov, push, lea...
48  RZ_ASM_TOKEN_OPERATOR, //< Arithmetic operators: +,-,<< etc.
49  RZ_ASM_TOKEN_NUMBER, //< Numbers
50  RZ_ASM_TOKEN_REGISTER, //< Registers
51  RZ_ASM_TOKEN_SEPARATOR, //< Brackets, comma etc.
52  RZ_ASM_TOKEN_META, //< Meta information (e.g Hexagon packet prefix, ARM & Hexagon number prefix).
53 
56 
60 typedef struct {
61  size_t start; //< byte-offset into `str` where this token starts. Must be exactly at a utf-8 codepoint boundary.
62  size_t len; //< `str` length of token in bytes.
64  union {
65  ut64 number; //< Number of RZ_ASM_TOKEN_NUMBER
66  } val;
67 } RzAsmToken;
68 
72 typedef struct {
74  RzStrBuf *str; //< Contains the raw asm string
75  RzVector /* <RzAsmToken> */ *tokens; //< Contains only the tokenization meta-info without strings, ordered by start for log2(n) access
77 
78 typedef struct {
79  const RzRegSet *reg_sets;
82 
86 typedef struct {
87  RzAsmTokenType type; //< Asm token type.
88  char *pattern; //< The regex pattern describing the tokens.
89  RzRegex *regex; //< Compiled regex pattern.
91 
96 typedef struct {
97  bool reset_bg; // Reset the background color?
98  ut64 hl_addr; // Address which should be highlighted. Usually the function address.
100 
101 typedef struct rz_print_zoom_t {
105  int size;
106  int mode;
108 
109 typedef struct rz_print_t {
110  void *user;
112  bool pava;
114  const char *cfmt;
115  char datefmt[32];
116  int datezone;
117  int (*write)(const unsigned char *buf, int len);
120  char *(*cb_color)(int idx, int last, bool bg);
122  int (*disasm)(void *p, ut64 addr);
125  int width;
126  int limit;
127  int bits;
128  bool histblock;
129  // true if the cursor is enabled, false otherwise
131  // offset of the selected byte from the first displayed one
132  int cur;
133  // offset of the selected byte from the first displayed one, when a
134  // range of bytes is selected. -1 is used if no bytes are selected.
135  int ocur;
136  int cols;
137  int flags;
138  int seggrn;
140  int addrmod;
141  int col;
142  int stride;
144  int pairs;
145  bool resetbg;
158  RzRegItem *(*get_register)(RzReg *reg, const char *name, int type);
160  bool (*exists_var)(struct rz_print_t *print, ut64 func_addr, char *str);
163  const char *strconv_mode;
167 
168  // when true it uses row_offsets
170  // offset of the first byte of each printed row.
171  // Last elements is marked with a UT32_MAX.
173  // size of row_offsets
175  // when true it makes visual mode flush the buffer to screen
176  bool vflush;
177  // represents the first not-visible offset on the screen
178  // (only when in visual disasm mode)
180  // Memoized current row number to calculate screen_bounds
181  int rows;
184 
185 #ifdef RZ_API
186 
187 /* RzConsBreak handlers */
188 typedef bool (*RzPrintIsInterruptedCallback)();
189 
191 RZ_API void rz_print_set_is_interrupted_cb(RzPrintIsInterruptedCallback cb);
192 
193 /* ... */
194 RZ_API char *rz_print_hexpair(RzPrint *p, const char *str, int idx);
197 RZ_API void rz_print_set_flags(RzPrint *p, int _flags);
200 RZ_API void rz_print_hexii(RzPrint *p, ut64 addr, const ut8 *buf, int len, int step);
201 RZ_API RZ_OWN char *rz_print_hexdump_str(RZ_NONNULL RzPrint *p, ut64 addr, RZ_NONNULL const ut8 *buf, int len, int base, int step, size_t zoomsz);
202 RZ_API RZ_OWN char *rz_print_jsondump_str(RZ_NONNULL RzPrint *p, RZ_NONNULL const ut8 *buf, int len, int wordsize);
203 RZ_API RZ_OWN char *rz_print_hexdiff_str(RZ_NONNULL RzPrint *p, ut64 aa, RZ_NONNULL const ut8 *_a, ut64 ba, RZ_NONNULL const ut8 *_b, int len, int scndcol);
204 RZ_API void rz_print_bytes(RzPrint *p, const ut8 *buf, int len, const char *fmt);
205 RZ_API void rz_print_fill(RzPrint *p, const ut8 *arr, int size, ut64 addr, int step);
206 RZ_API void rz_print_byte(RzPrint *p, const char *fmt, int idx, ut8 ch);
207 RZ_API const char *rz_print_byte_color(RzPrint *p, int ch);
208 RZ_API void rz_print_raw(RzPrint *p, ut64 addr, const ut8 *buf, int len);
209 RZ_API bool rz_print_have_cursor(RzPrint *p, int cur, int len);
210 RZ_API bool rz_print_cursor_pointer(RzPrint *p, int cur, int len);
211 RZ_API void rz_print_cursor(RzPrint *p, int cur, int len, int set);
213 RZ_API void rz_print_set_cursor(RzPrint *p, int curset, int ocursor, int cursor);
214 #define SEEFLAG -2
215 #define JSONOUTPUT -3
216 
217 /* mode values for rz_print_format_* API */
218 #define RZ_PRINT_MUSTSEE (1) // enable printing of data in specified fmt
219 #define RZ_PRINT_ISFIELD (1 << 1)
220 #define RZ_PRINT_SEEFLAGS (1 << 2)
221 #define RZ_PRINT_JSON (1 << 3)
222 #define RZ_PRINT_MUSTSET (1 << 4)
223 #define RZ_PRINT_UNIONMODE (1 << 5)
224 #define RZ_PRINT_VALUE (1 << 6)
225 #define RZ_PRINT_DOT (1 << 7)
226 #define RZ_PRINT_QUIET (1 << 8)
227 #define RZ_PRINT_STRUCT (1 << 9)
228 
229 RZ_API void rz_print_offset(RzPrint *p, ut64 off, int invert, int opt, int dec, int delta, const char *label);
230 RZ_API void rz_print_offset_sg(RzPrint *p, ut64 off, int invert, int offseg, int seggrn, int offdec, int delta, const char *label);
231 RZ_API void rz_print_progressbar(RzPrint *pr, int pc, int _cols);
232 RZ_API void rz_print_rangebar(RzPrint *p, ut64 startA, ut64 endA, ut64 min, ut64 max, int cols);
233 RZ_API const char *rz_print_color_op_type(RZ_NONNULL RzPrint *p, ut32 /* RzAnalaysisOpType */ analysis_type);
236 RZ_API void rz_print_set_rowoff(RzPrint *p, int i, ut32 offset, bool overwrite);
238 
239 // WIP
241 RZ_API char *rz_print_json_indent(const char *s, bool color, const char *tab, const char **colors);
242 RZ_API char *rz_print_json_human(const char *s);
243 RZ_API char *rz_print_json_path(const char *s, int pos);
244 
246 #endif
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif
size_t len
Definition: 6502dis.c:15
static unsigned invert(unsigned x)
Definition: aesdata.c:73
lzma_index ** i
Definition: index.h:629
ut16 val
Definition: armass64_const.h:6
RZ_API void rz_print_offset(RzPrint *p, ut64 off, int invert, int offseg, int offdec, int delta, const char *label)
Definition: cmd_print.c:5782
RZ_API void rz_print_offset_sg(RzPrint *p, ut64 off, int invert, int offseg, int seggrn, int offdec, int delta, const char *label)
Definition: cmd_print.c:5685
#define RZ_API
uint32_t ut32
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888
int max
Definition: enough.c:225
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
RZ_API char * rz_print_json_indent(const char *s, bool color, const char *tab, const char **palette)
Definition: json_indent.c:254
RZ_API char * rz_print_json_path(const char *s, int pos)
Definition: json_indent.c:43
RZ_API char * rz_print_json_human(const char *s)
Definition: json_indent.c:155
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
int type
Definition: mipsasm.c:17
int idx
Definition: setup.py:197
const char * name
Definition: op.c:541
int off
Definition: pal.c:13
struct @219 colors[]
RZ_API RZ_OWN RzStrBuf * rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const RzAsmTokenString *toks)
Colorizes a tokenized asm string.
Definition: print.c:1613
RZ_API void rz_print_set_flags(RzPrint *p, int _flags)
Definition: print.c:117
RZ_API void rz_print_fill(RzPrint *p, const ut8 *arr, int size, ut64 addr, int step)
Definition: print.c:1346
RZ_API void rz_print_init_rowoffsets(RzPrint *p)
Definition: print.c:1519
RZ_API bool rz_print_have_cursor(RzPrint *p, int cur, int len)
Definition: print.c:133
RZ_API void rz_print_cursor(RzPrint *p, int cur, int len, int set)
Definition: print.c:166
RZ_API void rz_print_byte(RzPrint *p, const char *fmt, int idx, ut8 ch)
Definition: print.c:552
RZ_API void rz_print_bytes(RzPrint *p, const ut8 *buf, int len, const char *fmt)
Definition: print.c:1233
RZ_API void rz_print_hexii(RzPrint *rp, ut64 addr, const ut8 *buf, int len, int step)
Definition: print.c:318
RZ_API const char * rz_print_byte_color(RzPrint *p, int ch)
Definition: print.c:269
RZ_API RZ_OWN char * rz_print_hexdiff_str(RZ_NONNULL RzPrint *p, ut64 aa, RZ_NONNULL const ut8 *_a, ut64 ba, RZ_NONNULL const ut8 *_b, int len, int scndcol)
Print hexdump diff between _a and _b.
Definition: print.c:1172
RZ_API int rz_print_row_at_off(RzPrint *p, ut32 offset)
Definition: print.c:1562
RZ_API ut32 rz_print_rowoff(RzPrint *p, int i)
Definition: print.c:1553
RZ_API void rz_print_rangebar(RzPrint *p, ut64 startA, ut64 endA, ut64 min, ut64 max, int cols)
Definition: print.c:1283
RZ_API void rz_print_progressbar(RzPrint *p, int pc, int _cols)
Definition: print.c:1259
RZ_API void rz_print_set_cursor(RzPrint *p, int enable, int ocursor, int cursor)
Definition: print.c:121
RZ_API bool rz_print_is_interrupted(void)
Definition: print.c:45
RZ_API void rz_print_set_rowoff(RzPrint *p, int i, ut32 offset, bool overwrite)
Definition: print.c:1527
RZ_API RzPrint * rz_print_new(void)
Definition: print.c:56
RZ_API bool rz_print_cursor_pointer(RzPrint *p, int cur, int len)
Definition: print.c:152
RZ_API void rz_print_set_screenbounds(RzPrint *p, ut64 addr)
Sets screen_bounds member of p to addr if the cursor is not visible on the screen.
Definition: print.c:376
RZ_API RzPrint * rz_print_free(RzPrint *p)
Definition: print.c:101
RZ_API RZ_OWN char * rz_print_hexdump_str(RZ_NONNULL RzPrint *p, ut64 addr, RZ_NONNULL const ut8 *buf, int len, int base, int step, size_t zoomsz)
Prints a hexdump of buf at addr.
Definition: print.c:573
RZ_API char * rz_print_hexpair(RzPrint *p, const char *str, int n)
Definition: print.c:172
RZ_API RZ_OWN char * rz_print_jsondump_str(RZ_NONNULL RzPrint *p, RZ_NONNULL const ut8 *buf, int len, int wordsize)
Print dump in json format.
Definition: print.c:1583
RZ_API char * rz_print_section_str(RzPrint *p, ut64 at)
Definition: print.c:511
RZ_API int rz_print_get_cursor(RzPrint *p)
Definition: print.c:1571
RZ_API void rz_print_raw(RzPrint *p, ut64 addr, const ut8 *buf, int len)
Definition: print.c:1249
RZ_API void rz_print_set_is_interrupted_cb(RzPrintIsInterruptedCallback cb)
Definition: print.c:52
RZ_API const char * rz_print_color_op_type(RZ_NONNULL RzPrint *p, ut32 analysis_type)
Definition: print.c:1435
RZ_API void rz_print_addr(RzPrint *p, ut64 addr)
Definition: print.c:489
#define min(a, b)
Definition: qsort.h:83
static RzSocket * s
Definition: rtr.c:28
int(* RzPrintSizeCallback)(void *user, ut64 addr)
Definition: rz_print.h:39
char *(* RzPrintHasRefs)(void *user, ut64 addr, int mode)
Definition: rz_print.h:43
const char *(* RzPrintNameCallback)(void *user, ut64 addr)
Definition: rz_print.h:38
struct rz_print_t RzPrint
const char *(* RzPrintColorFor)(void *user, ut64 addr, bool verbose)
Definition: rz_print.h:42
RzAsmTokenType
Definition: rz_print.h:45
@ RZ_ASM_TOKEN_MNEMONIC
Definition: rz_print.h:47
@ RZ_ASM_TOKEN_REGISTER
Definition: rz_print.h:50
@ RZ_ASM_TOKEN_OPERATOR
Definition: rz_print.h:48
@ RZ_ASM_TOKEN_META
Definition: rz_print.h:52
@ RZ_ASM_TOKEN_NUMBER
Definition: rz_print.h:49
@ RZ_ASM_TOKEN_SEPARATOR
Definition: rz_print.h:51
@ RZ_ASM_TOKEN_UNKNOWN
Definition: rz_print.h:46
@ RZ_ASM_TOKEN_LAST
Definition: rz_print.h:54
char *(* RzPrintCommentCallback)(void *user, ut64 addr)
Definition: rz_print.h:40
struct rz_print_zoom_t RzPrintZoom
const char *(* RzPrintSectionGet)(void *user, ut64 addr)
Definition: rz_print.h:41
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
int(* PrintfCallback)(const char *str,...) RZ_PRINTF_CHECK(1
Definition: rz_types.h:233
#define RZ_BORROW
Definition: rz_types.h:63
static int
Definition: sfsocketcall.h:114
ut32 ana_op_type
Analysis op type (see: _RzAnalysisOpType) of the token string to parse.
Definition: rz_print.h:80
const RzRegSet * reg_sets
Array of reg sets used to lookup register names during parsing.
Definition: rz_print.h:79
Pattern for a asm string token.
Definition: rz_print.h:86
RzAsmTokenType type
Definition: rz_print.h:87
char * pattern
Definition: rz_print.h:88
RzRegex * regex
Definition: rz_print.h:89
An tokenized asm string.
Definition: rz_print.h:72
RzVector * tokens
Definition: rz_print.h:75
ut32 op_type
RzAnalysisOpType. Mnemonic color depends on this.
Definition: rz_print.h:73
RzStrBuf * str
Definition: rz_print.h:74
A token of an asm string holding meta data.
Definition: rz_print.h:60
size_t len
Definition: rz_print.h:62
size_t start
Definition: rz_print.h:61
RzAsmTokenType type
Definition: rz_print.h:63
ut64 number
Definition: rz_print.h:65
Holds certain options to alter the colorizing of asm strings.
Definition: rz_print.h:96
Definition: dis.h:35
RzCons * cons
Definition: rz_print.h:154
RzPrintNameCallback offname
Definition: rz_print.h:147
RzConsBind consbind
Definition: rz_print.h:155
PrintfCallback oprintf
Definition: rz_print.h:123
bool show_offset
Definition: rz_print.h:166
RzPrintSectionGet get_section_name
Definition: rz_print.h:152
int addrmod
Definition: rz_print.h:140
bool resetbg
Definition: rz_print.h:145
int(* write)(const unsigned char *buf, int len)
Definition: rz_print.h:117
int width
Definition: rz_print.h:125
PrintfCallback cb_printf
Definition: rz_print.h:118
int(* disasm)(void *p, ut64 addr)
Definition: rz_print.h:122
int big_endian
Definition: rz_print.h:124
const char * cfmt
Definition: rz_print.h:114
RzPrintAsmColorOpts colorize_opts
Coloize options for asm strings.
Definition: rz_print.h:182
RzPrintSizeCallback offsize
Definition: rz_print.h:148
ut64(* get_register_value)(RzReg *reg, RzRegItem *item)
Definition: rz_print.h:159
ut64 screen_bounds
Definition: rz_print.h:179
bool scr_prompt
Definition: rz_print.h:121
int stride
Definition: rz_print.h:142
const char * strconv_mode
Definition: rz_print.h:163
int rows
Definition: rz_print.h:181
bool(* exists_var)(struct rz_print_t *print, ut64 func_addr, char *str)
Definition: rz_print.h:160
Sdb * sdb_types
Definition: rz_print.h:153
int cols
Definition: rz_print.h:136
void * user
Definition: rz_print.h:110
PrintfCallback cb_eprintf
Definition: rz_print.h:119
RzList * vars
Definition: rz_print.h:164
int datezone
Definition: rz_print.h:116
RzPrintHasRefs hasrefs
Definition: rz_print.h:150
int flags
Definition: rz_print.h:137
RzPrintZoom * zoom
Definition: rz_print.h:146
RzPrintColorFor colorfor
Definition: rz_print.h:149
int bytespace
Definition: rz_print.h:143
RzNum * num
Definition: rz_print.h:156
int pairs
Definition: rz_print.h:144
bool esc_bslash
Definition: rz_print.h:161
char io_unalloc_ch
Definition: rz_print.h:165
int limit
Definition: rz_print.h:126
bool calc_row_offsets
Definition: rz_print.h:169
int row_offsets_sz
Definition: rz_print.h:174
bool histblock
Definition: rz_print.h:128
bool use_comments
Definition: rz_print.h:139
RzPrintCommentCallback get_comments
Definition: rz_print.h:151
bool wide_offsets
Definition: rz_print.h:162
int ocur
Definition: rz_print.h:135
ut32 * row_offsets
Definition: rz_print.h:172
bool vflush
Definition: rz_print.h:176
bool pava
Definition: rz_print.h:112
int bits
Definition: rz_print.h:127
int seggrn
Definition: rz_print.h:138
RzCoreBind coreb
Definition: rz_print.h:113
RzReg * reg
Definition: rz_print.h:157
char datefmt[32]
Definition: rz_print.h:115
bool cur_enabled
Definition: rz_print.h:130
RzIOBind iob
Definition: rz_print.h:111
Definition: sdb.h:63
Definition: enough.c:118
int pos
Definition: main.c:11
#define bool
Definition: sysdefs.h:146
static int color
Definition: visual.c:20
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const char * cb[]
Definition: z80_tab.h:176
static int verbose
Definition: z80asm.c:73
static int addr
Definition: z80asm.c:58