diff mbox series

[16/20] nls: ascii: Support casefold and normalization operations

Message ID 20180703170700.9306-17-krisman@collabora.co.uk
State Changes Requested
Headers show
Series EXT4 encoding support | expand

Commit Message

Gabriel Krisman Bertazi July 3, 2018, 5:06 p.m. UTC
Normalization is identity, but casefold can be implemented with toupper
or tolower, and we have no specification on that. We should be safe, as
long as it is constant.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.co.uk>
---
 fs/nls/nls_ascii.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
diff mbox series

Patch

diff --git a/fs/nls/nls_ascii.c b/fs/nls/nls_ascii.c
index 2f4826478d3d..40b8e7acfe1e 100644
--- a/fs/nls/nls_ascii.c
+++ b/fs/nls/nls_ascii.c
@@ -12,6 +12,7 @@ 
 #include <linux/string.h>
 #include <linux/nls.h>
 #include <linux/errno.h>
+#include <linux/slab.h>
 
 static const wchar_t charset2uni[256] = {
 	/* 0x00*/
@@ -152,11 +153,43 @@  static unsigned char charset_toupper(const struct nls_table *table,
 	return charset2upper[c];
 }
 
+/* Ascii casefold can be defined as either to lower or to upper. As long
+ * as it is stable. */
+static int ascii_casefold(const struct nls_table *charset,
+			  const unsigned char *str, size_t len,
+			  unsigned char *dest, size_t dlen)
+{
+	unsigned int i;
+
+	if (dlen < len)
+		return -EINVAL;
+
+	for (i = 0; i < len; i++)
+		dest[i] = charset_tolower(charset, str[i]);
+
+	return 0;
+}
+
+/* Ascii normalization is identity. */
+static int ascii_normalize(const struct nls_table *charset,
+			   const unsigned char *str, size_t len,
+			   unsigned char *dest, size_t dlen)
+{
+	if (dlen < len)
+		return -EINVAL;
+
+	memcpy(dest, str, len);
+
+	return 0;
+}
+
 static const struct nls_ops charset_ops = {
 	.lowercase = charset_toupper,
 	.uppercase = charset_tolower,
 	.uni2char = uni2char,
 	.char2uni = char2uni,
+	.casefold = ascii_casefold,
+	.normalize = ascii_normalize,
 };
 
 static struct nls_charset nls_charset;