Crypto++  5.6.5
Free C++ class library of cryptographic schemes
basecode.h
Go to the documentation of this file.
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 
14 NAMESPACE_BEGIN(CryptoPP)
15 
16 //! \class BaseN_Encoder
17 //! \brief Encoder for bases that are a power of 2
18 class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
19 {
20 public:
21  //! \brief Construct a BaseN_Encoder
22  //! \param attachment a BufferedTransformation to attach to this object
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 
49 private:
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
58 class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
59 {
60 public:
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().
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 
101 private:
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
110 class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
111 {
112 public:
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 
135 private:
136  SecByteBlock m_separator, m_terminator;
137  size_t m_groupSize, m_counter;
138 };
139 
140 NAMESPACE_END
141 
142 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
Standard names for retrieving values by name when working with NameValuePairs.
Classes for working with NameValuePairs.
Grouper(BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:115
Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:123
Abstract base classes that provide a uniform interface to this library.
BaseN_Encoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Encoder.
Definition: basecode.h:23
SecBlock typedef.
Definition: secblock.h:731
Interface for buffered transformations.
Definition: cryptlib.h:1352
BaseN_Decoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:65
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:500
BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:78
Implementation of BufferedTransformation's attachment interface.
BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
Construct a BaseN_Encoder.
Definition: basecode.h:35
Base class for unflushable filters.
Definition: simple.h:94
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:110
Crypto++ library namespace.
Encoder for bases that are a power of 2.
Definition: basecode.h:18
Decoder for bases that are a power of 2.
Definition: basecode.h:58
Base class for bufferless filters.
Definition: simple.h:83
Interface for retrieving values given their names.
Definition: cryptlib.h:279