1 #ifndef R2_PRINT_H
2 #define R2_PRINT_H
3 
4 #include "r_types.h"
5 #include "r_cons.h"
6 #include "r_bind.h"
7 #include "r_io.h"
8 #include "r_reg.h"
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #define R_PRINT_FLAGS_COLOR    0x00000001
15 #define R_PRINT_FLAGS_ADDRMOD  0x00000002
16 #define R_PRINT_FLAGS_CURSOR   0x00000004
17 #define R_PRINT_FLAGS_HEADER   0x00000008
18 #define R_PRINT_FLAGS_SPARSE   0x00000010
19 #define R_PRINT_FLAGS_SEGOFF   0x00000020
20 #define R_PRINT_FLAGS_OFFSET   0x00000040
21 #define R_PRINT_FLAGS_REFS     0x00000080
22 #define R_PRINT_FLAGS_DIFFOUT  0x00000100 /* only show different rows in `cc` hexdiffing */
23 #define R_PRINT_FLAGS_ADDRDEC  0x00000200
24 #define R_PRINT_FLAGS_COMMENT  0x00000400
25 #define R_PRINT_FLAGS_COMPACT  0x00000800
26 #define R_PRINT_FLAGS_NONHEX   0x00001000
27 #define R_PRINT_FLAGS_SECSUB   0x00002000
28 #define R_PRINT_FLAGS_RAINBOW  0x00004000
29 #define R_PRINT_FLAGS_HDROFF   0x00008000
30 #define R_PRINT_FLAGS_STYLE    0x00010000
31 #define R_PRINT_FLAGS_NONASCII 0x00020000
32 #define R_PRINT_FLAGS_ALIGN    0x00040000
33 #define R_PRINT_FLAGS_UNALLOC  0x00080000
34 #define R_PRINT_FLAGS_BGFILL   0x00100000
35 #define R_PRINT_FLAGS_SECTION  0x00200000
36 
37 typedef int (*RPrintZoomCallback)(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size);
38 typedef const char *(*RPrintNameCallback)(void *user, ut64 addr);
39 typedef int (*RPrintSizeCallback)(void *user, ut64 addr);
40 typedef char *(*RPrintCommentCallback)(void *user, ut64 addr);
41 typedef const char *(*RPrintSectionGet)(void *user, ut64 addr);
42 typedef const char *(*RPrintColorFor)(void *user, ut64 addr, bool verbose);
43 typedef char *(*RPrintHasRefs)(void *user, ut64 addr, int mode);
44 
45 typedef struct r_print_zoom_t {
46 	ut8 *buf;
47 	ut64 from;
48 	ut64 to;
49 	int size;
50 	int mode;
51 } RPrintZoom;
52 
53 typedef struct r_print_t {
54 	void *user;
55 	RIOBind iob;
56 	bool pava;
57 	RCoreBind coreb;
58 	const char *cfmt;
59 	char datefmt[32];
60 	int datezone;
61 	int (*write)(const unsigned char *buf, int len);
62 	PrintfCallback cb_printf;
63 	PrintfCallback cb_eprintf;
64 	char *(*cb_color)(int idx, int last, bool bg);
65 	bool scr_prompt;
66 	int (*disasm)(void *p, ut64 addr);
67 	PrintfCallback oprintf;
68 	int big_endian;
69 	int width;
70 	int limit;
71 	int bits;
72 	bool histblock;
73 	// true if the cursor is enabled, false otherwise
74 	bool cur_enabled;
75 	// offset of the selected byte from the first displayed one
76 	int cur;
77 	// offset of the selected byte from the first displayed one, when a
78 	// range of bytes is selected. -1 is used if no bytes are selected.
79 	int ocur;
80 	int cols;
81 	int flags;
82 	int seggrn;
83 	bool use_comments;
84 	int addrmod;
85 	int col;
86 	int stride;
87 	int bytespace;
88 	int pairs;
89 	bool resetbg;
90 	RPrintZoom *zoom;
91 	RPrintNameCallback offname;
92 	RPrintSizeCallback offsize;
93 	RPrintColorFor colorfor;
94 	RPrintHasRefs hasrefs;
95 	RPrintCommentCallback get_comments;
96 	RPrintSectionGet get_section_name;
97 	Sdb *formats;
98 	Sdb *sdb_types;
99 	RCons *cons;
100 	RConsBind consbind;
101 	RNum *num;
102 	RReg *reg;
103 	RRegItem* (*get_register)(RReg *reg, const char *name, int type);
104 	ut64 (*get_register_value)(RReg *reg, RRegItem *item);
105 	bool (*exists_var)(struct r_print_t *print, ut64 func_addr, char *str);
106 	ut64* lines_cache;
107 	int lines_cache_sz;
108 	int lines_abs;
109 	bool esc_bslash;
110 	bool wide_offsets;
111 	const char *strconv_mode;
112 	RList *vars;
113 	char io_unalloc_ch;
114 	bool show_offset;
115 
116 	// when true it uses row_offsets
117 	bool calc_row_offsets;
118 	// offset of the first byte of each printed row.
119 	// Last elements is marked with a UT32_MAX.
120 	ut32 *row_offsets;
121 	// size of row_offsets
122 	int row_offsets_sz;
123 	// when true it makes visual mode flush the buffer to screen
124 	bool vflush;
125 	// represents the first not-visible offset on the screen
126 	// (only when in visual disasm mode)
127 	ut64 screen_bounds;
128 	// HACK: Used to temporarily disable the progress bar when it doesn't make sense to have it,
129 	// eg. when setting the default flag tags on startup. Does not override scr.progressbar.
130 	bool enable_progressbar;
131 	RCharset *charset;
132 } RPrint;
133 
134 #ifdef R_API
135 
136 /* RConsBreak handlers */
137 typedef bool (*RPrintIsInterruptedCallback)();
138 
139 R_API bool r_print_is_interrupted(void);
140 R_API void r_print_set_is_interrupted_cb(RPrintIsInterruptedCallback cb);
141 
142 /* ... */
143 R_API char *r_print_hexpair(RPrint *p, const char *str, int idx);
144 R_API void r_print_hex_from_bin(RPrint *p, char *bin_str);
145 R_API RPrint *r_print_new(void);
146 R_API RPrint *r_print_free(RPrint *p);
147 R_API bool r_print_mute(RPrint *p, int x);
148 R_API void r_print_set_flags(RPrint *p, int _flags);
149 R_API void r_print_unset_flags(RPrint *p, int flags);
150 R_API void r_print_addr(RPrint *p, ut64 addr);
151 R_API void r_print_section(RPrint *p, ut64 at);
152 R_API void r_print_columns(RPrint *p, const ut8 *buf, int len, int height);
153 R_API void r_print_hexii(RPrint *p, ut64 addr, const ut8 *buf, int len, int step);
154 R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int base, int step, size_t zoomsz);
155 R_API void r_print_hexdump_simple(const ut8 *buf, int len);
156 R_API int r_print_jsondump(RPrint *p, const ut8 *buf, int len, int wordsize);
157 R_API void r_print_hexpairs(RPrint *p, ut64 addr, const ut8 *buf, int len);
158 R_API void r_print_hexdiff(RPrint *p, ut64 aa, const ut8* a, ut64 ba, const ut8 *b, int len, int scndcol);
159 R_API void r_print_bytes(RPrint *p, const ut8* buf, int len, const char *fmt);
160 R_API void r_print_fill(RPrint *p, const ut8 *arr, int size, ut64 addr, int step);
161 R_API void r_print_byte(RPrint *p, const char *fmt, int idx, ut8 ch);
162 R_API const char *r_print_byte_color(RPrint *p, int ch);
163 R_API void r_print_c(RPrint *p, const ut8 *str, int len);
164 R_API void r_print_raw(RPrint *p, ut64 addr, const ut8* buf, int len, int offlines);
165 R_API bool r_print_have_cursor(RPrint *p, int cur, int len);
166 R_API bool r_print_cursor_pointer(RPrint *p, int cur, int len);
167 R_API void r_print_cursor(RPrint *p, int cur, int len, int set);
168 R_API void r_print_cursor_range(RPrint *p, int cur, int to, int set);
169 R_API int r_print_get_cursor(RPrint *p);
170 R_API void r_print_set_cursor(RPrint *p, int curset, int ocursor, int cursor);
171 R_API void r_print_code(RPrint *p, ut64 addr, const ut8 *buf, int len, char lang);
172 #define SEEFLAG -2
173 #define JSONOUTPUT -3
174 
175 /* mode values for r_print_format_* API */
176 #define R_PRINT_MUSTSEE   (1)      // enable printing of data in specified fmt
177 #define R_PRINT_ISFIELD   (1 << 1)
178 #define R_PRINT_SEEFLAGS  (1 << 2)
179 #define R_PRINT_JSON      (1 << 3)
180 #define R_PRINT_MUSTSET   (1 << 4)
181 #define R_PRINT_UNIONMODE (1 << 5)
182 #define R_PRINT_VALUE     (1 << 6)
183 #define R_PRINT_DOT       (1 << 7)
184 #define R_PRINT_QUIET     (1 << 8)
185 #define R_PRINT_STRUCT    (1 << 9)
186 R_API int r_print_format_struct_size(RPrint *p, const char *format, int mode, int n);
187 R_API int r_print_format(RPrint *p, ut64 seek, const ut8* buf, const int len, const char *fmt, int elem, const char *setval, char *field);
188 R_API const char *r_print_format_byname(RPrint *p, const char *name);
189 R_API void r_print_offset(RPrint *p, ut64 off, int invert, int opt, int dec, int delta, const char *label);
190 R_API void r_print_offset_sg(RPrint *p, ut64 off, int invert, int offseg, int seggrn, int offdec, int delta, const char *label);
191 #define R_PRINT_STRING_WIDE 1
192 #define R_PRINT_STRING_ZEROEND 2
193 #define R_PRINT_STRING_URLENCODE 4
194 #define R_PRINT_STRING_WRAP 8
195 #define R_PRINT_STRING_WIDE32 16
196 #define R_PRINT_STRING_ESC_NL 32
197 R_API int r_print_string(RPrint *p, ut64 seek, const ut8 *str, int len, int options);
198 R_API int r_print_date_dos(RPrint *p, const ut8 *buf, int len);
199 R_API int r_print_date_hfs(RPrint *p, const ut8 *buf, int len);
200 R_API int r_print_date_w32(RPrint *p, const ut8 *buf, int len);
201 R_API int r_print_date_unix(RPrint *p, const ut8 *buf, int len);
202 R_API int r_print_date_get_now(RPrint *p, char *str);
203 R_API void r_print_zoom(RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, int len, int maxlen);
204 R_API void r_print_zoom_buf(RPrint *p, void *user, RPrintZoomCallback cb, ut64 from, ut64 to, int len, int maxlen);
205 R_API void r_print_progressbar(RPrint *pr, int pc, int _cols);
206 R_API void r_print_progressbar_with_count(RPrint *pr, unsigned int pc, unsigned int total, int _cols, bool reset_line);
207 R_API void r_print_portionbar(RPrint *p, const ut64 *portions, int n_portions);
208 R_API void r_print_rangebar(RPrint *p, ut64 startA, ut64 endA, ut64 min, ut64 max, int cols);
209 R_API char * r_print_randomart(const ut8 *dgst_raw, ut32 dgst_raw_len, ut64 addr);
210 R_API void r_print_2bpp_row(RPrint *p, ut8 *buf, const char **colors);
211 R_API void r_print_2bpp_tiles(RPrint *p, ut8 *buf, size_t buflen, ut32 tiles, const char **colors);
212 R_API char * r_print_colorize_opcode(RPrint *print, char *p, const char *reg, const char *num, bool partial_reset, ut64 func_addr);
213 R_API const char * r_print_color_op_type(RPrint *p, ut32 anal_type);
214 R_API void r_print_set_interrupted(int i);
215 R_API void r_print_init_rowoffsets(RPrint *p);
216 R_API ut32 r_print_rowoff(RPrint *p, int i);
217 R_API void r_print_set_rowoff(RPrint *p, int i, ut32 offset, bool overwrite);
218 R_API int r_print_row_at_off(RPrint *p, ut32 offset);
219 R_API int r_print_pie(RPrint *p, ut64 *values, int nvalues, int size);
220 
221 R_API const char* r_print_rowlog(RPrint *print, const char *str);
222 R_API void r_print_rowlog_done(RPrint *print, const char *str);
223 
224 // WIP
225 R_API int r_print_unpack7bit(const char *src, char *dest);
226 R_API int r_print_pack7bit(const char *src, char *dest);
227 R_API char *r_print_stereogram_bytes(const ut8 *buf, int len);
228 R_API char *r_print_stereogram(const char *bump, int w, int h);
229 R_API void r_print_stereogram_print(RPrint *p, const char *buf);
230 R_API void r_print_set_screenbounds(RPrint *p, ut64 addr);
231 R_API int r_util_lines_getline(ut64 *lines_cache, int lines_cache_sz, ut64 off);
232 R_API char* r_print_json_indent(const char* s, bool color, const char *tab, const char **colors);
233 R_API char* r_print_json_human(const char* s);
234 R_API char* r_print_json_path(const char* s, int pos);
235 
236 #endif
237 
238 #ifdef __cplusplus
239 }
240 #endif
241 
242 #endif
243