VirtualBox

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


Ignore:
Timestamp:
Jul 20, 2009 3:11:45 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
50300
Message:

iprt/string: change behaviour of Utf16 to Latin1 to reject untranslatable strings instead of doing a best-effort translation

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

Legend:

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

    r21714 r21728  
    667667        if (!wc)
    668668            break;
     669        else if (wc < 256)
     670            ++cch;
    669671        else if (wc < 0xd800 || wc > 0xdfff)
    670672        {
    671673            if (wc < 0xfffe)
    672                 ++cch;
     674            {
     675                rc = VERR_NO_TRANSLATION;
     676                break;
     677            }
    673678            else
    674679            {
     
    699704                break;
    700705            }
    701             ++cch;
     706            rc = VERR_NO_TRANSLATION;
     707            break;
    702708        }
    703709    }
     
    745751            else if (wc < 0xfffe)
    746752            {
    747                 if (cch < 1)
    748                 {
    749                     RTStrAssertMsgFailed(("Buffer overflow! 3\n"));
    750                     rc = VERR_BUFFER_OVERFLOW;
    751                     break;
    752                 }
    753                 cch--;
    754                 *pwch++ = '?';
     753                rc = VERR_NO_TRANSLATION;
     754                break;
    755755            }
    756756            else
     
    782782                break;
    783783            }
    784             if (cch < 1)
    785             {
    786                 RTStrAssertMsgFailed(("Buffer overflow! 4\n"));
    787                 rc = VERR_BUFFER_OVERFLOW;
    788                 break;
    789             }
    790             cch--;
    791             *pwch++ = '?';
     784            rc = VERR_NO_TRANSLATION;
     785            break;
    792786        }
    793787    }
  • trunk/src/VBox/Runtime/testcase/tstUtf8.cpp

    r21723 r21728  
    10161016    /* Test Utf16 -> Latin1 */
    10171017    size_t cch_szAll = 0;
     1018    size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
     1019    RTTEST_CHECK(hTest, cbShort == 0);
    10181020    int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
    10191021    RTTEST_CHECK(hTest, (cch_szAll == 255));
    10201022    rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
     1023    RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
     1024    char *psz = NULL;
     1025    RTUTF16 wszShort[256] = { 0 };
     1026    for (unsigned i = 0; i < 255; ++i)
     1027        wszShort[i] = i + 1;
     1028    cbShort = RTUtf16CalcLatin1Len(wszShort);
     1029    RTTEST_CHECK(hTest, cbShort == 255);
     1030    rc = RTUtf16ToLatin1(wszShort, &psz);
    10211031    RTTEST_CHECK_RC_OK(hTest, rc);
    10221032    if (RT_SUCCESS(rc))
    10231033    {
    1024         RTTEST_CHECK(hTest, (cch_szAll == 0x110000 - 1 - 0x800 - 2));
    1025         RTTEST_CHECK(hTest, (cch_szAll == RTUtf16CalcLatin1Len(g_wszAll)));
    1026     }
    1027     char *psz = NULL;
    1028     rc = RTUtf16ToLatin1(g_wszAll, &psz);
    1029     RTTEST_CHECK_RC_OK(hTest, rc);
    1030     if (RT_SUCCESS(rc))
    1031     {
    1032         RTTEST_CHECK(hTest, (strlen(psz) == cch_szAll));
     1034        RTTEST_CHECK(hTest, (strlen(psz) == 255));
    10331035        for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
    1034             if (   ((j < 0x100) && (psz[i] != (char) j))
    1035                 || ((j > 0xff) && psz[i] != '?'))
     1036            if (psz[i] != (char) j)
    10361037            {
    10371038                RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
     
    10391040            }
    10401041    }
     1042    RTStrFree(psz);
     1043    rc = RTUtf16ToLatin1(g_wszAll, &psz);
     1044    RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
    10411045    char sz[512];
    10421046    char *psz2 = &sz[0];
     
    10441048    rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
    10451049                           &cchActual);
     1050    RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
     1051    RTTEST_CHECK_MSG(hTest, cchActual == 0,
     1052                     (hTest, "cchActual=%lu\n", cchActual));
     1053    rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
     1054                           &cchActual);
    10461055    RTTEST_CHECK_RC_OK(hTest, rc);
    10471056    if (RT_SUCCESS(rc))
    10481057    {
    1049         RTTEST_CHECK(hTest, (cchActual == sizeof(sz) - 1));
     1058        RTTEST_CHECK(hTest, (cchActual == 255));
    10501059        RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
    1051         for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
    1052             if (   ((j < 0x100) && (psz[i] != (char) j))
    1053                 || ((j > 0xff) && psz[i] != '?'))
     1060        for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
     1061            if (psz2[i] != (char) j)
    10541062            {
    10551063                RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
     
    10571065            }
    10581066    }
    1059     rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz), &psz2, sizeof(sz),
    1060                            &cchActual);
     1067    rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
    10611068    RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
    10621069    /** @todo Either fix the documentation or fix the code - cchActual is
    10631070     * set to the number of bytes actually encoded. */
    1064     RTTEST_CHECK(hTest, (cchActual == sizeof(sz)));
    1065     RTStrFree(psz);
    1066     rc = RTUtf16ToLatin1Ex(g_wszAll, 512, &psz, 0, &cchActual);
     1071    RTTEST_CHECK_MSG(hTest, (cchActual == 128),
     1072                     (hTest, "cchActual=%lu\n", cchActual));
     1073    rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
    10671074    RTTEST_CHECK_RC_OK(hTest, rc);
    10681075    if (RT_SUCCESS(rc))
    10691076    {
    1070         RTTEST_CHECK(hTest, (cchActual == 512));
     1077        RTTEST_CHECK(hTest, (cchActual == 255));
    10711078        RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
    10721079        for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
     
    10781085            }
    10791086    }
    1080     const char *pszGood = "H\0e\0l\0l\0o\0\0\xD8\0\xDC\0";
    10811087    const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
    1082     rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszGood, RTSTR_MAX, &psz2, sizeof(sz),
    1083                            &cchActual);
    1084     RTTEST_CHECK_RC_OK(hTest, rc);
    1085     RTTEST_CHECK(hTest, (cchActual == 6));
    1086     RTTEST_CHECK(hTest, (strlen(sz) == 6));
    1087     rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszGood, RTSTR_MAX, &psz2, sizeof(sz),
    1088                            NULL);
    1089     RTTEST_CHECK_RC_OK(hTest, rc);
    10901088    rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
    10911089                           &cchActual);
    10921090    RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
    1093     RTStrFree(psz);
    1094     rc = RTUtf16ToLatin1((RTUTF16 *) pszGood, &psz);
    1095     RTTEST_CHECK_RC_OK(hTest, rc);
    1096     RTTEST_CHECK(hTest, (strlen(psz) == 6));
    10971091    RTStrFree(psz);
    10981092
     
    11891183        RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
    11901184
    1191     RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
    1192     rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
    1193     RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
    1194     if (RT_SUCCESS(rc))
    1195         RTStrFree(pszOut);
    1196 
    11971185    RTStrFree(pszTest1);
    11981186    RTTestSubDone(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