Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/parser.h> |
| 7 | #include <linux/errno.h> |
Daniel Rosenberg | 3d7bfea | 2020-07-08 02:12:34 -0700 | [diff] [blame] | 8 | #include <linux/stringhash.h> |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 9 | |
| 10 | #include "utf8n.h" |
| 11 | |
| 12 | int utf8_validate(const struct unicode_map *um, const struct qstr *str) |
| 13 | { |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 14 | if (utf8nlen(um, UTF8_NFDI, str->name, str->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 15 | return -1; |
| 16 | return 0; |
| 17 | } |
| 18 | EXPORT_SYMBOL(utf8_validate); |
| 19 | |
| 20 | int utf8_strncmp(const struct unicode_map *um, |
| 21 | const struct qstr *s1, const struct qstr *s2) |
| 22 | { |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 23 | struct utf8cursor cur1, cur2; |
| 24 | int c1, c2; |
| 25 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 26 | if (utf8ncursor(&cur1, um, UTF8_NFDI, s1->name, s1->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 27 | return -EINVAL; |
| 28 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 29 | if (utf8ncursor(&cur2, um, UTF8_NFDI, s2->name, s2->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 30 | return -EINVAL; |
| 31 | |
| 32 | do { |
| 33 | c1 = utf8byte(&cur1); |
| 34 | c2 = utf8byte(&cur2); |
| 35 | |
| 36 | if (c1 < 0 || c2 < 0) |
| 37 | return -EINVAL; |
| 38 | if (c1 != c2) |
| 39 | return 1; |
| 40 | } while (c1); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | EXPORT_SYMBOL(utf8_strncmp); |
| 45 | |
| 46 | int utf8_strncasecmp(const struct unicode_map *um, |
| 47 | const struct qstr *s1, const struct qstr *s2) |
| 48 | { |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 49 | struct utf8cursor cur1, cur2; |
| 50 | int c1, c2; |
| 51 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 52 | if (utf8ncursor(&cur1, um, UTF8_NFDICF, s1->name, s1->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 53 | return -EINVAL; |
| 54 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 55 | if (utf8ncursor(&cur2, um, UTF8_NFDICF, s2->name, s2->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 56 | return -EINVAL; |
| 57 | |
| 58 | do { |
| 59 | c1 = utf8byte(&cur1); |
| 60 | c2 = utf8byte(&cur2); |
| 61 | |
| 62 | if (c1 < 0 || c2 < 0) |
| 63 | return -EINVAL; |
| 64 | if (c1 != c2) |
| 65 | return 1; |
| 66 | } while (c1); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | EXPORT_SYMBOL(utf8_strncasecmp); |
| 71 | |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 72 | /* String cf is expected to be a valid UTF-8 casefolded |
| 73 | * string. |
| 74 | */ |
| 75 | int utf8_strncasecmp_folded(const struct unicode_map *um, |
| 76 | const struct qstr *cf, |
| 77 | const struct qstr *s1) |
| 78 | { |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 79 | struct utf8cursor cur1; |
| 80 | int c1, c2; |
| 81 | int i = 0; |
| 82 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 83 | if (utf8ncursor(&cur1, um, UTF8_NFDICF, s1->name, s1->len) < 0) |
Gabriel Krisman Bertazi | 3ae7256 | 2019-06-19 23:45:09 -0400 | [diff] [blame] | 84 | return -EINVAL; |
| 85 | |
| 86 | do { |
| 87 | c1 = utf8byte(&cur1); |
| 88 | c2 = cf->name[i++]; |
| 89 | if (c1 < 0) |
| 90 | return -EINVAL; |
| 91 | if (c1 != c2) |
| 92 | return 1; |
| 93 | } while (c1); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | EXPORT_SYMBOL(utf8_strncasecmp_folded); |
| 98 | |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 99 | int utf8_casefold(const struct unicode_map *um, const struct qstr *str, |
| 100 | unsigned char *dest, size_t dlen) |
| 101 | { |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 102 | struct utf8cursor cur; |
| 103 | size_t nlen = 0; |
| 104 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 105 | if (utf8ncursor(&cur, um, UTF8_NFDICF, str->name, str->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 106 | return -EINVAL; |
| 107 | |
| 108 | for (nlen = 0; nlen < dlen; nlen++) { |
| 109 | int c = utf8byte(&cur); |
| 110 | |
| 111 | dest[nlen] = c; |
| 112 | if (!c) |
| 113 | return nlen; |
| 114 | if (c == -1) |
| 115 | break; |
| 116 | } |
| 117 | return -EINVAL; |
| 118 | } |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 119 | EXPORT_SYMBOL(utf8_casefold); |
| 120 | |
Daniel Rosenberg | 3d7bfea | 2020-07-08 02:12:34 -0700 | [diff] [blame] | 121 | int utf8_casefold_hash(const struct unicode_map *um, const void *salt, |
| 122 | struct qstr *str) |
| 123 | { |
Daniel Rosenberg | 3d7bfea | 2020-07-08 02:12:34 -0700 | [diff] [blame] | 124 | struct utf8cursor cur; |
| 125 | int c; |
| 126 | unsigned long hash = init_name_hash(salt); |
| 127 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 128 | if (utf8ncursor(&cur, um, UTF8_NFDICF, str->name, str->len) < 0) |
Daniel Rosenberg | 3d7bfea | 2020-07-08 02:12:34 -0700 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | |
| 131 | while ((c = utf8byte(&cur))) { |
| 132 | if (c < 0) |
| 133 | return -EINVAL; |
| 134 | hash = partial_name_hash((unsigned char)c, hash); |
| 135 | } |
| 136 | str->hash = end_name_hash(hash); |
| 137 | return 0; |
| 138 | } |
| 139 | EXPORT_SYMBOL(utf8_casefold_hash); |
| 140 | |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 141 | int utf8_normalize(const struct unicode_map *um, const struct qstr *str, |
| 142 | unsigned char *dest, size_t dlen) |
| 143 | { |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 144 | struct utf8cursor cur; |
| 145 | ssize_t nlen = 0; |
| 146 | |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 147 | if (utf8ncursor(&cur, um, UTF8_NFDI, str->name, str->len) < 0) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 148 | return -EINVAL; |
| 149 | |
| 150 | for (nlen = 0; nlen < dlen; nlen++) { |
| 151 | int c = utf8byte(&cur); |
| 152 | |
| 153 | dest[nlen] = c; |
| 154 | if (!c) |
| 155 | return nlen; |
| 156 | if (c == -1) |
| 157 | break; |
| 158 | } |
| 159 | return -EINVAL; |
| 160 | } |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 161 | EXPORT_SYMBOL(utf8_normalize); |
| 162 | |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 163 | static const struct utf8data *find_table_version(const struct utf8data *table, |
| 164 | size_t nr_entries, unsigned int version) |
| 165 | { |
| 166 | size_t i = nr_entries - 1; |
| 167 | |
| 168 | while (version < table[i].maxage) |
| 169 | i--; |
| 170 | if (version > table[i].maxage) |
| 171 | return NULL; |
| 172 | return &table[i]; |
| 173 | } |
| 174 | |
Christoph Hellwig | 49bd03c | 2021-09-15 09:00:00 +0200 | [diff] [blame] | 175 | struct unicode_map *utf8_load(unsigned int version) |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 176 | { |
Christoph Hellwig | 49bd03c | 2021-09-15 09:00:00 +0200 | [diff] [blame] | 177 | struct unicode_map *um; |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 178 | |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 179 | um = kzalloc(sizeof(struct unicode_map), GFP_KERNEL); |
| 180 | if (!um) |
| 181 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 49bd03c | 2021-09-15 09:00:00 +0200 | [diff] [blame] | 182 | um->version = version; |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 183 | |
| 184 | um->tables = symbol_request(utf8_data_table); |
| 185 | if (!um->tables) |
| 186 | goto out_free_um; |
| 187 | |
| 188 | if (!utf8version_is_supported(um, version)) |
| 189 | goto out_symbol_put; |
| 190 | um->ntab[UTF8_NFDI] = find_table_version(um->tables->utf8nfdidata, |
| 191 | um->tables->utf8nfdidata_size, um->version); |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 192 | if (!um->ntab[UTF8_NFDI]) |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 193 | goto out_symbol_put; |
| 194 | um->ntab[UTF8_NFDICF] = find_table_version(um->tables->utf8nfdicfdata, |
| 195 | um->tables->utf8nfdicfdata_size, um->version); |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 196 | if (!um->ntab[UTF8_NFDICF]) |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 197 | goto out_symbol_put; |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 198 | return um; |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 199 | |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 200 | out_symbol_put: |
| 201 | symbol_put(um->tables); |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame] | 202 | out_free_um: |
| 203 | kfree(um); |
| 204 | return ERR_PTR(-EINVAL); |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 205 | } |
| 206 | EXPORT_SYMBOL(utf8_load); |
| 207 | |
| 208 | void utf8_unload(struct unicode_map *um) |
| 209 | { |
Christoph Hellwig | 2b3d047 | 2021-09-15 09:00:05 +0200 | [diff] [blame] | 210 | if (um) { |
| 211 | symbol_put(utf8_data_table); |
| 212 | kfree(um); |
| 213 | } |
Gabriel Krisman Bertazi | 9d53690 | 2019-04-25 13:51:22 -0400 | [diff] [blame] | 214 | } |
| 215 | EXPORT_SYMBOL(utf8_unload); |
| 216 | |
| 217 | MODULE_LICENSE("GPL v2"); |