Crypto++  5.6.5
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 namespace
9 {
10  const byte s_stdVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11  const byte s_urlVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
12  const byte s_padding = '=';
13 }
14 
16 {
17  bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
18  int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
19 
20  const char *lineBreak = insertLineBreaks ? "\n" : "";
21 
22  m_filter->Initialize(CombinedNameValuePairs(
23  parameters,
24  MakeParameters(Name::EncodingLookupArray(), &s_stdVec[0], false)
25  (Name::PaddingByte(), s_padding)
26  (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
27  (Name::Separator(), ConstByteArrayParameter(lineBreak))
28  (Name::Terminator(), ConstByteArrayParameter(lineBreak))
29  (Name::Log2Base(), 6, true)));
30 }
31 
33 {
34  bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
35  int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
36 
37  const char *lineBreak = insertLineBreaks ? "\n" : "";
38 
39  m_filter->Initialize(CombinedNameValuePairs(
40  parameters,
41  MakeParameters(Name::EncodingLookupArray(), &s_urlVec[0], false)
42  (Name::PaddingByte(), s_padding)
43  (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
44  (Name::Separator(), ConstByteArrayParameter(lineBreak))
45  (Name::Terminator(), ConstByteArrayParameter(lineBreak))
46  (Name::Log2Base(), 6, true)));
47 }
48 
50 {
52  parameters,
53  MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
54 }
55 
56 const int *Base64Decoder::GetDecodingLookupArray()
57 {
58  static volatile bool s_initialized = false;
59  static int s_array[256];
60 
61  if (!s_initialized)
62  {
63  InitializeDecodingLookupArray(s_array, s_stdVec, 64, false);
64  s_initialized = true;
65  }
66  return s_array;
67 }
68 
70 {
72  parameters,
73  MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
74 }
75 
76 const int *Base64URLDecoder::GetDecodingLookupArray()
77 {
78  static volatile bool s_initialized = false;
79  static int s_array[256];
80 
81  if (!s_initialized)
82  {
83  InitializeDecodingLookupArray(s_array, s_urlVec, 64, false);
84  s_initialized = true;
85  }
86  return s_array;
87 }
88 
89 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:69
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:350
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
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:382
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:500
Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder.
void IsolatedInitialize(const NameValuePairs &parameters)
Definition: base64.cpp:32
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.cpp:49
Crypto++ library namespace.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: base64.cpp:15
Interface for retrieving values given their names.
Definition: cryptlib.h:279