00001
00002
00003 #include "pch.h"
00004
00005 #ifndef CRYPTOPP_IMPORTS
00006
00007 #include "basecode.h"
00008 #include "fltrimpl.h"
00009 #include <ctype.h>
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00013 void BaseN_Encoder::IsolatedInitialize(const NameValuePairs ¶meters)
00014 {
00015 parameters.GetRequiredParameter("BaseN_Encoder", "EncodingLookupArray", m_alphabet);
00016
00017 parameters.GetRequiredIntParameter("BaseN_Encoder", "Log2Base", m_bitsPerChar);
00018 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
00019 throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
00020
00021 byte padding;
00022 bool pad;
00023 if (parameters.GetValue("PaddingByte", padding))
00024 pad = parameters.GetValueWithDefault("Pad", true);
00025 else
00026 pad = false;
00027 m_padding = pad ? padding : -1;
00028
00029 m_bytePos = m_bitPos = 0;
00030
00031 int i = 8;
00032 while (i%m_bitsPerChar != 0)
00033 i += 8;
00034 m_outputBlockSize = i/m_bitsPerChar;
00035
00036 m_outBuf.New(m_outputBlockSize);
00037 }
00038
00039 unsigned int BaseN_Encoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
00040 {
00041 FILTER_BEGIN;
00042 while (m_inputPosition < length)
00043 {
00044 if (m_bytePos == 0)
00045 memset(m_outBuf, 0, m_outputBlockSize);
00046
00047 m_outBuf[m_bytePos] |= begin[m_inputPosition] >> (8-m_bitsPerChar+m_bitPos);
00048 m_outBuf[m_bytePos+1] |= ((begin[m_inputPosition] << (m_bitsPerChar-m_bitPos)) & 0xff) >> (8-m_bitsPerChar);
00049 ++m_inputPosition;
00050
00051 m_bitPos += 8;
00052 while (m_bitPos >= m_bitsPerChar)
00053 {
00054 m_bitPos -= m_bitsPerChar;
00055 ++m_bytePos;
00056 }
00057
00058 if (m_bytePos == m_outputBlockSize)
00059 {
00060 int i;
00061 for (i=0; i<m_bytePos; i++)
00062 {
00063 assert(m_outBuf[i] < (1 << m_bitsPerChar));
00064 m_outBuf[i] = m_alphabet[m_outBuf[i]];
00065 }
00066 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
00067
00068 m_bytePos = m_bitPos = 0;
00069 }
00070 }
00071 if (messageEnd)
00072 {
00073 if (m_bitPos > 0)
00074 ++m_bytePos;
00075
00076 int i;
00077 for (i=0; i<m_bytePos; i++)
00078 m_outBuf[i] = m_alphabet[m_outBuf[i]];
00079
00080 if (m_padding != -1 && m_bytePos > 0)
00081 {
00082 memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
00083 m_bytePos = m_outputBlockSize;
00084 }
00085 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
00086 m_bytePos = m_bitPos = 0;
00087 }
00088 FILTER_END;
00089 }
00090
00091 void BaseN_Decoder::IsolatedInitialize(const NameValuePairs ¶meters)
00092 {
00093 parameters.GetRequiredParameter("BaseN_Decoder", "DecodingLookupArray", m_lookup);
00094
00095 parameters.GetRequiredIntParameter("BaseN_Decoder", "Log2Base", m_bitsPerChar);
00096 if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
00097 throw InvalidArgument("BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
00098
00099 m_bytePos = m_bitPos = 0;
00100
00101 int i = m_bitsPerChar;
00102 while (i%8 != 0)
00103 i += m_bitsPerChar;
00104 m_outputBlockSize = i/8;
00105
00106 m_outBuf.New(m_outputBlockSize);
00107 }
00108
00109 unsigned int BaseN_Decoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
00110 {
00111 FILTER_BEGIN;
00112 while (m_inputPosition < length)
00113 {
00114 unsigned int value;
00115 value = m_lookup[begin[m_inputPosition++]];
00116 if (value >= 256)
00117 continue;
00118
00119 if (m_bytePos == 0 && m_bitPos == 0)
00120 memset(m_outBuf, 0, m_outputBlockSize);
00121
00122 {
00123 int newBitPos = m_bitPos + m_bitsPerChar;
00124 if (newBitPos <= 8)
00125 m_outBuf[m_bytePos] |= value << (8-newBitPos);
00126 else
00127 {
00128 m_outBuf[m_bytePos] |= value >> (newBitPos-8);
00129 m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
00130 }
00131
00132 m_bitPos = newBitPos;
00133 while (m_bitPos >= 8)
00134 {
00135 m_bitPos -= 8;
00136 ++m_bytePos;
00137 }
00138 }
00139
00140 if (m_bytePos == m_outputBlockSize)
00141 {
00142 FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
00143 m_bytePos = m_bitPos = 0;
00144 }
00145 }
00146 if (messageEnd)
00147 {
00148 FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
00149 m_bytePos = m_bitPos = 0;
00150 }
00151 FILTER_END_NO_MESSAGE_END;
00152 }
00153
00154 void BaseN_Decoder::InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
00155 {
00156 std::fill(lookup, lookup+256, -1);
00157
00158 for (unsigned int i=0; i<base; i++)
00159 {
00160 if (caseInsensitive && isalpha(alphabet[i]))
00161 {
00162 assert(lookup[toupper(alphabet[i])] == -1);
00163 lookup[toupper(alphabet[i])] = i;
00164 assert(lookup[tolower(alphabet[i])] == -1);
00165 lookup[tolower(alphabet[i])] = i;
00166 }
00167 else
00168 {
00169 assert(lookup[alphabet[i]] == -1);
00170 lookup[alphabet[i]] = i;
00171 }
00172 }
00173 }
00174
00175 void Grouper::IsolatedInitialize(const NameValuePairs ¶meters)
00176 {
00177 m_groupSize = parameters.GetIntValueWithDefault("GroupSize", 0);
00178 ConstByteArrayParameter seperator, terminator;
00179 if (m_groupSize)
00180 parameters.GetRequiredParameter("Grouper", "Seperator", seperator);
00181 else
00182 parameters.GetValue("Seperator", seperator);
00183 parameters.GetValue("Terminator", terminator);
00184
00185 m_seperator.Assign(seperator.begin(), seperator.size());
00186 m_terminator.Assign(terminator.begin(), terminator.size());
00187 m_counter = 0;
00188 }
00189
00190 unsigned int Grouper::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
00191 {
00192 if (m_groupSize)
00193 {
00194 FILTER_BEGIN;
00195 while (m_inputPosition < length)
00196 {
00197 if (m_counter == m_groupSize)
00198 {
00199 FILTER_OUTPUT(1, m_seperator, m_seperator.size(), 0);
00200 m_counter = 0;
00201 }
00202
00203 unsigned int len;
00204 FILTER_OUTPUT2(2, len = STDMIN(length-m_inputPosition, m_groupSize-m_counter),
00205 begin+m_inputPosition, len, 0);
00206 m_inputPosition += len;
00207 m_counter += len;
00208 }
00209 if (messageEnd)
00210 FILTER_OUTPUT(3, m_terminator, m_terminator.size(), messageEnd);
00211 FILTER_END_NO_MESSAGE_END
00212 }
00213 else
00214 return Output(0, begin, length, messageEnd, blocking);
00215 }
00216
00217 NAMESPACE_END
00218
00219 #endif