summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2022-05-10 00:04:48 -0400
committerKent Overstreet <kent.overstreet@gmail.com>2022-05-20 13:54:47 -0400
commit86e99754a2f4969866da029b495cd631dd6aab3f (patch)
treecc658da592f222a250fe5b775af8e5123124ce79
parent047e8d63d128867316ed31f44f26dc60bc520108 (diff)
vsprintf: Lift pr_hex_bytes() out from hex_string()
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@gmail.com>
-rw-r--r--include/linux/pretty-printers.h1
-rw-r--r--lib/pretty-printers.c23
-rw-r--r--lib/vsprintf.c13
3 files changed, 28 insertions, 9 deletions
diff --git a/include/linux/pretty-printers.h b/include/linux/pretty-printers.h
index 2e8b6b4426d2..ded34622e80c 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 d794648ef913..162e6865f989 100644
--- a/lib/pretty-printers.c
+++ b/lib/pretty-printers.c
@@ -5,6 +5,29 @@
#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 3f5638d27a19..d4293b4a4005 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