Crypto++  5.6.3
Free C++ class library of cryptographic schemes
base64.cpp
1 // base64.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "base64.h"
5 
6 NAMESPACE_BEGIN(CryptoPP)
7 
8 // Base64
9 static const byte s_stdVec[] =
10  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11 // Base64URL
12 static const byte s_urlVec[] =
13  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
14 static const byte s_padding = '=';
15 
16 void Base64Encoder::IsolatedInitialize(const NameValuePairs &parameters)
17 {
18  bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
19  int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
20 
21  const char *lineBreak = insertLineBreaks ? "\n" : "";
22 
24  parameters,
25  MakeParameters(Name::EncodingLookupArray(), &s_stdVec[0], false)
26  (Name::PaddingByte(), s_padding)
27  (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
28  (Name::Separator(), ConstByteArrayParameter(lineBreak))
29  (Name::Terminator(), ConstByteArrayParameter(lineBreak))
30  (Name::Log2Base(), 6, true)));
31 }
32 
34 {
35  bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
36  int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
37 
38  const char *lineBreak = insertLineBreaks ? "\n" : "";
39 
40  m_filter->Initialize(CombinedNameValuePairs(
41  parameters,
42  MakeParameters(Name::EncodingLookupArray(), &s_urlVec[0], false)
43  (Name::PaddingByte(), s_padding)
44  (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
45  (Name::Separator(), ConstByteArrayParameter(lineBreak))
46  (Name::Terminator(), ConstByteArrayParameter(lineBreak))
47  (Name::Log2Base(), 6, true)));
48 }
49 
51 {
53  parameters,
54  MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
55 }
56 
57 const int *Base64Decoder::GetDecodingLookupArray()
58 {
59  static volatile bool s_initialized = false;
60  static int s_array[256];
61 
62  if (!s_initialized)
63  {
64  InitializeDecodingLookupArray(s_array, s_stdVec, 64, false);
65  s_initialized = true;
66  }
67  return s_array;
68 }
69 
71 {
73  parameters,
74  MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
75 }
76 
77 const int *Base64URLDecoder::GetDecodingLookupArray()
78 {
79  static volatile bool s_initialized = false;
80  static int s_array[256];
81 
82  if (!s_initialized)
83  {
84  InitializeDecodingLookupArray(s_array, s_urlVec, 64, false);
85  s_initialized = true;
86  }
87  return s_array;
88 }
89 
90 NAMESPACE_END
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.cpp:70
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:348
Combines two sets of NameValuePairs.
Definition: algparam.h:135
static void InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
Intializes BaseN lookup array.
Definition: basecode.cpp:178
Base64 encodes data.
Definition: base64.h:17
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: basecode.cpp:115
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:380
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder.
void IsolatedInitialize(const NameValuePairs &parameters)
Definition: base64.cpp:33
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.cpp:50
void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: filters.cpp:71
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:277