VirtualBox

Ignore:
Timestamp:
Nov 10, 2008 1:06:14 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
39060
Message:

IPRT: RTStrNICmp correction - if cch == 0 then return equal regardless of the pointers. RTStrGetCpNEx argument order, pcch relates to ppsz and should come after it. RTStrGetCpNEx should not access *ppsz if *pcch == 0. RTStrGetCpNExInternal should update *pcch in the ASCII case. RTStrGetCpNEx returns VERR_END_OF_STRING when *pcch == 0.

File:
1 edited

Legend:

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

    r13927 r14007  
    920920 * @returns rc
    921921 * @param   ppsz        The pointer to the the string position point.
     922 * @param   pcch        Pointer to the string length.
    922923 * @param   pCp         Where to store RTUNICP_INVALID.
    923  * @param   pcch        Pointer to the string length.
    924924 * @param   rc          The iprt error code.
    925925 */
    926 static int rtStrGetCpNExFailure(const char **ppsz, PRTUNICP pCp, size_t *pcch, int rc)
     926static int rtStrGetCpNExFailure(const char **ppsz, size_t *pcch, PRTUNICP pCp, int rc)
    927927{
    928928    /*
     
    936936
    937937
    938 RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, PRTUNICP pCp, size_t *pcch)
     938RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp)
    939939{
    940940    const unsigned char *puch = (const unsigned char *)*ppsz;
    941941    const unsigned char uch = *puch;
     942    size_t              cch = *pcch;
    942943    RTUNICP             uc;
    943944
    944     if (*pcch == 0)
     945    if (cch == 0)
    945946    {
    946947        *pCp = RTUNICP_INVALID;
    947         return VERR_INVALID_UTF8_ENCODING;
    948     }
     948        return VERR_END_OF_STRING;
     949    }
     950
    949951    /* ASCII ? */
    950952    if (!(uch & RT_BIT(7)))
     
    952954        uc = uch;
    953955        puch++;
     956        cch--;
    954957    }
    955958    else if (uch & RT_BIT(6))
     
    970973        {
    971974            RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
    972             return rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING);
    973         }
    974 
    975         if (cb > *pcch)
    976             return rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING);
     975            return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
     976        }
     977
     978        if (cb > cch)
     979            return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
    977980
    978981        /* validate the rest */
     
    981984            case 6:
    982985                RTStrAssertMsgReturn((puch[5] & 0xc0) == 0x80, ("6/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    983                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     986                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    984987            case 5:
    985988                RTStrAssertMsgReturn((puch[4] & 0xc0) == 0x80, ("5/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    986                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     989                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    987990            case 4:
    988991                RTStrAssertMsgReturn((puch[3] & 0xc0) == 0x80, ("4/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    989                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     992                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    990993            case 3:
    991994                RTStrAssertMsgReturn((puch[2] & 0xc0) == 0x80, ("3/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    992                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     995                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    993996            case 2:
    994997                RTStrAssertMsgReturn((puch[1] & 0xc0) == 0x80, ("2/%u: %.*Rhxs\n", cb, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    995                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     998                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    996999               break;
    9971000        }
     
    10091012                RTStrAssertMsgReturn(uc >= 0x04000000 && uc <= 0x7fffffff,
    10101013                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1011                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     1014                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    10121015                break;
    10131016            case 5:
     
    10191022                RTStrAssertMsgReturn(uc >= 0x00200000 && uc <= 0x03ffffff,
    10201023                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1021                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     1024                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    10221025                break;
    10231026            case 4:
     
    10281031                RTStrAssertMsgReturn(uc >= 0x00010000 && uc <= 0x001fffff,
    10291032                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1030                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     1033                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    10311034                break;
    10321035            case 3:
     
    10361039                RTStrAssertMsgReturn(uc >= 0x00000800 && uc <= 0x0000fffd,
    10371040                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1038                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING));
     1041                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, uc == 0xffff || uc == 0xfffe ? VERR_CODE_POINT_ENDIAN_INDICATOR : VERR_INVALID_UTF8_ENCODING));
    10391042                RTStrAssertMsgReturn(uc < 0xd800 || uc > 0xdfff,
    10401043                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1041                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_CODE_POINT_SURROGATE));
     1044                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_CODE_POINT_SURROGATE));
    10421045                break;
    10431046            case 2:
     
    10461049                RTStrAssertMsgReturn(uc >= 0x00000080 && uc <= 0x000007ff,
    10471050                                     ("%u: cp=%#010RX32: %.*Rhxs\n", cb, uc, RT_MIN(cb + 10, strlen((char *)puch)), puch),
    1048                                      rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING));
     1051                                     rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING));
    10491052                break;
    10501053            default: /* impossible, but GCC is bitching. */
     
    10531056        }
    10541057        puch += cb;
    1055         (*pcch) -= cb;
     1058        cch -= cb;
    10561059    }
    10571060    else
     
    10591062        /* 6th bit is always set. */
    10601063        RTStrAssertMsgFailed(("Invalid UTF-8 first byte: %.*Rhxs\n", RT_MIN(strlen((char *)puch), 10), puch));
    1061         return rtStrGetCpNExFailure(ppsz, pCp, pcch, VERR_INVALID_UTF8_ENCODING);
     1064        return rtStrGetCpNExFailure(ppsz, pcch, pCp, VERR_INVALID_UTF8_ENCODING);
    10621065    }
    10631066    *pCp = uc;
    10641067    *ppsz = (const char *)puch;
     1068    (*pcch) = cch;
    10651069    return VINF_SUCCESS;
    10661070}
     
    12411245        return 1;
    12421246
    1243 #if 1 /* new */
    12441247    const char *pszStart1 = psz1;
    12451248    for (;;)
     
    12851288    /* Hit some bad encoding, continue in case insensitive mode. */
    12861289    return RTStrCmp(psz1, psz2);
    1287 #else /* old */
    1288 #ifdef RT_OS_WINDOWS
    1289     return stricmp(psz1, psz2);
    1290 #else /* !RT_OS_WINDOWS */
    1291     return strcasecmp(psz1, psz2);
    1292 #endif /* !RT_OS_WINDOWS */
    1293 #endif
    12941290}
    12951291
     
    13181314RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax)
    13191315{
     1316    if (cchMax == 0)
     1317        return 0;
    13201318    if (psz1 == psz2)
    13211319        return 0;
     
    13241322    if (!psz2)
    13251323        return 1;
    1326     if (cchMax == 0)
    1327         return 0;
    1328 
    1329 #if 1 /* new */
     1324
    13301325    const char *pszStart1 = psz1;
    13311326    for (;;)
     
    13341329        RTUNICP cp1;
    13351330        size_t cchMax2 = cchMax;
    1336         int rc = RTStrGetCpNEx(&psz1, &cp1, &cchMax);
     1331        int rc = RTStrGetCpNEx(&psz1, &cchMax, &cp1);
    13371332        if (RT_FAILURE(rc))
    13381333        {
     
    13441339
    13451340        RTUNICP cp2;
    1346         rc = RTStrGetCpNEx(&psz2, &cp2, &cchMax2);
     1341        rc = RTStrGetCpNEx(&psz2, &cchMax2, &cp2);
    13471342        if (RT_FAILURE(rc))
    13481343        {
     
    13741369    /* Hit some bad encoding, continue in case insensitive mode. */
    13751370    return RTStrNCmp(psz1, psz2, cchMax);
    1376 #else /* old */
    1377 #ifdef RT_OS_WINDOWS
    1378     return strnicmp(psz1, psz2, cchMax);
    1379 #else /* !RT_OS_WINDOWS */
    1380     return strncasecmp(psz1, psz2, cchMax);
    1381 #endif /* !RT_OS_WINDOWS */
    1382 #endif
    1383 }
    1384 
     1371}
     1372
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