Crypto++  8.0
Free C++ class library of cryptographic schemes
basecode.h
Go to the documentation of this file.
1 // basecode.h - originally 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 /// \brief Encoder for bases that are a power of 2
17 class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
18 {
19 public:
20  /// \brief Construct a BaseN_Encoder
21  /// \param attachment a BufferedTransformation to attach to this object
23  : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
24  , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
25  {Detach(attachment);}
26 
27  /// \brief Construct a BaseN_Encoder
28  /// \param alphabet table of ASCII characters to use as the alphabet
29  /// \param log2base the log<sub>2</sub>base
30  /// \param attachment a BufferedTransformation to attach to this object
31  /// \param padding the character to use as padding
32  /// \pre log2base must be between 1 and 7 inclusive
33  /// \throws InvalidArgument if log2base is not between 1 and 7
34  BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULLPTR, int padding=-1)
35  : m_alphabet(NULLPTR), m_padding(0), m_bitsPerChar(0)
36  , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
37  {
38  Detach(attachment);
39  IsolatedInitialize(MakeParameters(Name::EncodingLookupArray(), alphabet)
40  (Name::Log2Base(), log2base)
41  (Name::Pad(), padding != -1)
42  (Name::PaddingByte(), byte(padding)));
43  }
44 
45  void IsolatedInitialize(const NameValuePairs &parameters);
46  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
47 
48 private:
49  const byte *m_alphabet;
50  int m_padding, m_bitsPerChar, m_outputBlockSize;
51  int m_bytePos, m_bitPos;
52  SecByteBlock m_outBuf;
53 };
54 
55 /// \brief Decoder for bases that are a power of 2
56 class CRYPTOPP_DLL BaseN_Decoder : public Unflushable<Filter>
57 {
58 public:
59  /// \brief Construct a BaseN_Decoder
60  /// \param attachment a BufferedTransformation to attach to this object
61  /// \details padding is set to -1, which means use default padding. If not
62  /// required, then the value must be set via IsolatedInitialize().
64  : m_lookup(NULLPTR), m_bitsPerChar(0)
65  , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
66  {Detach(attachment);}
67 
68  /// \brief Construct a BaseN_Decoder
69  /// \param lookup table of values
70  /// \param log2base the log<sub>2</sub>base
71  /// \param attachment a BufferedTransformation to attach to this object
72  /// \details log2base is the exponent (like 5 in 2<sup>5</sup>), and not
73  /// the number of elements (like 32).
74  /// \details padding is set to -1, which means use default padding. If not
75  /// required, then the value must be set via IsolatedInitialize().
76  BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULLPTR)
77  : m_lookup(NULLPTR), m_bitsPerChar(0)
78  , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
79  {
80  Detach(attachment);
81  IsolatedInitialize(MakeParameters(Name::DecodingLookupArray(), lookup)(Name::Log2Base(), log2base));
82  }
83 
84  void IsolatedInitialize(const NameValuePairs &parameters);
85  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
86 
87  /// \brief Initializes BaseN lookup array
88  /// \param lookup table of values
89  /// \param alphabet table of ASCII characters
90  /// \param base the base for the encoder
91  /// \param caseInsensitive flag indicating whether the alphabet is case sensitivie
92  /// \pre COUNTOF(lookup) == 256
93  /// \pre COUNTOF(alphabet) == base
94  /// \details Internally, the function sets the first 256 elements in the lookup table to
95  /// their value from the alphabet array or -1. base is the number of element (like 32),
96  /// and not an exponent (like 5 in 2<sup>5</sup>)
97  static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
98 
99 private:
100  const int *m_lookup;
101  int m_bitsPerChar, m_outputBlockSize;
102  int m_bytePos, m_bitPos;
103  SecByteBlock m_outBuf;
104 };
105 
106 /// \brief Filter that breaks input stream into groups of fixed size
107 class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
108 {
109 public:
110  /// \brief Construct a Grouper
111  /// \param attachment a BufferedTransformation to attach to this object
112  Grouper(BufferedTransformation *attachment=NULLPTR)
113  : m_groupSize(0), m_counter(0) {Detach(attachment);}
114 
115  /// \brief Construct a Grouper
116  /// \param groupSize the size of the grouping
117  /// \param separator the separator to use between groups
118  /// \param terminator the terminator appeand after processing
119  /// \param attachment a BufferedTransformation to attach to this object
120  Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULLPTR)
121  : m_groupSize(0), m_counter(0)
122  {
123  Detach(attachment);
124  IsolatedInitialize(MakeParameters(Name::GroupSize(), groupSize)
126  (Name::Terminator(), ConstByteArrayParameter(terminator)));
127  }
128 
129  void IsolatedInitialize(const NameValuePairs &parameters);
130  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
131 
132 private:
133  SecByteBlock m_separator, m_terminator;
134  size_t m_groupSize, m_counter;
135 };
136 
137 NAMESPACE_END
138 
139 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:20
Standard names for retrieving values by name when working with NameValuePairs.
Classes for working with NameValuePairs.
const char * Pad()
bool
Definition: argnames.h:72
Grouper(BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:112
Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:120
Abstract base classes that provide a uniform interface to this library.
BaseN_Encoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Encoder.
Definition: basecode.h:22
SecBlock<byte> typedef.
Definition: secblock.h:1058
Interface for buffered transformations.
Definition: cryptlib.h:1598
const char * PaddingByte()
byte
Definition: argnames.h:73
const char * Separator()
ConstByteArrayParameter.
Definition: argnames.h:68
BaseN_Decoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:63
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
const char * EncodingLookupArray()
const byte *
Definition: argnames.h:75
BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:76
const char * Log2Base()
int
Definition: argnames.h:74
const char * GroupSize()
int
Definition: argnames.h:71
const char * Terminator()
ConstByteArrayParameter.
Definition: argnames.h:69
Implementation of BufferedTransformation&#39;s attachment interface.
BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
Construct a BaseN_Encoder.
Definition: basecode.h:34
Base class for unflushable filters.
Definition: simple.h:108
const char * DecodingLookupArray()
const byte *
Definition: argnames.h:76
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:35
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:107
Crypto++ library namespace.
Encoder for bases that are a power of 2.
Definition: basecode.h:17
Decoder for bases that are a power of 2.
Definition: basecode.h:56
Base class for bufferless filters.
Definition: simple.h:98
Interface for retrieving values given their names.
Definition: cryptlib.h:293