Skip to contentSkip to conversation

gobex: Replace g_convert by utf16_to_utf8

PATCH Applied Frédéric Danis 3 participants 3 messages Sep 10, 2024 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 gobex: Replace g_convert by utf16_to_utf8

The glibc's iconv implementation is based around plug in modules for specific translations which may not been built on the platform and prevent to use g_convert().
This commit replaces it by a function similar to the existing utf8_to_utf16() function.
---
v1 -> v2: Fix missing g_free
v2 -> v3: Replace g_malloc0 by alloca
Fix UTF-16 buffer size allocation
Ensure that UTF-16 buffer is terminated by '\0'
v3 -> v4: Fix memory allocation size

 gobex/gobex-header.c | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)
gobex/gobex-header.c +35 -6
index 002ba8861..f368f0e22 100644
@@ -62,6 +62,34 @@ static glong utf8_to_utf16(gunichar2 **utf16, const char *utf8) {
 	return utf16_len;
 }
 
+static glong utf16_to_utf8(char **utf8, const gunichar2 *utf16, guint16 len,
+				GError **err)
+{
+	glong utf8_len;
+	guint16 utf16_len, i;
+	gunichar2 *buf;
+
+	if (*utf16 == '\0') {
+		*utf8 = NULL;
+		return 0;
+	}
+
+	/* OBEX requires network byteorder (big endian) UTF-16
+	 * but g_utf16_to_utf8 expects host-byteorder UTF-8
+	 */
+	utf16_len = len / sizeof(gunichar2);
+	buf = alloca(sizeof(gunichar2) * (utf16_len + 1));
+	for (i = 0; i < utf16_len; i++)
+		(buf)[i] = g_ntohs(utf16[i]);
+	buf[utf16_len] = '\0';
+
+	*utf8 = g_utf16_to_utf8(buf, -1, NULL, &utf8_len, err);
+	if (*utf8 == NULL)
+		utf8_len = -1;
+
+	return utf8_len;
+}
+
 static guint8 *put_bytes(guint8 *to, const void *from, gsize count)
 {
 	memcpy(to, from, count);
@@ -130,7 +158,7 @@ GObexHeader *g_obex_header_decode(const void *data, gsize len,
 	GObexHeader *header;
 	const guint8 *ptr = data;
 	guint16 hdr_len;
-	gsize str_len;
+	glong str_len;
 	GError *conv_err = NULL;
 
 	if (len < 2) {
@@ -177,13 +205,14 @@ GObexHeader *g_obex_header_decode(const void *data, gsize len,
 			goto failed;
 		}
 
-		header->v.string = g_convert((const char *) ptr, hdr_len - 5,
-						"UTF-8", "UTF-16BE",
-						NULL, &str_len, &conv_err);
-		if (header->v.string == NULL) {
+		str_len = utf16_to_utf8(&header->v.string,
+					(const gunichar2 *) ptr,
+					hdr_len - 5,
+					&conv_err);
+		if (str_len < 0) {
 			g_set_error(err, G_OBEX_ERROR,
 					G_OBEX_ERROR_PARSE_ERROR,
-					"Unicode conversion failed: %s",
+					"UTF16 to UTF8 conversion failed: %s",
 					conv_err->message);
 			g_error_free(conv_err);
 			goto failed;
Signature · 1 line
2.34.1
#1 lore raw

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=888969

---Test result---

Test Summary:

CheckPatch                    PASS      0.56 seconds
GitLint                       PASS      0.33 seconds
BuildEll                      PASS      25.50 seconds
BluezMake                     PASS      1772.77 seconds
MakeCheck                     PASS      13.31 seconds
MakeDistcheck                 PASS      189.95 seconds
CheckValgrind                 PASS      265.31 seconds
CheckSmatch                   PASS      369.44 seconds
bluezmakeextell               PASS      124.73 seconds
IncrementalBuild              PASS      1528.22 seconds
ScanBuild                     WARNING   1049.82 seconds

Details
##############################
Test: ScanBuild - WARNING
Desc: Run Scan Build
Output:
gobex/gobex-header.c:95:2: warning: Null pointer passed to 2nd parameter expecting 'nonnull'

        memcpy(to, from, count);
        ^~~~~~~~~~~~~~~~~~~~~~~

1 warning generated.

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Frédéric Danis
This patch was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 12 lines · On Tue, 10 Sep 2024 17:44:50 +0200 you wrote:

The glibc's iconv implementation is based around plug in modules for specific translations which may not been built on the platform and prevent to use g_convert().
This commit replaces it by a function similar to the existing utf8_to_utf16() function.
---
v1 -> v2: Fix missing g_free
v2 -> v3: Replace g_malloc0 by alloca
Fix UTF-16 buffer size allocation
Ensure that UTF-16 buffer is terminated by '\0'
v3 -> v4: Fix memory allocation size

Quoted 1 line

[...]

Here is the summary with links:
- [BlueZ,v4] gobex: Replace g_convert by utf16_to_utf8

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=615fc3592a57

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html