ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
CxxUtils
Root
base64.cxx
Go to the documentation of this file.
1
/*
2
base64.cpp and base64.h
3
base64 encoding and decoding with C++.
4
5
Original: (C) 2004-2017 Rene Nyffenegger
6
Adapted by: Michal Lihocky (https://stackoverflow.com/a/13935718/8372556)
7
further adapted by: Constantin Heidegger
8
9
---------
10
11
base64.cpp and base64.h
12
13
base64 encoding and decoding with C++.
14
15
Version: 1.01.00
16
17
Copyright (C) 2004-2017 Rene Nyffenegger
18
19
This source code is provided 'as-is', without any express or implied
20
warranty. In no event will the author be held liable for any damages
21
arising from the use of this software.
22
23
Permission is granted to anyone to use this software for any purpose,
24
including commercial applications, and to alter it and redistribute it
25
freely, subject to the following restrictions:
26
27
1. The origin of this source code must not be misrepresented; you must not
28
claim that you wrote the original source code. If you use this source code
29
in a product, an acknowledgment in the product documentation would be
30
appreciated but is not required.
31
32
2. Altered source versions must be plainly marked as such, and must not be
33
misrepresented as being the original source code.
34
35
3. This notice may not be removed or altered from any source distribution.
36
37
Rene Nyffenegger rene.nyffenegger@adp-gmbh.ch
38
39
*/
40
41
#include "
CxxUtils/base64.h
"
42
43
namespace
CxxUtils
{
44
45
static
const
std::string
base64_chars
=
46
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47
"abcdefghijklmnopqrstuvwxyz"
48
"0123456789+/"
;
49
50
51
static
inline
bool
is_base64
(
unsigned
char
c) {
52
return
(isalnum(c) || (c ==
'+'
) || (c ==
'/'
));
53
}
54
55
std::string
base64_encode
(
const
unsigned
char
* bytes_to_encode,
unsigned
int
in_len) {
56
std::string ret;
57
int
i = 0;
58
int
j = 0;
59
unsigned
char
char_array_3[3];
60
unsigned
char
char_array_4[4];
61
62
while
(in_len--) {
63
char_array_3[i++] = *(bytes_to_encode++);
64
if
(i == 3) {
65
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
66
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
67
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
68
char_array_4[3] = char_array_3[2] & 0x3f;
69
70
for
(i = 0; (i <4) ; i++)
71
ret +=
base64_chars
[char_array_4[i]];
72
i = 0;
73
}
74
}
75
76
if
(i)
77
{
78
for
(j = i; j < 3; j++)
79
char_array_3[j] =
'\0'
;
80
81
char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
82
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
83
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
84
85
for
(j = 0; (j < i + 1); j++)
86
ret +=
base64_chars
[char_array_4[j]];
87
88
while
((i++ < 3))
89
ret +=
'='
;
90
91
}
92
93
return
ret;
94
95
}
96
97
std::vector<unsigned char>
base64_decode
(
const
std::string& encoded_string) {
98
int
in_len = encoded_string.size();
99
int
i = 0;
100
int
j = 0;
101
int
in_ = 0;
102
unsigned
char
char_array_4[4], char_array_3[3];
103
std::vector<unsigned char> ret;
104
105
while
(in_len-- && ( encoded_string[in_] !=
'='
) &&
is_base64
(encoded_string[in_])) {
106
char_array_4[i++] = encoded_string[in_]; in_++;
107
if
(i ==4) {
108
for
(i = 0; i <4; i++)
109
char_array_4[i] =
base64_chars
.find(char_array_4[i]);
110
111
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
112
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
113
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
114
115
for
(i = 0; (i < 3); i++)
116
ret.push_back(char_array_3[i]);
117
i = 0;
118
}
119
}
120
121
if
(i) {
122
for
(j = i; j <4; j++)
123
char_array_4[j] = 0;
124
125
for
(j = 0; j <4; j++)
126
char_array_4[j] =
base64_chars
.find(char_array_4[j]);
127
128
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
129
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
130
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
131
132
for
(j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
133
}
134
135
return
ret;
136
}
137
138
}
base64.h
CxxUtils
Definition
aligned_vector.h:29
CxxUtils::base64_decode
std::vector< unsigned char > base64_decode(const std::string &)
Definition
base64.cxx:97
CxxUtils::is_base64
static bool is_base64(unsigned char c)
Definition
base64.cxx:51
CxxUtils::base64_chars
static const std::string base64_chars
Definition
base64.cxx:45
CxxUtils::base64_encode
std::string base64_encode(const unsigned char *, unsigned int)
Definition
base64.cxx:55
Generated on
for ATLAS Offline Software by
1.17.0