| 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 <algorithm> |
| 26 |
#include <array> |
| 27 |
#include <cassert> |
| 28 |
#include <codecvt> |
| 29 |
#include <locale> |
| 30 |
|
| 31 |
#include <zipios++/zipinputstream.h> |
| 32 |
|
| 33 |
#include "Exception.h" |
| 34 |
#include "Reader.h" |
| 35 |
#include "Writer.h" |
| 36 |
|
| 37 |
/// Here the FreeCAD includes sorted by Base,App,Gui...... |
| 38 |
#include "Persistence.h" |
| 39 |
|
| 40 |
#include <QChar> |
| 41 |
#include <QVector> |
| 42 |
#include <QByteArray> |
| 43 |
|
| 44 |
|
| 45 |
using namespace Base; |
| 46 |
|
| 47 |
TYPESYSTEM_SOURCE_ABSTRACT(Base::Persistence, Base::BaseClass) |
| 48 |
|
| 49 |
|
| 50 |
//************************************************************************** |
| 51 |
// Construction/Destruction |
| 52 |
|
| 53 |
|
| 54 |
//************************************************************************** |
| 55 |
// separator for other implementation aspects |
| 56 |
|
| 57 |
unsigned int Persistence::getMemSize() const |
| 58 |
{ |
| 59 |
// you have to implement this method in all descending classes! |
| 60 |
assert(0); |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
void Persistence::Save(Writer& /*writer*/) const |
| 65 |
{ |
| 66 |
// you have to implement this method in all descending classes! |
| 67 |
assert(0); |
| 68 |
} |
| 69 |
|
| 70 |
void Persistence::Restore(XMLReader& /*reader*/) |
| 71 |
{ |
| 72 |
// you have to implement this method in all descending classes! |
| 73 |
assert(0); |
| 74 |
} |
| 75 |
|
| 76 |
void Persistence::SaveDocFile(Writer& /*writer*/) const |
| 77 |
{} |
| 78 |
|
| 79 |
void Persistence::RestoreDocFile(Reader& /*reader*/) |
| 80 |
{} |
| 81 |
|
| 82 |
std::string Persistence::encodeAttribute(const std::string& str) |
| 83 |
{ |
| 84 |
std::string tmp; |
| 85 |
for (char it : str) { |
| 86 |
switch (it) { |
| 87 |
case '<': |
| 88 |
tmp += "<"; |
| 89 |
break; |
| 90 |
case '\"': |
| 91 |
tmp += """; |
| 92 |
break; |
| 93 |
case '\'': |
| 94 |
tmp += "'"; |
| 95 |
break; |
| 96 |
case '&': |
| 97 |
tmp += "&"; |
| 98 |
break; |
| 99 |
case '>': |
| 100 |
tmp += ">"; |
| 101 |
break; |
| 102 |
case '\r': |
| 103 |
tmp += " "; |
| 104 |
break; |
| 105 |
case '\n': |
| 106 |
tmp += " "; |
| 107 |
break; |
| 108 |
case '\t': |
| 109 |
tmp += "	"; |
| 110 |
break; |
| 111 |
default: |
| 112 |
tmp += it; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return tmp; |
| 118 |
} |
| 119 |
|
| 120 |
// clang-format off |
| 121 |
// https://www.w3.org/TR/xml/#charsets |
| 122 |
// Nominally allowed ranges |
| 123 |
static constexpr std::array<std::pair<char32_t, char32_t>, 6> validRanges {{ |
| 124 |
{0x9, 0x9}, // TAB -- explicitly allowed |
| 125 |
{0xA, 0xA}, // LF -- explicitly allowed |
| 126 |
{0xD, 0xD}, // CR -- explicitly allowed |
| 127 |
{0x20, 0xD7FF}, |
| 128 |
{0xE000, 0xFFFD}, |
| 129 |
{0x10000, 0x10FFFF} |
| 130 |
}}; |
| 131 |
static constexpr std::array<std::pair<char32_t, char32_t>, 19> discouragedRanges {{ |
| 132 |
{0x7F, 0x84}, |
| 133 |
{0x86, 0x9F}, |
| 134 |
{0xFDD0, 0xFDEF}, |
| 135 |
{0x1FFFE, 0x1FFFF}, |
| 136 |
{0x2FFFE, 0x2FFFF}, |
| 137 |
{0x3FFFE, 0x3FFFF}, |
| 138 |
{0x4FFFE, 0x4FFFF}, |
| 139 |
{0x5FFFE, 0x5FFFF}, |
| 140 |
{0x6FFFE, 0x6FFFF}, |
| 141 |
{0x7FFFE, 0x7FFFF}, |
| 142 |
{0x8FFFE, 0x8FFFF}, |
| 143 |
{0x9FFFE, 0x9FFFF}, |
| 144 |
{0xAFFFE, 0xAFFFF}, |
| 145 |
{0xBFFFE, 0xBFFFF}, |
| 146 |
{0xCFFFE, 0xCFFFF}, |
| 147 |
{0xDFFFE, 0xDFFFF}, |
| 148 |
{0xEFFFE, 0xEFFFF}, |
| 149 |
{0xFFFFE, 0xFFFFF}, |
| 150 |
{0x10FFFE, 0x10FFFF} |
| 151 |
}}; |
| 152 |
// clang-format on |
| 153 |
|
| 154 |
/*! |
| 155 |
* In XML not all valid Unicode characters are allowed. Replace all |
| 156 |
* disallowed characters with '_' |
| 157 |
*/ |
| 158 |
std::string Persistence::validateXMLString(const std::string& str) |
| 159 |
{ |
| 160 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
| 161 |
// In newer Qt we cannot use QString::toUcs4, so we have to do it the long way... |
| 162 |
|
| 163 |
const QString input = QString::fromUtf8(str); |
| 164 |
QString output; |
| 165 |
output.reserve(input.size()); |
| 166 |
|
| 167 |
for (auto it = input.cbegin(); it != input.cend();) { |
| 168 |
const auto cp = static_cast<char32_t>(it->unicode()); |
| 169 |
const uint ch = it->unicode(); |
| 170 |
++it; |
| 171 |
|
| 172 |
if (QChar::isHighSurrogate(ch) && it != input.cend() |
| 173 |
&& QChar::isLowSurrogate(static_cast<char32_t>(it->unicode()))) { |
| 174 |
// We are outside the "Basic Multilingual Plane (BMP)" (directly storable in a UTF-16 |
| 175 |
// char, which is what QString uses internally). So now we have to use *two* chars, |
| 176 |
// combine them into one for our check, and then run the validity check for XML output. |
| 177 |
const uint low = it->unicode(); |
| 178 |
++it; |
| 179 |
const char32_t full = QChar::surrogateToUcs4(ch, low); |
| 180 |
const bool valid = std::ranges::any_of(validRanges, [full](const auto& r) { |
| 181 |
return full >= r.first && full <= r.second; |
| 182 |
}); |
| 183 |
const bool discouraged = std::ranges::any_of(discouragedRanges, [full](const auto& r) { |
| 184 |
return full >= r.first && full <= r.second; |
| 185 |
}); |
| 186 |
output.append((valid && !discouraged) ? QChar::fromUcs4(full) : QChar::fromUcs4('_')); |
| 187 |
} |
| 188 |
else { |
| 189 |
// The character fits into 16 bytes, it can be checked directly |
| 190 |
const bool valid = std::ranges::any_of(validRanges, [cp](const auto& r) { |
| 191 |
return cp >= r.first && cp <= r.second; |
| 192 |
}); |
| 193 |
const bool discouraged = std::ranges::any_of(discouragedRanges, [cp](const auto& r) { |
| 194 |
return cp >= r.first && cp <= r.second; |
| 195 |
}); |
| 196 |
output.append((valid && !discouraged) ? QChar(ch) : QChar::fromUcs2('_')); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
const QByteArray utf8 = output.toUtf8(); |
| 201 |
return {utf8.constData(), static_cast<size_t>(utf8.size())}; |
| 202 |
#else |
| 203 |
// In older Qt we can directly use QString::toUcs4, which makes for a bit simpler code |
| 204 |
const QString input = QString::fromStdString(str); |
| 205 |
const QVector<uint> ucs4 = input.toUcs4(); |
| 206 |
|
| 207 |
QVector<uint> filtered; |
| 208 |
filtered.reserve(ucs4.size()); |
| 209 |
|
| 210 |
for (uint cp : ucs4) { |
| 211 |
const char32_t c32 = static_cast<char32_t>(cp); |
| 212 |
const bool ok = std::ranges::any_of(validRanges, [c32](const auto& r) { |
| 213 |
return c32 >= r.first && c32 <= r.second; |
| 214 |
}); |
| 215 |
const bool discouraged = std::ranges::any_of(discouragedRanges, [c32](const auto& r) { |
| 216 |
return c32 >= r.first && c32 <= r.second; |
| 217 |
}); |
| 218 |
filtered.push_back((ok && !discouraged) ? cp : static_cast<uint>(U'_')); |
| 219 |
} |
| 220 |
|
| 221 |
const QString output = QString::fromUcs4(filtered.constData(), filtered.size()); |
| 222 |
const QByteArray utf8 = output.toUtf8(); |
| 223 |
return {utf8.constData(), static_cast<size_t>(utf8.size())}; |
| 224 |
#endif |
| 225 |
} |
| 226 |
|
| 227 |
void Persistence::dumpToStream(std::ostream& stream, int compression) |
| 228 |
{ |
| 229 |
// we need to close the zipstream to get a good result, the only way to do this is to delete the |
| 230 |
// ZipWriter. Hence the scope... |
| 231 |
{ |
| 232 |
// create the writer |
| 233 |
Base::ZipWriter writer(stream); |
| 234 |
writer.setLevel(compression); |
| 235 |
writer.putNextEntry("Persistence.xml"); |
| 236 |
writer.setMode("BinaryBrep"); |
| 237 |
|
| 238 |
// save the content (we need to encapsulate it with xml tags to be able to read single |
| 239 |
// element xmls like happen for properties) |
| 240 |
writer.Stream() << "<Content>" << std::endl; |
| 241 |
Save(writer); |
| 242 |
writer.Stream() << "</Content>"; |
| 243 |
writer.writeFiles(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void Persistence::restoreFromStream(std::istream& stream) |
| 248 |
{ |
| 249 |
zipios::ZipInputStream zipstream(stream); |
| 250 |
Base::XMLReader reader("", zipstream); |
| 251 |
|
| 252 |
if (!reader.isValid()) { |
| 253 |
throw Base::ValueError("Unable to construct reader"); |
| 254 |
} |
| 255 |
|
| 256 |
reader.readElement("Content"); |
| 257 |
Restore(reader); |
| 258 |
reader.readFiles(zipstream); |
| 259 |
restoreFinished(); |
| 260 |
} |