ExpatXmlParser.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 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 "ExpatXmlParser.h"
  36. #include <cstdio>
  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 splitNsName(const char** localname, const char** nsUri, const char* src)
  47. {
  48. const char* sep = strchr(src, '\t');
  49. if (sep) {
  50. *localname = sep + 1;
  51. size_t nsUriLen = sep - src;
  52. auto temp = new char[nsUriLen + 1];
  53. memcpy(temp, src, nsUriLen);
  54. temp[nsUriLen] = '\0';
  55. *nsUri = temp;
  56. }
  57. else {
  58. *localname = src;
  59. }
  60. }
  61. } // namespace
  62. namespace {
  63. void mlStartElement(void* userData, const char* nsName, const char** attrs)
  64. {
  65. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  66. std::vector<XmlAttr> xmlAttrs;
  67. if (attrs) {
  68. const char** p = attrs;
  69. while (*p) {
  70. XmlAttr xa;
  71. const char* attrNsName = *p++;
  72. if (!*p) {
  73. break;
  74. }
  75. splitNsName(&xa.localname, &xa.nsUri, attrNsName);
  76. const char* value = *p++;
  77. xa.value = value;
  78. xa.valueLength = strlen(value);
  79. xmlAttrs.push_back(xa);
  80. }
  81. }
  82. const char* localname = nullptr;
  83. const char* prefix = nullptr;
  84. const char* nsUri = nullptr;
  85. splitNsName(&localname, &nsUri, nsName);
  86. sd->psm->beginElement(localname, prefix, nsUri, xmlAttrs);
  87. delete[] nsUri;
  88. for (auto& a : xmlAttrs) {
  89. delete[] a.nsUri;
  90. }
  91. if (sd->psm->needsCharactersBuffering()) {
  92. sd->charactersStack.push_front(A2STR::NIL);
  93. }
  94. }
  95. } // namespace
  96. namespace {
  97. void mlEndElement(void* userData, const char* nsName)
  98. {
  99. const char* localname = nullptr;
  100. const char* prefix = nullptr;
  101. const char* nsUri = nullptr;
  102. splitNsName(&localname, &nsUri, nsName);
  103. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  104. std::string characters;
  105. if (sd->psm->needsCharactersBuffering()) {
  106. characters = std::move(sd->charactersStack.front());
  107. sd->charactersStack.pop_front();
  108. }
  109. sd->psm->endElement(localname, prefix, nsUri, std::move(characters));
  110. delete[] nsUri;
  111. }
  112. } // namespace
  113. namespace {
  114. void mlCharacters(void* userData, const char* ch, int len)
  115. {
  116. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  117. if (sd->psm->needsCharactersBuffering()) {
  118. sd->charactersStack.front().append(&ch[0], &ch[len]);
  119. }
  120. }
  121. } // namespace
  122. namespace {
  123. void setupParser(XML_Parser parser, SessionData* sd)
  124. {
  125. XML_SetUserData(parser, sd);
  126. XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
  127. XML_SetCharacterDataHandler(parser, &mlCharacters);
  128. }
  129. } // namespace
  130. XmlParser::XmlParser(ParserStateMachine* psm)
  131. : psm_(psm),
  132. sessionData_(psm_),
  133. ctx_(XML_ParserCreateNS(nullptr, static_cast<const XML_Char>('\t'))),
  134. lastError_(0)
  135. {
  136. setupParser(ctx_, &sessionData_);
  137. }
  138. XmlParser::~XmlParser() { XML_ParserFree(ctx_); }
  139. ssize_t XmlParser::parseUpdate(const char* data, size_t size)
  140. {
  141. if (lastError_ != 0) {
  142. return lastError_;
  143. }
  144. XML_Status rv = XML_Parse(ctx_, data, size, 0);
  145. if (rv == XML_STATUS_ERROR) {
  146. return lastError_ = ERR_XML_PARSE;
  147. }
  148. else {
  149. return size;
  150. }
  151. }
  152. ssize_t XmlParser::parseFinal(const char* data, size_t size)
  153. {
  154. if (lastError_ != 0) {
  155. return lastError_;
  156. }
  157. XML_Status rv = XML_Parse(ctx_, data, size, 1);
  158. if (rv == XML_STATUS_ERROR) {
  159. return lastError_ = ERR_XML_PARSE;
  160. }
  161. else {
  162. return size;
  163. }
  164. }
  165. int XmlParser::reset()
  166. {
  167. psm_->reset();
  168. sessionData_.reset();
  169. XML_Bool rv = XML_ParserReset(ctx_, nullptr);
  170. if (rv == XML_FALSE) {
  171. return lastError_ = ERR_RESET;
  172. }
  173. else {
  174. setupParser(ctx_, &sessionData_);
  175. return 0;
  176. }
  177. }
  178. } // namespace xml
  179. } // namespace aria2