ext-cryptopp/base64.cpp

90 lines
2.8 KiB
C++
Raw Normal View History

2015-11-05 06:59:46 +00:00
// base64.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "base64.h"
NAMESPACE_BEGIN(CryptoPP)
2016-10-10 22:20:49 +00:00
namespace
{
const byte s_stdVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const byte s_urlVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const byte s_padding = '=';
}
2015-11-05 06:59:46 +00:00
void Base64Encoder::IsolatedInitialize(const NameValuePairs &parameters)
{
bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
const char *lineBreak = insertLineBreaks ? "\n" : "";
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
m_filter->Initialize(CombinedNameValuePairs(
parameters,
MakeParameters(Name::EncodingLookupArray(), &s_stdVec[0], false)
(Name::PaddingByte(), s_padding)
(Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
(Name::Separator(), ConstByteArrayParameter(lineBreak))
(Name::Terminator(), ConstByteArrayParameter(lineBreak))
(Name::Log2Base(), 6, true)));
}
void Base64URLEncoder::IsolatedInitialize(const NameValuePairs &parameters)
{
bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
const char *lineBreak = insertLineBreaks ? "\n" : "";
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
m_filter->Initialize(CombinedNameValuePairs(
parameters,
MakeParameters(Name::EncodingLookupArray(), &s_urlVec[0], false)
(Name::PaddingByte(), s_padding)
(Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
(Name::Separator(), ConstByteArrayParameter(lineBreak))
(Name::Terminator(), ConstByteArrayParameter(lineBreak))
(Name::Log2Base(), 6, true)));
}
2016-01-12 04:36:46 +00:00
void Base64Decoder::IsolatedInitialize(const NameValuePairs &parameters)
{
BaseN_Decoder::IsolatedInitialize(CombinedNameValuePairs(
parameters,
MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
}
2015-11-05 06:59:46 +00:00
const int *Base64Decoder::GetDecodingLookupArray()
{
static volatile bool s_initialized = false;
static int s_array[256];
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
if (!s_initialized)
{
InitializeDecodingLookupArray(s_array, s_stdVec, 64, false);
s_initialized = true;
}
return s_array;
}
2016-01-12 04:36:46 +00:00
void Base64URLDecoder::IsolatedInitialize(const NameValuePairs &parameters)
{
BaseN_Decoder::IsolatedInitialize(CombinedNameValuePairs(
parameters,
MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
}
2015-11-05 06:59:46 +00:00
const int *Base64URLDecoder::GetDecodingLookupArray()
{
static volatile bool s_initialized = false;
static int s_array[256];
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
if (!s_initialized)
{
InitializeDecodingLookupArray(s_array, s_urlVec, 64, false);
s_initialized = true;
}
return s_array;
}
NAMESPACE_END