Files

1783 lines
59 KiB
C++

/**
* @file include/ActivMiddleware/Misc/Base64.h
*
* @brief Header file for base64 functions.
*/
#if (!defined ACTIV_MISC_BASE64_H)
#define ACTIV_MISC_BASE64_H
#include "ActivMiddleware/ActivMiddleware.h"
#include ACTIV_BOOST_INCLUDE(mpl/integral_c.hpp)
#include ACTIV_BOOST_INCLUDE(mpl/if.hpp)
#include ACTIV_BOOST_INCLUDE(mpl/equal_to.hpp)
#include ACTIV_BOOST_INCLUDE(type_traits/is_base_of.hpp)
#include <cassert>
//! Activ namespace.
namespace Activ
{
/**
* @brief Empty base for Base64SingleBufferIterator to ease template code.
*/
class Base64SingleBufferIteratorBase
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// Forward reference.
template<typename POINTER>
class Base64SingleBufferIteratorHelper;
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Base64 constants.
*/
class Base64Constants
{
public:
static const byte_t undefinedBits = Boost::integer_traits<byte_t>::const_max;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Base64 codec class.
*
* @param UTF7 whether to use UTF7 ("modified base64") encoding. Non-UTF7 encoded data is always a multiple of 4 bytes, padded
* by PAD.
* @param SIXTY_TWO the character to use for the binary value 62.
* @param SIXTY_THREE the character to use for the binary value 63.
* @param PAD the character to use for padding in the output of non-UTF7 format.
*/
template<bool UTF7, char SIXTY_TWO = '+', char SIXTY_THREE = '/', char PAD = '='>
class Base64
{
public:
/**
* @brief Stateful base64 decoder class.
*/
class Validator
{
public:
/**
* @brief Constructor.
*
* @param pData pointer to the base64-encoded character stream.
* @param dataLength length of data pointed to by @a pData.
*/
Validator(const void * const pData, const size_t dataLength);
/**
* @brief Decode some of the base64-encoded stream.
*
* @param pOutput pointer to a buffer to receive the decoded binary.
* @param outputLength on input, the length of the buffer pointed to by @a pOutput. On output, the number of bytes written
* to @a pOutput.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_BUFFER_UNDERRUN pOutput has had outputLength bytes written to it but there are more input bytes
* left to process. Call operator() again to continue decoding.
* @retval STATUS_CODE_WRONG_STATE internal error.
*/
StatusCode operator()(void * const pOutput, size_t &outputLength);
/**
* @brief Decode some of the base64-encoded stream into multiple buffers.
*
* ITERATOR_HELPER is a class that should provide static members size_t GetLength(iterator &) and
* void *GetData(iterator &) in order to access the length and data pointer from a buffer iterator.
*
* @param begin iterator to the first buffer.
* @param end iterator to passed the last buffer.
* @param outputLength on output, the number of bytes written across the buffers provided.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_BUFFER_UNDERRUN output buffers have been filled. There are more input bytes
* left to process. Call operator() again to continue decoding.
* @retval STATUS_CODE_WRONG_STATE internal error.
*/
template<typename ITERATOR_HELPER, typename ITERATOR>
StatusCode operator()(ITERATOR begin, ITERATOR end, size_t &outputLength)
{
// Quick hack to avoid multiple buffer range checking for a single buffer invocation.
// Compiler should make this all disappear at compile-time.
const bool isMultipleBuffer = !Boost::is_base_of<Base64SingleBufferIteratorBase, ITERATOR>::value;
ITERATOR currentIter = begin;
byte_t *pNextOutput = reinterpret_cast<byte_t *>(ITERATOR_HELPER::GetData(currentIter));
size_t outputBytesLeft = ITERATOR_HELPER::GetLength(currentIter);
char inputChar;
bool isDone = false;
outputLength = 0;
while (!isDone)
{
switch (m_state)
{
case STATE_1:
// First byte.
if (ACTIV_UNLIKELY(0 == m_inputBytesLeft))
{
isDone = true;
}
else
{
if (UTF7)
{
if (ACTIV_UNLIKELY(m_inputBytesLeft < 2))
return STATUS_CODE_INVALID_FORMAT;
}
else
{
if (ACTIV_UNLIKELY(m_inputBytesLeft < 4))
return STATUS_CODE_INVALID_FORMAT;
}
inputChar = *m_pInput++;
--m_inputBytesLeft;
m_decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == m_decodedByte))
return STATUS_CODE_INVALID_FORMAT;
m_outputByte = (m_decodedByte & Binary<byte_t, 111111>::value) << 2;
m_state = STATE_2;
}
break;
case STATE_2:
inputChar = *m_pInput++;
--m_inputBytesLeft;
m_decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == m_decodedByte))
return STATUS_CODE_INVALID_FORMAT;
m_outputByte |= (m_decodedByte >> 4) & Binary<byte_t, 11>::value;
m_state = STATE_ASSIGN_1;
break;
case STATE_ASSIGN_1:
if (!isMultipleBuffer)
{
if (0 == outputBytesLeft)
return STATUS_CODE_BUFFER_UNDERRUN;
}
else
{
if ((0 == outputBytesLeft) && !AdvanceIterator<ITERATOR_HELPER>(currentIter, outputBytesLeft,
pNextOutput, end))
{
return STATUS_CODE_BUFFER_UNDERRUN;
}
}
*pNextOutput++ = m_outputByte;
--outputBytesLeft;
++outputLength;
m_outputByte = (m_decodedByte & Binary<byte_t, 1111>::value) << 4;
m_state = STATE_3;
break;
case STATE_3:
// Third byte.
if (UTF7)
{
// End of input terminates with UTF7.
if (ACTIV_UNLIKELY(0 == m_inputBytesLeft))
{
isDone = true;
break;
}
}
inputChar = *m_pInput++;
--m_inputBytesLeft;
if (ACTIV_LIKELY(undefinedChar != inputChar))
{
m_decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == m_decodedByte))
return STATUS_CODE_INVALID_FORMAT;
m_outputByte |= (m_decodedByte >> 2) & Binary<byte_t, 1111>::value;
m_state = STATE_ASSIGN_2;
}
else
{
if (UTF7)
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != m_outputByte))
return STATUS_CODE_INVALID_FORMAT;
// Fourth byte must also be PAD if third was and we must be at the end.
inputChar = *m_pInput++;
--m_inputBytesLeft;
if (ACTIV_UNLIKELY(undefinedChar != inputChar) || ACTIV_UNLIKELY(0 != m_inputBytesLeft))
return STATUS_CODE_INVALID_FORMAT;
isDone = true;
}
break;
case STATE_ASSIGN_2:
if (!isMultipleBuffer)
{
if (0 == outputBytesLeft)
return STATUS_CODE_BUFFER_UNDERRUN;
}
else
{
if ((0 == outputBytesLeft) && !AdvanceIterator<ITERATOR_HELPER>(currentIter, outputBytesLeft,
pNextOutput, end))
{
return STATUS_CODE_BUFFER_UNDERRUN;
}
}
*pNextOutput++ = m_outputByte;
--outputBytesLeft;
++outputLength;
m_outputByte = (m_decodedByte & Binary<byte_t, 11>::value) << 6;
m_state = STATE_4;
break;
case STATE_4:
// Fourth byte.
if (UTF7)
{
// End of input terminates with UTF7.
if (ACTIV_UNLIKELY(0 == m_inputBytesLeft))
{
isDone = true;
break;
}
}
inputChar = *m_pInput++;
--m_inputBytesLeft;
if (ACTIV_LIKELY(undefinedChar != inputChar))
{
m_decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == m_decodedByte))
return STATUS_CODE_INVALID_FORMAT;
m_outputByte |= m_decodedByte & Binary<byte_t, 111111>::value;
m_state = STATE_ASSIGN_3;
}
else
{
if (UTF7)
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != m_outputByte))
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != m_inputBytesLeft))
return STATUS_CODE_INVALID_FORMAT;
isDone = true;
}
break;
case STATE_ASSIGN_3:
if (!isMultipleBuffer)
{
if (0 == outputBytesLeft)
return STATUS_CODE_BUFFER_UNDERRUN;
}
else
{
if ((0 == outputBytesLeft) && !AdvanceIterator<ITERATOR_HELPER>(currentIter, outputBytesLeft,
pNextOutput, end))
{
return STATUS_CODE_BUFFER_UNDERRUN;
}
}
*pNextOutput++ = m_outputByte;
--outputBytesLeft;
++outputLength;
m_state = STATE_1;
break;
default:
assert(false);
return STATUS_CODE_WRONG_STATE;
}
}
return STATUS_CODE_SUCCESS;
}
private:
/**
* @brief Decoding states.
*/
enum State
{
STATE_1,
STATE_2,
STATE_ASSIGN_1,
STATE_3,
STATE_ASSIGN_2,
STATE_4,
STATE_ASSIGN_3
};
State m_state; ///< Current state.
const char *m_pInput; ///< Next input byte to process.
size_t m_inputBytesLeft; ///< Number of input bytes left to process.
byte_t m_decodedByte; ///< Decoded value of current input byte.
byte_t m_outputByte; ///< Current output byte being generated.
};
/**
* @brief Encode a byte stream into base64.
*
* @param pData pointer to the data to encode.
* @param dataLength length of data pointed to by @a pData.
* @param pOutput pointer to buffer to store the base64-encoded data.
* @param outputLength on input, the size of the buffer pointed to by @a pOutput. On successful completion, the
* encoded length of the output.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW @a outputLength is not sufficient to hold the base64 encoded data.
*/
static StatusCode Encode(const void * const pData, const size_t dataLength, char * const pOutput, size_t &outputLength);
/**
* @brief Encode a byte stream into base64.
*
* ITERATOR_HELPER is a class that should provide static members size_t GetLength(iterator &) and
* void *GetData(iterator &) in order to access the length and data pointer from a buffer iterator.
*
* @param begin iterator to the first buffer.
* @param end iterator to passed the last buffer.
* @param offset offset within the buffer range begin,end at which to start encoding.
* @param dataLength the amount of data starting at offset @a offset within the buffer range to encode.
* @param pOutput pointer to buffer to store the base64-encoded data.
* @param outputLength on input, the size of the buffer pointed to by @a pOutput. On successful completion, the
* encoded length of the output.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_BUFFER_OVERFLOW @a outputLength is not sufficient to hold the base64 encoded data.
* @retval STATUS_CODE_BUFFER_UNDERRUN the iterator range begin,end starting at offset @a offset does not hold @a dataLength
* bytes.
*/
template<typename ITERATOR_HELPER, typename ITERATOR>
static StatusCode Encode(const ITERATOR begin, const ITERATOR end, const size_t offset, const size_t dataLength,
char * const pOutput, size_t &outputLength)
{
// Quick hack to avoid multiple buffer range checking for a single buffer invocation.
// Compiler should make this all disappear at compile-time.
const bool isMultipleBuffer = !Boost::is_base_of<Base64SingleBufferIteratorBase, ITERATOR>::value;
if (ACTIV_UNLIKELY(0 == dataLength))
return STATUS_CODE_SUCCESS;
if (ACTIV_UNLIKELY(outputLength < GetMaximumEncodedLength(dataLength)))
return STATUS_CODE_INVALID_PARAMETER;
// Find the iterator and offset into it for the provided offset.
ITERATOR currentIter = begin;
size_t initialOffset = offset;
while (initialOffset > 0)
{
const size_t iteratorLength = ITERATOR_HELPER::GetLength(currentIter);
if (initialOffset < iteratorLength)
break;
++currentIter;
if (currentIter == end)
return STATUS_CODE_INVALID_PARAMETER;
initialOffset -= iteratorLength;
}
char * const pInitialOutput = pOutput;
char *pNextOutput = pOutput;
size_t inputBytesLeft = dataLength;
const byte_t *pInput = reinterpret_cast<const byte_t *>(ITERATOR_HELPER::GetData(currentIter)) + initialOffset;
size_t currentIterBytesLeft = ITERATOR_HELPER::GetLength(currentIter) - initialOffset;
while (inputBytesLeft > 0)
{
byte_t inputByte;
if (ACTIV_UNLIKELY(isMultipleBuffer) && ACTIV_UNLIKELY(0 == currentIterBytesLeft))
{
// Move to next buffer in chain.
if (!AdvanceIterator<ITERATOR_HELPER>(currentIter, currentIterBytesLeft, pInput, end))
return STATUS_CODE_BUFFER_UNDERRUN;
}
inputByte = *pInput++;
--inputBytesLeft;
--currentIterBytesLeft;
const byte_t first6Bits = (inputByte >> 2) & Binary<byte_t, 111111>::value;
byte_t second6Bits = (inputByte & Binary<byte_t, 11>::value) << 4;
byte_t third6Bits = Base64Constants::undefinedBits;
byte_t fourth6Bits = Base64Constants::undefinedBits;
if (ACTIV_LIKELY(inputBytesLeft > 0))
{
if (ACTIV_UNLIKELY(isMultipleBuffer) && ACTIV_UNLIKELY(0 == currentIterBytesLeft))
{
// Move to next buffer in chain.
if (!AdvanceIterator<ITERATOR_HELPER>(currentIter, currentIterBytesLeft, pInput, end))
return STATUS_CODE_BUFFER_UNDERRUN;
}
inputByte = *pInput++;
--inputBytesLeft;
--currentIterBytesLeft;
second6Bits |= (inputByte >> 4) & Binary<byte_t, 1111>::value;
third6Bits = (inputByte & Binary<byte_t, 1111>::value) << 2;
if (ACTIV_LIKELY(inputBytesLeft > 0))
{
if (ACTIV_UNLIKELY(isMultipleBuffer) && ACTIV_UNLIKELY(0 == currentIterBytesLeft))
{
// Move to next buffer in chain.
if (!AdvanceIterator<ITERATOR_HELPER>(currentIter, currentIterBytesLeft, pInput, end))
return STATUS_CODE_BUFFER_UNDERRUN;
}
inputByte = *pInput++;
--inputBytesLeft;
--currentIterBytesLeft;
third6Bits |= (inputByte >> 6) & Binary<byte_t, 11>::value;
fourth6Bits = inputByte & Binary<byte_t, 111111>::value;
}
}
*pNextOutput++ = GetEncodedFromBinary(first6Bits);
*pNextOutput++ = GetEncodedFromBinary(second6Bits);
// For UTF7 format, we just stop if we have undefined bits. Otherwise we append PAD.
if (ACTIV_LIKELY(Base64Constants::undefinedBits != third6Bits))
{
*pNextOutput++ = GetEncodedFromBinary(third6Bits);
}
else
{
if (UTF7)
break;
else
*pNextOutput++ = undefinedChar;
}
if (ACTIV_LIKELY(Base64Constants::undefinedBits != fourth6Bits))
{
*pNextOutput++ = GetEncodedFromBinary(fourth6Bits);
}
else
{
if (UTF7)
break;
else
*pNextOutput++ = undefinedChar;
}
}
outputLength = pNextOutput - pInitialOutput;
return STATUS_CODE_SUCCESS;
}
/**
* @brief Encode a byte stream into base64.
*
* @param pData pointer to the data to encode.
* @param dataLength length of data pointed to by @a pData.
* @param output string to have the base64-encoded data appended to (string is *not* cleared).
*/
static void Encode(const void * const pData, const size_t dataLength, String &output);
/**
* @brief Decode a base64-encoded character stream into binary.
*
* @param pData pointer to the base64-encoded character stream.
* @param dataLength length of data pointed to by @a pData.
* @param pOutput pointer to a buffer to receive the decoded binary.
* @param outputLength on input, the length of the buffer pointed to by @a pOutput. On output, the number of bytes written
* to @a pOutput.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_FORMAT
* @retval STATUS_CODE_BUFFER_UNDERRUN @a outputLength insufficient to hold decoded data.
*/
static StatusCode Decode(const char * const pData, const size_t dataLength, void * const pOutput, size_t &outputLength);
/**
* @brief Calculate the maximum base64-encoded length for a given input length.
*
* @param inputLength length of the source data.
*
* @return The maximum possible encoded length given the input length.
*/
static size_t GetMaximumEncodedLength(const size_t inputLength);
/**
* @brief Calculate the maximum decoded length for a given base64 encoded length.
*
* Useful for reserving sufficient buffer space before calling Decode().
*
* @param inputLength length of the base64-encoded data.
*
* @return The maximum possible decoded length given the input length.
*/
static size_t GetMaximumDecodedLength(const size_t inputLength);
/**
* @brief Get encoded byte from binary.
*
* @param binaryByte the binary byte.
*
* @return The encoded byte.
*/
static char GetEncodedFromBinary(const byte_t binaryByte);
/**
* @brief Get binary from encoded byte.
*
* @param encodedByte the encoded byte.
*
* @return The binary byte.
*/
static byte_t GetBinaryFromEncoded(const char encodedByte);
private:
/**
* @brief Move along an iterator chain to the next non-empty buffer.
*
* @param currentIter the iterator to advance.
* @param currentIterByteLeft to receive the length of the new buffer.
* @param pData to receive the new buffer's data pointer.
* @param end passed the last iterator in the chain.
*
* @return false if we get to the end of the chain.
*/
template<typename ITERATOR_HELPER, typename ITERATOR, typename POINTER>
static bool AdvanceIterator(ITERATOR &currentIter, size_t &currentIterBytesLeft, POINTER &pData,
const ITERATOR end)
{
assert(0 == currentIterBytesLeft);
do
{
++currentIter;
if (currentIter != end)
{
pData = reinterpret_cast<POINTER>(ITERATOR_HELPER::GetData(currentIter));
currentIterBytesLeft = ITERATOR_HELPER::GetLength(currentIter);
}
else
{
return false;
}
}
while (0 == currentIterBytesLeft);
return true;
}
static const char undefinedChar = PAD;
static const char binaryToBase64Table[64];
static const byte_t base64ToBinaryTable[256];
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper class to map from base64 character to binary value.
*/
template<byte_t ASCII_VALUE, char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup
{
private:
typedef Boost::mpl::integral_c<byte_t, SIXTY_TWO> SixtyTwoChar;
typedef Boost::mpl::integral_c<byte_t, SIXTY_THREE> SixtyThreeChar;
typedef Boost::mpl::integral_c<byte_t, ASCII_VALUE> ThisChar;
typedef Boost::mpl::integral_c<byte_t, Base64Constants::undefinedBits> Undefined;
public:
// Really wanted to specialize for SIXTY_TWO and SIXTY_THREE something like this:
// template<char SIXTY_TWO, char SIXTY_THREE>
// class Base64Lookup<SIXTY_TWO, SIXTY_TWO, SIXTY_THREE> { static const byte_t value = 62; };
// But that doesn't work with GCC (OK with VC++).
// So some boost mpl, effectively:
// if (ASCII_VALUE == SIXTY_TWO)
// value = 62;
// else if (ASCII_VALUE == SIXTY_THREE)
// value = 63;
// else
// value = Base64Constants::undefinedBits;
static const byte_t value = Boost::mpl::if_
<
Boost::mpl::equal_to<SixtyTwoChar, ThisChar>,
Boost::mpl::integral_c<byte_t, 62>,
typename Boost::mpl::if_
<
Boost::mpl::equal_to<SixtyThreeChar, ThisChar>,
Boost::mpl::integral_c<byte_t, 63>,
Undefined
>::type
>::type::value;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Class to act like an iterator to a single ptr,length buffer.
*/
template<typename POINTER>
class Base64SingleBufferIterator : public Base64SingleBufferIteratorBase
{
public:
Base64SingleBufferIterator() :
m_pData(0),
m_dataLength(0)
{
}
Base64SingleBufferIterator(POINTER pData, const size_t dataLength) :
m_pData(pData),
m_dataLength(dataLength)
{
}
Base64SingleBufferIterator &operator++()
{
m_pData = 0;
m_dataLength = 0;
return *this;
}
bool operator==(const Base64SingleBufferIterator &rhs) const
{
return (m_pData == rhs.m_pData);
}
bool operator!=(const Base64SingleBufferIterator &rhs) const
{
return !operator==(rhs);
}
private:
POINTER m_pData;
size_t m_dataLength;
friend class Base64SingleBufferIteratorHelper<POINTER>;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper class to get ptr,length from a Base64SingleBufferIterator.
*/
template<typename POINTER>
class Base64SingleBufferIteratorHelper
{
public:
static POINTER GetData(Base64SingleBufferIterator<POINTER> &iterator)
{
return iterator.m_pData;
}
static size_t GetLength(const Base64SingleBufferIterator<POINTER> &iterator)
{
return iterator.m_dataLength;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline StatusCode Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::Encode(const void * const pData, const size_t dataLength,
char * const pOutput, size_t &outputLength)
{
return Encode<Base64SingleBufferIteratorHelper<const void *>, Base64SingleBufferIterator<const void *> >(
Base64SingleBufferIterator<const void *>(pData, dataLength),
Base64SingleBufferIterator<const void *>(), 0, dataLength, pOutput, outputLength);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline void Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::Encode(const void * const pData, const size_t dataLength,
String &output)
{
if (ACTIV_UNLIKELY(0 == dataLength))
return;
// String::append() is slow. push_back() is far worse, even with a pre-allocated correct buffer size.
// So cache a ptr to the string internal and just write to it. Slightly nasty...
const size_t initialOutputLength = output.length();
output.resize(initialOutputLength + GetMaximumEncodedLength(dataLength));
size_t outputLength = output.length() - initialOutputLength;
(void) Encode(pData, dataLength, &output[initialOutputLength], outputLength);
output.resize(initialOutputLength + outputLength);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
StatusCode Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::Decode(const char * const pData, const size_t dataLength,
void * const pOutput, size_t &outputLength)
{
const char *pInput = pData;
size_t inputBytesLeft = dataLength;
byte_t *pNextOutput = reinterpret_cast<byte_t *>(pOutput);
size_t outputBytesLeft = outputLength;
while (inputBytesLeft > 0)
{
if (UTF7)
{
if (ACTIV_UNLIKELY(inputBytesLeft < 2))
return STATUS_CODE_INVALID_FORMAT;
}
else
{
if (ACTIV_UNLIKELY(inputBytesLeft < 4))
return STATUS_CODE_INVALID_FORMAT;
}
char inputChar;
byte_t decodedByte, outputByte;
// First byte.
inputChar = *pInput++;
--inputBytesLeft;
decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == decodedByte))
return STATUS_CODE_INVALID_FORMAT;
outputByte = (decodedByte & Binary<byte_t, 111111>::value) << 2;
// Second byte.
inputChar = *pInput++;
--inputBytesLeft;
decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == decodedByte))
return STATUS_CODE_INVALID_FORMAT;
outputByte |= (decodedByte >> 4) & Binary<byte_t, 11>::value;
if (ACTIV_UNLIKELY(0 == outputBytesLeft))
return STATUS_CODE_BUFFER_UNDERRUN;
*pNextOutput++ = outputByte;
--outputBytesLeft;
outputByte = (decodedByte & Binary<byte_t, 1111>::value) << 4;
// Third byte.
if (UTF7)
{
// End of input terminates with UTF7.
if (ACTIV_UNLIKELY(0 == inputBytesLeft))
break;
}
inputChar = *pInput++;
--inputBytesLeft;
if (ACTIV_LIKELY(undefinedChar != inputChar))
{
decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == decodedByte))
return STATUS_CODE_INVALID_FORMAT;
outputByte |= (decodedByte >> 2) & Binary<byte_t, 1111>::value;
if (ACTIV_UNLIKELY(0 == outputBytesLeft))
return STATUS_CODE_BUFFER_UNDERRUN;
*pNextOutput++ = outputByte;
--outputBytesLeft;
outputByte = (decodedByte & Binary<byte_t, 11>::value) << 6;
// Fourth byte.
if (UTF7)
{
// End of input terminates with UTF7.
if (ACTIV_UNLIKELY(0 == inputBytesLeft))
break;
}
inputChar = *pInput++;
--inputBytesLeft;
if (ACTIV_LIKELY(undefinedChar != inputChar))
{
decodedByte = GetBinaryFromEncoded(inputChar);
if (ACTIV_UNLIKELY(Base64Constants::undefinedBits == decodedByte))
return STATUS_CODE_INVALID_FORMAT;
outputByte |= decodedByte & Binary<byte_t, 111111>::value;
if (ACTIV_UNLIKELY(0 == outputBytesLeft))
return STATUS_CODE_BUFFER_UNDERRUN;
*pNextOutput++ = outputByte;
--outputBytesLeft;
}
else
{
if (UTF7)
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != outputByte))
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != inputBytesLeft))
return STATUS_CODE_INVALID_FORMAT;
}
}
else
{
if (UTF7)
return STATUS_CODE_INVALID_FORMAT;
if (ACTIV_UNLIKELY(0 != outputByte))
return STATUS_CODE_INVALID_FORMAT;
// Fourth byte must also be PAD if third was and we must be at the end.
inputChar = *pInput++;
--inputBytesLeft;
if (ACTIV_UNLIKELY(undefinedChar != inputChar) || ACTIV_UNLIKELY(0 != inputBytesLeft))
return STATUS_CODE_INVALID_FORMAT;
}
}
outputLength -= outputBytesLeft;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline size_t Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::GetMaximumEncodedLength(const size_t inputLength)
{
return 4 * ((inputLength + 2) / 3);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline size_t Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::GetMaximumDecodedLength(const size_t inputLength)
{
return 3 * ((inputLength + 3) / 4);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline char Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::GetEncodedFromBinary(const byte_t binaryByte)
{
return binaryToBase64Table[binaryByte];
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline byte_t Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::GetBinaryFromEncoded(const char encodedByte)
{
return base64ToBinaryTable[static_cast<byte_t>(encodedByte)];
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
const char Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::binaryToBase64Table[64] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
SIXTY_TWO, SIXTY_THREE
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
const byte_t Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::base64ToBinaryTable[256] =
{
Base64Lookup<0, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<1, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<2, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<3, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<4, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<5, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<6, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<7, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<8, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<9, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<10, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<11, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<12, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<13, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<14, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<15, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<16, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<17, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<18, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<19, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<20, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<21, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<22, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<23, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<24, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<25, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<26, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<27, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<28, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<29, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<30, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<31, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<32, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<33, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<34, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<35, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<36, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<37, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<38, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<39, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<40, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<41, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<42, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<43, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<44, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<45, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<46, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<47, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<48, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<49, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<50, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<51, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<52, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<53, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<54, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<55, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<56, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<57, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<58, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<59, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<60, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<61, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<62, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<63, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<64, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<65, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<66, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<67, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<68, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<69, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<70, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<71, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<72, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<73, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<74, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<75, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<76, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<77, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<78, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<79, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<80, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<81, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<82, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<83, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<84, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<85, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<86, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<87, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<88, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<89, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<90, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<91, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<92, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<93, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<94, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<95, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<96, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<97, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<98, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<99, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<100, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<101, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<102, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<103, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<104, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<105, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<106, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<107, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<108, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<109, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<110, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<111, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<112, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<113, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<114, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<115, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<116, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<117, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<118, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<119, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<120, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<121, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<122, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<123, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<124, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<125, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<126, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<127, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<128, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<129, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<130, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<131, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<132, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<133, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<134, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<135, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<136, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<137, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<138, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<139, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<140, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<141, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<142, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<143, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<144, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<145, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<146, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<147, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<148, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<149, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<150, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<151, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<152, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<153, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<154, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<155, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<156, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<157, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<158, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<159, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<160, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<161, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<162, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<163, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<164, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<165, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<166, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<167, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<168, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<169, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<170, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<171, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<172, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<173, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<174, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<175, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<176, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<177, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<178, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<179, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<180, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<181, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<182, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<183, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<184, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<185, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<186, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<187, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<188, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<189, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<190, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<191, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<192, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<193, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<194, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<195, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<196, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<197, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<198, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<199, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<200, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<201, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<202, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<203, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<204, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<205, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<206, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<207, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<208, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<209, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<210, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<211, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<212, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<213, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<214, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<215, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<216, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<217, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<218, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<219, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<220, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<221, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<222, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<223, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<224, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<225, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<226, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<227, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<228, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<229, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<230, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<231, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<232, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<233, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<234, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<235, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<236, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<237, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<238, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<239, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<240, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<241, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<242, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<243, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<244, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<245, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<246, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<247, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<248, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<249, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<250, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<251, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<252, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<253, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<254, SIXTY_TWO, SIXTY_THREE>::value,
Base64Lookup<255, SIXTY_TWO, SIXTY_THREE>::value,
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// Upper-case base64 char -> binary mappings.
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'A', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 0;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'B', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 1;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'C', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 2;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'D', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 3;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'E', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 4;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'F', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 5;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'G', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 6;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'H', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 7;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'I', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 8;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'J', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 9;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'K', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 10;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'L', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 11;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'M', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 12;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'N', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 13;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'O', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 14;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'P', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 15;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'Q', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 16;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'R', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 17;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'S', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 18;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'T', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 19;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'U', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 20;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'V', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 21;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'W', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 22;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'X', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 23;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'Y', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 24;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'Z', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 25;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Lower-case base64 char -> binary mappings.
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'a', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 26;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'b', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 27;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'c', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 28;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'d', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 29;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'e', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 30;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'f', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 31;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'g', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 32;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'h', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 33;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'i', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 34;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'j', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 35;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'k', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 36;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'l', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 37;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'m', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 38;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'n', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 39;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'o', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 40;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'p', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 41;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'q', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 42;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'r', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 43;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'s', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 44;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'t', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 45;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'u', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 46;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'v', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 47;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'w', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 48;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'x', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 49;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'y', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 50;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'z', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 51;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Numerical base64 char -> binary mappings.
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'0', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 52;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'1', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 53;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'2', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 54;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'3', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 55;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'4', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 56;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'5', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 57;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'6', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 58;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'7', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 59;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'8', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 60;
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<char SIXTY_TWO, char SIXTY_THREE>
class Base64Lookup<'9', SIXTY_TWO, SIXTY_THREE>
{
public:
static const byte_t value = 61;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::Validator::Validator(const void * const pData, const size_t dataLength) :
m_state(STATE_1),
m_pInput(reinterpret_cast<const char *>(pData)),
m_inputBytesLeft(dataLength)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool UTF7, char SIXTY_TWO, char SIXTY_THREE, char PAD>
inline StatusCode Base64<UTF7, SIXTY_TWO, SIXTY_THREE, PAD>::Validator::operator()(void * const pOutput, size_t &outputLength)
{
return operator()<Base64SingleBufferIteratorHelper<void *> >(Base64SingleBufferIterator<void *>(pOutput, outputLength),
Base64SingleBufferIterator<void *>(), outputLength);
}
} // namespace Activ
#endif // (!defined ACTIV_MISC_BASE64_H)