diff --git a/libr/include/r_io.h b/libr/include/r_io.h index eb73ba90bc..a6674a049f 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -429,6 +429,7 @@ R_API int r_io_cache_invalidate(RIO *io, ut64 from, ut64 to); R_API bool r_io_cache_at(RIO *io, ut64 addr); R_API void r_io_cache_commit(RIO *io, ut64 from, ut64 to); R_API void r_io_cache_init(RIO *io); +R_API void r_io_cache_fini (RIO *io); R_API int r_io_cache_list(RIO *io, int rad); R_API void r_io_cache_reset(RIO *io, int set); R_API bool r_io_cache_write(RIO *io, ut64 addr, const ut8 *buf, int len); diff --git a/libr/io/cache.c b/libr/io/cache.c index 7028075640..72123ee278 100644 --- a/libr/io/cache.c +++ b/libr/io/cache.c @@ -34,6 +34,15 @@ R_API bool r_io_cache_at(RIO *io, ut64 addr) { R_API void r_io_cache_init(RIO *io) { io->cache = r_list_newf ((RListFree)cache_item_free); + io->buffer = r_cache_new (); + io->cached = 0; +} + +R_API void r_io_cache_fini (RIO *io) { + r_list_free (io->cache); + r_cache_free (io->buffer); + io->cache = NULL; + io->buffer = NULL; io->cached = 0; } diff --git a/libr/io/io.c b/libr/io/io.c index 1cd8365788..2ab053aa74 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -219,7 +219,9 @@ R_API RIO* r_io_init(RIO* io) { R_API RBuffer *r_io_read_buf(RIO *io, ut64 addr, int len) { RBuffer *b = R_NEW0 (RBuffer); - if (!b) return NULL; + if (!b) { + return NULL; + } b->buf = malloc (len); if (!b->buf) { free (b); @@ -248,7 +250,7 @@ R_API RIODesc *r_io_open_buffer(RIO *io, RBuffer *b, int flags, int mode) { char *uri = r_str_newf ("malloc://%d", bufSize); RIODesc *desc = r_io_open_nomap (io, uri, flags, mode); if (desc) { - r_io_desc_write (desc, r_buf_get_at(b, 0, NULL), bufSize); + r_io_desc_write (desc, r_buf_get_at (b, 0, NULL), bufSize); } return desc; } @@ -369,11 +371,10 @@ R_API int r_io_close_all(RIO* io) { // what about undo? r_io_map_fini (io); r_io_section_fini (io); ls_free (io->plugins); - r_list_free (io->cache); r_io_desc_init (io); r_io_map_init (io); r_io_section_init (io); - r_io_cache_init (io); + r_io_cache_fini (io); r_io_plugin_init (io); return true; } @@ -463,15 +464,16 @@ R_API bool r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) { return false; } if (io->buffer_enabled) { - return !!r_io_buffer_read(io, addr, buf, len); + int res = r_io_buffer_read (io, addr, buf, len); + return res > 0? true: false; } if (io->va) { - ret = r_io_vread_at_mapped(io, addr, buf, len); + ret = r_io_vread_at_mapped (io, addr, buf, len); } else { - ret = r_io_pread_at(io, addr, buf, len) > 0; + ret = r_io_pread_at (io, addr, buf, len) > 0; } if (io->cached & R_IO_READ) { - (void)r_io_cache_read(io, addr, buf, len); + (void)r_io_cache_read (io, addr, buf, len); } return ret; } @@ -589,10 +591,7 @@ 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) { - return false; - } - if (r_io_read_at (io, io->off, buf, len)) { + if (io && r_io_read_at (io, io->off, buf, len)) { io->off += len; return true; } @@ -600,10 +599,7 @@ R_API bool r_io_read(RIO* io, ut8* buf, int len) { } R_API bool r_io_write(RIO* io, ut8* buf, int len) { - if (!io || !buf || len < 1) { - return false; - } - if (r_io_write_at (io, io->off, buf, len)) { + if (io && buf && len > 0 && r_io_write_at (io, io->off, buf, len)) { io->off += len; return true; } diff --git a/libr/io/iobuf.c b/libr/io/iobuf.c index 5709c43cf4..50def8fa91 100644 --- a/libr/io/iobuf.c +++ b/libr/io/iobuf.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2013-2017 - pancake */ +/* radare - LGPL - Copyright 2013-2018 - pancake */ #include "r_io.h" @@ -34,10 +34,9 @@ R_API const ut8* r_io_buffer_get (RIO *io, ut64 addr, int *len) { } R_API int r_io_buffer_read (RIO *io, ut64 addr, ut8* buf, int len) { - const ut8 *ret; int next, l = 0; // align addr if out of buffer if its mapped on io // - ret = r_cache_get (io->buffer, addr, &l); + const ut8 *ret = r_cache_get (io->buffer, addr, &l); if (!ret) { if (l < 1) { return 0; // no next block in buffer cache @@ -57,7 +56,7 @@ R_API int r_io_buffer_read (RIO *io, ut64 addr, ut8* buf, int len) { l = len; } memset (buf, 0xff, next); - memcpy (buf + next, ret, len - next); + memcpy (buf + next, ret, R_MIN (l, len - next)); return len; } if (l > len) { diff --git a/libr/util/cache.c b/libr/util/cache.c index 31a6200f6f..37988f9925 100644 --- a/libr/util/cache.c +++ b/libr/util/cache.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2013-2017 - pancake */ +/* radare - LGPL - Copyright 2013-2018 - pancake */ // XXX: should use the same code as libr/io/cache.c // one malloc per write @@ -44,6 +44,9 @@ R_API const ut8 *r_cache_get(RCache *c, ut64 addr, int *len) { } R_API int r_cache_set(RCache *c, ut64 addr, const ut8 *buf, int len) { + if (!c) { + return 0; + } if (!c->buf) { c->buf = malloc (len); if (!c->buf) {