Skip to content

Commit c281a54

Browse files
committed
Using xml parser from Qt.
1 parent 61b41c1 commit c281a54

File tree

5 files changed

+61
-3372
lines changed

5 files changed

+61
-3372
lines changed

source/common/update_info.cc

+61-59
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@
2020

2121
#include "base/logging.h"
2222

23-
#include "third_party/rapidxml/rapidxml.hpp"
23+
#include <QXmlStreamReader>
2424

2525
namespace common {
2626

27+
namespace {
28+
29+
static const int kMaxDescriptionLength = 4096;
30+
static const int kMinUrlLength = 10;
31+
static const int kMaxUrlLength = 256;
32+
33+
//--------------------------------------------------------------------------------------------------
34+
QString parseElement(QXmlStreamReader& xml)
35+
{
36+
if (xml.tokenType() != QXmlStreamReader::StartElement)
37+
return QString();
38+
39+
if (xml.readNext() != QXmlStreamReader::Characters)
40+
return QString();
41+
42+
return xml.text().toString();
43+
}
44+
45+
} // namespace
46+
2747
//--------------------------------------------------------------------------------------------------
2848
// static
2949
UpdateInfo UpdateInfo::fromXml(const QByteArray& buffer)
@@ -42,83 +62,65 @@ UpdateInfo UpdateInfo::fromXml(const QByteArray& buffer)
4262
return UpdateInfo();
4363
}
4464

45-
rapidxml::xml_document<> xml;
46-
47-
try
48-
{
49-
xml.parse<rapidxml::parse_default>(const_cast<char*>(buffer.data()));
50-
}
51-
catch (const rapidxml::parse_error& error)
52-
{
53-
LOG(LS_ERROR) << "Invalid XML for update info: " << error.what() << " at "
54-
<< error.where<char>();
55-
return UpdateInfo();
56-
}
65+
UpdateInfo update_info;
5766

58-
rapidxml::xml_node<>* root_node = xml.first_node("update");
59-
if (!root_node)
67+
QXmlStreamReader xml(buffer);
68+
while (!xml.atEnd() && !xml.hasError())
6069
{
61-
LOG(LS_ERROR) << "Node 'update' not found. No available updates";
62-
return UpdateInfo();
63-
}
70+
QXmlStreamReader::TokenType token = xml.readNext();
6471

65-
UpdateInfo update_info;
72+
if (token == QXmlStreamReader::StartDocument)
73+
continue;
6674

67-
for (const rapidxml::xml_node<>* child_node = root_node->first_node();
68-
child_node != nullptr;
69-
child_node = child_node->next_sibling())
70-
{
71-
if (child_node->type() != rapidxml::node_element)
75+
if (token != QXmlStreamReader::StartElement)
7276
continue;
7377

74-
std::string_view name(child_node->name(), child_node->name_size());
78+
if (xml.name() == QLatin1String("update"))
79+
continue;
7580

76-
const rapidxml::xml_node<>* node = child_node->first_node();
77-
if (node && node->type() == rapidxml::node_data)
81+
while (xml.tokenType() != QXmlStreamReader::EndElement)
7882
{
79-
if (name == "version")
83+
if (xml.tokenType() == QXmlStreamReader::StartElement)
8084
{
81-
update_info.version_ = QVersionNumber::fromString(
82-
QString::fromUtf8(node->value(), static_cast<QString::size_type>(node->value_size())));
83-
if (update_info.version_.isNull())
85+
if (xml.name() == QLatin1String("version"))
8486
{
85-
LOG(LS_ERROR) << "Invalid version: " << node->value();
86-
return UpdateInfo();
87+
update_info.version_ = QVersionNumber::fromString(parseElement(xml));
8788
}
88-
}
89-
else if (name == "description")
90-
{
91-
static const size_t kMaxDescriptionLength = 2048;
92-
93-
if (node->value_size() > kMaxDescriptionLength)
89+
else if (xml.name() == QLatin1String("description"))
9490
{
95-
LOG(LS_ERROR) << "Invalid description length: " << node->value_size()
96-
<< " (max: " << kMaxDescriptionLength << ")";
97-
return UpdateInfo();
91+
update_info.description_ = parseElement(xml);
9892
}
99-
100-
update_info.description_ = QString::fromUtf8(
101-
node->value(), static_cast<QString::size_type>(node->value_size()));
102-
}
103-
else if (name == "url")
104-
{
105-
static const size_t kMinUrlLength = 10;
106-
static const size_t kMaxUrlLength = 256;
107-
108-
if (node->value_size() < kMinUrlLength || node->value_size() > kMaxUrlLength)
93+
else if (xml.name() == QLatin1String("url"))
10994
{
110-
LOG(LS_ERROR) << "Invalid URL length: " << node->value_size()
111-
<< " (min: " << kMinUrlLength << " max: " << kMaxUrlLength << ")";
112-
return UpdateInfo();
95+
update_info.url_ = parseElement(xml);
96+
}
97+
else
98+
{
99+
// Unknown field. Ignore.
113100
}
114-
115-
update_info.url_ = QString::fromUtf8(
116-
node->value(), static_cast<QString::size_type>(node->value_size()));
117101
}
102+
103+
xml.readNext();
118104
}
119105
}
120106

121-
update_info.valid_ = true;
107+
if (xml.hasError())
108+
{
109+
LOG(LS_ERROR) << "Error parsing XML: " << xml.errorString();
110+
}
111+
else if (update_info.description_.size() > kMaxDescriptionLength)
112+
{
113+
LOG(LS_ERROR) << "Too many characters in description";
114+
}
115+
else if (update_info.url_.size() < kMinUrlLength || update_info.url_.size() > kMaxUrlLength)
116+
{
117+
LOG(LS_ERROR) << "Incorrect number of characters in URL";
118+
}
119+
else
120+
{
121+
update_info.valid_ = true;
122+
}
123+
122124
return update_info;
123125
}
124126

0 commit comments

Comments
 (0)