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.
35 char getbase64character(const char& in
)
169 return '\0'; // ?????? yikes
172 std::vector
<char> base64encode(const std::vector
<char>& input
, const bool returns
) {
173 std::vector
<char> output
;
175 // add a newline (SMTP demands less than 1000 characters in a message line).
177 for(std::vector
<char>::size_type p
= 0; p
< input
.size(); p
+=3) {
178 output
.push_back(getbase64character((input
[p
] & 0xFC) >> 2));
181 if(p
+1 < input
.size()) {
182 output
.push_back(getbase64character(((input
[p
] & 0x03) <<4) | ((input
[p
+1] & 0xF0) >> 4)));
185 if(p
+2 < input
.size()) {
186 output
.push_back(getbase64character(((input
[p
+1] & 0x0F) <<2) | ((input
[p
+2] & 0xC0) >>6)));
187 output
.push_back(getbase64character((input
[p
+2] & 0x3F)));
191 if(p
+1 == input
.size()) {
192 output
.push_back(getbase64character(((input
[p
] & 0x03) <<4)));
194 else if(p
+2 == input
.size()) {
195 output
.push_back(getbase64character(((input
[p
+1] & 0x0F) <<2)));
199 // 79 characters on a line.
201 output
.push_back('\r');
202 output
.push_back('\n');
208 int pad(input
.size() % 3);
215 for(int i
= 0; i
< pad
; ++i
)
216 output
.push_back('=');
221 std::string
base64encode(const std::string
& input
, const bool returns
) {
222 std::vector
<char> in
, out
;
223 for(std::string::const_iterator it
= input
.begin(); it
!= input
.end(); ++it
) {
226 out
= base64encode(in
, returns
);
228 for(std::vector
<char>::const_iterator it1
= out
.begin(); it1
!= out
.end(); ++it1
) {
234 } // end namespace jwsmtp