Skip to content

Commit

Permalink
Boolify io.extend() and use more asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Oct 23, 2023
1 parent 5610cd9 commit 89b26be
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ typedef struct r_io_plugin_t {
bool (*getbase)(RIODesc *desc, ut64 *base);
///
bool (*resize)(RIO *io, RIODesc *fd, ut64 size);
int (*extend)(RIO *io, RIODesc *fd, ut64 size);
bool (*extend)(RIO *io, RIODesc *fd, ut64 size);
bool (*accept)(RIO *io, RIODesc *desc, int fd);
int (*create)(RIO *io, const char *file, int mode, int type);
bool (*check)(RIO *io, const char *, bool many);
Expand Down Expand Up @@ -441,7 +441,7 @@ R_API ut64 r_io_size(RIO *io);
R_API bool r_io_is_listener(RIO *io);
R_API char *r_io_system(RIO *io, const char* cmd);
R_API bool r_io_resize(RIO *io, ut64 newsize);
R_API int r_io_extend_at(RIO *io, ut64 addr, ut64 size);
R_API bool r_io_extend_at(RIO *io, ut64 addr, ut64 size);
R_API bool r_io_set_write_mask(RIO *io, const ut8 *mask, int len);
R_API void r_io_bind(RIO *io, RIOBind *bnd);
R_API bool r_io_shift(RIO *io, ut64 start, ut64 end, st64 move);
Expand Down Expand Up @@ -549,7 +549,7 @@ R_API void r_io_desc_cache_cleanup(RIODesc *desc);
R_API void r_io_desc_cache_fini(RIODesc *desc);
R_API void r_io_desc_cache_fini_all(RIO *io);
R_API RList *r_io_desc_cache_list(RIODesc *desc);
R_API int r_io_desc_extend(RIODesc *desc, ut64 size);
R_API bool r_io_desc_extend(RIODesc *desc, ut64 size);

/* io/fd.c */
R_API int r_io_fd_open(RIO *io, const char *uri, int flags, int mode);
Expand Down
79 changes: 41 additions & 38 deletions libr/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,17 @@ R_API bool r_io_write_at(RIO* io, ut64 addr, const ut8* buf, int len) {
}

R_API bool r_io_read(RIO* io, ut8* buf, int len) {
if (io && r_io_read_at (io, io->off, buf, len)) {
r_return_val_if_fail (io, false);
if (r_io_read_at (io, io->off, buf, len)) {
io->off += len;
return true;
}
return false;
}

R_API bool r_io_write(RIO* io, ut8* buf, int len) {
if (io && buf && len > 0 && r_io_write_at (io, io->off, buf, len)) {
r_return_val_if_fail (io, false);
if (buf && len > 0 && r_io_write_at (io, io->off, buf, len)) {
io->off += len;
return true;
}
Expand All @@ -362,61 +364,57 @@ R_API ut64 r_io_size(RIO* io) {
}

R_API bool r_io_is_listener(RIO* io) {
if (io && io->desc && io->desc->plugin && io->desc->plugin->listener) {
r_return_val_if_fail (io, false);
if (io->desc && io->desc->plugin && io->desc->plugin->listener) {
return io->desc->plugin->listener (io->desc);
}
return false;
}

R_API char *r_io_system(RIO* io, const char* cmd) {
if (io && io->desc) {
return r_io_desc_system (io->desc, cmd);
}
return NULL;
r_return_val_if_fail (io && cmd, NULL);
return io->desc? r_io_desc_system (io->desc, cmd): NULL;
}

R_API bool r_io_resize(RIO* io, ut64 newsize) {
if (io) {
RList *maps = r_io_map_get_by_fd (io, io->desc->fd);
RIOMap *current_map;
RListIter *iter;
ut64 fd_size = r_io_fd_size (io, io->desc->fd);
const bool ret = r_io_desc_resize (io->desc, newsize);
r_list_foreach (maps, iter, current_map) {
// we just resize map of the same size of its fd
if (r_io_map_size (current_map) == fd_size) {
r_io_map_resize (io, current_map->id, newsize);
}
r_return_val_if_fail (io, false);
RList *maps = r_io_map_get_by_fd (io, io->desc->fd);
RIOMap *current_map;
RListIter *iter;
ut64 fd_size = r_io_fd_size (io, io->desc->fd);
const bool ret = r_io_desc_resize (io->desc, newsize);
r_list_foreach (maps, iter, current_map) {
// we just resize map of the same size of its fd
if (r_io_map_size (current_map) == fd_size) {
r_io_map_resize (io, current_map->id, newsize);
}
r_list_free (maps);
return ret;
}
return false;
r_list_free (maps);
return ret;
}

R_API bool r_io_close(RIO *io) {
return io ? r_io_desc_close (io->desc) : false;
r_return_val_if_fail (io, false);
return r_io_desc_close (io->desc);
}

R_API int r_io_extend_at(RIO* io, ut64 addr, ut64 size) {
ut64 cur_size, tmp_size;
ut8* buffer;
if (!io || !io->desc || !io->desc->plugin || !size) {
R_API bool r_io_extend_at(RIO* io, ut64 addr, ut64 size) {
r_return_val_if_fail (io, false);
if (!io->desc || !io->desc->plugin || !size) {
return false;
}
if (io->desc->plugin->extend) {
int ret;
ut64 cur_off = io->off;
r_io_seek (io, addr, R_IO_SEEK_SET);
ret = r_io_desc_extend (io->desc, size);
int ret = r_io_desc_extend (io->desc, size);
//no need to seek here
io->off = cur_off;
return ret;
}
if ((io->desc->perm & R_PERM_RW) != R_PERM_RW) {
return false;
}
cur_size = r_io_desc_size (io->desc);
ut64 cur_size = r_io_desc_size (io->desc);
if (addr > cur_size) {
return false;
}
Expand All @@ -426,10 +424,12 @@ R_API int r_io_extend_at(RIO* io, ut64 addr, ut64 size) {
if (!r_io_resize (io, cur_size + size)) {
return false;
}
if ((tmp_size = cur_size - addr) == 0LL) {
ut64 tmp_size = cur_size - addr;
if (tmp_size == 0LL) {
return true;
}
if (!(buffer = calloc (1, (size_t) tmp_size + 1))) {
ut8 *buffer = calloc (1, (size_t) tmp_size + 1);
if (!buffer) {
return false;
}
r_io_pread_at (io, addr, buffer, (int) tmp_size);
Expand All @@ -445,7 +445,8 @@ R_API int r_io_extend_at(RIO* io, ut64 addr, ut64 size) {
}

R_API bool r_io_set_write_mask(RIO* io, const ut8* mask, int len) {
if (!io || len < 1) {
r_return_val_if_fail (io, false);
if (len < 1) {
return false;
}
free (io->write_mask);
Expand All @@ -461,6 +462,7 @@ R_API bool r_io_set_write_mask(RIO* io, const ut8* mask, int len) {
}

R_API ut64 r_io_p2v(RIO *io, ut64 pa) {
r_return_val_if_fail (io, 0);
RIOMap *map = r_io_map_get_paddr (io, pa);
if (map) {
return pa - map->delta + r_io_map_begin (map);
Expand All @@ -469,6 +471,7 @@ R_API ut64 r_io_p2v(RIO *io, ut64 pa) {
}

R_API ut64 r_io_v2p(RIO *io, ut64 va) {
r_return_val_if_fail (io, 0);
RIOMap *map = r_io_map_get_at (io, va);
if (map) {
st64 delta = va - r_io_map_begin (map);
Expand Down Expand Up @@ -522,15 +525,16 @@ R_API void r_io_bind(RIO *io, RIOBind *bnd) {

/* moves bytes up (+) or down (-) within the specified range */
R_API bool r_io_shift(RIO* io, ut64 start, ut64 end, st64 move) {
ut8* buf;
r_return_val_if_fail (io && start < end, false);
ut64 chunksize = 0x10000;
ut64 saved_off = io->off;
ut64 src, shiftsize = r_num_abs (move);
if (!shiftsize || (end - start) <= shiftsize) {
return false;
}
ut64 rest = (end - start) - shiftsize;
if (!(buf = calloc (1, chunksize + 1))) {
ut8 *buf = calloc (1, chunksize + 1);
if (!buf) {
return false;
}
if (move > 0) {
Expand Down Expand Up @@ -558,9 +562,7 @@ R_API bool r_io_shift(RIO* io, ut64 start, ut64 end, st64 move) {
}

R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence) {
if (!io) {
return 0LL;
}
r_return_val_if_fail (io, 0);
switch (whence) {
case R_IO_SEEK_SET:
io->off = offset;
Expand All @@ -576,12 +578,13 @@ R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence) {
return io->off;
}

static bool drain_cb (void *user, void *data, ut32 id) {
static bool drain_cb(void *user, void *data, ut32 id) {
r_io_map_drain_overlay ((RIOMap *)data);
return true;
}

R_API void r_io_drain_overlay(RIO *io) {
r_return_if_fail (io);
r_id_storage_foreach (io->maps, drain_cb, NULL);
}

Expand Down
2 changes: 1 addition & 1 deletion libr/io/io_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ R_API int r_io_desc_write_at(RIODesc *desc, ut64 addr, const ut8 *buf, int len)
return 0;
}

R_API int r_io_desc_extend(RIODesc *desc, ut64 size) {
R_API bool r_io_desc_extend(RIODesc *desc, ut64 size) {
if (desc && desc->plugin && desc->plugin->extend) {
return desc->plugin->extend (desc->io, desc, size);
}
Expand Down
3 changes: 0 additions & 3 deletions libr/io/p/io_ihex.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,6 @@ static char *__system(RIO *io, RIODesc *fd, const char *cmd) {
r_strbuf_append (sb, s);
free (s);
}
r_strbuf_appendf (sb, "min 0x%08"PFMT64x"\n", rih->min);
r_strbuf_appendf (sb, "max 0x%08"PFMT64x"\n", rih->max);
r_strbuf_appendf (sb, "siz 0x%08"PFMT64x"\n", rih->max - rih->min);
break;
default:
r_strbuf_appendf (sb, "min 0x%08"PFMT64x"\n", rih->min);
Expand Down

0 comments on commit 89b26be

Please sign in to comment.