blob: 2c6deb8034fd63a0c6fae15a8f4917201a048835 [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/**
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020016 * u16_strlen - count non-zero words
Rob Clark78178bb2017-09-09 06:47:40 -040017 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020018 * 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.
Rob Clark78178bb2017-09-09 06:47:40 -040020 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020021 * @in: null terminated u16 string
22 * ReturnValue: number of non-zero words.
23 * This is not the number of utf-16 letters!
Rob Clark78178bb2017-09-09 06:47:40 -040024 */
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020025size_t u16_strlen(const u16 *in);
Rob Clark78178bb2017-09-09 06:47:40 -040026
27/**
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020028 * u16_strlen - count non-zero words
Rob Clark78178bb2017-09-09 06:47:40 -040029 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020030 * 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.
Rob Clark78178bb2017-09-09 06:47:40 -040032 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020033 * @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!
Rob Clark78178bb2017-09-09 06:47:40 -040037 */
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +020038size_t u16_strnlen(const u16 *in, size_t count);
Rob Clark78178bb2017-09-09 06:47:40 -040039
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_ */