Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1574798 > unrolled thread

[PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper

Started byRichard Leitner <richard.leitner@skidata.com>
First post2017-02-06 15:10 +0100
Last post2017-02-08 10:00 +0100
Articles 7 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Richard Leitner <richard.leitner@skidata.com> - 2017-02-06 15:10 +0100
    Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Alan Stern <stern@rowland.harvard.edu> - 2017-02-06 16:20 +0100
      Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Richard Leitner <richard.leitner@skidata.com> - 2017-02-06 16:40 +0100
        Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Alan Stern <stern@rowland.harvard.edu> - 2017-02-06 16:50 +0100
          Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Richard Leitner <richard.leitner@skidata.com> - 2017-02-06 16:50 +0100
    Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Sergei Shtylyov <sergei.shtylyov@cogentembedded.com> - 2017-02-06 17:50 +0100
      Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper Richard Leitner <richard.leitner@skidata.com> - 2017-02-08 10:00 +0100

#1574798 — [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper

FromRichard Leitner <richard.leitner@skidata.com>
Date2017-02-06 15:10 +0100
Subject[PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper
Message-ID<t7S9s-5XR-21@gated-at.bofh.it>
For USB string descriptors we need to convert ASCII strings to UTF16-LE.
Therefore make a simple helper function (based on ascii2desc from
drivers/usb/core/hcd.c) for that purpose.

Signed-off-by: Richard Leitner <richard.leitner@skidata.com>
---
 include/linux/string.h |  1 +
 lib/string.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..48fd0c6 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -135,6 +135,7 @@ static inline int strtobool(const char *s, bool *res)
 }
 
 int match_string(const char * const *array, size_t n, const char *string);
+unsigned int ascii2utf16le(char const *s, u8 *buf, unsigned int len);
 
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
diff --git a/lib/string.c b/lib/string.c
index ed83562..a113e3e 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,29 @@ char *strreplace(char *s, char old, char new)
 	return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * ascii2utf16le() - Helper routine for producing UTF-16LE string descriptors
+ * @s: Null-terminated ASCII (actually ISO-8859-1) string
+ * @buf: Buffer for UTF-16LE string
+ * @len: Length (in bytes; may be odd) of UTF-16LE buffer.
+ *
+ * Return: The number of bytes filled in: 2*strlen(s) or @len, whichever is less
+ */
+unsigned int ascii2utf16le(char const *s, u8 *buf, unsigned int len)
+{
+	unsigned int n, t = 2 * strlen(s);
+
+	if (len > t)
+		len = t;
+	n = len;
+	while (n--) {
+		t = (unsigned char)*s++;
+		*buf++ = t;
+		if (!n--)
+			break;
+		*buf++ = t >> 8;
+	}
+	return len;
+}
+EXPORT_SYMBOL(ascii2utf16le);
-- 
2.1.4

[toc] | [next] | [standalone]


#1574894

FromAlan Stern <stern@rowland.harvard.edu>
Date2017-02-06 16:20 +0100
Message-ID<t7Tfc-6CJ-33@gated-at.bofh.it>
In reply to#1574798
On Mon, 6 Feb 2017, Richard Leitner wrote:

> For USB string descriptors we need to convert ASCII strings to UTF16-LE.
> Therefore make a simple helper function (based on ascii2desc from
> drivers/usb/core/hcd.c) for that purpose.

You know, we already have utf8s_to_utf16s() in fs/nls/nls_base.c.  
Maybe it doesn't do exactly what you want, but it should be pretty 
close.  Adding another helper function to do essentially the same thing 
seems unnecessary.

Alan Stern

[toc] | [prev] | [next] | [standalone]


#1574907

FromRichard Leitner <richard.leitner@skidata.com>
Date2017-02-06 16:40 +0100
Message-ID<t7Tyy-6JX-27@gated-at.bofh.it>
In reply to#1574894
On 02/06/2017 04:12 PM, Alan Stern wrote:
> On Mon, 6 Feb 2017, Richard Leitner wrote:
> 
>> For USB string descriptors we need to convert ASCII strings to UTF16-LE.
>> Therefore make a simple helper function (based on ascii2desc from
>> drivers/usb/core/hcd.c) for that purpose.
> 
> You know, we already have utf8s_to_utf16s() in fs/nls/nls_base.c.  
> Maybe it doesn't do exactly what you want, but it should be pretty 
> close.  Adding another helper function to do essentially the same thing 
> seems unnecessary.

Thanks for that pointer. I totally agree with you.

So it would be OK to include linux/nls.h and use utf8s_to_utf16s() in
drivers/usb/{core/hcd.c,misc/usb251xb.c}?

Thanks & regards,
Richard L

[toc] | [prev] | [next] | [standalone]


#1574924

FromAlan Stern <stern@rowland.harvard.edu>
Date2017-02-06 16:50 +0100
Message-ID<t7TIf-6NW-53@gated-at.bofh.it>
In reply to#1574907
On Mon, 6 Feb 2017, Richard Leitner wrote:

> On 02/06/2017 04:12 PM, Alan Stern wrote:
> > On Mon, 6 Feb 2017, Richard Leitner wrote:
> > 
> >> For USB string descriptors we need to convert ASCII strings to UTF16-LE.
> >> Therefore make a simple helper function (based on ascii2desc from
> >> drivers/usb/core/hcd.c) for that purpose.
> > 
> > You know, we already have utf8s_to_utf16s() in fs/nls/nls_base.c.  
> > Maybe it doesn't do exactly what you want, but it should be pretty 
> > close.  Adding another helper function to do essentially the same thing 
> > seems unnecessary.
> 
> Thanks for that pointer. I totally agree with you.
> 
> So it would be OK to include linux/nls.h and use utf8s_to_utf16s() in
> drivers/usb/{core/hcd.c,misc/usb251xb.c}?

Well, we already include linux/nls.h in drivers/usb/core/message.c and
a few files under drivers/usb/gadget.  Putting it in a few more places
shouldn't hurt.

Alan Stern

[toc] | [prev] | [next] | [standalone]


#1574926

FromRichard Leitner <richard.leitner@skidata.com>
Date2017-02-06 16:50 +0100
Message-ID<t7TIf-6NW-51@gated-at.bofh.it>
In reply to#1574924
On 02/06/2017 04:40 PM, Alan Stern wrote:
> On Mon, 6 Feb 2017, Richard Leitner wrote:
>> So it would be OK to include linux/nls.h and use utf8s_to_utf16s() in
>> drivers/usb/{core/hcd.c,misc/usb251xb.c}?
> 
> Well, we already include linux/nls.h in drivers/usb/core/message.c and
> a few files under drivers/usb/gadget.  Putting it in a few more places
> shouldn't hurt.

OK. Thanks! I will change that for v4.

regards,
Richard L

[toc] | [prev] | [next] | [standalone]


#1574974

FromSergei Shtylyov <sergei.shtylyov@cogentembedded.com>
Date2017-02-06 17:50 +0100
Message-ID<t7UEh-7p1-1@gated-at.bofh.it>
In reply to#1574798
Hello!

On 02/06/2017 05:03 PM, Richard Leitner wrote:

> For USB string descriptors we need to convert ASCII strings to UTF16-LE.
> Therefore make a simple helper function (based on ascii2desc from
> drivers/usb/core/hcd.c) for that purpose.
>
> Signed-off-by: Richard Leitner <richard.leitner@skidata.com>
[...]
> diff --git a/lib/string.c b/lib/string.c
> index ed83562..a113e3e 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -952,3 +952,29 @@ char *strreplace(char *s, char old, char new)
>  	return s;
>  }
>  EXPORT_SYMBOL(strreplace);
> +
> +/**
> + * ascii2utf16le() - Helper routine for producing UTF-16LE string descriptors
> + * @s: Null-terminated ASCII (actually ISO-8859-1) string
> + * @buf: Buffer for UTF-16LE string
> + * @len: Length (in bytes; may be odd) of UTF-16LE buffer.
> + *
> + * Return: The number of bytes filled in: 2*strlen(s) or @len, whichever is less
> + */
> +unsigned int ascii2utf16le(char const *s, u8 *buf, unsigned int len)
> +{
> +	unsigned int n, t = 2 * strlen(s);
> +
> +	if (len > t)
> +		len = t;
> +	n = len;
> +	while (n--) {
> +		t = (unsigned char)*s++;
> +		*buf++ = t;
> +		if (!n--)
> +			break;
> +		*buf++ = t >> 8;

    Isn't it always 0?

> +	}
> +	return len;
> +}
> +EXPORT_SYMBOL(ascii2utf16le);

MBR, Sergei

[toc] | [prev] | [next] | [standalone]


#1576364

FromRichard Leitner <richard.leitner@skidata.com>
Date2017-02-08 10:00 +0100
Message-ID<t8wgy-6w4-21@gated-at.bofh.it>
In reply to#1574974
On 02/06/2017 05:48 PM, Sergei Shtylyov wrote:
> Hello!
> 
> On 02/06/2017 05:03 PM, Richard Leitner wrote:
> 
>> For USB string descriptors we need to convert ASCII strings to UTF16-LE.
>> Therefore make a simple helper function (based on ascii2desc from
>> drivers/usb/core/hcd.c) for that purpose.
>>
>> Signed-off-by: Richard Leitner <richard.leitner@skidata.com>
> [...]
>> diff --git a/lib/string.c b/lib/string.c
>> index ed83562..a113e3e 100644
>> --- a/lib/string.c
>> +++ b/lib/string.c
>> @@ -952,3 +952,29 @@ char *strreplace(char *s, char old, char new)
>>      return s;
>>  }
>>  EXPORT_SYMBOL(strreplace);
>> +
>> +/**
>> + * ascii2utf16le() - Helper routine for producing UTF-16LE string
>> descriptors
>> + * @s: Null-terminated ASCII (actually ISO-8859-1) string
>> + * @buf: Buffer for UTF-16LE string
>> + * @len: Length (in bytes; may be odd) of UTF-16LE buffer.
>> + *
>> + * Return: The number of bytes filled in: 2*strlen(s) or @len,
>> whichever is less
>> + */
>> +unsigned int ascii2utf16le(char const *s, u8 *buf, unsigned int len)
>> +{
>> +    unsigned int n, t = 2 * strlen(s);
>> +
>> +    if (len > t)
>> +        len = t;
>> +    n = len;
>> +    while (n--) {
>> +        t = (unsigned char)*s++;
>> +        *buf++ = t;
>> +        if (!n--)
>> +            break;
>> +        *buf++ = t >> 8;
> 
>    Isn't it always 0?

As I will remove this function and use utf8s_to_utf16s() instead (as
suggested by Alan Stern) IMHO this issue needs no more attention.

Nonetheless, thank you for your feedback!

> 
>> +    }
>> +    return len;
>> +}
>> +EXPORT_SYMBOL(ascii2utf16le);

regards,
Richard L

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web