FaTony FaTony - 1 year ago 80
C++ Question

Custom source for Crypto++

I have made my own custom stream classes for binary I/O. Now I'm trying to make them compatible with Crypto++ library. I have found a question that deals with custom sink and implemented my own. Now I need to implement a source. I've searched through the documentation and there seems to be a huge inheritance hierarchy so I can't understand it yet.

Can someone provide an example code?

Here's the part of my stream class:

/// \brief Base class for a binary input stream.
/// \details Binary streams are used for low level unformatted I/O. Built on top
/// of standard streams, this system takes care of endianness and provides
/// convenient << and >> overloads. This class is designed to mirror
/// std::istream.

class BinaryInputStream : public virtual BinaryStreamBase
/// \brief Returns whether last I/O operation has completed successfully.
/// \return True if last I/O operation has completed successfully,
/// false otherwise.
virtual bool IsGood() const = 0;

/// \brief Returns whether end-of-file has been reached.
/// \return True if end-of-file has been reached, false otherwise.
virtual bool IsEOF() const = 0;

/// \brief Returns whether recoverable error has occured.
/// \return True if recoverable error has occured, false otherwise.
virtual bool IsFail() const = 0;

/// \brief Returns whether non-recoverable error has occured.
/// \return True if non-recoverable error has occured, false otherwise.
virtual bool IsBad() const = 0;

/// \brief Reads a sequence of bytes from the stream.
/// \param[in,out] buffer Buffer to write to.
/// \param[in] size Number of bytes to read.
/// \return Reference to this stream.
/// \warning You are responsible for allocating the buffer and ensuring that
/// it contains enough space to hold the data. If number of bytes to read is
/// greater than the size of the buffer, the behavior is undefined.
virtual BinaryInputStream& Read(char* buffer, std::size_t size) = 0;

Answer Source

I had to look at the source code of crypto++ and copy most of the implementation from FileSource class. The relevant files are files.h and files.cpp.

First, we need to look at the FileSource class. It inherits from SourceTemplate<FileStore>. So we need to examine FileStore class. It inherits from Store, FilterPutSpaceHelper and NotCopyable. We need to create class which also inherits from these classes.

Our store class must have a default constructor and implement the following virtual functions: TransferTo2, CopyRangeTo2 and StoreInitialize. StoreInitialize can be private.

Finally, our source class needs only constructors and if you look at files.h, FileSource is implemented entirely in a header file.

The full implementation code is confined to files.h and files.cpp so there's no need to copy it in this answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download