Crypto++ 8.9
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 "config.h"
5#include "base64.h"
6
7NAMESPACE_BEGIN(CryptoPP)
8ANONYMOUS_NAMESPACE_BEGIN
9
10const byte s_stdVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11const byte s_urlVec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
12const byte s_padding = '=';
13const int s_stdArray[256] = {
14 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
16 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
17 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
18 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
19 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
20 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
21 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
22 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
24 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
30};
31const int s_urlArray[256] = {
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
35 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
36 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
37 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
38 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
39 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
48};
49
50ANONYMOUS_NAMESPACE_END
51
53{
54 bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
55 int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
56
57 const char *lineBreak = insertLineBreaks ? "\n" : "";
58
59 m_filter->Initialize(CombinedNameValuePairs(
60 parameters,
61 MakeParameters(Name::EncodingLookupArray(), &s_stdVec[0], false)
62 (Name::PaddingByte(), s_padding)
63 (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
66 (Name::Log2Base(), 6, true)));
67}
68
70{
71 bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true);
72 int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72);
73
74 const char *lineBreak = insertLineBreaks ? "\n" : "";
75
76 m_filter->Initialize(CombinedNameValuePairs(
77 parameters,
78 MakeParameters(Name::EncodingLookupArray(), &s_urlVec[0], false)
79 (Name::PaddingByte(), s_padding)
80 (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0)
83 (Name::Log2Base(), 6, true)));
84}
85
87{
89 parameters,
90 MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
91}
92
93const int *Base64Decoder::GetDecodingLookupArray()
94{
95 return s_stdArray;
96}
97
99{
101 parameters,
102 MakeParameters(Name::DecodingLookupArray(), GetDecodingLookupArray(), false)(Name::Log2Base(), 6, true)));
103}
104
105// Unrolled initialization, http://github.com/weidai11/cryptopp/issues/376
106const int *Base64URLDecoder::GetDecodingLookupArray()
107{
108 return s_urlArray;
109}
110
111NAMESPACE_END
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition algparam.h:508
Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base64.cpp:86
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base64.cpp:52
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition base64.cpp:98
void IsolatedInitialize(const NameValuePairs &parameters)
Definition base64.cpp:69
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Combines two sets of NameValuePairs.
Definition algparam.h:129
Used to pass byte array input as part of a NameValuePairs object.
Definition algparam.h:25
Interface for retrieving values given their names.
Definition cryptlib.h:327
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition cryptlib.h:397
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition cryptlib.h:429
Library configuration file.
Crypto++ library namespace.
const char * InsertLineBreaks()
bool
Definition argnames.h:77
const char * DecodingLookupArray()
const byte *
Definition argnames.h:76
const char * GroupSize()
int
Definition argnames.h:71
const char * PaddingByte()
byte
Definition argnames.h:73
const char * EncodingLookupArray()
const byte *
Definition argnames.h:75
const char * MaxLineLength()
int
Definition argnames.h:78
const char * Terminator()
ConstByteArrayParameter.
Definition argnames.h:69
const char * Log2Base()
int
Definition argnames.h:74
const char * Separator()
ConstByteArrayParameter.
Definition argnames.h:68
Precompiled header file.