1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * charset conversion utils
5 * Copyright (c) 2017 Rob Clark
11 #include <linux/kernel.h>
12 #include <linux/types.h>
14 #define MAX_UTF8_PER_UTF16 3
17 * utf8_get() - get next UTF-8 code point from buffer
19 * @src: pointer to current byte, updated to point to next byte
20 * Return: code point, or 0 for end of string, or -1 if no legal
21 * code point is found. In case of an error src points to
24 s32 utf8_get(const char **src);
27 * utf8_put() - write UTF-8 code point to buffer
30 * @dst: pointer to destination buffer, updated to next position
31 * Return: -1 if the input parameters are invalid
33 int utf8_put(s32 code, char **dst);
36 * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
40 * @count: maximum number of code points to convert
41 * Return: length in bytes after conversion to utf-16 without the
42 * trailing \0. If an invalid UTF-8 sequence is hit one
43 * word will be reserved for a replacement character.
45 size_t utf8_utf16_strnlen(const char *src, size_t count);
48 * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
51 * Return: length in bytes after conversion to utf-16 without the
52 * trailing \0. -1 if the utf-8 string is not valid.
54 #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
57 * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
59 * @dst: destination buffer
61 * @count: maximum number of code points to copy
62 * Return: -1 if the input parameters are invalid
64 int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
67 * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
69 * @dst: destination buffer
71 * Return: -1 if the input parameters are invalid
73 #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
76 * utf16_get() - get next UTF-16 code point from buffer
78 * @src: pointer to current word, updated to point to next word
79 * Return: code point, or 0 for end of string, or -1 if no legal
80 * code point is found. In case of an error src points to
83 s32 utf16_get(const u16 **src);
86 * utf16_put() - write UTF-16 code point to buffer
89 * @dst: pointer to destination buffer, updated to next position
90 * Return: -1 if the input parameters are invalid
92 int utf16_put(s32 code, u16 **dst);
95 * utf16_strnlen() - length of a truncated utf-16 string
98 * @count: maximum number of code points to convert
99 * Return: length in code points. If an invalid UTF-16 sequence is
100 * hit one position will be reserved for a replacement
103 size_t utf16_strnlen(const u16 *src, size_t count);
106 * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
109 * @src: utf-16 string
110 * @count: maximum number of code points to convert
111 * Return: length in bytes after conversion to utf-8 without the
112 * trailing \0. If an invalid UTF-16 sequence is hit one
113 * byte will be reserved for a replacement character.
115 size_t utf16_utf8_strnlen(const u16 *src, size_t count);
118 * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
120 * @src: utf-16 string
121 * Return: length in bytes after conversion to utf-8 without the
122 * trailing \0. -1 if the utf-16 string is not valid.
124 #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
127 * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
129 * @dst: destination buffer
130 * @src: source buffer
131 * @count: maximum number of code points to copy
132 * Return: -1 if the input parameters are invalid
134 int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
137 * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
139 * @dst: destination buffer
140 * @src: source buffer
141 * Return: -1 if the input parameters are invalid
143 #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
146 * utf_to_lower() - convert a Unicode letter to lower case
148 * @code: letter to convert
149 * Return: lower case letter or unchanged letter
151 s32 utf_to_lower(const s32 code);
154 * utf_to_upper() - convert a Unicode letter to upper case
156 * @code: letter to convert
157 * Return: upper case letter or unchanged letter
159 s32 utf_to_upper(const s32 code);
162 * u16_strlen - count non-zero words
164 * This function matches wsclen() if the -fshort-wchar compiler flag is set.
165 * In the EFI context we explicitly need a function handling u16 strings.
167 * @in: null terminated u16 string
168 * ReturnValue: number of non-zero words.
169 * This is not the number of utf-16 letters!
171 size_t u16_strlen(const u16 *in);
174 * u16_strlen - count non-zero words
176 * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
177 * In the EFI context we explicitly need a function handling u16 strings.
179 * @in: null terminated u16 string
180 * @count: maximum number of words to count
181 * ReturnValue: number of non-zero words.
182 * This is not the number of utf-16 letters!
184 size_t u16_strnlen(const u16 *in, size_t count);
187 * utf16_to_utf8() - Convert an utf16 string to utf8
189 * Converts 'size' characters of the utf16 string 'src' to utf8
190 * written to the 'dest' buffer.
192 * NOTE that a single utf16 character can generate up to 3 utf8
193 * characters. See MAX_UTF8_PER_UTF16.
195 * @dest the destination buffer to write the utf8 characters
196 * @src the source utf16 string
197 * @size the number of utf16 characters to convert
198 * @return the pointer to the first unwritten byte in 'dest'
200 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
202 #endif /* __CHARSET_H_ */