From 3f066cdd2eb24db34a8332acb653b06ed5c3aed1 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 23 Oct 2018 21:44:38 +0300 Subject: memview: add memory write feature --- src/memview.c | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/memview.c b/src/memview.c index b326efd..c05b10c 100644 --- a/src/memview.c +++ b/src/memview.c @@ -161,6 +161,20 @@ static struct { struct mem_io io; } ctx = { .hexview.octects_per_group = 1 }; +static const char* +basename(const char *path) +{ + const char *base = strrchr(path, '/'); + return (base ? base + 1 : path); +} + +static const char* +skip_ws(const char *str) +{ + const size_t skipped = strspn(str, " \t\n"); + return str + skipped; +} + const char* hex_for_byte(unsigned char byte) { @@ -721,6 +735,31 @@ goto_offset(void *arg) } } +static void +write_bytes(void *arg) +{ + (void)arg; + const char *v; + if (!(v = input("bytes"))) + return; + + unsigned char bytes[255] = {0}, i = 0; + for (const char *s = skip_ws(v); i < sizeof(bytes) && *s; s += 2, s += !!isspace(*s)) { + unsigned int x; + if (!sscanf(s, "%2x", &x)) { + error("invalid input `%s`", v); + return; + } + if (i >= sizeof(bytes)) { + error("input is too long, sorry :( (max 255 bytes)"); + return; + } + bytes[i++] = x; + } + + ctx.io.write(&ctx.io, bytes, ctx.hexview.offset, i); +} + static void quit(void) { @@ -773,20 +812,6 @@ init(void) ); } -static const char* -basename(const char *path) -{ - const char *base = strrchr(path, '/'); - return (base ? base + 1 : path); -} - -static const char* -skip_ws(const char *str) -{ - const size_t skipped = strspn(str, " \t\n"); - return str + skipped; -} - static void region_cb(const char *line, void *data) { -- cgit v1.2.3