utf8to16_size() takes the size of the to be converted string. This is
needed to convert MULTI_SZ strings, which contain inline NUL characters,
but can be useful in other cases as well.
Change-Id: I6b4aa3d63c0b684bf95841271c04bc5d9c37793b
Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20241221224136.20984-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30158.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
| ... | ... |
@@ -247,17 +247,16 @@ MsgToEventLog(DWORD flags, LPCTSTR format, ...) |
| 247 | 247 |
return error; |
| 248 | 248 |
} |
| 249 | 249 |
|
| 250 |
-/* Convert a utf8 string to utf16. Caller should free the result */ |
|
| 251 | 250 |
wchar_t * |
| 252 |
-utf8to16(const char *utf8) |
|
| 251 |
+utf8to16_size(const char *utf8, int size) |
|
| 253 | 252 |
{
|
| 254 |
- int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); |
|
| 253 |
+ int n = MultiByteToWideChar(CP_UTF8, 0, utf8, size, NULL, 0); |
|
| 255 | 254 |
wchar_t *utf16 = malloc(n * sizeof(wchar_t)); |
| 256 | 255 |
if (!utf16) |
| 257 | 256 |
{
|
| 258 | 257 |
return NULL; |
| 259 | 258 |
} |
| 260 |
- MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, n); |
|
| 259 |
+ MultiByteToWideChar(CP_UTF8, 0, utf8, size, utf16, n); |
|
| 261 | 260 |
return utf16; |
| 262 | 261 |
} |
| 263 | 262 |
|
| ... | ... |
@@ -89,8 +89,40 @@ LPCTSTR GetLastErrorText(); |
| 89 | 89 |
|
| 90 | 90 |
DWORD MsgToEventLog(DWORD flags, LPCTSTR lpszMsg, ...); |
| 91 | 91 |
|
| 92 |
-/* Convert a utf8 string to utf16. Caller should free the result */ |
|
| 93 |
-wchar_t *utf8to16(const char *utf8); |
|
| 92 |
+/** |
|
| 93 |
+ * Convert a UTF-8 string to UTF-16 |
|
| 94 |
+ * |
|
| 95 |
+ * The size parameter can be used to convert strings which contain inline NUL |
|
| 96 |
+ * characters, like MULTI_SZ strings used as values in the registry do, |
|
| 97 |
+ * or (sub)strings that are not zero terminated. If size is -1 the length |
|
| 98 |
+ * of the string is determined automatically by the WIN32 API. Make sure |
|
| 99 |
+ * you pass a terminated string or else bad things will happen. Note that |
|
| 100 |
+ * the size you pass should always include the terminating zero as well. |
|
| 101 |
+ * |
|
| 102 |
+ * If the returned string is not NULL it must be freed by the caller. |
|
| 103 |
+ * |
|
| 104 |
+ * @param utf8 const string to be converted |
|
| 105 |
+ * @param size the size of the string |
|
| 106 |
+ * |
|
| 107 |
+ * @return wchar_t* heap allocated result string |
|
| 108 |
+ */ |
|
| 109 |
+wchar_t *utf8to16_size(const char *utf8, int size); |
|
| 110 |
+ |
|
| 111 |
+/** |
|
| 112 |
+ * Convert a zero terminated UTF-8 string to UTF-16 |
|
| 113 |
+ * |
|
| 114 |
+ * This is just a wrapper function that always passes -1 as string size |
|
| 115 |
+ * to \ref utf8to16_size. |
|
| 116 |
+ * |
|
| 117 |
+ * @param utf8 const string to be converted |
|
| 118 |
+ * |
|
| 119 |
+ * @return wchar_t* heap allocated result string |
|
| 120 |
+ */ |
|
| 121 |
+static inline wchar_t * |
|
| 122 |
+utf8to16(const char *utf8) |
|
| 123 |
+{
|
|
| 124 |
+ return utf8to16_size(utf8, -1); |
|
| 125 |
+} |
|
| 94 | 126 |
|
| 95 | 127 |
/* return windows system directory as a pointer to a static string */ |
| 96 | 128 |
const wchar_t *get_win_sys_path(void); |