public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v1] shared/util: Introduce strnlenutf8
@ 2025-07-08 17:46 Luiz Augusto von Dentz
  2025-07-08 19:06 ` [BlueZ,v1] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Luiz Augusto von Dentz @ 2025-07-08 17:46 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This introduces strnlenutf8 which works similarly to strnlen but return
only the number of valid bytes of UTF-8 encoded string then replace the
other copies of similar code.
---
 src/shared/util.c | 81 +++++++++++++++++------------------------------
 src/shared/util.h |  2 ++
 2 files changed, 31 insertions(+), 52 deletions(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index 4780f26b6d59..57f4d7bc00db 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1909,58 +1909,7 @@ char *strstrip(char *str)
 	return str;
 }
 
-bool strisutf8(const char *str, size_t len)
-{
-	size_t i = 0;
-
-	while (i < len) {
-		unsigned char c = str[i];
-		size_t size = 0;
-
-		/* Check the first byte to determine the number of bytes in the
-		 * UTF-8 character.
-		 */
-		if ((c & 0x80) == 0x00)
-			size = 1;
-		else if ((c & 0xE0) == 0xC0)
-			size = 2;
-		else if ((c & 0xF0) == 0xE0)
-			size = 3;
-		else if ((c & 0xF8) == 0xF0)
-			size = 4;
-		else
-			/* Invalid UTF-8 sequence */
-			return false;
-
-		/* Check the following bytes to ensure they have the correct
-		 * format.
-		 */
-		for (size_t j = 1; j < size; ++j) {
-			if (i + j > len || (str[i + j] & 0xC0) != 0x80)
-				/* Invalid UTF-8 sequence */
-				return false;
-		}
-
-		/* Move to the next character */
-		i += size;
-	}
-
-	return true;
-}
-
-bool argsisutf8(int argc, char *argv[])
-{
-	for (int i = 0; i < argc; i++) {
-		if (!strisutf8(argv[i], strlen(argv[i]))) {
-			printf("Invalid character in string: %s\n", argv[i]);
-			return false;
-		}
-	}
-
-	return true;
-}
-
-char *strtoutf8(char *str, size_t len)
+size_t strnlenutf8(const char *str, size_t len)
 {
 	size_t i = 0;
 
@@ -1997,6 +1946,34 @@ char *strtoutf8(char *str, size_t len)
 	}
 
 done:
+	return i;
+}
+
+bool strisutf8(const char *str, size_t len)
+{
+	return strnlenutf8(str, len) == len;
+}
+
+bool argsisutf8(int argc, char *argv[])
+{
+	for (int i = 0; i < argc; i++) {
+		if (!strisutf8(argv[i], strlen(argv[i]))) {
+			printf("Invalid character in string: %s\n", argv[i]);
+			return false;
+		}
+	}
+
+	return true;
+}
+
+char *strtoutf8(char *str, size_t len)
+{
+	size_t i = 0;
+
+	i = strnlenutf8(str, len);
+	if (i == len)
+		return str;
+
 	/* Truncate to the longest valid UTF-8 string */
 	memset(str + i, 0, len - i);
 	return str;
diff --git a/src/shared/util.h b/src/shared/util.h
index 6fc02a9dcb5a..c480351d6e9f 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -90,6 +90,8 @@ do {						\
 char *strdelimit(char *str, char *del, char c);
 int strsuffix(const char *str, const char *suffix);
 char *strstrip(char *str);
+
+size_t strnlenutf8(const char *str, size_t len);
 bool strisutf8(const char *str, size_t length);
 bool argsisutf8(int argc, char *argv[]);
 char *strtoutf8(char *str, size_t len);
-- 
2.50.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* RE: [BlueZ,v1] shared/util: Introduce strnlenutf8
  2025-07-08 17:46 [PATCH BlueZ v1] shared/util: Introduce strnlenutf8 Luiz Augusto von Dentz
@ 2025-07-08 19:06 ` bluez.test.bot
  0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2025-07-08 19:06 UTC (permalink / raw)
  To: linux-bluetooth, luiz.dentz

[-- Attachment #1: Type: text/plain, Size: 1261 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=980144

---Test result---

Test Summary:
CheckPatch                    PENDING   0.25 seconds
GitLint                       PENDING   0.31 seconds
BuildEll                      PASS      20.18 seconds
BluezMake                     PASS      2579.74 seconds
MakeCheck                     PASS      19.90 seconds
MakeDistcheck                 PASS      184.87 seconds
CheckValgrind                 PASS      236.56 seconds
CheckSmatch                   PASS      304.81 seconds
bluezmakeextell               PASS      128.16 seconds
IncrementalBuild              PENDING   0.24 seconds
ScanBuild                     PASS      912.39 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:



---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-07-08 19:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08 17:46 [PATCH BlueZ v1] shared/util: Introduce strnlenutf8 Luiz Augusto von Dentz
2025-07-08 19:06 ` [BlueZ,v1] " bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox