Rizin
unix-like reverse engineering framework and cli tools
ns.c File Reference
#include "sdb.h"

Go to the source code of this file.

Functions

RZ_API void sdb_ns_lock (Sdb *s, int lock, int depth)
 
static int in_list (SdbList *list, void *item)
 
static void ns_free_exc_list (Sdb *s, SdbList *list)
 
RZ_API void sdb_ns_free_all (Sdb *s)
 
static SdbNssdb_ns_new (Sdb *s, const char *name, ut32 hash)
 
static void sdb_ns_free (SdbNs *ns)
 
RZ_API bool sdb_ns_unset (Sdb *s, const char *name, Sdb *r)
 
RZ_API int sdb_ns_set (Sdb *s, const char *name, Sdb *r)
 
RZ_API Sdbsdb_ns (Sdb *s, const char *name, int create)
 
RZ_API Sdbsdb_ns_path (Sdb *s, const char *path, int create)
 
static void ns_sync (Sdb *s, SdbList *list)
 
RZ_API void sdb_ns_sync (Sdb *s)
 

Function Documentation

◆ in_list()

static int in_list ( SdbList list,
void *  item 
)
static

Definition at line 17 of file ns.c.

17  {
18  SdbNs *ns;
19  SdbListIter *it;
20  if (list && item)
21  ls_foreach (list, it, ns) {
22  if (item == ns) {
23  return 1;
24  }
25  }
26  return 0;
27 }
static void list(RzEgg *egg)
Definition: rz-gg.c:52
#define ls_foreach(list, it, pos)
Definition: ls.h:31
Definition: ls.h:17
Definition: sdb.h:88

References list(), and ls_foreach.

Referenced by ns_free_exc_list(), and ns_sync().

◆ ns_free_exc_list()

static void ns_free_exc_list ( Sdb s,
SdbList list 
)
static

Definition at line 29 of file ns.c.

29  {
30  SdbListIter next;
31  SdbListIter *it;
32  int deleted;
33  SdbNs *ns;
34  if (!list || !s) {
35  return;
36  }
37  // TODO: Implement and use ls_foreach_safe
38  if (in_list(list, s)) {
39  return;
40  }
41  ls_append(list, s);
42  ls_foreach (s->ns, it, ns) {
43  deleted = 0;
44  next.n = it->n;
45  if (!in_list(list, ns)) {
46  ls_delete(s->ns, it); // free (it)
47  free(ns->name);
48  ns->name = NULL;
49  deleted = 1;
50  if (ns->sdb) {
51  if (sdb_free(ns->sdb)) {
52  ns->sdb = NULL;
53  free(ns->name);
54  ns->name = NULL;
55  }
56  }
57  ls_append(list, ns);
58  ls_append(list, ns->sdb);
60  sdb_free(ns->sdb);
61  }
62  if (!deleted) {
63  sdb_free(ns->sdb);
64  s->ns->free = NULL;
65  ls_delete(s->ns, it); // free (it)
66  }
67  free(ns);
68  it = &next;
69  }
70  ls_free(s->ns);
71  s->ns = NULL;
72 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API SdbListIter * ls_append(SdbList *list, void *data)
Definition: ls.c:200
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
RZ_API void ls_delete(SdbList *list, SdbListIter *iter)
Definition: ls.c:133
static void ns_free_exc_list(Sdb *s, SdbList *list)
Definition: ns.c:29
static int in_list(SdbList *list, void *item)
Definition: ns.c:17
static RzSocket * s
Definition: rtr.c:28
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
struct ls_iter_t * n
Definition: ls.h:19
char * name
Definition: sdb.h:89
Sdb * sdb
Definition: sdb.h:91

References free(), in_list(), list(), ls_append(), ls_delete(), ls_foreach, ls_free(), ls_iter_t::n, sdb_ns_t::name, NULL, s, sdb_ns_t::sdb, and sdb_free().

Referenced by sdb_ns_free_all().

◆ ns_sync()

static void ns_sync ( Sdb s,
SdbList list 
)
static

Definition at line 234 of file ns.c.

234  {
235  SdbNs *ns;
236  SdbListIter *it;
237  ls_foreach (s->ns, it, ns) {
238  if (in_list(list, ns)) {
239  continue;
240  }
241  ls_append(list, ns);
242  ns_sync(ns->sdb, list);
243  sdb_sync(ns->sdb);
244  }
245  sdb_sync(s);
246 }
static void ns_sync(Sdb *s, SdbList *list)
Definition: ns.c:234
RZ_API bool sdb_sync(Sdb *s)
Definition: sdb.c:803

References in_list(), list(), ls_append(), ls_foreach, s, sdb_ns_t::sdb, and sdb_sync().

Referenced by sdb_ns_sync().

◆ sdb_ns()

RZ_API Sdb* sdb_ns ( Sdb s,
const char *  name,
int  create 
)

Definition at line 186 of file ns.c.

186  {
187  SdbListIter *it;
188  SdbNs *ns;
189  ut32 hash;
190  if (!s || !name || !*name) {
191  return NULL;
192  }
193  hash = sdb_hash(name);
194  ls_foreach (s->ns, it, ns) {
195  if (ns->hash == hash) {
196  return ns->sdb;
197  }
198  }
199  if (!create) {
200  return NULL;
201  }
202  if (s->ns_lock) {
203  return NULL;
204  }
205  ns = sdb_ns_new(s, name, hash);
206  if (!ns) {
207  return NULL;
208  }
209  ls_append(s->ns, ns);
210  return ns->sdb;
211 }
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
uint32_t ut32
static SdbNs * sdb_ns_new(Sdb *s, const char *name, ut32 hash)
Definition: ns.c:87
RZ_API ut32 sdb_hash(const char *key)
Definition: util.c:22
Definition: z80asm.h:102
ut32 hash
Definition: sdb.h:90

References create(), sdb_ns_t::hash, ls_append(), ls_foreach, NULL, s, sdb_ns_t::sdb, sdb_hash(), and sdb_ns_new().

Referenced by core_analysis_graph_nodes(), file_save_cb(), load_process_line(), print_rop(), rop_classify(), rop_kuery(), rz_analysis_dwarf_process_info(), rz_analysis_new(), rz_cmd_kuery(), rz_cmd_search(), rz_core_analysis_everything(), rz_core_bin_export_info(), rz_core_bin_get_compile_time(), rz_core_search_rop(), rz_debug_session_serialize(), rz_project_load(), rz_project_migrate_v1_v2(), rz_project_migrate_v2_v3(), rz_project_migrate_v3_v4(), rz_project_migrate_v5_v6(), rz_project_save(), rz_serialize_analysis_classes_load(), rz_serialize_analysis_meta_load(), rz_serialize_analysis_meta_save(), rz_serialize_analysis_save(), rz_serialize_core_save(), rz_serialize_debug_save(), rz_serialize_flag_load(), rz_serialize_flag_save(), rz_serialize_io_files_save(), rz_serialize_io_save(), rz_serialize_spaces_load(), rz_serialize_spaces_save(), sdb_copy(), sdb_diff_ctx(), sdb_ns_path(), sdb_querys(), and update_sdb().

◆ sdb_ns_free()

static void sdb_ns_free ( SdbNs ns)
static

Definition at line 130 of file ns.c.

130  {
131  sdb_free(ns->sdb);
132  free(ns->name);
133  free(ns);
134 }

References free(), sdb_ns_t::name, sdb_ns_t::sdb, and sdb_free().

Referenced by sdb_ns_unset().

◆ sdb_ns_free_all()

RZ_API void sdb_ns_free_all ( Sdb s)

Definition at line 74 of file ns.c.

74  {
75  SdbList *list;
76  if (!s) {
77  return;
78  }
79  list = ls_new();
80  list->free = NULL;
82  ls_free(list);
83  ls_free(s->ns);
84  s->ns = NULL;
85 }
RZ_API SdbList * ls_new(void)
Definition: ls.c:16
Definition: ls.h:22

References list(), ls_free(), ls_new(), ns_free_exc_list(), NULL, and s.

Referenced by sdb_fini().

◆ sdb_ns_lock()

RZ_API void sdb_ns_lock ( Sdb s,
int  lock,
int  depth 
)

Definition at line 6 of file ns.c.

6  {
7  SdbListIter *it;
8  SdbNs *ns;
9  s->ns_lock = lock;
10  if (depth) { // handles -1 as infinite
11  ls_foreach (s->ns, it, ns) {
12  sdb_ns_lock(ns->sdb, lock, depth - 1);
13  }
14  }
15 }
RZ_API void sdb_ns_lock(Sdb *s, int lock, int depth)
Definition: ns.c:6
static void lock(volatile int *lk)
Definition: malloc.c:61

References lock(), ls_foreach, s, and sdb_ns_t::sdb.

◆ sdb_ns_new()

static SdbNs* sdb_ns_new ( Sdb s,
const char *  name,
ut32  hash 
)
static

Definition at line 87 of file ns.c.

87  {
88  char dir[SDB_MAX_PATH];
89  SdbNs *ns;
90  if (s->dir && *s->dir && name && *name) {
91  int dir_len = strlen(s->dir);
92  int name_len = strlen(name);
93  if ((dir_len + name_len + 3) > SDB_MAX_PATH) {
94  return NULL;
95  }
96  memcpy(dir, s->dir, dir_len);
97  memcpy(dir + dir_len, ".", 1);
98  memcpy(dir + dir_len + 1, name, name_len + 1);
99  } else {
100  dir[0] = 0;
101  }
102  ns = malloc(sizeof(SdbNs));
103  if (!ns) {
104  return NULL;
105  }
106  ns->hash = hash;
107  ns->name = name ? strdup(name) : NULL;
108  // ns->sdb = sdb_new (dir, ns->name, 0);
109  ns->sdb = sdb_new0();
110  // TODO: generate path
111 
112  if (ns->sdb) {
113  free(ns->sdb->path);
114  ns->sdb->path = NULL;
115  if (*dir) {
116  ns->sdb->path = strdup(dir);
117  }
118  free(ns->sdb->name);
119  if (name && *name) {
120  ns->sdb->name = strdup(name);
121  }
122  } else {
123  free(ns->name);
124  free(ns);
125  ns = NULL;
126  }
127  return ns;
128 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
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 Sdb * sdb_new0(void)
Definition: sdb.c:43
#define SDB_MAX_PATH
Definition: sdb.h:49
char * path
Definition: sdb.h:65
char * name
Definition: sdb.h:66

References free(), sdb_ns_t::hash, malloc(), memcpy(), sdb_t::name, sdb_ns_t::name, NULL, sdb_t::path, s, sdb_ns_t::sdb, SDB_MAX_PATH, sdb_new0(), and strdup().

Referenced by sdb_ns().

◆ sdb_ns_path()

RZ_API Sdb* sdb_ns_path ( Sdb s,
const char *  path,
int  create 
)

Definition at line 213 of file ns.c.

213  {
214  char *ptr, *str;
215  char *slash;
216 
217  if (!s || !path || !*path)
218  return s;
219  ptr = str = strdup(path);
220  do {
221  slash = strchr(ptr, '/');
222  if (slash)
223  *slash = 0;
224  s = sdb_ns(s, ptr, create);
225  if (!s)
226  break;
227  if (slash)
228  ptr = slash + 1;
229  } while (slash);
230  free(str);
231  return s;
232 }
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 Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186

References create(), free(), path, s, sdb_ns(), cmd_descs_generate::str, and strdup().

Referenced by bin_elf_versioninfo_verneed(), bin_elf_versioninfo_versym(), bin_pe_versioninfo(), rz_cmd_kuery(), rz_core_bin_apply_resources(), and session_sdb_load_ns().

◆ sdb_ns_set()

RZ_API int sdb_ns_set ( Sdb s,
const char *  name,
Sdb r 
)

Definition at line 156 of file ns.c.

156  {
157  SdbNs *ns;
158  SdbListIter *it;
159  ut32 hash = sdb_hash(name);
160  if (!s || !r || !name) {
161  return 0;
162  }
163  ls_foreach (s->ns, it, ns) {
164  if (ns->hash == hash) {
165  if (ns->sdb == r) {
166  return 0;
167  }
168  sdb_free(ns->sdb);
169  r->refs++; // sdb_ref / sdb_unref //
170  ns->sdb = r;
171  return 1;
172  }
173  }
174  if (s->ns_lock) {
175  return 0;
176  }
177  ns = RZ_NEW(SdbNs);
178  ns->name = strdup(name);
179  ns->hash = hash;
180  ns->sdb = r;
181  r->refs++;
182  ls_append(s->ns, ns);
183  return 1;
184 }
#define r
Definition: crypto_rc6.c:12
#define RZ_NEW(x)
Definition: rz_types.h:285

References sdb_ns_t::hash, ls_append(), ls_foreach, sdb_ns_t::name, r, RZ_NEW, s, sdb_ns_t::sdb, sdb_free(), sdb_hash(), and strdup().

Referenced by _parse_resource_directory(), _store_resource_sdb(), get_gnu_verneed(), get_sdb(), get_verneed_entry_sdb(), init_symbols_info_aux(), load(), load_buffer(), Pe_r_bin_store_resource_version_info(), Pe_r_bin_store_string_file_info(), Pe_r_bin_store_string_table(), Pe_r_bin_store_var_file_info(), rz_bin_elf_get_symbols_info(), rz_bin_object_new(), update_sdb(), and update_syscall_ns().

◆ sdb_ns_sync()

RZ_API void sdb_ns_sync ( Sdb s)

Definition at line 248 of file ns.c.

248  {
249  SdbList *list = ls_new();
250  ns_sync(s, list);
251  list->free = NULL;
252  ls_free(list);
253 }

References list(), ls_free(), ls_new(), ns_sync(), NULL, and s.

◆ sdb_ns_unset()

RZ_API bool sdb_ns_unset ( Sdb s,
const char *  name,
Sdb r 
)

Definition at line 136 of file ns.c.

136  {
137  SdbNs *ns;
138  SdbListIter *it;
139  if (s && (name || r)) {
140  ls_foreach (s->ns, it, ns) {
141  if (name && (!strcmp(name, ns->name))) {
142  sdb_ns_free(ns);
143  ls_delete(s->ns, it);
144  return true;
145  }
146  if (r && ns->sdb == r) {
147  sdb_ns_free(ns);
148  ls_delete(s->ns, it);
149  return true;
150  }
151  }
152  }
153  return false;
154 }
static void sdb_ns_free(SdbNs *ns)
Definition: ns.c:130

References ls_delete(), ls_foreach, sdb_ns_t::name, r, s, sdb_ns_t::sdb, and sdb_ns_free().

Referenced by rz_project_migrate_v6_v7(), rz_project_migrate_v7_v8(), and update_syscall_ns().