1 // Note that the only valid version of the GPL as far as jwSMTP
2 // is concerned is v2 of the license (ie v2, not v2.2 or v3.x or whatever),
3 // unless explicitly otherwise stated.
5 // This file is part of the jwSMTP library.
7 // jwSMTP library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; version 2 of the License.
11 // jwSMTP library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with jwSMTP library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // http://johnwiggins.net
22 // smtplib@johnwiggins.net
24 // base64.h: base64 encoding functions
26 // Modified by Tom Cort <tom.cort@state.vt.us 30-Oct-2008.
27 // Renamed headers to .hxx and removed WIN32 specific code.
33
char getbase64character(const char &in
)
{
166
return '\0'; // ?????? yikes
168
std::vector
< char >base64encode(const std::vector
< char >&input
, const bool returns
) {
169
std::vector
< char >output
;
171 // add a newline (SMTP demands less than 1000 characters in a message line).
173
for (std::vector
< char >::size_type p
= 0; p
< input
.size(); p
+= 3) {
174
output
.push_back(getbase64character((input
[p
] & 0xFC) >> 2));
176
if (p
+ 1 < input
.size()) {
177
output
.push_back(getbase64character(((input
[p
] & 0x03) << 4) | ((input
[p
+ 1] & 0xF0) >> 4)));
180
if (p
+ 2 < input
.size()) {
181
output
.push_back(getbase64character(((input
[p
+ 1] & 0x0F) << 2) | ((input
[p
+ 2] & 0xC0) >> 6)));
182
output
.push_back(getbase64character((input
[p
+ 2] & 0x3F)));
185
if (p
+ 1 == input
.size()) {
186
output
.push_back(getbase64character(((input
[p
] & 0x03) << 4)));
189 else if (p
+ 2 == input
.size()) {
190
output
.push_back(getbase64character(((input
[p
+ 1] & 0x0F) << 2)));
194 // 79 characters on a line.
196
output
.push_back('\r');
197
output
.push_back('\n');
202
int pad(input
.size() % 3);
210
for (int i
= 0; i
< pad
; ++i
)
211
output
.push_back('=');
214
std::string
base64encode(const std::string
& input
, const bool returns
) {
215
std::vector
< char >in
, out
;
216
for (std::string::const_iterator it
= input
.begin(); it
!= input
.end(); ++it
) {
219
out
= base64encode(in
, returns
);
221
for (std::vector
< char >::const_iterator it1
= out
.begin(); it1
!= out
.end(); ++it1
) {
225
} // end namespace jwsmtp