📄 cryptlib.h
字号:
/*! Default implementation is to call GenerateByte() and return its parity. */
virtual unsigned int GenerateBit();
//! generate a random 32 bit word in the range min to max, inclusive
virtual word32 GenerateWord32(word32 a=0, word32 b=0xffffffffL);
//! generate random array of bytes
/*! Default implementation is to call GenerateByte() size times. */
virtual void GenerateBlock(byte *output, unsigned int size);
//! generate and discard n bytes
/*! Default implementation is to call GenerateByte() n times. */
virtual void DiscardBytes(unsigned int n);
//! randomly shuffle the specified array, resulting permutation is uniformly distributed
template <class IT> void Shuffle(IT begin, IT end)
{
for (; begin != end; ++begin)
std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1));
}
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
byte GetByte() {return GenerateByte();}
unsigned int GetBit() {return GenerateBit();}
word32 GetLong(word32 a=0, word32 b=0xffffffffL) {return GenerateWord32(a, b);}
word16 GetShort(word16 a=0, word16 b=0xffff) {return (word16)GenerateWord32(a, b);}
void GetBlock(byte *output, unsigned int size) {GenerateBlock(output, size);}
#endif
};
//! returns a reference that can be passed to functions that ask for a RNG but doesn't actually use it
RandomNumberGenerator & NullRNG();
class WaitObjectContainer;
//! interface for objects that you can wait for
class Waitable
{
public:
//! maximum number of wait objects that this object can return
virtual unsigned int GetMaxWaitObjectCount() const =0;
//! put wait objects into container
virtual void GetWaitObjects(WaitObjectContainer &container) =0;
//! wait on this object
/*! same as creating an empty container, calling GetWaitObjects(), and calling Wait() on the container */
bool Wait(unsigned long milliseconds);
};
//! interface for buffered transformations
/*! BufferedTransformation is a generalization of BlockTransformation,
StreamTransformation, and HashTransformation.
A buffered transformation is an object that takes a stream of bytes
as input (this may be done in stages), does some computation on them, and
then places the result into an internal buffer for later retrieval. Any
partial result already in the output buffer is not modified by further
input.
If a method takes a "blocking" parameter, and you
pass "false" for it, the method will return before all input has been processed if
the input cannot be processed without waiting (for network buffers to become available, for example).
In this case the method will return true
or a non-zero integer value. When this happens you must continue to call the method with the same
parameters until it returns false or zero, before calling any other method on it or
attached BufferedTransformation. The integer return value in this case is approximately
the number of bytes left to be processed, and can be used to implement a progress bar.
For functions that take a "propagation" parameter, propagation != 0 means pass on the signal to attached
BufferedTransformation objects, with propagation decremented at each step until it reaches 0.
-1 means unlimited propagation.
\nosubgrouping
*/
class BufferedTransformation : public Algorithm, public Waitable
{
public:
// placed up here for CW8
static const std::string NULL_CHANNEL; // the empty string ""
BufferedTransformation() : Algorithm(false) {}
//! return a reference to this object
/*! This function is useful for passing a temporary BufferedTransformation object to a
function that takes a non-const reference. */
BufferedTransformation& Ref() {return *this;}
//! \name INPUT
//@{
//! input a byte for processing
unsigned int Put(byte inByte, bool blocking=true)
{return Put(&inByte, 1, blocking);}
//! input multiple bytes
unsigned int Put(const byte *inString, unsigned int length, bool blocking=true)
{return Put2(inString, length, 0, blocking);}
//! input a 16-bit word
unsigned int PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
//! input a 32-bit word
unsigned int PutWord32(word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
//! request space which can be written into by the caller, and then used as input to Put()
/*! \param size is requested size (as a hint) for input, and size of the returned space for output */
/*! \note The purpose of this method is to help avoid doing extra memory allocations. */
virtual byte * CreatePutSpace(unsigned int &size) {size=0; return NULL;}
virtual bool CanModifyInput() const {return false;}
//! input multiple bytes that may be modified by callee
unsigned int PutModifiable(byte *inString, unsigned int length, bool blocking=true)
{return PutModifiable2(inString, length, 0, blocking);}
bool MessageEnd(int propagation=-1, bool blocking=true)
{return !!Put2(NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
unsigned int PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1, bool blocking=true)
{return Put2(inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
//! input multiple bytes for blocking or non-blocking processing
/*! \param messageEnd means how many filters to signal MessageEnd to, including this one */
virtual unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) =0;
//! input multiple bytes that may be modified by callee for blocking or non-blocking processing
/*! \param messageEnd means how many filters to signal MessageEnd to, including this one */
virtual unsigned int PutModifiable2(byte *inString, unsigned int length, int messageEnd, bool blocking)
{return Put2(inString, length, messageEnd, blocking);}
//! thrown by objects that have not implemented nonblocking input processing
struct BlockingInputOnly : public NotImplemented
{BlockingInputOnly(const std::string &s) : NotImplemented(s + ": Nonblocking input is not implemented by this object.") {}};
//@}
//! \name WAITING
//@{
unsigned int GetMaxWaitObjectCount() const;
void GetWaitObjects(WaitObjectContainer &container);
//@}
//! \name SIGNALS
//@{
virtual void IsolatedInitialize(const NameValuePairs ¶meters) {throw NotImplemented("BufferedTransformation: this object can't be reinitialized");}
virtual bool IsolatedFlush(bool hardFlush, bool blocking) =0;
virtual bool IsolatedMessageSeriesEnd(bool blocking) {return false;}
//! initialize or reinitialize this object
virtual void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1);
//! flush buffered input and/or output
/*! \param hardFlush is used to indicate whether all data should be flushed
\note Hard flushes must be used with care. It means try to process and output everything, even if
there may not be enough data to complete the action. For example, hard flushing a HexDecoder would
cause an error if you do it after inputing an odd number of hex encoded characters.
For some types of filters, for example ZlibDecompressor, hard flushes can only
be done at "synchronization points". These synchronization points are positions in the data
stream that are created by hard flushes on the corresponding reverse filters, in this
example ZlibCompressor. This is useful when zlib compressed data is moved across a
network in packets and compression state is preserved across packets, as in the ssh2 protocol.
*/
virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
//! mark end of a series of messages
/*! There should be a MessageEnd immediately before MessageSeriesEnd. */
virtual bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
//! set propagation of automatically generated and transfered signals
/*! propagation == 0 means do not automaticly generate signals */
virtual void SetAutoSignalPropagation(int propagation) {}
//!
virtual int GetAutoSignalPropagation() const {return 0;}
public:
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
void Close() {MessageEnd();}
#endif
//@}
//! \name RETRIEVAL OF ONE MESSAGE
//@{
//! returns number of bytes that is currently ready for retrieval
/*! All retrieval functions return the actual number of bytes
retrieved, which is the lesser of the request number and
MaxRetrievable(). */
virtual unsigned long MaxRetrievable() const;
//! returns whether any bytes are currently ready for retrieval
virtual bool AnyRetrievable() const;
//! try to retrieve a single byte
virtual unsigned int Get(byte &outByte);
//! try to retrieve multiple bytes
virtual unsigned int Get(byte *outString, unsigned int getMax);
//! peek at the next byte without removing it from the output buffer
virtual unsigned int Peek(byte &outByte) const;
//! peek at multiple bytes without removing them from the output buffer
virtual unsigned int Peek(byte *outString, unsigned int peekMax) const;
//! try to retrieve a 16-bit word
unsigned int GetWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER);
//! try to retrieve a 32-bit word
unsigned int GetWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER);
//! try to peek at a 16-bit word
unsigned int PeekWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER);
//! try to peek at a 32-bit word
unsigned int PeekWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER);
//! move transferMax bytes of the buffered output to target as input
unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL)
{TransferTo2(target, transferMax, channel); return transferMax;}
//! discard skipMax bytes from the output buffer
virtual unsigned long Skip(unsigned long skipMax=ULONG_MAX);
//! copy copyMax bytes of the buffered output to target as input
unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL) const
{return CopyRangeTo(target, 0, copyMax, channel);}
//! copy copyMax bytes of the buffered output, starting at position (relative to current position), to target as input
unsigned long CopyRangeTo(BufferedTransformation &target, unsigned long position, unsigned long copyMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL) const
{unsigned long i = position; CopyRangeTo2(target, i, i+copyMax, channel); return i-position;}
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
unsigned long MaxRetrieveable() const {return MaxRetrievable();}
#endif
//@}
//! \name RETRIEVAL OF MULTIPLE MESSAGES
//@{
//!
virtual unsigned long TotalBytesRetrievable() const;
//! number of times MessageEnd() has been received minus messages retrieved or skipped
virtual unsigned int NumberOfMessages() const;
//! returns true if NumberOfMessages() > 0
virtual bool AnyMessages() const;
//! start retrieving the next message
/*!
Returns false if no more messages exist or this message
is not completely retrieved.
*/
virtual bool GetNextMessage();
//! skip count number of messages
virtual unsigned int SkipMessages(unsigned int count=UINT_MAX);
//!
unsigned int TransferMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL)
{TransferMessagesTo2(target, count, channel); return count;}
//!
unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
//!
virtual void SkipAll();
//!
void TransferAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL)
{TransferAllTo2(target, channel);}
//!
void CopyAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL) const;
virtual bool GetNextMessageSeries() {return false;}
virtual unsigned int NumberOfMessagesInThisSeries() const {return NumberOfMessages();}
virtual unsigned int NumberOfMessageSeries() const {return 0;}
//@}
//! \name NON-BLOCKING TRANSFER OF OUTPUT
//@{
//! .
virtual unsigned int TransferTo2(BufferedTransformation &target, unsigned long &byteCount, const std::string &channel=NULL_CHANNEL, bool blocking=true) =0;
virtual unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const =0;
unsigned int TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel=NULL_CHANNEL, bool blocking=true);
unsigned int TransferAllTo2(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL, bool blocking=true);
//@}
//! \name CHANNELS
//@{
struct NoChannelSupport : public NotImplemented
{NoChannelSupport() : NotImplemented("BufferedTransformation: this object doesn't support multiple channels") {}};
unsigned int ChannelPut(const std::string &channel, byte inByte, bool blocking=true)
{return ChannelPut(channel, &inByte, 1, blocking);}
unsigned int ChannelPut(const std::string &channel, const byte *inString, unsigned int length, bool blocking=true)
{return ChannelPut2(channel, inString, length, 0, blocking);}
unsigned int ChannelPutModifiable(const std::string &channel, byte *inString, unsigned int length, bool blocking=true)
{return ChannelPutModifiable2(channel, inString, length, 0, blocking);}
unsigned int ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
unsigned int ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
{return !!ChannelPut2(channel, NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
unsigned int ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation=-1, bool blocking=true)
{return ChannelPut2(channel, inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
virtual byte * ChannelCreatePutSpace(const std::string &channel, unsigned int &size);
virtual unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking);
virtual unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking);
virtual void ChannelInitialize(const std::string &channel, const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1);
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -