Changeset 21740 in vbox for trunk/src/VBox/Runtime/common/string
- Timestamp:
- Jul 21, 2009 12:06:38 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 50317
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/utf-16.cpp
r21728 r21740 342 342 if (wc < 0x80) 343 343 { 344 if ( cch < 1)344 if (RT_UNLIKELY(cch < 1)) 345 345 { 346 346 RTStrAssertMsgFailed(("Buffer overflow! 1\n")); … … 353 353 else if (wc < 0x800) 354 354 { 355 if ( cch < 2)355 if (RT_UNLIKELY(cch < 2)) 356 356 { 357 357 RTStrAssertMsgFailed(("Buffer overflow! 2\n")); … … 365 365 else if (wc < 0xfffe) 366 366 { 367 if ( cch < 3)367 if (RT_UNLIKELY(cch < 3)) 368 368 { 369 369 RTStrAssertMsgFailed(("Buffer overflow! 3\n")); … … 407 407 + ( ((wc & 0x3ff) << 10) 408 408 | (wc2 & 0x3ff)); 409 if ( cch < 4)409 if (RT_UNLIKELY(cch < 4)) 410 410 { 411 411 RTStrAssertMsgFailed(("Buffer overflow! 4\n")); … … 495 495 { 496 496 fShouldFree = false; 497 if ( cch <= cchResult)497 if (RT_UNLIKELY(cch <= cchResult)) 498 498 return VERR_BUFFER_OVERFLOW; 499 499 pszResult = *ppsz; … … 667 667 if (!wc) 668 668 break; 669 else if ( wc < 256)669 else if (RT_LIKELY(wc < 0x100)) 670 670 ++cch; 671 else if (wc < 0xd800 || wc > 0xdfff) 672 { 673 if (wc < 0xfffe) 674 { 675 rc = VERR_NO_TRANSLATION; 676 break; 671 else 672 { 673 if (wc < 0xd800 || wc > 0xdfff) 674 { 675 if (wc >= 0xfffe) 676 { 677 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc)); 678 rc = VERR_CODE_POINT_ENDIAN_INDICATOR; 679 break; 680 } 677 681 } 678 682 else 679 683 { 680 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc)); 681 rc = VERR_CODE_POINT_ENDIAN_INDICATOR; 682 break; 683 } 684 } 685 else 686 { 687 if (wc >= 0xdc00) 688 { 689 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc)); 690 rc = VERR_INVALID_UTF16_ENCODING; 691 break; 692 } 693 if (cwc <= 0) 694 { 695 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc)); 696 rc = VERR_INVALID_UTF16_ENCODING; 697 break; 698 } 699 wc = *pwsz++; cwc--; 700 if (wc < 0xdc00 || wc > 0xdfff) 701 { 702 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc)); 703 rc = VERR_INVALID_UTF16_ENCODING; 704 break; 705 } 684 if (wc >= 0xdc00) 685 { 686 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc)); 687 rc = VERR_INVALID_UTF16_ENCODING; 688 break; 689 } 690 if (cwc <= 0) 691 { 692 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc)); 693 rc = VERR_INVALID_UTF16_ENCODING; 694 break; 695 } 696 wc = *pwsz++; cwc--; 697 if (wc < 0xdc00 || wc > 0xdfff) 698 { 699 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc)); 700 rc = VERR_INVALID_UTF16_ENCODING; 701 break; 702 } 703 } 704 706 705 rc = VERR_NO_TRANSLATION; 707 706 break; 708 707 } 709 708 } 710 711 709 712 710 /* done */ … … 725 723 * @param psz Where to store the Latin1 string. 726 724 * @param cch The size of the Latin1 buffer, excluding the terminator. 727 * @param pcch Where to store the number of octets actually encoded.728 725 */ 729 static int rtUtf16RecodeAsLatin1(PCRTUTF16 pwsz, size_t cwc, char *psz, size_t cch , size_t *pcch)730 { 731 unsigned char *p wch = (unsigned char *)psz;732 int rc = VINF_SUCCESS;726 static int rtUtf16RecodeAsLatin1(PCRTUTF16 pwsz, size_t cwc, char *psz, size_t cch) 727 { 728 unsigned char *pch = (unsigned char *)psz; 729 int rc = VINF_SUCCESS; 733 730 while (cwc > 0) 734 731 { … … 736 733 if (!wc) 737 734 break; 738 else if (wc < 0xd800 || wc > 0xdfff) 739 { 740 if (wc < 0x100) 741 { 742 if (cch < 1) 743 { 744 RTStrAssertMsgFailed(("Buffer overflow! 1\n")); 745 rc = VERR_BUFFER_OVERFLOW; 735 if (RT_LIKELY(wc < 0x100)) 736 { 737 if (RT_UNLIKELY(cch < 1)) 738 { 739 RTStrAssertMsgFailed(("Buffer overflow! 1\n")); 740 rc = VERR_BUFFER_OVERFLOW; 741 break; 742 } 743 cch--; 744 *pch++ = (unsigned char)wc; 745 } 746 else 747 { 748 if (wc < 0xd800 || wc > 0xdfff) 749 { 750 if (wc >= 0xfffe) 751 { 752 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc)); 753 rc = VERR_CODE_POINT_ENDIAN_INDICATOR; 746 754 break; 747 755 } 748 cch--;749 *pwch++ = (char)wc;750 }751 else if (wc < 0xfffe)752 {753 rc = VERR_NO_TRANSLATION;754 break;755 756 } 756 757 else 757 758 { 758 RTStrAssertMsgFailed(("endian indicator! wc=%#x\n", wc)); 759 rc = VERR_CODE_POINT_ENDIAN_INDICATOR; 760 break; 761 } 762 } 763 else 764 { 765 if (wc >= 0xdc00) 766 { 767 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc)); 768 rc = VERR_INVALID_UTF16_ENCODING; 769 break; 770 } 771 if (cwc <= 0) 772 { 773 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc)); 774 rc = VERR_INVALID_UTF16_ENCODING; 775 break; 776 } 777 RTUTF16 wc2 = *pwsz++; cwc--; 778 if (wc2 < 0xdc00 || wc2 > 0xdfff) 779 { 780 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc)); 781 rc = VERR_INVALID_UTF16_ENCODING; 782 break; 783 } 759 if (wc >= 0xdc00) 760 { 761 RTStrAssertMsgFailed(("Wrong 1st char in surrogate! wc=%#x\n", wc)); 762 rc = VERR_INVALID_UTF16_ENCODING; 763 break; 764 } 765 if (cwc <= 0) 766 { 767 RTStrAssertMsgFailed(("Invalid length! wc=%#x\n", wc)); 768 rc = VERR_INVALID_UTF16_ENCODING; 769 break; 770 } 771 RTUTF16 wc2 = *pwsz++; cwc--; 772 if (wc2 < 0xdc00 || wc2 > 0xdfff) 773 { 774 RTStrAssertMsgFailed(("Wrong 2nd char in surrogate! wc=%#x\n", wc)); 775 rc = VERR_INVALID_UTF16_ENCODING; 776 break; 777 } 778 } 779 784 780 rc = VERR_NO_TRANSLATION; 785 781 break; … … 788 784 789 785 /* done */ 790 *pwch = '\0'; 791 *pcch = (char *)pwch - psz; 786 *pch = '\0'; 792 787 return rc; 793 788 } … … 816 811 if (pszResult) 817 812 { 818 rc = rtUtf16RecodeAsLatin1(pwszString, RTSTR_MAX, pszResult, cch , &cch);813 rc = rtUtf16RecodeAsLatin1(pwszString, RTSTR_MAX, pszResult, cch); 819 814 if (RT_SUCCESS(rc)) 820 815 { … … 838 833 * Validate input. 839 834 */ 840 Assert (VALID_PTR(pwszString));841 Assert (VALID_PTR(ppsz));842 Assert (!pcch || VALID_PTR(pcch));835 AssertPtr(pwszString); 836 AssertPtr(ppsz); 837 AssertPtrNull(pcch); 843 838 844 839 /* … … 873 868 if (pszResult) 874 869 { 875 rc = rtUtf16RecodeAsLatin1(pwszString, cwcString, pszResult, cch - 1 , &cch);870 rc = rtUtf16RecodeAsLatin1(pwszString, cwcString, pszResult, cch - 1); 876 871 if (RT_SUCCESS(rc)) 877 872 { … … 941 936 * @param pwsz Where to store the UTF-16 string. 942 937 * @param cwc The number of RTUTF16 items the pwsz buffer can hold, excluding the terminator ('\\0'). 943 * @param pcwc Where to store the actual number of RTUTF16 items encoded into the UTF-16. This excludes the terminator.944 938 */ 945 static int rtLatin1RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc , size_t *pcwc)946 { 947 int rc = VINF_SUCCESS;939 static int rtLatin1RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc) 940 { 941 int rc = VINF_SUCCESS; 948 942 const unsigned char *puch = (const unsigned char *)psz; 949 const PRTUTF16 pwszEnd = pwsz + cwc; 950 PRTUTF16 pwc = pwsz; 951 Assert(pwszEnd >= pwc); 952 while (cch > 0) 943 PRTUTF16 pwc = pwsz; 944 while (cch-- > 0) 953 945 { 954 946 /* read the next char and check for terminator. */ … … 958 950 959 951 /* check for output overflow */ 960 if ( pwc >= pwszEnd)952 if (RT_UNLIKELY(cwc < 1)) 961 953 { 962 954 rc = VERR_BUFFER_OVERFLOW; … … 966 958 /* expand the code point */ 967 959 *pwc++ = uch; 960 cwc--; 968 961 puch++; 969 cch--;970 962 } 971 963 972 964 /* done */ 973 965 *pwc = '\0'; 974 *pcwc = pwc - pwsz;975 966 return rc; 976 967 } … … 1002 993 * Encode the UTF-16 string. 1003 994 */ 1004 rc = rtLatin1RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc , &cwc);995 rc = rtLatin1RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc); 1005 996 if (RT_SUCCESS(rc)) 1006 997 { … … 1061 1052 * Encode the UTF-16 string. 1062 1053 */ 1063 rc = rtLatin1RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1 , &cwcResult);1054 rc = rtLatin1RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1); 1064 1055 if (RT_SUCCESS(rc)) 1065 1056 {
Note:
See TracChangeset
for help on using the changeset viewer.