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