Rizin
unix-like reverse engineering framework and cli tools
platform_target_index.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_platform.h>
5 #include <string.h>
6 
12  if (!item) {
13  return NULL;
14  }
15  item->name = name ? strdup(name) : NULL;
16  item->comment = NULL;
17  return item;
18 }
19 
25  if (!target) {
26  return NULL;
27  }
28  target->platforms = ht_up_new0();
29  if (!target->platforms) {
30  free(target);
31  return NULL;
32  }
33  return target;
34 }
35 
40  if (!target) {
41  return;
42  }
43  ht_up_free(target->platforms);
44  free(target->path);
45  free(target);
46 }
47 
52  if (!item) {
53  return;
54  }
55  free(item->name);
56  free(item->comment);
57  free(item);
58 }
59 
61  rz_return_val_if_fail(t && sdb, false);
62  SdbKv *kv;
64  SdbList *l = sdb_foreach_list(sdb, false);
65  char *argument_key, *comment, *name;
66  ls_foreach (l, iter, kv) {
67  if (!strcmp(sdbkv_value(kv), "name")) {
68  name = sdbkv_key(kv);
69 
71 
72  argument_key = rz_str_newf("%s.address", item->name);
73  if (!argument_key) {
75  return false;
76  }
77  ut64 address = sdb_num_get(sdb, argument_key, NULL);
78  if (!address) {
80  return false;
81  }
82 
83  argument_key = rz_str_newf("%s.comment", item->name);
84  comment = sdb_get(sdb, argument_key, NULL);
85  if (comment) {
86  item->comment = comment;
87  }
88  ht_up_insert(t->platforms, address, item);
89  }
90  }
91  return true;
92 }
93 
95  rz_return_val_if_fail(t && path, false);
96  if (!path) {
97  return false;
98  }
99  Sdb *db = sdb_new(0, path, 0);
100  if (!db) {
101  return false;
102  }
103  bool result = sdb_load_platform_profile(t, db);
104  sdb_close(db);
105  sdb_free(db);
106  return result;
107 }
108 
116  rz_return_val_if_fail(t && path, false);
117  if (!path) {
118  return false;
119  }
120  if (!rz_file_exists(path)) {
121  return false;
122  }
124 }
125 
135  const char *platform, RZ_NONNULL const char *platforms_dir) {
136  if (RZ_STR_ISEMPTY(platform)) {
137  return true;
138  }
139  rz_return_val_if_fail(arch && cpu && platforms_dir, false);
140 
141  char buf[50];
142  char *path = rz_file_path_join(platforms_dir, rz_strf(buf, "%s-%s-%s.sdb", arch, cpu, platform));
143  if (!path) {
144  return false;
145  }
146  if (t->path && !strcmp(t->path, path)) {
147  free(path);
148  return true;
149  }
150  free(t->path);
151  t->path = path;
153 }
static ut32 cpu[32]
Definition: analysis_or1k.c:21
#define RZ_API
#define NULL
Definition: cris-opc.c:27
cs_arch arch
Definition: cstool.c:13
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
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")
#define ls_foreach(list, it, pos)
Definition: ls.h:31
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13
const char * name
Definition: op.c:541
RZ_API bool rz_platform_target_index_init(RzPlatformTargetIndex *t, RZ_NONNULL const char *arch, RZ_NONNULL const char *cpu, const char *platform, RZ_NONNULL const char *platforms_dir)
Initialize Platform Profiles by setting the path to the corresponding SDB file.
RZ_API void rz_platform_item_free(RzPlatformItem *item)
Frees an RzPlatformItem type.
RZ_API bool rz_platform_target_index_load_sdb(RZ_NONNULL RzPlatformTargetIndex *t, RZ_NONNULL const char *path)
Loads the contents of the Platform Profile to the RzPlatformTargetIndex.
static bool sdb_load_arch_platform_by_path(RZ_NONNULL RzPlatformTargetIndex *t, RZ_NONNULL const char *path)
RZ_API RZ_OWN RzPlatformItem * rz_platform_item_new(RZ_NULLABLE const char *name)
Creates a new RzPlatformItem type.
static bool sdb_load_platform_profile(RZ_NONNULL RzPlatformTargetIndex *t, RZ_NONNULL Sdb *sdb)
RZ_API void rz_platform_target_index_free(RzPlatformTargetIndex *target)
Frees an RzPlatformTargetIndex type.
RZ_API RZ_OWN RzPlatformTargetIndex * rz_platform_target_index_new()
Creates a new RzPlatformTargetIndex type.
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#define rz_strf(buf,...)
Convenience macro for local temporary strings.
Definition: rz_str.h:59
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NONNULL
Definition: rz_types.h:64
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API void sdb_close(Sdb *s)
Definition: sdb.c:416
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
RZ_API SdbList * sdb_foreach_list(Sdb *s, bool sorted)
Definition: sdb.c:630
static char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21
static char * sdbkv_value(const SdbKv *kv)
Definition: sdbht.h:25
Definition: ls.h:17
Definition: ls.h:22
Definition: z80asm.h:102
char * path
Path used to load the platforms, used to avoid reloading.
Definition: rz_platform.h:44
Definition: sdbht.h:14
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()