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 * codepage_437 - Unicode to codepage 437 translation table
19 extern const u16 codepage_437[128];
22 * console_read_unicode() - read Unicode code point from console
24 * @code: pointer to store Unicode code point
27 int console_read_unicode(s32 *code);
30 * utf8_get() - get next UTF-8 code point from buffer
32 * @src: pointer to current byte, updated to point to next byte
33 * Return: code point, or 0 for end of string, or -1 if no legal
34 * code point is found. In case of an error src points to
37 s32 utf8_get(const char **src);
40 * utf8_put() - write UTF-8 code point to buffer
43 * @dst: pointer to destination buffer, updated to next position
44 * Return: -1 if the input parameters are invalid
46 int utf8_put(s32 code, char **dst);
49 * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
53 * @count: maximum number of code points to convert
54 * Return: length in u16 after conversion to utf-16 without the
55 * trailing \0. If an invalid UTF-8 sequence is hit one
56 * u16 will be reserved for a replacement character.
58 size_t utf8_utf16_strnlen(const char *src, size_t count);
61 * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
64 * Return: length in u16 after conversion to utf-16 without the
65 * trailing \0. If an invalid UTF-8 sequence is hit one
66 * u16 will be reserved for a replacement character.
68 #define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
71 * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
73 * @dst: destination buffer
75 * @count: maximum number of code points to copy
76 * Return: -1 if the input parameters are invalid
78 int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
81 * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
83 * @d: destination buffer
85 * Return: -1 if the input parameters are invalid
87 #define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
90 * utf16_get() - get next UTF-16 code point from buffer
92 * @src: pointer to current word, updated to point to next word
93 * Return: code point, or 0 for end of string, or -1 if no legal
94 * code point is found. In case of an error src points to
97 s32 utf16_get(const u16 **src);
100 * utf16_put() - write UTF-16 code point to buffer
103 * @dst: pointer to destination buffer, updated to next position
104 * Return: -1 if the input parameters are invalid
106 int utf16_put(s32 code, u16 **dst);
109 * utf16_strnlen() - length of a truncated utf-16 string
111 * @src: utf-16 string
112 * @count: maximum number of code points to convert
113 * Return: length in code points. If an invalid UTF-16 sequence is
114 * hit one position will be reserved for a replacement
117 size_t utf16_strnlen(const u16 *src, size_t count);
120 * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
123 * @src: utf-16 string
124 * @count: maximum number of code points to convert
125 * Return: length in bytes after conversion to utf-8 without the
126 * trailing \0. If an invalid UTF-16 sequence is hit one
127 * byte will be reserved for a replacement character.
129 size_t utf16_utf8_strnlen(const u16 *src, size_t count);
132 * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
135 * Return: length in bytes after conversion to utf-8 without the
136 * trailing \0. If an invalid UTF-16 sequence is hit one
137 * byte will be reserved for a replacement character.
139 #define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
142 * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
144 * @dst: destination buffer
145 * @src: source buffer
146 * @count: maximum number of code points to copy
147 * Return: -1 if the input parameters are invalid
149 int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
152 * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
154 * @d: destination buffer
156 * Return: -1 if the input parameters are invalid
158 #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
161 * utf_to_lower() - convert a Unicode letter to lower case
163 * @code: letter to convert
164 * Return: lower case letter or unchanged letter
166 s32 utf_to_lower(const s32 code);
169 * utf_to_upper() - convert a Unicode letter to upper case
171 * @code: letter to convert
172 * Return: upper case letter or unchanged letter
174 s32 utf_to_upper(const s32 code);
177 * u16_strncmp() - compare two u16 string
179 * @s1: first string to compare
180 * @s2: second string to compare
181 * @n: maximum number of u16 to compare
182 * Return: 0 if the first n u16 are the same in s1 and s2
183 * < 0 if the first different u16 in s1 is less than the
184 * corresponding u16 in s2
185 * > 0 if the first different u16 in s1 is greater than the
186 * corresponding u16 in s2
188 int u16_strncmp(const u16 *s1, const u16 *s2, size_t n);
191 * u16_strcmp() - compare two u16 string
193 * @s1: first string to compare
194 * @s2: second string to compare
195 * Return: 0 if the first n u16 are the same in s1 and s2
196 * < 0 if the first different u16 in s1 is less than the
197 * corresponding u16 in s2
198 * > 0 if the first different u16 in s1 is greater than the
199 * corresponding u16 in s2
201 #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
204 * u16_strlen - count non-zero words
206 * This function matches wsclen() if the -fshort-wchar compiler flag is set.
207 * In the EFI context we explicitly need a function handling u16 strings.
209 * @in: null terminated u16 string
210 * Return: number of non-zero words.
211 * This is not the number of utf-16 letters!
213 size_t u16_strlen(const void *in);
216 * u16_strsize() - count size of u16 string in bytes including the null
219 * Counts the number of bytes occupied by a u16 string
221 * @in: null terminated u16 string
222 * Return: bytes in a u16 string
224 size_t u16_strsize(const void *in);
227 * u16_strnlen() - count non-zero words
229 * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
230 * In the EFI context we explicitly need a function handling u16 strings.
232 * @in: null terminated u16 string
233 * @count: maximum number of words to count
234 * Return: number of non-zero words.
235 * This is not the number of utf-16 letters!
237 size_t u16_strnlen(const u16 *in, size_t count);
240 * u16_strcpy() - copy u16 string
242 * Copy u16 string pointed to by src, including terminating null word, to
243 * the buffer pointed to by dest.
245 * @dest: destination buffer
246 * @src: source buffer (null terminated)
247 * Return: 'dest' address
249 u16 *u16_strcpy(u16 *dest, const u16 *src);
252 * u16_strdup() - duplicate u16 string
254 * Copy u16 string pointed to by src, including terminating null word, to a
255 * newly allocated buffer.
257 * @src: source buffer (null terminated)
258 * Return: allocated new buffer on success, NULL on failure
260 u16 *u16_strdup(const void *src);
263 * utf16_to_utf8() - Convert an utf16 string to utf8
265 * Converts 'size' characters of the utf16 string 'src' to utf8
266 * written to the 'dest' buffer.
268 * NOTE that a single utf16 character can generate up to 3 utf8
269 * characters. See MAX_UTF8_PER_UTF16.
271 * @dest: the destination buffer to write the utf8 characters
272 * @src: the source utf16 string
273 * @size: the number of utf16 characters to convert
274 * Return: the pointer to the first unwritten byte in 'dest'
276 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
279 * utf_to_cp() - translate Unicode code point to 8bit codepage
281 * Codepoints that do not exist in the codepage are rendered as question mark.
283 * @c: pointer to Unicode code point to be translated
284 * @codepage: Unicode to codepage translation table
285 * Return: 0 on success, -ENOENT if codepoint cannot be translated
287 int utf_to_cp(s32 *c, const u16 *codepage);
290 * utf8_to_cp437_stream() - convert UTF-8 stream to codepage 437
292 * @c: next UTF-8 character to convert
293 * @buffer: buffer, at least 5 characters
294 * Return: next codepage 437 character or 0
296 int utf8_to_cp437_stream(u8 c, char *buffer);
299 * utf8_to_utf32_stream() - convert UTF-8 stream to UTF-32
301 * @c: next UTF-8 character to convert
302 * @buffer: buffer, at least 5 characters
303 * Return: next codepage 437 character or 0
305 int utf8_to_utf32_stream(u8 c, char *buffer);
307 #endif /* __CHARSET_H_ */