• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  charset conversion utils
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7 
8 #ifndef __CHARSET_H_
9 #define __CHARSET_H_
10 
11 #include <linux/types.h>
12 
13 #define MAX_UTF8_PER_UTF16 3
14 
15 /**
16  * utf16_strlen() - Get the length of an utf16 string
17  *
18  * Returns the number of 16 bit characters in an utf16 string, not
19  * including the terminating NULL character.
20  *
21  * @in     the string to measure
22  * @return the string length
23  */
24 size_t utf16_strlen(const uint16_t *in);
25 
26 /**
27  * utf16_strnlen() - Get the length of a fixed-size utf16 string.
28  *
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.
33  *
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'
37  */
38 size_t utf16_strnlen(const uint16_t *in, size_t count);
39 
40 /**
41  * utf16_strcpy() - UTF16 equivalent of strcpy()
42  */
43 uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);
44 
45 /**
46  * utf16_strdup() - UTF16 equivalent of strdup()
47  */
48 uint16_t *utf16_strdup(const uint16_t *s);
49 
50 /**
51  * utf16_to_utf8() - Convert an utf16 string to utf8
52  *
53  * Converts 'size' characters of the utf16 string 'src' to utf8
54  * written to the 'dest' buffer.
55  *
56  * NOTE that a single utf16 character can generate up to 3 utf8
57  * characters.  See MAX_UTF8_PER_UTF16.
58  *
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'
63  */
64 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
65 
66 /**
67  * utf8_to_utf16() - Convert an utf8 string to utf16
68  *
69  * Converts up to 'size' characters of the utf16 string 'src' to utf8
70  * written to the 'dest' buffer. Stops at 0x00.
71  *
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'
76  */
77 uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size);
78 
79 #endif /* __CHARSET_H_ */
80