unicode: simplify utf8len
[platform/kernel/linux-rpi.git] / fs / unicode / utf8n.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2014 SGI.
4  * All rights reserved.
5  */
6
7 #ifndef UTF8NORM_H
8 #define UTF8NORM_H
9
10 #include <linux/types.h>
11 #include <linux/export.h>
12 #include <linux/string.h>
13 #include <linux/module.h>
14 #include <linux/unicode.h>
15
16 int utf8version_is_supported(unsigned int version);
17
18 /*
19  * Look for the correct const struct utf8data for a unicode version.
20  * Returns NULL if the version requested is too new.
21  *
22  * Two normalization forms are supported: nfdi and nfdicf.
23  *
24  * nfdi:
25  *  - Apply unicode normalization form NFD.
26  *  - Remove any Default_Ignorable_Code_Point.
27  *
28  * nfdicf:
29  *  - Apply unicode normalization form NFD.
30  *  - Remove any Default_Ignorable_Code_Point.
31  *  - Apply a full casefold (C + F).
32  */
33 extern const struct utf8data *utf8nfdi(unsigned int maxage);
34 extern const struct utf8data *utf8nfdicf(unsigned int maxage);
35
36 /*
37  * Determine the length of the normalized from of the string,
38  * excluding any terminating NULL byte.
39  * Returns 0 if only ignorable code points are present.
40  * Returns -1 if the input is not valid UTF-8.
41  */
42 extern ssize_t utf8nlen(const struct utf8data *data, const char *s, size_t len);
43
44 /* Needed in struct utf8cursor below. */
45 #define UTF8HANGULLEAF  (12)
46
47 /*
48  * Cursor structure used by the normalizer.
49  */
50 struct utf8cursor {
51         const struct utf8data   *data;
52         const char      *s;
53         const char      *p;
54         const char      *ss;
55         const char      *sp;
56         unsigned int    len;
57         unsigned int    slen;
58         short int       ccc;
59         short int       nccc;
60         unsigned char   hangul[UTF8HANGULLEAF];
61 };
62
63 /*
64  * Initialize a utf8cursor to normalize a string.
65  * Returns 0 on success.
66  * Returns -1 on failure.
67  */
68 extern int utf8cursor(struct utf8cursor *u8c, const struct utf8data *data,
69                       const char *s);
70 extern int utf8ncursor(struct utf8cursor *u8c, const struct utf8data *data,
71                        const char *s, size_t len);
72
73 /*
74  * Get the next byte in the normalization.
75  * Returns a value > 0 && < 256 on success.
76  * Returns 0 when the end of the normalization is reached.
77  * Returns -1 if the string being normalized is not valid UTF-8.
78  */
79 extern int utf8byte(struct utf8cursor *u8c);
80
81 #endif /* UTF8NORM_H */