1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include <cstring>
#include <fstream>
#include "core/engine.h"
#include "file/xml/xmlcallback.h"
#include "file/xml/xmlpacketreader.h"
#include "file/xml/xmltreeresolver.h"
#include "file/xml/xmltrireader.h"
#include "packet/container.h"
#include "utilities/stringutils.h"
#include "utilities/zstr.h"
namespace regina {
namespace {
/**
* Reads the outermost \<regina ...\> or \<reginadata ...\> XML element.
*/
class ReginaDataReader : public regina::XMLPacketReader {
private:
std::shared_ptr<Container> container;
/**< Sits above the entire packet tree read from file. */
bool isReginaData;
/**< Are we actually reading a \<regina ...\> or
\<reginadata ...\> element? */
std::string version_;
/**< The version of Regina that created this file, or
the empty string if this is not known. */
public:
/**
* Create a new top-level reader.
*/
ReginaDataReader(XMLTreeResolver& resolver) :
XMLPacketReader(resolver, nullptr, false, {}, {}),
container(new Container()), isReginaData(false) {
}
std::shared_ptr<Packet> packetToCommit() override {
if (isReginaData)
return container;
else
return nullptr;
}
const std::string& version() const {
return version_;
}
void startElement(const std::string& n,
const regina::xml::XMLPropertyDict& props,
XMLElementReader*) override {
if (n == "regina" || n == "reginadata") {
isReginaData = true;
auto it = props.find("engine");
if (it != props.end())
version_ = stripWhitespace(it->second);
}
}
};
/**
* The number of characters by which we expect to have finished the
* <reginadata ...> opening tag in an old pre-utf8 XML data stream.
*/
const int regDataOpenBy = 200;
/**
* The size of the XML chunks that are sent through to libxml2 for parsing.
*
* Since we plan to perform surgery on pre-utf8 data streams, the chunk
* size must be large enough to catch the regina engine version in the
* first bite. In particular, we require regChunkSize > regDataOpenBy.
*/
const int regChunkSize = 1024;
}
std::shared_ptr<Packet> open(const char* filename) {
std::ifstream file(filename, std::ios_base::in | std::ios_base::binary);
// We don't test whether the file was opened, since open(std::istream&)
// tests this for us as the first thing it does.
return regina::open(file);
}
std::shared_ptr<Packet> open(std::istream& s) {
// Note: open(const char*) relies on us testing here whether s was
// successfully opened. If anyone removes this test, then they
// should add a corresponding test to open(const char*) instead.
if (! s)
return nullptr;
// We declare buf outside the try-catch block because it is dynamically
// allocated, and so we need to deallocate it if an exception is caught.
char* buf = nullptr;
// The following try-catch block is to catch decompression errors.
try {
zstr::istream in(s); // Can handle both compressed and uncompressed.
XMLTreeResolver resolver;
ReginaDataReader reader(resolver);
regina::XMLCallback callback(reader, std::cerr);
// Instead of using the simpler regina::xml::XMLParser::parse_stream(),
// we split the stream into parseable chunks manually. This allows us
// to perform surgery on pre-utf8 streams and tell the parser that old
// files actually store their data in latin1 (which the old file format
// neglected to specify in the XML prologue).
{
regina::xml::XMLParser parser(callback);
buf = new char[regChunkSize];
int chunkRead;
bool seenFirstChunk = false;
while (callback.state() != XMLCallback::State::Aborted) {
// Read in the next chunk.
try {
for (chunkRead = 0; chunkRead < regChunkSize; ++chunkRead) {
int c = in.get();
if (c == EOF)
break;
buf[chunkRead] = static_cast<char>(c);
}
} catch (const zstr::Exception& e) {
std::cerr << "ERROR: Could not read stream: "
<< e.what() << std::endl;
delete[] buf;
return nullptr;
}
if (chunkRead == 0)
break;
// Parse the chunk that has just been read.
// Interesting things (i.e., surgery) can only happen in the
// first chunk.
if (seenFirstChunk) {
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
seenFirstChunk = true;
// This is the first chunk in the data stream. See if we
// need to perform surgery upon it.
//
// We will be fairly rigid in looking for substrings, since
// we are only trying to match old pre-utf8 data files. If
// future versions of the calculation engine change the
// opening "signature" then this does not matter, since they
// will not need surgery anyway.
//
// If anything looks out of place, just abort the surgery
// and send everything straight through to the XML parser,
// in the hope that either the file is new enough to not
// need surgery (i.e., it uses utf8 already) or it's old but
// only uses plain ASCII.
char tmp = buf[regDataOpenBy];
buf[regDataOpenBy] = 0;
char* start = ::strstr(buf, "<reginadata engine=\"");
if (! start) {
buf[regDataOpenBy] = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
start += 20; /* Length of "<reginadata engine=\"" */
char* finish = ::strchr(start, '"');
if ((! finish) || finish == start) {
// Never found a closing quote, or else the engine version
// string is empty.
buf[regDataOpenBy] = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// We've found the engine version.
buf[regDataOpenBy] = tmp;
tmp = *finish;
*finish = 0;
if (versionUsesUTF8(start)) {
// No need for surgery.
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// Surgery is required.
// Make sure there is somewhere to squeeze in an encoding.
start = ::strstr(buf, "encoding=");
if (start) {
// Someone else seems to have done it already.. *shrug*
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
start = ::strstr(buf, "?>");
if (! start) {
// Can't find the end of the <?xml ...?> opening sequence!
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// Good to go. Parse the chunk with a new encoding squeezed in
// just before the "?>".
*finish = tmp;
parser.parse_chunk(std::string(buf, start - buf));
parser.parse_chunk(" encoding=\"ISO-8859-1\"");
parser.parse_chunk(std::string(start,
chunkRead - (start - buf)));
}
parser.finish();
delete[] buf;
buf = nullptr; // Be safe in case we enter the catch block later.
}
// See if we read anything.
// If so, break it away from the top-level container and return it.
if (auto p = reader.packetToCommit()) {
// Resolve any dangling packet references.
resolver.resolveDelayed();
// As reader is destroyed, it will automatically orphan the
// child packet that we return and delete the old parent container.
return p->firstChild();
} else
return nullptr;
} catch (const zstr::Exception& e) {
std::cerr << "ERROR: Could not open: " << e.what() << std::endl;
delete[] buf;
return nullptr;
}
}
} // namespace regina
|