From 09d8ac653493a0bd16cb69664ca28fbfe3c61bbb Mon Sep 17 00:00:00 2001 From: Edwin Mons Date: Mon, 13 May 2019 14:34:28 +0200 Subject: Add flag to signal final XML data XML parse now takes an optional boolean that will signal that with the data that is fed into the parser, the document should now be complete. This will allow the parser to reject partial documents. Test-Information: Updated unit tests pass. Checked that default behaviour wasn't changed. Tested with various partial and complete documents. Change-Id: Ide7c2e47c49d5667f1febcb23da366e96d0dbc21 diff --git a/Swiften/Parser/ExpatParser.cpp b/Swiften/Parser/ExpatParser.cpp index 8415c42..e4e66f2 100644 --- a/Swiften/Parser/ExpatParser.cpp +++ b/Swiften/Parser/ExpatParser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -82,11 +82,11 @@ ExpatParser::~ExpatParser() { XML_ParserFree(p->parser_); } -bool ExpatParser::parse(const std::string& data) { +bool ExpatParser::parse(const std::string& data, bool finalData) { if (data.size() > std::numeric_limits::max()) { return false; } - bool success = XML_Parse(p->parser_, data.c_str(), static_cast(data.size()), false) == XML_STATUS_OK; + bool success = XML_Parse(p->parser_, data.c_str(), static_cast(data.size()), finalData) == XML_STATUS_OK; /*if (!success) { std::cout << "ERROR: " << XML_ErrorString(XML_GetErrorCode(p->parser_)) << " while parsing " << data << std::endl; }*/ diff --git a/Swiften/Parser/ExpatParser.h b/Swiften/Parser/ExpatParser.h index 12df463..7583339 100644 --- a/Swiften/Parser/ExpatParser.h +++ b/Swiften/Parser/ExpatParser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -19,7 +19,7 @@ namespace Swift { ExpatParser(XMLParserClient* client); ~ExpatParser(); - bool parse(const std::string& data); + bool parse(const std::string& data, bool finalData = false); void stopParser(); diff --git a/Swiften/Parser/LibXMLParser.cpp b/Swiften/Parser/LibXMLParser.cpp index 5bd3737..c9f3a07 100644 --- a/Swiften/Parser/LibXMLParser.cpp +++ b/Swiften/Parser/LibXMLParser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2018 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -94,11 +94,11 @@ LibXMLParser::~LibXMLParser() { } } -bool LibXMLParser::parse(const std::string& data) { +bool LibXMLParser::parse(const std::string& data, bool finalData) { if (data.size() > std::numeric_limits::max()) { return false; } - if (xmlParseChunk(p->context_, data.c_str(), static_cast(data.size()), false) == XML_ERR_OK) { + if (xmlParseChunk(p->context_, data.c_str(), static_cast(data.size()), finalData) == XML_ERR_OK) { return true; } xmlError* error = xmlCtxtGetLastError(p->context_); diff --git a/Swiften/Parser/LibXMLParser.h b/Swiften/Parser/LibXMLParser.h index 9f752ce..a863867 100644 --- a/Swiften/Parser/LibXMLParser.h +++ b/Swiften/Parser/LibXMLParser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -22,7 +22,7 @@ namespace Swift { LibXMLParser(XMLParserClient* client); virtual ~LibXMLParser(); - bool parse(const std::string& data); + bool parse(const std::string& data, bool finalData = false); private: static bool initialized; diff --git a/Swiften/Parser/UnitTest/XMLParserTest.cpp b/Swiften/Parser/UnitTest/XMLParserTest.cpp index b593aa7..9e9012b 100644 --- a/Swiften/Parser/UnitTest/XMLParserTest.cpp +++ b/Swiften/Parser/UnitTest/XMLParserTest.cpp @@ -217,6 +217,15 @@ class XMLParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("iq"), client_.events[1].data); } + void testParse_CompleteDocument() { + ParserType testling(&client_); + + CPPUNIT_ASSERT(!testling.parse("", true)); + CPPUNIT_ASSERT(!testling.parse("foo", true)); + CPPUNIT_ASSERT(testling.parse("foo", true)); + } + void testParse_WhitespaceInAttribute() { ParserType testling(&client_); diff --git a/Swiften/Parser/XMLParser.h b/Swiften/Parser/XMLParser.h index 8a73c3f..ad79b2d 100644 --- a/Swiften/Parser/XMLParser.h +++ b/Swiften/Parser/XMLParser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -18,7 +18,7 @@ namespace Swift { XMLParser(XMLParserClient* client); virtual ~XMLParser(); - virtual bool parse(const std::string& data) = 0; + virtual bool parse(const std::string& data, bool finalData = false) = 0; XMLParserClient* getClient() const { return client_; -- cgit v0.10.2-6-g49f6