blob: 11832cbd122eb4b8c3be417d467babad1174c647 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Rob Clark78178bb2017-09-09 06:47:40 -04002/*
3 * charset conversion utils
4 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark78178bb2017-09-09 06:47:40 -04006 */
7
8#ifndef __CHARSET_H_
9#define __CHARSET_H_
10
Heinrich Schuchardtf58c5ec2017-10-18 18:13:06 +020011#include <linux/types.h>
12
Heinrich Schuchardt984f2512017-10-09 21:09:05 +020013#define MAX_UTF8_PER_UTF16 3
Rob Clark78178bb2017-09-09 06:47:40 -040014
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 */
24size_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 */
38size_t utf16_strnlen(const uint16_t *in, size_t count);
39
40/**
41 * utf16_strcpy() - UTF16 equivalent of strcpy()
42 */
43uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src);
44
45/**
46 * utf16_strdup() - UTF16 equivalent of strdup()
47 */
48uint16_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 *
Heinrich Schuchardt984f2512017-10-09 21:09:05 +020056 * NOTE that a single utf16 character can generate up to 3 utf8
Rob Clark78178bb2017-09-09 06:47:40 -040057 * 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 */
64uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
65
Heinrich Schuchardtf58c5ec2017-10-18 18:13:06 +020066/**
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 */
77uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size);
78
Rob Clark78178bb2017-09-09 06:47:40 -040079#endif /* __CHARSET_H_ */