cpp-bencoding
Public Member Functions | Static Public Member Functions | Private Member Functions | List of all members
bencoding::Decoder Class Reference

Decoder of bencoded data. More...

#include <Decoder.h>

Public Member Functions

std::unique_ptr< BItemdecode (const std::string &data)
 Decodes the given bencoded data and returns them. More...
 
std::unique_ptr< BItemdecode (std::istream &input)
 Reads the data from the given input, decodes them and returns them. More...
 

Static Public Member Functions

static std::unique_ptr< Decodercreate ()
 Creates a new decoder. More...
 

Private Member Functions

 Decoder ()
 Constructs a decoder. More...
 
void readExpectedChar (std::istream &input, char expected_char) const
 Reads expected_char from input and discards it. More...
 
void validateInputDoesNotContainUndecodedCharacters (std::istream &input)
 Throws DecodingError if input has not been completely read. More...
 
Integer Decoding
std::unique_ptr< BDictionarydecodeDictionary (std::istream &input)
 Decodes a dictionary from input. More...
 
std::unique_ptr< BDictionarydecodeDictionaryItemsIntoDictionary (std::istream &input)
 Decodes items from input, adds them to a dictionary, and returns that dictionary. More...
 
std::shared_ptr< BStringdecodeDictionaryKey (std::istream &input)
 Decodes a dictionary key from input. More...
 
std::unique_ptr< BItemdecodeDictionaryValue (std::istream &input)
 Decodes a dictionary value from input. More...
 
std::unique_ptr< BIntegerdecodeInteger (std::istream &input) const
 Decodes an integer from input. More...
 
std::string readEncodedInteger (std::istream &input) const
 Reads an encoded integer from input. More...
 
std::unique_ptr< BIntegerdecodeEncodedInteger (const std::string &encodedInteger) const
 Decodes the given encoded integer. More...
 
List Decoding
std::unique_ptr< BListdecodeList (std::istream &input)
 Decodes a list from input. More...
 
std::unique_ptr< BListdecodeListItemsIntoList (std::istream &input)
 Decodes items from input, appends them to a list, and returns that list. More...
 
String Decoding
std::unique_ptr< BStringdecodeString (std::istream &input) const
 Decodes a string from input. More...
 
std::string::size_type readStringLength (std::istream &input) const
 Reads the string length from input, validates it, and returns it. More...
 
std::string readStringOfGivenLength (std::istream &input, std::string::size_type length) const
 Reads a string of the given length from input and returns it. More...
 

Detailed Description

Decoder of bencoded data.

The format is based on the BitTorrent specification.

Use create() to create instances.

Constructor & Destructor Documentation

◆ Decoder()

bencoding::Decoder::Decoder ( )
private

Constructs a decoder.

Member Function Documentation

◆ create()

std::unique_ptr< Decoder > bencoding::Decoder::create ( )
static

Creates a new decoder.

◆ decode() [1/2]

std::unique_ptr< BItem > bencoding::Decoder::decode ( const std::string &  data)

Decodes the given bencoded data and returns them.

If there are some characters left after the decoded data, this function throws DecodingError.

◆ decode() [2/2]

std::unique_ptr< BItem > bencoding::Decoder::decode ( std::istream &  input)

Reads the data from the given input, decodes them and returns them.

If there are some characters left after the decoding, they are left in input, i.e. they are not read. This behavior differs for the overload of decode() that takes std::string as the input.

◆ decodeDictionary()

std::unique_ptr< BDictionary > bencoding::Decoder::decodeDictionary ( std::istream &  input)
private

Decodes a dictionary from input.

Format
d<bencoded string><bencoded element>e
Example
d3:cow3:moo4:spam4:eggse represents the dictionary {"cow": "moo", "spam": "eggs"}
d4:spaml1:a1:bee represents the dictionary {"spam": ["a", "b"]}

The keys must be bencoded strings. The values may be any bencoded type, including integers, strings, lists, and other dictionaries. This function supports decoding of dictionaries whose keys are not lexicographically sorted (according to the specification, they must be sorted).

◆ decodeDictionaryItemsIntoDictionary()

std::unique_ptr< BDictionary > bencoding::Decoder::decodeDictionaryItemsIntoDictionary ( std::istream &  input)
private

Decodes items from input, adds them to a dictionary, and returns that dictionary.

◆ decodeDictionaryKey()

std::shared_ptr< BString > bencoding::Decoder::decodeDictionaryKey ( std::istream &  input)
private

Decodes a dictionary key from input.

◆ decodeDictionaryValue()

std::unique_ptr< BItem > bencoding::Decoder::decodeDictionaryValue ( std::istream &  input)
private

Decodes a dictionary value from input.

◆ decodeEncodedInteger()

std::unique_ptr< BInteger > bencoding::Decoder::decodeEncodedInteger ( const std::string &  encodedInteger) const
private

Decodes the given encoded integer.

◆ decodeInteger()

std::unique_ptr< BInteger > bencoding::Decoder::decodeInteger ( std::istream &  input) const
private

Decodes an integer from input.

Format
i<integer encoded in base ten ASCII>e
Example
i3e represents the integer 3

Moreover, only the significant digits should be used, one cannot pad the integer with zeroes, such as i04e (see the specification).

◆ decodeList()

std::unique_ptr< BList > bencoding::Decoder::decodeList ( std::istream &  input)
private

Decodes a list from input.

Format
l<bencoded values>e
Example
l4:spam4:eggse represents a list containing two strings "spam" and "eggs"

◆ decodeListItemsIntoList()

std::unique_ptr< BList > bencoding::Decoder::decodeListItemsIntoList ( std::istream &  input)
private

Decodes items from input, appends them to a list, and returns that list.

◆ decodeString()

std::unique_ptr< BString > bencoding::Decoder::decodeString ( std::istream &  input) const
private

Decodes a string from input.

Format
<string length encoded in base ten ASCII>:<string data>
Example
4:test represents the string "test"

◆ readEncodedInteger()

std::string bencoding::Decoder::readEncodedInteger ( std::istream &  input) const
private

Reads an encoded integer from input.

◆ readExpectedChar()

void bencoding::Decoder::readExpectedChar ( std::istream &  input,
char  expected_char 
) const
private

Reads expected_char from input and discards it.

◆ readStringLength()

std::string::size_type bencoding::Decoder::readStringLength ( std::istream &  input) const
private

Reads the string length from input, validates it, and returns it.

◆ readStringOfGivenLength()

std::string bencoding::Decoder::readStringOfGivenLength ( std::istream &  input,
std::string::size_type  length 
) const
private

Reads a string of the given length from input and returns it.

◆ validateInputDoesNotContainUndecodedCharacters()

void bencoding::Decoder::validateInputDoesNotContainUndecodedCharacters ( std::istream &  input)
private

Throws DecodingError if input has not been completely read.


The documentation for this class was generated from the following files: