Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 2 | /* |
| 3 | * charset conversion utils |
| 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef __CHARSET_H_ |
| 9 | #define __CHARSET_H_ |
| 10 | |
Heinrich Schuchardt | f58c5ec | 2017-10-18 18:13:06 +0200 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | |
Heinrich Schuchardt | 984f251 | 2017-10-09 21:09:05 +0200 | [diff] [blame] | 13 | #define MAX_UTF8_PER_UTF16 3 |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 14 | |
| 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 | */ |
| 24 | size_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 | */ |
| 38 | size_t utf16_strnlen(const uint16_t *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 | * |
Heinrich Schuchardt | 984f251 | 2017-10-09 21:09:05 +0200 | [diff] [blame] | 56 | * NOTE that a single utf16 character can generate up to 3 utf8 |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 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 | |
Heinrich Schuchardt | f58c5ec | 2017-10-18 18:13:06 +0200 | [diff] [blame] | 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 | |
Rob Clark | 78178bb | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 79 | #endif /* __CHARSET_H_ */ |