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 * console_read_unicode() - read Unicode code point from console
19 * @code: pointer to store Unicode code point
22 int console_read_unicode(s32 *code);
25 * utf8_get() - get next UTF-8 code point from buffer
27 * @src: pointer to current byte, updated to point to next byte
28 * Return: code point, or 0 for end of string, or -1 if no legal
29 * code point is found. In case of an error src points to
32 s32 utf8_get(const char **src);
35 * utf8_put() - write UTF-8 code point to buffer
38 * @dst: pointer to destination buffer, updated to next position
39 * Return: -1 if the input parameters are invalid
41 int utf8_put(s32 code, char **dst);
44 * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
48 * @count: maximum number of code points to convert
49 * Return: length in u16 after conversion to utf-16 without the
50 * trailing \0. If an invalid UTF-8 sequence is hit one
51 * u16 will be reserved for a replacement character.
53 size_t utf8_utf16_strnlen(const char *src, size_t count);
56 * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
59 * Return: length in u16 after conversion to utf-16 without the
60 * trailing \0. If an invalid UTF-8 sequence is hit one
61 * u16 will be reserved for a replacement character.
63 #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
66 * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
68 * @dst: destination buffer
70 * @count: maximum number of code points to copy
71 * Return: -1 if the input parameters are invalid
73 int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
76 * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
78 * @dst: destination buffer
80 * Return: -1 if the input parameters are invalid
82 #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
85 * utf16_get() - get next UTF-16 code point from buffer
87 * @src: pointer to current word, updated to point to next word
88 * Return: code point, or 0 for end of string, or -1 if no legal
89 * code point is found. In case of an error src points to
92 s32 utf16_get(const u16 **src);
95 * utf16_put() - write UTF-16 code point to buffer
98 * @dst: pointer to destination buffer, updated to next position
99 * Return: -1 if the input parameters are invalid
101 int utf16_put(s32 code, u16 **dst);
104 * utf16_strnlen() - length of a truncated utf-16 string
106 * @src: utf-16 string
107 * @count: maximum number of code points to convert
108 * Return: length in code points. If an invalid UTF-16 sequence is
109 * hit one position will be reserved for a replacement
112 size_t utf16_strnlen(const u16 *src, size_t count);
115 * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
118 * @src: utf-16 string
119 * @count: maximum number of code points to convert
120 * Return: length in bytes after conversion to utf-8 without the
121 * trailing \0. If an invalid UTF-16 sequence is hit one
122 * byte will be reserved for a replacement character.
124 size_t utf16_utf8_strnlen(const u16 *src, size_t count);
127 * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
129 * @src: utf-16 string
130 * Return: length in bytes after conversion to utf-8 without the
131 * trailing \0. If an invalid UTF-16 sequence is hit one
132 * byte will be reserved for a replacement character.
134 #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
137 * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
139 * @dst: destination buffer
140 * @src: source buffer
141 * @count: maximum number of code points to copy
142 * Return: -1 if the input parameters are invalid
144 int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
147 * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
149 * @dst: destination buffer
150 * @src: source buffer
151 * Return: -1 if the input parameters are invalid
153 #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
156 * utf_to_lower() - convert a Unicode letter to lower case
158 * @code: letter to convert
159 * Return: lower case letter or unchanged letter
161 s32 utf_to_lower(const s32 code);
164 * utf_to_upper() - convert a Unicode letter to upper case
166 * @code: letter to convert
167 * Return: upper case letter or unchanged letter
169 s32 utf_to_upper(const s32 code);
172 * u16_strncmp() - compare two u16 string
174 * @s1: first string to compare
175 * @s2: second string to compare
176 * @n: maximum number of u16 to compare
177 * Return: 0 if the first n u16 are the same in s1 and s2
178 * < 0 if the first different u16 in s1 is less than the
179 * corresponding u16 in s2
180 * > 0 if the first different u16 in s1 is greater than the
181 * corresponding u16 in s2
183 int u16_strncmp(const u16 *s1, const u16 *s2, size_t n);
184 #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
187 * u16_strlen - count non-zero words
189 * This function matches wsclen() if the -fshort-wchar compiler flag is set.
190 * In the EFI context we explicitly need a function handling u16 strings.
192 * @in: null terminated u16 string
193 * ReturnValue: number of non-zero words.
194 * This is not the number of utf-16 letters!
196 size_t u16_strlen(const void *in);
199 * u16_strsize() - count size of u16 string in bytes including the null
202 * Counts the number of bytes occupied by a u16 string
204 * @in: null terminated u16 string
205 * Return: bytes in a u16 string
208 size_t u16_strsize(const void *in);
211 * u16_strlen - count non-zero words
213 * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
214 * In the EFI context we explicitly need a function handling u16 strings.
216 * @in: null terminated u16 string
217 * @count: maximum number of words to count
218 * ReturnValue: number of non-zero words.
219 * This is not the number of utf-16 letters!
221 size_t u16_strnlen(const u16 *in, size_t count);
224 * u16_strcpy() - copy u16 string
226 * Copy u16 string pointed to by src, including terminating null word, to
227 * the buffer pointed to by dest.
229 * @dest: destination buffer
230 * @src: source buffer (null terminated)
231 * Return: 'dest' address
233 u16 *u16_strcpy(u16 *dest, const u16 *src);
236 * u16_strdup() - duplicate u16 string
238 * Copy u16 string pointed to by src, including terminating null word, to a
239 * newly allocated buffer.
241 * @src: source buffer (null terminated)
242 * Return: allocated new buffer on success, NULL on failure
244 u16 *u16_strdup(const void *src);
247 * utf16_to_utf8() - Convert an utf16 string to utf8
249 * Converts 'size' characters of the utf16 string 'src' to utf8
250 * written to the 'dest' buffer.
252 * NOTE that a single utf16 character can generate up to 3 utf8
253 * characters. See MAX_UTF8_PER_UTF16.
255 * @dest the destination buffer to write the utf8 characters
256 * @src the source utf16 string
257 * @size the number of utf16 characters to convert
258 * @return the pointer to the first unwritten byte in 'dest'
260 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
262 #endif /* __CHARSET_H_ */