Rizin
unix-like reverse engineering framework and cli tools
serialize_spaces.c File Reference

Go to the source code of this file.

Macros

#define KEY_NAME   "name"
 
#define KEY_SPACESTACK   "spacestack"
 
#define KEY_SPACES   "spaces"
 

Functions

RZ_API void rz_serialize_spaces_save (RZ_NONNULL Sdb *db, RZ_NONNULL RzSpaces *spaces)
 
static bool foreach_space_cb (void *user, const char *k, const char *v)
 
RZ_API bool rz_serialize_spaces_load (RZ_NONNULL Sdb *db, RZ_NONNULL RzSpaces *spaces, bool load_name, RZ_NULLABLE RzSerializeResultInfo *res)
 

Macro Definition Documentation

◆ KEY_NAME

#define KEY_NAME   "name"

Definition at line 18 of file serialize_spaces.c.

◆ KEY_SPACES

#define KEY_SPACES   "spaces"

Definition at line 20 of file serialize_spaces.c.

◆ KEY_SPACESTACK

#define KEY_SPACESTACK   "spacestack"

Definition at line 19 of file serialize_spaces.c.

Function Documentation

◆ foreach_space_cb()

static bool foreach_space_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 48 of file serialize_spaces.c.

48  {
49  RzSpaces *spaces = user;
51  return true;
52 }
const char * k
Definition: dsignal.c:11
const char * spaces(int count)
RZ_API RzSpace * rz_spaces_add(RzSpaces *sp, const char *name)
Definition: spaces.c:96

References k, rz_spaces_add(), and spaces().

Referenced by rz_serialize_spaces_load().

◆ rz_serialize_spaces_load()

RZ_API bool rz_serialize_spaces_load ( RZ_NONNULL Sdb db,
RZ_NONNULL RzSpaces spaces,
bool  load_name,
RZ_NULLABLE RzSerializeResultInfo res 
)
Parameters
load_namewhether to overwrite the name in spaces with the value from db

Definition at line 54 of file serialize_spaces.c.

54  {
55  if (load_name) {
56  char *old_name = (char *)spaces->name;
57  spaces->name = sdb_get(db, KEY_NAME, NULL);
58  if (!spaces->name) {
59  spaces->name = old_name;
60  RZ_SERIALIZE_ERR(res, "failed to get spaces name from db");
61  return false;
62  }
63  free(old_name);
64  }
65 
67 
68  Sdb *db_spaces = sdb_ns(db, KEY_SPACES, false);
69  if (!db_spaces) {
70  RZ_SERIALIZE_ERR(res, "failed to get spaces sub-namespace");
71  return false;
72  }
73  sdb_foreach(db_spaces, foreach_space_cb, spaces);
74 
75  char *stack_json_str = sdb_get(db, KEY_SPACESTACK, NULL);
76  if (!stack_json_str) {
77  RZ_SERIALIZE_ERR(res, "spacestack is missing");
78  return false;
79  }
80 
81  bool ret = true;
82  RzJson *stack_json = rz_json_parse(stack_json_str);
83  if (!stack_json) {
84  RZ_SERIALIZE_ERR(res, "failed to parse stackspace json");
85  ret = false;
86  goto beach;
87  }
88  if (stack_json->type != RZ_JSON_ARRAY) {
89  RZ_SERIALIZE_ERR(res, "stackspace json is not an array");
90  ret = false;
91  goto beach;
92  }
93  RzJson *stack_element;
94  for (stack_element = stack_json->children.first; stack_element; stack_element = stack_element->next) {
95  if (stack_element->type != RZ_JSON_STRING) {
96  RZ_SERIALIZE_ERR(res, "stackspace element is not a string");
97  ret = false;
98  goto beach;
99  }
100  RzSpace *space = rz_spaces_get(spaces, stack_element->str_value);
101  rz_list_append(spaces->spacestack, space ? space->name : "*");
102  }
103 
104  rz_spaces_pop(spaces); // current is the top stack element, pop it
105 
106 beach:
107  rz_json_free(stack_json);
108  free(stack_json_str);
109  return ret;
110 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
@ RZ_JSON_ARRAY
Definition: rz_json.h:31
@ RZ_JSON_STRING
Definition: rz_json.h:32
RZ_API RzJson * rz_json_parse(char *text)
Definition: json_parser.c:382
RZ_API void rz_json_free(RzJson *js)
Definition: json_parser.c:45
#define RZ_SERIALIZE_ERR(res,...)
Push an error to the local RzSerializeResultInfo \res RzSerializeInfoResult *.
Definition: rz_serialize.h:33
RZ_API RzSpace * rz_spaces_get(RzSpaces *sp, const char *name)
Definition: spaces.c:82
RZ_API bool rz_spaces_pop(RzSpaces *sp)
Definition: spaces.c:185
RZ_API void rz_spaces_purge(RzSpaces *sp)
Definition: spaces.c:70
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API bool sdb_foreach(Sdb *s, SdbForeachCallback cb, void *user)
Definition: sdb.c:758
#define KEY_SPACESTACK
static bool foreach_space_cb(void *user, const char *k, const char *v)
#define KEY_SPACES
#define KEY_NAME
struct rz_json_t::@304::@307 children
const char * str_value
Definition: rz_json.h:42
struct rz_json_t * next
Definition: rz_json.h:56
RzJsonType type
Definition: rz_json.h:39
char * name
Definition: rz_spaces.h:29
Definition: sdb.h:63

References rz_json_t::children, foreach_space_cb(), free(), KEY_NAME, KEY_SPACES, KEY_SPACESTACK, rz_space_t::name, rz_json_t::next, NULL, RZ_JSON_ARRAY, rz_json_free(), rz_json_parse(), RZ_JSON_STRING, rz_list_append(), RZ_SERIALIZE_ERR, rz_spaces_get(), rz_spaces_pop(), rz_spaces_purge(), sdb_foreach(), sdb_get(), sdb_ns(), spaces(), rz_json_t::str_value, and rz_json_t::type.

Referenced by rz_serialize_analysis_meta_load(), and rz_serialize_flag_load().

◆ rz_serialize_spaces_save()

RZ_API void rz_serialize_spaces_save ( RZ_NONNULL Sdb db,
RZ_NONNULL RzSpaces spaces 
)

Definition at line 22 of file serialize_spaces.c.

22  {
23  sdb_set(db, KEY_NAME, spaces->name, 0);
24 
25  PJ *j = pj_new();
26  if (!j) {
27  return;
28  }
29  pj_a(j);
31  char *spacename;
32  rz_list_foreach (spaces->spacestack, iter, spacename) {
33  pj_s(j, spacename);
34  }
35  pj_s(j, spaces->current ? spaces->current->name : "*"); // push current manually, will be popped on load
36  pj_end(j);
37  sdb_set(db, KEY_SPACESTACK, pj_string(j), 0);
38  pj_free(j);
39 
40  Sdb *db_spaces = sdb_ns(db, KEY_SPACES, true);
41  RBIter rbiter;
42  RzSpace *space;
43  rz_rbtree_foreach (spaces->spaces, rbiter, space, RzSpace, rb) {
44  sdb_set(db_spaces, space->name, "s", 0);
45  }
46 }
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_s(PJ *j, const char *k)
Definition: pj.c:197
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
#define rz_rbtree_foreach(root, it, data, struc, rb)
Definition: rz_rbtree.h:101
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
Definition: rz_pj.h:12

References KEY_NAME, KEY_SPACES, KEY_SPACESTACK, rz_space_t::name, pj_a(), pj_end(), pj_free(), pj_new(), pj_s(), pj_string(), rz_rbtree_foreach, sdb_ns(), sdb_set(), and spaces().

Referenced by rz_serialize_analysis_meta_save(), and rz_serialize_flag_save().