diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-05-05 17:23:21 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-05-06 13:06:50 +0300 |
commit | 05cec243c28f50bd5ee7a167067501f7bd8db3ff (patch) | |
tree | b3b4e97b873ebf5e082ee1092a4c33a1ebdd72b8 /src/utils/xml.cpp | |
parent | 25bed1bb7f7dbfa3caa0f239c63dbabd3b0bd50d (diff) | |
download | mv-05cec243c28f50bd5ee7a167067501f7bd8db3ff.tar.gz mv-05cec243c28f50bd5ee7a167067501f7bd8db3ff.tar.bz2 mv-05cec243c28f50bd5ee7a167067501f7bd8db3ff.tar.xz mv-05cec243c28f50bd5ee7a167067501f7bd8db3ff.zip |
Validate xml downloads, and if failed, use another mirror.
Diffstat (limited to 'src/utils/xml.cpp')
-rw-r--r-- | src/utils/xml.cpp | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 3d98fda61..eae3567ef 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -27,6 +27,7 @@ #include "resources/resourcemanager.h" #include "utils/fuzzer.h" +#include "utils/stringutils.h" #include "utils/translation/podict.h" @@ -34,6 +35,11 @@ #include "debug.h" +namespace +{ + bool valid = false; +} // namespace + static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...) #ifdef __GNUC__ #ifdef __OpenBSD__ @@ -61,12 +67,13 @@ static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg, ...) va_end(ap); if (logger) - logger->log1(buf); + logger->log_r("%s", buf); else puts(buf); // Delete temporary buffer delete [] buf; + valid = false; } namespace XML @@ -80,6 +87,7 @@ namespace XML #endif int size = 0; char *data = nullptr; + valid = true; if (useResman) { const ResourceManager *const resman @@ -122,6 +130,7 @@ namespace XML { logger->log("Error loading %s", filename.c_str()); } + mIsValid = valid; } Document::Document(const char *const data, const int size) : @@ -261,4 +270,31 @@ namespace XML xmlCleanupParser(); } + bool Document::validateXml(const std::string &fileName) + { + xmlDocPtr doc = xmlReadFile(fileName.c_str(), + nullptr, XML_PARSE_PEDANTIC); + const bool valid(doc); + xmlFreeDoc(doc); + if (!valid) + return false; + + std::ifstream file; + file.open(fileName.c_str(), std::ios::in); + if (!file.is_open()) + { + file.close(); + return false; + } + char line[101]; + if (!file.getline(line, 100)) + return false; + file.close(); + + const std::string str = line; + if (!strStartWith(str, "<?xml ")) + return false; + + return true; + } } // namespace XML |