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