Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/basecode.cpp
2 views
// basecode.cpp - originally written and placed in the public domain by Wei Dai12#include "pch.h"3#include "config.h"45#ifndef CRYPTOPP_IMPORTS67#include "basecode.h"8#include "fltrimpl.h"9#include <ctype.h>1011NAMESPACE_BEGIN(CryptoPP)1213void BaseN_Encoder::IsolatedInitialize(const NameValuePairs ¶meters)14{15parameters.GetRequiredParameter("BaseN_Encoder", Name::EncodingLookupArray(), m_alphabet);1617parameters.GetRequiredIntParameter("BaseN_Encoder", Name::Log2Base(), m_bitsPerChar);18if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)19throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");2021byte padding;22bool pad;23if (parameters.GetValue(Name::PaddingByte(), padding))24pad = parameters.GetValueWithDefault(Name::Pad(), true);25else26pad = false;27m_padding = pad ? padding : -1;2829m_bytePos = m_bitPos = 0;3031int i = 8;32while (i%m_bitsPerChar != 0)33i += 8;34m_outputBlockSize = i/m_bitsPerChar;3536m_outBuf.New(m_outputBlockSize);37}3839size_t BaseN_Encoder::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)40{41FILTER_BEGIN;42while (m_inputPosition < length)43{44if (m_bytePos == 0)45std::memset(m_outBuf, 0, m_outputBlockSize);4647{48unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8;49while (true)50{51CRYPTOPP_ASSERT(m_bitsPerChar-m_bitPos >= 0);52unsigned int bitsLeftInTarget = (unsigned int)(m_bitsPerChar-m_bitPos);53m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget);54if (bitsLeftInSource >= bitsLeftInTarget)55{56m_bitPos = 0;57++m_bytePos;58bitsLeftInSource -= bitsLeftInTarget;59if (bitsLeftInSource == 0)60break;61b <<= bitsLeftInTarget;62b &= 0xff;63}64else65{66m_bitPos += bitsLeftInSource;67break;68}69}70}7172CRYPTOPP_ASSERT(m_bytePos <= m_outputBlockSize);73if (m_bytePos == m_outputBlockSize)74{75int i;76for (i=0; i<m_bytePos; i++)77{78CRYPTOPP_ASSERT(m_outBuf[i] < (1 << m_bitsPerChar));79m_outBuf[i] = m_alphabet[m_outBuf[i]];80}81FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);8283m_bytePos = m_bitPos = 0;84}85}86if (messageEnd)87{88if (m_bitPos > 0)89++m_bytePos;9091int i;92for (i=0; i<m_bytePos; i++)93m_outBuf[i] = m_alphabet[m_outBuf[i]];9495if (m_padding != -1 && m_bytePos > 0)96{97std::memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);98m_bytePos = m_outputBlockSize;99}100FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);101m_bytePos = m_bitPos = 0;102}103FILTER_END_NO_MESSAGE_END;104}105106void BaseN_Decoder::IsolatedInitialize(const NameValuePairs ¶meters)107{108parameters.GetRequiredParameter("BaseN_Decoder", Name::DecodingLookupArray(), m_lookup);109110parameters.GetRequiredIntParameter("BaseN_Decoder", Name::Log2Base(), m_bitsPerChar);111if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)112throw InvalidArgument("BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");113114m_bytePos = m_bitPos = 0;115116int i = m_bitsPerChar;117while (i%8 != 0)118i += m_bitsPerChar;119m_outputBlockSize = i/8;120121m_outBuf.New(m_outputBlockSize);122}123124size_t BaseN_Decoder::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)125{126FILTER_BEGIN;127while (m_inputPosition < length)128{129unsigned int value;130value = m_lookup[begin[m_inputPosition++]];131if (value >= 256)132continue;133134if (m_bytePos == 0 && m_bitPos == 0)135std::memset(m_outBuf, 0, m_outputBlockSize);136137{138int newBitPos = m_bitPos + m_bitsPerChar;139if (newBitPos <= 8)140m_outBuf[m_bytePos] |= value << (8-newBitPos);141else142{143m_outBuf[m_bytePos] |= value >> (newBitPos-8);144m_outBuf[m_bytePos+1] |= value << (16-newBitPos);145}146147m_bitPos = newBitPos;148while (m_bitPos >= 8)149{150m_bitPos -= 8;151++m_bytePos;152}153}154155if (m_bytePos == m_outputBlockSize)156{157FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);158m_bytePos = m_bitPos = 0;159}160}161if (messageEnd)162{163FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);164m_bytePos = m_bitPos = 0;165}166FILTER_END_NO_MESSAGE_END;167}168169void BaseN_Decoder::InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)170{171std::fill(lookup, lookup+256, -1);172173for (unsigned int i=0; i<base; i++)174{175// Debug asserts for 'lookup[alphabet[i]] == -1' removed because the self tests176// have unusual tests that try to break the encoders and decoders. Tests include177// a string of the same characters. I.,e., a string of stars like '********...'.178if (caseInsensitive && isalpha(alphabet[i]))179{180lookup[toupper(alphabet[i])] = i;181lookup[tolower(alphabet[i])] = i;182}183else184{185lookup[alphabet[i]] = i;186}187}188}189190void Grouper::IsolatedInitialize(const NameValuePairs ¶meters)191{192m_groupSize = parameters.GetIntValueWithDefault(Name::GroupSize(), 0);193ConstByteArrayParameter separator, terminator;194if (m_groupSize)195parameters.GetRequiredParameter("Grouper", Name::Separator(), separator);196else197parameters.GetValue(Name::Separator(), separator);198parameters.GetValue(Name::Terminator(), terminator);199200m_separator.Assign(separator.begin(), separator.size());201m_terminator.Assign(terminator.begin(), terminator.size());202m_counter = 0;203}204205size_t Grouper::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)206{207FILTER_BEGIN;208if (m_groupSize)209{210while (m_inputPosition < length)211{212if (m_counter == m_groupSize)213{214FILTER_OUTPUT(1, m_separator, m_separator.size(), 0);215m_counter = 0;216}217218size_t len;219FILTER_OUTPUT2(2, (len = STDMIN(length-m_inputPosition, m_groupSize-m_counter)),220begin+m_inputPosition, len, 0);221m_inputPosition += len;222m_counter += len;223}224}225else226FILTER_OUTPUT(3, begin, length, 0);227228if (messageEnd)229{230FILTER_OUTPUT(4, m_terminator, m_terminator.size(), messageEnd);231m_counter = 0;232}233FILTER_END_NO_MESSAGE_END234}235236NAMESPACE_END237238#endif239240241