[PATCH v2 11/28] vsprintf: Lift pr_hex_bytes() out from hex_string()

From: Kent Overstreet
Date: Thu May 19 2022 - 13:26:22 EST


This factors pr_hex_bytes(), a new printbuf-style helper, out from
hex_string and adds it to pretty-printers.c.

Signed-off-by: Kent Overstreet <kent.overstreet@xxxxxxxxx>
---
include/linux/pretty-printers.h | 1 +
lib/pretty-printers.c | 23 +++++++++++++++++++++++
lib/vsprintf.c | 13 ++++---------
3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/include/linux/pretty-printers.h b/include/linux/pretty-printers.h
index 2e8b6b4426..ded34622e8 100644
--- a/include/linux/pretty-printers.h
+++ b/include/linux/pretty-printers.h
@@ -4,6 +4,7 @@
#ifndef _LINUX_PRETTY_PRINTERS_H
#define _LINUX_PRETTY_PRINTERS_H

+void pr_hex_bytes(struct printbuf *, const u8 *, unsigned, unsigned);
void pr_string_option(struct printbuf *, const char * const[], size_t);
void pr_bitflags(struct printbuf *, const char * const[], u64);

diff --git a/lib/pretty-printers.c b/lib/pretty-printers.c
index d794648ef9..162e6865f9 100644
--- a/lib/pretty-printers.c
+++ b/lib/pretty-printers.c
@@ -4,6 +4,29 @@
#include <linux/kernel.h>
#include <linux/printbuf.h>

+/**
+ * pr_hex_bytes - Print a string of hex bytes, with optional separator
+ *
+ * @out: The printbuf to output to
+ * @addr: Buffer to print
+ * @nr: Number of bytes to print
+ * @separator: Optional separator character between each byte
+ */
+void pr_hex_bytes(struct printbuf *out, const u8 *addr,
+ unsigned nr, unsigned separator)
+{
+ unsigned i;
+
+ for (i = 0; i < nr; ++i) {
+ if (separator && i)
+ pr_char(out, separator);
+ pr_hex_byte(out, addr[i]);
+ }
+
+ printbuf_nul_terminate(out);
+}
+EXPORT_SYMBOL(pr_hex_bytes);
+
/**
* pr_string_option - Given a list of strings, print out the list and indicate
* which option is selected, with square brackets (sysfs style)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 3f5638d27a..d4293b4a40 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -52,6 +52,7 @@
#include <asm/byteorder.h> /* cpu_to_le16 */

#include <linux/string_helpers.h>
+#include <linux/pretty-printers.h>
#include "kstrtox.h"

static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base)
@@ -1107,10 +1108,10 @@ void resource_string(struct printbuf *out, struct resource *res,
}

static noinline_for_stack
-void hex_string(struct printbuf *out, u8 *addr,
+void hex_string(struct printbuf *out, const u8 *addr,
struct printf_spec spec, const char *fmt)
{
- int i, len = 1; /* if we pass '%ph[CDN]', field width remains
+ int len = 1; /* if we pass '%ph[CDN]', field width remains
negative value, fallback to the default */
char separator;

@@ -1139,13 +1140,7 @@ void hex_string(struct printbuf *out, u8 *addr,
if (spec.field_width > 0)
len = min_t(int, spec.field_width, 64);

- for (i = 0; i < len; ++i) {
- __pr_char(out, hex_asc_hi(addr[i]));
- __pr_char(out, hex_asc_lo(addr[i]));
-
- if (separator && i != len - 1)
- __pr_char(out, separator);
- }
+ pr_hex_bytes(out, addr, len, separator);
}

static noinline_for_stack
--
2.36.0