mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 23/25] lib: Port basic Linux kernel NLS functions
Date: Tue, 19 Feb 2019 23:29:28 -0800	[thread overview]
Message-ID: <20190220072930.14300-24-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20190220072930.14300-1-andrew.smirnov@gmail.com>

Port basic Linux kernel NLS functions: utf8_to_utf32() and
utf8s_to_utf16s() in order to support porting kernel code that uses
them.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 include/linux/nls.h |  40 ++++++++++++++
 lib/Kconfig         |   3 +
 lib/Makefile        |   1 +
 lib/nls_base.c      | 131 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 175 insertions(+)
 create mode 100644 include/linux/nls.h
 create mode 100644 lib/nls_base.c

diff --git a/include/linux/nls.h b/include/linux/nls.h
new file mode 100644
index 000000000..62fb7b5a9
--- /dev/null
+++ b/include/linux/nls.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_NLS_H
+#define _LINUX_NLS_H
+
+/* Unicode has changed over the years.  Unicode code points no longer
+ * fit into 16 bits; as of Unicode 5 valid code points range from 0
+ * to 0x10ffff (17 planes, where each plane holds 65536 code points).
+ *
+ * The original decision to represent Unicode characters as 16-bit
+ * wchar_t values is now outdated.  But plane 0 still includes the
+ * most commonly used characters, so we will retain it.  The newer
+ * 32-bit unicode_t type can be used when it is necessary to
+ * represent the full Unicode character set.
+ */
+
+/* Plane-0 Unicode character */
+typedef u16 wchar_t;
+#define MAX_WCHAR_T	0xffff
+
+/* Arbitrary Unicode character */
+typedef u32 unicode_t;
+
+/* this value hold the maximum octet of charset */
+#define NLS_MAX_CHARSET_SIZE 6 /* for UTF-8 */
+
+/* Byte order for UTF-16 strings */
+enum utf16_endian {
+	UTF16_HOST_ENDIAN,
+	UTF16_LITTLE_ENDIAN,
+	UTF16_BIG_ENDIAN
+};
+
+/* nls_base.c */
+
+extern int utf8_to_utf32(const u8 *s, int len, unicode_t *pu);
+extern int utf8s_to_utf16s(const u8 *s, int len,
+		enum utf16_endian endian, wchar_t *pwcs, int maxlen);
+
+#endif /* _LINUX_NLS_H */
+
diff --git a/lib/Kconfig b/lib/Kconfig
index e048aded8..44f30d824 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -148,4 +148,7 @@ config GENERIC_LIB_LSHRDI3
 config GENERIC_LIB_MULDI3
 	bool
 
+config NLS
+        bool "Native language support"
+
 endmenu
diff --git a/lib/Makefile b/lib/Makefile
index e72aa6655..763516d41 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -67,6 +67,7 @@ obj-y			+= parseopt.o
 obj-y			+= clz_ctz.o
 obj-$(CONFIG_CRC_CCITT) += crc-ccitt.o
 obj-$(CONFIG_CRC8)	+= crc8.o
+obj-$(CONFIG_NLS)	+= nls_base.o
 
 # GCC library routines
 obj-$(CONFIG_GENERIC_LIB_ASHLDI3) += ashldi3.o
diff --git a/lib/nls_base.c b/lib/nls_base.c
new file mode 100644
index 000000000..cd6a5ff12
--- /dev/null
+++ b/lib/nls_base.c
@@ -0,0 +1,131 @@
+/*
+ * linux/fs/nls/nls_base.c
+ *
+ * Native language support--charsets and unicode translations.
+ * By Gordon Chaffee 1996, 1997
+ *
+ * Unicode based case conversion 1999 by Wolfram Pienkoss
+ *
+ */
+#include <common.h>
+#include <linux/nls.h>
+
+/*
+ * Sample implementation from Unicode home page.
+ * http://www.stonehand.com/unicode/standard/fss-utf.html
+ */
+struct utf8_table {
+	int     cmask;
+	int     cval;
+	int     shift;
+	long    lmask;
+	long    lval;
+};
+
+static const struct utf8_table utf8_table[] =
+{
+    {0x80,  0x00,   0*6,    0x7F,           0,         /* 1 byte sequence */},
+    {0xE0,  0xC0,   1*6,    0x7FF,          0x80,      /* 2 byte sequence */},
+    {0xF0,  0xE0,   2*6,    0xFFFF,         0x800,     /* 3 byte sequence */},
+    {0xF8,  0xF0,   3*6,    0x1FFFFF,       0x10000,   /* 4 byte sequence */},
+    {0xFC,  0xF8,   4*6,    0x3FFFFFF,      0x200000,  /* 5 byte sequence */},
+    {0xFE,  0xFC,   5*6,    0x7FFFFFFF,     0x4000000, /* 6 byte sequence */},
+    {0,						       /* end of table    */}
+};
+
+#define UNICODE_MAX	0x0010ffff
+#define PLANE_SIZE	0x00010000
+
+#define SURROGATE_MASK	0xfffff800
+#define SURROGATE_PAIR	0x0000d800
+#define SURROGATE_LOW	0x00000400
+#define SURROGATE_BITS	0x000003ff
+
+int utf8_to_utf32(const u8 *s, int inlen, unicode_t *pu)
+{
+	unsigned long l;
+	int c0, c, nc;
+	const struct utf8_table *t;
+
+	nc = 0;
+	c0 = *s;
+	l = c0;
+	for (t = utf8_table; t->cmask; t++) {
+		nc++;
+		if ((c0 & t->cmask) == t->cval) {
+			l &= t->lmask;
+			if (l < t->lval || l > UNICODE_MAX ||
+					(l & SURROGATE_MASK) == SURROGATE_PAIR)
+				return -1;
+			*pu = (unicode_t) l;
+			return nc;
+		}
+		if (inlen <= nc)
+			return -1;
+		s++;
+		c = (*s ^ 0x80) & 0xFF;
+		if (c & 0xC0)
+			return -1;
+		l = (l << 6) | c;
+	}
+	return -1;
+}
+EXPORT_SYMBOL(utf8_to_utf32);
+
+static inline void put_utf16(wchar_t *s, unsigned c, enum utf16_endian endian)
+{
+	switch (endian) {
+	default:
+		*s = (wchar_t) c;
+		break;
+	case UTF16_LITTLE_ENDIAN:
+		*s = __cpu_to_le16(c);
+		break;
+	case UTF16_BIG_ENDIAN:
+		*s = __cpu_to_be16(c);
+		break;
+	}
+}
+
+int utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian,
+		wchar_t *pwcs, int maxout)
+{
+	u16 *op;
+	int size;
+	unicode_t u;
+
+	op = pwcs;
+	while (inlen > 0 && maxout > 0 && *s) {
+		if (*s & 0x80) {
+			size = utf8_to_utf32(s, inlen, &u);
+			if (size < 0)
+				return -EINVAL;
+			s += size;
+			inlen -= size;
+
+			if (u >= PLANE_SIZE) {
+				if (maxout < 2)
+					break;
+				u -= PLANE_SIZE;
+				put_utf16(op++, SURROGATE_PAIR |
+						((u >> 10) & SURROGATE_BITS),
+						endian);
+				put_utf16(op++, SURROGATE_PAIR |
+						SURROGATE_LOW |
+						(u & SURROGATE_BITS),
+						endian);
+				maxout -= 2;
+			} else {
+				put_utf16(op++, u, endian);
+				maxout--;
+			}
+		} else {
+			put_utf16(op++, *s++, endian);
+			inlen--;
+			maxout--;
+		}
+	}
+	return op - pwcs;
+}
+EXPORT_SYMBOL(utf8s_to_utf16s);
+
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-02-20  7:30 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-20  7:29 [PATCH 00/25] i.MX8MQ USB support Andrey Smirnov
2019-02-20  7:29 ` [PATCH 01/25] usb: xhci-hcd: Tabify the file Andrey Smirnov
2019-02-20  7:29 ` [PATCH 02/25] usb: xhci-hcd: Do not zero out DMA coherent memory Andrey Smirnov
2019-02-20  9:29   ` Yann Sionneau
2019-02-20 18:54     ` Andrey Smirnov
2019-02-23 10:16     ` Sam Ravnborg
2019-02-20  7:29 ` [PATCH 03/25] usb: xhci-hcd: Cast ~XHCI_IRQS to u32 Andrey Smirnov
2019-02-20  7:29 ` [PATCH 04/25] usb: xhci-hcd: Make sure to initialize xhci->rings_list Andrey Smirnov
2019-02-20  7:29 ` [PATCH 05/25] usb: xhci-hcd: Drop pointless bitwise or Andrey Smirnov
2019-02-20  7:29 ` [PATCH 06/25] usb: xhci-hcd: Add support for 64-byte context size Andrey Smirnov
2019-02-20  7:29 ` [PATCH 07/25] usb: xhci-hcd: Don't try to DMA sync if buffer is NULL Andrey Smirnov
2019-02-20  7:29 ` [PATCH 08/25] usb: xhci-hcd: Always wait for "Response Data" completion Andrey Smirnov
2019-02-20  7:29 ` [PATCH 09/25] usb: xhci-hcd: Convert xhci_submit_normal() to use dma_map_single() Andrey Smirnov
2019-02-20  7:29 ` [PATCH 10/25] usb: xhci-hcd: Convert xhci_submit_control() " Andrey Smirnov
2019-02-20  7:29 ` [PATCH 11/25] usb: xhci-hcd: Simplify TRB initialization code Andrey Smirnov
2019-02-20  7:29 ` [PATCH 12/25] usb: xhci-hcd: Drop 'dma' field from struct xhci_hcd Andrey Smirnov
2019-02-20  7:29 ` [PATCH 13/25] usb: xhci-hcd: Check usb_pipein(pipe) only once in xhci_submit_normal() Andrey Smirnov
2019-02-20  7:29 ` [PATCH 14/25] usb: xhci-hcd: Initialize TRT flag for xHCI >= 1.0 Andrey Smirnov
2019-02-20  7:29 ` [PATCH 15/25] usb: xhci-hcd: Simplify route string building loop Andrey Smirnov
2019-02-20  7:29 ` [PATCH 16/25] usb: xhci-hcd: Make use of lo_hi_readq/writeq() Andrey Smirnov
2019-02-20  7:29 ` [PATCH 17/25] phy: core: Assume EPROBE_DEFER in of_phy_provider_lookup() Andrey Smirnov
2019-02-20  7:29 ` [PATCH 18/25] phy: Port i.MX8MQ USB PHY driver from Linux Andrey Smirnov
2019-02-20  7:29 ` [PATCH 19/25] clk: Drop separate definitions of clk_put() Andrey Smirnov
2019-02-20  7:29 ` [PATCH 20/25] include/usb: Import USB_SPEED_SUPER_PLUS from Linux Andrey Smirnov
2019-02-20  7:29 ` [PATCH 21/25] clk: Import a subset of clk_bulk API " Andrey Smirnov
2019-02-20  7:29 ` [PATCH 22/25] usb: Import DWC3 USB controller driver " Andrey Smirnov
2019-02-20  7:29 ` Andrey Smirnov [this message]
2019-02-20  7:29 ` [PATCH 24/25] usb: Port Microchip USB251x USB hub " Andrey Smirnov
2019-02-20  7:29 ` [PATCH 25/25] usb: usb251xb: add usb data lane port swap feature Andrey Smirnov
2019-02-22  7:27 ` [PATCH 00/25] i.MX8MQ USB support Sascha Hauer

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=20190220072930.14300-24-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox