xref: /openbmc/u-boot/include/charset.h (revision 1dde0d57a5d1281aaa949e9bf7b5c476345a56ee)
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   * u16_strlen - count non-zero words
17   *
18   * This function matches wsclen() if the -fshort-wchar compiler flag is set.
19   * In the EFI context we explicitly need a function handling u16 strings.
20   *
21   * @in:			null terminated u16 string
22   * ReturnValue:		number of non-zero words.
23   *			This is not the number of utf-16 letters!
24   */
25  size_t u16_strlen(const u16 *in);
26  
27  /**
28   * u16_strlen - count non-zero words
29   *
30   * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
31   * In the EFI context we explicitly need a function handling u16 strings.
32   *
33   * @in:			null terminated u16 string
34   * @count:		maximum number of words to count
35   * ReturnValue:		number of non-zero words.
36   *			This is not the number of utf-16 letters!
37   */
38  size_t u16_strnlen(const u16 *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