Rizin
unix-like reverse engineering framework and cli tools
ns.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 
6 RZ_API void sdb_ns_lock(Sdb *s, int lock, int depth) {
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 }
16 
17 static int in_list(SdbList *list, void *item) {
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 }
28 
29 static void ns_free_exc_list(Sdb *s, SdbList *list) {
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 }
73 
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 }
86 
87 static SdbNs *sdb_ns_new(Sdb *s, const char *name, ut32 hash) {
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 }
129 
130 static void sdb_ns_free(SdbNs *ns) {
131  sdb_free(ns->sdb);
132  free(ns->name);
133  free(ns);
134 }
135 
136 RZ_API bool sdb_ns_unset(Sdb *s, const char *name, Sdb *r) {
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 }
155 
156 RZ_API int sdb_ns_set(Sdb *s, const char *name, Sdb *r) {
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 }
185 
186 RZ_API Sdb *sdb_ns(Sdb *s, const char *name, int create) {
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 }
212 
213 RZ_API Sdb *sdb_ns_path(Sdb *s, const char *path, int create) {
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 }
233 
234 static void ns_sync(Sdb *s, SdbList *list) {
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 }
247 
249  SdbList *list = ls_new();
250  ns_sync(s, list);
251  list->free = NULL;
252  ls_free(list);
253 }
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
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 SdbList * ls_new(void)
Definition: ls.c:16
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
#define ls_foreach(list, it, pos)
Definition: ls.h:31
static void ns_sync(Sdb *s, SdbList *list)
Definition: ns.c:234
RZ_API int sdb_ns_set(Sdb *s, const char *name, Sdb *r)
Definition: ns.c:156
RZ_API void sdb_ns_free_all(Sdb *s)
Definition: ns.c:74
RZ_API Sdb * sdb_ns_path(Sdb *s, const char *path, int create)
Definition: ns.c:213
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
static void ns_free_exc_list(Sdb *s, SdbList *list)
Definition: ns.c:29
static void sdb_ns_free(SdbNs *ns)
Definition: ns.c:130
RZ_API void sdb_ns_lock(Sdb *s, int lock, int depth)
Definition: ns.c:6
RZ_API void sdb_ns_sync(Sdb *s)
Definition: ns.c:248
RZ_API bool sdb_ns_unset(Sdb *s, const char *name, Sdb *r)
Definition: ns.c:136
static int in_list(SdbList *list, void *item)
Definition: ns.c:17
static SdbNs * sdb_ns_new(Sdb *s, const char *name, ut32 hash)
Definition: ns.c:87
static RzSocket * s
Definition: rtr.c:28
#define RZ_NEW(x)
Definition: rz_types.h:285
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
RZ_API bool sdb_sync(Sdb *s)
Definition: sdb.c:803
RZ_API ut32 sdb_hash(const char *key)
Definition: util.c:22
#define SDB_MAX_PATH
Definition: sdb.h:49
Definition: ls.h:17
struct ls_iter_t * n
Definition: ls.h:19
Definition: ls.h:22
Definition: z80asm.h:102
Definition: sdb.h:88
char * name
Definition: sdb.h:89
Sdb * sdb
Definition: sdb.h:91
ut32 hash
Definition: sdb.h:90
Definition: sdb.h:63
char * path
Definition: sdb.h:65
char * name
Definition: sdb.h:66
static void lock(volatile int *lk)
Definition: malloc.c:61