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