Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/winpr/libwinpr/crt/unicode.c
Line
Count
Source (jump to first uncovered line)
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, NULL, 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
0
{
156
0
  return int_MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr,
157
0
                                 cchWideChar);
158
0
}
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, NULL, 0, NULL, NULL);
175
 *
176
 * A value of -1 for cbMultiByte indicates that the input string is null-terminated,
177
 * and the null terminator *will* be processed. The size returned by WideCharToMultiByte
178
 * will therefore include the null terminator. Equivalent behavior can be obtained by
179
 * computing the length in bytes of the input buffer, including the null terminator:
180
 *
181
 * cchWideChar = _wcslen((WCHAR*) lpWideCharStr) + 1;
182
 *
183
 * An output buffer of the proper size can then be allocated:
184
 * lpMultiByteStr = (LPSTR) malloc(cbMultiByte);
185
 *
186
 * Since cbMultiByte is an output size in bytes, it is the same as the buffer size
187
 *
188
 * Finally, perform the conversion:
189
 *
190
 * cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) lpWideCharStr, -1, lpMultiByteStr,
191
 * cbMultiByte, NULL, NULL);
192
 *
193
 * The value returned by WideCharToMultiByte corresponds to the number of bytes written
194
 * to the output buffer, and should match the value obtained on the first call to
195
 * WideCharToMultiByte.
196
 *
197
 */
198
199
#if !defined(WITH_WINPR_DEPRECATED)
200
static
201
#endif
202
    int
203
    WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
204
                        LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
205
                        LPBOOL lpUsedDefaultChar)
206
0
{
207
0
  return int_WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr,
208
0
                                 cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
209
0
}
210
211
#endif
212
213
/**
214
 * ConvertToUnicode is a convenience wrapper for MultiByteToWideChar:
215
 *
216
 * If the lpWideCharStr parameter for the converted string points to NULL
217
 * or if the cchWideChar parameter is set to 0 this function will automatically
218
 * allocate the required memory which is guaranteed to be null-terminated
219
 * after the conversion, even if the source c string isn't.
220
 *
221
 * If the cbMultiByte parameter is set to -1 the passed lpMultiByteStr must
222
 * be null-terminated and the required length for the converted string will be
223
 * calculated accordingly.
224
 */
225
#if defined(WITH_WINPR_DEPRECATED)
226
int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
227
                     LPWSTR* lpWideCharStr, int cchWideChar)
228
{
229
  int status = 0;
230
  BOOL allocate = FALSE;
231
232
  if (!lpMultiByteStr)
233
    return 0;
234
235
  if (!lpWideCharStr)
236
    return 0;
237
238
  if (cbMultiByte == -1)
239
  {
240
    size_t len = strnlen(lpMultiByteStr, INT_MAX);
241
    if (len >= INT_MAX)
242
      return 0;
243
    cbMultiByte = (int)(len + 1);
244
  }
245
246
  if (cchWideChar == 0)
247
  {
248
    cchWideChar = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
249
    allocate = TRUE;
250
  }
251
  else if (!(*lpWideCharStr))
252
    allocate = TRUE;
253
254
  if (cchWideChar < 1)
255
    return 0;
256
257
  if (allocate)
258
  {
259
    *lpWideCharStr = (LPWSTR)calloc(cchWideChar + 1, sizeof(WCHAR));
260
261
    if (!(*lpWideCharStr))
262
    {
263
      // SetLastError(ERROR_INSUFFICIENT_BUFFER);
264
      return 0;
265
    }
266
  }
267
268
  status = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, *lpWideCharStr,
269
                               cchWideChar);
270
271
  if (status != cchWideChar)
272
  {
273
    if (allocate)
274
    {
275
      free(*lpWideCharStr);
276
      *lpWideCharStr = NULL;
277
      status = 0;
278
    }
279
  }
280
281
  return status;
282
}
283
#endif
284
285
/**
286
 * ConvertFromUnicode is a convenience wrapper for WideCharToMultiByte:
287
 *
288
 * If the lpMultiByteStr parameter for the converted string points to NULL
289
 * or if the cbMultiByte parameter is set to 0 this function will automatically
290
 * allocate the required memory which is guaranteed to be null-terminated
291
 * after the conversion, even if the source unicode string isn't.
292
 *
293
 * If the cchWideChar parameter is set to -1 the passed lpWideCharStr must
294
 * be null-terminated and the required length for the converted string will be
295
 * calculated accordingly.
296
 */
297
#if defined(WITH_WINPR_DEPRECATED)
298
int ConvertFromUnicode(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar,
299
                       LPSTR* lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
300
                       LPBOOL lpUsedDefaultChar)
301
{
302
  int status = 0;
303
  BOOL allocate = FALSE;
304
305
  if (!lpWideCharStr)
306
    return 0;
307
308
  if (!lpMultiByteStr)
309
    return 0;
310
311
  if (cchWideChar == -1)
312
    cchWideChar = (int)(_wcslen(lpWideCharStr) + 1);
313
314
  if (cbMultiByte == 0)
315
  {
316
    cbMultiByte =
317
        WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, NULL, NULL);
318
    allocate = TRUE;
319
  }
320
  else if (!(*lpMultiByteStr))
321
    allocate = TRUE;
322
323
  if (cbMultiByte < 1)
324
    return 0;
325
326
  if (allocate)
327
  {
328
    *lpMultiByteStr = (LPSTR)calloc(1, cbMultiByte + 1);
329
330
    if (!(*lpMultiByteStr))
331
    {
332
      // SetLastError(ERROR_INSUFFICIENT_BUFFER);
333
      return 0;
334
    }
335
  }
336
337
  status = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, *lpMultiByteStr,
338
                               cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
339
340
  if ((status != cbMultiByte) && allocate)
341
  {
342
    status = 0;
343
  }
344
345
  if ((status <= 0) && allocate)
346
  {
347
    free(*lpMultiByteStr);
348
    *lpMultiByteStr = NULL;
349
  }
350
351
  return status;
352
}
353
#endif
354
355
/**
356
 * Swap Unicode byte order (UTF16LE <-> UTF16BE)
357
 */
358
359
const WCHAR* ByteSwapUnicode(WCHAR* wstr, size_t length)
360
0
{
361
0
  WINPR_ASSERT(wstr || (length == 0));
362
363
0
  for (size_t x = 0; x < length; x++)
364
0
    wstr[x] = _byteswap_ushort(wstr[x]);
365
0
  return wstr;
366
0
}
367
368
SSIZE_T ConvertWCharToUtf8(const WCHAR* wstr, char* str, size_t len)
369
0
{
370
0
  if (!wstr)
371
0
  {
372
0
    if (str && len)
373
0
      str[0] = 0;
374
0
    return 0;
375
0
  }
376
377
0
  const size_t wlen = _wcslen(wstr);
378
0
  return ConvertWCharNToUtf8(wstr, wlen + 1, str, len);
379
0
}
380
381
SSIZE_T ConvertWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len)
382
0
{
383
0
  BOOL isNullTerminated = FALSE;
384
0
  if (wlen == 0)
385
0
    return 0;
386
387
0
  WINPR_ASSERT(wstr);
388
0
  size_t iwlen = _wcsnlen(wstr, wlen);
389
390
0
  if ((len > INT32_MAX) || (wlen > INT32_MAX))
391
0
  {
392
0
    SetLastError(ERROR_INVALID_PARAMETER);
393
0
    return -1;
394
0
  }
395
396
0
  if (iwlen < wlen)
397
0
  {
398
0
    isNullTerminated = TRUE;
399
0
    iwlen++;
400
0
  }
401
0
  const int rc = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)iwlen, str, (int)len, NULL, NULL);
402
0
  if ((rc <= 0) || ((len > 0) && ((size_t)rc > len)))
403
0
    return -1;
404
0
  else if (!isNullTerminated)
405
0
  {
406
0
    if (str && ((size_t)rc < len))
407
0
      str[rc] = '\0';
408
0
    return rc;
409
0
  }
410
0
  else if ((size_t)rc == len)
411
0
  {
412
0
    if (str && (str[rc - 1] != '\0'))
413
0
      return rc;
414
0
  }
415
0
  return rc - 1;
416
0
}
417
418
SSIZE_T ConvertMszWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len)
419
0
{
420
0
  if (wlen == 0)
421
0
    return 0;
422
423
0
  WINPR_ASSERT(wstr);
424
425
0
  if ((len > INT32_MAX) || (wlen > INT32_MAX))
426
0
  {
427
0
    SetLastError(ERROR_INVALID_PARAMETER);
428
0
    return -1;
429
0
  }
430
431
0
  const int iwlen = (int)len;
432
0
  const int rc = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)wlen, str, iwlen, NULL, NULL);
433
0
  if ((rc <= 0) || ((len > 0) && (rc > iwlen)))
434
0
    return -1;
435
436
0
  return rc;
437
0
}
438
439
SSIZE_T ConvertUtf8ToWChar(const char* str, WCHAR* wstr, size_t wlen)
440
0
{
441
0
  if (!str)
442
0
  {
443
0
    if (wstr && wlen)
444
0
      wstr[0] = 0;
445
0
    return 0;
446
0
  }
447
448
0
  const size_t len = strlen(str);
449
0
  return ConvertUtf8NToWChar(str, len + 1, wstr, wlen);
450
0
}
451
452
SSIZE_T ConvertUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen)
453
0
{
454
0
  size_t ilen = strnlen(str, len);
455
0
  BOOL isNullTerminated = FALSE;
456
0
  if (len == 0)
457
0
    return 0;
458
459
0
  WINPR_ASSERT(str);
460
461
0
  if ((len > INT32_MAX) || (wlen > INT32_MAX))
462
0
  {
463
0
    SetLastError(ERROR_INVALID_PARAMETER);
464
0
    return -1;
465
0
  }
466
0
  if (ilen < len)
467
0
  {
468
0
    isNullTerminated = TRUE;
469
0
    ilen++;
470
0
  }
471
472
0
  const int iwlen = (int)wlen;
473
0
  const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)ilen, wstr, iwlen);
474
0
  if ((rc <= 0) || ((wlen > 0) && (rc > iwlen)))
475
0
    return -1;
476
0
  if (!isNullTerminated)
477
0
  {
478
0
    if (wstr && (rc < iwlen))
479
0
      wstr[rc] = '\0';
480
0
    return rc;
481
0
  }
482
0
  else if (rc == iwlen)
483
0
  {
484
0
    if (wstr && (wstr[rc - 1] != '\0'))
485
0
      return rc;
486
0
  }
487
0
  return rc - 1;
488
0
}
489
490
SSIZE_T ConvertMszUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen)
491
0
{
492
0
  if (len == 0)
493
0
    return 0;
494
495
0
  WINPR_ASSERT(str);
496
497
0
  if ((len > INT32_MAX) || (wlen > INT32_MAX))
498
0
  {
499
0
    SetLastError(ERROR_INVALID_PARAMETER);
500
0
    return -1;
501
0
  }
502
503
0
  const int iwlen = (int)wlen;
504
0
  const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)len, wstr, iwlen);
505
0
  if ((rc <= 0) || ((wlen > 0) && (rc > iwlen)))
506
0
    return -1;
507
508
0
  return rc;
509
0
}
510
511
char* ConvertWCharToUtf8Alloc(const WCHAR* wstr, size_t* pUtfCharLength)
512
0
{
513
0
  char* tmp = NULL;
514
0
  const SSIZE_T rc = ConvertWCharToUtf8(wstr, NULL, 0);
515
0
  if (pUtfCharLength)
516
0
    *pUtfCharLength = 0;
517
0
  if (rc < 0)
518
0
    return NULL;
519
0
  tmp = calloc((size_t)rc + 1ull, sizeof(char));
520
0
  if (!tmp)
521
0
    return NULL;
522
0
  const SSIZE_T rc2 = ConvertWCharToUtf8(wstr, tmp, (size_t)rc + 1ull);
523
0
  if (rc2 < 0)
524
0
  {
525
0
    free(tmp);
526
0
    return NULL;
527
0
  }
528
0
  WINPR_ASSERT(rc == rc2);
529
0
  if (pUtfCharLength)
530
0
    *pUtfCharLength = (size_t)rc2;
531
0
  return tmp;
532
0
}
533
534
char* ConvertWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength)
535
0
{
536
0
  char* tmp = NULL;
537
0
  const SSIZE_T rc = ConvertWCharNToUtf8(wstr, wlen, NULL, 0);
538
539
0
  if (pUtfCharLength)
540
0
    *pUtfCharLength = 0;
541
0
  if (rc < 0)
542
0
    return NULL;
543
0
  tmp = calloc((size_t)rc + 1ull, sizeof(char));
544
0
  if (!tmp)
545
0
    return NULL;
546
0
  const SSIZE_T rc2 = ConvertWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull);
547
0
  if (rc2 < 0)
548
0
  {
549
0
    free(tmp);
550
0
    return NULL;
551
0
  }
552
0
  WINPR_ASSERT(rc == rc2);
553
0
  if (pUtfCharLength)
554
0
    *pUtfCharLength = (size_t)rc2;
555
0
  return tmp;
556
0
}
557
558
char* ConvertMszWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength)
559
0
{
560
0
  char* tmp = NULL;
561
0
  const SSIZE_T rc = ConvertMszWCharNToUtf8(wstr, wlen, NULL, 0);
562
563
0
  if (pUtfCharLength)
564
0
    *pUtfCharLength = 0;
565
0
  if (rc < 0)
566
0
    return NULL;
567
0
  tmp = calloc((size_t)rc + 1ull, sizeof(char));
568
0
  if (!tmp)
569
0
    return NULL;
570
0
  const SSIZE_T rc2 = ConvertMszWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull);
571
0
  if (rc2 < 0)
572
0
  {
573
0
    free(tmp);
574
0
    return NULL;
575
0
  }
576
0
  WINPR_ASSERT(rc == rc2);
577
0
  if (pUtfCharLength)
578
0
    *pUtfCharLength = (size_t)rc2;
579
0
  return tmp;
580
0
}
581
582
WCHAR* ConvertUtf8ToWCharAlloc(const char* str, size_t* pSize)
583
0
{
584
0
  WCHAR* tmp = NULL;
585
0
  const SSIZE_T rc = ConvertUtf8ToWChar(str, NULL, 0);
586
0
  if (pSize)
587
0
    *pSize = 0;
588
0
  if (rc < 0)
589
0
    return NULL;
590
0
  tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
591
0
  if (!tmp)
592
0
    return NULL;
593
0
  const SSIZE_T rc2 = ConvertUtf8ToWChar(str, tmp, (size_t)rc + 1ull);
594
0
  if (rc2 < 0)
595
0
  {
596
0
    free(tmp);
597
0
    return NULL;
598
0
  }
599
0
  WINPR_ASSERT(rc == rc2);
600
0
  if (pSize)
601
0
    *pSize = (size_t)rc2;
602
0
  return tmp;
603
0
}
604
605
WCHAR* ConvertUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize)
606
0
{
607
0
  WCHAR* tmp = NULL;
608
0
  const SSIZE_T rc = ConvertUtf8NToWChar(str, len, NULL, 0);
609
0
  if (pSize)
610
0
    *pSize = 0;
611
0
  if (rc < 0)
612
0
    return NULL;
613
0
  tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
614
0
  if (!tmp)
615
0
    return NULL;
616
0
  const SSIZE_T rc2 = ConvertUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull);
617
0
  if (rc2 < 0)
618
0
  {
619
0
    free(tmp);
620
0
    return NULL;
621
0
  }
622
0
  WINPR_ASSERT(rc == rc2);
623
0
  if (pSize)
624
0
    *pSize = (size_t)rc2;
625
0
  return tmp;
626
0
}
627
628
WCHAR* ConvertMszUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize)
629
0
{
630
0
  WCHAR* tmp = NULL;
631
0
  const SSIZE_T rc = ConvertMszUtf8NToWChar(str, len, NULL, 0);
632
0
  if (pSize)
633
0
    *pSize = 0;
634
0
  if (rc < 0)
635
0
    return NULL;
636
0
  tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
637
0
  if (!tmp)
638
0
    return NULL;
639
0
  const SSIZE_T rc2 = ConvertMszUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull);
640
0
  if (rc2 < 0)
641
0
  {
642
0
    free(tmp);
643
0
    return NULL;
644
0
  }
645
0
  WINPR_ASSERT(rc == rc2);
646
0
  if (pSize)
647
0
    *pSize = (size_t)rc2;
648
0
  return tmp;
649
0
}