bencode.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "Util.h"
  40. #include "RecoverableException.h"
  41. namespace aria2 {
  42. namespace bencode {
  43. const BDE BDE::none;
  44. BDE::BDE():_type(TYPE_NONE) {}
  45. BDE::BDE(Integer integer):_type(TYPE_INTEGER),
  46. _integer(new Integer(integer)) {}
  47. BDE::BDE(const std::string& string):_type(TYPE_STRING),
  48. _string(new std::string(string)) {}
  49. BDE::BDE(const char* cstring):_type(TYPE_STRING),
  50. _string(new std::string(cstring)) {}
  51. BDE::BDE(const char* data, size_t length):
  52. _type(TYPE_STRING),
  53. _string(new std::string(&data[0], &data[length])) {}
  54. BDE::BDE(const unsigned char* data, size_t length):
  55. _type(TYPE_STRING),
  56. _string(new std::string(&data[0], &data[length])) {}
  57. BDE BDE::dict()
  58. {
  59. BDE bde;
  60. bde._type = TYPE_DICT;
  61. bde._dict.reset(new Dict());
  62. return bde;
  63. }
  64. BDE BDE::list()
  65. {
  66. BDE bde;
  67. bde._type = TYPE_LIST;
  68. bde._list.reset(new List());
  69. return bde;
  70. }
  71. // Test for Null data
  72. bool BDE::isNone() const
  73. {
  74. return _type == TYPE_NONE;
  75. }
  76. // Integer Interface
  77. bool BDE::isInteger() const
  78. {
  79. return _type == TYPE_INTEGER;
  80. }
  81. BDE::Integer BDE::i() const
  82. {
  83. if(isInteger()) {
  84. return *_integer.get();
  85. } else {
  86. throw RecoverableException("Not Integer");
  87. }
  88. }
  89. // String Interface
  90. bool BDE::isString() const
  91. {
  92. return _type == TYPE_STRING;
  93. }
  94. const std::string& BDE::s() const
  95. {
  96. if(isString()) {
  97. return *_string.get();
  98. } else {
  99. throw RecoverableException("Not String");
  100. }
  101. }
  102. const unsigned char* BDE::uc() const
  103. {
  104. if(isString()) {
  105. return reinterpret_cast<const unsigned char*>(_string->data());
  106. } else {
  107. throw RecoverableException("Not String");
  108. }
  109. }
  110. // Dictionary Interface
  111. bool BDE::isDict() const
  112. {
  113. return _type == TYPE_DICT;
  114. }
  115. BDE& BDE::operator[](const std::string& key)
  116. {
  117. if(isDict()) {
  118. return (*_dict.get())[key];
  119. } else {
  120. throw RecoverableException("Not Dict");
  121. }
  122. }
  123. const BDE& BDE::operator[](const std::string& key) const
  124. {
  125. if(isDict()) {
  126. BDE::Dict::const_iterator i = _dict->find(key);
  127. if(i == _dict->end()) {
  128. return none;
  129. } else {
  130. return (*i).second;
  131. }
  132. } else {
  133. throw RecoverableException("Not Dict");
  134. }
  135. }
  136. bool BDE::containsKey(const std::string& key) const
  137. {
  138. if(isDict()) {
  139. return _dict->find(key) != _dict->end();
  140. } else {
  141. throw RecoverableException("Not Dict");
  142. }
  143. }
  144. void BDE::removeKey(const std::string& key) const
  145. {
  146. if(isDict()) {
  147. _dict->erase(key);
  148. } else {
  149. throw RecoverableException("Not Dict");
  150. }
  151. }
  152. BDE::Dict::iterator BDE::dictBegin()
  153. {
  154. if(isDict()) {
  155. return _dict->begin();
  156. } else {
  157. throw RecoverableException("Not Dict");
  158. }
  159. }
  160. BDE::Dict::const_iterator BDE::dictBegin() const
  161. {
  162. if(isDict()) {
  163. return _dict->begin();
  164. } else {
  165. throw RecoverableException("Not Dict");
  166. }
  167. }
  168. BDE::Dict::iterator BDE::dictEnd()
  169. {
  170. if(isDict()) {
  171. return _dict->end();
  172. } else {
  173. throw RecoverableException("Not Dict");
  174. }
  175. }
  176. BDE::Dict::const_iterator BDE::dictEnd() const
  177. {
  178. if(isDict()) {
  179. return _dict->end();
  180. } else {
  181. throw RecoverableException("Not Dict");
  182. }
  183. }
  184. // List Interface
  185. bool BDE::isList() const
  186. {
  187. return _type == TYPE_LIST;
  188. }
  189. void BDE::append(const BDE& bde)
  190. {
  191. if(isList()) {
  192. _list->push_back(bde);
  193. } else {
  194. throw RecoverableException("Not List");
  195. }
  196. }
  197. void BDE::operator<<(const BDE& bde)
  198. {
  199. if(isList()) {
  200. _list->push_back(bde);
  201. } else {
  202. throw RecoverableException("Not List");
  203. }
  204. }
  205. BDE& BDE::operator[](size_t index)
  206. {
  207. if(isList()) {
  208. return (*_list.get())[index];
  209. } else {
  210. throw RecoverableException("Not List");
  211. }
  212. }
  213. const BDE& BDE::operator[](size_t index) const
  214. {
  215. if(isList()) {
  216. return (*_list.get())[index];
  217. } else {
  218. throw RecoverableException("Not List");
  219. }
  220. }
  221. BDE::List::iterator BDE::listBegin()
  222. {
  223. if(isList()) {
  224. return _list->begin();
  225. } else {
  226. throw RecoverableException("Not List");
  227. }
  228. }
  229. BDE::List::const_iterator BDE::listBegin() const
  230. {
  231. if(isList()) {
  232. return _list->begin();
  233. } else {
  234. throw RecoverableException("Not List");
  235. }
  236. }
  237. BDE::List::iterator BDE::listEnd()
  238. {
  239. if(isList()) {
  240. return _list->end();
  241. } else {
  242. throw RecoverableException("Not List");
  243. }
  244. }
  245. BDE::List::const_iterator BDE::listEnd() const
  246. {
  247. if(isList()) {
  248. return _list->end();
  249. } else {
  250. throw RecoverableException("Not List");
  251. }
  252. }
  253. // Callable from List and Dict
  254. size_t BDE::size() const
  255. {
  256. if(isDict()) {
  257. return _dict->size();
  258. } else if(isList()) {
  259. return _list->size();
  260. } else {
  261. throw RecoverableException("Not Dict nor List");
  262. }
  263. }
  264. // Callable from List and Dict
  265. bool BDE::empty() const
  266. {
  267. if(isDict()) {
  268. return _dict->empty();
  269. } else if(isList()) {
  270. return _list->empty();
  271. } else {
  272. throw RecoverableException("Not Dict nor List");
  273. }
  274. }
  275. static BDE decodeiter(std::istream& ss);
  276. static void checkdelim(std::istream& ss, const char delim = ':')
  277. {
  278. char d;
  279. if(!(ss.get(d) && d == delim)) {
  280. throw RecoverableException
  281. (StringFormat("Delimiter '%c' not found.", delim).str());
  282. }
  283. }
  284. static std::string decoderawstring(std::istream& ss)
  285. {
  286. size_t length;
  287. ss >> length;
  288. if(!ss) {
  289. throw RecoverableException("A positive integer expected but none found.");
  290. }
  291. // TODO check length, it must be less than or equal to INT_MAX
  292. checkdelim(ss);
  293. char* buf = new char[length];
  294. ss.read(buf, length);
  295. std::string str(&buf[0], &buf[length]);
  296. delete [] buf;
  297. if(ss.gcount() != static_cast<int>(length)) {
  298. throw RecoverableException
  299. (StringFormat("Expected %lu bytes of data, but only %d read.",
  300. static_cast<unsigned long>(length), ss.gcount()).str());
  301. }
  302. return str;
  303. }
  304. static BDE decodestring(std::istream& ss)
  305. {
  306. return BDE(decoderawstring(ss));
  307. }
  308. static BDE decodeinteger(std::istream& ss)
  309. {
  310. BDE::Integer integer;
  311. ss >> integer;
  312. if(!ss) {
  313. throw RecoverableException("Integer expected but none found");
  314. }
  315. checkdelim(ss, 'e');
  316. return BDE(integer);
  317. }
  318. static BDE decodedict(std::istream& ss)
  319. {
  320. BDE dict = BDE::dict();
  321. char c;
  322. while(ss.get(c)) {
  323. if(c == 'e') {
  324. return dict;
  325. } else {
  326. ss.unget();
  327. std::string key = decoderawstring(ss);
  328. dict[key] = decodeiter(ss);
  329. }
  330. }
  331. throw RecoverableException("Unexpected EOF in dict context. 'e' expected.");
  332. }
  333. static BDE decodelist(std::istream& ss)
  334. {
  335. BDE list = BDE::list();
  336. char c;
  337. while(ss.get(c)) {
  338. if(c == 'e') {
  339. return list;
  340. } else {
  341. ss.unget();
  342. list << decodeiter(ss);
  343. }
  344. }
  345. throw RecoverableException("Unexpected EOF in list context. 'e' expected.");
  346. }
  347. static BDE decodeiter(std::istream& ss)
  348. {
  349. char c;
  350. if(!ss.get(c)) {
  351. throw RecoverableException("Unexpected EOF in term context."
  352. " 'd', 'l', 'i' or digit is expected.");
  353. }
  354. if(c == 'd') {
  355. return decodedict(ss);
  356. } else if(c == 'l') {
  357. return decodelist(ss);
  358. } else if(c == 'i') {
  359. return decodeinteger(ss);
  360. } else {
  361. ss.unget();
  362. return decodestring(ss);
  363. }
  364. }
  365. BDE decode(std::istream& in)
  366. {
  367. return decodeiter(in);
  368. }
  369. BDE decode(const std::string& s)
  370. {
  371. if(s.empty()) {
  372. return BDE::none;
  373. }
  374. std::istringstream ss(s);
  375. return decodeiter(ss);
  376. }
  377. BDE decode(const unsigned char* data, size_t length)
  378. {
  379. return decode(std::string(&data[0], &data[length]));
  380. }
  381. BDE decodeFromFile(const std::string& filename)
  382. {
  383. std::ifstream f(filename.c_str(), std::ios::binary);
  384. if(f) {
  385. return decode(f);
  386. } else {
  387. throw RecoverableException
  388. (StringFormat("Cannot open file '%s'.", filename.c_str()).str());
  389. }
  390. }
  391. static void encodeIter(std::ostream& o, const BDE& bde)
  392. {
  393. if(bde.isInteger()) {
  394. o << "i" << bde.i() << "e";
  395. } else if(bde.isString()) {
  396. const std::string& s = bde.s();
  397. o << s.size() << ":";
  398. o.write(s.data(), s.size());
  399. } else if(bde.isDict()) {
  400. o << "d";
  401. for(BDE::Dict::const_iterator i = bde.dictBegin(); i != bde.dictEnd(); ++i){
  402. const std::string& key = (*i).first;
  403. o << key.size() << ":";
  404. o.write(key.data(), key.size());
  405. encodeIter(o, (*i).second);
  406. }
  407. o << "e";
  408. } else if(bde.isList()) {
  409. o << "l";
  410. for(BDE::List::const_iterator i = bde.listBegin(); i != bde.listEnd(); ++i){
  411. encodeIter(o, *i);
  412. }
  413. o << "e";
  414. }
  415. }
  416. std::string encode(const BDE& bde)
  417. {
  418. std::ostringstream ss;
  419. encodeIter(ss, bde);
  420. return ss.str();
  421. }
  422. } // namespace bencode
  423. } // namespace aria2