[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 1/2] qemu_hexstr(): hexdump a small buffer to a stri
From: |
Laszlo Ersek |
Subject: |
[Qemu-devel] [PATCH 1/2] qemu_hexstr(): hexdump a small buffer to a string, for in-line printing |
Date: |
Thu, 29 Aug 2013 15:37:49 +0200 |
This function should primarily serve tracing needs.
Signed-off-by: Laszlo Ersek <address@hidden>
---
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
+ * be formatted.
+ * If @str_len is non-NULL, then the length of the output string (excluding the
+ * terminating NUL) will be stored in address@hidden
+ * 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;
+}
--
1.7.1