bencode.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "bencode.h"
  36. #include <fstream>
  37. #include <sstream>
  38. #include "StringFormat.h"
  39. #include "DlAbortEx.h"
  40. namespace aria2 {
  41. namespace bencode {
  42. static BDE decodeiter(std::istream& ss);
  43. static void checkdelim(std::istream& ss, const char delim = ':')
  44. {
  45. char d;
  46. if(!(ss.get(d) && d == delim)) {
  47. throw DL_ABORT_EX
  48. (StringFormat("Delimiter '%c' not found.", delim).str());
  49. }
  50. }
  51. static std::string decoderawstring(std::istream& ss)
  52. {
  53. int length;
  54. ss >> length;
  55. if(!ss || length < 0) {
  56. throw DL_ABORT_EX("A positive integer expected but none found.");
  57. }
  58. // TODO check length, it must be less than or equal to INT_MAX
  59. checkdelim(ss);
  60. char* buf = new char[length];
  61. ss.read(buf, length);
  62. std::string str(&buf[0], &buf[length]);
  63. delete [] buf;
  64. if(ss.gcount() != static_cast<int>(length)) {
  65. throw DL_ABORT_EX
  66. (StringFormat("Expected %lu bytes of data, but only %d read.",
  67. static_cast<unsigned long>(length), ss.gcount()).str());
  68. }
  69. return str;
  70. }
  71. static BDE decodestring(std::istream& ss)
  72. {
  73. return BDE(decoderawstring(ss));
  74. }
  75. static BDE decodeinteger(std::istream& ss)
  76. {
  77. BDE::Integer integer;
  78. ss >> integer;
  79. if(!ss) {
  80. throw DL_ABORT_EX("Integer expected but none found");
  81. }
  82. checkdelim(ss, 'e');
  83. return BDE(integer);
  84. }
  85. static BDE decodedict(std::istream& ss)
  86. {
  87. BDE dict = BDE::dict();
  88. char c;
  89. while(ss.get(c)) {
  90. if(c == 'e') {
  91. return dict;
  92. } else {
  93. ss.unget();
  94. std::string key = decoderawstring(ss);
  95. dict[key] = decodeiter(ss);
  96. }
  97. }
  98. throw DL_ABORT_EX("Unexpected EOF in dict context. 'e' expected.");
  99. }
  100. static BDE decodelist(std::istream& ss)
  101. {
  102. BDE list = BDE::list();
  103. char c;
  104. while(ss.get(c)) {
  105. if(c == 'e') {
  106. return list;
  107. } else {
  108. ss.unget();
  109. list << decodeiter(ss);
  110. }
  111. }
  112. throw DL_ABORT_EX("Unexpected EOF in list context. 'e' expected.");
  113. }
  114. static BDE decodeiter(std::istream& ss)
  115. {
  116. char c;
  117. if(!ss.get(c)) {
  118. throw DL_ABORT_EX("Unexpected EOF in term context."
  119. " 'd', 'l', 'i' or digit is expected.");
  120. }
  121. if(c == 'd') {
  122. return decodedict(ss);
  123. } else if(c == 'l') {
  124. return decodelist(ss);
  125. } else if(c == 'i') {
  126. return decodeinteger(ss);
  127. } else {
  128. ss.unget();
  129. return decodestring(ss);
  130. }
  131. }
  132. BDE decode(std::istream& in)
  133. {
  134. return decodeiter(in);
  135. }
  136. BDE decode(const std::string& s)
  137. {
  138. if(s.empty()) {
  139. return BDE::none;
  140. }
  141. std::istringstream ss(s);
  142. return decodeiter(ss);
  143. }
  144. BDE decode(const unsigned char* data, size_t length)
  145. {
  146. return decode(std::string(&data[0], &data[length]));
  147. }
  148. BDE decodeFromFile(const std::string& filename)
  149. {
  150. std::ifstream f(filename.c_str(), std::ios::binary);
  151. if(f) {
  152. return decode(f);
  153. } else {
  154. throw DL_ABORT_EX
  155. (StringFormat("Cannot open file '%s'.", filename.c_str()).str());
  156. }
  157. }
  158. static void encodeIter(std::ostream& o, const BDE& bde)
  159. {
  160. if(bde.isInteger()) {
  161. o << "i" << bde.i() << "e";
  162. } else if(bde.isString()) {
  163. const std::string& s = bde.s();
  164. o << s.size() << ":";
  165. o.write(s.data(), s.size());
  166. } else if(bde.isDict()) {
  167. o << "d";
  168. for(BDE::Dict::const_iterator i = bde.dictBegin(); i != bde.dictEnd(); ++i){
  169. const std::string& key = (*i).first;
  170. o << key.size() << ":";
  171. o.write(key.data(), key.size());
  172. encodeIter(o, (*i).second);
  173. }
  174. o << "e";
  175. } else if(bde.isList()) {
  176. o << "l";
  177. for(BDE::List::const_iterator i = bde.listBegin(); i != bde.listEnd(); ++i){
  178. encodeIter(o, *i);
  179. }
  180. o << "e";
  181. }
  182. }
  183. std::string encode(const BDE& bde)
  184. {
  185. std::ostringstream ss;
  186. encodeIter(ss, bde);
  187. return ss.str();
  188. }
  189. } // namespace bencode
  190. } // namespace aria2