bencode.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef _D_BENCODE_H_
  36. #define _D_BENCODE_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <map>
  40. #include <deque>
  41. #include <iosfwd>
  42. #include "SharedHandle.h"
  43. #include "RecoverableException.h"
  44. namespace aria2 {
  45. namespace bencode {
  46. class BDE;
  47. class BDE {
  48. public:
  49. typedef std::map<std::string, BDE> Dict;
  50. typedef std::deque<BDE> List;
  51. typedef int64_t Integer;
  52. private:
  53. enum TYPE{
  54. TYPE_NONE,
  55. TYPE_INTEGER,
  56. TYPE_STRING,
  57. TYPE_DICT,
  58. TYPE_LIST,
  59. };
  60. TYPE _type;
  61. SharedHandle<Dict> _dict;
  62. SharedHandle<List> _list;
  63. SharedHandle<std::string> _string;
  64. SharedHandle<Integer> _integer;
  65. public:
  66. BDE() throw();
  67. static BDE dict() throw();
  68. static BDE list() throw();
  69. static const BDE none;
  70. // Test for Null data
  71. // Return true if the type of this object is None.
  72. bool isNone() const throw();
  73. //////////////////////////////////////////////////////////////////////////////
  74. // Integer Interface
  75. BDE(Integer integer) throw();
  76. // Returns true if the type of this object is Integer.
  77. bool isInteger() const throw();
  78. // Returns Integer. Requires this object to be Integer.
  79. Integer i() const throw(RecoverableException);
  80. //////////////////////////////////////////////////////////////////////////////
  81. // String Interface
  82. BDE(const std::string& string) throw();
  83. // Made explicit to avoid ambiguity with BDE(Integer).
  84. explicit BDE(const char* cstring) throw();
  85. BDE(const char* data, size_t length) throw();
  86. BDE(const unsigned char* data, size_t length) throw();
  87. // Returns true if the type of this object is String.
  88. bool isString() const throw();
  89. // Returns std::string. Requires this object to be String
  90. const std::string& s() const throw(RecoverableException);
  91. // Returns std::string.data() casted to unsigned char*.
  92. // Use s().size() to get length.
  93. const unsigned char* uc() const throw(RecoverableException);
  94. //////////////////////////////////////////////////////////////////////////////
  95. // Dictionary Interface
  96. // Returns true if the type of this object is Dict.
  97. bool isDict() const throw();
  98. // Returns the reference to BDE object associated with given key.
  99. // If the key is not found, new pair with that key is created using default
  100. // values, which is then returned. In other words, this is the same behavior
  101. // of std::map's operator[].
  102. // Requires this object to be Dict.
  103. BDE& operator[](const std::string& key) throw(RecoverableException);
  104. // Returns the const reference to BDE ojbect associated with given key.
  105. // If the key is not found, BDE::none is returned.
  106. // Requires this object to be Dict.
  107. const BDE& operator[](const std::string& key) const
  108. throw(RecoverableException);
  109. // Returns true if the given key is found in dict.
  110. // Requires this object to be Dict.
  111. bool containsKey(const std::string& key) const throw(RecoverableException);
  112. // Removes specified key from dict.
  113. // Requires this object to be Dict.
  114. void removeKey(const std::string& key) const throw(RecoverableException);
  115. // Returns a read/write iterator that points to the first pair in the dict.
  116. // Requires this object to be Dict.
  117. Dict::iterator dictBegin() throw(RecoverableException);
  118. // Returns a read/write read-only iterator that points to the first pair in
  119. // the dict.
  120. // Requires this object to be Dict.
  121. Dict::const_iterator dictBegin() const throw(RecoverableException);
  122. // Returns a read/write read-only iterator that points to one past the last
  123. // pair in the dict.
  124. // Requires this object to be Dict.
  125. Dict::iterator dictEnd() throw(RecoverableException);
  126. // Returns a read/write read-only iterator that points to one past the last
  127. // pair in the dict.
  128. // Requires this object to be Dict.
  129. Dict::const_iterator dictEnd() const throw(RecoverableException);
  130. //////////////////////////////////////////////////////////////////////////////
  131. // List Interface
  132. // Returns true if the type of this object is List.
  133. bool isList() const throw();
  134. // Appends given bde to list. Required the type of this object to be List.
  135. void append(const BDE& bde) throw(RecoverableException);
  136. // Alias for append()
  137. void operator<<(const BDE& bde) throw(RecoverableException);
  138. // Returns the reference of the object at the given index. Required this
  139. // object to be List.
  140. BDE& operator[](size_t index) throw(RecoverableException);
  141. // Returns the const reference of the object at the given index.
  142. // Required this object to be List.
  143. const BDE& operator[](size_t index) const throw(RecoverableException);
  144. // Returns a read/write iterator that points to the first object in list.
  145. // Required this object to be List.
  146. List::iterator listBegin() throw(RecoverableException);
  147. // Returns a read/write read-only iterator that points to the first object
  148. // in list. Required this object to be List.
  149. List::const_iterator listBegin() const throw(RecoverableException);
  150. // Returns a read/write iterator that points to the one past the last object
  151. // in list. Required this object to be List.
  152. List::iterator listEnd() throw(RecoverableException);
  153. // Returns a read/write read-only iterator that points to the one past the
  154. // last object in list. Required this object to be List.
  155. List::const_iterator listEnd() const throw(RecoverableException);
  156. // For List type: Returns size of list.
  157. // For Dict type: Returns size of dict.
  158. size_t size() const throw(RecoverableException);
  159. // For List type: Returns true if size of list is 0.
  160. // For Dict type: Returns true if size of dict is 0.
  161. bool empty() const throw(RecoverableException);
  162. };
  163. BDE decode(std::istream& in) throw(RecoverableException);
  164. // Decode the data in s.
  165. BDE decode(const std::string& s) throw(RecoverableException);
  166. BDE decode(const unsigned char* data, size_t length)
  167. throw(RecoverableException);
  168. BDE decodeFromFile(const std::string& filename) throw(RecoverableException);
  169. std::string encode(const BDE& bde) throw();
  170. } // namespace bencode
  171. } // namespace aria2
  172. #endif // _D_BENCODE_H_