Blame SOURCES/0008-dmidecode-Refactor-ASCII-filtering-of-DMI-strings.patch

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