Xml2XmlParser.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Xml2XmlParser.h"
  36. #include <cassert>
  37. #include <cstring>
  38. #include "a2io.h"
  39. #include "ParserStateMachine.h"
  40. #include "A2STR.h"
  41. #include "a2functional.h"
  42. #include "XmlAttr.h"
  43. namespace aria2 {
  44. namespace xml {
  45. namespace {
  46. void mlStartElement(void* userData, const xmlChar* localname,
  47. const xmlChar* prefix, const xmlChar* nsUri,
  48. int numNamespaces, const xmlChar** namespaces, int numAttrs,
  49. int numDefaulted, const xmlChar** attrs)
  50. {
  51. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  52. std::vector<XmlAttr> xmlAttrs;
  53. const char** pattrs = reinterpret_cast<const char**>(attrs);
  54. for (size_t i = 0, max = numAttrs * 5; i < max; i += 5) {
  55. XmlAttr xmlAttr;
  56. assert(pattrs[i]);
  57. xmlAttr.localname = pattrs[i];
  58. if (pattrs[i + 1]) {
  59. xmlAttr.prefix = pattrs[i + 1];
  60. }
  61. if (attrs[i + 2]) {
  62. xmlAttr.nsUri = pattrs[i + 2];
  63. }
  64. xmlAttr.value = pattrs[i + 3];
  65. xmlAttr.valueLength = pattrs[i + 4] - xmlAttr.value;
  66. xmlAttrs.push_back(xmlAttr);
  67. }
  68. sd->psm->beginElement(reinterpret_cast<const char*>(localname),
  69. reinterpret_cast<const char*>(prefix),
  70. reinterpret_cast<const char*>(nsUri), xmlAttrs);
  71. if (sd->psm->needsCharactersBuffering()) {
  72. sd->charactersStack.push_front(A2STR::NIL);
  73. }
  74. }
  75. } // namespace
  76. namespace {
  77. void mlEndElement(void* userData, const xmlChar* localname,
  78. const xmlChar* prefix, const xmlChar* nsUri)
  79. {
  80. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  81. std::string characters;
  82. if (sd->psm->needsCharactersBuffering()) {
  83. characters = std::move(sd->charactersStack.front());
  84. sd->charactersStack.pop_front();
  85. }
  86. sd->psm->endElement(reinterpret_cast<const char*>(localname),
  87. reinterpret_cast<const char*>(prefix),
  88. reinterpret_cast<const char*>(nsUri), characters);
  89. }
  90. } // namespace
  91. namespace {
  92. void mlCharacters(void* userData, const xmlChar* ch, int len)
  93. {
  94. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  95. if (sd->psm->needsCharactersBuffering()) {
  96. sd->charactersStack.front().append(&ch[0], &ch[len]);
  97. }
  98. }
  99. } // namespace
  100. namespace {
  101. xmlSAXHandler mySAXHandler = {
  102. nullptr, // internalSubsetSAXFunc
  103. nullptr, // isStandaloneSAXFunc
  104. nullptr, // hasInternalSubsetSAXFunc
  105. nullptr, // hasExternalSubsetSAXFunc
  106. nullptr, // resolveEntitySAXFunc
  107. nullptr, // getEntitySAXFunc
  108. nullptr, // entityDeclSAXFunc
  109. nullptr, // notationDeclSAXFunc
  110. nullptr, // attributeDeclSAXFunc
  111. nullptr, // elementDeclSAXFunc
  112. nullptr, // unparsedEntityDeclSAXFunc
  113. nullptr, // setDocumentLocatorSAXFunc
  114. nullptr, // startDocumentSAXFunc
  115. nullptr, // endDocumentSAXFunc
  116. nullptr, // startElementSAXFunc
  117. nullptr, // endElementSAXFunc
  118. nullptr, // referenceSAXFunc
  119. &mlCharacters, // charactersSAXFunc
  120. nullptr, // ignorableWhitespaceSAXFunc
  121. nullptr, // processingInstructionSAXFunc
  122. nullptr, // commentSAXFunc
  123. nullptr, // warningSAXFunc
  124. nullptr, // errorSAXFunc
  125. nullptr, // fatalErrorSAXFunc
  126. nullptr, // getParameterEntitySAXFunc
  127. nullptr, // cdataBlockSAXFunc
  128. nullptr, // externalSubsetSAXFunc
  129. XML_SAX2_MAGIC, // unsigned int initialized
  130. nullptr, // void * _private
  131. &mlStartElement, // startElementNsSAX2Func
  132. &mlEndElement, // endElementNsSAX2Func
  133. nullptr, // xmlStructuredErrorFunc
  134. };
  135. } // namespace
  136. XmlParser::XmlParser(ParserStateMachine* psm)
  137. : psm_(psm),
  138. sessionData_(psm),
  139. ctx_(xmlCreatePushParserCtxt(&mySAXHandler, &sessionData_, nullptr, 0,
  140. nullptr)),
  141. lastError_(0)
  142. {
  143. }
  144. XmlParser::~XmlParser() { xmlFreeParserCtxt(ctx_); }
  145. ssize_t XmlParser::parseUpdate(const char* data, size_t size)
  146. {
  147. if (lastError_ != 0) {
  148. return lastError_;
  149. }
  150. int rv = xmlParseChunk(ctx_, data, size, 0);
  151. if (rv != 0) {
  152. return lastError_ = ERR_XML_PARSE;
  153. }
  154. else {
  155. return size;
  156. }
  157. }
  158. ssize_t XmlParser::parseFinal(const char* data, size_t size)
  159. {
  160. if (lastError_ != 0) {
  161. return lastError_;
  162. }
  163. int rv = xmlParseChunk(ctx_, data, size, 1);
  164. if (rv != 0) {
  165. return lastError_ = ERR_XML_PARSE;
  166. }
  167. else {
  168. return size;
  169. }
  170. }
  171. int XmlParser::reset()
  172. {
  173. psm_->reset();
  174. sessionData_.reset();
  175. int rv = xmlCtxtResetPush(ctx_, nullptr, 0, nullptr, nullptr);
  176. if (rv != 0) {
  177. return lastError_ = ERR_RESET;
  178. }
  179. else {
  180. return 0;
  181. }
  182. }
  183. } // namespace xml
  184. } // namespace aria2