Rizin
unix-like reverse engineering framework and cli tools
serialize_types.c File Reference
#include <rz_util/rz_serialize.h>
#include <rz_list.h>
#include <rz_vector.h>
#include <rz_type.h>
#include <sdb.h>

Go to the source code of this file.

Classes

struct  TypeFormatPair
 
struct  typedb_sdb
 

Functions

static RzTypeTypeclass get_base_type_typeclass (RZ_NONNULL const RzBaseType *type)
 
static void set_base_type_typeclass (RZ_NONNULL RzBaseType *type, RzTypeTypeclass typeclass)
 
static char * get_type_data (Sdb *sdb, const char *type, const char *sname)
 
static TypeFormatPairget_enum_type (Sdb *sdb, const char *sname)
 
static TypeFormatPairget_struct_type (RzTypeDB *typedb, Sdb *sdb, const char *sname)
 
static TypeFormatPairget_union_type (RzTypeDB *typedb, Sdb *sdb, const char *sname)
 
static TypeFormatPairget_typedef_type (RzTypeDB *typedb, Sdb *sdb, const char *sname)
 
static TypeFormatPairget_atomic_type (RzTypeDB *typedb, Sdb *sdb, const char *sname)
 
bool sdb_load_base_types (RzTypeDB *typedb, Sdb *sdb)
 
static void save_struct (const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
 
static void save_union (const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
 
static void save_enum (const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
 
static void save_atomic_type (const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
 
static void save_typedef (const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
 
void sdb_save_base_type (const RzTypeDB *typedb, RZ_NONNULL Sdb *sdb, const RzBaseType *type)
 
RZ_IPI bool types_load_sdb (RZ_NONNULL Sdb *db, RZ_NONNULL RzTypeDB *typedb)
 
static bool export_base_type_cb (void *user, const void *k, const void *v)
 
static bool types_export_sdb (RZ_NONNULL Sdb *db, RZ_NONNULL const RzTypeDB *typedb)
 
static bool sdb_load_by_path (RZ_NONNULL RzTypeDB *typedb, RZ_NONNULL const char *path)
 
static bool sdb_load_from_string (RZ_NONNULL RzTypeDB *typedb, RZ_NONNULL const char *string)
 
RZ_API bool rz_type_db_load_sdb (RzTypeDB *typedb, RZ_NONNULL const char *path)
 Loads the types from compiled SDB specified by path. More...
 
RZ_API bool rz_type_db_load_sdb_str (RzTypeDB *typedb, RZ_NONNULL const char *str)
 Loads the types from SDB KV string. More...
 
RZ_API void rz_serialize_types_save (RZ_NONNULL Sdb *db, RZ_NONNULL const RzTypeDB *typedb)
 Saves the types into SDB. More...
 
RZ_API bool rz_serialize_types_load (RZ_NONNULL Sdb *db, RZ_NONNULL RzTypeDB *typedb, RZ_NULLABLE RzSerializeResultInfo *res)
 Loads the types from SDB. More...
 

Function Documentation

◆ export_base_type_cb()

static bool export_base_type_cb ( void *  user,
const void *  k,
const void *  v 
)
static

Definition at line 604 of file serialize_types.c.

604  {
605  struct typedb_sdb *s = user;
606  RzBaseType *btype = (RzBaseType *)v;
607  sdb_save_base_type(s->typedb, s->sdb, btype);
608  return true;
609 }
const char * v
Definition: dsignal.c:12
static RzSocket * s
Definition: rtr.c:28
void sdb_save_base_type(const RzTypeDB *typedb, RZ_NONNULL Sdb *sdb, const RzBaseType *type)

References s, sdb_save_base_type(), and v.

Referenced by types_export_sdb().

◆ get_atomic_type()

static TypeFormatPair* get_atomic_type ( RzTypeDB typedb,
Sdb sdb,
const char *  sname 
)
static

Definition at line 278 of file serialize_types.c.

278  {
279  rz_return_val_if_fail(typedb && sdb && RZ_STR_ISNOTEMPTY(sname), NULL);
280 
282  if (!base_type) {
283  return NULL;
284  }
285 
286  RzType *ttype = RZ_NEW0(RzType);
287  if (!ttype) {
288  goto error;
289  }
290  ttype->kind = RZ_TYPE_KIND_IDENTIFIER;
291  ttype->identifier.name = strdup(sname);
292  ttype->identifier.is_const = false; // We don't preload const types by default
294  base_type->type = ttype;
295 
296  base_type->name = strdup(sname);
297  RzStrBuf key;
298  base_type->size = sdb_num_get(sdb, rz_strbuf_initf(&key, "type.%s.size", sname), 0);
300  const char *tclass = sdb_const_get(sdb, rz_strbuf_setf(&key, "type.%s.typeclass", sname), 0);
301  if (tclass) {
302  typeclass = rz_type_typeclass_from_string(tclass);
303  }
304  set_base_type_typeclass(base_type, typeclass);
305  const char *format = sdb_const_get(sdb, rz_strbuf_setf(&key, "type.%s", sname), 0);
307 
309  tpair->type = base_type;
310  tpair->format = format ? strdup(format) : NULL;
311 
312  return tpair;
313 
314 error:
315  rz_type_base_type_free(base_type);
316  return NULL;
317 }
RZ_API void rz_type_base_type_free(RzBaseType *type)
Frees the RzBaseType instance and all of its members.
Definition: base.c:132
RZ_API RZ_OWN RzBaseType * rz_type_base_type_new(RzBaseTypeKind kind)
Allocates a new instance of RzBaseType given the kind.
Definition: base.c:162
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API const char * rz_strbuf_initf(RzStrBuf *sb, const char *fmt,...)
Definition: strbuf.c:38
RZ_API const char * rz_strbuf_setf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
@ RZ_BASE_TYPE_KIND_ATOMIC
Definition: rz_type.h:77
@ RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED
Definition: rz_type.h:135
RzTypeTypeclass
Definition: rz_type.h:61
@ RZ_TYPE_TYPECLASS_NONE
Definition: rz_type.h:62
@ RZ_TYPE_KIND_IDENTIFIER
Definition: rz_type.h:128
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
static void set_base_type_typeclass(RZ_NONNULL RzBaseType *type, RzTypeTypeclass typeclass)
RzBaseType * type
char * name
Definition: rz_type.h:112
RzType * type
Definition: rz_type.h:113
RzTypeKind kind
Definition: rz_type.h:155
struct rz_type_t::@292::@294 identifier
RZ_API RzTypeTypeclass rz_type_typeclass_from_string(RZ_NONNULL const char *typeclass)
Returns the typeclass from the string name of it.
Definition: typeclass.c:66
void error(const char *msg)
Definition: untgz.c:593

References error(), TypeFormatPair::format, rz_type_t::identifier, key, rz_type_t::kind, rz_base_type_t::name, NULL, RZ_BASE_TYPE_KIND_ATOMIC, RZ_NEW0, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_strbuf_fini(), rz_strbuf_initf(), rz_strbuf_setf(), rz_type_base_type_free(), rz_type_base_type_new(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, rz_type_typeclass_from_string(), RZ_TYPE_TYPECLASS_NONE, sdb_const_get(), sdb_num_get(), set_base_type_typeclass(), rz_base_type_t::size, strdup(), rz_base_type_t::type, and TypeFormatPair::type.

Referenced by sdb_load_base_types().

◆ get_base_type_typeclass()

static RzTypeTypeclass get_base_type_typeclass ( RZ_NONNULL const RzBaseType type)
inlinestatic

Definition at line 15 of file serialize_types.c.

15  {
17  return type->attrs & RZ_TYPE_ATTRIBUTE_TYPECLASS_MASK;
18 }
int type
Definition: mipsasm.c:17
@ RZ_TYPE_ATTRIBUTE_TYPECLASS_MASK
Definition: rz_type.h:56
@ RZ_TYPE_TYPECLASS_INVALID
Definition: rz_type.h:69

References rz_return_val_if_fail, RZ_TYPE_ATTRIBUTE_TYPECLASS_MASK, RZ_TYPE_TYPECLASS_INVALID, and type.

Referenced by save_atomic_type().

◆ get_enum_type()

static TypeFormatPair* get_enum_type ( Sdb sdb,
const char *  sname 
)
static

Definition at line 36 of file serialize_types.c.

36  {
38 
40  if (!base_type) {
41  return NULL;
42  }
43 
44  char *members = get_type_data(sdb, "enum", sname);
45  if (!members) {
46  goto error;
47  }
48 
49  base_type->name = strdup(sname);
50  RzVector *cases = &base_type->enum_data.cases;
51  if (!rz_vector_reserve(cases, (size_t)sdb_alen(members))) {
52  goto error;
53  }
54 
55  char *cur;
56  sdb_aforeach(cur, members) {
57  char *val_key = rz_str_newf("enum.%s.%s", sname, cur);
58  if (!val_key) {
59  goto error;
60  }
61  const char *value = sdb_const_get(sdb, val_key, NULL);
62  free(val_key);
63 
64  if (!value) { // if nothing is found, ret NULL
65  goto error;
66  }
67 
68  RzTypeEnumCase cas = { .name = strdup(cur), .val = strtol(value, NULL, 16) };
69 
70  void *element = rz_vector_push(cases, &cas); // returns null if no space available
71  if (!element) {
72  goto error;
73  }
74 
75  sdb_aforeach_next(cur);
76  }
77  free(members);
78 
79  RzStrBuf key;
80  char *format = sdb_get(sdb, rz_strbuf_initf(&key, "type.%s", sname), 0);
82 
84  tpair->type = base_type;
85  tpair->format = format;
86 
87  return tpair;
88 
89 error:
90  free(members);
91  rz_type_base_type_free(base_type);
92  return NULL;
93 }
static int value
Definition: cmd_api.c:93
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
@ RZ_BASE_TYPE_KIND_ENUM
Definition: rz_type.h:75
RZ_API void * rz_vector_reserve(RzVector *vec, size_t capacity)
Definition: vector.c:214
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API int sdb_alen(const char *str)
Definition: util.c:151
#define sdb_aforeach_next(x)
Definition: sdb.h:348
#define sdb_aforeach(x, y)
Definition: sdb.h:342
static char * get_type_data(Sdb *sdb, const char *type, const char *sname)
RzVector cases
Definition: rz_type.h:108
RzBaseTypeEnum enum_data
Definition: rz_type.h:119

References rz_base_type_enum_t::cases, rz_base_type_t::enum_data, error(), TypeFormatPair::format, free(), get_type_data(), key, rz_type_enum_case_t::name, rz_base_type_t::name, NULL, RZ_BASE_TYPE_KIND_ENUM, RZ_NEW0, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_newf(), rz_strbuf_fini(), rz_strbuf_initf(), rz_type_base_type_free(), rz_type_base_type_new(), rz_vector_push(), rz_vector_reserve(), sdb_aforeach, sdb_aforeach_next, sdb_alen(), sdb_const_get(), sdb_get(), strdup(), TypeFormatPair::type, and value.

Referenced by sdb_load_base_types().

◆ get_struct_type()

static TypeFormatPair* get_struct_type ( RzTypeDB typedb,
Sdb sdb,
const char *  sname 
)
static

Definition at line 95 of file serialize_types.c.

95  {
96  rz_return_val_if_fail(typedb && sdb && RZ_STR_ISNOTEMPTY(sname), NULL);
97 
99  if (!base_type) {
100  return NULL;
101  }
102  base_type->name = strdup(sname);
103 
104  char *sdb_members = get_type_data(sdb, "struct", sname);
105  if (sdb_members) {
106  RzVector *members = &base_type->struct_data.members;
107  if (!rz_vector_reserve(members, (size_t)sdb_alen(sdb_members))) {
108  goto error;
109  }
110 
111  char *cur;
112  sdb_aforeach(cur, sdb_members) {
113  char *type_key = rz_str_newf("struct.%s.%s", sname, cur);
114  if (!type_key) {
115  goto error;
116  }
117  char *values = sdb_get(sdb, type_key, NULL);
118  free(type_key);
119 
120  if (!values) {
121  goto error;
122  }
123  char *offset = NULL;
124  char *type = sdb_anext(values, &offset);
125  if (!offset) { // offset is missing, malformed state
126  free(values);
127  goto error;
128  }
129  // Parse type as a C string
130  char *error_msg = NULL;
131  RzType *ttype = rz_type_parse_string_single(typedb->parser, type, &error_msg);
132  if (!ttype || error_msg) {
133  free(values);
134  goto error;
135  }
136 
137  RzTypeStructMember memb = {
138  .name = strdup(cur),
139  .type = ttype,
140  .offset = strtol(offset, NULL, 10)
141  };
142 
143  free(values);
144 
145  void *element = rz_vector_push(members, &memb); // returns null if no space available
146  if (!element) {
147  goto error;
148  }
149 
150  sdb_aforeach_next(cur);
151  }
152  free(sdb_members);
153  }
154 
155  RzStrBuf key;
156  const char *format = sdb_get(sdb, rz_strbuf_initf(&key, "type.%s", sname), 0);
158 
160  tpair->type = base_type;
161  tpair->format = format ? strdup(format) : NULL;
162 
163  return tpair;
164 
165 error:
166  rz_type_base_type_free(base_type);
167  free(sdb_members);
168  return NULL;
169 }
RZ_API RZ_OWN RzType * rz_type_parse_string_single(RzTypeParser *parser, const char *code, char **error_msg)
Parses the single C type definition.
Definition: c_cpp_parser.c:309
voidpf uLong offset
Definition: ioapi.h:144
@ RZ_BASE_TYPE_KIND_STRUCT
Definition: rz_type.h:73
RZ_API char * sdb_anext(char *str, char **next)
Definition: util.c:192
RzBaseTypeStruct struct_data
Definition: rz_type.h:118
RzTypeParser * parser
Definition: rz_type.h:37

References error(), TypeFormatPair::format, free(), get_type_data(), key, rz_base_type_struct_t::members, rz_type_struct_member_t::name, rz_base_type_t::name, NULL, rz_type_db_t::parser, RZ_BASE_TYPE_KIND_STRUCT, RZ_NEW0, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_newf(), rz_strbuf_fini(), rz_strbuf_initf(), rz_type_base_type_free(), rz_type_base_type_new(), rz_type_parse_string_single(), rz_vector_push(), rz_vector_reserve(), sdb_aforeach, sdb_aforeach_next, sdb_alen(), sdb_anext(), sdb_get(), strdup(), rz_base_type_t::struct_data, type, and TypeFormatPair::type.

Referenced by sdb_load_base_types().

◆ get_type_data()

static char* get_type_data ( Sdb sdb,
const char *  type,
const char *  sname 
)
static

Definition at line 25 of file serialize_types.c.

25  {
27  char *key = rz_str_newf("%s.%s", type, sname);
28  if (!key) {
29  return NULL;
30  }
31  char *members = sdb_get(sdb, key, NULL);
32  free(key);
33  return members;
34 }

References free(), key, NULL, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_newf(), sdb_get(), and type.

Referenced by get_enum_type(), get_struct_type(), get_typedef_type(), and get_union_type().

◆ get_typedef_type()

static TypeFormatPair* get_typedef_type ( RzTypeDB typedb,
Sdb sdb,
const char *  sname 
)
static

Definition at line 240 of file serialize_types.c.

240  {
241  rz_return_val_if_fail(typedb && sdb && RZ_STR_ISNOTEMPTY(sname), NULL);
242 
244  if (!base_type) {
245  return NULL;
246  }
247 
248  base_type->name = strdup(sname);
249  char *type = get_type_data(sdb, "typedef", sname);
250  char *error_msg = NULL;
251  RzType *ttype = rz_type_parse_string_single(typedb->parser, type, &error_msg);
252  if (!ttype || error_msg) {
253  goto error;
254  }
255  free(type);
256 
257  base_type->type = ttype;
258  if (!base_type->type) {
259  goto error;
260  }
261 
262  RzStrBuf key;
263  char *format = sdb_get(sdb, rz_strbuf_initf(&key, "type.%s", sname), 0);
265 
267  tpair->type = base_type;
268  tpair->format = format;
269 
270  return tpair;
271 
272 error:
273  free(type);
274  rz_type_base_type_free(base_type);
275  return NULL;
276 }
@ RZ_BASE_TYPE_KIND_TYPEDEF
Definition: rz_type.h:76

References error(), TypeFormatPair::format, free(), get_type_data(), key, rz_base_type_t::name, NULL, rz_type_db_t::parser, RZ_BASE_TYPE_KIND_TYPEDEF, RZ_NEW0, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_strbuf_fini(), rz_strbuf_initf(), rz_type_base_type_free(), rz_type_base_type_new(), rz_type_parse_string_single(), sdb_get(), strdup(), type, rz_base_type_t::type, and TypeFormatPair::type.

Referenced by sdb_load_base_types().

◆ get_union_type()

static TypeFormatPair* get_union_type ( RzTypeDB typedb,
Sdb sdb,
const char *  sname 
)
static

Definition at line 171 of file serialize_types.c.

171  {
172  rz_return_val_if_fail(typedb && sdb && RZ_STR_ISNOTEMPTY(sname), NULL);
173 
175  if (!base_type) {
176  return NULL;
177  }
178 
179  base_type->name = strdup(sname);
180 
181  char *sdb_members = get_type_data(sdb, "union", sname);
182  if (sdb_members) {
183  RzVector *members = &base_type->union_data.members;
184  if (!rz_vector_reserve(members, (size_t)sdb_alen(sdb_members))) {
185  goto error;
186  }
187 
188  char *cur;
189  sdb_aforeach(cur, sdb_members) {
190  char *type_key = rz_str_newf("union.%s.%s", sname, cur);
191  if (!type_key) {
192  goto error;
193  }
194  char *values = sdb_get(sdb, type_key, NULL);
195  free(type_key);
196 
197  if (!values) {
198  goto error;
199  }
200  char *value = sdb_anext(values, NULL);
201  char *error_msg = NULL;
202  RzType *ttype = rz_type_parse_string_single(typedb->parser, value, &error_msg);
203  if (!ttype || error_msg) {
204  free(values);
205  goto error;
206  }
207 
208  RzTypeUnionMember memb = {
209  .name = strdup(cur),
210  .type = ttype
211  };
212  free(values);
213 
214  void *element = rz_vector_push(members, &memb); // returns null if no space available
215  if (!element) {
216  goto error;
217  }
218 
219  sdb_aforeach_next(cur);
220  }
221  free(sdb_members);
222  }
223 
224  RzStrBuf key;
225  const char *format = sdb_get(sdb, rz_strbuf_initf(&key, "type.%s", sname), 0);
227 
229  tpair->type = base_type;
230  tpair->format = format ? strdup(format) : NULL;
231 
232  return tpair;
233 
234 error:
235  rz_type_base_type_free(base_type);
236  free(sdb_members);
237  return NULL;
238 }
@ RZ_BASE_TYPE_KIND_UNION
Definition: rz_type.h:74
RzBaseTypeUnion union_data
Definition: rz_type.h:120
RzVector members
Definition: rz_type.h:104

References error(), TypeFormatPair::format, free(), get_type_data(), key, rz_base_type_union_t::members, rz_type_union_member_t::name, rz_base_type_t::name, NULL, rz_type_db_t::parser, RZ_BASE_TYPE_KIND_UNION, RZ_NEW0, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_newf(), rz_strbuf_fini(), rz_strbuf_initf(), rz_type_base_type_free(), rz_type_base_type_new(), rz_type_parse_string_single(), rz_vector_push(), rz_vector_reserve(), sdb_aforeach, sdb_aforeach_next, sdb_alen(), sdb_anext(), sdb_get(), strdup(), TypeFormatPair::type, rz_base_type_t::union_data, and value.

Referenced by sdb_load_base_types().

◆ rz_serialize_types_load()

RZ_API bool rz_serialize_types_load ( RZ_NONNULL Sdb db,
RZ_NONNULL RzTypeDB typedb,
RZ_NULLABLE RzSerializeResultInfo res 
)

Loads the types from SDB.

Parameters
dbA SDB database object
typedbRzTypeDB instance
resA structure where the result is stored

Definition at line 688 of file serialize_types.c.

688  {
689  rz_return_val_if_fail(db && typedb, false);
690  return types_load_sdb(db, typedb);
691 }
RZ_IPI bool types_load_sdb(RZ_NONNULL Sdb *db, RZ_NONNULL RzTypeDB *typedb)

References rz_return_val_if_fail, typedb_sdb::typedb, and types_load_sdb().

Referenced by rz_serialize_analysis_types_load().

◆ rz_serialize_types_save()

RZ_API void rz_serialize_types_save ( RZ_NONNULL Sdb db,
RZ_NONNULL const RzTypeDB typedb 
)

Saves the types into SDB.

Parameters
dbA SDB database object
typedbRzTypeDB instance

Definition at line 676 of file serialize_types.c.

676  {
677  rz_return_if_fail(db && typedb);
678  types_export_sdb(db, typedb);
679 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
static bool types_export_sdb(RZ_NONNULL Sdb *db, RZ_NONNULL const RzTypeDB *typedb)

References rz_return_if_fail, typedb_sdb::typedb, and types_export_sdb().

Referenced by rz_serialize_analysis_types_save().

◆ rz_type_db_load_sdb()

RZ_API bool rz_type_db_load_sdb ( RzTypeDB typedb,
RZ_NONNULL const char *  path 
)

Loads the types from compiled SDB specified by path.

Parameters
typedbRzTypeDB instance
pathA path to the compiled SDB containing serialized types

Definition at line 648 of file serialize_types.c.

648  {
649  rz_return_val_if_fail(typedb && path, false);
650  if (!rz_file_exists(path)) {
651  return false;
652  }
653  return sdb_load_by_path(typedb, path);
654 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
static bool sdb_load_by_path(RZ_NONNULL RzTypeDB *typedb, RZ_NONNULL const char *path)

References path, rz_file_exists(), rz_return_val_if_fail, sdb_load_by_path(), and typedb_sdb::typedb.

Referenced by rz_type_db_init(), and rz_type_open_sdb_handler().

◆ rz_type_db_load_sdb_str()

RZ_API bool rz_type_db_load_sdb_str ( RzTypeDB typedb,
RZ_NONNULL const char *  str 
)

Loads the types from SDB KV string.

Parameters
typedbRzTypeDB instance
strA string in Key-Value format as for non-compiled SDB

Definition at line 662 of file serialize_types.c.

662  {
663  rz_return_val_if_fail(typedb && str, false);
664  if (RZ_STR_ISEMPTY(str)) {
665  return false;
666  }
667  return sdb_load_from_string(typedb, str);
668 }
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
static bool sdb_load_from_string(RZ_NONNULL RzTypeDB *typedb, RZ_NONNULL const char *string)

References rz_return_val_if_fail, RZ_STR_ISEMPTY, sdb_load_from_string(), cmd_descs_generate::str, and typedb_sdb::typedb.

◆ save_atomic_type()

static void save_atomic_type ( const RzTypeDB typedb,
Sdb sdb,
const RzBaseType type 
)
static

Definition at line 504 of file serialize_types.c.

504  {
505  rz_return_if_fail(typedb && sdb && type && type->name && type->kind == RZ_BASE_TYPE_KIND_ATOMIC);
506  /*
507  C: (cannot define a custom atomic type)
508  Sdb:
509  char=type
510  type.char=c
511  type.char.size=8
512  type.char.typeclass=Signed Integral
513  */
514  char *sname = type->name;
515  sdb_set(sdb, sname, "type", 0);
516 
517  RzStrBuf key;
518  RzStrBuf val;
521 
522  sdb_set(sdb,
523  rz_strbuf_setf(&key, "type.%s.size", sname),
524  rz_strbuf_setf(&val, "%" PFMT64u "", type->size), 0);
525  sdb_set(sdb,
526  rz_strbuf_setf(&key, "type.%s.typeclass", sname),
528 
529  const char *typefmt = rz_type_db_format_get(typedb, sname);
530  sdb_set(sdb,
531  rz_strbuf_setf(&key, "type.%s", sname),
532  typefmt, 0);
533 
536 }
ut16 val
Definition: armass64_const.h:6
RZ_API const char * rz_type_db_format_get(const RzTypeDB *typedb, const char *name)
Definition: format.c:1901
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
#define PFMT64u
Definition: rz_types.h:395
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
static RzTypeTypeclass get_base_type_typeclass(RZ_NONNULL const RzBaseType *type)
RZ_API RZ_BORROW const char * rz_type_typeclass_as_string(RzTypeTypeclass typeclass)
Returns the string representation of a typeclass.
Definition: typeclass.c:39

References get_base_type_typeclass(), key, PFMT64u, RZ_BASE_TYPE_KIND_ATOMIC, rz_return_if_fail, rz_strbuf_fini(), rz_strbuf_init(), rz_strbuf_setf(), rz_type_db_format_get(), rz_type_typeclass_as_string(), sdb_set(), type, and val.

Referenced by sdb_save_base_type().

◆ save_enum()

static void save_enum ( const RzTypeDB typedb,
Sdb sdb,
const RzBaseType type 
)
static

Definition at line 453 of file serialize_types.c.

453  {
454  rz_return_if_fail(typedb && sdb && type && type->name && type->kind == RZ_BASE_TYPE_KIND_ENUM);
455  /*
456  C:
457  enum name {case1 = 1, case2 = 2, caseN = 3};
458  Sdb:
459  name=enum
460  enum.name=arg1,arg2,argN
461  enum.MyEnum.0x1=arg1
462  enum.MyEnum.0x3=arg2
463  enum.MyEnum.0x63=argN
464  enum.MyEnum.arg1=0x1
465  enum.MyEnum.arg2=0x63
466  enum.MyEnum.argN=0x3
467  */
468  char *sname = type->name;
469  sdb_set(sdb, sname, "enum", 0);
470 
471  RzStrBuf arglist;
472  RzStrBuf param_key;
473  RzStrBuf param_val;
474  rz_strbuf_init(&arglist);
475  rz_strbuf_init(&param_key);
476  rz_strbuf_init(&param_val);
477 
478  int i = 0;
479  RzTypeEnumCase *cas;
480  rz_vector_foreach(&type->enum_data.cases, cas) {
481  // enum.name.arg1=type,offset,???
482  char *case_sname = rz_str_sanitize_sdb_key(cas->name);
483  sdb_set(sdb,
484  rz_strbuf_setf(&param_key, "enum.%s.%s", sname, case_sname),
485  rz_strbuf_setf(&param_val, "0x%" PFMT64x "", cas->val), 0);
486 
487  sdb_set(sdb,
488  rz_strbuf_setf(&param_key, "enum.%s.0x%" PFMT64x "", sname, cas->val),
489  case_sname, 0);
490  free(case_sname);
491 
492  rz_strbuf_appendf(&arglist, (i++ == 0) ? "%s" : ",%s", cas->name);
493  }
494  // enum.name=arg1,arg2,argN
495  char *key = rz_str_newf("enum.%s", sname);
496  sdb_set(sdb, key, rz_strbuf_get(&arglist), 0);
497  free(key);
498 
499  rz_strbuf_fini(&arglist);
500  rz_strbuf_fini(&param_key);
501  rz_strbuf_fini(&param_val);
502 }
lzma_index ** i
Definition: index.h:629
RZ_API char * rz_str_sanitize_sdb_key(const char *s)
Definition: str.c:1405
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define PFMT64x
Definition: rz_types.h:393
#define rz_vector_foreach(vec, it)
Definition: rz_vector.h:169

References free(), i, key, rz_type_enum_case_t::name, PFMT64x, RZ_BASE_TYPE_KIND_ENUM, rz_return_if_fail, rz_str_newf(), rz_str_sanitize_sdb_key(), rz_strbuf_appendf(), rz_strbuf_fini(), rz_strbuf_get(), rz_strbuf_init(), rz_strbuf_setf(), rz_vector_foreach, sdb_set(), type, and rz_type_enum_case_t::val.

Referenced by sdb_save_base_type().

◆ save_struct()

static void save_struct ( const RzTypeDB typedb,
Sdb sdb,
const RzBaseType type 
)
static

Definition at line 357 of file serialize_types.c.

357  {
358  rz_return_if_fail(typedb && sdb && type && type->name && type->kind == RZ_BASE_TYPE_KIND_STRUCT);
359  const char *kind = "struct";
360  /*
361  C:
362  struct name {type param1; type param2; type paramN;};
363  Sdb:
364  name=struct
365  struct.name=param1,param2,paramN
366  struct.name.param1=type,0,0
367  struct.name.param2=type,4,0
368  struct.name.paramN=type,8,0
369  */
370  char *sname = type->name;
371  // name=struct
372  sdb_set(sdb, sname, kind, 0);
373 
374  RzStrBuf arglist;
375  RzStrBuf param_key;
376  RzStrBuf param_val;
377  rz_strbuf_init(&arglist);
378  rz_strbuf_init(&param_key);
379  rz_strbuf_init(&param_val);
380 
381  int i = 0;
382  RzTypeStructMember *member;
383  rz_vector_foreach(&type->struct_data.members, member) {
384  // struct.name.param=type,offset,argsize
385  char *member_sname = rz_str_sanitize_sdb_key(member->name);
386  char *member_type = rz_type_as_string(typedb, member->type);
387  sdb_set(sdb,
388  rz_strbuf_setf(&param_key, "%s.%s.%s", kind, sname, member_sname),
389  rz_strbuf_setf(&param_val, "%s,%zu,%u", member_type, member->offset, 0), 0ULL);
390  free(member_type);
391  free(member_sname);
392 
393  rz_strbuf_appendf(&arglist, (i++ == 0) ? "%s" : ",%s", member->name);
394  }
395  // struct.name=param1,param2,paramN
396  char *key = rz_str_newf("%s.%s", kind, sname);
397  sdb_set(sdb, key, rz_strbuf_get(&arglist), 0);
398  free(key);
399 
400  rz_strbuf_fini(&arglist);
401  rz_strbuf_fini(&param_key);
402  rz_strbuf_fini(&param_val);
403 }
RZ_API RZ_OWN char * rz_type_as_string(const RzTypeDB *typedb, RZ_NONNULL const RzType *type)
Returns the type C representation.
Definition: type.c:817

References free(), i, key, rz_type_struct_member_t::name, rz_type_struct_member_t::offset, RZ_BASE_TYPE_KIND_STRUCT, rz_return_if_fail, rz_str_newf(), rz_str_sanitize_sdb_key(), rz_strbuf_appendf(), rz_strbuf_fini(), rz_strbuf_get(), rz_strbuf_init(), rz_strbuf_setf(), rz_type_as_string(), rz_vector_foreach, sdb_set(), type, and rz_type_struct_member_t::type.

Referenced by sdb_save_base_type().

◆ save_typedef()

static void save_typedef ( const RzTypeDB typedb,
Sdb sdb,
const RzBaseType type 
)
static

Definition at line 538 of file serialize_types.c.

538  {
539  rz_return_if_fail(typedb && sdb && type && type->name && type->kind == RZ_BASE_TYPE_KIND_TYPEDEF);
540  /*
541  C:
542  typedef char byte;
543  Sdb:
544  byte=typedef
545  typedef.byte=char
546  */
547  char *sname = type->name;
548  sdb_set(sdb, sname, "typedef", 0);
549 
550  RzStrBuf key;
551  RzStrBuf val;
554 
555  char *ttype = rz_type_as_string(typedb, type->type);
556  sdb_set(sdb,
557  rz_strbuf_setf(&key, "typedef.%s", sname),
558  rz_strbuf_setf(&val, "%s", ttype), 0);
559 
560  free(ttype);
561 
564 }

References free(), key, RZ_BASE_TYPE_KIND_TYPEDEF, rz_return_if_fail, rz_strbuf_fini(), rz_strbuf_init(), rz_strbuf_setf(), rz_type_as_string(), sdb_set(), type, and val.

Referenced by sdb_save_base_type().

◆ save_union()

static void save_union ( const RzTypeDB typedb,
Sdb sdb,
const RzBaseType type 
)
static

Definition at line 405 of file serialize_types.c.

405  {
406  rz_return_if_fail(typedb && sdb && type && type->name && type->kind == RZ_BASE_TYPE_KIND_UNION);
407  const char *kind = "union";
408  /*
409  C:
410  union name {type param1; type param2; type paramN;};
411  Sdb:
412  name=union
413  union.name=param1,param2,paramN
414  union.name.param1=type,0,0
415  union.name.param2=type,0,0
416  union.name.paramN=type,0,0
417  */
418  char *sname = type->name;
419  // name=union
420  sdb_set(sdb, sname, kind, 0);
421 
422  RzStrBuf arglist;
423  RzStrBuf param_key;
424  RzStrBuf param_val;
425  rz_strbuf_init(&arglist);
426  rz_strbuf_init(&param_key);
427  rz_strbuf_init(&param_val);
428 
429  int i = 0;
430  RzTypeUnionMember *member;
431  rz_vector_foreach(&type->union_data.members, member) {
432  // union.name.arg1=type,offset,argsize
433  char *member_sname = rz_str_sanitize_sdb_key(member->name);
434  char *member_type = rz_type_as_string(typedb, member->type);
435  sdb_set(sdb,
436  rz_strbuf_setf(&param_key, "%s.%s.%s", kind, sname, member_sname),
437  rz_strbuf_setf(&param_val, "%s,%zu,%u", member_type, member->offset, 0), 0ULL);
438  free(member_type);
439  free(member_sname);
440 
441  rz_strbuf_appendf(&arglist, (i++ == 0) ? "%s" : ",%s", member->name);
442  }
443  // union.name=arg1,arg2,argN
444  char *key = rz_str_newf("%s.%s", kind, sname);
445  sdb_set(sdb, key, rz_strbuf_get(&arglist), 0);
446  free(key);
447 
448  rz_strbuf_fini(&arglist);
449  rz_strbuf_fini(&param_key);
450  rz_strbuf_fini(&param_val);
451 }

References free(), i, key, rz_type_union_member_t::name, rz_type_union_member_t::offset, RZ_BASE_TYPE_KIND_UNION, rz_return_if_fail, rz_str_newf(), rz_str_sanitize_sdb_key(), rz_strbuf_appendf(), rz_strbuf_fini(), rz_strbuf_get(), rz_strbuf_init(), rz_strbuf_setf(), rz_type_as_string(), rz_vector_foreach, sdb_set(), type, and rz_type_union_member_t::type.

Referenced by sdb_save_base_type().

◆ sdb_load_base_types()

bool sdb_load_base_types ( RzTypeDB typedb,
Sdb sdb 
)

Definition at line 319 of file serialize_types.c.

319  {
320  rz_return_val_if_fail(typedb && sdb, false);
321  SdbKv *kv;
322  SdbListIter *iter;
323  SdbList *l = sdb_foreach_list(sdb, false);
324  ls_foreach (l, iter, kv) {
325  TypeFormatPair *tpair = NULL;
326  if (!strcmp(sdbkv_value(kv), "struct")) {
327  tpair = get_struct_type(typedb, sdb, sdbkv_key(kv));
328  } else if (!strcmp(sdbkv_value(kv), "enum")) {
329  tpair = get_enum_type(sdb, sdbkv_key(kv));
330  } else if (!strcmp(sdbkv_value(kv), "union")) {
331  tpair = get_union_type(typedb, sdb, sdbkv_key(kv));
332  } else if (!strcmp(sdbkv_value(kv), "typedef")) {
333  tpair = get_typedef_type(typedb, sdb, sdbkv_key(kv));
334  } else if (!strcmp(sdbkv_value(kv), "type")) {
335  tpair = get_atomic_type(typedb, sdb, sdbkv_key(kv));
336  }
337  if (tpair && tpair->type) {
338  ht_pp_update(typedb->types, tpair->type->name, tpair->type);
339  // If the SDB provided the preferred type format then we store it
340  char *format = tpair->format ? tpair->format : NULL;
341  // Format is not always defined, e.g. for types like "void" or anonymous types
342  if (format) {
343  ht_pp_update(typedb->formats, tpair->type->name, format);
344  RZ_LOG_DEBUG("inserting the \"%s\" type & format: \"%s\"\n", tpair->type->name, format);
345  } else {
346  ht_pp_delete(typedb->formats, tpair->type->name);
347  }
348  } else if (tpair) {
349  free(tpair->format);
350  }
351  free(tpair);
352  }
353  ls_free(l);
354  return true;
355 }
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
#define ls_foreach(list, it, pos)
Definition: ls.h:31
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
RZ_API SdbList * sdb_foreach_list(Sdb *s, bool sorted)
Definition: sdb.c:630
static char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21
static char * sdbkv_value(const SdbKv *kv)
Definition: sdbht.h:25
static TypeFormatPair * get_atomic_type(RzTypeDB *typedb, Sdb *sdb, const char *sname)
static TypeFormatPair * get_struct_type(RzTypeDB *typedb, Sdb *sdb, const char *sname)
static TypeFormatPair * get_typedef_type(RzTypeDB *typedb, Sdb *sdb, const char *sname)
static TypeFormatPair * get_enum_type(Sdb *sdb, const char *sname)
static TypeFormatPair * get_union_type(RzTypeDB *typedb, Sdb *sdb, const char *sname)
Definition: ls.h:17
Definition: ls.h:22
HtPP * formats
Definition: rz_type.h:34
HtPP * types
Definition: rz_type.h:33
Definition: sdbht.h:14

References TypeFormatPair::format, rz_type_db_t::formats, free(), get_atomic_type(), get_enum_type(), get_struct_type(), get_typedef_type(), get_union_type(), ls_foreach, ls_free(), rz_base_type_t::name, NULL, RZ_LOG_DEBUG, rz_return_val_if_fail, sdb_foreach_list(), sdbkv_key(), sdbkv_value(), TypeFormatPair::type, and rz_type_db_t::types.

Referenced by types_load_sdb().

◆ sdb_load_by_path()

static bool sdb_load_by_path ( RZ_NONNULL RzTypeDB typedb,
RZ_NONNULL const char *  path 
)
static

Definition at line 617 of file serialize_types.c.

617  {
618  rz_return_val_if_fail(typedb && path, false);
619  if (RZ_STR_ISEMPTY(path)) {
620  return false;
621  }
622  Sdb *db = sdb_new(0, path, 0);
623  bool result = types_load_sdb(db, typedb);
624  sdb_close(db);
625  sdb_free(db);
626  return result;
627 }
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API void sdb_close(Sdb *s)
Definition: sdb.c:416
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
Definition: sdb.h:63

References path, rz_return_val_if_fail, RZ_STR_ISEMPTY, sdb_close(), sdb_free(), sdb_new(), typedb_sdb::typedb, and types_load_sdb().

Referenced by rz_type_db_load_sdb().

◆ sdb_load_from_string()

static bool sdb_load_from_string ( RZ_NONNULL RzTypeDB typedb,
RZ_NONNULL const char *  string 
)
static

Definition at line 629 of file serialize_types.c.

629  {
630  rz_return_val_if_fail(typedb && string, false);
631  if (RZ_STR_ISEMPTY(string)) {
632  return false;
633  }
634  Sdb *db = sdb_new0();
635  sdb_query_lines(db, string);
636  bool result = types_load_sdb(db, typedb);
637  sdb_close(db);
638  sdb_free(db);
639  return result;
640 }
RZ_API int sdb_query_lines(Sdb *s, const char *cmd)
Definition: query.c:800
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43

References rz_return_val_if_fail, RZ_STR_ISEMPTY, sdb_close(), sdb_free(), sdb_new0(), sdb_query_lines(), typedb_sdb::typedb, and types_load_sdb().

Referenced by rz_type_db_load_sdb_str().

◆ sdb_save_base_type()

void sdb_save_base_type ( const RzTypeDB typedb,
RZ_NONNULL Sdb sdb,
const RzBaseType type 
)

Definition at line 566 of file serialize_types.c.

566  {
567  rz_return_if_fail(typedb && sdb && type && type->name);
568 
569  switch (type->kind) {
571  RZ_LOG_DEBUG("Serializing struct \"%s\"\n", type->name);
572  save_struct(typedb, sdb, type);
573  break;
575  RZ_LOG_DEBUG("Serializing enum \"%s\"\n", type->name);
576  save_enum(typedb, sdb, type);
577  break;
579  RZ_LOG_DEBUG("Serializing union \"%s\"\n", type->name);
580  save_union(typedb, sdb, type);
581  break;
583  RZ_LOG_DEBUG("Serializing type alias \"%s\"\n", type->name);
584  save_typedef(typedb, sdb, type);
585  break;
587  RZ_LOG_DEBUG("Serializing atomic type \"%s\"\n", type->name);
588  save_atomic_type(typedb, sdb, type);
589  break;
590  default:
591  break;
592  }
593 }
static void save_enum(const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
static void save_struct(const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
static void save_union(const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
static void save_typedef(const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)
static void save_atomic_type(const RzTypeDB *typedb, Sdb *sdb, const RzBaseType *type)

References RZ_BASE_TYPE_KIND_ATOMIC, RZ_BASE_TYPE_KIND_ENUM, RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_TYPEDEF, RZ_BASE_TYPE_KIND_UNION, RZ_LOG_DEBUG, rz_return_if_fail, save_atomic_type(), save_enum(), save_struct(), save_typedef(), save_union(), and type.

Referenced by export_base_type_cb().

◆ set_base_type_typeclass()

static void set_base_type_typeclass ( RZ_NONNULL RzBaseType type,
RzTypeTypeclass  typeclass 
)
inlinestatic

Definition at line 20 of file serialize_types.c.

20  {
22  type->attrs = typeclass;
23 }

References rz_return_if_fail, RZ_TYPE_TYPECLASS_INVALID, and type.

Referenced by get_atomic_type().

◆ types_export_sdb()

static bool types_export_sdb ( RZ_NONNULL Sdb db,
RZ_NONNULL const RzTypeDB typedb 
)
static

Definition at line 611 of file serialize_types.c.

611  {
612  struct typedb_sdb tdb = { typedb, db };
613  ht_pp_foreach(typedb->types, export_base_type_cb, &tdb);
614  return true;
615 }
static bool export_base_type_cb(void *user, const void *k, const void *v)
const RzTypeDB * typedb

References export_base_type_cb(), typedb_sdb::typedb, and rz_type_db_t::types.

Referenced by rz_serialize_types_save().

◆ types_load_sdb()

RZ_IPI bool types_load_sdb ( RZ_NONNULL Sdb db,
RZ_NONNULL RzTypeDB typedb 
)

Definition at line 595 of file serialize_types.c.

595  {
596  return sdb_load_base_types(typedb, db);
597 }
bool sdb_load_base_types(RzTypeDB *typedb, Sdb *sdb)

References sdb_load_base_types().

Referenced by rz_serialize_types_load(), sdb_load_by_path(), and sdb_load_from_string().