mirror of
https://github.com/radareorg/radare2.git
synced 2024-12-02 10:16:21 +00:00
More boolification and code cleanup -70LOC
This commit is contained in:
parent
a05003fd5c
commit
6479943c46
@ -179,7 +179,7 @@ typedef struct r_io_plugin_t {
|
||||
ut64 (*lseek)(RIO *io, RIODesc *fd, ut64 offset, int whence);
|
||||
int (*write)(RIO *io, RIODesc *fd, const ut8 *buf, int count);
|
||||
int (*close)(RIODesc *desc);
|
||||
int (*resize)(RIO *io, RIODesc *fd, ut64 size);
|
||||
bool (*resize)(RIO *io, RIODesc *fd, ut64 size);
|
||||
int (*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);
|
||||
@ -333,7 +333,7 @@ R_API int r_io_plugin_close(RIO *io, RIODesc *desc);
|
||||
R_API int r_io_close(RIO *io, RIODesc *desc);
|
||||
R_API int r_io_close_all(RIO *io);
|
||||
R_API ut64 r_io_size(RIO *io); //, int fd);
|
||||
R_API int r_io_resize(RIO *io, ut64 newsize);
|
||||
R_API bool r_io_resize(RIO *io, ut64 newsize);
|
||||
R_API int r_io_extend(RIO *io, ut64 size);
|
||||
R_API int r_io_extend_at(RIO *io, ut64 addr, ut64 size);
|
||||
R_API int r_io_accept(RIO *io, int fd);
|
||||
|
@ -562,12 +562,13 @@ R_API ut64 r_io_read_i(RIO *io, ut64 addr, int sz) {
|
||||
}
|
||||
|
||||
// TODO. this is a physical resize
|
||||
R_API int r_io_resize(RIO *io, ut64 newsize) {
|
||||
R_API bool r_io_resize(RIO *io, ut64 newsize) {
|
||||
if (io->plugin) {
|
||||
if (io->plugin->resize && io->desc) {
|
||||
int res = io->plugin->resize (io, io->desc, newsize);
|
||||
if (res)
|
||||
bool res = io->plugin->resize (io, io->desc, newsize);
|
||||
if (res) {
|
||||
r_io_map_truncate_update (io, io->desc->fd, newsize);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return false;
|
||||
|
@ -16,28 +16,11 @@ typedef struct r_io_mmo_t {
|
||||
int rawio;
|
||||
} RIOMMapFileObj;
|
||||
|
||||
static int r_io_def_mmap_refresh_def_mmap_buf(RIOMMapFileObj *mmo);
|
||||
static void r_io_def_mmap_free (RIOMMapFileObj *mmo);
|
||||
static int r_io_def_mmap_close(RIODesc *fd);
|
||||
static int r_io_def_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count);
|
||||
static int r_io_def_mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count);
|
||||
static RIODesc *r_io_def_mmap_open(RIO *io, const char *file, int flags, int mode);
|
||||
static ut64 r_io_def_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence);
|
||||
static ut64 r_io_def_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence);
|
||||
static int r_io_def_mmap_truncate(RIOMMapFileObj *mmo, ut64 size);
|
||||
static int r_io_def_mmap_resize(RIO *io, RIODesc *fd, ut64 size);
|
||||
|
||||
static RIODesc *__open_default(RIO *io, const char *file, int flags, int mode);
|
||||
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len);
|
||||
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int len);
|
||||
static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence);
|
||||
static int __close(RIODesc *fd);
|
||||
static int __resize(RIO *io, RIODesc *fd, ut64 newsize);
|
||||
|
||||
static int __io_posix_open (const char *file, int flags, int mode) {
|
||||
int fd;
|
||||
if (r_file_is_directory (file))
|
||||
if (r_file_is_directory (file)) {
|
||||
return -1;
|
||||
}
|
||||
#if __WINDOWS__
|
||||
if (flags & R_IO_WRITE) {
|
||||
fd = r_sandbox_open (file, O_BINARY | O_RDWR, 0);
|
||||
@ -53,6 +36,29 @@ static int __io_posix_open (const char *file, int flags, int mode) {
|
||||
return fd;
|
||||
}
|
||||
|
||||
static ut64 r_io_def_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence) {
|
||||
ut64 seek_val = UT64_MAX;
|
||||
|
||||
if (!mmo) return UT64_MAX;
|
||||
if (mmo->rawio) return lseek (mmo->fd, offset, whence);
|
||||
if (!mmo->buf) return UT64_MAX;
|
||||
|
||||
seek_val = mmo->buf->cur;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
seek_val = R_MIN (mmo->buf->length, offset);
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
seek_val = R_MIN (mmo->buf->length, (offset + mmo->buf->cur));
|
||||
break;
|
||||
case SEEK_END:
|
||||
seek_val = mmo->buf->length;
|
||||
break;
|
||||
}
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
}
|
||||
|
||||
static int r_io_def_mmap_refresh_def_mmap_buf(RIOMMapFileObj *mmo) {
|
||||
RIO* io = mmo->io_backref;
|
||||
ut64 cur;
|
||||
@ -84,6 +90,14 @@ static int r_io_def_mmap_refresh_def_mmap_buf(RIOMMapFileObj *mmo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static void r_io_def_mmap_free (RIOMMapFileObj *mmo) {
|
||||
free (mmo->filename);
|
||||
r_buf_free (mmo->buf);
|
||||
close (mmo->fd);
|
||||
memset (mmo, 0, sizeof (RIOMMapFileObj));
|
||||
free (mmo);
|
||||
}
|
||||
|
||||
RIOMMapFileObj *r_io_def_mmap_create_new_file(RIO *io, const char *filename, int mode, int flags) {
|
||||
RIOMMapFileObj *mmo = NULL;
|
||||
if (!io)
|
||||
@ -116,14 +130,6 @@ RIOMMapFileObj *r_io_def_mmap_create_new_file(RIO *io, const char *filename, in
|
||||
return mmo;
|
||||
}
|
||||
|
||||
static void r_io_def_mmap_free (RIOMMapFileObj *mmo) {
|
||||
free (mmo->filename);
|
||||
r_buf_free (mmo->buf);
|
||||
close (mmo->fd);
|
||||
memset (mmo, 0, sizeof (RIOMMapFileObj));
|
||||
free (mmo);
|
||||
}
|
||||
|
||||
static int r_io_def_mmap_close(RIODesc *fd) {
|
||||
if (!fd || !fd->data) return -1;
|
||||
r_io_def_mmap_free ((RIOMMapFileObj *) fd->data);
|
||||
@ -268,29 +274,6 @@ static RIODesc *r_io_def_mmap_open(RIO *io, const char *file, int flags, int mod
|
||||
return r_io_desc_new (&r_io_plugin_default, mmo->fd, mmo->filename, flags, mode, mmo);
|
||||
}
|
||||
|
||||
static ut64 r_io_def_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence) {
|
||||
ut64 seek_val = UT64_MAX;
|
||||
|
||||
if (!mmo) return UT64_MAX;
|
||||
if (mmo->rawio) return lseek (mmo->fd, offset, whence);
|
||||
if (!mmo->buf) return UT64_MAX;
|
||||
|
||||
seek_val = mmo->buf->cur;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
seek_val = R_MIN (mmo->buf->length, offset);
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
seek_val = R_MIN (mmo->buf->length, (offset + mmo->buf->cur));
|
||||
break;
|
||||
case SEEK_END:
|
||||
seek_val = mmo->buf->length;
|
||||
break;
|
||||
}
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
}
|
||||
|
||||
static ut64 r_io_def_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
|
||||
if (!fd || !fd->data)
|
||||
return UT64_MAX;
|
||||
@ -298,7 +281,7 @@ static ut64 r_io_def_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
|
||||
}
|
||||
|
||||
static int r_io_def_mmap_truncate(RIOMMapFileObj *mmo, ut64 size) {
|
||||
int res = r_file_truncate (mmo->filename, size);
|
||||
bool res = r_file_truncate (mmo->filename, size);
|
||||
if (res && !r_io_def_mmap_refresh_def_mmap_buf (mmo) ) {
|
||||
eprintf ("r_io_def_mmap_truncate: Error trying to refresh the def_mmap'ed file.");
|
||||
res = false;
|
||||
@ -306,15 +289,6 @@ static int r_io_def_mmap_truncate(RIOMMapFileObj *mmo, ut64 size) {
|
||||
return res;
|
||||
}
|
||||
|
||||
static int r_io_def_mmap_resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
RIOMMapFileObj *mmo;
|
||||
if (!fd || !fd->data)
|
||||
return -1;
|
||||
mmo = fd->data;
|
||||
if (!(mmo->flags & R_IO_WRITE)) return -1;
|
||||
return r_io_def_mmap_truncate (mmo, size);
|
||||
}
|
||||
|
||||
static bool __plugin_open_default(RIO *io, const char *file, bool many) {
|
||||
return r_io_def_mmap_check_default (file);
|
||||
}
|
||||
@ -345,8 +319,15 @@ static int __close(RIODesc *fd) {
|
||||
return r_io_def_mmap_close (fd);
|
||||
}
|
||||
|
||||
static int __resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
return r_io_def_mmap_resize (io, fd, size);
|
||||
static bool __resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
if (!fd || !fd->data) {
|
||||
return false;
|
||||
}
|
||||
RIOMMapFileObj *mmo = fd->data;
|
||||
if (!(mmo->flags & R_IO_WRITE)) {
|
||||
return false;
|
||||
}
|
||||
return r_io_def_mmap_truncate (mmo, size);
|
||||
}
|
||||
|
||||
struct r_io_plugin_t r_io_plugin_default = {
|
||||
|
@ -21,10 +21,12 @@ typedef struct {
|
||||
#define RIOMALLOC_OFF(x) (((RIOGzip*)x->data)->offset)
|
||||
|
||||
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
if (fd == NULL || fd->data == NULL)
|
||||
if (fd == NULL || fd->data == NULL) {
|
||||
return -1;
|
||||
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd))
|
||||
}
|
||||
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
||||
return -1;
|
||||
}
|
||||
if (RIOMALLOC_OFF (fd) + count > RIOMALLOC_SZ (fd))
|
||||
count -= (RIOMALLOC_OFF (fd) + count-(RIOMALLOC_SZ (fd)));
|
||||
|
||||
@ -36,24 +38,25 @@ static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static int __resize(RIO *io, RIODesc *fd, ut64 count) {
|
||||
static bool __resize(RIO *io, RIODesc *fd, ut64 count) {
|
||||
ut8 * new_buf = NULL;
|
||||
if (fd == NULL || fd->data == NULL || count == 0)
|
||||
return -1;
|
||||
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd))
|
||||
return -1;
|
||||
if (!fd || !fd->data || !count) {
|
||||
return false;
|
||||
}
|
||||
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
||||
return false;
|
||||
}
|
||||
new_buf = malloc (count);
|
||||
if (!new_buf) return -1;
|
||||
memcpy (new_buf, RIOMALLOC_BUF (fd), R_MIN(count, RIOMALLOC_SZ (fd)));
|
||||
if (count > RIOMALLOC_SZ (fd) )
|
||||
memset (new_buf+RIOMALLOC_SZ (fd), 0, count-RIOMALLOC_SZ (fd));
|
||||
|
||||
if (!new_buf) {
|
||||
return false;
|
||||
}
|
||||
memcpy (new_buf, RIOMALLOC_BUF (fd), R_MIN (count, RIOMALLOC_SZ (fd)));
|
||||
if (count > RIOMALLOC_SZ (fd))
|
||||
memset (new_buf + RIOMALLOC_SZ (fd), 0, count - RIOMALLOC_SZ (fd));
|
||||
free (RIOMALLOC_BUF (fd));
|
||||
RIOMALLOC_BUF (fd) = new_buf;
|
||||
RIOMALLOC_SZ (fd) = count;
|
||||
|
||||
return count;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
|
||||
@ -84,8 +87,9 @@ static int __close(RIODesc *fd) {
|
||||
|
||||
static ut64 __lseek(RIO* io, RIODesc *fd, ut64 offset, int whence) {
|
||||
ut64 r_offset = offset;
|
||||
if (!fd->data)
|
||||
if (!fd->data) {
|
||||
return offset;
|
||||
}
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
r_offset = (offset <= RIOMALLOC_SZ (fd)) ? offset : RIOMALLOC_SZ (fd);
|
||||
@ -109,18 +113,18 @@ static bool __plugin_open(RIO *io, const char *pathname, bool many) {
|
||||
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
|
||||
if (__plugin_open (io, pathname, 0)) {
|
||||
RIOGzip *mal = R_NEW0 (RIOGzip);
|
||||
if (!mal) return NULL;
|
||||
int len;
|
||||
ut8 *data = (ut8*)r_file_slurp (pathname+7, &len);
|
||||
mal->buf = r_inflate (data, len, NULL, &mal->size);
|
||||
if (mal->buf) {
|
||||
RETURN_IO_DESC_NEW (&r_io_plugin_malloc,
|
||||
mal->fd, pathname, rw, mode, mal);
|
||||
if (mal) {
|
||||
int len;
|
||||
ut8 *data = (ut8*)r_file_slurp (pathname+7, &len);
|
||||
mal->buf = r_inflate (data, len, NULL, &mal->size);
|
||||
if (mal->buf) {
|
||||
RETURN_IO_DESC_NEW (&r_io_plugin_malloc,
|
||||
mal->fd, pathname, rw, mode, mal);
|
||||
}
|
||||
free (data);
|
||||
eprintf ("Cannot allocate (%s) %d bytes\n", pathname + 9, mal->size);
|
||||
free (mal);
|
||||
}
|
||||
free (data);
|
||||
eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9,
|
||||
mal->size);
|
||||
free (mal);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -36,23 +36,24 @@ static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int __resize(RIO *io, RIODesc *fd, ut64 count) {
|
||||
static bool __resize(RIO *io, RIODesc *fd, ut64 count) {
|
||||
ut8 * new_buf = NULL;
|
||||
if (fd == NULL || fd->data == NULL || count == 0) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
if (RIOMALLOC_OFF (fd) > RIOMALLOC_SZ (fd)) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
new_buf = malloc (count);
|
||||
if (!new_buf) return -1;
|
||||
memcpy (new_buf, RIOMALLOC_BUF (fd), R_MIN(count, RIOMALLOC_SZ (fd)));
|
||||
if (count > RIOMALLOC_SZ (fd) )
|
||||
memset (new_buf+RIOMALLOC_SZ (fd), 0, count-RIOMALLOC_SZ (fd));
|
||||
memcpy (new_buf, RIOMALLOC_BUF (fd), R_MIN (count, RIOMALLOC_SZ (fd)));
|
||||
if (count > RIOMALLOC_SZ (fd)) {
|
||||
memset (new_buf + RIOMALLOC_SZ (fd), 0, count - RIOMALLOC_SZ (fd));
|
||||
}
|
||||
free (RIOMALLOC_BUF (fd));
|
||||
RIOMALLOC_BUF (fd) = new_buf;
|
||||
RIOMALLOC_SZ (fd) = count;
|
||||
return count;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
|
||||
@ -105,10 +106,7 @@ static ut64 __lseek(RIO* io, RIODesc *fd, ut64 offset, int whence) {
|
||||
}
|
||||
|
||||
static bool __check(RIO *io, const char *pathname, bool many) {
|
||||
return (
|
||||
(!strncmp (pathname, "malloc://", 9)) ||
|
||||
(!strncmp (pathname, "hex://", 6))
|
||||
);
|
||||
return (!strncmp (pathname, "malloc://", 9)) || (!strncmp (pathname, "hex://", 6));
|
||||
}
|
||||
|
||||
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
|
||||
@ -126,7 +124,7 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
|
||||
mal->offset = 0;
|
||||
memset (mal->buf, 0, mal->size);
|
||||
mal->size = r_hex_str2bin (pathname + 6, mal->buf);
|
||||
if ((int)mal->size<1) {
|
||||
if ((int)mal->size < 1) {
|
||||
R_FREE (mal->buf);
|
||||
}
|
||||
} else {
|
||||
|
@ -15,19 +15,28 @@ typedef struct r_io_mmo_t {
|
||||
RIO * io_backref;
|
||||
} RIOMMapFileObj;
|
||||
|
||||
static int r_io_mmap_refresh_buf(RIOMMapFileObj *mmo);
|
||||
static void r_io_mmap_free (RIOMMapFileObj *mmo);
|
||||
static int r_io_mmap_close(RIODesc *fd);
|
||||
static int r_io_mmap_check (const char *filname);
|
||||
static int r_io_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count);
|
||||
static int r_io_mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count);
|
||||
static RIODesc *r_io_mmap_open(RIO *io, const char *file, int flags, int mode);
|
||||
static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence);
|
||||
static ut64 r_io_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence);
|
||||
static int r_io_mmap_truncate(RIOMMapFileObj *mmo, ut64 size);
|
||||
static int r_io_mmap_resize(RIO *io, RIODesc *fd, ut64 size);
|
||||
static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence) {
|
||||
ut64 seek_val = mmo->buf->cur;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
seek_val = (mmo->buf->length < offset) ?
|
||||
mmo->buf->length : offset;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
case SEEK_CUR:
|
||||
seek_val = (mmo->buf->length < (offset + mmo->buf->cur)) ?
|
||||
mmo->buf->length : offset + mmo->buf->cur;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
case SEEK_END:
|
||||
seek_val = mmo->buf->length;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
}
|
||||
return seek_val;
|
||||
}
|
||||
|
||||
static int r_io_mmap_refresh_buf(RIOMMapFileObj *mmo) {
|
||||
static bool r_io_mmap_refresh_buf(RIOMMapFileObj *mmo) {
|
||||
RIO* io = mmo->io_backref;
|
||||
ut64 cur = mmo->buf ? mmo->buf->cur : 0;
|
||||
if (mmo->buf) {
|
||||
@ -35,31 +44,10 @@ static int r_io_mmap_refresh_buf(RIOMMapFileObj *mmo) {
|
||||
mmo->buf = NULL;
|
||||
}
|
||||
mmo->buf = r_buf_mmap (mmo->filename, mmo->flags);
|
||||
if (mmo->buf)
|
||||
if (mmo->buf) {
|
||||
r_io_mmap_seek (io, mmo, cur, SEEK_SET);
|
||||
return (mmo->buf ? true : false);
|
||||
}
|
||||
|
||||
RIOMMapFileObj *r_io_mmap_create_new_file(RIO *io, const char *filename, int mode, int flags) {
|
||||
RIOMMapFileObj *mmo = NULL;
|
||||
if (!io)
|
||||
return NULL;
|
||||
|
||||
mmo = R_NEW0 (RIOMMapFileObj);
|
||||
if (!mmo)
|
||||
return NULL;
|
||||
|
||||
mmo->filename = strdup (filename);
|
||||
mmo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api
|
||||
mmo->mode = mode;
|
||||
mmo->flags = flags;
|
||||
mmo->io_backref = io;
|
||||
|
||||
if (!r_io_mmap_refresh_buf (mmo)) {
|
||||
r_io_mmap_free (mmo);
|
||||
mmo = NULL;
|
||||
}
|
||||
return mmo;
|
||||
return mmo->buf != NULL;
|
||||
}
|
||||
|
||||
static void r_io_mmap_free (RIOMMapFileObj *mmo) {
|
||||
@ -69,6 +57,27 @@ static void r_io_mmap_free (RIOMMapFileObj *mmo) {
|
||||
free (mmo);
|
||||
}
|
||||
|
||||
RIOMMapFileObj *r_io_mmap_create_new_file(RIO *io, const char *filename, int mode, int flags) {
|
||||
RIOMMapFileObj *mmo;
|
||||
if (!io) {
|
||||
return NULL;
|
||||
}
|
||||
mmo = R_NEW0 (RIOMMapFileObj);
|
||||
if (!mmo) {
|
||||
return NULL;
|
||||
}
|
||||
mmo->filename = strdup (filename);
|
||||
mmo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api
|
||||
mmo->mode = mode;
|
||||
mmo->flags = flags;
|
||||
mmo->io_backref = io;
|
||||
if (!r_io_mmap_refresh_buf (mmo)) {
|
||||
r_io_mmap_free (mmo);
|
||||
mmo = NULL;
|
||||
}
|
||||
return mmo;
|
||||
}
|
||||
|
||||
static int r_io_mmap_close(RIODesc *fd) {
|
||||
if (!fd || !fd->data) {
|
||||
return -1;
|
||||
@ -102,10 +111,8 @@ static int r_io_mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
if (!io || !fd || !fd->data || !buf) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
mmo = fd->data;
|
||||
addr = io->off;
|
||||
|
||||
if ( !(mmo->flags & R_IO_WRITE)) {
|
||||
return -1;
|
||||
}
|
||||
@ -130,27 +137,6 @@ static RIODesc *r_io_mmap_open(RIO *io, const char *file, int flags, int mode) {
|
||||
return r_io_desc_new (&r_io_plugin_mmap, mmo->fd, mmo->filename, flags, mode, mmo);
|
||||
}
|
||||
|
||||
static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence) {
|
||||
ut64 seek_val = mmo->buf->cur;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
seek_val = (mmo->buf->length < offset) ?
|
||||
mmo->buf->length : offset;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
case SEEK_CUR:
|
||||
seek_val = (mmo->buf->length < (offset + mmo->buf->cur)) ?
|
||||
mmo->buf->length : offset + mmo->buf->cur;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
case SEEK_END:
|
||||
seek_val = mmo->buf->length;
|
||||
mmo->buf->cur = io->off = seek_val;
|
||||
return seek_val;
|
||||
}
|
||||
return seek_val;
|
||||
}
|
||||
|
||||
static ut64 r_io_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
|
||||
RIOMMapFileObj *mmo;
|
||||
if (!fd || !fd->data) {
|
||||
@ -160,26 +146,17 @@ static ut64 r_io_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
|
||||
return r_io_mmap_seek (io, mmo, offset, whence);
|
||||
}
|
||||
|
||||
static int r_io_mmap_truncate(RIOMMapFileObj *mmo, ut64 size) {
|
||||
static bool r_io_mmap_truncate(RIOMMapFileObj *mmo, ut64 size) {
|
||||
int res = r_file_truncate (mmo->filename, size);
|
||||
|
||||
if (res && !r_io_mmap_refresh_buf (mmo)) {
|
||||
eprintf ("r_io_mmap_truncate: Error trying to refresh the mmap'ed file.");
|
||||
res = false;
|
||||
} else if (res) {
|
||||
eprintf ("r_io_mmap_truncate: Error trying to resize the file.");
|
||||
}
|
||||
else if (res) eprintf ("r_io_mmap_truncate: Error trying to resize the file.");
|
||||
return res;
|
||||
}
|
||||
|
||||
static int r_io_mmap_resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
RIOMMapFileObj *mmo;
|
||||
if (!fd || !fd->data) {
|
||||
return -1;
|
||||
}
|
||||
mmo = fd->data;
|
||||
return r_io_mmap_truncate(mmo, size);
|
||||
}
|
||||
|
||||
|
||||
static bool __plugin_open(RIO *io, const char *file, bool many) {
|
||||
return r_io_mmap_check (file);
|
||||
@ -206,8 +183,11 @@ static int __close(RIODesc *fd) {
|
||||
return r_io_mmap_close (fd);
|
||||
}
|
||||
|
||||
static int __resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
return r_io_mmap_resize (io, fd, size);
|
||||
static bool __resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
if (!fd || !fd->data) {
|
||||
return -1;
|
||||
}
|
||||
return r_io_mmap_truncate ((RIOMMapFileObj*)fd->data, size);
|
||||
}
|
||||
|
||||
struct r_io_plugin_t r_io_plugin_mmap = {
|
||||
|
@ -34,13 +34,12 @@ static int debug_os_read_at(int fdn, void *buf, int sz, ut64 addr) {
|
||||
}
|
||||
|
||||
static int __read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int len) {
|
||||
ut64 addr = io->off;
|
||||
memset (buf, '\xff', len); // TODO: only memset the non-readed bytes
|
||||
return debug_os_read_at (RIOPROCPID_FD (fd), buf, len, addr);
|
||||
return debug_os_read_at (RIOPROCPID_FD (fd), buf, len, io->off);
|
||||
}
|
||||
|
||||
static int procpid_write_at(int fd, const ut8 *buf, int sz, ut64 addr) {
|
||||
if ( lseek (fd, addr, 0) < 0) {
|
||||
if (lseek (fd, addr, 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return write (fd, buf, sz);
|
||||
@ -50,7 +49,7 @@ static int __write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int len) {
|
||||
return procpid_write_at (RIOPROCPID_FD (fd), buf, len, io->off);
|
||||
}
|
||||
|
||||
static int __plugin_open(struct r_io_t *io, const char *file, ut8 many) {
|
||||
static bool __plugin_open(struct r_io_t *io, const char *file, bool many) {
|
||||
return (!strncmp (file, "procpid://", 10));
|
||||
}
|
||||
|
||||
|
@ -57,26 +57,33 @@ typedef struct r_io_zfo_t {
|
||||
RIO * io_backref;
|
||||
} RIOZipFileObj;
|
||||
|
||||
static int r_io_zip_realloc_buf(RIOZipFileObj *zfo, int count);
|
||||
static int r_io_zip_truncate_buf(RIOZipFileObj *zfo, int size);
|
||||
static int r_io_zip_slurp_file(RIOZipFileObj *zfo);
|
||||
//static int r_io_zip_check_file(const char *file);
|
||||
RIODesc *check_zip_file_open(RIO *io, const char* filename);
|
||||
RList *r_io_zip_get_files(char *archivename, ut32 flags, int mode, int rw);
|
||||
RIOZipFileObj * r_io_zip_create_new_file(const char *archivename, const char *filename, struct zip_stat *sb, ut32 flags, int mode, int rw);
|
||||
RIOZipFileObj *r_io_zip_alloc_zipfileobj(const char *archive_name, const char *filename, ut32 flags, int mode, int rw);
|
||||
static int r_io_zip_init();
|
||||
static int r_io_zip_has_uri_substr(const char *file);
|
||||
static int r_io_zip_check_uri(const char *file);
|
||||
static int r_io_zip_flush_file(RIOZipFileObj *zfo);
|
||||
static int r_io_zip_read(RIO *io, RIODesc *fd, ut8 *buf, int count);
|
||||
static int r_io_zip_write(RIO *io, RIODesc *fd, const ut8 *buf, int count);
|
||||
static int r_io_zip_close(RIODesc *desc);
|
||||
static char * r_io_zip_get_by_file_idx(const char * archivename, const char *idx, ut32 flags, int mode, int rw);
|
||||
static RIODesc * r_io_zip_open(RIO *io, const char *file, int rw, int mode);
|
||||
static RList *r_io_zip_open_many(RIO *io, const char *file, int rw, int mode);
|
||||
static int r_io_zip_check_uri_many(const char *file);
|
||||
static int r_io_zip_resize(RIO *io, RIODesc *fd, ut64 size);
|
||||
static int r_io_zip_has_uri_substr(const char *file) {
|
||||
return (file && strstr (file, "://"));
|
||||
}
|
||||
|
||||
static int r_io_zip_check_uri_many(const char *file) {
|
||||
int i;
|
||||
if (r_io_zip_has_uri_substr (file)) {
|
||||
for (i = 0; ZIP_ALL_URIS[i].name != NULL; i++) {
|
||||
if (!strncmp (file, ZIP_ALL_URIS[i].name, ZIP_ALL_URIS[i].len) && file[ZIP_ALL_URIS[i].len]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int r_io_zip_check_uri(const char *file) {
|
||||
int i;
|
||||
if (r_io_zip_has_uri_substr (file)) {
|
||||
for (i = 0; ZIP_URIS[i].name != NULL; i++) {
|
||||
if (!strncmp (file, ZIP_URIS[i].name, ZIP_URIS[i].len) && file[ZIP_URIS[i].len]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool r_io_zip_plugin_open(RIO *io, const char *file, bool many) {
|
||||
if (io && file) {
|
||||
@ -88,74 +95,38 @@ static bool r_io_zip_plugin_open(RIO *io, const char *file, bool many) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static int r_io_zip_init(RIO *io) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static int r_io_zip_has_uri_substr(const char *file) {
|
||||
return (file && strstr (file, "://"));
|
||||
}
|
||||
|
||||
static int r_io_zip_check_uri(const char *file) {
|
||||
int res = false;
|
||||
int i = 0;
|
||||
if (r_io_zip_has_uri_substr (file)) {
|
||||
for (i = 0; ZIP_URIS[i].name != NULL; i++) {
|
||||
if (!strncmp (file, ZIP_URIS[i].name, ZIP_URIS[i].len) && file[ZIP_URIS[i].len]) {
|
||||
res = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int r_io_zip_check_uri_many(const char *file) {
|
||||
int res = false;
|
||||
int i = 0;
|
||||
if (r_io_zip_has_uri_substr (file)) {
|
||||
for (i = 0; ZIP_ALL_URIS[i].name != NULL; i++) {
|
||||
if (!strncmp (file, ZIP_ALL_URIS[i].name, ZIP_ALL_URIS[i].len) && file[ZIP_ALL_URIS[i].len]) {
|
||||
res = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
struct zip * r_io_zip_open_archive(const char *archivename, ut32 flags, int mode, int rw) {
|
||||
struct zip *r_io_zip_open_archive(const char *archivename, ut32 flags, int mode, int rw) {
|
||||
struct zip * zipArch = NULL;
|
||||
int zip_errorp;
|
||||
|
||||
if (!archivename)
|
||||
return zipArch;
|
||||
|
||||
zipArch = zip_open (archivename, flags, &zip_errorp);
|
||||
if (!zipArch) {
|
||||
if (zip_errorp == ZIP_ER_INVAL) {
|
||||
eprintf("ZIP File Error: Invalid file name (NULL).\n");
|
||||
} else if (zip_errorp == ZIP_ER_OPEN) {
|
||||
eprintf ("ZIP File Error: File could not be opened file name.\n");
|
||||
} else if (zip_errorp == ZIP_ER_NOENT) {
|
||||
eprintf ("ZIP File Error: File does not exist.\n");
|
||||
} else if (zip_errorp == ZIP_ER_READ) {
|
||||
eprintf ("ZIP File Error: Read error occurred.\n");
|
||||
} else if (zip_errorp == ZIP_ER_NOZIP) {
|
||||
eprintf ("ZIP File Error: File is not a valid ZIP archive.\n");
|
||||
} else if (zip_errorp == ZIP_ER_INCONS) {
|
||||
eprintf ("ZIP File Error: ZIP file had some inconsistencies archive.\n");
|
||||
} else eprintf ("ZIP File Error: Something bad happened, get your debug on.\n");
|
||||
if (!archivename) {
|
||||
return NULL;
|
||||
}
|
||||
return zipArch;
|
||||
if ((zipArch = zip_open (archivename, flags, &zip_errorp))) {
|
||||
return zipArch;
|
||||
}
|
||||
if (zip_errorp == ZIP_ER_INVAL) {
|
||||
eprintf ("ZIP File Error: Invalid file name (NULL).\n");
|
||||
} else if (zip_errorp == ZIP_ER_OPEN) {
|
||||
eprintf ("ZIP File Error: File could not be opened file name.\n");
|
||||
} else if (zip_errorp == ZIP_ER_NOENT) {
|
||||
eprintf ("ZIP File Error: File does not exist.\n");
|
||||
} else if (zip_errorp == ZIP_ER_READ) {
|
||||
eprintf ("ZIP File Error: Read error occurred.\n");
|
||||
} else if (zip_errorp == ZIP_ER_NOZIP) {
|
||||
eprintf ("ZIP File Error: File is not a valid ZIP archive.\n");
|
||||
} else if (zip_errorp == ZIP_ER_INCONS) {
|
||||
eprintf ("ZIP File Error: ZIP file had some inconsistencies archive.\n");
|
||||
} else {
|
||||
eprintf ("ZIP File Error: Something bad happened, get your debug on.\n");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int r_io_zip_slurp_file(RIOZipFileObj *zfo) {
|
||||
bool res = false;
|
||||
struct zip_stat sb;
|
||||
struct zip_file *zFile = NULL;
|
||||
struct zip * zipArch ;
|
||||
|
||||
struct zip *zipArch;
|
||||
struct zip_stat sb;
|
||||
bool res = false;
|
||||
if (!zfo) {
|
||||
return res;
|
||||
}
|
||||
@ -165,11 +136,11 @@ static int r_io_zip_slurp_file(RIOZipFileObj *zfo) {
|
||||
|
||||
if (zipArch && zfo && zfo->entry != -1) {
|
||||
zFile = zip_fopen_index (zipArch, zfo->entry, 0);
|
||||
if (!zfo->b)
|
||||
if (!zfo->b) {
|
||||
zfo->b = r_buf_new ();
|
||||
}
|
||||
zip_stat_init (&sb);
|
||||
if (zFile && zfo->b && !zip_stat_index(zipArch,
|
||||
zfo->entry, 0, &sb) ) {
|
||||
if (zFile && zfo->b && !zip_stat_index (zipArch, zfo->entry, 0, &sb)) {
|
||||
ut8 *buf = malloc (sb.size);
|
||||
memset (buf, 0, sb.size);
|
||||
if (buf) {
|
||||
@ -186,12 +157,11 @@ static int r_io_zip_slurp_file(RIOZipFileObj *zfo) {
|
||||
}
|
||||
|
||||
RList * r_io_zip_get_files(char *archivename, ut32 flags, int mode, int rw) {
|
||||
ut64 num_entries = 0, i = 0;
|
||||
struct zip *zipArch = r_io_zip_open_archive (archivename, flags, mode, rw);
|
||||
ut64 num_entries = 0, i = 0;
|
||||
RList *files = NULL;
|
||||
struct zip_stat sb;
|
||||
char *name;
|
||||
//eprintf("Slurping file");
|
||||
if (zipArch) {
|
||||
files = r_list_newf (free);
|
||||
if (!files) {
|
||||
@ -199,13 +169,12 @@ RList * r_io_zip_get_files(char *archivename, ut32 flags, int mode, int rw) {
|
||||
return NULL;
|
||||
}
|
||||
num_entries = zip_get_num_files (zipArch);
|
||||
|
||||
for (i=0; i < num_entries; i++) {
|
||||
for (i = 0; i < num_entries; i++) {
|
||||
zip_stat_init (&sb);
|
||||
zip_stat_index (zipArch, i, 0, &sb);
|
||||
//eprintf("Comparing %s == %s = %d\n", sb.name, filename, strcmp(sb.name, filename));
|
||||
if ((name = strdup (sb.name)))
|
||||
if ((name = strdup (sb.name))) {
|
||||
r_list_append (files, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
zip_close (zipArch);
|
||||
@ -251,6 +220,51 @@ static void r_io_zip_free_zipfileobj(RIOZipFileObj *zfo) {
|
||||
free (zfo);
|
||||
}
|
||||
|
||||
RIOZipFileObj *r_io_zip_create_new_file(const char *archivename, const char *filename, struct zip_stat *sb, ut32 flags, int mode, int rw) {
|
||||
RIOZipFileObj *zfo = R_NEW0 (RIOZipFileObj);
|
||||
if (zfo) {
|
||||
zfo->b = r_buf_new ();
|
||||
zfo->archivename = strdup (archivename);
|
||||
zfo->name = strdup (sb? sb->name: filename);
|
||||
zfo->entry = sb == NULL ? -1 : sb->index;
|
||||
zfo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api
|
||||
zfo->flags = flags;
|
||||
zfo->mode = mode;
|
||||
zfo->rw = rw;
|
||||
}
|
||||
return zfo;
|
||||
}
|
||||
|
||||
/* The file can be a file in the archive or ::[num]. */
|
||||
RIOZipFileObj* r_io_zip_alloc_zipfileobj(const char *archivename, const char *filename, ut32 flags, int mode, int rw) {
|
||||
RIOZipFileObj *zfo = NULL;
|
||||
ut64 i, num_entries;
|
||||
struct zip_stat sb;
|
||||
struct zip *zipArch = r_io_zip_open_archive (archivename, flags, mode, rw);
|
||||
if (!zipArch) return NULL;
|
||||
num_entries = zip_get_num_files (zipArch);
|
||||
|
||||
for (i = 0; i < num_entries; i++) {
|
||||
zip_stat_init (&sb);
|
||||
zip_stat_index (zipArch, i, 0, &sb);
|
||||
if (sb.name != NULL) {
|
||||
if (strcmp (sb.name, filename) == 0) {
|
||||
zfo = r_io_zip_create_new_file (
|
||||
archivename, filename, &sb,
|
||||
flags, mode, rw);
|
||||
r_io_zip_slurp_file (zfo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!zfo) {
|
||||
zfo = r_io_zip_create_new_file (archivename,
|
||||
filename, NULL, flags, mode, rw);
|
||||
}
|
||||
zip_close (zipArch);
|
||||
return zfo;
|
||||
}
|
||||
|
||||
// Below this line are the r_io_zip plugin APIs
|
||||
static RList *r_io_zip_open_many(RIO *io, const char *file, int rw, int mode) {
|
||||
RList *list_fds = NULL;
|
||||
@ -312,6 +326,34 @@ static RList *r_io_zip_open_many(RIO *io, const char *file, int rw, int mode) {
|
||||
return list_fds;
|
||||
}
|
||||
|
||||
char * r_io_zip_get_by_file_idx(const char * archivename, const char *idx, ut32 flags, int mode, int rw) {
|
||||
char *filename = NULL;
|
||||
ut64 i, num_entries;
|
||||
ut32 file_idx = -1;
|
||||
struct zip_stat sb;
|
||||
struct zip * zipArch = r_io_zip_open_archive (archivename, flags, mode, rw);
|
||||
if (!idx || !zipArch) {
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
num_entries = zip_get_num_files (zipArch);
|
||||
file_idx = atoi (idx);
|
||||
if ((file_idx == 0 && idx[0] != '0') || (file_idx >= num_entries)) {
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
for (i = 0; i < num_entries; i++) {
|
||||
zip_stat_init (&sb);
|
||||
zip_stat_index (zipArch, i, 0, &sb );
|
||||
if (file_idx == i) {
|
||||
filename = strdup (sb.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
|
||||
static RIODesc *r_io_zip_open(RIO *io, const char *file, int rw, int mode) {
|
||||
RIODesc *res = NULL;
|
||||
char *pikaboo;
|
||||
@ -409,7 +451,6 @@ static RIODesc *r_io_zip_open(RIO *io, const char *file, int rw, int mode) {
|
||||
//eprintf("usage: zip:///path/to/archive//filepath\n");
|
||||
files = r_io_zip_get_files (zip_filename, 0, mode, rw);
|
||||
files = r_io_zip_get_files (zip_filename, 0, mode, rw);
|
||||
|
||||
if (files) {
|
||||
ut32 i = 0;
|
||||
r_list_foreach (files, iter, name) {
|
||||
@ -423,7 +464,6 @@ static RIODesc *r_io_zip_open(RIO *io, const char *file, int rw, int mode) {
|
||||
//eprintf("After parsing the given uri: %s\n", file);
|
||||
//eprintf("Zip filename the given uri: %s\n", zip_filename);
|
||||
//eprintf("File in the zip: %s\n", filename_in_zipfile);
|
||||
|
||||
zfo = r_io_zip_alloc_zipfileobj (zip_filename,
|
||||
filename_in_zipfile, ZIP_CREATE, mode, rw);
|
||||
|
||||
@ -489,14 +529,39 @@ static int r_io_zip_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
|
||||
return r_buf_read_at (zfo->b, io->off, buf, count);
|
||||
}
|
||||
|
||||
static int r_io_zip_truncate_buf(RIOZipFileObj *zfo, int size) {
|
||||
if (zfo->b->length < size)
|
||||
return r_io_zip_realloc_buf(zfo, size - zfo->b->length);
|
||||
static int r_io_zip_realloc_buf(RIOZipFileObj *zfo, int count) {
|
||||
int res = false;
|
||||
if (count >= 0 && zfo->b->cur + count > zfo->b->length) {
|
||||
RBuffer *buffer = r_buf_new ();
|
||||
if (!buffer) {
|
||||
return false;
|
||||
}
|
||||
buffer->buf = malloc (zfo->b->cur + count );
|
||||
if (!buffer->buf) {
|
||||
r_buf_free (buffer);
|
||||
return false;
|
||||
}
|
||||
buffer->length = zfo->b->cur + count;
|
||||
memcpy (buffer->buf, zfo->b->buf, zfo->b->length);
|
||||
memset (buffer->buf + zfo->b->length, 0, count);
|
||||
buffer->cur = zfo->b->cur;
|
||||
r_buf_free (zfo->b);
|
||||
zfo->b = buffer;
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
if (size > 0){
|
||||
static bool r_io_zip_truncate_buf(RIOZipFileObj *zfo, int size) {
|
||||
if (zfo->b->length < size) {
|
||||
return r_io_zip_realloc_buf (zfo, size - zfo->b->length);
|
||||
}
|
||||
if (size > 0) {
|
||||
ut8 *buf = malloc (size);
|
||||
if (!buf) return false;
|
||||
memcpy(buf, zfo->b->buf, size);
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
memcpy (buf, zfo->b->buf, size);
|
||||
free (zfo->b->buf);
|
||||
zfo->b->buf = buf;
|
||||
zfo->b->length = size;
|
||||
@ -507,57 +572,36 @@ static int r_io_zip_truncate_buf(RIOZipFileObj *zfo, int size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static int r_io_zip_realloc_buf(RIOZipFileObj *zfo, int count) {
|
||||
int res = false;
|
||||
if (zfo->b->cur + count > zfo->b->length) {
|
||||
RBuffer *buffer = r_buf_new();
|
||||
if (!buffer) return false;
|
||||
buffer->buf = malloc (zfo->b->cur + count );
|
||||
if (!buffer->buf) {
|
||||
r_buf_free(buffer);
|
||||
return false;
|
||||
}
|
||||
buffer->length = zfo->b->cur + count;
|
||||
memcpy (buffer->buf, zfo->b->buf, zfo->b->length);
|
||||
memset (buffer->buf+zfo->b->length, 0, count);
|
||||
buffer->cur = zfo->b->cur;
|
||||
r_buf_free (zfo->b);
|
||||
zfo->b = buffer;
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int r_io_zip_resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
static bool r_io_zip_resize(RIO *io, RIODesc *fd, ut64 size) {
|
||||
RIOZipFileObj *zfo;
|
||||
int res = false;
|
||||
ut64 cur_off = io->off;
|
||||
if (!fd || !fd->data)
|
||||
return -1;
|
||||
if (!fd || !fd->data) {
|
||||
return false;
|
||||
}
|
||||
zfo = fd->data;
|
||||
res = r_io_zip_truncate_buf(zfo, size);
|
||||
if (res == true) {
|
||||
// XXX - Implement a flush of some sort, but until then, lets
|
||||
// just write through
|
||||
if (r_io_zip_truncate_buf (zfo, size)) {
|
||||
zfo->modified = 1;
|
||||
r_io_zip_flush_file (zfo);
|
||||
return true;
|
||||
}
|
||||
io->off = cur_off;
|
||||
return res;
|
||||
return false;
|
||||
}
|
||||
|
||||
static int r_io_zip_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
RIOZipFileObj *zfo;
|
||||
int ret = 0;
|
||||
if ( !fd || !fd->data || !buf)
|
||||
if (!fd || !fd->data || !buf) {
|
||||
return -1;
|
||||
}
|
||||
zfo = fd->data;
|
||||
if ( !(zfo->flags & R_IO_WRITE)) return -1;
|
||||
if (zfo->b->cur + count >= zfo->b->length)
|
||||
if (!(zfo->flags & R_IO_WRITE)) {
|
||||
return -1;
|
||||
}
|
||||
if (zfo->b->cur + count >= zfo->b->length) {
|
||||
r_io_zip_realloc_buf (zfo, count);
|
||||
|
||||
if (zfo->b->length < io->off)
|
||||
}
|
||||
if (zfo->b->length < io->off) {
|
||||
io->off = zfo->b->length;
|
||||
}
|
||||
zfo->modified = 1;
|
||||
ret = r_buf_write_at (zfo->b, io->off, buf, count);
|
||||
// XXX - Implement a flush of some sort, but until then, lets
|
||||
@ -567,99 +611,19 @@ static int r_io_zip_write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
|
||||
}
|
||||
|
||||
static int r_io_zip_close(RIODesc *fd) {
|
||||
RIOZipFileObj *zfo = NULL;
|
||||
//eprintf("Am I called 2x?\n");
|
||||
// this api will be called multiple times :/
|
||||
if (!fd || !fd->data)
|
||||
RIOZipFileObj *zfo;
|
||||
if (!fd || !fd->data) {
|
||||
return -1;
|
||||
}
|
||||
zfo = fd->data;
|
||||
r_io_zip_free_zipfileobj (zfo);
|
||||
zfo = fd->data = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char * r_io_zip_get_by_file_idx(const char * archivename, const char *idx, ut32 flags, int mode, int rw) {
|
||||
char *filename = NULL;
|
||||
ut64 i, num_entries;
|
||||
ut32 file_idx = -1;
|
||||
struct zip_stat sb;
|
||||
struct zip * zipArch = r_io_zip_open_archive (archivename,
|
||||
flags, mode, rw);
|
||||
if (!idx || !zipArch) {
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
|
||||
num_entries = zip_get_num_files (zipArch);
|
||||
// filename starts with ::
|
||||
file_idx = atoi (idx);
|
||||
|
||||
if ((file_idx == 0 && idx[0] != '0') || (file_idx >= num_entries)) {
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
|
||||
for (i=0; i < num_entries; i++) {
|
||||
zip_stat_init (&sb);
|
||||
zip_stat_index (zipArch, i, 0, &sb );
|
||||
//eprintf("Comparing %s == %s = %d\n", sb.name, filename, strcmp(sb.name, filename));
|
||||
// filename starts with ::[number]
|
||||
if (file_idx == i) {
|
||||
filename = strdup (sb.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
zip_close (zipArch);
|
||||
return filename;
|
||||
}
|
||||
|
||||
/* The file can be a file in the archive or ::[num]. */
|
||||
RIOZipFileObj* r_io_zip_alloc_zipfileobj(const char *archivename, const char *filename, ut32 flags, int mode, int rw) {
|
||||
RIOZipFileObj *zfo = NULL;
|
||||
ut64 i, num_entries;
|
||||
struct zip_stat sb;
|
||||
struct zip *zipArch = r_io_zip_open_archive (archivename, flags, mode, rw);
|
||||
if (!zipArch) return NULL;
|
||||
num_entries = zip_get_num_files (zipArch);
|
||||
|
||||
for (i=0; i < num_entries; i++) {
|
||||
zip_stat_init (&sb);
|
||||
zip_stat_index (zipArch, i, 0, &sb);
|
||||
if (sb.name != NULL) {
|
||||
if (strcmp (sb.name, filename) == 0) {
|
||||
zfo = r_io_zip_create_new_file (
|
||||
archivename, filename, &sb,
|
||||
flags, mode, rw);
|
||||
r_io_zip_slurp_file (zfo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!zfo)
|
||||
zfo = r_io_zip_create_new_file (archivename,
|
||||
filename, NULL, flags, mode, rw);
|
||||
zip_close (zipArch);
|
||||
return zfo;
|
||||
}
|
||||
|
||||
RIOZipFileObj *r_io_zip_create_new_file(const char *archivename, const char *filename, struct zip_stat *sb, ut32 flags, int mode, int rw) {
|
||||
RIOZipFileObj *zfo = R_NEW0 (RIOZipFileObj);
|
||||
if (zfo) {
|
||||
zfo->b = r_buf_new ();
|
||||
zfo->archivename = strdup (archivename);
|
||||
zfo->name = strdup (sb? sb->name: filename);
|
||||
zfo->entry = sb == NULL ? -1 : sb->index;
|
||||
zfo->fd = r_num_rand (0xFFFF); // XXX: Use r_io_fd api
|
||||
zfo->flags = flags;
|
||||
zfo->mode = mode;
|
||||
zfo->rw = rw;
|
||||
}
|
||||
return zfo;
|
||||
}
|
||||
|
||||
RIOPlugin r_io_plugin_zip = {
|
||||
.name = "zip",
|
||||
.desc = "Open zip files apk://foo.apk//MANIFEST or zip://foo.apk//theclass/fun.class, show files with: zip://foo.apk/, open all files with zipall://",
|
||||
.desc = "Open zip files [apk|ipa|zip|zipall]://[file//path]",
|
||||
.license = "BSD",
|
||||
.open = r_io_zip_open,
|
||||
.open_many = r_io_zip_open_many,
|
||||
@ -669,7 +633,6 @@ RIOPlugin r_io_plugin_zip = {
|
||||
.lseek = r_io_zip_lseek,
|
||||
.check = r_io_zip_plugin_open,
|
||||
.resize = r_io_zip_resize,
|
||||
.init = r_io_zip_init
|
||||
};
|
||||
|
||||
#ifndef CORELIB
|
||||
|
@ -15,20 +15,23 @@
|
||||
|
||||
R_API bool r_file_truncate (const char *filename, ut64 newsize) {
|
||||
int fd;
|
||||
if (r_file_is_directory (filename))
|
||||
return R_FALSE;
|
||||
if (!r_file_exists (filename) || !r_file_is_regular (filename))
|
||||
return R_FALSE;
|
||||
if (r_file_is_directory (filename)) {
|
||||
return false;
|
||||
}
|
||||
if (!r_file_exists (filename) || !r_file_is_regular (filename)) {
|
||||
return false;
|
||||
}
|
||||
#if __WINDOWS__
|
||||
fd = r_sandbox_open (filename, O_RDWR, 0644);
|
||||
#else
|
||||
fd = r_sandbox_open (filename, O_RDWR|O_SYNC, 0644);
|
||||
#endif
|
||||
if (fd == -1)
|
||||
return R_FALSE;
|
||||
if (fd == -1) {
|
||||
return false;
|
||||
}
|
||||
ftruncate (fd, newsize);
|
||||
close (fd);
|
||||
return R_TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -67,22 +70,21 @@ R_API char *r_file_dirname (const char *path) {
|
||||
|
||||
R_API bool r_file_is_regular(const char *str) {
|
||||
struct stat buf = {0};
|
||||
if (!str||!*str)
|
||||
return R_FALSE;
|
||||
if (stat (str, &buf)==-1)
|
||||
return R_FALSE;
|
||||
return ((S_IFREG & buf.st_mode)==S_IFREG)? R_TRUE: R_FALSE;
|
||||
if (!str || !*str || stat (str, &buf) == -1) {
|
||||
return false;
|
||||
}
|
||||
return ((S_IFREG & buf.st_mode)==S_IFREG)? true: false;
|
||||
}
|
||||
|
||||
R_API bool r_file_is_directory(const char *str) {
|
||||
struct stat buf = {0};
|
||||
if (!str||!*str)
|
||||
return R_FALSE;
|
||||
return false;
|
||||
if (stat (str, &buf)==-1)
|
||||
return R_FALSE;
|
||||
return false;
|
||||
if ((S_IFBLK & buf.st_mode) == S_IFBLK)
|
||||
return R_FALSE;
|
||||
return (S_IFDIR==(S_IFDIR & buf.st_mode))? R_TRUE: R_FALSE;
|
||||
return false;
|
||||
return (S_IFDIR==(S_IFDIR & buf.st_mode))? true: false;
|
||||
}
|
||||
|
||||
R_API bool r_file_fexists(const char *fmt, ...) {
|
||||
@ -99,8 +101,8 @@ R_API bool r_file_fexists(const char *fmt, ...) {
|
||||
R_API bool r_file_exists(const char *str) {
|
||||
struct stat buf = {0};
|
||||
if (str && *str && stat (str, &buf)==-1)
|
||||
return R_FALSE;
|
||||
return (S_ISREG (buf.st_mode))? R_TRUE: R_FALSE;
|
||||
return false;
|
||||
return (S_ISREG (buf.st_mode))? true: false;
|
||||
}
|
||||
|
||||
R_API long r_file_proc_size(FILE *fd) {
|
||||
@ -413,11 +415,12 @@ R_API char *r_file_slurp_line(const char *file, int line, int context) {
|
||||
if (str[i]=='\n')
|
||||
lines--;
|
||||
ptr = str+i;
|
||||
for (i=0; ptr[i]; i++)
|
||||
for (i=0; ptr[i]; i++) {
|
||||
if (ptr[i]=='\n') {
|
||||
ptr[i]='\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
ptr = strdup (ptr);
|
||||
free (str);
|
||||
}
|
||||
@ -465,18 +468,20 @@ R_API bool r_file_dump(const char *file, const ut8 *buf, int len, int append) {
|
||||
}
|
||||
|
||||
R_API bool r_file_rm(const char *file) {
|
||||
if (r_sandbox_enable (0)) return R_FALSE;
|
||||
if (r_sandbox_enable (0)) {
|
||||
return false;
|
||||
}
|
||||
if (r_file_is_directory (file)) {
|
||||
#if __WINDOWS__
|
||||
return (RemoveDirectory (file)==0)? R_TRUE: R_FALSE;
|
||||
return !RemoveDirectory (file);
|
||||
#else
|
||||
return (rmdir (file)==0)? R_TRUE: R_FALSE;
|
||||
return !rmdir (file);
|
||||
#endif
|
||||
} else {
|
||||
#if __WINDOWS__
|
||||
return (DeleteFile (file)==0)? R_TRUE: R_FALSE;
|
||||
return !DeleteFile (file);
|
||||
#else
|
||||
return (unlink (file)==0)? R_TRUE: R_FALSE;
|
||||
return !unlink (file);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user