All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] Convert properly UTF-8 to UTF-16
@ 2012-08-07  9:33 ` Frediano Ziglio
  0 siblings, 0 replies; 17+ messages in thread
From: Frediano Ziglio @ 2012-08-07  9:33 UTC (permalink / raw)
  To: sfrench-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org,
	jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
  Cc: linux-cifs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	samba-technical-w/Ol4Ecudpl8XjKLYN78aQ@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org


wchar_t is currently 16bit so converting a utf8 encoded characters not
in plane 0 (>= 0x10000) to wchar_t (that is calling char2uni) lead to a
-EINVAL return. This patch detect utf8 in cifs_strtoUTF16 and add special
code calling utf8s_to_utf16s.

Signed-off-by: Frediano Ziglio <frediano.ziglio@citrix.com>
---
 fs/cifs/cifs_unicode.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/fs/cifs/cifs_unicode.c b/fs/cifs/cifs_unicode.c
index 7dab9c0..1166b95 100644
--- a/fs/cifs/cifs_unicode.c
+++ b/fs/cifs/cifs_unicode.c
@@ -203,6 +203,27 @@ cifs_strtoUTF16(__le16 *to, const char *from, int len,
 	int i;
 	wchar_t wchar_to; /* needed to quiet sparse */
 
+	/* special case for utf8 to handle no plane0 chars */
+	if (!strcmp(codepage->charset, "utf8")) {
+		/*
+		 * convert utf8 -> utf16, we assume we have enough space
+		 * as caller should have assumed conversion does not overflow
+		 * in destination len is length in wchar_t units (16bits)
+		 */
+		i  = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
+				       (wchar_t *) to, len);
+
+		/* if success terminate and exit */
+		if (i >= 0)
+			goto success;
+		/*
+		 * if fails fall back to UCS encoding as this
+		 * function should not return negative values
+		 * currently can fail only if source contains
+		 * invalid encoded characters
+		 */
+	}
+
 	for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
 		charlen = codepage->char2uni(from, len, &wchar_to);
 		if (charlen < 1) {
@@ -215,6 +236,7 @@ cifs_strtoUTF16(__le16 *to, const char *from, int len,
 		put_unaligned_le16(wchar_to, &to[i]);
 	}
 
+success:
 	put_unaligned_le16(0, &to[i]);
 	return i;
 }
-- 
1.7.5.4


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

end of thread, other threads:[~2012-10-09  4:43 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-07  9:33 [PATCH v2] Convert properly UTF-8 to UTF-16 Frediano Ziglio
2012-08-07  9:33 ` Frediano Ziglio
     [not found] ` <7CE799CC0E4DE04B88D5FDF226E18AC2CDFFB08D16-aFusIB7tbcLeU4JHVX8hdHnr0TU713UqXqFh9Ls21Oc@public.gmane.org>
2012-08-07 10:47   ` Jeff Layton
2012-08-07 10:47     ` Jeff Layton
     [not found]     ` <20120807064752.22e0da81-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org>
2012-08-28  7:28       ` Frediano Ziglio
2012-08-28  7:28         ` Frediano Ziglio
     [not found]         ` <7CE799CC0E4DE04B88D5FDF226E18AC2E07634EB82-aFusIB7tbcLeU4JHVX8hdHnr0TU713UqXqFh9Ls21Oc@public.gmane.org>
2012-10-03 14:34           ` Frediano Ziglio
2012-10-03 14:34             ` Frediano Ziglio
     [not found]             ` <7CE799CC0E4DE04B88D5FDF226E18AC2E08D0408DA-aFusIB7tbcLeU4JHVX8hdHnr0TU713UqXqFh9Ls21Oc@public.gmane.org>
2012-10-03 19:49               ` Steve French
2012-10-03 19:49                 ` Steve French
2012-10-08  8:18                 ` Frediano Ziglio
2012-10-08  8:18                   ` Frediano Ziglio
2012-10-08 11:26                   ` Jeff Layton
     [not found]                   ` <7CE799CC0E4DE04B88D5FDF226E18AC2E08D0408E8-aFusIB7tbcLeU4JHVX8hdHnr0TU713UqXqFh9Ls21Oc@public.gmane.org>
2012-10-08 14:04                     ` Steve French
2012-10-08 14:04                       ` Steve French
2012-10-09  4:43                     ` Suresh Jayaraman
2012-10-09  4:43                       ` Suresh Jayaraman

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.