source: git/src-cryptopp/basecode.h

Last change on this file was e230cb0, checked in by David Stainton <dstainton415@…>, at 2016-10-12T13:27:29Z

Add cryptopp from tag CRYPTOPP_5_6_5

  • Property mode set to 100644
File size: 5.4 KB
Line 
1// basecode.h - written and placed in the public domain by Wei Dai
2
3//! \file
4//! \brief Base classes for working with encoders and decoders.
5
6#ifndef CRYPTOPP_BASECODE_H
7#define CRYPTOPP_BASECODE_H
8
9#include "cryptlib.h"
10#include "filters.h"
11#include "algparam.h"
12#include "argnames.h"
13
14NAMESPACE_BEGIN(CryptoPP)
15
16//! \class BaseN_Encoder
17//! \brief Encoder for bases that are a power of 2
18class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
19{
20public:
21        //! \brief Construct a BaseN_Encoder
22        //! \param attachment a BufferedTransformation to attach to this object
23        BaseN_Encoder(BufferedTransformation *attachment=NULL)
24                : m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
25                , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
26                        {Detach(attachment);}
27
28        //! \brief Construct a BaseN_Encoder
29        //! \param alphabet table of ASCII characters to use as the alphabet
30        //! \param log2base the log<sub>2</sub>base
31        //! \param attachment a BufferedTransformation to attach to this object
32        //! \param padding the character to use as padding
33        //! \pre log2base must be between 1 and 7 inclusive
34        //! \throws InvalidArgument if log2base is not between 1 and 7
35        BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
36                : m_alphabet(NULL), m_padding(0), m_bitsPerChar(0)
37                , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
38        {
39                Detach(attachment);
40                IsolatedInitialize(MakeParameters(Name::EncodingLookupArray(), alphabet)
41                        (Name::Log2Base(), log2base)
42                        (Name::Pad(), padding != -1)
43                        (Name::PaddingByte(), byte(padding)));
44        }
45
46        void IsolatedInitialize(const NameValuePairs &parameters);
47        size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
48
49private:
50        const byte *m_alphabet;
51        int m_padding, m_bitsPerChar, m_outputBlockSize;
52        int m_bytePos, m_bitPos;
53        SecByteBlock m_outBuf;
54};
55
56//! \class BaseN_Decoder
57//! \brief Decoder for bases that are a power of 2
58class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
59{
60public:
61        //! \brief Construct a BaseN_Decoder
62        //! \param attachment a BufferedTransformation to attach to this object
63        //! \details padding is set to -1, which means use default padding. If not
64        //!   required, then the value must be set via IsolatedInitialize().
65        BaseN_Decoder(BufferedTransformation *attachment=NULL)
66                : m_lookup(0), m_padding(0), m_bitsPerChar(0)
67                , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
68                        {Detach(attachment);}
69
70        //! \brief Construct a BaseN_Decoder
71        //! \param lookup table of values
72        //! \param log2base the log<sub>2</sub>base
73        //! \param attachment a BufferedTransformation to attach to this object
74        //! \details log2base is the exponent (like 5 in 2<sup>5</sup>), and not
75        //!   the number of elements (like 32).
76        //! \details padding is set to -1, which means use default padding. If not
77        //!   required, then the value must be set via IsolatedInitialize().
78        BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULL)
79                : m_lookup(0), m_padding(0), m_bitsPerChar(0)
80                , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
81        {
82                Detach(attachment);
83                IsolatedInitialize(MakeParameters(Name::DecodingLookupArray(), lookup)(Name::Log2Base(), log2base));
84        }
85
86        void IsolatedInitialize(const NameValuePairs &parameters);
87        size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
88
89        //! \brief Intializes BaseN lookup array
90        //! \param lookup table of values
91        //! \param alphabet table of ASCII characters
92        //! \param base the base for the encoder
93        //! \param caseInsensitive flag indicating whether the alpabet is case sensitivie
94        //! \pre COUNTOF(lookup) == 256
95        //! \pre COUNTOF(alphabet) == base
96        //! \details Internally, the function sets the first 256 elements in the lookup table to
97        //!  their value from the alphabet array or -1. base is the number of element (like 32),
98        //!  and not an exponent (like 5 in 2<sup>5</sup>)
99        static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
100
101private:
102        const int *m_lookup;
103        int m_padding, m_bitsPerChar, m_outputBlockSize;
104        int m_bytePos, m_bitPos;
105        SecByteBlock m_outBuf;
106};
107
108//! \class Grouper
109//! \brief Filter that breaks input stream into groups of fixed size
110class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
111{
112public:
113        //! \brief Construct a Grouper
114        //! \param attachment a BufferedTransformation to attach to this object
115        Grouper(BufferedTransformation *attachment=NULL)
116                : m_groupSize(0), m_counter(0) {Detach(attachment);}
117
118        //! \brief Construct a Grouper
119        //! \param groupSize the size of the grouping
120        //! \param separator the separator to use between groups
121        //! \param terminator the terminator appeand after processing
122        //! \param attachment a BufferedTransformation to attach to this object
123        Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
124                : m_groupSize(0), m_counter(0)
125        {
126                Detach(attachment);
127                IsolatedInitialize(MakeParameters(Name::GroupSize(), groupSize)
128                        (Name::Separator(), ConstByteArrayParameter(separator))
129                        (Name::Terminator(), ConstByteArrayParameter(terminator)));
130        }
131
132        void IsolatedInitialize(const NameValuePairs &parameters);
133        size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
134
135private:
136        SecByteBlock m_separator, m_terminator;
137        size_t m_groupSize, m_counter;
138};
139
140NAMESPACE_END
141
142#endif
Note: See TracBrowser for help on using the repository browser.