Rizin
unix-like reverse engineering framework and cli tools
lib.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2020 condret
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_lib.h>
5 #include <rz_core.h>
6 #include <rz_lang.h>
7 
8 static int lang_lib_init(RzLang *user) {
9  return true;
10 }
11 
12 static int lang_lib_file_run(RzLang *user, const char *file) {
13  char *libpath;
14  void *lib;
15  if (!(libpath = rz_str_new(file))) {
16  return -1;
17  }
18  if (!rz_str_startswith(libpath, "/") && !rz_str_startswith(libpath, "./")) {
19  libpath = rz_str_prepend(libpath, "./");
20  }
21  if (!rz_file_exists(libpath)) {
22  if (!rz_str_endswith(libpath, RZ_LIB_EXT)) {
23  libpath = rz_str_appendf(libpath, ".%s", RZ_LIB_EXT);
24  }
25  }
26  if (!rz_file_exists(libpath)) {
27  free(libpath);
28  return -1;
29  }
30 
31  lib = rz_lib_dl_open(libpath);
32  if (lib) {
33  void (*fcn)(RzCore *);
34  fcn = rz_lib_dl_sym(lib, "entry");
35  if (fcn) {
36  fcn(user->user);
37  } else {
38  eprintf("Cannot find 'entry' symbol in library\n");
39  }
40  rz_lib_dl_close(lib);
41  }
42  free(libpath);
43  return 0;
44 }
45 
47  .name = "lib",
48  .ext = RZ_LIB_EXT,
49  .desc = "Load libs directly into rizin",
50  .license = "LGPL",
51  .init = lang_lib_init,
52  .run_file = lang_lib_file_run,
53 };
54 
55 #ifndef RZ_PLUGIN_INCORE
58  .data = &rz_lang_plugin_lib,
59  .version = RZ_VERSION
60 };
61 #endif
#define RZ_API
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RzLangPlugin rz_lang_plugin_lib
Definition: lib.c:46
static int lang_lib_file_run(RzLang *user, const char *file)
Definition: lib.c:12
RZ_API RzLibStruct rizin_plugin
Definition: lib.c:56
static int lang_lib_init(RzLang *user)
Definition: lib.c:8
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
RZ_API int rz_lib_dl_close(void *handler)
Definition: lib.c:104
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
@ RZ_LIB_TYPE_LANG
Definition: rz_lib.h:71
#define RZ_LIB_EXT
Definition: rz_lib.h:31
RZ_API char * rz_str_appendf(char *ptr, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RZ_API char * rz_str_prepend(char *ptr, const char *string)
Definition: str.c:1027
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329
#define RZ_VERSION
Definition: rz_version.h:8
Definition: gzappend.c:170
const char * name
Definition: rz_lang.h:28
void * user
Definition: rz_lang.h:19