blob: 206c89f0dbf7124f127398efdeb4c48f2e9966d0 [file] [log] [blame]
Thomas Gleixner9f806852019-05-29 07:18:08 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Olaf Weber44594c22019-04-25 13:45:46 -04002/*
3 * Copyright (c) 2014 SGI.
4 * All rights reserved.
Olaf Weber44594c22019-04-25 13:45:46 -04005 */
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>
Christoph Hellwig49bd03c2021-09-15 09:00:00 +020014#include <linux/unicode.h>
Olaf Weber44594c22019-04-25 13:45:46 -040015
Christoph Hellwig49bd03c2021-09-15 09:00:00 +020016int utf8version_is_supported(unsigned int version);
Olaf Weber44594c22019-04-25 13:45:46 -040017
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 */
33extern const struct utf8data *utf8nfdi(unsigned int maxage);
34extern const struct utf8data *utf8nfdicf(unsigned int maxage);
35
36/*
Olaf Weber44594c22019-04-25 13:45:46 -040037 * 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 */
Christoph Hellwig6ca99ce2021-09-15 09:00:04 +020042ssize_t utf8nlen(const struct unicode_map *um, enum utf8_normalization n,
43 const char *s, size_t len);
Olaf Weber44594c22019-04-25 13:45:46 -040044
Olaf Webera8384c62019-04-25 13:49:18 -040045/* Needed in struct utf8cursor below. */
46#define UTF8HANGULLEAF (12)
47
Olaf Weber44594c22019-04-25 13:45:46 -040048/*
49 * Cursor structure used by the normalizer.
50 */
51struct utf8cursor {
Christoph Hellwig6ca99ce2021-09-15 09:00:04 +020052 const struct unicode_map *um;
53 enum utf8_normalization n;
Olaf Weber44594c22019-04-25 13:45:46 -040054 const char *s;
55 const char *p;
56 const char *ss;
57 const char *sp;
58 unsigned int len;
59 unsigned int slen;
60 short int ccc;
61 short int nccc;
Olaf Webera8384c62019-04-25 13:49:18 -040062 unsigned char hangul[UTF8HANGULLEAF];
Olaf Weber44594c22019-04-25 13:45:46 -040063};
64
65/*
66 * Initialize a utf8cursor to normalize a string.
67 * Returns 0 on success.
68 * Returns -1 on failure.
69 */
Christoph Hellwig6ca99ce2021-09-15 09:00:04 +020070int utf8ncursor(struct utf8cursor *u8c, const struct unicode_map *um,
71 enum utf8_normalization n, const char *s, size_t len);
Olaf Weber44594c22019-04-25 13:45:46 -040072
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 */
79extern int utf8byte(struct utf8cursor *u8c);
80
81#endif /* UTF8NORM_H */