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

Go to the source code of this file.

Macros

#define SUB(ns, call)   RZ_SERIALIZE_SUB_DO(db, subdb, res, ns, call, return false;)
 

Enumerations

enum  FileRet { FILE_SUCCESS , FILE_DOES_NOT_EXIST , FILE_LOAD_FAIL }
 

Functions

static void file_save (RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file)
 
static bool file_load (RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file, RZ_NULLABLE RzSerializeResultInfo *res)
 
RZ_API void rz_serialize_core_save (RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file)
 
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)
 
static char * prj_dir_abs (const char *prj_file)
 
static char * prj_relative_make (const char *prj_dir, const char *abs_file)
 
static char * prj_relative_restore (const char *prj_dir, const char *rel_file)
 
static FileRet try_load_file (RZ_NONNULL RzCore *core, const char *file, RZ_NULLABLE RzSerializeResultInfo *res)
 

Variables

static const char *const config_exclude []
 

Macro Definition Documentation

◆ SUB

#define SUB (   ns,
  call 
)    RZ_SERIALIZE_SUB_DO(db, subdb, res, ns, call, return false;)

Enumeration Type Documentation

◆ FileRet

enum FileRet
Enumerator
FILE_SUCCESS 
FILE_DOES_NOT_EXIST 
FILE_LOAD_FAIL 

Definition at line 196 of file serialize_core.c.

196  {
197  FILE_SUCCESS,
200 } FileRet;
FileRet
@ FILE_SUCCESS
@ FILE_DOES_NOT_EXIST
@ FILE_LOAD_FAIL

Function Documentation

◆ file_load()

static bool file_load ( RZ_NONNULL Sdb db,
RZ_NONNULL RzCore core,
RZ_NULLABLE const char *  prj_file,
RZ_NULLABLE RzSerializeResultInfo res 
)
static

Definition at line 217 of file serialize_core.c.

218  {
219  rz_core_file_close_fd(core, -1);
220  rz_io_close_all(core->io);
221  rz_bin_file_delete_all(core->bin);
222 
224  const char *rel = sdb_const_get(db, "relative", 0);
225  if (rel && prj_file) {
226  char *prj_dir = prj_dir_abs(prj_file);
227  if (prj_dir) {
228  char *file = prj_relative_restore(prj_dir, rel);
229  if (file) {
230  r = try_load_file(core, file, res);
231  free(file);
232  }
233  free(prj_dir);
234  }
235  }
236  if (r != FILE_DOES_NOT_EXIST) {
237  return r == FILE_SUCCESS;
238  }
239 
240  const char *file = sdb_const_get(db, "absolute", 0);
241  if (file) {
242  r = try_load_file(core, file, res);
243  }
244  if (r != FILE_DOES_NOT_EXIST) {
245  return r == FILE_SUCCESS;
246  }
247 
248  file = sdb_const_get(db, "raw", 0);
249  if (file) {
250  r = try_load_file(core, file, res);
251  }
252  if (r != FILE_DOES_NOT_EXIST) {
253  return r == FILE_SUCCESS;
254  }
255 
256  RZ_SERIALIZE_ERR(res, "failed to re-locate file referenced by project");
257  return false;
258 }
RZ_API ut64 rz_bin_file_delete_all(RzBin *bin)
Definition: bfile.c:199
RZ_API bool rz_core_file_close_fd(RzCore *core, int fd)
Definition: cfile.c:1510
#define r
Definition: crypto_rc6.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_io_close_all(RzIO *io)
Definition: io.c:258
#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
static char * prj_dir_abs(const char *prj_file)
static char * prj_relative_restore(const char *prj_dir, const char *rel_file)
static FileRet try_load_file(RZ_NONNULL RzCore *core, const char *file, RZ_NULLABLE RzSerializeResultInfo *res)
Definition: gzappend.c:170

References FILE_DOES_NOT_EXIST, FILE_SUCCESS, free(), prj_dir_abs(), prj_relative_restore(), r, rz_bin_file_delete_all(), rz_core_file_close_fd(), rz_io_close_all(), RZ_SERIALIZE_ERR, sdb_const_get(), and try_load_file().

Referenced by rz_serialize_core_load().

◆ file_save()

static void file_save ( RZ_NONNULL Sdb db,
RZ_NONNULL RzCore core,
RZ_NULLABLE const char *  prj_file 
)
static

Definition at line 154 of file serialize_core.c.

154  {
155  rz_return_if_fail(db && core);
156 
157  if (!core->file) {
158  return;
159  }
160  RzIODesc *desc = rz_io_desc_get(core->io, core->file->fd);
161  if (!desc) {
162  return;
163  }
164  if (!desc->plugin || strcmp(desc->plugin->name, "default")) {
165  eprintf("Warning: The current file is not loaded as a regular file. "
166  "This is not supported in projects yet and it will be necessary to manually re-load to use the project.\n");
167  return;
168  }
169  const char *filename = desc->uri;
170  if (!filename) {
171  return;
172  }
173  sdb_set(db, "raw", filename, 0);
174  char *abs = rz_file_abspath(filename);
175  if (!abs) {
176  return;
177  }
178  sdb_set(db, "absolute", abs, 0);
179  if (prj_file) {
180  char *prj_dir = prj_dir_abs(prj_file);
181  if (!prj_dir) {
182  goto beach;
183  }
184  char *rel = prj_relative_make(prj_dir, abs);
185  if (rel) {
186  sdb_set(db, "relative", rel, 0);
187  free(rel);
188  }
189  free(prj_dir);
190  }
191 beach:
192  free(abs);
193  return;
194 }
const char * desc
Definition: bin_vsf.c:19
const char * filename
Definition: ioapi.h:137
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API char * rz_file_abspath(const char *file)
Definition: file.c:267
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
static char * prj_relative_make(const char *prj_dir, const char *abs_file)

References desc, eprintf, free(), prj_dir_abs(), prj_relative_make(), rz_file_abspath(), rz_io_desc_get(), rz_return_if_fail, and sdb_set().

Referenced by rz_serialize_core_save().

◆ prj_dir_abs()

static char* prj_dir_abs ( const char *  prj_file)
static

Definition at line 120 of file serialize_core.c.

120  {
121  char *prj_abs = rz_file_abspath(prj_file);
122  if (!prj_abs) {
123  return NULL;
124  }
125  char *r = rz_file_dirname(prj_abs);
126  free(prj_abs);
127  return r;
128 }
#define NULL
Definition: cris-opc.c:27
RZ_API char * rz_file_dirname(const char *path)
Definition: file.c:120

References free(), NULL, r, rz_file_abspath(), and rz_file_dirname().

Referenced by file_load(), and file_save().

◆ prj_relative_make()

static char* prj_relative_make ( const char *  prj_dir,
const char *  abs_file 
)
static

Definition at line 131 of file serialize_core.c.

131  {
132  char *rel = rz_file_relpath(prj_dir, abs_file);
133  if (!rel) {
134  return NULL;
135  }
136  // convert only the relative path to a common format because it is the only path that makes any sense
137  // when a project file is relocated to another machine. Absolute and raw are always specific to one environment.
138  char *rel_unix = rz_file_path_local_to_unix(rel);
139  free(rel);
140  return rel_unix;
141 }
RZ_API char * rz_file_relpath(const char *base, const char *path)
Definition: file.c:278
RZ_API char * rz_file_path_local_to_unix(const char *path)
Definition: file.c:332

References free(), NULL, rz_file_path_local_to_unix(), and rz_file_relpath().

Referenced by file_save().

◆ prj_relative_restore()

static char* prj_relative_restore ( const char *  prj_dir,
const char *  rel_file 
)
static

Definition at line 144 of file serialize_core.c.

144  {
145  char *rel_local = rz_file_path_unix_to_local(rel_file);
146  if (!rel_local) {
147  return NULL;
148  }
149  char *abs = rz_file_abspath_rel(prj_dir, rel_local);
150  free(rel_local);
151  return abs;
152 }
RZ_API char * rz_file_abspath_rel(const char *cwd, const char *file)
Definition: file.c:219
RZ_API char * rz_file_path_unix_to_local(const char *path)
Definition: file.c:343

References free(), NULL, rz_file_abspath_rel(), and rz_file_path_unix_to_local().

Referenced by file_load().

◆ rz_serialize_core_load()

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 at line 69 of file serialize_core.c.

70  {
71  Sdb *subdb;
72 
73 #define SUB(ns, call) RZ_SERIALIZE_SUB_DO(db, subdb, res, ns, call, return false;)
74 
75  if (load_bin_io) {
76  SUB("file", file_load(subdb, core, prj_file, res));
77  }
78  SUB("config", rz_serialize_config_load(subdb, core->config, config_exclude, res));
79  SUB("flags", rz_serialize_flag_load(subdb, core->flags, res));
80  SUB("analysis", rz_serialize_analysis_load(subdb, core->analysis, res));
81  SUB("debug", rz_serialize_debug_load(subdb, core->dbg, res));
82 
83  const char *str = sdb_get(db, "offset", 0);
84  if (!str || !*str) {
85  RZ_SERIALIZE_ERR(res, "missing offset in core");
86  return false;
87  }
88  core->offset = strtoull(str, NULL, 0);
89 
90  str = sdb_get(db, "blocksize", 0);
91  if (!str || !*str) {
92  RZ_SERIALIZE_ERR(res, "missing blocksize in core");
93  return false;
94  }
95  ut64 bs = strtoull(str, NULL, 0);
96  rz_core_block_size(core, (int)bs);
97 
98  // handled by config already:
99  // cfglog, cmdrepeat, cmdtimes
100 
101  return true;
102 }
RZ_API bool rz_core_block_size(RzCore *core, ut32 bsize)
Definition: core.c:2842
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API bool rz_serialize_analysis_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzAnalysis *analysis, RZ_NULLABLE RzSerializeResultInfo *res)
RZ_API bool rz_serialize_config_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzConfig *config, RZ_NULLABLE const char *const *exclude, RZ_NULLABLE RzSerializeResultInfo *res)
static bool file_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file, RZ_NULLABLE RzSerializeResultInfo *res)
static const char *const config_exclude[]
#define SUB(ns, call)
RZ_API bool rz_serialize_debug_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzDebug *dbg, RZ_NULLABLE RzSerializeResultInfo *res)
Load a serialized debug state to a RzDebug instance.
RZ_API bool rz_serialize_flag_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzFlag *flag, RZ_NULLABLE RzSerializeResultInfo *res)
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References config_exclude, file_load(), NULL, rz_core_block_size(), rz_serialize_analysis_load(), rz_serialize_config_load(), rz_serialize_debug_load(), RZ_SERIALIZE_ERR, rz_serialize_flag_load(), sdb_get(), cmd_descs_generate::str, SUB, and ut64().

Referenced by rz_project_load().

◆ rz_serialize_core_save()

RZ_API void rz_serialize_core_save ( RZ_NONNULL Sdb db,
RZ_NONNULL RzCore core,
RZ_NULLABLE const char *  prj_file 
)

Definition at line 23 of file serialize_core.c.

23  {
24  file_save(sdb_ns(db, "file", true), core, prj_file);
25  rz_serialize_config_save(sdb_ns(db, "config", true), core->config);
26  rz_serialize_flag_save(sdb_ns(db, "flags", true), core->flags);
27  rz_serialize_analysis_save(sdb_ns(db, "analysis", true), core->analysis);
28  rz_serialize_debug_save(sdb_ns(db, "debug", true), core->dbg);
29 
30  char buf[0x20];
31  if (snprintf(buf, sizeof(buf), "0x%" PFMT64x, core->offset) < 0) {
32  return;
33  }
34  sdb_set(db, "offset", buf, 0);
35 
36  if (snprintf(buf, sizeof(buf), "0x%" PFMT32x, core->blocksize) < 0) {
37  return;
38  }
39  sdb_set(db, "blocksize", buf, 0);
40 }
#define PFMT32x
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
#define PFMT64x
Definition: rz_types.h:393
RZ_API void rz_serialize_analysis_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzAnalysis *analysis)
RZ_API void rz_serialize_config_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzConfig *config)
static void file_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzCore *core, RZ_NULLABLE const char *prj_file)
RZ_API void rz_serialize_debug_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzDebug *dbg)
Serialize debug state (RzDebug) and save to a sdb.
RZ_API void rz_serialize_flag_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzFlag *flag)

References file_save(), PFMT32x, PFMT64x, rz_serialize_analysis_save(), rz_serialize_config_save(), rz_serialize_debug_save(), rz_serialize_flag_save(), sdb_ns(), sdb_set(), and snprintf.

Referenced by rz_project_save().

◆ try_load_file()

static FileRet try_load_file ( RZ_NONNULL RzCore core,
const char *  file,
RZ_NULLABLE RzSerializeResultInfo res 
)
static

Definition at line 202 of file serialize_core.c.

202  {
203  if (!rz_file_is_regular(file)) {
204  return FILE_DOES_NOT_EXIST;
205  }
206 
208  if (!fh) {
209  RZ_SERIALIZE_ERR(res, "failed re-open file \"%s\" referenced by project", file);
210  return FILE_LOAD_FAIL;
211  }
213 
214  return FILE_SUCCESS;
215 }
FILE * fh
Definition: cabinfo.c:52
RZ_API RZ_BORROW RzCoreFile * rz_core_file_open(RZ_NONNULL RzCore *r, RZ_NONNULL const char *file, int flags, ut64 loadaddr)
Tries to open the file as is, otherwise tries as is a compilation of files.
Definition: cfile.c:1182
RZ_API bool rz_core_bin_load(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *filenameuri, ut64 baddr)
Definition: cfile.c:942
RZ_API bool rz_file_is_regular(const char *str)
Definition: file.c:159
#define RZ_PERM_RX
Definition: rz_types.h:97
#define UT64_MAX
Definition: rz_types_base.h:86

References fh, FILE_DOES_NOT_EXIST, FILE_LOAD_FAIL, FILE_SUCCESS, rz_core_bin_load(), rz_core_file_open(), rz_file_is_regular(), RZ_PERM_RX, RZ_SERIALIZE_ERR, and UT64_MAX.

Referenced by file_load().

Variable Documentation

◆ config_exclude

const char* const config_exclude[]
static
Initial value:
= {
"dir.home",
"dir.libs",
"dir.magic",
"dir.plugins",
"dir.prefix",
"dir.projects",
"dir.source",
"dir.tmp",
"dir.types",
"http.root",
"pdb.symstore",
"scr.color",
"scr.color.args",
"scr.color.bytes",
"scr.color.grep",
"scr.color.ops",
"scr.color.pipe",
"scr.interactive",
"scr.prompt",
"scr.rainbow",
"scr.utf8",
"scr.utf8.curvy",
"ghidra.sleighhome",
}

Definition at line 42 of file serialize_core.c.

Referenced by rz_serialize_core_load().