| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_STRING_UTF8_H |
| 4 | #define NETDATA_STRING_UTF8_H 1 |
| 5 | |
| 6 | #include "../libnetdata.h" |
| 7 | |
| 8 | #define IS_UTF8_BYTE(x) ((uint8_t)(x) & (uint8_t)0x80) |
| 9 | #define IS_UTF8_STARTBYTE(x) (IS_UTF8_BYTE(x) && ((uint8_t)(x) & (uint8_t)0x40)) |
| 10 | |
| 11 | #ifndef _countof |
| 12 | #define _countof(x) (sizeof(x) / sizeof(*(x))) |
| 13 | #endif |
| 14 | |
| 15 | #if defined(OS_WINDOWS) |
| 16 | |
| 17 | // return an always null terminated wide string, truncate to given size if destination is not big enough, |
| 18 | // src_len can be -1 use all of it. |
| 19 | // returns zero on errors, > 0 otherwise (including the null, even if src is not null terminated). |
| 20 | size_t any_to_utf16(uint32_t CodePage, wchar_t *dst, size_t dst_size, const char *src, int src_len, bool *truncated); |
| 21 | |
| 22 | // always null terminated, truncated if it does not fit, src_len can be -1 to use all of it. |
| 23 | // returns zero on errors, > 0 otherwise (including the null, even if src is not null terminated). |
| 24 | #define utf8_to_utf16(utf16, utf16_count, src, src_len) any_to_utf16(CP_UTF8, utf16, utf16_count, src, src_len, NULL) |
| 25 | |
| 26 | // always null terminated, truncated if it does not fit, src_len can be -1 to use all of it. |
| 27 | // returns zero on errors, > 0 otherwise (including the null, even if src is not null terminated). |
| 28 | size_t utf16_to_utf8(char *dst, size_t dst_size, const wchar_t *src, int src_len, bool *truncated); |
| 29 | |
| 30 | // -------------------------------------------------------------------------------------------------------------------- |
| 31 | // TXT_UTF8 |
| 32 | |
| 33 | typedef enum __attribute__((packed)) { |
| 34 | TXT_SOURCE_UNKNOWN = 0, |
| 35 | TXT_SOURCE_PROVIDER, |
| 36 | TXT_SOURCE_FIELD_CACHE, |
| 37 | TXT_SOURCE_EVENT_LOG, |
| 38 | TXT_SOURCE_HARDCODED, |
| 39 | |
| 40 | // terminator |
| 41 | TXT_SOURCE_MAX, |
| 42 | } TXT_SOURCE; |
| 43 | |
| 44 | typedef struct { |
| 45 | char *data; |
| 46 | uint32_t size; // the allocated size of data buffer |
| 47 | uint32_t used; // the used size of the data buffer (including null terminators, if any) |
| 48 | TXT_SOURCE src; |
| 49 | } TXT_UTF8; |
| 50 | |
| 51 | void txt_utf8_append(TXT_UTF8 *dst, const char *txt, size_t txt_len); |
| 52 | void txt_utf8_set(TXT_UTF8 *dst, const char *txt, size_t txt_len); |
| 53 | void txt_utf8_empty(TXT_UTF8 *dst); |
| 54 | void txt_utf8_resize(TXT_UTF8 *dst, size_t required_size, bool keep); |
| 55 | void txt_utf8_cleanup(TXT_UTF8 *dst); |
| 56 | |
| 57 | // -------------------------------------------------------------------------------------------------------------------- |
| 58 | // TXT_UTF16 |
| 59 | |
| 60 | typedef struct { |
| 61 | wchar_t *data; |
| 62 | uint32_t size; // the allocated size of data buffer |
| 63 | uint32_t used; // the used size of the data buffer (including null terminators, if any) |
| 64 | } TXT_UTF16; |
| 65 | |
| 66 | void txt_utf16_cleanup(TXT_UTF16 *dst); |
| 67 | void txt_utf16_resize(TXT_UTF16 *dst, size_t required_size, bool keep); |
| 68 | void txt_utf16_set(TXT_UTF16 *dst, const wchar_t *txt, size_t txt_len); |
| 69 | void txt_utf16_append(TXT_UTF16 *dst, const wchar_t *txt, size_t txt_len); |
| 70 | |
| 71 | // -------------------------------------------------------------------------------------------------------------------- |
| 72 | |
| 73 | size_t txt_compute_new_size(size_t old_size, size_t required_size); |
| 74 | |
| 75 | bool txt_utf16_to_utf8(TXT_UTF8 *utf8, TXT_UTF16 *utf16); |
| 76 | bool wchar_to_txt_utf8(TXT_UTF8 *dst, const wchar_t *src, int src_len); |
| 77 | char *utf16_to_utf8_strdupz(const wchar_t *src, size_t *dst_len); |
| 78 | |
| 79 | // -------------------------------------------------------------------------------------------------------------------- |
| 80 | |
| 81 | #endif // OS_WINDOWS |
| 82 | |
| 83 | #endif /* NETDATA_STRING_UTF8_H */ |