bencode2.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "bencode2.h"
  36. #include <sstream>
  37. #include "fmt.h"
  38. #include "DlAbortEx.h"
  39. #include "error_code.h"
  40. #include "BufferedFile.h"
  41. #include "util.h"
  42. #include "ValueBaseBencodeParser.h"
  43. namespace aria2 {
  44. namespace bencode2 {
  45. std::unique_ptr<ValueBase> decode(const unsigned char* data, size_t len)
  46. {
  47. size_t end;
  48. return decode(data, len, end);
  49. }
  50. std::unique_ptr<ValueBase> decode(const std::string& data)
  51. {
  52. size_t end;
  53. return decode(reinterpret_cast<const unsigned char*>(data.c_str()),
  54. data.size(), end);
  55. }
  56. std::unique_ptr<ValueBase> decode(const unsigned char* data, size_t len,
  57. size_t& end)
  58. {
  59. ssize_t error;
  60. bittorrent::ValueBaseBencodeParser parser;
  61. auto res = parser.parseFinal(reinterpret_cast<const char*>(data), len, error);
  62. if (error < 0) {
  63. throw DL_ABORT_EX2(
  64. fmt("Bencode decoding failed: error=%d", static_cast<int>(error)),
  65. error_code::BENCODE_PARSE_ERROR);
  66. }
  67. end = error;
  68. return res;
  69. }
  70. std::string encode(const ValueBase* vlb)
  71. {
  72. class BencodeValueBaseVisitor : public ValueBaseVisitor {
  73. private:
  74. std::ostringstream out_;
  75. public:
  76. virtual void visit(const String& string) CXX11_OVERRIDE
  77. {
  78. const std::string& s = string.s();
  79. out_ << s.size() << ":";
  80. out_.write(s.data(), s.size());
  81. }
  82. virtual void visit(const Integer& integer) CXX11_OVERRIDE
  83. {
  84. out_ << "i" << integer.i() << "e";
  85. }
  86. virtual void visit(const Bool& v) CXX11_OVERRIDE {}
  87. virtual void visit(const Null& v) CXX11_OVERRIDE {}
  88. virtual void visit(const List& list) CXX11_OVERRIDE
  89. {
  90. out_ << "l";
  91. for (const auto& e : list) {
  92. e->accept(*this);
  93. }
  94. out_ << "e";
  95. }
  96. virtual void visit(const Dict& dict) CXX11_OVERRIDE
  97. {
  98. out_ << "d";
  99. for (const auto& e : dict) {
  100. auto& key = e.first;
  101. out_ << key.size() << ":";
  102. out_.write(key.data(), key.size());
  103. e.second->accept(*this);
  104. }
  105. out_ << "e";
  106. }
  107. std::string getResult() const { return out_.str(); }
  108. };
  109. BencodeValueBaseVisitor visitor;
  110. vlb->accept(visitor);
  111. return visitor.getResult();
  112. }
  113. } // namespace bencode2
  114. } // namespace aria2