blob: 020f8a90df070c71604f96548297e07e40f8a153 [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 Schuchardtd8c28232018-08-31 21:31:27 +020011#include <linux/kernel.h>
Heinrich Schuchardtf58c5ec2017-10-18 18:13:06 +020012#include <linux/types.h>
13
Heinrich Schuchardt984f2512017-10-09 21:09:05 +020014#define MAX_UTF8_PER_UTF16 3
Rob Clark78178bb2017-09-09 06:47:40 -040015
16/**
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +020017 * console_read_unicode() - read Unicode code point from console
18 *
19 * @code: pointer to store Unicode code point
20 * Return: 0 = success
21 */
22int console_read_unicode(s32 *code);
23
24/**
Heinrich Schuchardtd8c28232018-08-31 21:31:27 +020025 * utf8_get() - get next UTF-8 code point from buffer
26 *
27 * @src: pointer to current byte, updated to point to next byte
28 * Return: code point, or 0 for end of string, or -1 if no legal
29 * code point is found. In case of an error src points to
30 * the incorrect byte.
31 */
32s32 utf8_get(const char **src);
33
34/**
35 * utf8_put() - write UTF-8 code point to buffer
36 *
37 * @code: code point
38 * @dst: pointer to destination buffer, updated to next position
39 * Return: -1 if the input parameters are invalid
40 */
41int utf8_put(s32 code, char **dst);
42
43/**
44 * utf8_utf16_strnlen() - length of a truncated utf-8 string after conversion
45 * to utf-16
46 *
47 * @src: utf-8 string
48 * @count: maximum number of code points to convert
Heinrich Schuchardt8a4c4432019-05-08 19:34:48 +020049 * Return: length in u16 after conversion to utf-16 without the
Heinrich Schuchardtd8c28232018-08-31 21:31:27 +020050 * trailing \0. If an invalid UTF-8 sequence is hit one
Heinrich Schuchardt8a4c4432019-05-08 19:34:48 +020051 * u16 will be reserved for a replacement character.
Heinrich Schuchardtd8c28232018-08-31 21:31:27 +020052 */
53size_t utf8_utf16_strnlen(const char *src, size_t count);
54
55/**
56 * utf8_utf16_strlen() - length of a utf-8 string after conversion to utf-16
57 *
58 * @src: utf-8 string
Heinrich Schuchardt8a4c4432019-05-08 19:34:48 +020059 * Return: length in u16 after conversion to utf-16 without the
60 * trailing \0. If an invalid UTF-8 sequence is hit one
61 * u16 will be reserved for a replacement character.
Heinrich Schuchardtd8c28232018-08-31 21:31:27 +020062 */
63#define utf8_utf16_strlen(a) utf8_utf16_strnlen((a), SIZE_MAX)
64
65/**
66 * utf8_utf16_strncpy() - copy utf-8 string to utf-16 string
67 *
68 * @dst: destination buffer
69 * @src: source buffer
70 * @count: maximum number of code points to copy
71 * Return: -1 if the input parameters are invalid
72 */
73int utf8_utf16_strncpy(u16 **dst, const char *src, size_t count);
74
75/**
76 * utf8_utf16_strcpy() - copy utf-8 string to utf-16 string
77 *
78 * @dst: destination buffer
79 * @src: source buffer
80 * Return: -1 if the input parameters are invalid
81 */
82#define utf8_utf16_strcpy(d, s) utf8_utf16_strncpy((d), (s), SIZE_MAX)
83
84/**
85 * utf16_get() - get next UTF-16 code point from buffer
86 *
87 * @src: pointer to current word, updated to point to next word
88 * Return: code point, or 0 for end of string, or -1 if no legal
89 * code point is found. In case of an error src points to
90 * the incorrect word.
91 */
92s32 utf16_get(const u16 **src);
93
94/**
95 * utf16_put() - write UTF-16 code point to buffer
96 *
97 * @code: code point
98 * @dst: pointer to destination buffer, updated to next position
99 * Return: -1 if the input parameters are invalid
100 */
101int utf16_put(s32 code, u16 **dst);
102
103/**
104 * utf16_strnlen() - length of a truncated utf-16 string
105 *
106 * @src: utf-16 string
107 * @count: maximum number of code points to convert
108 * Return: length in code points. If an invalid UTF-16 sequence is
109 * hit one position will be reserved for a replacement
110 * character.
111 */
112size_t utf16_strnlen(const u16 *src, size_t count);
113
114/**
115 * utf16_utf8_strnlen() - length of a truncated utf-16 string after conversion
116 * to utf-8
117 *
118 * @src: utf-16 string
119 * @count: maximum number of code points to convert
120 * Return: length in bytes after conversion to utf-8 without the
121 * trailing \0. If an invalid UTF-16 sequence is hit one
122 * byte will be reserved for a replacement character.
123 */
124size_t utf16_utf8_strnlen(const u16 *src, size_t count);
125
126/**
127 * utf16_utf8_strlen() - length of a utf-16 string after conversion to utf-8
128 *
129 * @src: utf-16 string
130 * Return: length in bytes after conversion to utf-8 without the
Heinrich Schuchardt8a4c4432019-05-08 19:34:48 +0200131 * trailing \0. If an invalid UTF-16 sequence is hit one
132 * byte will be reserved for a replacement character.
Heinrich Schuchardtd8c28232018-08-31 21:31:27 +0200133 */
134#define utf16_utf8_strlen(a) utf16_utf8_strnlen((a), SIZE_MAX)
135
136/**
137 * utf16_utf8_strncpy() - copy utf-16 string to utf-8 string
138 *
139 * @dst: destination buffer
140 * @src: source buffer
141 * @count: maximum number of code points to copy
142 * Return: -1 if the input parameters are invalid
143 */
144int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
145
146/**
147 * utf16_utf8_strcpy() - copy utf-16 string to utf-8 string
148 *
149 * @dst: destination buffer
150 * @src: source buffer
151 * Return: -1 if the input parameters are invalid
152 */
153#define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
154
155/**
Heinrich Schuchardtb5130a82018-09-04 19:34:56 +0200156 * utf_to_lower() - convert a Unicode letter to lower case
157 *
158 * @code: letter to convert
159 * Return: lower case letter or unchanged letter
160 */
161s32 utf_to_lower(const s32 code);
162
163/**
164 * utf_to_upper() - convert a Unicode letter to upper case
165 *
166 * @code: letter to convert
167 * Return: upper case letter or unchanged letter
168 */
169s32 utf_to_upper(const s32 code);
170
171/**
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200172 * u16_strlen - count non-zero words
Rob Clark78178bb2017-09-09 06:47:40 -0400173 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200174 * This function matches wsclen() if the -fshort-wchar compiler flag is set.
175 * In the EFI context we explicitly need a function handling u16 strings.
Rob Clark78178bb2017-09-09 06:47:40 -0400176 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200177 * @in: null terminated u16 string
178 * ReturnValue: number of non-zero words.
179 * This is not the number of utf-16 letters!
Rob Clark78178bb2017-09-09 06:47:40 -0400180 */
Heinrich Schuchardt317068b2019-07-14 17:28:49 +0200181size_t u16_strlen(const void *in);
Rob Clark78178bb2017-09-09 06:47:40 -0400182
183/**
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200184 * u16_strlen - count non-zero words
Rob Clark78178bb2017-09-09 06:47:40 -0400185 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200186 * This function matches wscnlen_s() if the -fshort-wchar compiler flag is set.
187 * In the EFI context we explicitly need a function handling u16 strings.
Rob Clark78178bb2017-09-09 06:47:40 -0400188 *
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200189 * @in: null terminated u16 string
190 * @count: maximum number of words to count
191 * ReturnValue: number of non-zero words.
192 * This is not the number of utf-16 letters!
Rob Clark78178bb2017-09-09 06:47:40 -0400193 */
Heinrich Schuchardt1dde0d52018-08-31 21:31:26 +0200194size_t u16_strnlen(const u16 *in, size_t count);
Rob Clark78178bb2017-09-09 06:47:40 -0400195
196/**
Akashi, Takahiro2a3537a2018-12-14 19:10:38 +0900197 * u16_strcpy() - copy u16 string
198 *
199 * Copy u16 string pointed to by src, including terminating null word, to
200 * the buffer pointed to by dest.
201 *
202 * @dest: destination buffer
203 * @src: source buffer (null terminated)
204 * Return: 'dest' address
205 */
206u16 *u16_strcpy(u16 *dest, const u16 *src);
207
208/**
209 * u16_strdup() - duplicate u16 string
210 *
211 * Copy u16 string pointed to by src, including terminating null word, to a
212 * newly allocated buffer.
213 *
214 * @src: source buffer (null terminated)
215 * Return: allocated new buffer on success, NULL on failure
216 */
Heinrich Schuchardt317068b2019-07-14 17:28:49 +0200217u16 *u16_strdup(const void *src);
Akashi, Takahiro2a3537a2018-12-14 19:10:38 +0900218
219/**
Rob Clark78178bb2017-09-09 06:47:40 -0400220 * utf16_to_utf8() - Convert an utf16 string to utf8
221 *
222 * Converts 'size' characters of the utf16 string 'src' to utf8
223 * written to the 'dest' buffer.
224 *
Heinrich Schuchardt984f2512017-10-09 21:09:05 +0200225 * NOTE that a single utf16 character can generate up to 3 utf8
Rob Clark78178bb2017-09-09 06:47:40 -0400226 * characters. See MAX_UTF8_PER_UTF16.
227 *
228 * @dest the destination buffer to write the utf8 characters
229 * @src the source utf16 string
230 * @size the number of utf16 characters to convert
231 * @return the pointer to the first unwritten byte in 'dest'
232 */
233uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size);
234
Rob Clark78178bb2017-09-09 06:47:40 -0400235#endif /* __CHARSET_H_ */