summaryrefslogtreecommitdiff
path: root/basecode.cpp
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
committerweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
commita3b6ece7ab341b5b14135baeccea7d5e4c086771 (patch)
tree8b045309c238226c32a563b1df6b9c30a2f0e0b3 /basecode.cpp
downloadcryptopp-git-a3b6ece7ab341b5b14135baeccea7d5e4c086771.tar.gz
Initial revision
Diffstat (limited to 'basecode.cpp')
-rw-r--r--basecode.cpp214
1 files changed, 214 insertions, 0 deletions
diff --git a/basecode.cpp b/basecode.cpp
new file mode 100644
index 00000000..51347b60
--- /dev/null
+++ b/basecode.cpp
@@ -0,0 +1,214 @@
+// basecode.cpp - written and placed in the public domain by Wei Dai
+
+#include "pch.h"
+#include "basecode.h"
+#include "fltrimpl.h"
+#include <ctype.h>
+
+NAMESPACE_BEGIN(CryptoPP)
+
+void BaseN_Encoder::IsolatedInitialize(const NameValuePairs &parameters)
+{
+ parameters.GetRequiredParameter("BaseN_Encoder", "EncodingLookupArray", m_alphabet);
+
+ parameters.GetRequiredIntParameter("BaseN_Encoder", "Log2Base", m_bitsPerChar);
+ if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
+ throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
+
+ byte padding;
+ bool pad;
+ if (parameters.GetValue("PaddingByte", padding))
+ pad = parameters.GetValueWithDefault("Pad", true);
+ else
+ pad = false;
+ m_padding = pad ? padding : -1;
+
+ m_bytePos = m_bitPos = 0;
+
+ int i = 8;
+ while (i%m_bitsPerChar != 0)
+ i += 8;
+ m_outputBlockSize = i/m_bitsPerChar;
+
+ m_outBuf.New(m_outputBlockSize);
+}
+
+unsigned int BaseN_Encoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
+{
+ FILTER_BEGIN;
+ while (m_inputPosition < length)
+ {
+ if (m_bytePos == 0)
+ memset(m_outBuf, 0, m_outputBlockSize);
+
+ m_outBuf[m_bytePos] |= begin[m_inputPosition] >> (8-m_bitsPerChar+m_bitPos);
+ m_outBuf[m_bytePos+1] |= ((begin[m_inputPosition] << (m_bitsPerChar-m_bitPos)) & 0xff) >> (8-m_bitsPerChar);
+ ++m_inputPosition;
+
+ m_bitPos += 8;
+ while (m_bitPos >= m_bitsPerChar)
+ {
+ m_bitPos -= m_bitsPerChar;
+ ++m_bytePos;
+ }
+
+ if (m_bytePos == m_outputBlockSize)
+ {
+ int i;
+ for (i=0; i<m_bytePos; i++)
+ {
+ assert(m_outBuf[i] < (1 << m_bitsPerChar));
+ m_outBuf[i] = m_alphabet[m_outBuf[i]];
+ }
+ FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
+
+ m_bytePos = m_bitPos = 0;
+ }
+ }
+ if (messageEnd)
+ {
+ if (m_bitPos > 0)
+ ++m_bytePos;
+
+ int i;
+ for (i=0; i<m_bytePos; i++)
+ m_outBuf[i] = m_alphabet[m_outBuf[i]];
+
+ if (m_padding != -1 && m_bytePos > 0)
+ {
+ memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
+ m_bytePos = m_outputBlockSize;
+ }
+ FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
+ m_bytePos = m_bitPos = 0;
+ }
+ FILTER_END;
+}
+
+void BaseN_Decoder::IsolatedInitialize(const NameValuePairs &parameters)
+{
+ parameters.GetRequiredParameter("BaseN_Decoder", "DecodingLookupArray", m_lookup);
+
+ parameters.GetRequiredIntParameter("BaseN_Decoder", "Log2Base", m_bitsPerChar);
+ if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
+ throw InvalidArgument("BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
+
+ m_bytePos = m_bitPos = 0;
+
+ int i = m_bitsPerChar;
+ while (i%8 != 0)
+ i += m_bitsPerChar;
+ m_outputBlockSize = i/8;
+
+ m_outBuf.New(m_outputBlockSize);
+}
+
+unsigned int BaseN_Decoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
+{
+ FILTER_BEGIN;
+ while (m_inputPosition < length)
+ {
+ unsigned int value;
+ value = m_lookup[begin[m_inputPosition++]];
+ if (value >= 256)
+ continue;
+
+ if (m_bytePos == 0 && m_bitPos == 0)
+ memset(m_outBuf, 0, m_outputBlockSize);
+
+ {
+ int newBitPos = m_bitPos + m_bitsPerChar;
+ if (newBitPos <= 8)
+ m_outBuf[m_bytePos] |= value << (8-newBitPos);
+ else
+ {
+ m_outBuf[m_bytePos] |= value >> (newBitPos-8);
+ m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
+ }
+
+ m_bitPos = newBitPos;
+ while (m_bitPos >= 8)
+ {
+ m_bitPos -= 8;
+ ++m_bytePos;
+ }
+ }
+
+ if (m_bytePos == m_outputBlockSize)
+ {
+ FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
+ m_bytePos = m_bitPos = 0;
+ }
+ }
+ if (messageEnd)
+ {
+ FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
+ m_bytePos = m_bitPos = 0;
+ }
+ FILTER_END_NO_MESSAGE_END;
+}
+
+void BaseN_Decoder::InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
+{
+ std::fill(lookup, lookup+256, -1);
+
+ for (unsigned int i=0; i<base; i++)
+ {
+ if (caseInsensitive && isalpha(alphabet[i]))
+ {
+ assert(lookup[toupper(alphabet[i])] == -1);
+ lookup[toupper(alphabet[i])] = i;
+ assert(lookup[tolower(alphabet[i])] == -1);
+ lookup[tolower(alphabet[i])] = i;
+ }
+ else
+ {
+ assert(lookup[alphabet[i]] == -1);
+ lookup[alphabet[i]] = i;
+ }
+ }
+}
+
+void Grouper::IsolatedInitialize(const NameValuePairs &parameters)
+{
+ m_groupSize = parameters.GetIntValueWithDefault("GroupSize", 0);
+ ConstByteArrayParameter seperator, terminator;
+ if (m_groupSize)
+ parameters.GetRequiredParameter("Grouper", "Seperator", seperator);
+ else
+ parameters.GetValue("Seperator", seperator);
+ parameters.GetValue("Terminator", terminator);
+
+ m_seperator.Assign(seperator.begin(), seperator.size());
+ m_terminator.Assign(terminator.begin(), terminator.size());
+ m_counter = 0;
+}
+
+unsigned int Grouper::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
+{
+ if (m_groupSize)
+ {
+ FILTER_BEGIN;
+ while (m_inputPosition < length)
+ {
+ if (m_counter == m_groupSize)
+ {
+ FILTER_OUTPUT(1, m_seperator, m_seperator.size(), 0);
+ m_counter = 0;
+ }
+
+ unsigned int len;
+ FILTER_OUTPUT2(2, len = STDMIN(length-m_inputPosition, m_groupSize-m_counter),
+ begin+m_inputPosition, len, 0);
+ m_inputPosition += len;
+ m_counter += len;
+ }
+ if (messageEnd)
+ FILTER_OUTPUT(3, m_terminator, m_terminator.size(), messageEnd);
+ FILTER_END_NO_MESSAGE_END
+ }
+ else
+ return Output(0, begin, length, messageEnd, blocking);
+}
+
+NAMESPACE_END