VirtualBox

Changeset 40071 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Feb 10, 2012 9:35:27 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
76205
Message:

Runtime/strings: add Utf-8 and Utf-16 sanitising to a white list of characters.

Location:
trunk/src/VBox/Runtime
Files:
3 edited

Legend:

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

    r31157 r40071  
    250250
    251251
     252ssize_t RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidSet, char chReplacement)
     253{
     254    size_t cReplacements = 0;
     255    AssertReturn(chReplacement && (unsigned)chReplacement < 128, -1);
     256    /* Validate the encoding. */
     257    if (RT_FAILURE(RTUtf16CalcUtf8LenEx(pwsz, RTSTR_MAX, NULL)))
     258        return -1;
     259    for (;;)
     260    {
     261        RTUNICP Cp;
     262        PCRTUNICP pCp;
     263        PRTUTF16 pwszOld = pwsz;
     264        RTUtf16GetCpEx((PCRTUTF16 *)&pwsz, &Cp);
     265        if (!Cp)
     266            break;
     267        for (pCp = puszValidSet; ; ++pCp)
     268            if (!*pCp || *pCp == Cp)
     269                break;
     270        if (!*pCp)
     271        {
     272            for (; pwszOld != pwsz; ++pwszOld)
     273                *pwszOld = chReplacement;
     274            ++cReplacements;
     275        }
     276    }
     277    return cReplacements;
     278}
     279RT_EXPORT_SYMBOL(RTUtf16PurgeComplementSet);
     280
     281
    252282/**
    253283 * Validate the UTF-16 encoding and calculates the length of an UTF-8 encoding.
  • trunk/src/VBox/Runtime/common/string/utf-8.cpp

    r36555 r40071  
    350350}
    351351RT_EXPORT_SYMBOL(RTStrPurgeEncoding);
     352
     353
     354ssize_t RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidSet, char chReplacement)
     355{
     356    size_t cReplacements = 0;
     357    AssertReturn(chReplacement && (unsigned)chReplacement < 128, -1);
     358    if (RT_FAILURE(RTStrValidateEncoding(psz)))
     359        return -1;
     360    for (;;)
     361    {
     362        RTUNICP Cp;
     363        PCRTUNICP pCp;
     364        char *pszOld = psz;
     365        RTStrGetCpEx((const char **)&psz, &Cp);
     366        if (!Cp)
     367            break;
     368        for (pCp = puszValidSet; ; ++pCp)
     369            if (!*pCp || *pCp == Cp)
     370                break;
     371        if (!*pCp)
     372        {
     373            for (; pszOld != psz; ++pszOld)
     374                *pszOld = chReplacement;
     375            ++cReplacements;
     376        }
     377    }
     378    return cReplacements;
     379}
     380RT_EXPORT_SYMBOL(RTStrPurgeComplementSet);
    352381
    353382
  • trunk/src/VBox/Runtime/testcase/tstUtf8.cpp

    r40065 r40071  
    836836
    837837    RTTestSubDone(hTest);
     838}
     839
     840
     841/**
     842 * Check string sanitising.
     843 */
     844void TstRTStrPurgeComplementSet(RTTEST hTest)
     845{
     846    RTTestSub(hTest, "RTStrPurgeComplementSet");
     847    RTUNICP aCpSet[] = { '1', '2', '3', '4', '5', 'w', 'r', 'f', 't', 'e',
     848                         '\0' };
     849    struct
     850    {
     851        const char *pcszIn;
     852        const char *pcszOut;
     853        PCRTUNICP   pcCpSet;
     854        char        chReplacement;
     855        ssize_t     cExpected;
     856    }
     857    aTests[] =
     858    {
     859        { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
     860        { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
     861          "123_54wert__trew___4321", aCpSet, '_', 3 },
     862        { "hjhj8766", "????????", aCpSet, '?', 8 },
     863        { "123\xf0\xa4\xad\xa2""4", "123____4", aCpSet, '_', 1 },
     864        { "\xff", "\xff", aCpSet, '_', -1 }
     865    };
     866    enum { MAX_IN_STRING = 256 };
     867
     868    for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
     869    {
     870        char szCopy[MAX_IN_STRING];
     871        ssize_t cReplacements;
     872        AssertRC(RTStrCopy(szCopy, RT_ELEMENTS(szCopy), aTests[i].pcszIn));
     873        cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet,
     874                                                aTests[i].chReplacement);
     875        if (cReplacements != aTests[i].cExpected)
     876            RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
     877                         (long long) aTests[i].cExpected,
     878                         (long long) cReplacements);
     879        if (strcmp(aTests[i].pcszOut, szCopy))
     880            RTTestFailed(hTest, "#%u: expected %s, actual %s\n", i,
     881                         aTests[i].pcszOut, szCopy);
     882    }
     883}
     884
     885
     886/**
     887 * Check string sanitising.
     888 */
     889void TstRTUtf16PurgeComplementSet(RTTEST hTest)
     890{
     891    RTTestSub(hTest, "RTUtf16PurgeComplementSet");
     892    RTUNICP aCpSet[] = { '1', '2', '3', '4', '5', 'w', 'r', 'f', 't', 'e',
     893                         '\0' };
     894    struct
     895    {
     896        const char *pcszIn;
     897        const char *pcszOut;
     898        PCRTUNICP   pcCpSet;
     899        char        chReplacement;
     900        ssize_t     cExpected;
     901    }
     902    aTests[] =
     903    {
     904        { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
     905        { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
     906          "123_54wert_trew_4321", aCpSet, '_', 3 },
     907        { "hjhj8766", "????????", aCpSet, '?', 8 },
     908        { "123\xf0\xa4\xad\xa2""4", "123__4", aCpSet, '_', 1 }
     909        /* No test for invalid encoding. */
     910    };
     911    enum { MAX_IN_STRING = 256 };
     912
     913    for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
     914    {
     915        RTUTF16 wszInCopy[MAX_IN_STRING],  *pwszInCopy  = wszInCopy;
     916        RTUTF16 wszOutCopy[MAX_IN_STRING], *pwszOutCopy = wszOutCopy;
     917        ssize_t cReplacements;
     918        AssertRC(RTStrToUtf16Ex(aTests[i].pcszIn, RTSTR_MAX, &pwszInCopy,
     919                                RT_ELEMENTS(wszInCopy), NULL));
     920        AssertRC(RTStrToUtf16Ex(aTests[i].pcszOut, RTSTR_MAX, &pwszOutCopy,
     921                                RT_ELEMENTS(wszOutCopy), NULL));
     922        cReplacements = RTUtf16PurgeComplementSet(wszInCopy, aTests[i].pcCpSet,
     923                                                  aTests[i].chReplacement);
     924        if (cReplacements != aTests[i].cExpected)
     925            RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
     926                         (long long) aTests[i].cExpected,
     927                         (long long) cReplacements);
     928        if (RTUtf16Cmp(wszInCopy, wszOutCopy))
     929            RTTestFailed(hTest, "#%u: expected %ls, actual %ls\n", i,
     930                         wszOutCopy, wszInCopy);
     931    }
    838932}
    839933
     
    14031497    TstRTStrXCmp(hTest);
    14041498    TstRTStrPurgeEncoding(hTest);
     1499    TstRTStrPurgeComplementSet(hTest);
     1500    TstRTUtf16PurgeComplementSet(hTest);
    14051501    testStrEnd(hTest);
    14061502    testStrStr(hTest);
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