Whamcloud - gitweb
ext2fs: add method to validate casefolded strings
authorGabriel Krisman Bertazi <krisman@collabora.com>
Thu, 17 Dec 2020 17:35:35 +0000 (18:35 +0100)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 28 Jan 2021 03:09:36 +0000 (22:09 -0500)
This is exported to be used by fsck.

Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/ext2fs.h
lib/ext2fs/ext2fsP.h
lib/ext2fs/nls_utf8.c

index b1752ac..68f6b02 100644 (file)
@@ -1633,6 +1633,8 @@ extern errcode_t ext2fs_new_dir_inline_data(ext2_filsys fs, ext2_ino_t dir_ino,
 
 /* nls_utf8.c */
 extern const struct ext2fs_nls_table *ext2fs_load_nls_table(int encoding);
+extern int ext2fs_check_encoded_name(const struct ext2fs_nls_table *table,
+                                    char *s, size_t len, char **pos);
 
 /* mkdir.c */
 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
index ad8b7d5..30564de 100644 (file)
@@ -104,6 +104,8 @@ struct ext2fs_nls_ops {
        int (*casefold)(const struct ext2fs_nls_table *charset,
                        const unsigned char *str, size_t len,
                        unsigned char *dest, size_t dlen);
+       int (*validate)(const struct ext2fs_nls_table *table,
+                       char *s, size_t len, char **pos);
 };
 
 /* Function prototypes */
index e4c4e7a..7d2cf42 100644 (file)
@@ -920,8 +920,30 @@ invalid_seq:
        return -EINVAL;
 }
 
+static int utf8_validate(const struct ext2fs_nls_table *table,
+                        char *s, size_t len, char **pos)
+{
+       const struct utf8data *data = utf8nfdicf(table->version);
+       utf8leaf_t      *leaf;
+       unsigned char   hangul[UTF8HANGULLEAF];
+
+       if (!data)
+               return -1;
+       while (len && *s) {
+               leaf = utf8nlookup(data, hangul, s, len);
+               if (!leaf) {
+                       *pos = s;
+                       return 1;
+               }
+               len -= utf8clen(s);
+               s += utf8clen(s);
+       }
+       return 0;
+}
+
 static const struct ext2fs_nls_ops utf8_ops = {
        .casefold = utf8_casefold,
+       .validate = utf8_validate,
 };
 
 static const struct ext2fs_nls_table nls_utf8 = {
@@ -936,3 +958,9 @@ const struct ext2fs_nls_table *ext2fs_load_nls_table(int encoding)
 
        return NULL;
 }
+
+int ext2fs_check_encoded_name(const struct ext2fs_nls_table *table,
+                             char *name, size_t len, char **pos)
+{
+       return table->ops->validate(table, name, len, pos);
+}