Skip to content

Commit

Permalink
Give local seek to iobfd
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 6, 2024
1 parent f060f50 commit d2a03c5
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions libr/io/p/io_bfdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef struct {
int fd;
ut8 *buf;
ut32 size;
ut64 seek;
BfvmCPU *bfvm;
} RIOBfdbg;

Expand Down Expand Up @@ -36,41 +37,45 @@ static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
}
riom = fd->data;
/* data base buffer */
if (is_in_base (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->base;
if (is_in_base (riom->seek, riom->bfvm)) {
int n = riom->seek-riom->bfvm->base;
if (n > count) {
count = n;
}
memcpy (riom->bfvm->mem+n, buf, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* screen buffer */
if (is_in_screen (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->screen;
if (is_in_screen (riom->seek, riom->bfvm)) {
int n = riom->seek-riom->bfvm->screen;
if (n > count) {
count = riom->bfvm->screen_size - n;
}
memcpy (riom->bfvm->screen_buf+n, buf, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* input buffer */
if (is_in_input (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->input;
if (is_in_input (riom->seek, riom->bfvm)) {
int n = riom->seek-riom->bfvm->input;
if (n > count) {
count = riom->bfvm->input_size - n;
}
memcpy (riom->bfvm->input_buf+n, buf, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* read from file */
sz = RIOBFDBG_SZ (fd);
if (io->off + count >= sz) {
count = sz - io->off;
if (riom->seek + count >= sz) {
count = sz - riom->seek;
}
if (io->off >= sz) {
if (riom->seek >= sz) {
return -1;
}
memcpy (RIOBFDBG_BUF (fd)+io->off, buf, count);
memcpy (RIOBFDBG_BUF (fd)+riom->seek, buf, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}

Expand All @@ -82,41 +87,45 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
}
riom = fd->data;
/* data base buffer */
if (is_in_base (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->base;
if (is_in_base (riom->seek, riom->bfvm)) {
int n = riom->seek - riom->bfvm->base;
if (n > count) {
count = n;
}
memcpy (buf, riom->bfvm->mem+n, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* screen buffer */
if (is_in_screen (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->screen;
if (is_in_screen (riom->seek, riom->bfvm)) {
int n = riom->seek-riom->bfvm->screen;
if (n > count) {
count = riom->bfvm->screen_size - n;
}
memcpy (buf, riom->bfvm->screen_buf+n, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* input buffer */
if (is_in_input (io->off, riom->bfvm)) {
int n = io->off-riom->bfvm->input;
if (is_in_input (riom->seek, riom->bfvm)) {
int n = riom->seek-riom->bfvm->input;
if (n > count) {
count = riom->bfvm->input_size - n;
}
memcpy (buf, riom->bfvm->input_buf+n, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}
/* read from file */
sz = RIOBFDBG_SZ (fd);
if (io->off + count >= sz) {
count = sz - io->off;
if (riom->seek + count >= sz) {
count = sz - riom->seek;
}
if (io->off >= sz) {
if (riom->seek >= sz) {
return -1;
}
memcpy (buf, RIOBFDBG_BUF (fd)+io->off, count);
memcpy (buf, RIOBFDBG_BUF (fd)+riom->seek, count);
riom->seek = R_MIN (count + riom->seek, riom->size);
return count;
}

Expand All @@ -132,12 +141,19 @@ static bool __close(RIODesc *fd) {
}

static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
RIOBfdbg *d = fd->data;
switch (whence) {
case SEEK_SET: return offset;
case SEEK_CUR: return io->off + offset;
case SEEK_END: return RIOBFDBG_SZ (fd);
case R_IO_SEEK_SET:
d->seek = R_MIN (offset, d->size);
break;
case R_IO_SEEK_CUR:
d->seek = R_MIN (d->seek + offset, d->size);
break;
case R_IO_SEEK_END:
d->seek = d->size;
break;
}
return offset;
return d->seek;
}

static bool __plugin_open(RIO *io, const char *pathname, bool many) {
Expand Down

0 comments on commit d2a03c5

Please sign in to comment.