drm/panfrost: Remove set but not used variable 'bo'
[linux/fpc-iii.git] / fs / unicode / utf8-core.c
blob2a878b739115d2e2734a62b04ec40d8c20a1e586
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>
8 #include <linux/unicode.h>
10 #include "utf8n.h"
12 int utf8_validate(const struct unicode_map *um, const struct qstr *str)
14 const struct utf8data *data = utf8nfdi(um->version);
16 if (utf8nlen(data, str->name, str->len) < 0)
17 return -1;
18 return 0;
20 EXPORT_SYMBOL(utf8_validate);
22 int utf8_strncmp(const struct unicode_map *um,
23 const struct qstr *s1, const struct qstr *s2)
25 const struct utf8data *data = utf8nfdi(um->version);
26 struct utf8cursor cur1, cur2;
27 int c1, c2;
29 if (utf8ncursor(&cur1, data, s1->name, s1->len) < 0)
30 return -EINVAL;
32 if (utf8ncursor(&cur2, data, s2->name, s2->len) < 0)
33 return -EINVAL;
35 do {
36 c1 = utf8byte(&cur1);
37 c2 = utf8byte(&cur2);
39 if (c1 < 0 || c2 < 0)
40 return -EINVAL;
41 if (c1 != c2)
42 return 1;
43 } while (c1);
45 return 0;
47 EXPORT_SYMBOL(utf8_strncmp);
49 int utf8_strncasecmp(const struct unicode_map *um,
50 const struct qstr *s1, const struct qstr *s2)
52 const struct utf8data *data = utf8nfdicf(um->version);
53 struct utf8cursor cur1, cur2;
54 int c1, c2;
56 if (utf8ncursor(&cur1, data, s1->name, s1->len) < 0)
57 return -EINVAL;
59 if (utf8ncursor(&cur2, data, s2->name, s2->len) < 0)
60 return -EINVAL;
62 do {
63 c1 = utf8byte(&cur1);
64 c2 = utf8byte(&cur2);
66 if (c1 < 0 || c2 < 0)
67 return -EINVAL;
68 if (c1 != c2)
69 return 1;
70 } while (c1);
72 return 0;
74 EXPORT_SYMBOL(utf8_strncasecmp);
76 /* String cf is expected to be a valid UTF-8 casefolded
77 * string.
79 int utf8_strncasecmp_folded(const struct unicode_map *um,
80 const struct qstr *cf,
81 const struct qstr *s1)
83 const struct utf8data *data = utf8nfdicf(um->version);
84 struct utf8cursor cur1;
85 int c1, c2;
86 int i = 0;
88 if (utf8ncursor(&cur1, data, s1->name, s1->len) < 0)
89 return -EINVAL;
91 do {
92 c1 = utf8byte(&cur1);
93 c2 = cf->name[i++];
94 if (c1 < 0)
95 return -EINVAL;
96 if (c1 != c2)
97 return 1;
98 } while (c1);
100 return 0;
102 EXPORT_SYMBOL(utf8_strncasecmp_folded);
104 int utf8_casefold(const struct unicode_map *um, const struct qstr *str,
105 unsigned char *dest, size_t dlen)
107 const struct utf8data *data = utf8nfdicf(um->version);
108 struct utf8cursor cur;
109 size_t nlen = 0;
111 if (utf8ncursor(&cur, data, str->name, str->len) < 0)
112 return -EINVAL;
114 for (nlen = 0; nlen < dlen; nlen++) {
115 int c = utf8byte(&cur);
117 dest[nlen] = c;
118 if (!c)
119 return nlen;
120 if (c == -1)
121 break;
123 return -EINVAL;
126 EXPORT_SYMBOL(utf8_casefold);
128 int utf8_normalize(const struct unicode_map *um, const struct qstr *str,
129 unsigned char *dest, size_t dlen)
131 const struct utf8data *data = utf8nfdi(um->version);
132 struct utf8cursor cur;
133 ssize_t nlen = 0;
135 if (utf8ncursor(&cur, data, str->name, str->len) < 0)
136 return -EINVAL;
138 for (nlen = 0; nlen < dlen; nlen++) {
139 int c = utf8byte(&cur);
141 dest[nlen] = c;
142 if (!c)
143 return nlen;
144 if (c == -1)
145 break;
147 return -EINVAL;
150 EXPORT_SYMBOL(utf8_normalize);
152 static int utf8_parse_version(const char *version, unsigned int *maj,
153 unsigned int *min, unsigned int *rev)
155 substring_t args[3];
156 char version_string[12];
157 static const struct match_token token[] = {
158 {1, "%d.%d.%d"},
159 {0, NULL}
162 strncpy(version_string, version, sizeof(version_string));
164 if (match_token(version_string, token, args) != 1)
165 return -EINVAL;
167 if (match_int(&args[0], maj) || match_int(&args[1], min) ||
168 match_int(&args[2], rev))
169 return -EINVAL;
171 return 0;
174 struct unicode_map *utf8_load(const char *version)
176 struct unicode_map *um = NULL;
177 int unicode_version;
179 if (version) {
180 unsigned int maj, min, rev;
182 if (utf8_parse_version(version, &maj, &min, &rev) < 0)
183 return ERR_PTR(-EINVAL);
185 if (!utf8version_is_supported(maj, min, rev))
186 return ERR_PTR(-EINVAL);
188 unicode_version = UNICODE_AGE(maj, min, rev);
189 } else {
190 unicode_version = utf8version_latest();
191 printk(KERN_WARNING"UTF-8 version not specified. "
192 "Assuming latest supported version (%d.%d.%d).",
193 (unicode_version >> 16) & 0xff,
194 (unicode_version >> 8) & 0xff,
195 (unicode_version & 0xff));
198 um = kzalloc(sizeof(struct unicode_map), GFP_KERNEL);
199 if (!um)
200 return ERR_PTR(-ENOMEM);
202 um->charset = "UTF-8";
203 um->version = unicode_version;
205 return um;
207 EXPORT_SYMBOL(utf8_load);
209 void utf8_unload(struct unicode_map *um)
211 kfree(um);
213 EXPORT_SYMBOL(utf8_unload);
215 MODULE_LICENSE("GPL v2");