Skip to contentSkip to conversation

nls: add surroagate pair support in nls utf8.

PATCH New Namjae Jeon 1 participant 1 message Dec 4, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nls: add surroagate pair support in nls utf8.

It allows surrogate pair in nls utf8.

---
 fs/nls/nls_base.c   |    8 --------
 fs/nls/nls_utf8.c   |   15 +++++++++++++--
 include/linux/nls.h |    8 ++++++++
 3 files changed, 21 insertions(+), 10 deletions(-)
fs/nls/nls_base.c +0 -8
index 44a88a9..07a12cc 100644
@@ -44,14 +44,6 @@ static const struct utf8_table utf8_table[] =
     {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 len, unicode_t *pu)
 {
 	unsigned long l;
fs/nls/nls_utf8.c +13 -2
index 0d60a44..be7685a 100644
@@ -30,13 +30,24 @@ static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
 {
 	int n;
 	unicode_t u;
+	u16 *op;
 
+	op = uni;
 	n = utf8_to_utf32(rawstring, boundlen, &u);
-	if (n < 0 || u > MAX_WCHAR_T) {
+	if (n < 0 || u > UNICODE_MAX) {
 		*uni = 0x003f;	/* ? */
 		return -EINVAL;
 	}
-	*uni = (wchar_t) u;
+
+	if (u >= PLANE_SIZE) {
+		u -= PLANE_SIZE;
+		*op++ = (wchar_t) (SURROGATE_PAIR |
+		((u >> 10) & SURROGATE_BITS));
+		*op++ = (wchar_t) (SURROGATE_PAIR |
+		SURROGATE_LOW | (u & SURROGATE_BITS));
+		} else
+			*op++ = (wchar_t) u;
+
 	return n;
 }
 
include/linux/nls.h +8 -0
index d47beef..253cbd2 100644
@@ -18,6 +18,14 @@
 typedef u16 wchar_t;
 #define MAX_WCHAR_T	0xffff
 
+/* Plane-1 Unicode surrogate pair characters */
+#define UNICODE_MAX	0x0010ffff
+#define PLANE_SIZE	0x00010000
+#define SURROGATE_MASK	0xfffff800
+#define SURROGATE_PAIR	0x0000d800
+#define SURROGATE_LOW	0x00000400
+#define SURROGATE_BITS	0x000003ff
+
 /* Arbitrary Unicode character */
 typedef u32 unicode_t;
 
Signature · 1 line
1.7.5.4