From nobody Tue Feb 10 06:26:23 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600693558; cv=none; d=zohomail.com; s=zohoarc; b=D2Eg653vKIpXrMDHTvjqU2UgijgBQns7KiJmXieLOGLsMCw1/Bdj9HxqEc8LRe2v7bh06ibbbz5MtHz48YcQgIu8IKcRfwIJFKVRuK1ILn+GUUmewWRhJf+QtDY0qJkM1qj9xcRricDcMubrTOneKbuZOOhfkEwOGEGBbAn4d2A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600693558; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=le/AFLjMKIUwwPTP9MPoIbrqeSSlGtYNtRO3yJzdnUk=; b=IZX9wQ9J40qzC7grgQTBTFPF/w2MV230DshzQkVcuZcHzcCzjT3V+v4DJJ/dhcxuKYEwYWEOJRqCA8U36iRetQcyWgnYSoGVgR07h4cLJqThw2ecu5x+e+IR/Qyb5E5BrFxRhrIfi+WxsZn8MBn0kc8YnvFvR52aQIDdY3Kde/I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1600693558567728.4445431959028; Mon, 21 Sep 2020 06:05:58 -0700 (PDT) Received: from localhost ([::1]:43198 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKLW1-0006Qi-3F for importer@patchew.org; Mon, 21 Sep 2020 09:05:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41804) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKLUw-00057F-4T for qemu-devel@nongnu.org; Mon, 21 Sep 2020 09:04:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47436) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKLUt-0006di-UJ for qemu-devel@nongnu.org; Mon, 21 Sep 2020 09:04:49 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-117-79uqxJWVMcqD9bEyzNCSNw-1; Mon, 21 Sep 2020 09:04:44 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2B6C48B1275 for ; Mon, 21 Sep 2020 13:04:35 +0000 (UTC) Received: from thinkpad.redhat.com (ovpn-113-15.ams2.redhat.com [10.36.113.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6CC92838D9; Mon, 21 Sep 2020 13:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600693486; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=le/AFLjMKIUwwPTP9MPoIbrqeSSlGtYNtRO3yJzdnUk=; b=VT/4DRPHAgHvJhiSpCTtmD7l9qlXlGpdGFWGciUoQGplJCJO0ZmdfssMsi28MhoCwjjn79 gJODapJYc04oDVTpUKBW6wCnGpHnZsJ7QPIobENzjyYamIyCwP0FZCPAOgHGZFPwN/cEi3 ny018NgfyoEu6Ja3fTSuJolwr8cikv0= X-MC-Unique: 79uqxJWVMcqD9bEyzNCSNw-1 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PATCH v2 1/2] util/hexdump: introduce qemu_hexdump_line() Date: Mon, 21 Sep 2020 15:04:05 +0200 Message-Id: <20200921130406.941363-2-lvivier@redhat.com> In-Reply-To: <20200921130406.941363-1-lvivier@redhat.com> References: <20200921130406.941363-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=lvivier@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=lvivier@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/21 01:43:11 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Jason Wang , Cindy Lu , Stefan Hajnoczi , "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Dumping one line of hexadecimal/ASCII from a buffer is often needed. Move this part from qemu_hexdump() and use it Acked-by: Stefan Hajnoczi Signed-off-by: Laurent Vivier --- include/qemu-common.h | 8 +++++++ util/hexdump.c | 54 +++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/include/qemu-common.h b/include/qemu-common.h index 9cfd62669bf8..cc902b690db3 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -134,6 +134,14 @@ void os_setup_early_signal_handling(void); char *os_find_datadir(void); int os_parse_cmd_args(int index, const char *optarg); =20 +/* + * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer + */ +#define QEMU_HEXDUMP_LINE_BYTES 16 /* Number of bytes to dump */ +#define QEMU_HEXDUMP_LINE_LEN 75 /* Number of characters in line */ +void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr, + unsigned int len, bool ascii); + /* * Hexdump a buffer to a file. An optional string prefix is added to every= line */ diff --git a/util/hexdump.c b/util/hexdump.c index 0b4662e701d8..2c105a884620 100644 --- a/util/hexdump.c +++ b/util/hexdump.c @@ -16,36 +16,50 @@ #include "qemu/osdep.h" #include "qemu-common.h" =20 -void qemu_hexdump(FILE *fp, const char *prefix, - const void *bufptr, size_t size) +void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr, + unsigned int len, bool ascii) { const char *buf =3D bufptr; - unsigned int b, len, i, c; + int i, c; =20 - for (b =3D 0; b < size; b +=3D 16) { - len =3D size - b; - if (len > 16) { - len =3D 16; + if (len > QEMU_HEXDUMP_LINE_BYTES) { + len =3D QEMU_HEXDUMP_LINE_BYTES; + } + + line +=3D snprintf(line, 6, "%04x:", b); + for (i =3D 0; i < QEMU_HEXDUMP_LINE_BYTES; i++) { + if ((i % 4) =3D=3D 0) { + *line++ =3D ' '; } - fprintf(fp, "%s: %04x:", prefix, b); - for (i =3D 0; i < 16; i++) { - if ((i % 4) =3D=3D 0) { - fprintf(fp, " "); - } - if (i < len) { - fprintf(fp, " %02x", (unsigned char)buf[b + i]); - } else { - fprintf(fp, " "); - } + if (i < len) { + line +=3D sprintf(line, " %02x", (unsigned char)buf[b + i]); + } else { + line +=3D sprintf(line, " "); } - fprintf(fp, " "); + } + if (ascii) { + *line++ =3D ' '; for (i =3D 0; i < len; i++) { c =3D buf[b + i]; if (c < ' ' || c > '~') { c =3D '.'; } - fprintf(fp, "%c", c); + *line++ =3D c; } - fprintf(fp, "\n"); } + *line =3D '\0'; +} + +void qemu_hexdump(FILE *fp, const char *prefix, + const void *bufptr, size_t size) +{ + unsigned int b, len; + char line[QEMU_HEXDUMP_LINE_LEN]; + + for (b =3D 0; b < size; b +=3D QEMU_HEXDUMP_LINE_BYTES) { + len =3D size - b; + qemu_hexdump_line(line, b, bufptr, len, true); + fprintf(fp, "%s: %s\n", prefix, line); + } + } --=20 2.26.2