3 ** \author grymse@alhem.net
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE
{
37 const char *Base64::bstr
=
43 const char Base64::rstr
[] = {
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
47 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
48 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
50 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
51 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};
59 void Base64::encode(FILE *fil
, std::string
& output
, bool add_crlf
)
67 remain
= fread(input
,1,3,fil
);
70 if (add_crlf
&& o
&& o
% 76 == 0)
75 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
76 output
+= bstr
[ ((input
[i
] << 4) & 0x30) ];
80 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
81 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
82 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) ];
86 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
87 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
88 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) + ((input
[i
+ 2] >> 6) & 0x03) ];
89 output
+= bstr
[ (input
[i
+ 2] & 0x3f) ];
93 remain
= fread(input
,1,3,fil
);
98 void Base64::encode(const std::string
& str_in
, std::string
& str_out
, bool add_crlf
)
100 encode(str_in
.c_str(), str_in
.size(), str_out
, add_crlf
);
104 void Base64::encode(const char* input
,size_t l
,std::string
& output
, bool add_crlf
)
112 size_t remain
= l
- i
;
113 if (add_crlf
&& o
&& o
% 76 == 0)
118 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
119 output
+= bstr
[ ((input
[i
] << 4) & 0x30) ];
123 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
124 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
125 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) ];
129 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
130 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
131 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) + ((input
[i
+ 2] >> 6) & 0x03) ];
132 output
+= bstr
[ (input
[i
+ 2] & 0x3f) ];
140 void Base64::encode(const unsigned char* input
,size_t l
,std::string
& output
,bool add_crlf
)
148 size_t remain
= l
- i
;
149 if (add_crlf
&& o
&& o
% 76 == 0)
154 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
155 output
+= bstr
[ ((input
[i
] << 4) & 0x30) ];
159 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
160 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
161 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) ];
165 output
+= bstr
[ ((input
[i
] >> 2) & 0x3f) ];
166 output
+= bstr
[ ((input
[i
] << 4) & 0x30) + ((input
[i
+ 1] >> 4) & 0x0f) ];
167 output
+= bstr
[ ((input
[i
+ 1] << 2) & 0x3c) + ((input
[i
+ 2] >> 6) & 0x03) ];
168 output
+= bstr
[ (input
[i
+ 2] & 0x3f) ];
176 void Base64::decode(const std::string
& input
,std::string
& output
)
179 size_t l
= input
.size();
184 while (i
< l
&& (input
[i
] == 13 || input
[i
] == 10))
188 char b1
= (char)((rstr
[(int)input
[i
]] << 2 & 0xfc) +
189 (rstr
[(int)input
[i
+ 1]] >> 4 & 0x03));
191 if (input
[i
+ 2] != '=')
193 char b2
= (char)((rstr
[(int)input
[i
+ 1]] << 4 & 0xf0) +
194 (rstr
[(int)input
[i
+ 2]] >> 2 & 0x0f));
197 if (input
[i
+ 3] != '=')
199 char b3
= (char)((rstr
[(int)input
[i
+ 2]] << 6 & 0xc0) +
200 rstr
[(int)input
[i
+ 3]]);
209 void Base64::decode(const std::string
& input
, unsigned char *output
, size_t& sz
)
212 size_t l
= input
.size();
217 while (i
< l
&& (input
[i
] == 13 || input
[i
] == 10))
221 unsigned char b1
= (unsigned char)((rstr
[(int)input
[i
]] << 2 & 0xfc) +
222 (rstr
[(int)input
[i
+ 1]] >> 4 & 0x03));
228 if (input
[i
+ 2] != '=')
230 unsigned char b2
= (unsigned char)((rstr
[(int)input
[i
+ 1]] << 4 & 0xf0) +
231 (rstr
[(int)input
[i
+ 2]] >> 2 & 0x0f));
238 if (input
[i
+ 3] != '=')
240 unsigned char b3
= (unsigned char)((rstr
[(int)input
[i
+ 2]] << 6 & 0xc0) +
241 rstr
[(int)input
[i
+ 3]]);
255 size_t Base64::decode_length(const std::string
& str64
)
257 if (!str64
.size() || str64
.size() % 4)
259 size_t l
= 3 * (str64
.size() / 4 - 1) + 1;
260 if (str64
[str64
.size() - 2] != '=')
262 if (str64
[str64
.size() - 1] != '=')
268 #ifdef SOCKETS_NAMESPACE