Rizin
unix-like reverse engineering framework and cli tools
rz_project.h File Reference
#include <rz_util/rz_serialize.h>
#include <rz_core.h>
#include <sdb.h>

Go to the source code of this file.

Macros

#define RZ_PROJECT_VERSION   8
 

Typedefs

typedef Sdb RzProject
 
typedef enum rz_project_err RzProjectErr
 

Enumerations

enum  rz_project_err {
  RZ_PROJECT_ERR_SUCCESS , RZ_PROJECT_ERR_FILE , RZ_PROJECT_ERR_INVALID_TYPE , RZ_PROJECT_ERR_INVALID_VERSION ,
  RZ_PROJECT_ERR_NEWER_VERSION , RZ_PROJECT_ERR_INVALID_CONTENTS , RZ_PROJECT_ERR_MIGRATION_FAILED , RZ_PROJECT_ERR_COMPRESSION_FAILED ,
  RZ_PROJECT_ERR_UNKNOWN
}
 

Functions

RZ_API RZ_NONNULL const char * rz_project_err_message (RzProjectErr err)
 
RZ_API RzProjectErr rz_project_save (RzCore *core, RzProject *prj, const char *file)
 
RZ_API RzProjectErr rz_project_save_file (RzCore *core, const char *file, bool compress)
 
RZ_API RzProjectrz_project_load_file_raw (const char *file)
 Load a file into an RzProject but don't actually migrate anything or load it into an RzCore. More...
 
RZ_API void rz_project_free (RzProject *prj)
 
RZ_API RzProjectErr rz_project_load (RzCore *core, RzProject *prj, bool load_bin_io, RZ_NULLABLE const char *file, RzSerializeResultInfo *res)
 
RZ_API RzProjectErr rz_project_load_file (RzCore *core, const char *file, bool load_bin_io, RzSerializeResultInfo *res)
 
RZ_API bool rz_project_migrate_v1_v2 (RzProject *prj, RzSerializeResultInfo *res)
 
RZ_API bool rz_project_migrate_v2_v3 (RzProject *prj, RzSerializeResultInfo *res)
 
RZ_API bool rz_project_migrate_v3_v4 (RzProject *prj, RzSerializeResultInfo *res)
 
RZ_API bool rz_project_migrate (RzProject *prj, unsigned long version, RzSerializeResultInfo *res)
 Migrate the given project to the current version in-place. More...
 

Macro Definition Documentation

◆ RZ_PROJECT_VERSION

#define RZ_PROJECT_VERSION   8

Definition at line 15 of file rz_project.h.

Typedef Documentation

◆ RzProject

typedef Sdb RzProject

Definition at line 17 of file rz_project.h.

◆ RzProjectErr

Enumeration Type Documentation

◆ rz_project_err

Enumerator
RZ_PROJECT_ERR_SUCCESS 
RZ_PROJECT_ERR_FILE 
RZ_PROJECT_ERR_INVALID_TYPE 
RZ_PROJECT_ERR_INVALID_VERSION 
RZ_PROJECT_ERR_NEWER_VERSION 
RZ_PROJECT_ERR_INVALID_CONTENTS 
RZ_PROJECT_ERR_MIGRATION_FAILED 
RZ_PROJECT_ERR_COMPRESSION_FAILED 
RZ_PROJECT_ERR_UNKNOWN 

Definition at line 19 of file rz_project.h.

19  {
29 } RzProjectErr;
@ RZ_PROJECT_ERR_MIGRATION_FAILED
Definition: rz_project.h:26
@ RZ_PROJECT_ERR_COMPRESSION_FAILED
Definition: rz_project.h:27
@ RZ_PROJECT_ERR_NEWER_VERSION
Definition: rz_project.h:24
@ RZ_PROJECT_ERR_SUCCESS
Definition: rz_project.h:20
@ RZ_PROJECT_ERR_INVALID_CONTENTS
Definition: rz_project.h:25
@ RZ_PROJECT_ERR_INVALID_TYPE
Definition: rz_project.h:22
@ RZ_PROJECT_ERR_FILE
Definition: rz_project.h:21
@ RZ_PROJECT_ERR_INVALID_VERSION
Definition: rz_project.h:23
@ RZ_PROJECT_ERR_UNKNOWN
Definition: rz_project.h:28
enum rz_project_err RzProjectErr

Function Documentation

◆ rz_project_err_message()

RZ_API RZ_NONNULL const char* rz_project_err_message ( RzProjectErr  err)

Definition at line 11 of file project.c.

11  {
12  switch (err) {
14  return "success";
16  return "file access error";
18  return "invalid file type";
20  return "invalid project version";
22  return "newer project version";
24  return "invalid content encountered";
26  return "migration failed";
28  return "project file compression failed";
30  break;
31  }
32  return "unknown error";
33 }
static bool err
Definition: armass.c:435

References err, RZ_PROJECT_ERR_COMPRESSION_FAILED, RZ_PROJECT_ERR_FILE, RZ_PROJECT_ERR_INVALID_CONTENTS, RZ_PROJECT_ERR_INVALID_TYPE, RZ_PROJECT_ERR_INVALID_VERSION, RZ_PROJECT_ERR_MIGRATION_FAILED, RZ_PROJECT_ERR_NEWER_VERSION, RZ_PROJECT_ERR_SUCCESS, and RZ_PROJECT_ERR_UNKNOWN.

Referenced by rz_core_project_load_for_cli(), rz_main_rizin(), and rz_project_save_handler().

◆ rz_project_free()

RZ_API void rz_project_free ( RzProject prj)

Definition at line 129 of file project.c.

129  {
130  sdb_free(prj);
131 }
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206

References sdb_free().

◆ rz_project_load()

RZ_API RzProjectErr rz_project_load ( RzCore core,
RzProject prj,
bool  load_bin_io,
RZ_NULLABLE const char *  file,
RzSerializeResultInfo res 
)
Parameters
load_bin_iowhether to also load the underlying RIO and RBin state from the project. If false, the current state will be kept and the project loaded on top.
filefilename of the project that db comes from. This is only used to re-locate the loaded RIO descs, the project file itself is not touched by this function.

Definition at line 133 of file project.c.

133  {
135  const char *type = sdb_const_get(prj, RZ_PROJECT_KEY_TYPE, 0);
136  if (!type || strcmp(type, RZ_PROJECT_TYPE) != 0) {
138  }
139  const char *version_str = sdb_const_get(prj, RZ_PROJECT_KEY_VERSION, 0);
140  if (!version_str) {
142  }
143  unsigned long version = strtoul(version_str, NULL, 0);
144  if (!version || version == ULONG_MAX) {
146  }
147  if (version > RZ_PROJECT_VERSION) {
149  }
150  if (!rz_project_migrate(prj, version, res)) {
152  }
153 
154  Sdb *core_db = sdb_ns(prj, "core", false);
155  if (!core_db) {
156  RZ_SERIALIZE_ERR(res, "missing core namespace");
158  }
159  if (!rz_serialize_core_load(core_db, core, load_bin_io, file, res)) {
161  }
162 
163  rz_config_set(core->config, "prj.file", file);
164 
165  return RZ_PROJECT_ERR_SUCCESS;
166 }
RZ_API RzConfigNode * rz_config_set(RzConfig *cfg, RZ_NONNULL const char *name, const char *value)
Definition: config.c:267
#define NULL
Definition: cris-opc.c:27
int type
Definition: mipsasm.c:17
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
#define RZ_PROJECT_KEY_VERSION
Definition: project.c:7
#define RZ_PROJECT_KEY_TYPE
Definition: project.c:6
#define RZ_PROJECT_TYPE
Definition: project.c:9
RZ_API bool rz_project_migrate(RzProject *prj, unsigned long version, RzSerializeResultInfo *res)
Migrate the given project to the current version in-place.
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_PROJECT_VERSION
Definition: rz_project.h:15
#define RZ_SERIALIZE_ERR(res,...)
Push an error to the local RzSerializeResultInfo \res RzSerializeInfoResult *.
Definition: rz_serialize.h:33
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
RZ_API bool rz_serialize_core_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, bool load_bin_io, RZ_NULLABLE const char *prj_file, RZ_NULLABLE RzSerializeResultInfo *res)
Definition: gzappend.c:170
RzConfig * config
Definition: rz_core.h:300
Definition: sdb.h:63

References rz_core_t::config, NULL, rz_config_set(), RZ_PROJECT_ERR_INVALID_CONTENTS, RZ_PROJECT_ERR_INVALID_TYPE, RZ_PROJECT_ERR_INVALID_VERSION, RZ_PROJECT_ERR_MIGRATION_FAILED, RZ_PROJECT_ERR_NEWER_VERSION, RZ_PROJECT_ERR_SUCCESS, RZ_PROJECT_ERR_UNKNOWN, RZ_PROJECT_KEY_TYPE, RZ_PROJECT_KEY_VERSION, rz_project_migrate(), RZ_PROJECT_TYPE, RZ_PROJECT_VERSION, rz_return_val_if_fail, rz_serialize_core_load(), RZ_SERIALIZE_ERR, sdb_const_get(), sdb_ns(), and type.

Referenced by rz_project_load_file().

◆ rz_project_load_file()

RZ_API RzProjectErr rz_project_load_file ( RzCore core,
const char *  file,
bool  load_bin_io,
RzSerializeResultInfo res 
)
Parameters
load_bin_iowhether to also load the underlying RIO and RBin state from the project. If false, the current state will be kept and the project loaded on top.
filefilename of the project to load from

Definition at line 168 of file project.c.

168  {
170  if (!prj) {
171  RZ_SERIALIZE_ERR(res, "failed to read database file");
172  return RZ_PROJECT_ERR_FILE;
173  }
174  RzProjectErr ret = rz_project_load(core, prj, load_bin_io, file, res);
175  sdb_free(prj);
176  return ret;
177 }
RZ_API RzProjectErr rz_project_load(RzCore *core, RzProject *prj, bool load_bin_io, RZ_NULLABLE const char *file, RzSerializeResultInfo *res)
Definition: project.c:133
RZ_API RzProject * rz_project_load_file_raw(const char *file)
Load a file into an RzProject but don't actually migrate anything or load it into an RzCore.
Definition: project.c:88

References RZ_PROJECT_ERR_FILE, rz_project_load(), rz_project_load_file_raw(), RZ_SERIALIZE_ERR, and sdb_free().

Referenced by rz_core_project_load_for_cli().

◆ rz_project_load_file_raw()

RZ_API RzProject* rz_project_load_file_raw ( const char *  file)

Load a file into an RzProject but don't actually migrate anything or load it into an RzCore.

Definition at line 88 of file project.c.

88  {
89  RzProject *prj = sdb_new0();
90  if (!prj) {
91  return NULL;
92  }
93 
94  char *tmp_file;
95  int mkstemp_fd = rz_file_mkstemp("ldprj", &tmp_file);
96  close(mkstemp_fd);
97 
98  if (mkstemp_fd == -1 || !tmp_file) {
99  free(tmp_file);
100  return NULL;
101  }
102 
103  const char *load_file = tmp_file;
104 
105  if (!rz_file_exists(file)) {
106  prj = NULL;
107  goto return_goto;
108  }
109  if (rz_file_is_deflated(file)) {
110  if (!rz_file_inflate(file, tmp_file)) {
111  prj = NULL;
112  goto return_goto;
113  }
114  } else {
115  load_file = file;
116  }
117 
118  if (!sdb_text_load(prj, load_file)) {
119  sdb_free(prj);
120  prj = NULL;
121  }
122 
123 return_goto:
124  rz_file_rm(tmp_file);
125  free(tmp_file);
126  return prj;
127 }
static static fork const void static count close
Definition: sflib.h:33
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API bool rz_file_is_deflated(RZ_NONNULL const char *src)
check whether a file is a deflated (gzip) file
Definition: file.c:1392
RZ_API bool rz_file_inflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst)
unzip the contents of src and store in dst
Definition: file.c:1361
RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname)
Definition: file.c:1058
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43
RZ_API bool sdb_text_load(Sdb *s, const char *file)
Definition: text.c:405
static int file
Definition: z80asm.c:58

References close, file, free(), NULL, rz_file_exists(), rz_file_inflate(), rz_file_is_deflated(), rz_file_mkstemp(), rz_file_rm(), sdb_free(), sdb_new0(), and sdb_text_load().

Referenced by rz_project_load_file().

◆ rz_project_migrate()

RZ_API bool rz_project_migrate ( RzProject prj,
unsigned long  version,
RzSerializeResultInfo res 
)

Migrate the given project to the current version in-place.

Definition at line 318 of file project_migrate.c.

318  {
320  while (version < RZ_PROJECT_VERSION) {
321  bool succ = migrations[version - 1](prj, res);
322  if (!succ) {
323  rz_list_push(res, rz_str_newf("project migration from version %lu to %lu failed.", version, version + 1));
324  return false;
325  }
326  rz_list_push(res, rz_str_newf("project migrated from version %lu to %lu.", version, version + 1));
327  version++;
328  }
329  return true;
330 }
RZ_API RZ_BORROW RzListIter * rz_list_push(RZ_NONNULL RzList *list, void *item)
Alias for rz_list_append.
Definition: list.c:60
static bool(*const migrations[])(RzProject *prj, RzSerializeResultInfo *res)
#define RZ_STATIC_ASSERT(x)
Definition: rz_assert.h:10
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300

References migrations, RZ_ARRAY_SIZE, rz_list_push(), RZ_PROJECT_VERSION, RZ_STATIC_ASSERT, and rz_str_newf().

Referenced by rz_project_load().

◆ rz_project_migrate_v1_v2()

RZ_API bool rz_project_migrate_v1_v2 ( RzProject prj,
RzSerializeResultInfo res 
)

Definition at line 53 of file project_migrate.c.

53  {
54  Sdb *core_db;
55  RZ_SERIALIZE_SUB(prj, core_db, res, "core", return false;);
56  Sdb *analysis_db;
57  RZ_SERIALIZE_SUB(core_db, analysis_db, res, "analysis", return false;);
58  Sdb *types_db;
59  RZ_SERIALIZE_SUB(analysis_db, types_db, res, "types", return false;);
60  V1V2TypesCtx ctx = {
61  .moved_keys = rz_list_newf(free),
62  .noreturn_db = sdb_ns(analysis_db, "noreturn", true)
63  };
64  if (!ctx.moved_keys || !ctx.noreturn_db) {
65  return false;
66  }
68  RzListIter *it;
69  char *s;
70  rz_list_foreach (ctx.moved_keys, it, s) {
71  sdb_unset(types_db, s, 0);
72  }
73  rz_list_free(ctx.moved_keys);
74  return true;
75 }
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
bool v1_v2_types_foreach_cb(void *user, const char *k, const char *v)
static RzSocket * s
Definition: rtr.c:28
#define RZ_SERIALIZE_SUB(db, subdb, res, ns, rip)
Get an sdb sub-namespace or fail.
Definition: rz_serialize.h:104
RZ_API bool sdb_foreach(Sdb *s, SdbForeachCallback cb, void *user)
Definition: sdb.c:758
RZ_API int sdb_unset(Sdb *s, const char *key, ut32 cas)
Definition: sdb.c:294

References free(), rz_list_free(), rz_list_newf(), RZ_SERIALIZE_SUB, s, sdb_foreach(), sdb_ns(), sdb_unset(), and v1_v2_types_foreach_cb().

◆ rz_project_migrate_v2_v3()

RZ_API bool rz_project_migrate_v2_v3 ( RzProject prj,
RzSerializeResultInfo res 
)

Definition at line 110 of file project_migrate.c.

110  {
111  Sdb *core_db;
112  RZ_SERIALIZE_SUB(prj, core_db, res, "core", return false;);
113  Sdb *analysis_db;
114  RZ_SERIALIZE_SUB(core_db, analysis_db, res, "analysis", return false;);
115  Sdb *types_db;
116  RZ_SERIALIZE_SUB(analysis_db, types_db, res, "types", return false;);
117  V2V3TypesCtx ctx = {
118  .moved_keys = rz_list_newf(free),
119  .callables_db = sdb_ns(analysis_db, "callables", true),
120  .typelinks_db = sdb_ns(analysis_db, "typelinks", true)
121  };
122  if (!ctx.moved_keys || !ctx.callables_db || !ctx.typelinks_db) {
123  return false;
124  }
126  RzListIter *it;
127  char *s;
128  rz_list_foreach (ctx.moved_keys, it, s) {
129  sdb_unset(types_db, s, 0);
130  }
131  rz_list_free(ctx.moved_keys);
132  return true;
133 }
bool v2_v3_types_foreach_cb(void *user, const char *k, const char *v)

References free(), rz_list_free(), rz_list_newf(), RZ_SERIALIZE_SUB, s, sdb_foreach(), sdb_ns(), sdb_unset(), and v2_v3_types_foreach_cb().

◆ rz_project_migrate_v3_v4()

RZ_API bool rz_project_migrate_v3_v4 ( RzProject prj,
RzSerializeResultInfo res 
)

Definition at line 166 of file project_migrate.c.

166  {
167  Sdb *core_db;
168  RZ_SERIALIZE_SUB(prj, core_db, res, "core", return false;);
169  Sdb *analysis_db;
170  RZ_SERIALIZE_SUB(core_db, analysis_db, res, "analysis", return false;);
171  // Kill me in the future
172  sdb_ns(analysis_db, "vars", true);
173 #if 0
174  V3V4TypesCtx ctx = {
175  .moved_keys = rz_list_newf(free),
176  .global_vars_db = sdb_ns(analysis_db, "vars", true)
177  };
178 
179  if (!ctx.moved_keys || !ctx.global_vars_db) {
180  return false;
181  }
182  Sdb *typelinks_db = sdb_ns(analysis_db, "typelinks", true);
183  sdb_foreach(typelinks_db, v3_v4_types_foreach_cb, &ctx);
184  RzListIter *it;
185  char *s;
186  rz_list_foreach (ctx.moved_keys, it, s) {
187  sdb_unset(typelinks_db, s, 0);
188  }
189  rz_list_free(ctx.moved_keys);
190 #endif
191  return true;
192 }

References free(), rz_list_free(), rz_list_newf(), RZ_SERIALIZE_SUB, s, sdb_foreach(), sdb_ns(), and sdb_unset().

◆ rz_project_save()

RZ_API RzProjectErr rz_project_save ( RzCore core,
RzProject prj,
const char *  file 
)

Definition at line 35 of file project.c.

35  {
38  rz_serialize_core_save(sdb_ns(prj, "core", true), core, file);
40 }
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
RZ_API void rz_serialize_core_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file)

References RZ_PROJECT_ERR_SUCCESS, RZ_PROJECT_KEY_TYPE, RZ_PROJECT_KEY_VERSION, RZ_PROJECT_TYPE, RZ_PROJECT_VERSION, rz_serialize_core_save(), sdb_fmt(), sdb_ns(), and sdb_set().

Referenced by rz_project_save_file().

◆ rz_project_save_file()

RZ_API RzProjectErr rz_project_save_file ( RzCore core,
const char *  file,
bool  compress 
)

Definition at line 42 of file project.c.

42  {
43  char *tmp_file = NULL;
44 
45  if (compress) {
46  int mkstemp_fd = rz_file_mkstemp("svprj", &tmp_file);
47  if (mkstemp_fd == -1 || !tmp_file) {
48  return RZ_PROJECT_ERR_FILE;
49  }
50  close(mkstemp_fd);
51  }
52 
54  const char *save_file = compress ? tmp_file : file;
55  RzProject *prj = sdb_new0();
56  if (!prj) {
58  goto tmp_file_err;
59  }
60  err = rz_project_save(core, prj, file);
61  if (err != RZ_PROJECT_ERR_SUCCESS) {
62  sdb_free(prj);
63  return err;
64  }
65  if (!sdb_text_save(prj, save_file, true)) {
67  }
68  sdb_free(prj);
69 
70  if (err != RZ_PROJECT_ERR_SUCCESS) {
71  goto tmp_file_err;
72  }
73 
74  if (compress && !rz_file_deflate(tmp_file, file)) {
76  goto tmp_file_err;
77  }
78 
79  rz_config_set(core->config, "prj.file", file);
80 
81 tmp_file_err:
82  rz_file_rm(tmp_file);
83  free(tmp_file);
84  return err;
85 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
RZ_API RzProjectErr rz_project_save(RzCore *core, RzProject *prj, const char *file)
Definition: project.c:35
RZ_API bool rz_file_deflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst)
zip the contents of src and store in dst
Definition: file.c:1329
RZ_API bool sdb_text_save(Sdb *s, const char *file, bool sort)
Definition: text.c:199

References close, compress(), rz_core_t::config, err, file, free(), NULL, rz_config_set(), rz_file_deflate(), rz_file_mkstemp(), rz_file_rm(), RZ_PROJECT_ERR_COMPRESSION_FAILED, RZ_PROJECT_ERR_FILE, RZ_PROJECT_ERR_SUCCESS, RZ_PROJECT_ERR_UNKNOWN, rz_project_save(), sdb_free(), sdb_new0(), and sdb_text_save().

Referenced by rz_main_rizin(), and rz_project_save_handler().