cpp-bencoding
Decoder.h
Go to the documentation of this file.
1 
8 #ifndef BENCODING_DECODER_H
9 #define BENCODING_DECODER_H
10 
11 #include <exception>
12 #include <memory>
13 #include <string>
14 
15 #include "BItem.h"
16 
17 namespace bencoding {
18 
19 class BDictionary;
20 class BInteger;
21 class BList;
22 class BString;
23 
27 class DecodingError: public std::runtime_error {
28 public:
29  explicit DecodingError(const std::string &what);
30 };
31 
41 class Decoder {
42 public:
43  static std::unique_ptr<Decoder> create();
44 
45  std::unique_ptr<BItem> decode(const std::string &data);
46  std::unique_ptr<BItem> decode(std::istream &input);
47 
48 private:
49  Decoder();
50 
51  void readExpectedChar(std::istream &input, char expected_char) const;
52 
55  std::unique_ptr<BDictionary> decodeDictionary(std::istream &input);
56  std::unique_ptr<BDictionary> decodeDictionaryItemsIntoDictionary(
57  std::istream &input);
58  std::shared_ptr<BString> decodeDictionaryKey(std::istream &input);
59  std::unique_ptr<BItem> decodeDictionaryValue(std::istream &input);
61 
64  std::unique_ptr<BInteger> decodeInteger(std::istream &input) const;
65  std::string readEncodedInteger(std::istream &input) const;
66  std::unique_ptr<BInteger> decodeEncodedInteger(
67  const std::string &encodedInteger) const;
69 
72  std::unique_ptr<BList> decodeList(std::istream &input);
73  std::unique_ptr<BList> decodeListItemsIntoList(std::istream &input);
75 
78  std::unique_ptr<BString> decodeString(std::istream &input) const;
79  std::string::size_type readStringLength(std::istream &input) const;
80  std::string readStringOfGivenLength(std::istream &input,
81  std::string::size_type length) const;
83 
84  void validateInputDoesNotContainUndecodedCharacters(std::istream &input);
85 };
86 
89 std::unique_ptr<BItem> decode(const std::string &data);
90 std::unique_ptr<BItem> decode(std::istream &input);
92 
93 } // namespace bencoding
94 
95 #endif
std::unique_ptr< BItem > decodeDictionaryValue(std::istream &input)
Decodes a dictionary value from input.
Definition: Decoder.cpp:160
std::unique_ptr< BInteger > decodeInteger(std::istream &input) const
Decodes an integer from input.
Definition: Decoder.cpp:182
std::unique_ptr< BItem > decode(const std::string &data)
Decodes the given bencoded data and returns them.
Definition: Decoder.cpp:46
std::unique_ptr< BList > decodeList(std::istream &input)
Decodes a list from input.
Definition: Decoder.cpp:233
Decoder of bencoded data.
Definition: Decoder.h:41
std::unique_ptr< BItem > decode(const std::string &data)
Decodes the given bencoded data and returns them.
Definition: Decoder.cpp:325
static std::unique_ptr< Decoder > create()
Creates a new decoder.
Definition: Decoder.cpp:36
std::unique_ptr< BString > decodeString(std::istream &input) const
Decodes a string from input.
Definition: Decoder.cpp:265
Main namespace of the bencoding library.
std::unique_ptr< BInteger > decodeEncodedInteger(const std::string &encodedInteger) const
Decodes the given encoded integer.
Definition: Decoder.cpp:204
Base class for all items (integers, strings, etc.).
void readExpectedChar(std::istream &input, char expected_char) const
Reads expected_char from input and discards it.
Definition: Decoder.cpp:91
std::string readStringOfGivenLength(std::istream &input, std::string::size_type length) const
Reads a string of the given length from input and returns it.
Definition: Decoder.cpp:295
Exception thrown when there is an error during the decoding.
Definition: Decoder.h:27
DecodingError(const std::string &what)
Constructs a new exception with the given message.
Definition: Decoder.cpp:25
std::unique_ptr< BList > decodeListItemsIntoList(std::istream &input)
Decodes items from input, appends them to a list, and returns that list.
Definition: Decoder.cpp:244
std::unique_ptr< BDictionary > decodeDictionaryItemsIntoDictionary(std::istream &input)
Decodes items from input, adds them to a dictionary, and returns that dictionary. ...
Definition: Decoder.cpp:131
std::string::size_type readStringLength(std::istream &input) const
Reads the string length from input, validates it, and returns it.
Definition: Decoder.cpp:275
void validateInputDoesNotContainUndecodedCharacters(std::istream &input)
Throws DecodingError if input has not been completely read.
Definition: Decoder.cpp:311
std::string readEncodedInteger(std::istream &input) const
Reads an encoded integer from input.
Definition: Decoder.cpp:189
std::unique_ptr< BDictionary > decodeDictionary(std::istream &input)
Decodes a dictionary from input.
Definition: Decoder.cpp:120
std::shared_ptr< BString > decodeDictionaryKey(std::istream &input)
Decodes a dictionary key from input.
Definition: Decoder.cpp:145
Decoder()
Constructs a decoder.
Definition: Decoder.cpp:31