Rizin
unix-like reverse engineering framework and cli tools
graph_drawable.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 Anton Kochkov <anton.kochkov@gmail.com>
2 // SPDX-FileCopyrightText: 2020 karliss <karlis3p70l1ij@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_core.h>
7 
8 RZ_API void rz_graph_free_node_info(void *ptr) {
9  if (!ptr) {
10  return;
11  }
12  RzGraphNodeInfo *info = ptr;
13  free(info->body);
14  free(info->title);
15  free(info);
16 }
17 
18 RZ_API RzGraphNodeInfo *rz_graph_create_node_info(const char *title, const char *body, ut64 offset) {
20  if (data) {
21  data->title = RZ_STR_DUP(title);
22  data->body = RZ_STR_DUP(body);
23  data->offset = offset;
24  }
25  return data;
26 }
27 
28 RZ_API RzGraphNode *rz_graph_add_node_info(RzGraph *graph, const char *title, const char *body, ut64 offset) {
30  RzGraphNodeInfo *data = rz_graph_create_node_info(title, body, offset);
31  if (!data) {
32  return NULL;
33  }
35  if (!node) {
37  }
38  return node;
39 }
40 
41 RZ_API char *rz_graph_drawable_to_dot(RzGraph /*<RzGraphNodeInfo *>*/ *graph, const char *node_properties, const char *edge_properties) {
42  RzList *nodes = graph->nodes;
43  RzListIter *it, *itt;
44  RzGraphNode *node = NULL, *target = NULL;
45  RzStrBuf buf;
48  "digraph code {\nrankdir=LR;\noutputorder=edgesfirst\ngraph [bgcolor=azure];\n"
49  "edge [arrowhead=normal, color=\"#3030c0\" style=bold weight=2 %s];\n"
50  "node [fillcolor=white, style=filled shape=box "
51  "fontsize=\"8\" %s];\n",
52  edge_properties ? edge_properties : "",
53  node_properties ? node_properties : "");
54 
55  rz_list_foreach (nodes, it, node) {
56  RzGraphNodeInfo *print_node = (RzGraphNodeInfo *)node->data;
57  const char *body = print_node->body;
58  if (!body || !*body) {
59  rz_strbuf_appendf(&buf, "%d [URL=\"%s\", color=\"lightgray\", label=\"%s\"]\n",
60  node->idx, print_node->title, print_node->title);
61  } else {
62  rz_strbuf_appendf(&buf, "%d [URL=\"%s\", color=\"lightgray\", label=\"%s\\n%s\"]\n",
63  node->idx, print_node->title, print_node->title, body);
64  }
65  rz_list_foreach (node->out_nodes, itt, target) {
66  rz_strbuf_appendf(&buf, "%d -> %d\n", node->idx, target->idx);
67  }
68  }
69  rz_strbuf_append(&buf, "}\n");
70  return rz_strbuf_drain_nofree(&buf);
71 }
72 
73 RZ_API void rz_graph_drawable_to_json(RzGraph /*<RzGraphNodeInfo *>*/ *graph, PJ *pj, bool use_offset) {
74  RzList *nodes = graph->nodes, *neighbours = NULL;
75  RzListIter *it, *itt;
76  RzGraphNode *node = NULL, *neighbour = NULL;
77  if (!pj) {
78  return;
79  }
80  pj_o(pj);
81  pj_k(pj, "nodes");
82  pj_a(pj);
83 
84  rz_list_foreach (nodes, it, node) {
85  RzGraphNodeInfo *print_node = (RzGraphNodeInfo *)node->data;
86  pj_o(pj);
87  pj_ki(pj, "id", node->idx);
88  if (print_node->title) {
89  pj_ks(pj, "title", print_node->title);
90  }
91  if (print_node->body) {
92  pj_ks(pj, "body", print_node->body);
93  }
94  if (use_offset) {
95  pj_kn(pj, "offset", print_node->offset);
96  }
97  pj_k(pj, "out_nodes");
98  pj_a(pj);
99  neighbours = node->out_nodes;
100  rz_list_foreach (neighbours, itt, neighbour) {
101  pj_i(pj, neighbour->idx);
102  }
103  pj_end(pj);
104  pj_end(pj);
105  }
106  pj_end(pj);
107  pj_end(pj);
108 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void rz_graph_drawable_to_json(RzGraph *graph, PJ *pj, bool use_offset)
Convert graph to JSON.
RZ_API RzGraphNodeInfo * rz_graph_create_node_info(const char *title, const char *body, ut64 offset)
RZ_API char * rz_graph_drawable_to_dot(RzGraph *graph, const char *node_properties, const char *edge_properties)
Convert graph to Graphviz dot format.
RZ_API void rz_graph_free_node_info(void *ptr)
Definition: graph_drawable.c:8
RZ_API RzGraphNode * rz_graph_add_node_info(RzGraph *graph, const char *title, const char *body, ut64 offset)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzGraphNode * rz_graph_add_nodef(RzGraph *g, void *data, RzListFree user_free)
Definition: graph.c:167
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
RZ_API PJ * pj_k(PJ *j, const char *k)
Definition: pj.c:104
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API PJ * pj_i(PJ *j, int d)
Definition: pj.c:284
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_kn(PJ *j, const char *k, ut64 n)
Definition: pj.c:121
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
#define RZ_STR_DUP(x)
Definition: rz_str.h:69
RZ_API RZ_OWN char * rz_strbuf_drain_nofree(RzStrBuf *sb)
Definition: strbuf.c:349
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: rz_pj.h:12
Generic drawable graph node.
ut64 offset
Optional offset for the object corresponding to node.
void * data
Definition: rz_graph.h:12
unsigned int idx
Definition: rz_graph.h:11
RzList * out_nodes
Definition: rz_graph.h:13
RzList * nodes
Definition: rz_graph.h:29
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
ut64(WINAPI *w32_GetEnabledXStateFeatures)()