1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * charset conversion utils
5 * Copyright (c) 2017 Rob Clark
11 #include <linux/types.h>
13 #define MAX_UTF8_PER_UTF16 3
16 * utf16_strlen() - Get the length of an utf16 string
18 * Returns the number of 16 bit characters in an utf16 string, not
19 * including the terminating NULL character.
21 * @in the string to measure
22 * @return the string length
24 size_t utf16_strlen(const uint16_t *in);
27 * utf16_strnlen() - Get the length of a fixed-size utf16 string.
29 * Returns the number of 16 bit characters in an utf16 string,
30 * not including the terminating NULL character, but at most
31 * 'count' number of characters. In doing this, utf16_strnlen()
32 * looks at only the first 'count' characters.
34 * @in the string to measure
35 * @count the maximum number of characters to count
36 * @return the string length, up to a maximum of 'count'
38 size_t utf16_strnlen(const uint16_t *in, size_t count);
41 * utf16_strcpy() - UTF16 equivalent of strcpy()
43 uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);
46 * utf16_strdup() - UTF16 equivalent of strdup()
48 uint16_t *utf16_strdup(const uint16_t *s);
51 * utf16_to_utf8() - Convert an utf16 string to utf8
53 * Converts 'size' characters of the utf16 string 'src' to utf8
54 * written to the 'dest' buffer.
56 * NOTE that a single utf16 character can generate up to 3 utf8
57 * characters. See MAX_UTF8_PER_UTF16.
59 * @dest the destination buffer to write the utf8 characters
60 * @src the source utf16 string
61 * @size the number of utf16 characters to convert
62 * @return the pointer to the first unwritten byte in 'dest'
64 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
67 * utf8_to_utf16() - Convert an utf8 string to utf16
69 * Converts up to 'size' characters of the utf16 string 'src' to utf8
70 * written to the 'dest' buffer. Stops at 0x00.
72 * @dest the destination buffer to write the utf8 characters
73 * @src the source utf16 string
74 * @size maximum number of utf16 characters to convert
75 * @return the pointer to the first unwritten byte in 'dest'
77 uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size);
79 #endif /* __CHARSET_H_ */