FreeRDP
Loading...
Searching...
No Matches
unicode.c
1
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
108
149
150#if !defined(WITH_WINPR_DEPRECATED)
151static
152#endif
153 int
154 MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte,
155 LPWSTR lpWideCharStr, int cchWideChar)
156{
157 return int_MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr,
158 cchWideChar);
159}
160
200
201#if !defined(WITH_WINPR_DEPRECATED)
202static
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{
209 return int_WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr,
210 cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
211}
212
213#endif
214
227#if defined(WITH_WINPR_DEPRECATED)
228int 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
300#if defined(WITH_WINPR_DEPRECATED)
301int 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
361
362const WCHAR* ByteSwapUnicode(WCHAR* wstr, size_t length)
363{
364 WINPR_ASSERT(wstr || (length == 0));
365
366 for (size_t x = 0; x < length; x++)
367 wstr[x] = _byteswap_ushort(wstr[x]);
368 return wstr;
369}
370
371SSIZE_T ConvertWCharToUtf8(const WCHAR* wstr, char* str, size_t len)
372{
373 if (!wstr)
374 {
375 if (str && len)
376 str[0] = 0;
377 return 0;
378 }
379
380 const size_t wlen = _wcslen(wstr);
381 if (wlen == SIZE_MAX)
382 return -1;
383 return ConvertWCharNToUtf8(wstr, wlen + 1, str, len);
384}
385
386SSIZE_T ConvertWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len)
387{
388 BOOL isNullTerminated = FALSE;
389 if (wlen == 0)
390 return 0;
391
392 WINPR_ASSERT(wstr);
393 size_t iwlen = _wcsnlen(wstr, wlen);
394
395 if ((len > INT32_MAX) || (wlen > INT32_MAX))
396 {
397 SetLastError(ERROR_INVALID_PARAMETER);
398 return -1;
399 }
400
401 if (iwlen < wlen)
402 {
403 isNullTerminated = TRUE;
404 iwlen++;
405 }
406 WINPR_PRAGMA_DIAG_PUSH
407 WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL
408 const int rc =
409 WideCharToMultiByte(CP_UTF8, 0, wstr, (int)iwlen, str, (int)len, nullptr, nullptr);
410 WINPR_PRAGMA_DIAG_POP
411 if ((rc <= 0) || ((len > 0) && ((size_t)rc > len)))
412 return -1;
413 else if (!isNullTerminated)
414 {
415 if (str && ((size_t)rc < len))
416 str[rc] = '\0';
417 return rc;
418 }
419 else if ((size_t)rc == len)
420 {
421 if (str && (str[rc - 1] != '\0'))
422 return rc;
423 }
424 return rc - 1;
425}
426
427SSIZE_T ConvertMszWCharNToUtf8(const WCHAR* wstr, size_t wlen, char* str, size_t len)
428{
429 if (wlen == 0)
430 return 0;
431
432 WINPR_ASSERT(wstr);
433
434 if ((len > INT32_MAX) || (wlen > INT32_MAX))
435 {
436 SetLastError(ERROR_INVALID_PARAMETER);
437 return -1;
438 }
439
440 const int iwlen = (int)len;
441 WINPR_PRAGMA_DIAG_PUSH
442 WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL
443 const int rc = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)wlen, str, iwlen, nullptr, nullptr);
444 WINPR_PRAGMA_DIAG_POP
445 if ((rc <= 0) || ((len > 0) && (rc > iwlen)))
446 return -1;
447
448 return rc;
449}
450
451SSIZE_T ConvertUtf8ToWChar(const char* str, WCHAR* wstr, size_t wlen)
452{
453 if (!str)
454 {
455 if (wstr && wlen)
456 wstr[0] = 0;
457 return 0;
458 }
459
460 const size_t len = strlen(str);
461 if (len == SIZE_MAX)
462 return -1;
463 return ConvertUtf8NToWChar(str, len + 1, wstr, wlen);
464}
465
466SSIZE_T ConvertUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen)
467{
468 size_t ilen = strnlen(str, len);
469 BOOL isNullTerminated = FALSE;
470 if (len == 0)
471 return 0;
472
473 WINPR_ASSERT(str);
474
475 if ((len > INT32_MAX) || (wlen > INT32_MAX))
476 {
477 SetLastError(ERROR_INVALID_PARAMETER);
478 return -1;
479 }
480 if (ilen < len)
481 {
482 isNullTerminated = TRUE;
483 ilen++;
484 }
485
486 const int iwlen = (int)wlen;
487 WINPR_PRAGMA_DIAG_PUSH
488 WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL
489 const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)ilen, wstr, iwlen);
490 WINPR_PRAGMA_DIAG_POP
491 if ((rc <= 0) || ((wlen > 0) && (rc > iwlen)))
492 return -1;
493 if (!isNullTerminated)
494 {
495 if (wstr && (rc < iwlen))
496 wstr[rc] = '\0';
497 return rc;
498 }
499 else if (rc == iwlen)
500 {
501 if (wstr && (wstr[rc - 1] != '\0'))
502 return rc;
503 }
504 return rc - 1;
505}
506
507SSIZE_T ConvertMszUtf8NToWChar(const char* str, size_t len, WCHAR* wstr, size_t wlen)
508{
509 if (len == 0)
510 return 0;
511
512 WINPR_ASSERT(str);
513
514 if ((len > INT32_MAX) || (wlen > INT32_MAX))
515 {
516 SetLastError(ERROR_INVALID_PARAMETER);
517 return -1;
518 }
519
520 const int iwlen = (int)wlen;
521 WINPR_PRAGMA_DIAG_PUSH
522 WINPR_PRAGMA_DIAG_IGNORED_DEPRECATED_DECL
523 const int rc = MultiByteToWideChar(CP_UTF8, 0, str, (int)len, wstr, iwlen);
524 WINPR_PRAGMA_DIAG_POP
525 if ((rc <= 0) || ((wlen > 0) && (rc > iwlen)))
526 return -1;
527
528 return rc;
529}
530
531char* ConvertWCharToUtf8Alloc(const WCHAR* wstr, size_t* pUtfCharLength)
532{
533 char* tmp = nullptr;
534 const SSIZE_T rc = ConvertWCharToUtf8(wstr, nullptr, 0);
535 if (pUtfCharLength)
536 *pUtfCharLength = 0;
537 if (rc < 0)
538 return nullptr;
539 tmp = calloc((size_t)rc + 1ull, sizeof(char));
540 if (!tmp)
541 return nullptr;
542 const SSIZE_T rc2 = ConvertWCharToUtf8(wstr, tmp, (size_t)rc + 1ull);
543 if (rc2 < 0)
544 {
545 free(tmp);
546 return nullptr;
547 }
548 WINPR_ASSERT(rc == rc2);
549 if (pUtfCharLength)
550 *pUtfCharLength = (size_t)rc2;
551 return tmp;
552}
553
554char* ConvertWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength)
555{
556 char* tmp = nullptr;
557 const SSIZE_T rc = ConvertWCharNToUtf8(wstr, wlen, nullptr, 0);
558
559 if (pUtfCharLength)
560 *pUtfCharLength = 0;
561 if (rc < 0)
562 return nullptr;
563 tmp = calloc((size_t)rc + 1ull, sizeof(char));
564 if (!tmp)
565 return nullptr;
566 const SSIZE_T rc2 = ConvertWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull);
567 if (rc2 < 0)
568 {
569 free(tmp);
570 return nullptr;
571 }
572 WINPR_ASSERT(rc == rc2);
573 if (pUtfCharLength)
574 *pUtfCharLength = (size_t)rc2;
575 return tmp;
576}
577
578char* ConvertMszWCharNToUtf8Alloc(const WCHAR* wstr, size_t wlen, size_t* pUtfCharLength)
579{
580 char* tmp = nullptr;
581 const SSIZE_T rc = ConvertMszWCharNToUtf8(wstr, wlen, nullptr, 0);
582
583 if (pUtfCharLength)
584 *pUtfCharLength = 0;
585 if (rc < 0)
586 return nullptr;
587 tmp = calloc((size_t)rc + 1ull, sizeof(char));
588 if (!tmp)
589 return nullptr;
590 const SSIZE_T rc2 = ConvertMszWCharNToUtf8(wstr, wlen, tmp, (size_t)rc + 1ull);
591 if (rc2 < 0)
592 {
593 free(tmp);
594 return nullptr;
595 }
596 WINPR_ASSERT(rc == rc2);
597 if (pUtfCharLength)
598 *pUtfCharLength = (size_t)rc2;
599 return tmp;
600}
601
602WCHAR* ConvertUtf8ToWCharAlloc(const char* str, size_t* pSize)
603{
604 WCHAR* tmp = nullptr;
605 const SSIZE_T rc = ConvertUtf8ToWChar(str, nullptr, 0);
606 if (pSize)
607 *pSize = 0;
608 if (rc < 0)
609 return nullptr;
610 tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
611 if (!tmp)
612 return nullptr;
613 const SSIZE_T rc2 = ConvertUtf8ToWChar(str, tmp, (size_t)rc + 1ull);
614 if (rc2 < 0)
615 {
616 free(tmp);
617 return nullptr;
618 }
619 WINPR_ASSERT(rc == rc2);
620 if (pSize)
621 *pSize = (size_t)rc2;
622 return tmp;
623}
624
625WCHAR* ConvertUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize)
626{
627 WCHAR* tmp = nullptr;
628 const SSIZE_T rc = ConvertUtf8NToWChar(str, len, nullptr, 0);
629 if (pSize)
630 *pSize = 0;
631 if (rc < 0)
632 return nullptr;
633 tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
634 if (!tmp)
635 return nullptr;
636 const SSIZE_T rc2 = ConvertUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull);
637 if (rc2 < 0)
638 {
639 free(tmp);
640 return nullptr;
641 }
642 WINPR_ASSERT(rc == rc2);
643 if (pSize)
644 *pSize = (size_t)rc2;
645 return tmp;
646}
647
648WCHAR* ConvertMszUtf8NToWCharAlloc(const char* str, size_t len, size_t* pSize)
649{
650 WCHAR* tmp = nullptr;
651 const SSIZE_T rc = ConvertMszUtf8NToWChar(str, len, nullptr, 0);
652 if (pSize)
653 *pSize = 0;
654 if (rc < 0)
655 return nullptr;
656 tmp = calloc((size_t)rc + 1ull, sizeof(WCHAR));
657 if (!tmp)
658 return nullptr;
659 const SSIZE_T rc2 = ConvertMszUtf8NToWChar(str, len, tmp, (size_t)rc + 1ull);
660 if (rc2 < 0)
661 {
662 free(tmp);
663 return nullptr;
664 }
665 WINPR_ASSERT(rc == rc2);
666 if (pSize)
667 *pSize = (size_t)rc2;
668 return tmp;
669}
670
671WINPR_ATTR_NODISCARD
672static INT8 nnibble(char c)
673{
674 if ((c >= '0') && (c <= '9'))
675 return (INT8)(c - '0');
676 if ((c >= 'a') && (c <= 'f'))
677 return (INT8)((c - 'a') + 0xa);
678 if ((c >= 'A') && (c <= 'F'))
679 return (INT8)((c - 'A') + 0xa);
680 return -1;
681}
682
683WINPR_ATTR_NODISCARD
684static INT32 escapeToNumber(char* str, char** next)
685{
686 if (strncmp(str, "\\u", 2) != 0)
687 return -1;
688
689 size_t offset = 2;
690 INT32 code = 0;
691 for (size_t x = 0; x < 4; x++)
692 {
693 const INT8 val = nnibble(str[offset++]);
694 if (val < 0)
695 return -1;
696
697 code <<= 4;
698 code |= val;
699 }
700 *next = &str[offset];
701 return code;
702}
703
704SSIZE_T winpr_utfEscapedStringToUtf8(char* str, size_t len)
705{
706 /* A decoded escape is never longer than the escape itself, so decode in place with separate
707 * read and write positions. Every byte is moved at most once, otherwise a string made of
708 * escapes costs time quadratic in its length. */
709 char* const end = &str[strnlen(str, len)];
710 char* cur = str;
711 char* dst = str;
712 while (cur < end)
713 {
714 const char* esc = winpr_strnstr(cur, "\\u", (size_t)(end - cur));
715 const size_t plain = (size_t)((esc ? esc : end) - cur);
716 memmove(dst, cur, plain);
717 dst += plain;
718 cur += plain;
719 if (!esc)
720 break;
721
722 char* next = nullptr;
723 const INT32 code = escapeToNumber(cur, &next);
724 if (code < 0)
725 {
726 /* Not a \uXXXX escape (e.g. "C:\users"): keep it as literal text */
727 memmove(dst, cur, 2);
728 dst += 2;
729 cur += 2;
730 continue;
731 }
732
733 WCHAR wc[2] = WINPR_C_ARRAY_INIT;
734 winpr_Data_Write_UINT16(&wc[0], (WCHAR)code);
735
736 // Check for high surrogate
737 if ((code & 0xd800) == 0xd800)
738 {
739 const INT32 code2 = escapeToNumber(next, &next);
740 if (code2 < 0)
741 return -1;
742
743 // low surrogate check. must match
744 if ((code2 & 0xdc00) != 0xdc00)
745 return -1;
746
747 winpr_Data_Write_UINT16(&wc[1], (WCHAR)code2);
748 }
749
750 char utf8[8] = WINPR_C_ARRAY_INIT;
751 const SSIZE_T rc = ConvertWCharNToUtf8(wc, ARRAYSIZE(wc), utf8, ARRAYSIZE(utf8));
752 if (rc < 0)
753 return -1;
754
755 /* Continue after the escape, so the decoded bytes are never decoded again */
756 memcpy(dst, utf8, (size_t)rc);
757 dst += rc;
758 cur = next;
759 }
760
761 if (dst < end)
762 *dst = '\0';
763 return (SSIZE_T)(dst - str);
764}
765
766char* winpr_utf8ToUtfEscapedString(char* str, size_t len, size_t* pDstLen)
767{
768 WINPR_ASSERT(str || (len == 0));
769
770 if (pDstLen)
771 *pDstLen = 0;
772
773 if (len >= SIZE_MAX / 12)
774 return nullptr;
775
776 char* escaped = nullptr;
777 size_t offset = 0;
778 size_t wlen = 0;
779 WCHAR* wstr = ConvertUtf8NToWCharAlloc(str, len, &wlen);
780 if (!wstr || (wlen == 0))
781 goto fail;
782
783 const size_t esclen = len * 12ull;
784 escaped = calloc(len + 1, 12);
785 if (!escaped)
786 goto fail;
787
788 for (size_t x = 0; x < wlen; x++)
789 {
790 const WCHAR wc = winpr_Data_Get_UINT16(&wstr[x]);
791 if ((wc < 0x80) && (wc != '\\'))
792 escaped[offset++] = (char)wc;
793 else
794 {
795 const int rc = snprintf(&escaped[offset], esclen - offset, "\\u%04x", wc);
796 if (rc < 0)
797 break;
798 offset += (size_t)rc;
799 }
800 }
801 escaped[offset] = '\0';
802
803fail:
804 if (pDstLen)
805 *pDstLen = offset;
806
807 winpr_znfree(wstr, wlen * sizeof(WCHAR));
808 return escaped;
809}