自定义源代码的crypto++

Custom source for Crypto++

本文关键字:crypto++ 源代码 自定义      更新时间:2023-10-16

我已经为二进制I/O制作了自己的自定义流类。现在我正试图使它们与crypto++库兼容。我发现了一个问题,处理自定义水槽和实现我自己的。现在我需要实现一个源。我已经搜索了文档,似乎有一个巨大的继承层次结构,所以我还不能理解它。

有人能提供一个示例代码吗?

这是我的流类的一部分:

/// 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
{
public:
    /// 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;
};

我不得不查看crypto++的源代码,并从FileSource类复制大部分实现。相关文件为files.hfiles.cpp

首先,我们需要看看FileSource类。它继承自SourceTemplate<FileStore>。所以我们需要检查FileStore类。它继承自StoreFilterPutSpaceHelperNotCopyable。我们需要创建一个继承这些类的类。

store类必须有默认构造函数,并实现以下虚函数:TransferTo2CopyRangeTo2StoreInitializeStoreInitialize可以是private

最后,我们的源类只需要构造函数,如果你看看files.h, FileSource完全是在头文件中实现的。

完整的实现代码仅限于files.hfiles.cpp,所以没有必要在这个答案中复制它。