Crypto++ 8.7
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
14NAMESPACE_BEGIN(CryptoPP)
15
16/// \brief Encoder for bases that are a power of 2
17class CRYPTOPP_DLL BaseN_Encoder : public Unflushable<Filter>
18{
19public:
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 /// \throw 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);
41 (Name::EncodingLookupArray(), alphabet)
42 (Name::Log2Base(), log2base)
43 (Name::Pad(), padding != -1)
44 (Name::PaddingByte(), byte(padding)));
45 }
46
47 void IsolatedInitialize(const NameValuePairs &parameters);
48 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
49
50private:
51 const byte *m_alphabet;
52 int m_padding, m_bitsPerChar, m_outputBlockSize;
53 int m_bytePos, m_bitPos;
54 SecByteBlock m_outBuf;
55};
56
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().
66 : m_lookup(NULLPTR), 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=NULLPTR)
79 : m_lookup(NULLPTR), m_bitsPerChar(0)
80 , m_outputBlockSize(0), m_bytePos(0), m_bitPos(0)
81 {
82 Detach(attachment);
86 (Name::Log2Base(), log2base));
87 }
88
89 void IsolatedInitialize(const NameValuePairs &parameters);
90 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
91
92 /// \brief Initializes BaseN lookup array
93 /// \param lookup table of values
94 /// \param alphabet table of ASCII characters
95 /// \param base the base for the encoder
96 /// \param caseInsensitive flag indicating whether the alphabet is case sensitivie
97 /// \pre COUNTOF(lookup) == 256
98 /// \pre COUNTOF(alphabet) == base
99 /// \details Internally, the function sets the first 256 elements in the lookup table to
100 /// their value from the alphabet array or -1. base is the number of element (like 32),
101 /// and not an exponent (like 5 in 2<sup>5</sup>)
102 static void CRYPTOPP_API InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive);
103
104private:
105 const int *m_lookup;
106 int m_bitsPerChar, m_outputBlockSize;
107 int m_bytePos, m_bitPos;
108 SecByteBlock m_outBuf;
109};
110
111/// \brief Filter that breaks input stream into groups of fixed size
112class CRYPTOPP_DLL Grouper : public Bufferless<Filter>
113{
114public:
115 /// \brief Construct a Grouper
116 /// \param attachment a BufferedTransformation to attach to this object
117 Grouper(BufferedTransformation *attachment=NULLPTR)
118 : m_groupSize(0), m_counter(0) {Detach(attachment);}
119
120 /// \brief Construct a Grouper
121 /// \param groupSize the size of the grouping
122 /// \param separator the separator to use between groups
123 /// \param terminator the terminator appeand after processing
124 /// \param attachment a BufferedTransformation to attach to this object
125 Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULLPTR)
126 : m_groupSize(0), m_counter(0)
127 {
128 Detach(attachment);
131 (Name::GroupSize(), groupSize)
134 }
135
136 void IsolatedInitialize(const NameValuePairs &parameters);
137 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
138
139private:
140 SecByteBlock m_separator, m_terminator;
141 size_t m_groupSize, m_counter;
142};
143
144NAMESPACE_END
145
146#endif
Classes for working with NameValuePairs.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:508
Standard names for retrieving values by name when working with NameValuePairs.
Decoder for bases that are a power of 2.
Definition: basecode.h:59
static void InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
Initializes BaseN lookup array.
BaseN_Decoder(const int *lookup, int log2base, BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:78
BaseN_Decoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Decoder.
Definition: basecode.h:65
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Encoder for bases that are a power of 2.
Definition: basecode.h:18
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
BaseN_Encoder(const byte *alphabet, int log2base, BufferedTransformation *attachment=NULL, int padding=-1)
Construct a BaseN_Encoder.
Definition: basecode.h:34
BaseN_Encoder(BufferedTransformation *attachment=NULL)
Construct a BaseN_Encoder.
Definition: basecode.h:22
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Interface for buffered transformations.
Definition: cryptlib.h:1652
Base class for bufferless filters.
Definition: simple.h:120
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:25
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
Filter that breaks input stream into groups of fixed size.
Definition: basecode.h:113
Grouper(int groupSize, const std::string &separator, const std::string &terminator, BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:125
Grouper(BufferedTransformation *attachment=NULL)
Construct a Grouper.
Definition: basecode.h:117
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Interface for retrieving values given their names.
Definition: cryptlib.h:322
SecBlock<byte> typedef.
Definition: secblock.h:1226
Base class for unflushable filters.
Definition: simple.h:134
#define CRYPTOPP_API
Win32 calling convention.
Definition: config_dll.h:119
Abstract base classes that provide a uniform interface to this library.
Implementation of BufferedTransformation's attachment interface.
Crypto++ library namespace.
const char * DecodingLookupArray()
const byte *
Definition: argnames.h:76
const char * GroupSize()
int
Definition: argnames.h:71
const char * PaddingByte()
byte
Definition: argnames.h:73
const char * EncodingLookupArray()
const byte *
Definition: argnames.h:75
const char * Pad()
bool
Definition: argnames.h:72
const char * Terminator()
ConstByteArrayParameter.
Definition: argnames.h:69
const char * Log2Base()
int
Definition: argnames.h:74
const char * Separator()
ConstByteArrayParameter.
Definition: argnames.h:68