Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * charset conversion utils |
| 3 | * |
| 4 | * Copyright (c) 2017 Rob Clark |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #ifndef __CHARSET_H_ |
| 10 | #define __CHARSET_H_ |
| 11 | |
Heinrich Schuchardt | d0faa9e | 2017-10-18 18:13:06 +0200 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | |
Heinrich Schuchardt | 1636c9e | 2017-10-09 21:09:05 +0200 | [diff] [blame] | 14 | #define MAX_UTF8_PER_UTF16 3 |
Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * utf16_strlen() - Get the length of an utf16 string |
| 18 | * |
| 19 | * Returns the number of 16 bit characters in an utf16 string, not |
| 20 | * including the terminating NULL character. |
| 21 | * |
| 22 | * @in the string to measure |
| 23 | * @return the string length |
| 24 | */ |
| 25 | size_t utf16_strlen(const uint16_t *in); |
| 26 | |
| 27 | /** |
| 28 | * utf16_strnlen() - Get the length of a fixed-size utf16 string. |
| 29 | * |
| 30 | * Returns the number of 16 bit characters in an utf16 string, |
| 31 | * not including the terminating NULL character, but at most |
| 32 | * 'count' number of characters. In doing this, utf16_strnlen() |
| 33 | * looks at only the first 'count' characters. |
| 34 | * |
| 35 | * @in the string to measure |
| 36 | * @count the maximum number of characters to count |
| 37 | * @return the string length, up to a maximum of 'count' |
| 38 | */ |
| 39 | size_t utf16_strnlen(const uint16_t *in, size_t count); |
| 40 | |
| 41 | /** |
| 42 | * utf16_strcpy() - UTF16 equivalent of strcpy() |
| 43 | */ |
| 44 | uint16_t *utf16_strcpy(uint16_t *dest, const uint16_t *src); |
| 45 | |
| 46 | /** |
| 47 | * utf16_strdup() - UTF16 equivalent of strdup() |
| 48 | */ |
| 49 | uint16_t *utf16_strdup(const uint16_t *s); |
| 50 | |
| 51 | /** |
| 52 | * utf16_to_utf8() - Convert an utf16 string to utf8 |
| 53 | * |
| 54 | * Converts 'size' characters of the utf16 string 'src' to utf8 |
| 55 | * written to the 'dest' buffer. |
| 56 | * |
Heinrich Schuchardt | 1636c9e | 2017-10-09 21:09:05 +0200 | [diff] [blame] | 57 | * NOTE that a single utf16 character can generate up to 3 utf8 |
Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 58 | * characters. See MAX_UTF8_PER_UTF16. |
| 59 | * |
| 60 | * @dest the destination buffer to write the utf8 characters |
| 61 | * @src the source utf16 string |
| 62 | * @size the number of utf16 characters to convert |
| 63 | * @return the pointer to the first unwritten byte in 'dest' |
| 64 | */ |
| 65 | uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size); |
| 66 | |
Heinrich Schuchardt | d0faa9e | 2017-10-18 18:13:06 +0200 | [diff] [blame] | 67 | /** |
| 68 | * utf8_to_utf16() - Convert an utf8 string to utf16 |
| 69 | * |
| 70 | * Converts up to 'size' characters of the utf16 string 'src' to utf8 |
| 71 | * written to the 'dest' buffer. Stops at 0x00. |
| 72 | * |
| 73 | * @dest the destination buffer to write the utf8 characters |
| 74 | * @src the source utf16 string |
| 75 | * @size maximum number of utf16 characters to convert |
| 76 | * @return the pointer to the first unwritten byte in 'dest' |
| 77 | */ |
| 78 | uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size); |
| 79 | |
Rob Clark | 0d138cf | 2017-09-09 06:47:40 -0400 | [diff] [blame] | 80 | #endif /* __CHARSET_H_ */ |