Forgot to add these. Oops.
[inoclam.git] / src / base64.cxx
bloba365c67460bce18305e0210b7351d40b53b5ee5c
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.
4 //
5 // This file is part of the jwSMTP library.
6 //
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
20 // jwSMTP library
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.
29 #include <vector>
30 #include <string>
31 #include "base64.hxx"
33 namespace jwsmtp {
35 char getbase64character(const char& in)
37 switch(in) {
38 case B64_A:
39 return 'A';
40 case B64_B:
41 return 'B';
42 case B64_C:
43 return 'C';
44 case B64_D:
45 return 'D';
46 case B64_E:
47 return 'E';
48 case B64_F:
49 return 'F';
50 case B64_G:
51 return 'G';
52 case B64_H:
53 return 'H';
54 case B64_I:
55 return 'I';
56 case B64_J:
57 return 'J';
58 case B64_K:
59 return 'K';
60 case B64_L:
61 return 'L';
62 case B64_M:
63 return 'M';
64 case B64_N:
65 return 'N';
66 case B64_O:
67 return 'O';
68 case B64_P:
69 return 'P';
70 case B64_Q:
71 return 'Q';
72 case B64_R:
73 return 'R';
74 case B64_S:
75 return 'S';
76 case B64_T:
77 return 'T';
78 case B64_U:
79 return 'U';
80 case B64_V:
81 return 'V';
82 case B64_W:
83 return 'W';
84 case B64_X:
85 return 'X';
86 case B64_Y:
87 return 'Y';
88 case B64_Z:
89 return 'Z';
90 case B64_a:
91 return 'a';
92 case B64_b:
93 return 'b';
94 case B64_c:
95 return 'c';
96 case B64_d:
97 return 'd';
98 case B64_e:
99 return 'e';
100 case B64_f:
101 return 'f';
102 case B64_g:
103 return 'g';
104 case B64_h:
105 return 'h';
106 case B64_i:
107 return 'i';
108 case B64_j:
109 return 'j';
110 case B64_k:
111 return 'k';
112 case B64_l:
113 return 'l';
114 case B64_m:
115 return 'm';
116 case B64_n:
117 return 'n';
118 case B64_o:
119 return 'o';
120 case B64_p:
121 return 'p';
122 case B64_q:
123 return 'q';
124 case B64_r:
125 return 'r';
126 case B64_s:
127 return 's';
128 case B64_t:
129 return 't';
130 case B64_u:
131 return 'u';
132 case B64_v:
133 return 'v';
134 case B64_w:
135 return 'w';
136 case B64_x:
137 return 'x';
138 case B64_y:
139 return 'y';
140 case B64_z:
141 return 'z';
142 case B64_0:
143 return '0';
144 case B64_1:
145 return '1';
146 case B64_2:
147 return '2';
148 case B64_3:
149 return '3';
150 case B64_4:
151 return '4';
152 case B64_5:
153 return '5';
154 case B64_6:
155 return '6';
156 case B64_7:
157 return '7';
158 case B64_8:
159 return '8';
160 case B64_9:
161 return '9';
162 case plus:
163 return '+';
164 case slash:
165 return '/';
166 case padding:
167 return '=';
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).
176 long count = 0;
177 for(std::vector<char>::size_type p = 0; p < input.size(); p+=3) {
178 output.push_back(getbase64character((input[p] & 0xFC) >> 2));
179 ++count;
181 if(p+1 < input.size()) {
182 output.push_back(getbase64character(((input[p] & 0x03) <<4) | ((input[p+1] & 0xF0) >> 4)));
183 ++count;
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)));
188 ++count;
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)));
198 if(returns) {
199 // 79 characters on a line.
200 if(count > 75) {
201 output.push_back('\r');
202 output.push_back('\n');
203 count = 0;
208 int pad(input.size() % 3);
209 if(pad) {
210 if(pad == 1)
211 pad = 2;
212 else
213 pad = 1;
215 for(int i = 0; i < pad; ++i)
216 output.push_back('=');
218 return output;
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) {
224 in.push_back(*it);
226 out = base64encode(in, returns);
227 std::string ret;
228 for(std::vector<char>::const_iterator it1 = out.begin(); it1 != out.end(); ++it1) {
229 ret += *it1;
231 return ret;
234 } // end namespace jwsmtp