-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
project.c
102 lines (90 loc) · 3 KB
/
project.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* rizin - LGPL - Copyright 2020 - thestr4ng3r */
#include <rz_project.h>
#include "../util/serialize_helper.h"
#define RZ_DB_KEY_TYPE "type"
#define RZ_DB_KEY_VERSION "version"
#define RZ_DB_PROJECT_VERSION 1
#define RZ_DB_PROJECT_TYPE "rizin rz-db project"
RZ_API RZ_NONNULL const char *rz_project_err_message(RzProjectErr err) {
switch (err) {
case RZ_PROJECT_ERR_SUCCESS:
return "success";
case RZ_PROJECT_ERR_FILE:
return "file access error";
case RZ_PROJECT_ERR_INVALID_TYPE:
return "invalid file type";
case RZ_PROJECT_ERR_INVALID_VERSION:
return "invalid project version";
case RZ_PROJECT_ERR_NEWER_VERSION:
return "newer project version";
case RZ_PROJECT_ERR_INVALID_CONTENTS:
return "invalid content encountered";
case RZ_PROJECT_ERR_UNKNOWN:
break;
}
return "unknown error";
}
RZ_API RzProjectErr rz_project_save(RzCore *core, RzProject *prj, const char *file) {
sdb_set (prj, RZ_DB_KEY_TYPE, RZ_DB_PROJECT_TYPE, 0);
sdb_set (prj, RZ_DB_KEY_VERSION, sdb_fmt ("%u", RZ_DB_PROJECT_VERSION), 0);
rz_serialize_core_save (sdb_ns (prj, "core", true), core, file);
return RZ_PROJECT_ERR_SUCCESS;
}
RZ_API RzProjectErr rz_project_save_file(RzCore *core, const char *file) {
RzProject *prj = sdb_new0 ();
if (!prj) {
return RZ_PROJECT_ERR_UNKNOWN;
}
RzProjectErr err = rz_project_save (core, prj, file);
if (err != RZ_PROJECT_ERR_SUCCESS) {
sdb_free (prj);
return err;
}
if (!sdb_text_save (prj, file, true)) {
err = RZ_PROJECT_ERR_FILE;
}
sdb_free (prj);
if (err == RZ_PROJECT_ERR_SUCCESS) {
rz_config_set (core->config, "prj.file", file);
}
return err;
}
RZ_API RzProjectErr rz_project_load(RzCore *core, RzProject *prj, bool load_bin_io, RZ_NULLABLE const char *file, RzSerializeResultInfo *res) {
const char *type = sdb_const_get (prj, RZ_DB_KEY_TYPE, 0);
if (!type || strcmp (type, RZ_DB_PROJECT_TYPE) != 0) {
return RZ_PROJECT_ERR_INVALID_TYPE;
}
const char *version_str = sdb_const_get (prj, RZ_DB_KEY_VERSION, 0);
if (!version_str) {
return RZ_PROJECT_ERR_INVALID_VERSION;
}
unsigned long version = strtoul (version_str, NULL, 0);
if (!version || version == ULONG_MAX) {
return RZ_PROJECT_ERR_INVALID_VERSION;
} else if (version > RZ_DB_PROJECT_VERSION) {
return RZ_PROJECT_ERR_NEWER_VERSION;
}
Sdb *core_db = sdb_ns (prj, "core", false);
if (!core_db) {
SERIALIZE_ERR ("missing core namespace");
return RZ_PROJECT_ERR_INVALID_CONTENTS;
}
if (!rz_serialize_core_load (core_db, core, load_bin_io, file, res)) {
return RZ_PROJECT_ERR_INVALID_CONTENTS;
}
rz_config_set (core->config, "prj.file", file);
return RZ_PROJECT_ERR_SUCCESS;
}
RZ_API RzProjectErr rz_project_load_file(RzCore *core, const char *file, bool load_bin_io, RzSerializeResultInfo *res) {
RzProject *prj = sdb_new0 ();
if (!prj) {
return RZ_PROJECT_ERR_UNKNOWN;
}
if (!sdb_text_load (prj, file)) {
SERIALIZE_ERR ("failed to read database file");
return RZ_PROJECT_ERR_FILE;
}
RzProjectErr ret = rz_project_load (core, prj, load_bin_io, file, res);
sdb_free (prj);
return ret;
}