8
8
9
9
#include "pdb.h"
10
10
11
- /**
12
- * \brief Prints out types in a default format "idpi" command
13
- *
14
- * \param pdb pdb structure for printing function
15
- * \param types List of types
16
- */
17
- static void print_types_regular (RzTypeDB * db , const RzPdb * pdb , const RzList * types ) {
18
- rz_return_if_fail (pdb );
11
+ static char * pdb_type_as_string_regular (const RzTypeDB * db , const RzPdb * pdb , const RzList * types ) {
12
+ rz_return_val_if_fail (pdb && db , NULL );
19
13
if (!types ) {
20
14
eprintf ("there is nothing to print!\n" );
21
15
}
22
16
RzListIter * it ;
23
17
RzBaseType * type ;
18
+ RzStrBuf * buf = rz_strbuf_new (NULL );
24
19
rz_list_foreach (types , it , type ) {
25
- RzStrBuf * buf = rz_strbuf_new (NULL );
26
20
switch (type -> kind ) {
27
21
case RZ_BASE_TYPE_KIND_STRUCT : {
28
22
rz_strbuf_appendf (buf , "struct %s { \n" , type -> name );
@@ -62,22 +56,22 @@ static void print_types_regular(RzTypeDB *db, const RzPdb *pdb, const RzList *ty
62
56
default :
63
57
break ;
64
58
}
65
- rz_cons_printf ("%s\n" , rz_strbuf_get (buf ));
66
- rz_strbuf_free (buf );
67
59
}
60
+ char * str = strdup (rz_strbuf_get (buf ));
61
+ rz_strbuf_free (buf );
62
+ return str ;
68
63
}
69
64
70
- /**
71
- * \brief Prints out types in a json format - "idpij" command
72
- *
73
- * \param pdb pdb structure for printing function
74
- * \param types List of types
75
- */
76
- static void print_types_json (RzTypeDB * db , const RzPdb * pdb , PJ * pj , const RzList * types ) {
77
- rz_return_if_fail (pdb && types && pj );
65
+ static char * pdb_type_as_string_json (const RzTypeDB * db , const RzPdb * pdb , const RzList * types ) {
66
+ rz_return_val_if_fail (db && pdb && types , NULL );
78
67
79
68
RzListIter * it ;
80
69
RzBaseType * type ;
70
+ PJ * pj = pj_new ();
71
+ if (!pj ) {
72
+ return NULL ;
73
+ }
74
+ pj_o (pj );
81
75
rz_list_foreach (types , it , type ) {
82
76
pj_o (pj );
83
77
switch (type -> kind ) {
@@ -130,65 +124,90 @@ static void print_types_json(RzTypeDB *db, const RzPdb *pdb, PJ *pj, const RzLis
130
124
}
131
125
pj_end (pj );
132
126
}
127
+ pj_end (pj );
128
+ char * str = strdup (pj_string (pj ));
129
+ pj_free (pj );
130
+ return str ;
133
131
}
134
132
135
133
/**
136
- * \brief Prints out all the type information in regular,json or pf format
137
- *
138
- * \param pdb PDB information
134
+ * \brief return the output text for types in PDB
135
+ * \param db RzTypeDB
136
+ * \param pdb PDB instance
139
137
* \param mode printing mode
138
+ * \return string of pdb types
140
139
*/
141
- RZ_API void rz_bin_pdb_print_types (RzTypeDB * db , const RzPdb * pdb , PJ * pj , const int mode ) {
140
+ RZ_API RZ_OWN char * rz_bin_pdb_types_as_string (RZ_NONNULL const RzTypeDB * db , RZ_NONNULL const RzPdb * pdb , const RzOutputMode mode ) {
141
+ rz_return_val_if_fail (db && pdb , NULL );
142
142
TpiStream * stream = pdb -> s_tpi ;
143
-
144
143
if (!stream ) {
145
144
eprintf ("There is no tpi stream in current pdb\n" );
146
- return ;
145
+ return NULL ;
147
146
}
148
147
switch (mode ) {
149
- case 'd' :
150
- print_types_regular (db , pdb , stream -> print_type );
151
- return ;
152
- case 'j' :
153
- print_types_json ( db , pdb , pj , stream -> print_type );
154
- return ;
148
+ case RZ_OUTPUT_MODE_STANDARD :
149
+ return pdb_type_as_string_regular (db , pdb , stream -> print_type );
150
+ case RZ_OUTPUT_MODE_JSON :
151
+ return pdb_type_as_string_json ( db , pdb , stream -> print_type );
152
+ default :
153
+ return NULL ;
155
154
}
156
155
}
157
156
158
- RZ_API void rz_bin_pdb_print_gvars (RzPdb * pdb , ut64 img_base , PJ * pj , int format ) {
157
+ /**
158
+ * \brief return the output text for global symbols in PDB
159
+ *
160
+ * \param pdb PDB instance
161
+ * \param img_base image base addr
162
+ * \param mode print mode
163
+ * \return string of pdb symbols
164
+ */
165
+ RZ_API RZ_OWN char * rz_bin_pdb_gvars_as_string (RZ_NONNULL const RzPdb * pdb , const ut64 img_base , RzOutputMode mode ) {
166
+ rz_return_val_if_fail (pdb , NULL );
159
167
PeImageSectionHeader * sctn_header = 0 ;
160
168
GDataStream * gsym_data_stream = 0 ;
161
169
PeStream * pe_stream = 0 ;
162
170
OmapStream * omap_stream ;
163
171
GDataGlobal * gdata = 0 ;
164
172
RzListIter * it = 0 ;
173
+ PJ * pj = NULL ;
165
174
char * name ;
166
-
167
- if (format == 'j' ) {
175
+ RzStrBuf * buf = rz_strbuf_new (NULL );
176
+ if (!buf ) {
177
+ return NULL ;
178
+ }
179
+ if (mode == RZ_OUTPUT_MODE_JSON ) {
180
+ pj = pj_new ();
181
+ if (!pj ) {
182
+ rz_strbuf_free (buf );
183
+ return NULL ;
184
+ }
185
+ pj_o (pj );
168
186
pj_ka (pj , "gvars" );
169
187
}
170
188
gsym_data_stream = pdb -> s_gdata ;
171
189
pe_stream = pdb -> s_pe ;
172
190
omap_stream = pdb -> s_omap ;
173
191
if (!pe_stream ) {
174
- return ;
192
+ rz_strbuf_free (buf );
193
+ return NULL ;
175
194
}
176
195
rz_list_foreach (gsym_data_stream -> global_list , it , gdata ) {
177
196
sctn_header = rz_list_get_n (pe_stream -> sections_hdrs , (gdata -> segment - 1 ));
178
197
if (sctn_header ) {
179
198
name = rz_bin_demangle_msvc (gdata -> name );
180
199
name = (name ) ? name : strdup (gdata -> name );
181
- switch (format ) {
182
- case 'j' : // JSON
200
+ switch (mode ) {
201
+ case RZ_OUTPUT_MODE_JSON : // JSON
183
202
pj_o (pj );
184
203
pj_kN (pj , "address" , (img_base + omap_remap (omap_stream , gdata -> offset + sctn_header -> virtual_address )));
185
204
pj_kN (pj , "symtype" , gdata -> symtype );
186
205
pj_ks (pj , "section_name" , sctn_header -> name );
187
206
pj_ks (pj , "gdata_name" , name );
188
207
pj_end (pj );
189
208
break ;
190
- case 'd' :
191
- rz_cons_printf ( "0x%08" PFMT64x " %d %.*s %s\n" ,
209
+ case RZ_OUTPUT_MODE_STANDARD :
210
+ rz_strbuf_appendf ( buf , "0x%08" PFMT64x " %d %.*s %s\n" ,
192
211
(ut64 )(img_base + omap_remap (omap_stream , gdata -> offset + sctn_header -> virtual_address )),
193
212
gdata -> symtype , PDB_SIZEOF_SECTION_NAME , sctn_header -> name , name );
194
213
break ;
@@ -198,9 +217,15 @@ RZ_API void rz_bin_pdb_print_gvars(RzPdb *pdb, ut64 img_base, PJ *pj, int format
198
217
free (name );
199
218
}
200
219
}
201
- if (format == 'j' ) {
220
+ if (mode == RZ_OUTPUT_MODE_JSON ) {
221
+ pj_end (pj );
202
222
pj_end (pj );
223
+ rz_strbuf_append (buf , pj_string (pj ));
224
+ pj_free (pj );
203
225
}
226
+ char * str = strdup (rz_strbuf_get (buf ));
227
+ rz_strbuf_free (buf );
228
+ return str ;
204
229
}
205
230
206
231
static bool parse_pdb_stream (RzPdb * pdb , MsfStream * stream ) {
0 commit comments