Do not exclude Unicode noncharacters from rtl_convertUnicodeToText

For one, that broke round-tripping with e.g. UTF-8 (see the test case added to
Test::testComplex in sal/qa/rtl/textenc/rtl_textcvt.cxx) which did not treat
noncharacters as invalid.

For another, <https://unicode.org/faq/private_use.html#nonchar7> is meanwhile
quite clear on the matter:

"Q: Are noncharacters prohibited in interchange?

"A: This question has led to some controversy, because the Unicode Standard has
been somewhat ambiguous about the status of noncharacters. The formal wording of
the definition of 'noncharacter' in the standard has always indicated that
noncharacters 'should never be interchanged.' That led some people to assume
that the definition actually meant 'shall not be interchanged' and that
therefore the presence of a noncharacter in any Unicode string immediately
rendered that string malformed according to the standard. But the intended use
of noncharacters requires the ability to exchange them in a limited context, at
least across APIs and even through data files and other means of 'interchange',
so that they can be processed as intended. The choice of the word 'should' in
the original definition was deliberate, and indicated that one should not try to
interchange noncharacters precisely because their interpretation is strictly
internal to whatever implementation uses them, so they have no publicly
interchangeable semantics. But other informative wording in the text of the core
specification and in the character names list was differently and more strongly
worded, leading to contradictory interpretations.

"Given this ambiguity of intent, in 2013 the UTC issued Corrigendum #9, which
deleted the phrase 'and that should never be interchanged' from the definition
of noncharacters, to make it clear that prohibition from interchange is not part
of the formal definition of noncharacters. Corrigendum #9 has been incorporated
into the core specification for Unicode 7.0.

"Q: Are noncharacters invalid in Unicode strings and UTFs?

"A: Absolutely not. Noncharacters do not cause a Unicode string to be ill-formed
in any UTF. This can be seen explicitly in the table above, where every
noncharacter code point has a well-formed representation in UTF-32, in UTF-16,
and in UTF-8. An implementation which converts noncharacter code points between
one UTF representation and another must preserve these values correctly. The
fact that they are called 'noncharacters' and are not intended for open
interchange does not mean that they are somehow illegal or invalid code points
which make strings containing them invalid."

Change-Id: I4fcc0156e3d2fd305a7c7bb0c7b3dbef846c9e64
Reviewed-on: https://gerrit.libreoffice.org/78598
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Stephan Bergmann
2019-09-04 14:31:30 +02:00
parent 1496a1831d
commit cd563e7b80
11 changed files with 110 additions and 50 deletions

View File

@@ -457,6 +457,8 @@ public:
void testInvalidUtf8();
void testInvalidUnicode();
void testSRCBUFFERTOSMALL();
void testMime();
@@ -471,6 +473,7 @@ public:
CPPUNIT_TEST(testComplexCut);
CPPUNIT_TEST(testInvalidUtf7);
CPPUNIT_TEST(testInvalidUtf8);
CPPUNIT_TEST(testInvalidUnicode);
CPPUNIT_TEST(testSRCBUFFERTOSMALL);
CPPUNIT_TEST(testMime);
CPPUNIT_TEST(testWindows);
@@ -2336,6 +2339,15 @@ void Test::testComplex() {
true,
false,
RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR },
{ RTL_TEXTENCODING_UTF8,
RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBF"),
{0xFFFF},
1,
false,
true,
true,
false,
RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR },
// Test Java UTF-8:
@@ -2960,6 +2972,24 @@ void Test::testInvalidUtf8() {
}
}
void Test::testInvalidUnicode() {
auto const converter = rtl_createUnicodeToTextConverter(RTL_TEXTENCODING_UTF8);
CPPUNIT_ASSERT(converter != nullptr);
sal_Unicode const input[] = {0xDC00}; // lone low surrogate
char buf[TEST_STRING_SIZE];
sal_uInt32 info;
sal_Size converted;
auto const size = rtl_convertUnicodeToText(
converter, nullptr, input, SAL_N_ELEMENTS(input), buf, TEST_STRING_SIZE,
(RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
| RTL_UNICODETOTEXT_FLAGS_FLUSH),
&info, &converted);
CPPUNIT_ASSERT_EQUAL(sal_Size(0), size);
CPPUNIT_ASSERT_EQUAL(RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_INVALID, info);
CPPUNIT_ASSERT_EQUAL(sal_Size(1), converted);
rtl_destroyTextToUnicodeConverter(converter);
}
void Test::testSRCBUFFERTOSMALL() {
rtl_TextToUnicodeConverter cv = rtl_createTextToUnicodeConverter(
RTL_TEXTENCODING_EUC_JP);

View File

@@ -21,6 +21,7 @@
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -333,6 +334,11 @@ sal_Size ImplConvertUnicodeToBig5Hkscs(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -342,11 +348,7 @@ sal_Size ImplConvertUnicodeToBig5Hkscs(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)

View File

@@ -19,6 +19,9 @@
#include <sal/config.h>
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -342,6 +345,11 @@ sal_Size ImplConvertUnicodeToEucTw(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -351,11 +359,7 @@ sal_Size ImplConvertUnicodeToEucTw(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)

View File

@@ -19,6 +19,9 @@
#include <sal/config.h>
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -332,6 +335,11 @@ sal_Size ImplConvertUnicodeToGb18030(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -341,11 +349,7 @@ sal_Size ImplConvertUnicodeToGb18030(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)

View File

@@ -7,10 +7,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <cassert>
#include "converter.hxx"
#include "unichars.hxx"
#include "convertisciidevangari.hxx"
#include "convertsinglebytetobmpunicode.hxx"
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
using namespace sal::detail::textenc;
@@ -261,6 +267,11 @@ sal_Size UnicodeToIsciiDevanagari::convert(sal_Unicode const* pSrcBuf, sal_Size
cHighSurrogate = static_cast< sal_Unicode >(c);
continue;
}
else if (ImplIsLowSurrogate(c))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(c))
{
@@ -271,11 +282,7 @@ sal_Size UnicodeToIsciiDevanagari::convert(sal_Unicode const* pSrcBuf, sal_Size
bUndefined = false;
goto bad_input;
}
if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(c));
//halant + halant E8 E8 -> halant + ZWNJ 094D 200C
//halant + nukta E8 E9 halant + ZWJ 094D 200D

View File

@@ -19,6 +19,9 @@
#include <sal/config.h>
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -558,6 +561,11 @@ sal_Size ImplConvertUnicodeToIso2022Cn(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -567,11 +575,7 @@ sal_Size ImplConvertUnicodeToIso2022Cn(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{

View File

@@ -19,6 +19,9 @@
#include <sal/config.h>
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -377,6 +380,11 @@ sal_Size ImplConvertUnicodeToIso2022Jp(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -386,11 +394,7 @@ sal_Size ImplConvertUnicodeToIso2022Jp(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{

View File

@@ -19,6 +19,9 @@
#include <sal/config.h>
#include <cassert>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -355,6 +358,11 @@ sal_Size ImplConvertUnicodeToIso2022Kr(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar))
{
bUndefined = false;
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -364,11 +372,7 @@ sal_Size ImplConvertUnicodeToIso2022Kr(void const * pData,
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{

View File

@@ -19,8 +19,10 @@
#include <sal/config.h>
#include <cassert>
#include <cstddef>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -113,16 +115,18 @@ sal_Size rtl_textenc_convertBmpUnicodeToSingleByte(
highSurrogate = static_cast< sal_Unicode >(c);
continue;
}
else if (ImplIsLowSurrogate(c))
{
undefined = false;
goto bad_input;
}
} else if (ImplIsLowSurrogate(c)) {
c = ImplCombineSurrogates(highSurrogate, c);
} else {
undefined = false;
goto bad_input;
}
if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c)) {
undefined = false;
goto bad_input;
}
assert(rtl::isUnicodeScalarValue(c));
// Linearly searching through the ranges if probably fastest, assuming
// that most converted characters belong to the ASCII subset:
for (std::size_t i = 0; i < entries; ++i) {

View File

@@ -19,7 +19,10 @@
#include <sal/config.h>
#include <cassert>
#include <sal/types.h>
#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include "converter.hxx"
@@ -347,15 +350,17 @@ sal_Size ImplConvertUnicodeToUtf8(
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
else if (ImplIsLowSurrogate(nChar) && !bJavaUtf8)
{
goto bad_input;
}
}
else if (ImplIsLowSurrogate(nChar) && !bJavaUtf8)
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
else
goto bad_input;
if ((ImplIsLowSurrogate(nChar) && !bJavaUtf8)
|| ImplIsNoncharacter(nChar))
goto bad_input;
assert(bJavaUtf8 ? nChar <= 0xFFFF : rtl::isUnicodeScalarValue(nChar));
if (nChar <= 0x7F && (!bJavaUtf8 || nChar != 0))
if (pDestBufPtr != pDestBufEnd)

View File

@@ -29,14 +29,6 @@
#define RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER 0xFFFD
inline bool ImplIsNoncharacter(sal_uInt32 nUtf32)
{
return (nUtf32 >= 0xFDD0 && nUtf32 <= 0xFDEF)
|| (nUtf32 & 0xFFFF) >= 0xFFFE
|| !rtl::isUnicodeCodePoint(nUtf32);
}
// All code points that are noncharacters, as of Unicode 3.1.1.
bool ImplIsControlOrFormat(sal_uInt32 nUtf32);
inline bool ImplIsHighSurrogate(sal_uInt32 nUtf32)