Thomas Gleixner | 9f80685 | 2019-05-29 07:18:08 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 SGI. |
| 4 | * All rights reserved. |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 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> |
Christoph Hellwig | 49bd03c | 2021-09-15 09:00:00 +0200 | [diff] [blame] | 14 | #include <linux/unicode.h> |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 15 | |
Christoph Hellwig | 49bd03c | 2021-09-15 09:00:00 +0200 | [diff] [blame] | 16 | int utf8version_is_supported(unsigned int version); |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 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 | /* |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 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 | */ |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame^] | 42 | ssize_t utf8nlen(const struct unicode_map *um, enum utf8_normalization n, |
| 43 | const char *s, size_t len); |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 44 | |
Olaf Weber | a8384c6 | 2019-04-25 13:49:18 -0400 | [diff] [blame] | 45 | /* Needed in struct utf8cursor below. */ |
| 46 | #define UTF8HANGULLEAF (12) |
| 47 | |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 48 | /* |
| 49 | * Cursor structure used by the normalizer. |
| 50 | */ |
| 51 | struct utf8cursor { |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame^] | 52 | const struct unicode_map *um; |
| 53 | enum utf8_normalization n; |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 54 | 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 Weber | a8384c6 | 2019-04-25 13:49:18 -0400 | [diff] [blame] | 62 | unsigned char hangul[UTF8HANGULLEAF]; |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * Initialize a utf8cursor to normalize a string. |
| 67 | * Returns 0 on success. |
| 68 | * Returns -1 on failure. |
| 69 | */ |
Christoph Hellwig | 6ca99ce | 2021-09-15 09:00:04 +0200 | [diff] [blame^] | 70 | int utf8ncursor(struct utf8cursor *u8c, const struct unicode_map *um, |
| 71 | enum utf8_normalization n, const char *s, size_t len); |
Olaf Weber | 44594c2 | 2019-04-25 13:45:46 -0400 | [diff] [blame] | 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 */ |