20
20
21
21
#include " base/logging.h"
22
22
23
- #include " third_party/rapidxml/rapidxml.hpp "
23
+ #include < QXmlStreamReader >
24
24
25
25
namespace common {
26
26
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
+
27
47
// --------------------------------------------------------------------------------------------------
28
48
// static
29
49
UpdateInfo UpdateInfo::fromXml (const QByteArray& buffer)
@@ -42,83 +62,65 @@ UpdateInfo UpdateInfo::fromXml(const QByteArray& buffer)
42
62
return UpdateInfo ();
43
63
}
44
64
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;
57
66
58
- rapidxml::xml_node<>* root_node = xml. first_node ( " update " );
59
- if (!root_node )
67
+ QXmlStreamReader xml (buffer );
68
+ while (!xml. atEnd () && !xml. hasError () )
60
69
{
61
- LOG (LS_ERROR) << " Node 'update' not found. No available updates" ;
62
- return UpdateInfo ();
63
- }
70
+ QXmlStreamReader::TokenType token = xml.readNext ();
64
71
65
- UpdateInfo update_info;
72
+ if (token == QXmlStreamReader::StartDocument)
73
+ continue ;
66
74
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)
72
76
continue ;
73
77
74
- std::string_view name (child_node->name (), child_node->name_size ());
78
+ if (xml.name () == QLatin1String (" update" ))
79
+ continue ;
75
80
76
- const rapidxml::xml_node<>* node = child_node->first_node ();
77
- if (node && node->type () == rapidxml::node_data)
81
+ while (xml.tokenType () != QXmlStreamReader::EndElement)
78
82
{
79
- if (name == " version " )
83
+ if (xml. tokenType () == QXmlStreamReader::StartElement )
80
84
{
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" ))
84
86
{
85
- LOG (LS_ERROR) << " Invalid version: " << node->value ();
86
- return UpdateInfo ();
87
+ update_info.version_ = QVersionNumber::fromString (parseElement (xml));
87
88
}
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" ))
94
90
{
95
- LOG (LS_ERROR) << " Invalid description length: " << node->value_size ()
96
- << " (max: " << kMaxDescriptionLength << " )" ;
97
- return UpdateInfo ();
91
+ update_info.description_ = parseElement (xml);
98
92
}
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" ))
109
94
{
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.
113
100
}
114
-
115
- update_info.url_ = QString::fromUtf8 (
116
- node->value (), static_cast <QString::size_type>(node->value_size ()));
117
101
}
102
+
103
+ xml.readNext ();
118
104
}
119
105
}
120
106
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
+
122
124
return update_info;
123
125
}
124
126
0 commit comments