From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751931AbdBFQsr (ORCPT ); Mon, 6 Feb 2017 11:48:47 -0500 Received: from mail-lf0-f48.google.com ([209.85.215.48]:34029 "EHLO mail-lf0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751814AbdBFQsn (ORCPT ); Mon, 6 Feb 2017 11:48:43 -0500 Subject: Re: [PATCH v3 1/3] lib/string: introduce ascii2utf16le() helper To: Richard Leitner , linux-usb@vger.kernel.org References: Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, gregkh@linuxfoundation.org, robh+dt@kernel.org, mark.rutland@arm.com, akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, keescook@chromium.org, dev@g0hl1n.net From: Sergei Shtylyov Organization: Cogent Embedded Message-ID: <29ff4981-ed4d-d87b-f6e2-112234f6a10a@cogentembedded.com> Date: Mon, 6 Feb 2017 19:48:39 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 [...] > 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