Message ID | 1377783470-8981-1-git-send-email-lersek@redhat.com |
---|---|
State | New |
Headers | show |
Laszlo Ersek <lersek@redhat.com> writes: > This function should primarily serve tracing needs. > > Signed-off-by: Laszlo Ersek <lersek@redhat.com> > --- > include/qemu-common.h | 11 +++++++++++ > util/hexdump.c | 20 ++++++++++++++++++++ > 2 files changed, 31 insertions(+), 0 deletions(-) > > diff --git a/include/qemu-common.h b/include/qemu-common.h > index 6948bb9..18a373f 100644 > --- a/include/qemu-common.h > +++ b/include/qemu-common.h > @@ -433,6 +433,17 @@ int mod_utf8_codepoint(const char *s, size_t n, char **end); > > void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size); > > +/* > + * Hexdump @size bytes from @buf to a dynamically allocated string. > + * If @size is NULL, then @buf can be a NULL pointer, and an empty string will If @size is zero > + * be formatted. > + * If @str_len is non-NULL, then the length of the output string (excluding the > + * terminating NUL) will be stored in *@str_len. > + * The output is meant to be printed on a single line, hence it contains no > + * newlines, and @size should be reasonable. > + */ > +char *qemu_hexstr(const void *buf, size_t size, size_t *str_len); > + > /* vector definitions */ > #ifdef __ALTIVEC__ > #include <altivec.h> [...]
diff --git a/include/qemu-common.h b/include/qemu-common.h index 6948bb9..18a373f 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -433,6 +433,17 @@ int mod_utf8_codepoint(const char *s, size_t n, char **end); void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size); +/* + * Hexdump @size bytes from @buf to a dynamically allocated string. + * If @size is NULL, then @buf can be a NULL pointer, and an empty string will + * be formatted. + * If @str_len is non-NULL, then the length of the output string (excluding the + * terminating NUL) will be stored in *@str_len. + * The output is meant to be printed on a single line, hence it contains no + * newlines, and @size should be reasonable. + */ +char *qemu_hexstr(const void *buf, size_t size, size_t *str_len); + /* vector definitions */ #ifdef __ALTIVEC__ #include <altivec.h> diff --git a/util/hexdump.c b/util/hexdump.c index 969b340..5c7e1a2 100644 --- a/util/hexdump.c +++ b/util/hexdump.c @@ -35,3 +35,23 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) fprintf(fp, "\n"); } } + +char *qemu_hexstr(const void *buf, size_t size, size_t *str_len) +{ + size_t str_size, i; + char *ret, *output; + + str_size = size * 3 + (size == 0); + ret = g_malloc(str_size); + output = ret; + + for (i = 0; i < size; ++i) { + output += sprintf(output, "%s%02x", (i == 0) ? "" : " ", + ((uint8_t *)buf)[i]); + } + *output = '\0'; + if (str_len != NULL) { + *str_len = str_size - 1; + } + return ret; +}
This function should primarily serve tracing needs. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- include/qemu-common.h | 11 +++++++++++ util/hexdump.c | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 0 deletions(-)