summaryrefslogtreecommitdiff
path: root/include/charset.h
blob: 2c6deb8034fd63a0c6fae15a8f4917201a048835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 *  charset conversion utils
 *
 *  Copyright (c) 2017 Rob Clark
 */

#ifndef __CHARSET_H_
#define __CHARSET_H_

#include <linux/types.h>

#define MAX_UTF8_PER_UTF16 3

/**
 * u16_strlen - count non-zero words
 *
 * This function matches wsclen() if the -fshort-wchar compiler flag is set.
 * In the EFI context we explicitly need a function handling u16 strings.
 *
 * @in:			null terminated u16 string
 * ReturnValue:		number of non-zero words.
 *			This is not the number of utf-16 letters!
 */
size_t u16_strlen(const u16 *in);

/**
 * u16_strlen - count non-zero words
 *
 * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
 * In the EFI context we explicitly need a function handling u16 strings.
 *
 * @in:			null terminated u16 string
 * @count:		maximum number of words to count
 * ReturnValue:		number of non-zero words.
 *			This is not the number of utf-16 letters!
 */
size_t u16_strnlen(const u16 *in, size_t count);

/**
 * utf16_strcpy() - UTF16 equivalent of strcpy()
 */
uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);

/**
 * utf16_strdup() - UTF16 equivalent of strdup()
 */
uint16_t *utf16_strdup(const uint16_t *s);

/**
 * utf16_to_utf8() - Convert an utf16 string to utf8
 *
 * Converts 'size' characters of the utf16 string 'src' to utf8
 * written to the 'dest' buffer.
 *
 * NOTE that a single utf16 character can generate up to 3 utf8
 * characters.  See MAX_UTF8_PER_UTF16.
 *
 * @dest   the destination buffer to write the utf8 characters
 * @src    the source utf16 string
 * @size   the number of utf16 characters to convert
 * @return the pointer to the first unwritten byte in 'dest'
 */
uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);

/**
 * utf8_to_utf16() - Convert an utf8 string to utf16
 *
 * Converts up to 'size' characters of the utf16 string 'src' to utf8
 * written to the 'dest' buffer. Stops at 0x00.
 *
 * @dest   the destination buffer to write the utf8 characters
 * @src    the source utf16 string
 * @size   maximum number of utf16 characters to convert
 * @return the pointer to the first unwritten byte in 'dest'
 */
uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size);

#endif /* __CHARSET_H_ */