Rizin
unix-like reverse engineering framework and cli tools
serialize_typelink.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Anton Kochkov <anton.kochkov@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util/rz_serialize.h>
5 #include <rz_list.h>
6 #include <rz_vector.h>
7 #include <rz_analysis.h>
8 #include <sdb.h>
9 
10 static bool typelinks_load_sdb(RzAnalysis *analysis, Sdb *sdb) {
11  rz_return_val_if_fail(analysis && sdb, false);
12  RzType *type;
13  SdbKv *kv;
15  SdbList *l = sdb_foreach_list(sdb, false);
16  ls_foreach (l, iter, kv) {
18  if (addr > 0) {
19  char *error_msg = NULL;
20  type = rz_type_parse_string_single(analysis->typedb->parser, sdbkv_value(kv), &error_msg);
21  if (type) {
22  ht_up_insert(analysis->type_links, addr, type);
23  }
24  free(error_msg);
25  }
26  }
27  return true;
28 }
29 
30 static void save_typelink(const RzAnalysis *analysis, Sdb *sdb, const RzType *type, ut64 addr) {
31  rz_return_if_fail(analysis && sdb && type);
32  /*
33  Sdb:
34  addr=type
35  */
36 
37  // addr=type
38  char *key = rz_str_newf("0x%08" PFMT64x, addr);
39  char *type_str = rz_type_as_string(analysis->typedb, type);
40  sdb_set(sdb, key, type_str, 0);
41 }
42 
43 struct analysis_sdb {
45  Sdb *sdb;
46 };
47 
48 static bool export_typelink_cb(void *user, ut64 k, const void *v) {
49  struct analysis_sdb *s = user;
50  RzType *type = (RzType *)v;
51  save_typelink(s->analysis, s->sdb, type, k);
52  return true;
53 }
54 
56  rz_return_val_if_fail(db && analysis, false);
57  struct analysis_sdb tdb = { analysis, db };
58  ht_up_foreach(analysis->type_links, export_typelink_cb, &tdb);
59  return true;
60 }
61 
71 }
72 
81  rz_return_val_if_fail(db && analysis, false);
82  return typelinks_load_sdb(analysis, db);
83 }
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
#define RZ_API
#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
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define ls_foreach(list, it, pos)
Definition: ls.h:31
int type
Definition: mipsasm.c:17
static RzSocket * s
Definition: rtr.c:28
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NONNULL
Definition: rz_types.h:64
#define PFMT64x
Definition: rz_types.h:393
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
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
const RzAnalysis * analysis
Definition: ls.h:17
Definition: ls.h:22
HtUP * type_links
Definition: rz_analysis.h:603
RzTypeDB * typedb
Definition: rz_analysis.h:602
RzTypeParser * parser
Definition: rz_type.h:37
Definition: sdbht.h:14
Definition: sdb.h:63
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
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58