From patchwork Thu Oct 7 08:21:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 12541281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D53ADC4167E for ; Thu, 7 Oct 2021 08:22:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BDD2861262 for ; Thu, 7 Oct 2021 08:22:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240659AbhJGIX6 (ORCPT ); Thu, 7 Oct 2021 04:23:58 -0400 Received: from mga14.intel.com ([192.55.52.115]:1555 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240623AbhJGIX4 (ORCPT ); Thu, 7 Oct 2021 04:23:56 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10129"; a="226507649" X-IronPort-AV: E=Sophos;i="5.85,354,1624345200"; d="scan'208";a="226507649" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2021 01:21:56 -0700 X-IronPort-AV: E=Sophos;i="5.85,354,1624345200"; d="scan'208";a="568555110" Received: from abishekh-mobl.amr.corp.intel.com (HELO vverma7-desk.amr.corp.intel.com) ([10.251.133.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Oct 2021 01:21:56 -0700 From: Vishal Verma To: Cc: Dan Williams , Ben Widawsky , , Vishal Verma Subject: [ndctl PATCH v4 09/17] util/hexdump: Add a util helper to print a buffer in hex Date: Thu, 7 Oct 2021 02:21:31 -0600 Message-Id: <20211007082139.3088615-10-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211007082139.3088615-1-vishal.l.verma@intel.com> References: <20211007082139.3088615-1-vishal.l.verma@intel.com> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2591; h=from:subject; bh=UFfhkygRZQ5b+SUkgXgsoSOEW8sH9Qk7pdyP6rLtMe8=; b=owGbwMvMwCHGf25diOft7jLG02pJDIlx6zgcyp8YWx8sWBF5zuK9mPiZ7RmvPllzXIpPnbWhu7Tz g5BkRykLgxgHg6yYIsvfPR8Zj8ltz+cJTHCEmcPKBDKEgYtTACbieJzhD19xfPskzeWvLzH36/0+a9 l6IOqVYrFh9jS7qauP/dpispjhf1Xho7Wba+v1cx4k5lYqHHqyu/2iZPv6q6tF1jbwVif84wAA X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org In preparation for tests that may need to set, retrieve, and display opaque data, add a hexdump function in util/ Cc: Ben Widawsky Cc: Dan Williams Signed-off-by: Vishal Verma Reviewed-by: Dan Williams --- util/hexdump.h | 8 ++++++++ util/hexdump.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 util/hexdump.h create mode 100644 util/hexdump.c diff --git a/util/hexdump.h b/util/hexdump.h new file mode 100644 index 0000000..d322b6a --- /dev/null +++ b/util/hexdump.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2021 Intel Corporation. All rights reserved. */ +#ifndef _UTIL_HEXDUMP_H_ +#define _UTIL_HEXDUMP_H_ + +void hex_dump_buf(unsigned char *buf, int size); + +#endif /* _UTIL_HEXDUMP_H_*/ diff --git a/util/hexdump.c b/util/hexdump.c new file mode 100644 index 0000000..1ab0118 --- /dev/null +++ b/util/hexdump.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2015-2021 Intel Corporation. All rights reserved. */ +#include +#include + +static void print_separator(int len) +{ + int i; + + for (i = 0; i < len; i++) + fprintf(stderr, "-"); + fprintf(stderr, "\n"); +} + +void hex_dump_buf(unsigned char *buf, int size) +{ + int i; + const int grp = 4; /* Number of bytes in a group */ + const int wid = 16; /* Bytes per line. Should be a multiple of grp */ + char ascii[wid + 1]; + + /* Generate header */ + print_separator((wid * 4) + (wid / grp) + 12); + + fprintf(stderr, "Offset "); + for (i = 0; i < wid; i++) { + if (i % grp == 0) fprintf(stderr, " "); + fprintf(stderr, "%02x ", i); + } + fprintf(stderr, " Ascii\n"); + + print_separator((wid * 4) + (wid / grp) + 12); + + /* Generate hex dump */ + for (i = 0; i < size; i++) { + if (i % wid == 0) fprintf(stderr, "%08x ", i); + ascii[i % wid] = + ((buf[i] >= ' ') && (buf[i] <= '~')) ? buf[i] : '.'; + if (i % grp == 0) fprintf(stderr, " "); + fprintf(stderr, "%02x ", buf[i]); + if ((i == size - 1) && (size % wid != 0)) { + int j; + int done = size % wid; + int grps_done = (done / grp) + ((done % grp) ? 1 : 0); + int spaces = wid / grp - grps_done + ((wid - done) * 3); + + for (j = 0; j < spaces; j++) fprintf(stderr, " "); + } + if ((i % wid == wid - 1) || (i == size - 1)) + fprintf(stderr, " %.*s\n", (i % wid) + 1, ascii); + } + print_separator((wid * 4) + (wid / grp) + 12); +}