VirtualBox

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/common/string
Files:
2 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
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