VirtualBox

Ignore:
Timestamp:
Jul 21, 2009 12:06:38 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
50317
Message:

IPRT: Some latin-1 cleanup and some preditions in the recoding loops.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/utf-16.cpp

    r21728 r21740  
    342342            if (wc < 0x80)
    343343            {
    344                 if (cch < 1)
     344                if (RT_UNLIKELY(cch < 1))
    345345                {
    346346                    RTStrAssertMsgFailed(("Buffer overflow! 1\n"));
     
    353353            else if (wc < 0x800)
    354354            {
    355                 if (cch < 2)
     355                if (RT_UNLIKELY(cch < 2))
    356356                {
    357357                    RTStrAssertMsgFailed(("Buffer overflow! 2\n"));
     
    365365            else if (wc < 0xfffe)
    366366            {
    367                 if (cch < 3)
     367                if (RT_UNLIKELY(cch < 3))
    368368                {
    369369                    RTStrAssertMsgFailed(("Buffer overflow! 3\n"));
     
    407407                               + (  ((wc & 0x3ff) << 10)
    408408                                  | (wc2 & 0x3ff));
    409             if (cch < 4)
     409            if (RT_UNLIKELY(cch < 4))
    410410            {
    411411                RTStrAssertMsgFailed(("Buffer overflow! 4\n"));
     
    495495        {
    496496            fShouldFree = false;
    497             if (cch <= cchResult)
     497            if (RT_UNLIKELY(cch <= cchResult))
    498498                return VERR_BUFFER_OVERFLOW;
    499499            pszResult = *ppsz;
     
    667667        if (!wc)
    668668            break;
    669         else if (wc < 256)
     669        else if (RT_LIKELY(wc < 0x100))
    670670            ++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                }
    677681            }
    678682            else
    679683            {
    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
    706705            rc = VERR_NO_TRANSLATION;
    707706            break;
    708707        }
    709708    }
    710 
    711709
    712710    /* done */
     
    725723 * @param   psz         Where to store the Latin1 string.
    726724 * @param   cch         The size of the Latin1 buffer, excluding the terminator.
    727  * @param   pcch        Where to store the number of octets actually encoded.
    728725 */
    729 static int rtUtf16RecodeAsLatin1(PCRTUTF16 pwsz, size_t cwc, char *psz, size_t cch, size_t *pcch)
    730 {
    731     unsigned char  *pwch = (unsigned char *)psz;
    732     int             rc = VINF_SUCCESS;
     726static 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;
    733730    while (cwc > 0)
    734731    {
     
    736733        if (!wc)
    737734            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;
    746754                    break;
    747755                }
    748                 cch--;
    749                 *pwch++ = (char)wc;
    750             }
    751             else if (wc < 0xfffe)
    752             {
    753                 rc = VERR_NO_TRANSLATION;
    754                 break;
    755756            }
    756757            else
    757758            {
    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
    784780            rc = VERR_NO_TRANSLATION;
    785781            break;
     
    788784
    789785    /* done */
    790     *pwch = '\0';
    791     *pcch = (char *)pwch - psz;
     786    *pch = '\0';
    792787    return rc;
    793788}
     
    816811        if (pszResult)
    817812        {
    818             rc = rtUtf16RecodeAsLatin1(pwszString, RTSTR_MAX, pszResult, cch, &cch);
     813            rc = rtUtf16RecodeAsLatin1(pwszString, RTSTR_MAX, pszResult, cch);
    819814            if (RT_SUCCESS(rc))
    820815            {
     
    838833     * Validate input.
    839834     */
    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);
    843838
    844839    /*
     
    873868        if (pszResult)
    874869        {
    875             rc = rtUtf16RecodeAsLatin1(pwszString, cwcString, pszResult, cch - 1, &cch);
     870            rc = rtUtf16RecodeAsLatin1(pwszString, cwcString, pszResult, cch - 1);
    876871            if (RT_SUCCESS(rc))
    877872            {
     
    941936 * @param   pwsz    Where to store the UTF-16 string.
    942937 * @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.
    944938 */
    945 static int rtLatin1RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc, size_t *pcwc)
    946 {
    947     int                     rc = VINF_SUCCESS;
     939static int rtLatin1RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc)
     940{
     941    int                     rc   = VINF_SUCCESS;
    948942    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)
    953945    {
    954946        /* read the next char and check for terminator. */
     
    958950
    959951        /* check for output overflow */
    960         if (pwc >= pwszEnd)
     952        if (RT_UNLIKELY(cwc < 1))
    961953        {
    962954            rc = VERR_BUFFER_OVERFLOW;
     
    966958        /* expand the code point */
    967959        *pwc++ = uch;
     960        cwc--;
    968961        puch++;
    969         cch--;
    970962    }
    971963
    972964    /* done */
    973965    *pwc = '\0';
    974     *pcwc = pwc - pwsz;
    975966    return rc;
    976967}
     
    1002993             * Encode the UTF-16 string.
    1003994             */
    1004             rc = rtLatin1RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc, &cwc);
     995            rc = rtLatin1RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc);
    1005996            if (RT_SUCCESS(rc))
    1006997            {
     
    10611052             * Encode the UTF-16 string.
    10621053             */
    1063             rc = rtLatin1RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1, &cwcResult);
     1054            rc = rtLatin1RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1);
    10641055            if (RT_SUCCESS(rc))
    10651056            {
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette