From 26a66d708c6caef09912245c93371ec96c1e0cbd Mon Sep 17 00:00:00 2001
From: Jean Delvare <jdelvare@suse.de>
Date: Mon, 23 Mar 2020 16:47:44 +0100
Subject: [PATCH 08/23] dmidecode: Refactor ASCII filtering of DMI strings
Split dmi_string into 3 functions to make it more modular. ASCII
filtering is an explicit option now to give the caller more control.
Use the new functions in dmi_dump to avoid reimplementing the ASCII
filtering and printing characters one by one.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
dmidecode.c | 60 ++++++++++++++++++++++++++++-------------------------
1 file changed, 32 insertions(+), 28 deletions(-)
diff --git a/dmidecode.c b/dmidecode.c
index 50fd0bffa26d..8ec1d6b9597a 100644
--- a/dmidecode.c
+++ b/dmidecode.c
@@ -109,13 +109,19 @@ int is_printable(const u8 *data, int len)
return 1;
}
-const char *dmi_string(const struct dmi_header *dm, u8 s)
+/* Replace non-ASCII characters with dots */
+static void ascii_filter(char *bp, size_t len)
{
- char *bp = (char *)dm->data;
- size_t i, len;
+ size_t i;
- if (s == 0)
- return "Not Specified";
+ for (i = 0; i < len; i++)
+ if (bp[i] < 32 || bp[i] == 127)
+ bp[i] = '.';
+}
+
+static char *_dmi_string(const struct dmi_header *dm, u8 s, int filter)
+{
+ char *bp = (char *)dm->data;
bp += dm->length;
while (s > 1 && *bp)
@@ -126,16 +132,24 @@ const char *dmi_string(const struct dmi_header *dm, u8 s)
}
if (!*bp)
- return bad_index;
+ return NULL;
- if (!(opt.flags & FLAG_DUMP))
- {
- /* ASCII filtering */
- len = strlen(bp);
- for (i = 0; i < len; i++)
- if (bp[i] < 32 || bp[i] == 127)
- bp[i] = '.';
- }
+ if (filter)
+ ascii_filter(bp, strlen(bp));
+
+ return bp;
+}
+
+const char *dmi_string(const struct dmi_header *dm, u8 s)
+{
+ char *bp;
+
+ if (s == 0)
+ return "Not Specified";
+
+ bp = _dmi_string(dm, s, 1);
+ if (bp == NULL)
+ return bad_index;
return bp;
}
@@ -208,7 +222,7 @@ static int dmi_bcd_range(u8 value, u8 low, u8 high)
static void dmi_dump(const struct dmi_header *h, const char *prefix)
{
int row, i;
- const char *s;
+ char *s;
printf("%sHeader and Data:\n", prefix);
for (row = 0; row < ((h->length - 1) >> 4) + 1; row++)
@@ -224,7 +238,7 @@ static void dmi_dump(const struct dmi_header *h, const char *prefix)
{
printf("%sStrings:\n", prefix);
i = 1;
- while ((s = dmi_string(h, i++)) != bad_index)
+ while ((s = _dmi_string(h, i++, !(opt.flags & FLAG_DUMP))))
{
if (opt.flags & FLAG_DUMP)
{
@@ -238,19 +252,9 @@ static void dmi_dump(const struct dmi_header *h, const char *prefix)
printf("\n");
}
/* String isn't filtered yet so do it now */
- printf("%s\t\"", prefix);
- while (*s)
- {
- if (*s < 32 || *s == 127)
- fputc('.', stdout);
- else
- fputc(*s, stdout);
- s++;
- }
- printf("\"\n");
+ ascii_filter(s, l - 1);
}
- else
- printf("%s\t%s\n", prefix, s);
+ printf("%s\t%s\n", prefix, s);
}
}
}
--
2.17.1