6 #ifndef CRYPTOPP_IMPORTS
19 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
20 throw InvalidArgument(
"BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
24 if (parameters.
GetValue(Name::PaddingByte(), padding))
28 m_padding = pad ? padding : -1;
30 m_bytePos = m_bitPos = 0;
33 while (i%m_bitsPerChar != 0)
35 m_outputBlockSize = i/m_bitsPerChar;
37 m_outBuf.
New(m_outputBlockSize);
43 while (m_inputPosition < length)
46 std::memset(m_outBuf, 0, m_outputBlockSize);
49 unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8;
53 unsigned int bitsLeftInTarget = (
unsigned int)(m_bitsPerChar-m_bitPos);
54 m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget);
55 if (bitsLeftInSource >= bitsLeftInTarget)
59 bitsLeftInSource -= bitsLeftInTarget;
60 if (bitsLeftInSource == 0)
62 b <<= bitsLeftInTarget;
67 m_bitPos += bitsLeftInSource;
74 if (m_bytePos == m_outputBlockSize)
77 for (i=0; i<m_bytePos; i++)
80 m_outBuf[i] = m_alphabet[m_outBuf[i]];
82 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
84 m_bytePos = m_bitPos = 0;
93 for (i=0; i<m_bytePos; i++)
94 m_outBuf[i] = m_alphabet[m_outBuf[i]];
96 if (m_padding != -1 && m_bytePos > 0)
98 std::memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
99 m_bytePos = m_outputBlockSize;
101 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
102 m_bytePos = m_bitPos = 0;
104 FILTER_END_NO_MESSAGE_END;
112 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
113 throw InvalidArgument(
"BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
115 m_bytePos = m_bitPos = 0;
117 int i = m_bitsPerChar;
120 m_outputBlockSize = i/8;
122 m_outBuf.
New(m_outputBlockSize);
125 size_t BaseN_Decoder::Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
128 while (m_inputPosition < length)
131 value = m_lookup[begin[m_inputPosition++]];
135 if (m_bytePos == 0 && m_bitPos == 0)
136 std::memset(m_outBuf, 0, m_outputBlockSize);
139 int newBitPos = m_bitPos + m_bitsPerChar;
141 m_outBuf[m_bytePos] |= value << (8-newBitPos);
144 m_outBuf[m_bytePos] |= value >> (newBitPos-8);
145 m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
148 m_bitPos = newBitPos;
149 while (m_bitPos >= 8)
156 if (m_bytePos == m_outputBlockSize)
158 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
159 m_bytePos = m_bitPos = 0;
164 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
165 m_bytePos = m_bitPos = 0;
167 FILTER_END_NO_MESSAGE_END;
172 std::fill(lookup, lookup+256, -1);
174 for (
unsigned int i=0; i<base; i++)
179 if (caseInsensitive && isalpha(alphabet[i]))
181 lookup[toupper(alphabet[i])] = i;
182 lookup[tolower(alphabet[i])] = i;
186 lookup[alphabet[i]] = i;
198 parameters.
GetValue(Name::Separator(), separator);
199 parameters.
GetValue(Name::Terminator(), terminator);
206 size_t Grouper::Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
211 while (m_inputPosition < length)
213 if (m_counter == m_groupSize)
215 FILTER_OUTPUT(1, m_separator, m_separator.
size(), 0);
220 FILTER_OUTPUT2(2, (len =
STDMIN(length-m_inputPosition, m_groupSize-m_counter)),
221 begin+m_inputPosition, len, 0);
222 m_inputPosition += len;
227 FILTER_OUTPUT(3, begin, length, 0);
231 FILTER_OUTPUT(4, m_terminator, m_terminator.
size(), messageEnd);
234 FILTER_END_NO_MESSAGE_END