5 #ifndef CRYPTOPP_IMPORTS
11 NAMESPACE_BEGIN(CryptoPP)
15 parameters.GetRequiredParameter(
"BaseN_Encoder", Name::EncodingLookupArray(), m_alphabet);
17 parameters.GetRequiredIntParameter(
"BaseN_Encoder", Name::Log2Base(), m_bitsPerChar);
18 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
19 throw InvalidArgument(
"BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
23 if (parameters.GetValue(Name::PaddingByte(), padding))
24 pad = parameters.GetValueWithDefault(Name::Pad(),
true);
27 m_padding = pad ? padding : -1;
29 m_bytePos = m_bitPos = 0;
32 while (i%m_bitsPerChar != 0)
34 m_outputBlockSize = i/m_bitsPerChar;
36 m_outBuf.New(m_outputBlockSize);
42 while (m_inputPosition < length)
45 memset(m_outBuf, 0, m_outputBlockSize);
48 unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8;
51 assert(m_bitPos < m_bitsPerChar);
52 unsigned int bitsLeftInTarget = m_bitsPerChar-m_bitPos;
53 m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget);
54 if (bitsLeftInSource >= bitsLeftInTarget)
58 bitsLeftInSource -= bitsLeftInTarget;
59 if (bitsLeftInSource == 0)
61 b <<= bitsLeftInTarget;
66 m_bitPos += bitsLeftInSource;
72 assert(m_bytePos <= m_outputBlockSize);
73 if (m_bytePos == m_outputBlockSize)
76 for (i=0; i<m_bytePos; i++)
78 assert(m_outBuf[i] < (1 << m_bitsPerChar));
79 m_outBuf[i] = m_alphabet[m_outBuf[i]];
81 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
83 m_bytePos = m_bitPos = 0;
92 for (i=0; i<m_bytePos; i++)
93 m_outBuf[i] = m_alphabet[m_outBuf[i]];
95 if (m_padding != -1 && m_bytePos > 0)
97 memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
98 m_bytePos = m_outputBlockSize;
100 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
101 m_bytePos = m_bitPos = 0;
103 FILTER_END_NO_MESSAGE_END;
106 void BaseN_Decoder::IsolatedInitialize(
const NameValuePairs ¶meters)
108 parameters.GetRequiredParameter(
"BaseN_Decoder", Name::DecodingLookupArray(), m_lookup);
110 parameters.GetRequiredIntParameter(
"BaseN_Decoder", Name::Log2Base(), m_bitsPerChar);
111 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
112 throw InvalidArgument(
"BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
114 m_bytePos = m_bitPos = 0;
116 int i = m_bitsPerChar;
119 m_outputBlockSize = i/8;
121 m_outBuf.
New(m_outputBlockSize);
127 while (m_inputPosition < length)
130 value = m_lookup[begin[m_inputPosition++]];
134 if (m_bytePos == 0 && m_bitPos == 0)
135 memset(m_outBuf, 0, m_outputBlockSize);
138 int newBitPos = m_bitPos + m_bitsPerChar;
140 m_outBuf[m_bytePos] |= value << (8-newBitPos);
143 m_outBuf[m_bytePos] |= value >> (newBitPos-8);
144 m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
147 m_bitPos = newBitPos;
148 while (m_bitPos >= 8)
155 if (m_bytePos == m_outputBlockSize)
157 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
158 m_bytePos = m_bitPos = 0;
163 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
164 m_bytePos = m_bitPos = 0;
166 FILTER_END_NO_MESSAGE_END;
169 void BaseN_Decoder::InitializeDecodingLookupArray(
int *lookup,
const byte *alphabet,
unsigned int base,
bool caseInsensitive)
171 std::fill(lookup, lookup+256, -1);
173 for (
unsigned int i=0; i<base; i++)
175 if (caseInsensitive && isalpha(alphabet[i]))
177 assert(lookup[toupper(alphabet[i])] == -1);
178 lookup[toupper(alphabet[i])] = i;
179 assert(lookup[tolower(alphabet[i])] == -1);
180 lookup[tolower(alphabet[i])] = i;
184 assert(lookup[alphabet[i]] == -1);
185 lookup[alphabet[i]] = i;
190 void Grouper::IsolatedInitialize(
const NameValuePairs ¶meters)
195 parameters.GetRequiredParameter(
"Grouper", Name::Separator(), separator);
197 parameters.
GetValue(Name::Separator(), separator);
198 parameters.
GetValue(Name::Terminator(), terminator);
200 m_separator.
Assign(separator.begin(), separator.size());
201 m_terminator.
Assign(terminator.begin(), terminator.size());
205 size_t Grouper::Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
210 while (m_inputPosition < length)
212 if (m_counter == m_groupSize)
214 FILTER_OUTPUT(1, m_separator, m_separator.size(), 0);
219 FILTER_OUTPUT2(2, len = STDMIN(length-m_inputPosition, m_groupSize-m_counter),
220 begin+m_inputPosition, len, 0);
221 m_inputPosition += len;
226 FILTER_OUTPUT(3, begin, length, 0);
230 FILTER_OUTPUT(4, m_terminator, m_terminator.size(), messageEnd);
233 FILTER_END_NO_MESSAGE_END
used to pass byte array input as part of a NameValuePairs object
exception thrown when an invalid argument is detected
void New(size_type newSize)
change size, without preserving contents
bool GetValue(const char *name, T &value) const
get a named value, returns true if the name exists
int GetIntValueWithDefault(const char *name, int defaultValue) const
get a named value with type int, with default
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
void Assign(const T *t, size_type len)
set contents and size
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
base n encoder, where n is a power of 2
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
interface for retrieving values given their names