2018-01-10 06:43:23 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2018 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include "r_io.h"
|
2017-05-26 00:43:26 +00:00
|
|
|
#include "config.h"
|
2009-02-05 21:08:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-05-03 15:19:49 +00:00
|
|
|
volatile static RIOPlugin *DEFAULT = NULL;
|
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
static RIOPlugin *io_static_plugins[] = {
|
|
|
|
R_IO_STATIC_PLUGINS
|
|
|
|
};
|
2009-08-22 03:11:33 +00:00
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
R_API bool r_io_plugin_add(RIO *io, RIOPlugin *plugin) {
|
|
|
|
if (!io || !io->plugins || !plugin || !plugin->name) {
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2016-06-19 22:29:41 +00:00
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
ls_append (io->plugins, plugin);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
R_API bool r_io_plugin_init(RIO *io) {
|
2010-05-28 00:44:51 +00:00
|
|
|
RIOPlugin *static_plugin;
|
2009-08-22 05:29:58 +00:00
|
|
|
int i;
|
2017-05-03 15:19:49 +00:00
|
|
|
if (!io) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
io->plugins = ls_newf (free);
|
2016-06-19 22:29:41 +00:00
|
|
|
for (i = 0; io_static_plugins[i]; i++) {
|
|
|
|
if (!io_static_plugins[i]->name) {
|
2011-10-08 21:39:06 +00:00
|
|
|
continue;
|
2016-06-19 22:29:41 +00:00
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
static_plugin = R_NEW0 (RIOPlugin);
|
2016-06-19 22:29:41 +00:00
|
|
|
if (!static_plugin) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-28 00:44:51 +00:00
|
|
|
memcpy (static_plugin, io_static_plugins[i], sizeof (RIOPlugin));
|
2017-05-03 15:19:49 +00:00
|
|
|
r_io_plugin_add (io, static_plugin);
|
2010-05-28 00:44:51 +00:00
|
|
|
}
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2009-08-22 05:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
R_API RIOPlugin *r_io_plugin_get_default(RIO *io, const char *filename, bool many) {
|
2018-01-10 06:43:23 +00:00
|
|
|
if (!DEFAULT || !DEFAULT->check || !DEFAULT->check (io, filename, many) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
return (RIOPlugin*) DEFAULT;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:29:41 +00:00
|
|
|
R_API RIOPlugin *r_io_plugin_resolve(RIO *io, const char *filename, bool many) {
|
2017-05-03 15:19:49 +00:00
|
|
|
SdbListIter *iter;
|
|
|
|
RIOPlugin *ret;
|
|
|
|
ls_foreach (io->plugins, iter, ret) {
|
2017-08-14 00:55:47 +00:00
|
|
|
if (!ret || !ret->check) {
|
2017-05-03 15:19:49 +00:00
|
|
|
continue;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2018-01-10 06:43:23 +00:00
|
|
|
if (ret->check (io, filename, many)) {
|
2017-05-03 15:19:49 +00:00
|
|
|
return ret;
|
2018-01-10 06:43:23 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
return r_io_plugin_get_default (io, filename, many);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:12:44 +00:00
|
|
|
R_API RIOPlugin *r_io_plugin_byname(RIO *io, const char *name) {
|
2017-05-03 15:19:49 +00:00
|
|
|
SdbListIter *iter;
|
2016-06-20 07:12:44 +00:00
|
|
|
RIOPlugin *iop;
|
2017-05-03 15:19:49 +00:00
|
|
|
ls_foreach (io->plugins, iter, iop) {
|
2016-06-20 07:12:44 +00:00
|
|
|
if (!strcmp (name, iop->name)) {
|
|
|
|
return iop;
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 15:19:49 +00:00
|
|
|
return r_io_plugin_get_default (io, name, false);
|
2016-06-20 07:12:44 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 10:10:30 +00:00
|
|
|
R_API int r_io_plugin_list(RIO *io) {
|
2016-06-19 22:29:41 +00:00
|
|
|
RIOPlugin *plugin;
|
2017-05-03 15:19:49 +00:00
|
|
|
SdbListIter *iter;
|
2016-06-19 22:29:41 +00:00
|
|
|
char str[4];
|
2009-02-05 21:08:46 +00:00
|
|
|
int n = 0;
|
2016-06-19 22:29:41 +00:00
|
|
|
|
2017-05-03 15:19:49 +00:00
|
|
|
ls_foreach (io->plugins, iter, plugin) {
|
2016-06-19 22:29:41 +00:00
|
|
|
str[0] = 'r';
|
|
|
|
str[1] = plugin->write ? 'w' : '_';
|
|
|
|
str[2] = plugin->isdbg ? 'd' : '_';
|
|
|
|
str[3] = 0;
|
2017-02-28 01:06:46 +00:00
|
|
|
io->cb_printf ("%s %-8s %s (%s)",
|
|
|
|
str, plugin->name,
|
2016-06-19 22:29:41 +00:00
|
|
|
plugin->desc, plugin->license);
|
2017-02-28 01:06:46 +00:00
|
|
|
if (plugin->version) {
|
|
|
|
io->cb_printf (" v%s", plugin->version);
|
|
|
|
}
|
|
|
|
if (plugin->author) {
|
|
|
|
io->cb_printf (" %s", plugin->author);
|
|
|
|
}
|
|
|
|
io->cb_printf ("\n");
|
2009-02-05 21:08:46 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2017-11-04 19:55:26 +00:00
|
|
|
|
|
|
|
R_API int r_io_plugin_list_json(RIO *io) {
|
|
|
|
RIOPlugin *plugin;
|
|
|
|
SdbListIter *iter;
|
|
|
|
char str[4];
|
|
|
|
int n = 0;
|
|
|
|
io->cb_printf("{\"IO_Plugins\":[");
|
|
|
|
ls_foreach (io->plugins, iter, plugin) {
|
|
|
|
str[0] = 'r';
|
|
|
|
str[1] = plugin->write ? 'w' : '_';
|
|
|
|
str[2] = plugin->isdbg ? 'd' : '_';
|
|
|
|
str[3] = 0;
|
|
|
|
|
2018-03-09 01:41:21 +00:00
|
|
|
io->cb_printf ("%s{\"Permissions\":\"%s\",\"Name\":\"%s\",\"Description\":\"%s\",\"License\":\"%s\"",
|
|
|
|
n? "," : "", str, plugin->name,
|
2017-11-04 19:55:26 +00:00
|
|
|
plugin->desc, plugin->license);
|
|
|
|
if (plugin->version) {
|
|
|
|
io->cb_printf (",\"version\":\"%s\"", plugin->version);
|
|
|
|
}
|
|
|
|
if (plugin->author) {
|
|
|
|
io->cb_printf (",\"plugin\":\"%s\"", plugin->author);
|
|
|
|
}
|
|
|
|
io->cb_printf ("}");
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
io->cb_printf("]}");
|
|
|
|
return n;
|
|
|
|
}
|