1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2014 SGI.
10 #include <linux/types.h>
11 #include <linux/export.h>
12 #include <linux/string.h>
13 #include <linux/module.h>
15 /* Encoding a unicode version number as a single unsigned int. */
16 #define UNICODE_MAJ_SHIFT (16)
17 #define UNICODE_MIN_SHIFT (8)
19 #define UNICODE_AGE(MAJ, MIN, REV) \
20 (((unsigned int)(MAJ) << UNICODE_MAJ_SHIFT) | \
21 ((unsigned int)(MIN) << UNICODE_MIN_SHIFT) | \
22 ((unsigned int)(REV)))
24 /* Highest unicode version supported by the data tables. */
25 extern int utf8version_is_supported(u8 maj
, u8 min
, u8 rev
);
26 extern int utf8version_latest(void);
29 * Look for the correct const struct utf8data for a unicode version.
30 * Returns NULL if the version requested is too new.
32 * Two normalization forms are supported: nfdi and nfdicf.
35 * - Apply unicode normalization form NFD.
36 * - Remove any Default_Ignorable_Code_Point.
39 * - Apply unicode normalization form NFD.
40 * - Remove any Default_Ignorable_Code_Point.
41 * - Apply a full casefold (C + F).
43 extern const struct utf8data
*utf8nfdi(unsigned int maxage
);
44 extern const struct utf8data
*utf8nfdicf(unsigned int maxage
);
47 * Determine the maximum age of any unicode character in the string.
48 * Returns 0 if only unassigned code points are present.
49 * Returns -1 if the input is not valid UTF-8.
51 extern int utf8agemax(const struct utf8data
*data
, const char *s
);
52 extern int utf8nagemax(const struct utf8data
*data
, const char *s
, size_t len
);
55 * Determine the minimum age of any unicode character in the string.
56 * Returns 0 if any unassigned code points are present.
57 * Returns -1 if the input is not valid UTF-8.
59 extern int utf8agemin(const struct utf8data
*data
, const char *s
);
60 extern int utf8nagemin(const struct utf8data
*data
, const char *s
, size_t len
);
63 * Determine the length of the normalized from of the string,
64 * excluding any terminating NULL byte.
65 * Returns 0 if only ignorable code points are present.
66 * Returns -1 if the input is not valid UTF-8.
68 extern ssize_t
utf8len(const struct utf8data
*data
, const char *s
);
69 extern ssize_t
utf8nlen(const struct utf8data
*data
, const char *s
, size_t len
);
71 /* Needed in struct utf8cursor below. */
72 #define UTF8HANGULLEAF (12)
75 * Cursor structure used by the normalizer.
78 const struct utf8data
*data
;
87 unsigned char hangul
[UTF8HANGULLEAF
];
91 * Initialize a utf8cursor to normalize a string.
92 * Returns 0 on success.
93 * Returns -1 on failure.
95 extern int utf8cursor(struct utf8cursor
*u8c
, const struct utf8data
*data
,
97 extern int utf8ncursor(struct utf8cursor
*u8c
, const struct utf8data
*data
,
98 const char *s
, size_t len
);
101 * Get the next byte in the normalization.
102 * Returns a value > 0 && < 256 on success.
103 * Returns 0 when the end of the normalization is reached.
104 * Returns -1 if the string being normalized is not valid UTF-8.
106 extern int utf8byte(struct utf8cursor
*u8c
);
108 #endif /* UTF8NORM_H */