]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
Convert properly UTF-8 to UTF-16
authorFrediano Ziglio <frediano.ziglio@citrix.com>
Tue, 7 Aug 2012 09:33:03 +0000 (04:33 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 12 Oct 2012 20:39:00 +0000 (05:39 +0900)
commit fd3ba42c76d3d4b776120c2b24c1791e7bb3deb1 upstream.

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>
Acked-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve French <smfrench@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/cifs/cifs_unicode.c

index 33ef60d8e2fb50dbb996074b29fd563a1c1db82c..6a8568c6466f1eec4368926e1ce01e3a492a1153 100644 (file)
@@ -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;
 }