Crypto++  5.6.5
Free C++ class library of cryptographic schemes
base64.cpp
1 // base64.cpp - originally 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 ANONYMOUS_NAMESPACE_BEGIN
57 static const int s_stdArray[256] = {
58  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
61  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
62  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
63  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
64  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
65  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
66  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
74 };
75 NAMESPACE_END
76 
77 const int *Base64Decoder::GetDecodingLookupArray()
78 {
79  return s_stdArray;
80 }
81 
83 {
85  parameters,
86  MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
87 }
88 
89 ANONYMOUS_NAMESPACE_BEGIN
90 static const int s_urlArray[256] = {
91  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
92  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
93  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
94  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
95  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
96  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
97  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
98  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
99  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
100  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
101  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
102  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
103  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
104  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
105  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
106  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
107 };
108 NAMESPACE_END
109 
110 // Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
111 const int *Base64URLDecoder::GetDecodingLookupArray()
112 {
113  return s_urlArray;
114 }
115 
116 NAMESPACE_END
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:388
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:356
Combines two sets of NameValuePairs.
Definition: algparam.h:135
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: basecode.cpp:115
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
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:285