From patchwork Thu Aug 29 13:37:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laszlo Ersek X-Patchwork-Id: 270829 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AD34E2C00B3 for ; Thu, 29 Aug 2013 23:42:39 +1000 (EST) Received: from localhost ([::1]:43422 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VF2Uf-0007Sl-Au for incoming@patchwork.ozlabs.org; Thu, 29 Aug 2013 09:42:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49219) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VF2UE-0007SR-M9 for qemu-devel@nongnu.org; Thu, 29 Aug 2013 09:42:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VF2U5-0001xY-Jv for qemu-devel@nongnu.org; Thu, 29 Aug 2013 09:42:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3442) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VF2U5-0001x3-Bf for qemu-devel@nongnu.org; Thu, 29 Aug 2013 09:42:01 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7TDfxdG016838 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 29 Aug 2013 09:42:00 -0400 Received: from lacos-laptop.usersys.redhat.com (vpn1-6-145.ams2.redhat.com [10.36.6.145]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r7TDZL5s006869; Thu, 29 Aug 2013 09:35:22 -0400 From: Laszlo Ersek To: qemu-devel@nongnu.org, pbonzini@redhat.com Date: Thu, 29 Aug 2013 15:37:49 +0200 Message-Id: <1377783470-8981-1-git-send-email-lersek@redhat.com> In-Reply-To: <521F4E76.2090507@redhat.com> References: <521F4E76.2090507@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] qemu_hexstr(): hexdump a small buffer to a string, for in-line printing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This function should primarily serve tracing needs. Signed-off-by: Laszlo Ersek --- 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 *@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 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; +}