summaryrefslogtreecommitdiffstats
path: root/src/corelib/codecs/qicucodec.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/codecs/qicucodec.cpp')
-rw-r--r--src/corelib/codecs/qicucodec.cpp78
1 files changed, 57 insertions, 21 deletions
diff --git a/src/corelib/codecs/qicucodec.cpp b/src/corelib/codecs/qicucodec.cpp
index c1f4eecd52..1a727ebb51 100644
--- a/src/corelib/codecs/qicucodec.cpp
+++ b/src/corelib/codecs/qicucodec.cpp
@@ -1,12 +1,11 @@
/****************************************************************************
**
-** Copyright (C) 2021 The Qt Company Ltd.
+** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:COMM$
-**
+** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
@@ -15,25 +14,26 @@
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
-** $QT_END_LICENSE$
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
+** $QT_END_LICENSE$
**
****************************************************************************/
@@ -565,6 +565,32 @@ QIcuCodec::~QIcuCodec()
{
}
+/*!
+ \internal
+
+ Custom callback for the ICU from Unicode conversion. It's invoked when the
+ conversion from Unicode detects illegal or unrecognized character.
+
+ Assumes that context contains a pointer to QTextCodec::ConverterState
+ structure. Updates its invalid characters count and calls a default
+ callback, that replaces the invalid characters properly.
+*/
+static void customFromUnicodeSubstitutionCallback(const void *context,
+ UConverterFromUnicodeArgs *fromUArgs,
+ const UChar *codeUnits,
+ int32_t length,
+ UChar32 codePoint,
+ UConverterCallbackReason reason,
+ UErrorCode *err)
+{
+ auto *state = reinterpret_cast<QTextCodec::ConverterState *>(const_cast<void *>(context));
+ if (state)
+ state->invalidChars++;
+ // Call the default callback that replaces all illegal or unrecognized
+ // sequences with the substitute string
+ UCNV_FROM_U_CALLBACK_SUBSTITUTE(nullptr, fromUArgs, codeUnits, length, codePoint, reason, err);
+}
+
UConverter *QIcuCodec::getConverter(QTextCodec::ConverterState *state) const
{
UConverter *conv = nullptr;
@@ -577,8 +603,18 @@ UConverter *QIcuCodec::getConverter(QTextCodec::ConverterState *state) const
state->d = ucnv_open(m_name, &error);
ucnv_setSubstChars(static_cast<UConverter *>(state->d),
state->flags & QTextCodec::ConvertInvalidToNull ? "\0" : "?", 1, &error);
- if (U_FAILURE(error))
+ if (!U_FAILURE(error)) {
+ error = U_ZERO_ERROR;
+ ucnv_setFromUCallBack(static_cast<UConverter *>(state->d),
+ customFromUnicodeSubstitutionCallback, state, nullptr,
+ nullptr, &error);
+ if (U_FAILURE(error)) {
+ qDebug("getConverter(state) failed to install custom callback. "
+ "canEncode() may report incorrect results.");
+ }
+ } else {
qDebug("getConverter(state) ucnv_open failed %s %s", m_name, u_errorName(error));
+ }
}
conv = static_cast<UConverter *>(state->d);
}