/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 | | |
32 | | #ifndef _WIN32 |
33 | | |
34 | | #include "unicode.h" |
35 | | |
36 | | /** |
37 | | * Notes on cross-platform Unicode portability: |
38 | | * |
39 | | * Unicode has many possible Unicode Transformation Format (UTF) encodings, |
40 | | * where some of the most commonly used are UTF-8, UTF-16 and sometimes UTF-32. |
41 | | * |
42 | | * The number in the UTF encoding name (8, 16, 32) refers to the number of bits |
43 | | * per code unit. A code unit is the minimal bit combination that can represent |
44 | | * a unit of encoded text in the given encoding. For instance, UTF-8 encodes |
45 | | * the English alphabet using 8 bits (or one byte) each, just like in ASCII. |
46 | | * |
47 | | * However, the total number of code points (values in the Unicode codespace) |
48 | | * only fits completely within 32 bits. This means that for UTF-8 and UTF-16, |
49 | | * more than one code unit may be required to fully encode a specific value. |
50 | | * UTF-8 and UTF-16 are variable-width encodings, while UTF-32 is fixed-width. |
51 | | * |
52 | | * UTF-8 has the advantage of being backwards compatible with ASCII, and is |
53 | | * one of the most commonly used Unicode encoding. |
54 | | * |
55 | | * UTF-16 is used everywhere in the Windows API. The strategy employed by |
56 | | * Microsoft to provide backwards compatibility in their API was to create |
57 | | * an ANSI and a Unicode version of the same function, ending with A (ANSI) |
58 | | * and W (Wide character, or UTF-16 Unicode). In headers, the original |
59 | | * function name is replaced by a macro that defines to either the ANSI |
60 | | * or Unicode version based on the definition of the _UNICODE macro. |
61 | | * |
62 | | * UTF-32 has the advantage of being fixed width, but wastes a lot of space |
63 | | * for English text (4x more than UTF-8, 2x more than UTF-16). |
64 | | * |
65 | | * In C, wide character strings are often defined with the wchar_t type. |
66 | | * Many functions are provided to deal with those wide character strings, |
67 | | * such as wcslen (strlen equivalent) or wprintf (printf equivalent). |
68 | | * |
69 | | * This may lead to some confusion, since many of these functions exist |
70 | | * on both Windows and Linux, but they are *not* the same! |
71 | | * |
72 | | * This sample hello world is a good example: |
73 | | * |
74 | | * #include <wchar.h> |
75 | | * |
76 | | * wchar_t hello[] = L"Hello, World!\n"; |
77 | | * |
78 | | * int main(int argc, char** argv) |
79 | | * { |
80 | | * wprintf(hello); |
81 | | * wprintf(L"sizeof(wchar_t): %d\n", sizeof(wchar_t)); |
82 | | * return 0; |
83 | | * } |
84 | | * |
85 | | * There is a reason why the sample prints the size of the wchar_t type: |
86 | | * On Windows, wchar_t is two bytes (UTF-16), while on most other systems |
87 | | * it is 4 bytes (UTF-32). This means that if you write code on Windows, |
88 | | * use L"" to define a string which is meant to be UTF-16 and not UTF-32, |
89 | | * you will have a little surprise when trying to port your code to Linux. |
90 | | * |
91 | | * Since the Windows API uses UTF-16, not UTF-32, WinPR defines the WCHAR |
92 | | * type to always be 2-bytes long and uses it instead of wchar_t. Do not |
93 | | * ever use wchar_t with WinPR unless you know what you are doing. |
94 | | * |
95 | | * As for L"", it is unfortunately unusable in a portable way, unless a |
96 | | * special option is passed to GCC to define wchar_t as being two bytes. |
97 | | * For string constants that must be UTF-16, it is a pain, but they can |
98 | | * be defined in a portable way like this: |
99 | | * |
100 | | * WCHAR hello[] = { 'H','e','l','l','o','\0' }; |
101 | | * |
102 | | * Such strings cannot be passed to native functions like wcslen(), which |
103 | | * may expect a different wchar_t size. For this reason, WinPR provides |
104 | | * _wcslen, which expects UTF-16 WCHAR strings on all platforms. |
105 | | * |
106 | | */ |
107 | | |
108 | | /** \deprecated We no longer export this function, see ConvertUtf8ToWChar family of functions for a |
109 | | * replacement |
110 | | * |
111 | | * Conversion to Unicode (UTF-16) |
112 | | * MultiByteToWideChar: http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072/ |
113 | | * |
114 | | * cbMultiByte is an input size in bytes (BYTE) |
115 | | * cchWideChar is an output size in wide characters (WCHAR) |
116 | | * |
117 | | * Null-terminated UTF-8 strings: |
118 | | * |
119 | | * cchWideChar *cannot* be assumed to be cbMultiByte since UTF-8 is variable-width! |
120 | | * |
121 | | * Instead, obtain the required cchWideChar output size like this: |
122 | | * cchWideChar = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) lpMultiByteStr, -1, nullptr, 0); |
123 | | * |
124 | | * A value of -1 for cbMultiByte indicates that the input string is null-terminated, |
125 | | * and the null terminator *will* be processed. The size returned by MultiByteToWideChar |
126 | | * will therefore include the null terminator. Equivalent behavior can be obtained by |
127 | | * computing the length in bytes of the input buffer, including the null terminator: |
128 | | * |
129 | | * cbMultiByte = strlen((char*) lpMultiByteStr) + 1; |
130 | | * |
131 | | * An output buffer of the proper size can then be allocated: |
132 | | * |
133 | | * lpWideCharStr = (LPWSTR) malloc(cchWideChar * sizeof(WCHAR)); |
134 | | * |
135 | | * Since cchWideChar is an output size in wide characters, the actual buffer size is: |
136 | | * (cchWideChar * sizeof(WCHAR)) or (cchWideChar * 2) |
137 | | * |
138 | | * Finally, perform the conversion: |
139 | | * |
140 | | * cchWideChar = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) lpMultiByteStr, -1, lpWideCharStr, |
141 | | * cchWideChar); |
142 | | * |
143 | | * The value returned by MultiByteToWideChar corresponds to the number of wide characters written |
144 | | * to the output buffer, and should match the value obtained on the first call to |
145 | | * MultiByteToWideChar. |
146 | | * |
147 | | */ |
148 | | |
149 | | #if !defined(WITH_WINPR_DEPRECATED) |
150 | | static |
151 | | #endif |
152 | | int |
153 | | MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, |
154 | | LPWSTR lpWideCharStr, int cchWideChar) |
155 | 1 | { |
156 | 1 | return int_MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, |
157 | 1 | cchWideChar); |
158 | 1 | } |
159 | | |
160 | | /** \deprecated We no longer export this function, see ConvertWCharToUtf8 family of functions for a |
161 | | * replacement |
162 | | * |
163 | | * Conversion from Unicode (UTF-16) |
164 | | * WideCharToMultiByte: http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130/ |
165 | | * |
166 | | * cchWideChar is an input size in wide characters (WCHAR) |
167 | | * cbMultiByte is an output size in bytes (BYTE) |
168 | | * |
169 | | * Null-terminated UTF-16 strings: |
170 | | * |
171 | | * cbMultiByte *cannot* be assumed to be cchWideChar since UTF-8 is variable-width! |
172 | | * |
173 | | * Instead, obtain the required cbMultiByte output size like this: |
174 | | * cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) lpWideCharStr, -1, nullptr, 0, nullptr, |
175 | | * nullptr); |
176 | | * |
177 | | * A value of -1 for cbMultiByte indicates that the input string is null-terminated, |
178 | | * and the null terminator *will* be processed. The size returned by WideCharToMultiByte |
179 | | * will therefore include the null terminator. Equivalent behavior can be obtained by |
180 | | * computing the length in bytes of the input buffer, including the null terminator: |
181 | | * |
182 | | * cchWideChar = _wcslen((WCHAR*) lpWideCharStr) + 1; |
183 | | * |
184 | | * An output buffer of the proper size can then be allocated: |
185 | | * lpMultiByteStr = (LPSTR) malloc(cbMultiByte); |
186 | | * |
187 | | * Since cbMultiByte is an output size in bytes, it is the same as the buffer size |
188 | | * |
189 | | * Finally, perform the conversion: |
190 | | * |
191 | | * cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) lpWideCharStr, -1, lpMultiByteStr, |
192 | | * cbMultiByte, nullptr, nullptr); |
193 | | * |
194 | | * The value returned by WideCharToMultiByte corresponds to the number of bytes written |
195 | | * to the output buffer, and should match the value obtained on the first call to |
196 | | * WideCharToMultiByte. |
197 | | * |
198 | | */ |
199 | | |
200 | | #if !defined(WITH_WINPR_DEPRECATED) |
201 | | static |
202 | | #endif |
203 | | int |
204 | | WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, |
205 | | LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, |
206 | | LPBOOL lpUsedDefaultChar) |
207 | 0 | { |
208 | 0 | return int_WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, |
209 | 0 | cbMultiByte, lpDefaultChar, lpUsedDefaultChar); |
210 | 0 | } |
211 | | |
212 | | #endif |
213 | | |
214 | | /** |
215 | | * ConvertToUnicode is a convenience wrapper for MultiByteToWideChar: |
216 | | * |
217 | | * If the lpWideCharStr parameter for the converted string points to nullptr |
218 | | * or if the cchWideChar parameter is set to 0 this function will automatically |
219 | | * allocate the required memory which is guaranteed to be null-terminated |
220 | | * after the conversion, even if the source c string isn't. |
221 | | * |
222 | | * If the cbMultiByte parameter is set to -1 the passed lpMultiByteStr must |
223 | | * be null-terminated and the required length for the converted string will be |
224 | | * calculated accordingly. |
225 | | */ |
226 | | #if defined(WITH_WINPR_DEPRECATED) |
227 | | int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, |
228 | | LPWSTR* lpWideCharStr, int cchWideChar) |
229 | | { |
230 | | int status = 0; |
231 | | BOOL allocate = FALSE; |
232 | | |
233 | | if (!lpMultiByteStr) |
234 | | return 0; |
235 | | |
236 | | if (!lpWideCharStr) |
237 | | return 0; |
238 | | |
239 | | if (cbMultiByte == -1) |
240 | | { |
241 | | size_t len = strnlen(lpMultiByteStr, INT_MAX); |
242 | | if (len >= INT_MAX) |
243 | | return 0; |
244 | | cbMultiByte = (int)(len + 1); |
245 | | } |
246 | | |
247 | | if (cchWideChar == 0) |
248 | | { |
249 | | cchWideChar = |
250 | | MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, nullptr, 0); |
251 | | allocate = TRUE; |
252 | | } |
253 | | else if (!(*lpWideCharStr)) |
254 | | allocate = TRUE; |
255 | | |
256 | | if (cchWideChar < 1) |
257 | | return 0; |
258 | | |
259 | | if (allocate) |
260 | | { |
261 | | *lpWideCharStr = (LPWSTR)calloc((size_t)cchWideChar + 1ull, sizeof(WCHAR)); |
262 | | |
263 | | if (!(*lpWideCharStr)) |
264 | | { |
265 | | // SetLastError(ERROR_INSUFFICIENT_BUFFER); |
266 | | return 0; |
267 | | } |
268 | | } |
269 | | |
270 | | status = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, *lpWideCharStr, |
271 | | cchWideChar); |
272 | | |
273 | | if (status != cchWideChar) |
274 | | { |
275 | | if (allocate) |
276 | | { |
277 | | free(*lpWideCharStr); |
278 | | *lpWideCharStr = nullptr; |
279 | | status = 0; |
280 | | } |
281 | | } |
282 | | |
283 | | return status; |
284 | | } |
285 | | #endif |
286 | | |
287 | | /** |
288 | | * ConvertFromUnicode is a convenience wrapper for WideCharToMultiByte: |
289 | | * |
290 | | * If the lpMultiByteStr parameter for the converted string points to nullptr |
291 | | * or if the cbMultiByte parameter is set to 0 this function will automatically |
292 | | * allocate the required memory which is guaranteed to be null-terminated |
293 | | * after the conversion, even if the source unicode string isn't. |
294 | | * |
295 | | * If the cchWideChar parameter is set to -1 the passed lpWideCharStr must |
296 | | * be null-terminated and the required length for the converted string will be |
297 | | * calculated accordingly. |
298 | | */ |
299 | | #if defined(WITH_WINPR_DEPRECATED) |
300 | | int ConvertFromUnicode(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, |
301 | | LPSTR* lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, |
302 | | LPBOOL lpUsedDefaultChar) |
303 | | { |
304 | | int status = 0; |
305 | | BOOL allocate = FALSE; |
306 | | |
307 | | if (!lpWideCharStr) |
308 | | return 0; |
309 | | |
310 | | if (!lpMultiByteStr) |
311 | | return 0; |
312 | | |
313 | | if (cchWideChar == -1) |
314 | | cchWideChar = (int)(_wcslen(lpWideCharStr) + 1); |
315 | | |
316 | | if (cbMultiByte == 0) |
317 | | { |
318 | | cbMultiByte = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, nullptr, 0, |
319 | | nullptr, nullptr); |
320 | | allocate = TRUE; |
321 | | } |
322 | | else if (!(*lpMultiByteStr)) |
323 | | allocate = TRUE; |
324 | | |
325 | | if (cbMultiByte < 1) |
326 | | return 0; |
327 | | |
328 | | if (allocate) |
329 | | { |
330 | | *lpMultiByteStr = (LPSTR)calloc(1, (size_t)cbMultiByte + 1ull); |
331 | | |
332 | | if (!(*lpMultiByteStr)) |
333 | | { |
334 | | // SetLastError(ERROR_INSUFFICIENT_BUFFER); |
335 | | return 0; |
336 | | } |
337 | | } |
338 | | |
339 | | status = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, *lpMultiByteStr, |
340 | | cbMultiByte, lpDefaultChar, lpUsedDefaultChar); |
341 | | |
342 | | if ((status != cbMultiByte) && allocate) |
343 | | { |
344 | | status = 0; |
345 | | } |
346 | | |
347 | | if ((status <= 0) && allocate) |
348 | | { |
349 | | free(*lpMultiByteStr); |
350 | | *lpMultiByteStr = nullptr; |
351 | | } |
352 | | |
353 | | return status; |
354 | | } |
355 | | #endif |
356 | | |
357 | | /** |
358 | | * Swap Unicode byte order (UTF16LE <-> UTF16BE) |
359 | | */ |
360 | | |
361 | | const WCHAR* ByteSwapUnicode(WCHAR* wstr, size_t length) |
362 | 0 | { |
363 | 0 | WINPR_ASSERT(wstr || (length == 0)); |
364 | |
|
365 | 0 | for (size_t x = 0; x < length; x++) |
366 | 0 | wstr[x] = _byteswap_ushort(wstr[x]); |
367 | 0 | return wstr; |
368 | 0 | } |
369 | | |
370 | | SSIZE_T ConvertWCharToUtf8(const WCHAR* wstr, char* str, size_t len) |
371 | 0 | { |
372 | 0 | if (!wstr) |
373 | 0 | { |
374 | 0 | if (str && len) |
375 | 0 | str[0] = 0; |
376 | 0 | return 0; |
377 | 0 | } |
378 | | |
379 | 0 | const size_t wlen = _wcslen(wstr); |
380 | 0 | return ConvertWCharNToUtf8(wstr, wlen + 1, str, len); |
381 | 0 | } |
382 | | |
383 | | SSIZE_T ConvertWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len) |
384 | 0 | { |
385 | 0 | BOOL isNullTerminated = FALSE; |
386 | 0 | if (wlen == 0) |
387 | 0 | return 0; |
388 | | |
389 | 0 | WINPR_ASSERT(wstr); |
390 | 0 | size_t iwlen = _wcsnlen(wstr, wlen); |
391 | |
|
392 | 0 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
393 | 0 | { |
394 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
395 | 0 | return -1; |
396 | 0 | } |
397 | | |
398 | 0 | if (iwlen < wlen) |
399 | 0 | { |
400 | 0 | isNullTerminated = TRUE; |
401 | 0 | iwlen++; |
402 | 0 | } |
403 | 0 | const int rc = |
404 | 0 | WideCharToMultiByte(CP_UTF8, 0, wstr, (int)iwlen, str, (int)len, nullptr, nullptr); |
405 | 0 | if ((rc <= 0) || ((len > 0) && ((size_t)rc > len))) |
406 | 0 | return -1; |
407 | 0 | else if (!isNullTerminated) |
408 | 0 | { |
409 | 0 | if (str && ((size_t)rc < len)) |
410 | 0 | str[rc] = '\0'; |
411 | 0 | return rc; |
412 | 0 | } |
413 | 0 | else if ((size_t)rc == len) |
414 | 0 | { |
415 | 0 | if (str && (str[rc - 1] != '\0')) |
416 | 0 | return rc; |
417 | 0 | } |
418 | 0 | return rc - 1; |
419 | 0 | } |
420 | | |
421 | | SSIZE_T ConvertMszWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len) |
422 | 0 | { |
423 | 0 | if (wlen == 0) |
424 | 0 | return 0; |
425 | | |
426 | 0 | WINPR_ASSERT(wstr); |
427 | |
|
428 | 0 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
429 | 0 | { |
430 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
431 | 0 | return -1; |
432 | 0 | } |
433 | | |
434 | 0 | const int iwlen = (int)len; |
435 | 0 | const int rc = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)wlen, str, iwlen, nullptr, nullptr); |
436 | 0 | if ((rc <= 0) || ((len > 0) && (rc > iwlen))) |
437 | 0 | return -1; |
438 | | |
439 | 0 | return rc; |
440 | 0 | } |
441 | | |
442 | | SSIZE_T ConvertUtf8ToWChar(const char* str, WCHAR* wstr, size_t wlen) |
443 | 0 | { |
444 | 0 | if (!str) |
445 | 0 | { |
446 | 0 | if (wstr && wlen) |
447 | 0 | wstr[0] = 0; |
448 | 0 | return 0; |
449 | 0 | } |
450 | | |
451 | 0 | const size_t len = strlen(str); |
452 | 0 | return ConvertUtf8NToWChar(str, len + 1, wstr, wlen); |
453 | 0 | } |
454 | | |
455 | | SSIZE_T ConvertUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen) |
456 | 1 | { |
457 | 1 | size_t ilen = strnlen(str, len); |
458 | 1 | BOOL isNullTerminated = FALSE; |
459 | 1 | if (len == 0) |
460 | 0 | return 0; |
461 | | |
462 | 1 | WINPR_ASSERT(str); |
463 | | |
464 | 1 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
465 | 0 | { |
466 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
467 | 0 | return -1; |
468 | 0 | } |
469 | 1 | if (ilen < len) |
470 | 1 | { |
471 | 1 | isNullTerminated = TRUE; |
472 | 1 | ilen++; |
473 | 1 | } |
474 | | |
475 | 1 | const int iwlen = (int)wlen; |
476 | 1 | const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)ilen, wstr, iwlen); |
477 | 1 | if ((rc <= 0) || ((wlen > 0) && (rc > iwlen))) |
478 | 0 | return -1; |
479 | 1 | if (!isNullTerminated) |
480 | 0 | { |
481 | 0 | if (wstr && (rc < iwlen)) |
482 | 0 | wstr[rc] = '\0'; |
483 | 0 | return rc; |
484 | 0 | } |
485 | 1 | else if (rc == iwlen) |
486 | 0 | { |
487 | 0 | if (wstr && (wstr[rc - 1] != '\0')) |
488 | 0 | return rc; |
489 | 0 | } |
490 | 1 | return rc - 1; |
491 | 1 | } |
492 | | |
493 | | SSIZE_T ConvertMszUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen) |
494 | 0 | { |
495 | 0 | if (len == 0) |
496 | 0 | return 0; |
497 | | |
498 | 0 | WINPR_ASSERT(str); |
499 | |
|
500 | 0 | if ((len > INT32_MAX) || (wlen > INT32_MAX)) |
501 | 0 | { |
502 | 0 | SetLastError(ERROR_INVALID_PARAMETER); |
503 | 0 | return -1; |
504 | 0 | } |
505 | | |
506 | 0 | const int iwlen = (int)wlen; |
507 | 0 | const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)len, wstr, iwlen); |
508 | 0 | if ((rc <= 0) || ((wlen > 0) && (rc > iwlen))) |
509 | 0 | return -1; |
510 | | |
511 | 0 | return rc; |
512 | 0 | } |
513 | | |
514 | | char* ConvertWCharToUtf8Alloc(const WCHAR* wstr, size_t* pUtfCharLength) |
515 | 0 | { |
516 | 0 | char* tmp = nullptr; |
517 | 0 | const SSIZE_T rc = ConvertWCharToUtf8(wstr, nullptr, 0); |
518 | 0 | if (pUtfCharLength) |
519 | 0 | *pUtfCharLength = 0; |
520 | 0 | if (rc < 0) |
521 | 0 | return nullptr; |
522 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
523 | 0 | if (!tmp) |
524 | 0 | return nullptr; |
525 | 0 | const SSIZE_T rc2 = ConvertWCharToUtf8(wstr, tmp, (size_t)rc + 1ull); |
526 | 0 | if (rc2 < 0) |
527 | 0 | { |
528 | 0 | free(tmp); |
529 | 0 | return nullptr; |
530 | 0 | } |
531 | 0 | WINPR_ASSERT(rc == rc2); |
532 | 0 | if (pUtfCharLength) |
533 | 0 | *pUtfCharLength = (size_t)rc2; |
534 | 0 | return tmp; |
535 | 0 | } |
536 | | |
537 | | char* ConvertWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength) |
538 | 0 | { |
539 | 0 | char* tmp = nullptr; |
540 | 0 | const SSIZE_T rc = ConvertWCharNToUtf8(wstr, wlen, nullptr, 0); |
541 | |
|
542 | 0 | if (pUtfCharLength) |
543 | 0 | *pUtfCharLength = 0; |
544 | 0 | if (rc < 0) |
545 | 0 | return nullptr; |
546 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
547 | 0 | if (!tmp) |
548 | 0 | return nullptr; |
549 | 0 | const SSIZE_T rc2 = ConvertWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull); |
550 | 0 | if (rc2 < 0) |
551 | 0 | { |
552 | 0 | free(tmp); |
553 | 0 | return nullptr; |
554 | 0 | } |
555 | 0 | WINPR_ASSERT(rc == rc2); |
556 | 0 | if (pUtfCharLength) |
557 | 0 | *pUtfCharLength = (size_t)rc2; |
558 | 0 | return tmp; |
559 | 0 | } |
560 | | |
561 | | char* ConvertMszWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength) |
562 | 0 | { |
563 | 0 | char* tmp = nullptr; |
564 | 0 | const SSIZE_T rc = ConvertMszWCharNToUtf8(wstr, wlen, nullptr, 0); |
565 | |
|
566 | 0 | if (pUtfCharLength) |
567 | 0 | *pUtfCharLength = 0; |
568 | 0 | if (rc < 0) |
569 | 0 | return nullptr; |
570 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(char)); |
571 | 0 | if (!tmp) |
572 | 0 | return nullptr; |
573 | 0 | const SSIZE_T rc2 = ConvertMszWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull); |
574 | 0 | if (rc2 < 0) |
575 | 0 | { |
576 | 0 | free(tmp); |
577 | 0 | return nullptr; |
578 | 0 | } |
579 | 0 | WINPR_ASSERT(rc == rc2); |
580 | 0 | if (pUtfCharLength) |
581 | 0 | *pUtfCharLength = (size_t)rc2; |
582 | 0 | return tmp; |
583 | 0 | } |
584 | | |
585 | | WCHAR* ConvertUtf8ToWCharAlloc(const char* str, size_t* pSize) |
586 | 0 | { |
587 | 0 | WCHAR* tmp = nullptr; |
588 | 0 | const SSIZE_T rc = ConvertUtf8ToWChar(str, nullptr, 0); |
589 | 0 | if (pSize) |
590 | 0 | *pSize = 0; |
591 | 0 | if (rc < 0) |
592 | 0 | return nullptr; |
593 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
594 | 0 | if (!tmp) |
595 | 0 | return nullptr; |
596 | 0 | const SSIZE_T rc2 = ConvertUtf8ToWChar(str, tmp, (size_t)rc + 1ull); |
597 | 0 | if (rc2 < 0) |
598 | 0 | { |
599 | 0 | free(tmp); |
600 | 0 | return nullptr; |
601 | 0 | } |
602 | 0 | WINPR_ASSERT(rc == rc2); |
603 | 0 | if (pSize) |
604 | 0 | *pSize = (size_t)rc2; |
605 | 0 | return tmp; |
606 | 0 | } |
607 | | |
608 | | WCHAR* ConvertUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize) |
609 | 0 | { |
610 | 0 | WCHAR* tmp = nullptr; |
611 | 0 | const SSIZE_T rc = ConvertUtf8NToWChar(str, len, nullptr, 0); |
612 | 0 | if (pSize) |
613 | 0 | *pSize = 0; |
614 | 0 | if (rc < 0) |
615 | 0 | return nullptr; |
616 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
617 | 0 | if (!tmp) |
618 | 0 | return nullptr; |
619 | 0 | const SSIZE_T rc2 = ConvertUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull); |
620 | 0 | if (rc2 < 0) |
621 | 0 | { |
622 | 0 | free(tmp); |
623 | 0 | return nullptr; |
624 | 0 | } |
625 | 0 | WINPR_ASSERT(rc == rc2); |
626 | 0 | if (pSize) |
627 | 0 | *pSize = (size_t)rc2; |
628 | 0 | return tmp; |
629 | 0 | } |
630 | | |
631 | | WCHAR* ConvertMszUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize) |
632 | 0 | { |
633 | 0 | WCHAR* tmp = nullptr; |
634 | 0 | const SSIZE_T rc = ConvertMszUtf8NToWChar(str, len, nullptr, 0); |
635 | 0 | if (pSize) |
636 | 0 | *pSize = 0; |
637 | 0 | if (rc < 0) |
638 | 0 | return nullptr; |
639 | 0 | tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR)); |
640 | 0 | if (!tmp) |
641 | 0 | return nullptr; |
642 | 0 | const SSIZE_T rc2 = ConvertMszUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull); |
643 | 0 | if (rc2 < 0) |
644 | 0 | { |
645 | 0 | free(tmp); |
646 | 0 | return nullptr; |
647 | 0 | } |
648 | 0 | WINPR_ASSERT(rc == rc2); |
649 | 0 | if (pSize) |
650 | 0 | *pSize = (size_t)rc2; |
651 | 0 | return tmp; |
652 | 0 | } |