All of lore.kernel.org
 help / color / mirror / Atom feed
From: peng.fan at nxp.com <peng.fan@nxp.com>
To: u-boot@lists.denx.de
Subject: [PATCH 04/16] usb: gadget: move utf8_to_utf16le to header file
Date: Wed, 16 Sep 2020 21:25:25 +0800	[thread overview]
Message-ID: <20200916132537.8313-5-peng.fan@nxp.com> (raw)
In-Reply-To: <20200916132537.8313-1-peng.fan@nxp.com>

From: Li Jun <jun.li@nxp.com>

As other users may use utf8_to_utf16le() to convert the utf8
to utf16 for usb, so move it to head file.

Signed-off-by: Li Jun <jun.li@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/usb/gadget/usbstring.c | 74 +--------------------------------
 include/linux/utf.h            | 75 ++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 73 deletions(-)
 create mode 100644 include/linux/utf.h

diff --git a/drivers/usb/gadget/usbstring.c b/drivers/usb/gadget/usbstring.c
index 83cdd8a259..e2464ad923 100644
--- a/drivers/usb/gadget/usbstring.c
+++ b/drivers/usb/gadget/usbstring.c
@@ -10,79 +10,7 @@
 #include <linux/errno.h>
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
-
-#include <asm/unaligned.h>
-
-
-static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len)
-{
-	int	count = 0;
-	u8	c;
-	u16	uchar;
-
-	/*
-	 * this insists on correct encodings, though not minimal ones.
-	 * BUT it currently rejects legit 4-byte UTF-8 code points,
-	 * which need surrogate pairs.  (Unicode 3.1 can use them.)
-	 */
-	while (len != 0 && (c = (u8) *s++) != 0) {
-		if ((c & 0x80)) {
-			/*
-			 * 2-byte sequence:
-			 * 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
-			 */
-			if ((c & 0xe0) == 0xc0) {
-				uchar = (c & 0x1f) << 6;
-
-				c = (u8) *s++;
-				if ((c & 0xc0) != 0x80)
-					goto fail;
-				c &= 0x3f;
-				uchar |= c;
-
-			/*
-			 * 3-byte sequence (most CJKV characters):
-			 * zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
-			 */
-			} else if ((c & 0xf0) == 0xe0) {
-				uchar = (c & 0x0f) << 12;
-
-				c = (u8) *s++;
-				if ((c & 0xc0) != 0x80)
-					goto fail;
-				c &= 0x3f;
-				uchar |= c << 6;
-
-				c = (u8) *s++;
-				if ((c & 0xc0) != 0x80)
-					goto fail;
-				c &= 0x3f;
-				uchar |= c;
-
-				/* no bogus surrogates */
-				if (0xd800 <= uchar && uchar <= 0xdfff)
-					goto fail;
-
-			/*
-			 * 4-byte sequence (surrogate pairs, currently rare):
-			 * 11101110wwwwzzzzyy + 110111yyyyxxxxxx
-			 *     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
-			 * (uuuuu = wwww + 1)
-			 * FIXME accept the surrogate code points (only)
-			 */
-			} else
-				goto fail;
-		} else
-			uchar = c;
-		put_unaligned_le16(uchar, cp++);
-		count++;
-		len--;
-	}
-	return count;
-fail:
-	return -1;
-}
-
+#include <linux/utf.h>
 
 /**
  * usb_gadget_get_string - fill out a string descriptor
diff --git a/include/linux/utf.h b/include/linux/utf.h
new file mode 100644
index 0000000000..e1f7d3bd1d
--- /dev/null
+++ b/include/linux/utf.h
@@ -0,0 +1,75 @@
+#ifndef _LINUX_UTF_H
+#define _LINUX_UTF_H
+
+#include <asm/unaligned.h>
+
+static inline int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len)
+{
+	int	count = 0;
+	u8	c;
+	u16	uchar;
+
+	/*
+	 * this insists on correct encodings, though not minimal ones.
+	 * BUT it currently rejects legit 4-byte UTF-8 code points,
+	 * which need surrogate pairs.  (Unicode 3.1 can use them.)
+	 */
+	while (len != 0 && (c = (u8) *s++) != 0) {
+		if ((c & 0x80)) {
+			/*
+			 * 2-byte sequence:
+			 * 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
+			 */
+			if ((c & 0xe0) == 0xc0) {
+				uchar = (c & 0x1f) << 6;
+
+				c = (u8) *s++;
+				if ((c & 0xc0) != 0x80)
+					goto fail;
+				c &= 0x3f;
+				uchar |= c;
+
+			/*
+			 * 3-byte sequence (most CJKV characters):
+			 * zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
+			 */
+			} else if ((c & 0xf0) == 0xe0) {
+				uchar = (c & 0x0f) << 12;
+
+				c = (u8) *s++;
+				if ((c & 0xc0) != 0x80)
+					goto fail;
+				c &= 0x3f;
+				uchar |= c << 6;
+
+				c = (u8) *s++;
+				if ((c & 0xc0) != 0x80)
+					goto fail;
+				c &= 0x3f;
+				uchar |= c;
+
+				/* no bogus surrogates */
+				if (0xd800 <= uchar && uchar <= 0xdfff)
+					goto fail;
+
+			/*
+			 * 4-byte sequence (surrogate pairs, currently rare):
+			 * 11101110wwwwzzzzyy + 110111yyyyxxxxxx
+			 *     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
+			 * (uuuuu = wwww + 1)
+			 * FIXME accept the surrogate code points (only)
+			 */
+			} else
+				goto fail;
+		} else
+			uchar = c;
+		put_unaligned_le16(uchar, cp++);
+		count++;
+		len--;
+	}
+	return count;
+fail:
+	return -1;
+}
+
+#endif /* _LINUX_UTF_H */
-- 
2.28.0

  parent reply	other threads:[~2020-09-16 13:25 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-16 13:25 [PATCH 00/16] usb: gadget: update peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 01/16] usb: gadget: Add ep_config call back to usb_gadget_ops peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 02/16] usb: gadget: don't change ep name for dwc3 while ep autoconfig peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 03/16] usb: gadget: OS String support peng.fan at nxp.com
2020-09-16 13:25 ` peng.fan at nxp.com [this message]
2020-09-16 13:25 ` [PATCH 05/16] usb: gadget: OS Feature Descriptors support peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 06/16] usb: gadget: add WCID support for mfgtool peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 07/16] usb: gadget: fastboot: add ext properties for WCID peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 08/16] usb: gadget: set correct usb_configuration for os_desc_config peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 09/16] usb: gadget: update os_desc_config when add config peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 10/16] usb: gadget: add super speed support peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 11/16] usb: fastboot: " peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 12/16] usb: gadget: dnl: set dnl to be super speed peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 13/16] usb: composite: force gadget to be USB2 for HS only function peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 14/16] usb: udc: ci: update speed handling peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 15/16] usb: gadget: fastboot: use correct max packet size peng.fan at nxp.com
2020-09-16 13:25 ` [PATCH 16/16] usb: gaget: ci: set ep's desc when enable ep peng.fan at nxp.com
2020-10-10  6:00 ` [PATCH 00/16] usb: gadget: update Peng Fan
2021-01-03 10:32   ` Peng Fan
2021-01-23 12:11     ` Lukasz Majewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200916132537.8313-5-peng.fan@nxp.com \
    --to=peng.fan@nxp.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.