From 05cec243c28f50bd5ee7a167067501f7bd8db3ff Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 5 May 2014 17:23:21 +0300 Subject: Validate xml downloads, and if failed, use another mirror. --- src/utils/xml.cpp | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/utils/xml.cpp') 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, "