VirtualBox

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

iprt/utf-8.cpp: dropped some unnecessary bits and fixed theoretical pointer wrapping with ancient compilers.

File:
1 edited

Legend:

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

    r21337 r21744  
    189189 * @param   paCps   Where to store the code points array.
    190190 * @param   cCps    The number of RTUNICP items the paCps buffer can hold, excluding the terminator ('\\0').
    191  * @param   pcCps   Where to store the actual number of decoded code points. This excludes the terminator.
    192191 */
    193 static int rtUtf8Decode(const char *psz, size_t cch, PRTUNICP paCps, size_t cCps, size_t *pcCps)
    194 {
    195     int                     rc = VINF_SUCCESS;
     192static int rtUtf8Decode(const char *psz, size_t cch, PRTUNICP paCps, size_t cCps)
     193{
     194    int                     rc   = VINF_SUCCESS;
    196195    const unsigned char    *puch = (const unsigned char *)psz;
    197     const PRTUNICP          pCpEnd = paCps + cCps;
    198     PRTUNICP                pCp = paCps;
    199     Assert(pCpEnd >= pCp);
     196    PRTUNICP                pCp  = paCps;
    200197    while (cch > 0)
    201198    {
     
    206203
    207204        /* check for output overflow */
    208         if (pCp >= pCpEnd)
     205        if (RT_UNLIKELY(cCps < 1))
    209206        {
    210207            rc = VERR_BUFFER_OVERFLOW;
    211208            break;
    212209        }
     210        cCps--;
    213211
    214212        /* decode and recode the code point */
     
    273271    /* done */
    274272    *pCp = 0;
    275     *pcCps = pCp - paCps;
    276273    return rc;
    277274}
     
    363360             * Decode the string.
    364361             */
    365             rc = rtUtf8Decode(pszString, RTSTR_MAX, paCps, cCps, &cCps);
     362            rc = rtUtf8Decode(pszString, RTSTR_MAX, paCps, cCps);
    366363            if (RT_SUCCESS(rc))
    367364            {
     
    422419             * Encode the UTF-16 string.
    423420             */
    424             rc = rtUtf8Decode(pszString, cchString, paCpsResult, cCps - 1, &cCpsResult);
     421            rc = rtUtf8Decode(pszString, cchString, paCpsResult, cCps - 1);
    425422            if (RT_SUCCESS(rc))
    426423            {
     
    585582 * @param   pwsz    Where to store the UTF-16 string.
    586583 * @param   cwc     The number of RTUTF16 items the pwsz buffer can hold, excluding the terminator ('\\0').
    587  * @param   pcwc    Where to store the actual number of RTUTF16 items encoded into the UTF-16. This excludes the terminator.
    588584 */
    589 static int rtUtf8RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc, size_t *pcwc)
    590 {
    591     int                     rc = VINF_SUCCESS;
     585static int rtUtf8RecodeAsUtf16(const char *psz, size_t cch, PRTUTF16 pwsz, size_t cwc)
     586{
     587    int                     rc   = VINF_SUCCESS;
    592588    const unsigned char    *puch = (const unsigned char *)psz;
    593     const PRTUTF16          pwszEnd = pwsz + cwc;
    594     PRTUTF16                pwc = pwsz;
    595     Assert(pwszEnd >= pwc);
     589    PRTUTF16                pwc  = pwsz;
    596590    while (cch > 0)
    597591    {
     
    602596
    603597        /* check for output overflow */
    604         if (pwc >= pwszEnd)
     598        if (RT_UNLIKELY(cwc < 1))
    605599        {
    606600            rc = VERR_BUFFER_OVERFLOW;
    607601            break;
    608602        }
     603        cwc--;
    609604
    610605        /* decode and recode the code point */
     
    640635                       | ((RTUNICP)(puch[1] & 0x3f) << 12)
    641636                       | ((RTUNICP)(uch     & 0x07) << 18);
    642             if (pwc + 1 >= pwszEnd)
     637            if (RT_UNLIKELY(cwc < 1))
    643638            {
    644639                rc = VERR_BUFFER_OVERFLOW;
    645640                break;
    646641            }
     642            cwc--;
     643
    647644            uc -= 0x10000;
    648645            *pwc++ = 0xd800 | (uc >> 10);
     
    655652    /* done */
    656653    *pwc = '\0';
    657     *pcwc = pwc - pwsz;
    658654    return rc;
    659655}
     
    685681             * Encode the UTF-16 string.
    686682             */
    687             rc = rtUtf8RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc, &cwc);
     683            rc = rtUtf8RecodeAsUtf16(pszString, RTSTR_MAX, pwsz, cwc);
    688684            if (RT_SUCCESS(rc))
    689685            {
     
    744740             * Encode the UTF-16 string.
    745741             */
    746             rc = rtUtf8RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1, &cwcResult);
     742            rc = rtUtf8RecodeAsUtf16(pszString, cchString, pwszResult, cwc - 1);
    747743            if (RT_SUCCESS(rc))
    748744            {
Note: See TracChangeset for help on using the changeset viewer.

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