xref: /OK3568_Linux_fs/u-boot/include/charset.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  charset conversion utils
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Copyright (c) 2017 Rob Clark
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  SPDX-License-Identifier:     GPL-2.0+
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef __CHARSET_H_
10*4882a593Smuzhiyun #define __CHARSET_H_
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define MAX_UTF8_PER_UTF16 4
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * utf16_strlen() - Get the length of an utf16 string
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Returns the number of 16 bit characters in an utf16 string, not
21*4882a593Smuzhiyun  * including the terminating NULL character.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * @in     the string to measure
24*4882a593Smuzhiyun  * @return the string length
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun size_t utf16_strlen(const uint16_t *in);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun  * utf16_strnlen() - Get the length of a fixed-size utf16 string.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Returns the number of 16 bit characters in an utf16 string,
32*4882a593Smuzhiyun  * not including the terminating NULL character, but at most
33*4882a593Smuzhiyun  * 'count' number of characters.  In doing this, utf16_strnlen()
34*4882a593Smuzhiyun  * looks at only the first 'count' characters.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * @in     the string to measure
37*4882a593Smuzhiyun  * @count  the maximum number of characters to count
38*4882a593Smuzhiyun  * @return the string length, up to a maximum of 'count'
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun size_t utf16_strnlen(const uint16_t *in, size_t count);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun  * utf16_strcpy() - UTF16 equivalent of strcpy()
44*4882a593Smuzhiyun  */
45*4882a593Smuzhiyun uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  * utf16_strdup() - UTF16 equivalent of strdup()
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun uint16_t *utf16_strdup(const uint16_t *s);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * utf16_to_utf8() - Convert an utf16 string to utf8
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Converts 'size' characters of the utf16 string 'src' to utf8
56*4882a593Smuzhiyun  * written to the 'dest' buffer.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * NOTE that a single utf16 character can generate up to 4 utf8
59*4882a593Smuzhiyun  * characters.  See MAX_UTF8_PER_UTF16.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * @dest   the destination buffer to write the utf8 characters
62*4882a593Smuzhiyun  * @src    the source utf16 string
63*4882a593Smuzhiyun  * @size   the number of utf16 characters to convert
64*4882a593Smuzhiyun  * @return the pointer to the first unwritten byte in 'dest'
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun #endif /* __CHARSET_H_ */
69