Skip to content

Commit 3d7bfea

Browse files
drosen-googleJaegeuk Kim
authored andcommitted
unicode: Add utf8_casefold_hash
This adds a case insensitive hash function to allow taking the hash without needing to allocate a casefolded copy of the string. The existing d_hash implementations for casefolding allocate memory within rcu-walk, by avoiding it we can be more efficient and avoid worrying about a failed allocation. Signed-off-by: Daniel Rosenberg <drosen@google.com> Reviewed-by: Gabriel Krisman Bertazi <krisman@collabora.com> Reviewed-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent e6c3948 commit 3d7bfea

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

fs/unicode/utf8-core.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/parser.h>
77
#include <linux/errno.h>
88
#include <linux/unicode.h>
9+
#include <linux/stringhash.h>
910

1011
#include "utf8n.h"
1112

@@ -122,9 +123,29 @@ int utf8_casefold(const struct unicode_map *um, const struct qstr *str,
122123
}
123124
return -EINVAL;
124125
}
125-
126126
EXPORT_SYMBOL(utf8_casefold);
127127

128+
int utf8_casefold_hash(const struct unicode_map *um, const void *salt,
129+
struct qstr *str)
130+
{
131+
const struct utf8data *data = utf8nfdicf(um->version);
132+
struct utf8cursor cur;
133+
int c;
134+
unsigned long hash = init_name_hash(salt);
135+
136+
if (utf8ncursor(&cur, data, str->name, str->len) < 0)
137+
return -EINVAL;
138+
139+
while ((c = utf8byte(&cur))) {
140+
if (c < 0)
141+
return -EINVAL;
142+
hash = partial_name_hash((unsigned char)c, hash);
143+
}
144+
str->hash = end_name_hash(hash);
145+
return 0;
146+
}
147+
EXPORT_SYMBOL(utf8_casefold_hash);
148+
128149
int utf8_normalize(const struct unicode_map *um, const struct qstr *str,
129150
unsigned char *dest, size_t dlen)
130151
{

include/linux/unicode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ int utf8_normalize(const struct unicode_map *um, const struct qstr *str,
2727
int utf8_casefold(const struct unicode_map *um, const struct qstr *str,
2828
unsigned char *dest, size_t dlen);
2929

30+
int utf8_casefold_hash(const struct unicode_map *um, const void *salt,
31+
struct qstr *str);
32+
3033
struct unicode_map *utf8_load(const char *version);
3134
void utf8_unload(struct unicode_map *um);
3235

0 commit comments

Comments
 (0)