| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <istream> |
| 26 |
|
| 27 |
#include <qglobal.h> |
| 28 |
#if QT_VERSION < 0x060000 |
| 29 |
# include <QTextCodec> |
| 30 |
#else |
| 31 |
# include <QByteArray> |
| 32 |
# include <QStringDecoder> |
| 33 |
# include <QStringEncoder> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "InputSource.h" |
| 37 |
#include "XMLTools.h" |
| 38 |
|
| 39 |
#ifndef XERCES_CPP_NAMESPACE_BEGIN |
| 40 |
# define XERCES_CPP_NAMESPACE_QUALIFIER |
| 41 |
using namespace XERCES_CPP_NAMESPACE; |
| 42 |
#else |
| 43 |
XERCES_CPP_NAMESPACE_USE |
| 44 |
#endif |
| 45 |
|
| 46 |
using namespace Base; |
| 47 |
using namespace std; |
| 48 |
|
| 49 |
|
| 50 |
// --------------------------------------------------------------------------- |
| 51 |
// StdInputStream: Constructors and Destructor |
| 52 |
// --------------------------------------------------------------------------- |
| 53 |
|
| 54 |
#if QT_VERSION < 0x060000 |
| 55 |
struct StdInputStream::TextCodec |
| 56 |
{ |
| 57 |
QTextCodec::ConverterState state; |
| 58 |
TextCodec() |
| 59 |
{ |
| 60 |
state.flags |= QTextCodec::IgnoreHeader; |
| 61 |
state.flags |= QTextCodec::ConvertInvalidToNull; |
| 62 |
} |
| 63 |
|
| 64 |
void validateBytes(XMLByte* const toFill, std::streamsize len) |
| 65 |
{ |
| 66 |
QTextCodec* textCodec = QTextCodec::codecForName("UTF-8"); |
| 67 |
if (textCodec) { |
| 68 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 69 |
const QString text |
| 70 |
= textCodec->toUnicode(reinterpret_cast<char*>(toFill), static_cast<int>(len), &state); |
| 71 |
if (state.invalidChars > 0) { |
| 72 |
// In case invalid characters were found decode back to 'utf-8' and replace |
| 73 |
// them with '?' |
| 74 |
// First, Qt replaces invalid characters with '\0' (see ConvertInvalidToNull) |
| 75 |
// but Xerces doesn't like this because it handles this as termination. Thus, |
| 76 |
// we have to go through the array and replace '\0' with '?'. |
| 77 |
std::streamsize pos = 0; |
| 78 |
QByteArray ba = textCodec->fromUnicode(text); |
| 79 |
for (int i = 0; i < ba.length(); i++, pos++) { |
| 80 |
if (pos < len && ba[i] == '\0') { |
| 81 |
toFill[i] = '?'; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
}; |
| 88 |
#else |
| 89 |
struct StdInputStream::TextCodec |
| 90 |
{ |
| 91 |
void validateBytes(XMLByte* const toFill, std::streamsize len) |
| 92 |
{ |
| 93 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 94 |
QByteArray encodedString(reinterpret_cast<char*>(toFill), static_cast<int>(len)); |
| 95 |
auto toUtf16 = QStringDecoder(QStringDecoder::Utf8); |
| 96 |
QString text = toUtf16(encodedString); |
| 97 |
if (toUtf16.hasError()) { |
| 98 |
// In case invalid characters were found decode back to 'utf-8' and replace |
| 99 |
// them with '?' |
| 100 |
// First, Qt replaces invalid characters with '\0' |
| 101 |
// but Xerces doesn't like this because it handles this as termination. Thus, |
| 102 |
// we have to go through the array and replace '\0' with '?'. |
| 103 |
std::streamsize pos = 0; |
| 104 |
auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8); |
| 105 |
QByteArray ba = fromUtf16(text); |
| 106 |
for (int i = 0; i < ba.length(); i++, pos++) { |
| 107 |
if (pos < len && ba[i] == '\0') { |
| 108 |
toFill[i] = '?'; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
}; |
| 114 |
#endif |
| 115 |
|
| 116 |
StdInputStream::StdInputStream( |
| 117 |
std::istream& Stream, |
| 118 |
XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const manager |
| 119 |
) |
| 120 |
: stream(Stream) |
| 121 |
, codec(new TextCodec) |
| 122 |
{ |
| 123 |
(void)manager; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
StdInputStream::~StdInputStream() = default; |
| 128 |
|
| 129 |
|
| 130 |
// --------------------------------------------------------------------------- |
| 131 |
// StdInputStream: Implementation of the input stream interface |
| 132 |
// --------------------------------------------------------------------------- |
| 133 |
XMLFilePos StdInputStream::curPos() const |
| 134 |
{ |
| 135 |
return static_cast<XMLFilePos>(stream.tellg()); |
| 136 |
} |
| 137 |
|
| 138 |
XMLSize_t StdInputStream::readBytes(XMLByte* const toFill, const XMLSize_t maxToRead) |
| 139 |
{ |
| 140 |
// |
| 141 |
// Read up to the maximum bytes requested. We return the number |
| 142 |
// actually read. |
| 143 |
// |
| 144 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 145 |
stream.read(reinterpret_cast<char*>(toFill), static_cast<std::streamsize>(maxToRead)); |
| 146 |
std::streamsize len = stream.gcount(); |
| 147 |
|
| 148 |
codec->validateBytes(toFill, len); |
| 149 |
|
| 150 |
return static_cast<XMLSize_t>(len); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
// --------------------------------------------------------------------------- |
| 155 |
// StdInputSource: Constructors and Destructor |
| 156 |
// --------------------------------------------------------------------------- |
| 157 |
StdInputSource::StdInputSource( |
| 158 |
std::istream& Stream, |
| 159 |
const char* filePath, |
| 160 |
XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const manager |
| 161 |
) |
| 162 |
: InputSource(manager) |
| 163 |
, stream(Stream) |
| 164 |
{ |
| 165 |
// we have to set the file name in case an error occurs |
| 166 |
XStr tmpBuf(filePath); |
| 167 |
setSystemId(tmpBuf.unicodeForm()); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
StdInputSource::~StdInputSource() = default; |
| 172 |
|
| 173 |
|
| 174 |
// --------------------------------------------------------------------------- |
| 175 |
// StdInputSource: InputSource interface implementation |
| 176 |
// --------------------------------------------------------------------------- |
| 177 |
BinInputStream* StdInputSource::makeStream() const |
| 178 |
{ |
| 179 |
StdInputStream* retStrm = new StdInputStream(stream /*, getMemoryManager()*/); |
| 180 |
return retStrm; |
| 181 |
} |