Some code cleanup and start to introduce r_return in r_io (#12523)

This commit is contained in:
radare 2018-12-20 15:31:18 +01:00 committed by GitHub
parent e3620985a5
commit 84edbe8064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 79 deletions

View File

@ -22,15 +22,9 @@ static Sdb* get_sdb (RBinFile *bf) {
}
static char *entitlements(RBinFile *bf, bool json) {
struct MACH0_(obj_t) *bin;
if (!bf || !bf->o || json) {
return NULL;
}
bin = bf->o->bin_obj;
if (!bin->signature) {
return NULL;
}
return strdup ((char*) bin->signature);
r_return_val_if_fail (bf && bf->o && bf->o->bin_obj, NULL);
struct MACH0_(obj_t) *bin = bf->o->bin_obj;
return r_str_dup (NULL, bin->signature);
}
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){

View File

@ -100,9 +100,7 @@ R_API RIO* r_io_new() {
}
R_API RIO* r_io_init(RIO* io) {
if (!io) {
return NULL;
}
r_return_val_if_fail (io, NULL);
io->addrbytes = 1;
r_io_desc_init (io);
r_pvector_init (&io->map_skyline, free);
@ -167,9 +165,7 @@ R_API RIODesc *r_io_open_nomap(RIO *io, const char *uri, int perm, int mode) {
/* opens a file and maps it to 0x0 */
R_API RIODesc* r_io_open(RIO* io, const char* uri, int perm, int mode) {
if (!io || !io->maps) {
return NULL;
}
r_return_val_if_fail (io && io->maps, NULL);
RIODesc* desc = r_io_open_nomap (io, uri, perm, mode);
if (!desc) {
return NULL;
@ -180,11 +176,9 @@ R_API RIODesc* r_io_open(RIO* io, const char* uri, int perm, int mode) {
/* opens a file and maps it to an offset specified by the "at"-parameter */
R_API RIODesc* r_io_open_at(RIO* io, const char* uri, int perm, int mode, ut64 at) {
r_return_val_if_fail (io && io->maps, NULL);
RIODesc* desc;
ut64 size;
if (!io || !io->maps) {
return NULL;
}
desc = r_io_open_nomap (io, uri, perm, mode);
if (!desc) {
return NULL;
@ -207,9 +201,7 @@ R_API RList* r_io_open_many(RIO* io, const char* uri, int perm, int mode) {
RList* desc_list;
RListIter* iter;
RIODesc* desc;
if (!io || !io->files || !uri) {
return NULL;
}
r_return_val_if_fail (io && io->files && uri, NULL);
RIOPlugin* plugin = r_io_plugin_resolve (io, uri, 1);
if (!plugin || !plugin->open_many || !plugin->close) {
return NULL;
@ -278,9 +270,7 @@ R_API int r_io_close_all(RIO* io) { // what about undo?
}
R_API int r_io_pread_at(RIO* io, ut64 paddr, ut8* buf, int len) {
if (!io || !buf || len < 1) {
return 0;
}
r_return_val_if_fail (io && buf && len > 0, -1);
if (io->ff) {
memset (buf, io->Oxff, len);
}
@ -288,33 +278,20 @@ R_API int r_io_pread_at(RIO* io, ut64 paddr, ut8* buf, int len) {
}
R_API int r_io_pwrite_at(RIO* io, ut64 paddr, const ut8* buf, int len) {
if (!io) {
return 0;
}
r_return_val_if_fail (io && buf && len > 0, -1);
return r_io_desc_write_at (io->desc, paddr, buf, len);
}
// Returns true iff all reads on mapped regions are successful and complete.
R_API bool r_io_vread_at_mapped(RIO* io, ut64 vaddr, ut8* buf, int len) {
if (!io || !buf || (len < 1)) {
return false;
}
r_return_val_if_fail (io && buf && len > 0, false);
if (io->ff) {
memset (buf, io->Oxff, len);
}
if (!io->maps) {
return false;
}
return on_map_skyline (io, vaddr, buf, len, R_PERM_R, fd_read_at_wrap, false);
}
static bool r_io_vwrite_at(RIO* io, ut64 vaddr, const ut8* buf, int len) {
if (!io || !buf || (len < 1)) {
return false;
}
if (!io->maps) {
return false;
}
return on_map_skyline (io, vaddr, (ut8*)buf, len, R_PERM_W, fd_write_at_wrap, false);
}
@ -324,15 +301,10 @@ static bool r_io_vwrite_at(RIO* io, ut64 vaddr, const ut8* buf, int len) {
// For physical mode, the interface is broken because the actual read bytes are
// not available. This requires fixes in all call sites.
R_API bool r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) {
bool ret;
if (!io || !buf || len < 1) {
return false;
}
if (io->va) {
ret = r_io_vread_at_mapped (io, addr, buf, len);
} else {
ret = r_io_pread_at (io, addr, buf, len) > 0;
}
r_return_val_if_fail (io && buf && len > 0, false);
bool ret = (io->va)
? r_io_vread_at_mapped (io, addr, buf, len)
: r_io_pread_at (io, addr, buf, len) > 0;
if (io->cached & R_PERM_R) {
(void)r_io_cache_read (io, addr, buf, len);
}
@ -345,9 +317,7 @@ R_API bool r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) {
// of read bytes.
R_API bool r_io_read_at_mapped(RIO *io, ut64 addr, ut8 *buf, int len) {
bool ret;
if (!io || !buf) {
return false;
}
r_return_val_if_fail (io && buf, false);
if (io->ff) {
memset (buf, io->Oxff, len);
}
@ -367,9 +337,7 @@ R_API bool r_io_read_at_mapped(RIO *io, ut64 addr, ut8 *buf, int len) {
// Returns -1 on error.
R_API int r_io_nread_at(RIO *io, ut64 addr, ut8 *buf, int len) {
int ret;
if (!io || !buf) {
return -1;
}
r_return_val_if_fail (io && buf && len > 0, -1);
if (io->va) {
if (io->ff) {
memset (buf, io->Oxff, len);
@ -388,9 +356,7 @@ R_API bool r_io_write_at(RIO* io, ut64 addr, const ut8* buf, int len) {
int i;
bool ret = false;
ut8 *mybuf = (ut8*)buf;
if (!io || !buf || len < 1) {
return false;
}
r_return_val_if_fail (io && buf && len > 0, false);
if (io->write_mask) {
mybuf = r_mem_dup ((void*)buf, len);
for (i = 0; i < len; i++) {

View File

@ -160,7 +160,7 @@ R_API int r_str_bits(char *strout, const ut8 *buf, int len, const char *bitz) {
// For example, the bitstring 1000000000000000 will not be modified, but the
// bitstring 0000000001000000 will be changed to 01000000.
static void trimbits(char *b) {
int len = strlen (b);
const int len = strlen (b);
char *one = strchr (b, '1');
int pos = one ? (int)(size_t)(one - b) : len - 1;
pos = (pos / 8) * 8;
@ -180,7 +180,7 @@ R_API int r_str_bits64(char* strout, ut64 in) {
} else {
strout[count] = '0';
}
++count;
count++;
}
strout[count] = '\0';
/* trim by 8 bits */
@ -666,41 +666,35 @@ R_API const char *r_str_nstr(const char *from, const char *to, int size) {
}
// Returns a new heap-allocated copy of str.
// XXX whats the diff with r_str_dup ?
R_API char *r_str_new(const char *str) {
if (!str) {
return NULL;
}
return strdup (str);
return str? strdup (str): NULL;
}
// Returns a new heap-allocated copy of str, sets str[len] to '\0'.
// If the input str is longer than len, it will be truncated.
R_API char *r_str_newlen(const char *str, int len) {
char *buf;
if (len < 1) {
return NULL;
}
buf = malloc (len + 1);
if (!buf) {
return NULL;
char *buf = malloc (len + 1);
if (buf) {
memcpy (buf, str, len);
buf[len] = 0;
}
memcpy (buf, str, len);
buf[len] = 0;
return buf;
}
R_API char *r_str_trunc_ellipsis(const char *str, int len) {
char *buf;
if (!str) {
return NULL;
}
if (strlen (str) < len) {
buf = strdup (str);
} else {
buf = r_str_newlen (str, len);
if (buf) {
strcpy (buf + len - 4, "...");
}
return strdup (str);
}
char *buf = r_str_newlen (str, len);
if (buf && len > 4) {
strcpy (buf + len - 4, "...");
}
return buf;
}
@ -828,13 +822,12 @@ R_API char *r_str_ndup(const char *ptr, int len) {
// TODO: deprecate?
R_API char *r_str_dup(char *ptr, const char *string) {
int len;
free (ptr);
if (!string) {
return NULL;
}
len = strlen (string)+1;
ptr = malloc (len+1);
int len = strlen (string) + 1;
ptr = malloc (len + 1);
if (!ptr) {
return NULL;
}