From: "Frédéric Danis" <frederic.danis@collabora.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2 1/5] shared/util: Add strtoutf8 function
Date: Tue, 8 Jul 2025 09:08:18 +0200 [thread overview]
Message-ID: <20250708070822.185375-2-frederic.danis@collabora.com> (raw)
In-Reply-To: <20250708070822.185375-1-frederic.danis@collabora.com>
This adds the strtoutf8 function that truncate a string before the
first non UTF-8 character.
This truncation is done in place.
---
src/shared/util.c | 42 ++++++++++++++++++++++++++++++++++++++++++
src/shared/util.h | 1 +
2 files changed, 43 insertions(+)
diff --git a/src/shared/util.c b/src/shared/util.c
index 5d3a14d96..5262458cb 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1959,3 +1959,45 @@ bool argsisutf8(int argc, char *argv[])
return true;
}
+
+char *strtoutf8(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 */
+ goto done;
+
+ /* 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 */
+ goto done;
+ }
+
+ /* Move to the next character */
+ i += size;
+ }
+
+done:
+ /* 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 dd357fb93..6fc02a9dc 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -92,6 +92,7 @@ int strsuffix(const char *str, const char *suffix);
char *strstrip(char *str);
bool strisutf8(const char *str, size_t length);
bool argsisutf8(int argc, char *argv[]);
+char *strtoutf8(char *str, size_t len);
void *util_malloc(size_t size);
void *util_memdup(const void *src, size_t size);
--
2.43.0
next prev parent reply other threads:[~2025-07-08 7:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 7:08 [PATCH BlueZ v2 0/5] audio/avrcp: Fix crash with invalid UTF-8 item name Frédéric Danis
2025-07-08 7:08 ` Frédéric Danis [this message]
2025-07-08 8:32 ` bluez.test.bot
2025-07-08 13:55 ` [PATCH BlueZ v2 1/5] shared/util: Add strtoutf8 function Luiz Augusto von Dentz
2025-07-08 7:08 ` [PATCH BlueZ v2 2/5] audio/avrcp: Fix crash with invalid UTF-8 item name Frédéric Danis
2025-07-08 7:08 ` [PATCH BlueZ v2 3/5] audio/mcp: Use strtoutf8 for player name and track title Frédéric Danis
2025-07-08 7:08 ` [PATCH BlueZ v2 4/5] audio/gap: Use strtoutf8 for GAP device name Frédéric Danis
2025-07-08 7:08 ` [PATCH BlueZ v2 5/5] eir: Use strtoutf8 for device names Frédéric Danis
2025-07-08 8:05 ` [PATCH BlueZ v2 0/5] audio/avrcp: Fix crash with invalid UTF-8 item name Bastien Nocera
2025-07-08 14:01 ` Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250708070822.185375-2-frederic.danis@collabora.com \
--to=frederic.danis@collabora.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.