/src/FreeRDP/winpr/libwinpr/crt/unicode.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Unicode Conversion (CRT) |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2022 Armin Novak <anovak@thincast.com> |
7 | | * Copyright 2022 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <winpr/config.h> |
23 | | #include <winpr/assert.h> |
24 | | |
25 | | #include <errno.h> |
26 | | #include <wctype.h> |
27 | | |
28 | | #include <winpr/crt.h> |
29 | | #include <winpr/error.h> |
30 | | #include <winpr/print.h> |
31 | | #include <winpr/endian.h> |
32 | | |
33 | | #ifndef _WIN32 |
34 | | |
35 | | #include "unicode.h" |
36 | | |
37 | | /** |
38 | | * Notes on cross-platform Unicode portability: |
39 | | * |
40 | | * Unicode has many possible Unicode Transformation Format (UTF) encodings, |
41 | | * where some of the most commonly used are UTF-8, UTF-16 and sometimes UTF-32. |
42 | | * |
43 | | * The number in the UTF encoding name (8, 16, 32) refers to the number of bits |
44 | | * per code unit. A code unit is the minimal bit combination that can represent |
45 | | * a unit of encoded text in the given encoding. For instance, UTF-8 encodes |
46 | | * the English alphabet using 8 bits (or one byte) each, just like in ASCII. |
47 | | * |
48 | | * However, the total number of code points (values in the Unicode codespace) |
49 | | * only fits completely within 32 bits. This means that for UTF-8 and UTF-16, |
50 | | * more than one code unit may be required to fully encode a specific value. |
51 | | * UTF-8 and UTF-16 are variable-width encodings, while UTF-32 is fixed-width. |
52 | | * |
53 | | * UTF-8 has the advantage of being backwards compatible with ASCII, and is |
54 | | * one of the most commonly used Unicode encoding. |
55 | | * |
56 | | * UTF-16 is used everywhere in the Windows API. The strategy employed by |
57 | | * Microsoft to provide backwards compatibility in their API was to create |
58 | | * an ANSI and a Unicode version of the same function, ending with A (ANSI) |
59 | | * and W (Wide character, or UTF-16 Unicode). In headers, the original |
60 | | * function name is replaced by a macro that defines to either the ANSI |
61 | | * or Unicode version based on the definition of the _UNICODE macro. |
62 | | * |
63 | | * UTF-32 has the advantage of being fixed width, but wastes a lot of space |
64 | | * for English text (4x more than UTF-8, 2x more than UTF-16). |
65 | | * |
66 | | * In C, wide character strings are often defined with the wchar_t type. |
67 | | * Many functions are provided to deal with those wide character strings, |
68 | | * such as wcslen (strlen equivalent) or wprintf (printf equivalent). |
69 | | * |
70 | | * This may lead to some confusion, since many of these functions exist |
71 | | * on both Windows and Linux, but they are *not* the same! |
72 | | * |
73 | | * This sample hello world is a good example: |
74 | | * |
75 | | * #include <wchar.h> |
76 | | * |
77 | | * wchar_t hello[] = L"Hello, World!\n"; |
78 | | * |
79 | | * int main(int argc, char** argv) |
80 | | * { |
81 | | * wprintf(hello); |
82 | | * wprintf(L"sizeof(wchar_t): %d\n", sizeof(wchar_t)); |
83 | | * return 0; |
84 | | * } |
85 | | * |
86 | | * There is a reason why the sample prints the size of the wchar_t type: |
87 | | * On Windows, wchar_t is two bytes (UTF-16), while on most other systems |
88 | | * it is 4 bytes (UTF-32). This means that if you write code on Windows, |
89 | | * use L"" to define a string which is meant to be UTF-16 and not UTF-32, |
90 | | * you will have a little surprise when trying to port your code to Linux. |
91 | | * |
92 | | * Since the Windows API uses UTF-16, not UTF-32, WinPR defines the WCHAR |
93 | | * type to always be 2-bytes long and uses it instead of wchar_t. Do not |
94 | | * ever use wchar_t with WinPR unless you know what you are doing. |
95 | | * |
96 | | * As for L"", it is unfortunately unusable in a portable way, unless a |
97 | | * special option is passed to GCC to define wchar_t as being two bytes. |
98 | | * For string constants that must be UTF-16, it is a pain, but they can |
99 | | * be defined in a portable way like this: |
100 | | * |
101 | | * WCHAR hello[] = { 'H','e','l','l','o','\0' }; |
102 | | * |
103 | | * Such strings cannot be passed to native functions like wcslen(), which |
104 | | * may expect a different wchar_t size. For this reason, WinPR provides |
105 | | * _wcslen, which expects UTF-16 WCHAR strings on all platforms. |
106 | | * |
107 | | */ |
108 | | |
109 | | /** \deprecated We no longer export this function, see ConvertUtf8ToWChar family of functions for a |
110 | | * replacement |
111 | | * |
112 | | * Conversion to Unicode (UTF-16) |
113 | | * MultiByteToWideChar: http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072/ |
114 | | * |
115 | | * cbMultiByte is an input size in bytes (BYTE) |
116 | | * cchWideChar is an output size in wide characters (WCHAR) |
117 | | * |
118 | | * Null-terminated UTF-8 strings: |
119 | | * |
120 | | * cchWideChar *cannot* be assumed to be cbMultiByte since UTF-8 is variable-width! |
121 | | * |
122 | | * Instead, obtain the required cchWideChar output size like this: |
123 | | * cchWideChar = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) lpMultiByteStr, -1, nullptr, 0); |
124 | | * |
125 | | * A value of -1 for cbMultiByte indicates that the input string is null-terminated, |
126 | | * and the null terminator *will* be processed. The size returned by MultiByteToWideChar |
127 | | * will therefore include the null terminator. Equivalent behavior can be obtained by |
128 | | * computing the length in bytes of the input buffer, including the null terminator: |
129 | | * |
130 | | * cbMultiByte = strlen((char*) lpMultiByteStr) + 1; |
131 | | * |
132 | | * An output buffer of the proper size can then be allocated: |
133 | | * |
134 | | * lpWideCharStr = (LPWSTR) malloc(cchWideChar * sizeof(WCHAR)); |
135 | | * |
136 | | * Since cchWideChar is an output size in wide characters, the actual buffer size is: |
137 | | * (cchWideChar * sizeof(WCHAR)) or (cchWideChar * 2) |
138 | | * |
139 | | * Finally, perform the conversion: |
140 | | * |
141 | | * cchWideChar = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) lpMultiByteStr, -1, lpWideCharStr, |
142 | | * cchWideChar); |
143 | | * |
144 | | * The value returned by MultiByteToWideChar corresponds to the number of wide characters written |
145 | | * to the output buffer, and should match the value obtained on the first call to |
146 | | * MultiByteToWideChar. |
147 | | * |
148 | | */ |
149 | | |
150 | | #if !defined(WITH_WINPR_DEPRECATED) |
151 | | static |
152 | | #endif |
153 | | int |
154 | | MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, |
155 | | LPWSTR lpWideCharStr, int cchWideChar) |
156 | 7.80k | { |
157 | 7.80k | return int_MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, |
158 | 7.80k | cchWideChar); |
159 | 7.80k | } |
160 | | |
161 | | /** \deprecated We no longer export this function, see ConvertWCharToUtf8 family of functions for a |
162 | | * replacement |
163 | | * |
164 | | * Conversion from Unicode (UTF-16) |
165 | | * WideCharToMultiByte: http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130/ |
166 | | * |
167 | | * cchWideChar is an input size in wide characters (WCHAR) |
168 | | * cbMultiByte is an output size in bytes (BYTE) |
169 | | * |
170 | | * Null-terminated UTF-16 strings: |
171 | | * |
172 | | * cbMultiByte *cannot* be assumed to be cchWideChar since UTF-8 is variable-width! |
173 | | * |
174 | | * Instead, obtain the required cbMultiByte output size like this: |
175 | | * cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) lpWideCharStr, -1, nullptr, 0, nullptr, |
176 | | * nullptr); |
177 | | * |
178 | | * A value of -1 for cbMultiByte indicates that the input string is null-terminated, |
179 | | * and the null terminator *will* be processed. The size returned by WideCharToMultiByte |
180 | | * will therefore include the null terminator. Equivalent behavior can be obtained by |
181 | | * computing the length in bytes of the input buffer, including the null terminator: |
182 | | * |
183 | | * cchWideChar = _wcslen((WCHAR*) lpWideCharStr) + 1; |
184 | | * |
185 | | * An output buffer of the proper size can then be allocated: |
186 | | * lpMultiByteStr = (LPSTR) malloc(cbMultiByte); |
187 | | * |
188 | | * Since cbMultiByte is an output size in bytes, it is the same as the buffer size |
189 | | * |
190 | | * Finally, perform the conversion: |
191 | | * |
192 | | * cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) lpWideCharStr, -1, lpMultiByteStr, |
193 | | * cbMultiByte, nullptr, nullptr); |
194 | | * |
195 | | * The value returned by WideCharToMultiByte corresponds to the number of bytes written |
196 | | * to the output buffer, and should match the value obtained on the first call to |
197 | | * WideCharToMultiByte. |
198 | | * |
199 | | */ |
200 | | |
201 | | #if !defined(WITH_WINPR_DEPRECATED) |
202 | | static |
203 | | #endif |
204 | | int |
205 | | WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, |
206 | | LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, |
207 | | LPBOOL lpUsedDefaultChar) |
208 | 7.80k | { |
209 | 7.80k | return int_WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, |
210 | 7.80k | cbMultiByte, lpDefaultChar, lpUsedDefaultChar); |
211 | 7.80k | } |
212 | | |
213 | | #endif |
214 | | |
215 | | /** |
216 | | * ConvertToUnicode is a convenience wrapper for MultiByteToWideChar: |
217 | | * |
218 | | * If the lpWideCharStr parameter for the converted string points to nullptr |
219 | | * or if the cchWideChar parameter is set to 0 this function will automatically |
220 | | * allocate the required memory which is guaranteed to be null-terminated |
221 | | * after the conversion, even if the source c string isn't. |
222 | | * |
223 | | * If the cbMultiByte parameter is set to -1 the passed lpMultiByteStr must |
224 | | * be null-terminated and the required length for the converted string will be |
225 | | * calculated accordingly. |
226 | | */ |
227 | | #if defined(WITH_WINPR_DEPRECATED) |
228 | | int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, |
229 | | LPWSTR* lpWideCharStr, int cchWideChar) |
230 | | { |
231 | | int status = 0; |
232 | | BOOL allocate = FALSE; |
233 | | |
234 | | if (!lpMultiByteStr) |
235 | | return 0; |
236 | | |
237 | | if (!lpWideCharStr) |
238 | | return 0; |
239 | | |
240 | | if (cbMultiByte == -1) |
241 | | { |
242 | | size_t len = strnlen(lpMultiByteStr, INT_MAX); |
243 | | if (len >= INT_MAX) |
244 | | return 0; |
245 | | cbMultiByte = (int)(len + 1); |
246 | | } |
247 | | |
248 | | if (cchWideChar == 0) |
249 | | { |
250 | | cchWideChar = |
251 | | MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, nullptr, 0); |
252 | | allocate = TRUE; |
253 | | } |
254 | | else if (!(*lpWideCharStr)) |
255 | | allocate = TRUE; |
256 | | |
257 | | if (cchWideChar < 1) |
258 | | return 0; |
259 | | |
260 | | if (allocate) |
261 | | { |
262 | | *lpWideCharStr = (LPWSTR)calloc((size_t)cchWideChar + 1ull, sizeof(WCHAR)); |
263 | | |
264 | | if (!(*lpWideCharStr)) |
265 | | { |
266 | | // SetLastError(ERROR_INSUFFICIENT_BUFFER); |
267 | | return 0; |
268 | | } |
269 | | } |
270 | | |
271 | | status = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, *lpWideCharStr, |
272 | | cchWideChar); |
273 | | |
274 | | if (status != cchWideChar) |
275 | | { |
276 | | if (allocate) |
277 | | { |
278 | | free(*lpWideCharStr); |
279 | | *lpWideCharStr = nullptr; |
280 | | status = 0; |
281 | | } |
282 | | } |
283 | | |
284 | | return status; |
285 | | } |
286 | | #endif |
287 | | |
288 | | /** |
289 | | * ConvertFromUnicode is a convenience wrapper for WideCharToMultiByte: |
290 | | * |
291 | | * If the lpMultiByteStr parameter for the converted string points to nullptr |
292 | | * or if the cbMultiByte parameter is set to 0 this function will automatically |
293 | | * allocate the required memory which is guaranteed to be null-terminated |
294 | | * after the conversion, even if the source unicode string isn't. |
295 | | * |
296 | | * If the cchWideChar parameter is set to -1 the passed lpWideCharStr must |
297 | | * be null-terminated and the required length for the converted string will be |
298 | | * calculated accordingly. |
299 | | */ |
300 | | #if defined(WITH_WINPR_DEPRECATED) |
301 | | int ConvertFromUnicode(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, |
302 | | LPSTR* lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, |
303 | | LPBOOL lpUsedDefaultChar) |
304 | | { |
305 | | int status = 0; |
306 | | BOOL allocate = FALSE; |
307 | | |
308 | | if (!lpWideCharStr) |
309 | | return 0; |
310 | | |
311 | | if (!lpMultiByteStr) |
312 | | return 0; |
313 | | |
314 | | if (cchWideChar == -1) |
315 | | cchWideChar = (int)(_wcslen(lpWideCharStr) + 1); |
316 | | |
317 | | if (cbMultiByte == 0) |
318 | | { |
319 | | cbMultiByte = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, nullptr, 0, |
320 | | nullptr, nullptr); |
321 | | allocate = TRUE; |
322 | | } |
323 | | else if (!(*lpMultiByteStr)) |
324 | | allocate = TRUE; |
325 | | |
326 | | if (cbMultiByte < 1) |
327 | | return 0; |
328 | | |
329 | | if (allocate) |
330 | | { |
331 | | *lpMultiByteStr = (LPSTR)calloc(1, (size_t)cbMultiByte + 1ull); |
332 | | |
333 | | if (!(*lpMultiByteStr)) |
334 | | { |
335 | | // SetLastError(ERROR_INSUFFICIENT_BUFFER); |
336 | | return 0; |
337 | | } |
338 | | } |
339 | | |
340 | | status = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, *lpMultiByteStr, |
341 | | cbMultiByte, lpDefaultChar, lpUsedDefaultChar); |
342 | | |
343 | | if ((status != cbMultiByte) && allocate) |
344 | | { |
345 | | status = 0; |
346 | | } |
347 | | |
348 | | if ((status <= 0) && allocate) |
349 | | { |
350 | | free(*lpMultiByteStr); |
351 | | *lpMultiByteStr = nullptr; |
352 | | } |
353 | | |
354 | | return status; |
355 | | } |
356 | | #endif |
357 | | |
358 | | /** |
359 | | * Swap Unicode byte order (UTF16LE <-> UTF16BE) |
360 | | */ |
361 | | |
362 | | const WCHAR* ByteSwapUnicode(WCHAR* wstr, size_t length) |
363 | 0 | { |
364 | 0 | WINPR_ASSERT(wstr || (length == 0)); |
365 | |
|
366 | 0 | for (size_t x = 0; x < length; x++) |
367 | 0 | wstr[x] = _byteswap_ushort(wstr[x]); |
368 | 0 | return wstr; |
369 | 0 | } |
370 | | |
371 | | SSIZE_T ConvertWCharToUtf8(const WCHAR* wstr, char* str, size_t len) |
372 | 0 | { |
373 | 0 | if (!wstr) |
374 | 0 | { |
375 | 0 | if (str && len) |
376 | 0 | str[0] = 0; |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | 0 | const size_t wlen = _wcslen(wstr); |
381 | 0 | return ConvertWCharNToUtf8(wstr, wlen + 1, str, len); |
382 | 0 | } |
383 | | |
384 | | SSIZE_T ConvertWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len) |
385 | 7.80k | { |
386 | 7.80k | BOOL isNullTerminated = FALSE; |
387 | 7.80k | if (wlen == 0) |
388 | 0 | return 0; |
389 | | |
390 | 7.80k | WINPR_ASSERT(wstr); |
391 | 7.80k | size_t iwlen = _wcsnlen(wstr, wlen); |
392 | | |
393 | 7.80k | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
394 | 0 | { |
395 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
396 | 0 | return -1; |
397 | 0 | } |
398 | | |
399 | 7.80k | if (iwlen < wlen) |
400 | 7.80k | { |
401 | 7.80k | isNullTerminated = TRUE; |
402 | 7.80k | iwlen++; |
403 | 7.80k | } |
404 | 7.80k | WINPR_PRAGMA_DIAG_PUSH |
405 | 7.80k | WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL |
406 | 7.80k | const int rc = |
407 | 7.80k | WideCharToMultiByte(CP_UTF8, 0, wstr, (int)iwlen, str, (int)len, nullptr, nullptr); |
408 | 7.80k | WINPR_PRAGMA_DIAG_POP |
409 | 7.80k | if ((rc <= 0) || ((len > 0) && ((size_t)rc > len))) |
410 | 0 | return -1; |
411 | 7.80k | else if (!isNullTerminated) |
412 | 0 | { |
413 | 0 | if (str && ((size_t)rc < len)) |
414 | 0 | str[rc] = '\0'; |
415 | 0 | return rc; |
416 | 0 | } |
417 | 7.80k | else if ((size_t)rc == len) |
418 | 975 | { |
419 | 975 | if (str && (str[rc - 1] != '\0')) |
420 | 0 | return rc; |
421 | 975 | } |
422 | 7.80k | return rc - 1; |
423 | 7.80k | } |
424 | | |
425 | | SSIZE_T ConvertMszWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len) |
426 | 0 | { |
427 | 0 | if (wlen == 0) |
428 | 0 | return 0; |
429 | | |
430 | 0 | WINPR_ASSERT(wstr); |
431 | |
|
432 | 0 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
433 | 0 | { |
434 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
435 | 0 | return -1; |
436 | 0 | } |
437 | | |
438 | 0 | const int iwlen = (int)len; |
439 | 0 | WINPR_PRAGMA_DIAG_PUSH |
440 | 0 | WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL |
441 | 0 | const int rc = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)wlen, str, iwlen, nullptr, nullptr); |
442 | 0 | WINPR_PRAGMA_DIAG_POP |
443 | 0 | if ((rc <= 0) || ((len > 0) && (rc > iwlen))) |
444 | 0 | return -1; |
445 | | |
446 | 0 | return rc; |
447 | 0 | } |
448 | | |
449 | | SSIZE_T ConvertUtf8ToWChar(const char* str, WCHAR* wstr, size_t wlen) |
450 | 7.80k | { |
451 | 7.80k | if (!str) |
452 | 0 | { |
453 | 0 | if (wstr && wlen) |
454 | 0 | wstr[0] = 0; |
455 | 0 | return 0; |
456 | 0 | } |
457 | | |
458 | 7.80k | const size_t len = strlen(str); |
459 | 7.80k | return ConvertUtf8NToWChar(str, len + 1, wstr, wlen); |
460 | 7.80k | } |
461 | | |
462 | | SSIZE_T ConvertUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen) |
463 | 7.80k | { |
464 | 7.80k | size_t ilen = strnlen(str, len); |
465 | 7.80k | BOOL isNullTerminated = FALSE; |
466 | 7.80k | if (len == 0) |
467 | 0 | return 0; |
468 | | |
469 | 7.80k | WINPR_ASSERT(str); |
470 | | |
471 | 7.80k | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
472 | 0 | { |
473 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
474 | 0 | return -1; |
475 | 0 | } |
476 | 7.80k | if (ilen < len) |
477 | 7.80k | { |
478 | 7.80k | isNullTerminated = TRUE; |
479 | 7.80k | ilen++; |
480 | 7.80k | } |
481 | | |
482 | 7.80k | const int iwlen = (int)wlen; |
483 | 7.80k | WINPR_PRAGMA_DIAG_PUSH |
484 | 7.80k | WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL |
485 | 7.80k | const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)ilen, wstr, iwlen); |
486 | 7.80k | WINPR_PRAGMA_DIAG_POP |
487 | 7.80k | if ((rc <= 0) || ((wlen > 0) && (rc > iwlen))) |
488 | 0 | return -1; |
489 | 7.80k | if (!isNullTerminated) |
490 | 0 | { |
491 | 0 | if (wstr && (rc < iwlen)) |
492 | 0 | wstr[rc] = '\0'; |
493 | 0 | return rc; |
494 | 0 | } |
495 | 7.80k | else if (rc == iwlen) |
496 | 0 | { |
497 | 0 | if (wstr && (wstr[rc - 1] != '\0')) |
498 | 0 | return rc; |
499 | 0 | } |
500 | 7.80k | return rc - 1; |
501 | 7.80k | } |
502 | | |
503 | | SSIZE_T ConvertMszUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen) |
504 | 0 | { |
505 | 0 | if (len == 0) |
506 | 0 | return 0; |
507 | | |
508 | 0 | WINPR_ASSERT(str); |
509 | |
|
510 | 0 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
511 | 0 | { |
512 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
513 | 0 | return -1; |
514 | 0 | } |
515 | | |
516 | 0 | const int iwlen = (int)wlen; |
517 | 0 | WINPR_PRAGMA_DIAG_PUSH |
518 | 0 | WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL |
519 | 0 | const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)len, wstr, iwlen); |
520 | 0 | WINPR_PRAGMA_DIAG_POP |
521 | 0 | if ((rc <= 0) || ((wlen > 0) && (rc > iwlen))) |
522 | 0 | return -1; |
523 | | |
524 | 0 | return rc; |
525 | 0 | } |
526 | | |
527 | | char* ConvertWCharToUtf8Alloc(const WCHAR* wstr, size_t* pUtfCharLength) |
528 | 0 | { |
529 | 0 | char* tmp = nullptr; |
530 | 0 | const SSIZE_T rc = ConvertWCharToUtf8(wstr, nullptr, 0); |
531 | 0 | if (pUtfCharLength) |
532 | 0 | *pUtfCharLength = 0; |
533 | 0 | if (rc < 0) |
534 | 0 | return nullptr; |
535 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
536 | 0 | if (!tmp) |
537 | 0 | return nullptr; |
538 | 0 | const SSIZE_T rc2 = ConvertWCharToUtf8(wstr, tmp, (size_t)rc + 1ull); |
539 | 0 | if (rc2 < 0) |
540 | 0 | { |
541 | 0 | free(tmp); |
542 | 0 | return nullptr; |
543 | 0 | } |
544 | 0 | WINPR_ASSERT(rc == rc2); |
545 | 0 | if (pUtfCharLength) |
546 | 0 | *pUtfCharLength = (size_t)rc2; |
547 | 0 | return tmp; |
548 | 0 | } |
549 | | |
550 | | char* ConvertWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength) |
551 | 975 | { |
552 | 975 | char* tmp = nullptr; |
553 | 975 | const SSIZE_T rc = ConvertWCharNToUtf8(wstr, wlen, nullptr, 0); |
554 | | |
555 | 975 | if (pUtfCharLength) |
556 | 975 | *pUtfCharLength = 0; |
557 | 975 | if (rc < 0) |
558 | 0 | return nullptr; |
559 | 975 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
560 | 975 | if (!tmp) |
561 | 0 | return nullptr; |
562 | 975 | const SSIZE_T rc2 = ConvertWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull); |
563 | 975 | if (rc2 < 0) |
564 | 0 | { |
565 | 0 | free(tmp); |
566 | 0 | return nullptr; |
567 | 0 | } |
568 | 975 | WINPR_ASSERT(rc == rc2); |
569 | 975 | if (pUtfCharLength) |
570 | 975 | *pUtfCharLength = (size_t)rc2; |
571 | 975 | return tmp; |
572 | 975 | } |
573 | | |
574 | | char* ConvertMszWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength) |
575 | 0 | { |
576 | 0 | char* tmp = nullptr; |
577 | 0 | const SSIZE_T rc = ConvertMszWCharNToUtf8(wstr, wlen, nullptr, 0); |
578 | |
|
579 | 0 | if (pUtfCharLength) |
580 | 0 | *pUtfCharLength = 0; |
581 | 0 | if (rc < 0) |
582 | 0 | return nullptr; |
583 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
584 | 0 | if (!tmp) |
585 | 0 | return nullptr; |
586 | 0 | const SSIZE_T rc2 = ConvertMszWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull); |
587 | 0 | if (rc2 < 0) |
588 | 0 | { |
589 | 0 | free(tmp); |
590 | 0 | return nullptr; |
591 | 0 | } |
592 | 0 | WINPR_ASSERT(rc == rc2); |
593 | 0 | if (pUtfCharLength) |
594 | 0 | *pUtfCharLength = (size_t)rc2; |
595 | 0 | return tmp; |
596 | 0 | } |
597 | | |
598 | | WCHAR* ConvertUtf8ToWCharAlloc(const char* str, size_t* pSize) |
599 | 0 | { |
600 | 0 | WCHAR* tmp = nullptr; |
601 | 0 | const SSIZE_T rc = ConvertUtf8ToWChar(str, nullptr, 0); |
602 | 0 | if (pSize) |
603 | 0 | *pSize = 0; |
604 | 0 | if (rc < 0) |
605 | 0 | return nullptr; |
606 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
607 | 0 | if (!tmp) |
608 | 0 | return nullptr; |
609 | 0 | const SSIZE_T rc2 = ConvertUtf8ToWChar(str, tmp, (size_t)rc + 1ull); |
610 | 0 | if (rc2 < 0) |
611 | 0 | { |
612 | 0 | free(tmp); |
613 | 0 | return nullptr; |
614 | 0 | } |
615 | 0 | WINPR_ASSERT(rc == rc2); |
616 | 0 | if (pSize) |
617 | 0 | *pSize = (size_t)rc2; |
618 | 0 | return tmp; |
619 | 0 | } |
620 | | |
621 | | WCHAR* ConvertUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize) |
622 | 0 | { |
623 | 0 | WCHAR* tmp = nullptr; |
624 | 0 | const SSIZE_T rc = ConvertUtf8NToWChar(str, len, nullptr, 0); |
625 | 0 | if (pSize) |
626 | 0 | *pSize = 0; |
627 | 0 | if (rc < 0) |
628 | 0 | return nullptr; |
629 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
630 | 0 | if (!tmp) |
631 | 0 | return nullptr; |
632 | 0 | const SSIZE_T rc2 = ConvertUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull); |
633 | 0 | if (rc2 < 0) |
634 | 0 | { |
635 | 0 | free(tmp); |
636 | 0 | return nullptr; |
637 | 0 | } |
638 | 0 | WINPR_ASSERT(rc == rc2); |
639 | 0 | if (pSize) |
640 | 0 | *pSize = (size_t)rc2; |
641 | 0 | return tmp; |
642 | 0 | } |
643 | | |
644 | | WCHAR* ConvertMszUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize) |
645 | 0 | { |
646 | 0 | WCHAR* tmp = nullptr; |
647 | 0 | const SSIZE_T rc = ConvertMszUtf8NToWChar(str, len, nullptr, 0); |
648 | 0 | if (pSize) |
649 | 0 | *pSize = 0; |
650 | 0 | if (rc < 0) |
651 | 0 | return nullptr; |
652 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
653 | 0 | if (!tmp) |
654 | 0 | return nullptr; |
655 | 0 | const SSIZE_T rc2 = ConvertMszUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull); |
656 | 0 | if (rc2 < 0) |
657 | 0 | { |
658 | 0 | free(tmp); |
659 | 0 | return nullptr; |
660 | 0 | } |
661 | 0 | WINPR_ASSERT(rc == rc2); |
662 | 0 | if (pSize) |
663 | 0 | *pSize = (size_t)rc2; |
664 | 0 | return tmp; |
665 | 0 | } |
666 | | |
667 | | WINPR_ATTR_NODISCARD |
668 | | static INT8 nnibble(char c) |
669 | 0 | { |
670 | 0 | if ((c >= '0') && (c <= '9')) |
671 | 0 | return (INT8)(c - '0'); |
672 | 0 | if ((c >= 'a') && (c <= 'f')) |
673 | 0 | return (INT8)((c - 'a') + 0xa); |
674 | 0 | if ((c >= 'A') && (c <= 'F')) |
675 | 0 | return (INT8)((c - 'A') + 0xa); |
676 | 0 | return -1; |
677 | 0 | } |
678 | | |
679 | | WINPR_ATTR_NODISCARD |
680 | | static INT32 escapeToNumber(char* str, char** next) |
681 | 0 | { |
682 | 0 | if (strncmp(str, "\\u", 2) != 0) |
683 | 0 | return -1; |
684 | | |
685 | 0 | size_t offset = 2; |
686 | 0 | INT32 code = 0; |
687 | 0 | for (size_t x = 0; x < 4; x++) |
688 | 0 | { |
689 | 0 | const INT8 val = nnibble(str[offset++]); |
690 | 0 | if (val < 0) |
691 | 0 | return -1; |
692 | | |
693 | 0 | code <<= 4; |
694 | 0 | code |= val; |
695 | 0 | } |
696 | 0 | *next = &str[offset]; |
697 | 0 | return code; |
698 | 0 | } |
699 | | |
700 | | SSIZE_T winpr_utfEscapedStringToUtf8(char* str, size_t len) |
701 | 0 | { |
702 | 0 | char* cur = str; |
703 | 0 | size_t clen = len; |
704 | 0 | while ((cur = winpr_strnstr(cur, "\\u", clen))) |
705 | 0 | { |
706 | 0 | char* next = nullptr; |
707 | 0 | const INT32 code = escapeToNumber(cur, &next); |
708 | 0 | if (code < 0) |
709 | 0 | { |
710 | | /* Not a \uXXXX escape (e.g. "C:\users"): keep it as literal text */ |
711 | 0 | cur += 2; |
712 | 0 | clen = len - (size_t)(cur - str); |
713 | 0 | continue; |
714 | 0 | } |
715 | | |
716 | 0 | WCHAR wc[2] = WINPR_C_ARRAY_INIT; |
717 | 0 | winpr_Data_Write_UINT16(&wc[0], (WCHAR)code); |
718 | | |
719 | | // Check for high surrogate |
720 | 0 | if ((code & 0xd800) == 0xd800) |
721 | 0 | { |
722 | 0 | const INT32 code2 = escapeToNumber(next, &next); |
723 | 0 | if (code2 < 0) |
724 | 0 | return -1; |
725 | | |
726 | | // low surrogate check. must match |
727 | 0 | if ((code2 & 0xdc00) != 0xdc00) |
728 | 0 | return -1; |
729 | | |
730 | 0 | winpr_Data_Write_UINT16(&wc[1], (WCHAR)code2); |
731 | 0 | } |
732 | | |
733 | 0 | char utf8[8] = WINPR_C_ARRAY_INIT; |
734 | 0 | const SSIZE_T rc = ConvertWCharNToUtf8(wc, ARRAYSIZE(wc), utf8, ARRAYSIZE(utf8)); |
735 | 0 | if (rc < 0) |
736 | 0 | return -1; |
737 | | |
738 | 0 | strncpy(cur, utf8, (size_t)rc); |
739 | |
|
740 | 0 | const size_t diff = (size_t)(next - cur); |
741 | 0 | if (diff > (size_t)rc) |
742 | 0 | { |
743 | 0 | const size_t rlen = strnlen(next, len); |
744 | 0 | memmove(&cur[(size_t)rc], next, rlen + 1); |
745 | 0 | } |
746 | | |
747 | | /* Continue after the decoded bytes, so they are never decoded again */ |
748 | 0 | cur += rc; |
749 | 0 | clen = len - (size_t)(cur - str); |
750 | 0 | } |
751 | 0 | return (SSIZE_T)strnlen(str, len); |
752 | 0 | } |
753 | | |
754 | | char* winpr_utf8ToUtfEscapedString(char* str, size_t len, size_t* pDstLen) |
755 | 0 | { |
756 | 0 | WINPR_ASSERT(str || (len == 0)); |
757 | |
|
758 | 0 | if (pDstLen) |
759 | 0 | *pDstLen = 0; |
760 | |
|
761 | 0 | if (len >= SIZE_MAX / 12) |
762 | 0 | return nullptr; |
763 | | |
764 | 0 | char* escaped = nullptr; |
765 | 0 | size_t offset = 0; |
766 | 0 | size_t wlen = 0; |
767 | 0 | WCHAR* wstr = ConvertUtf8NToWCharAlloc(str, len, &wlen); |
768 | 0 | if (!wstr || (wlen == 0)) |
769 | 0 | goto fail; |
770 | | |
771 | 0 | const size_t esclen = len * 12ull; |
772 | 0 | escaped = calloc(len + 1, 12); |
773 | 0 | if (!escaped) |
774 | 0 | goto fail; |
775 | | |
776 | 0 | for (size_t x = 0; x < wlen; x++) |
777 | 0 | { |
778 | 0 | const WCHAR wc = winpr_Data_Get_UINT16(&wstr[x]); |
779 | 0 | if (wc < 0x80) |
780 | 0 | escaped[offset++] = (char)wc; |
781 | 0 | else |
782 | 0 | { |
783 | 0 | const int rc = snprintf(&escaped[offset], esclen - offset, "\\u%04x", wc); |
784 | 0 | if (rc < 0) |
785 | 0 | break; |
786 | 0 | offset += (size_t)rc; |
787 | 0 | } |
788 | 0 | } |
789 | 0 | escaped[offset] = '\0'; |
790 | |
|
791 | 0 | fail: |
792 | 0 | if (pDstLen) |
793 | 0 | *pDstLen = offset; |
794 | |
|
795 | 0 | winpr_znfree(wstr, wlen * sizeof(WCHAR)); |
796 | 0 | return escaped; |
797 | 0 | } |