diff mbox

Convert properly UTF-8 to UTF-16

Message ID 7CE799CC0E4DE04B88D5FDF226E18AC2CDFFB08CFE@LONPMAILBOX01.citrite.net (mailing list archive)
State New, archived
Headers show

Commit Message

Frediano Ziglio July 31, 2012, 10:45 a.m. UTC
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 |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

 		if (charlen < 1) {
@@ -215,6 +228,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;
 }

Comments

Jeff Layton July 31, 2012, 11:15 a.m. UTC | #1
On Tue, 31 Jul 2012 11:45:35 +0100
Frediano Ziglio <frediano.ziglio@citrix.com> wrote:

> 
> 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 |   14 ++++++++++++++
>  1 files changed, 14 insertions(+), 0 deletions(-)
> 
> diff --git a/fs/cifs/cifs_unicode.c b/fs/cifs/cifs_unicode.c
> index 7dab9c0..a798e01 100644
> --- a/fs/cifs/cifs_unicode.c
> +++ b/fs/cifs/cifs_unicode.c
> @@ -203,6 +203,19 @@ cifs_strtoUTF16(__le16 *to, const char *from, int
> len,
>  	int i;
>  	wchar_t wchar_to; /* needed to quiet sparse */
>  
> +	if (!strcmp(codepage->charset, "utf8")) {
> +		i  = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
> +				       (wchar_t *) to, len);

	Much nicer, but...

	You're using "len" both for "inlen" and "maxout". Won't that
	give you a truncated string if there are single-byte characters
	that convert to multibyte ones?

> +		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 +228,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;
>  }
Frediano Ziglio July 31, 2012, 11:24 a.m. UTC | #2
On Tue, 2012-07-31 at 07:15 -0400, Jeff Layton wrote:
> On Tue, 31 Jul 2012 11:45:35 +0100
> Frediano Ziglio <frediano.ziglio@citrix.com> wrote:
> 
> > 
> > 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 |   14 ++++++++++++++
> >  1 files changed, 14 insertions(+), 0 deletions(-)
> > 
> > diff --git a/fs/cifs/cifs_unicode.c b/fs/cifs/cifs_unicode.c
> > index 7dab9c0..a798e01 100644
> > --- a/fs/cifs/cifs_unicode.c
> > +++ b/fs/cifs/cifs_unicode.c
> > @@ -203,6 +203,19 @@ cifs_strtoUTF16(__le16 *to, const char *from, int
> > len,
> >  	int i;
> >  	wchar_t wchar_to; /* needed to quiet sparse */
> >  
> > +	if (!strcmp(codepage->charset, "utf8")) {
> > +		i  = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
> > +				       (wchar_t *) to, len);
> 
> 	Much nicer, but...
> 
> 	You're using "len" both for "inlen" and "maxout". Won't that
> 	give you a truncated string if there are single-byte characters
> 	that convert to multibyte ones?
> 

That's impossible as len on destination is the length of 2 byte
sequences.

Also a single-byte encoded UTF-8 is an ASCII characters which lead to a
single 2-byte sequence.

> > +		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 +228,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;
> >  }
> 
> 

Frediano
diff mbox

Patch

diff --git a/fs/cifs/cifs_unicode.c b/fs/cifs/cifs_unicode.c
index 7dab9c0..a798e01 100644
--- a/fs/cifs/cifs_unicode.c
+++ b/fs/cifs/cifs_unicode.c
@@ -203,6 +203,19 @@  cifs_strtoUTF16(__le16 *to, const char *from, int
len,
 	int i;
 	wchar_t wchar_to; /* needed to quiet sparse */
 
+	if (!strcmp(codepage->charset, "utf8")) {
+		i  = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
+				       (wchar_t *) to, len);
+		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);