Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // basecode.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file

0004 /// \brief Base classes for working with encoders and decoders.

0005 
0006 #ifndef CRYPTOPP_BASECODE_H
0007 #define CRYPTOPP_BASECODE_H
0008 
0009 #include "cryptlib.h"
0010 #include "filters.h"
0011 #include "algparam.h"
0012 #include "argnames.h"
0013 
0014 NAMESPACE_BEGIN(CryptoPP)
0015 
0016 /// \brief Encoder for bases that are a power of 2

0017 class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
0018 {
0019 public:
0020     /// \brief Construct a BaseN_Encoder

0021     /// \param attachment a BufferedTransformation to attach to this object

0022     BaseN_Encoder(BufferedTransformation *attachment=NULLPTR)
0023         : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
0024         , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
0025             {Detach(attachment);}
0026 
0027     /// \brief Construct a BaseN_Encoder

0028     /// \param alphabet table of ASCII characters to use as the alphabet

0029     /// \param log2base the log<sub>2</sub>base

0030     /// \param attachment a BufferedTransformation to attach to this object

0031     /// \param padding the character to use as padding

0032     /// \pre log2base must be between 1 and 7 inclusive

0033     /// \throw InvalidArgument if log2base is not between 1 and 7

0034     BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULLPTR, int padding=-1)
0035         : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
0036         , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
0037     {
0038         Detach(attachment);
0039         BaseN_Encoder::IsolatedInitialize(
0040             MakeParameters
0041                 (Name::EncodingLookupArray(), alphabet)
0042                 (Name::Log2Base(), log2base)
0043                 (Name::Pad(), padding != -1)
0044                 (Name::PaddingByte(), byte(padding)));
0045     }
0046 
0047     void IsolatedInitialize(const NameValuePairs &parameters);
0048     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
0049 
0050 private:
0051     const byte *m_alphabet;
0052     int m_padding, m_bitsPerChar, m_outputBlockSize;
0053     int m_bytePos, m_bitPos;
0054     SecByteBlock m_outBuf;
0055 };
0056 
0057 /// \brief Decoder for bases that are a power of 2

0058 class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
0059 {
0060 public:
0061     /// \brief Construct a BaseN_Decoder

0062     /// \param attachment a BufferedTransformation to attach to this object

0063     /// \details padding is set to -1, which means use default padding. If not

0064     ///   required, then the value must be set via IsolatedInitialize().

0065     BaseN_Decoder(BufferedTransformation *attachment=NULLPTR)
0066         : m_lookup(NULLPTR), m_bitsPerChar(0)
0067         , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
0068             {Detach(attachment);}
0069 
0070     /// \brief Construct a BaseN_Decoder

0071     /// \param lookup table of values

0072     /// \param log2base the log<sub>2</sub>base

0073     /// \param attachment a BufferedTransformation to attach to this object

0074     /// \details log2base is the exponent (like 5 in 2<sup>5</sup>), and not

0075     ///   the number of elements (like 32).

0076     /// \details padding is set to -1, which means use default padding. If not

0077     ///   required, then the value must be set via IsolatedInitialize().

0078     BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULLPTR)
0079         : m_lookup(NULLPTR), m_bitsPerChar(0)
0080         , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
0081     {
0082         Detach(attachment);
0083         BaseN_Decoder::IsolatedInitialize(
0084             MakeParameters
0085                 (Name::DecodingLookupArray(), lookup)
0086                 (Name::Log2Base(), log2base));
0087     }
0088 
0089     void IsolatedInitialize(const NameValuePairs &parameters);
0090     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
0091 
0092     /// \brief Initializes BaseN lookup array

0093     /// \param lookup table of values

0094     /// \param alphabet table of ASCII characters

0095     /// \param base the base for the encoder

0096     /// \param caseInsensitive flag indicating whether the alphabet is case sensitivie

0097     /// \pre COUNTOF(lookup) == 256

0098     /// \pre COUNTOF(alphabet) == base

0099     /// \details Internally, the function sets the first 256 elements in the lookup table to

0100     ///  their value from the alphabet array or -1. base is the number of element (like 32),

0101     ///  and not an exponent (like 5 in 2<sup>5</sup>)

0102     static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
0103 
0104 private:
0105     const int *m_lookup;
0106     int m_bitsPerChar, m_outputBlockSize;
0107     int m_bytePos, m_bitPos;
0108     SecByteBlock m_outBuf;
0109 };
0110 
0111 /// \brief Filter that breaks input stream into groups of fixed size

0112 class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
0113 {
0114 public:
0115     /// \brief Construct a Grouper

0116     /// \param attachment a BufferedTransformation to attach to this object

0117     Grouper(BufferedTransformation *attachment=NULLPTR)
0118         : m_groupSize(0), m_counter(0) {Detach(attachment);}
0119 
0120     /// \brief Construct a Grouper

0121     /// \param groupSize the size of the grouping

0122     /// \param separator the separator to use between groups

0123     /// \param terminator the terminator appeand after processing

0124     /// \param attachment a BufferedTransformation to attach to this object

0125     Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULLPTR)
0126         : m_groupSize(0), m_counter(0)
0127     {
0128         Detach(attachment);
0129         Grouper::IsolatedInitialize(
0130             MakeParameters
0131                 (Name::GroupSize(), groupSize)
0132                 (Name::Separator(), ConstByteArrayParameter(separator))
0133                 (Name::Terminator(), ConstByteArrayParameter(terminator)));
0134     }
0135 
0136     void IsolatedInitialize(const NameValuePairs &parameters);
0137     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
0138 
0139 private:
0140     SecByteBlock m_separator, m_terminator;
0141     size_t m_groupSize, m_counter;
0142 };
0143 
0144 NAMESPACE_END
0145 
0146 #endif