root/trunk/dep/src/sockets/Base64.cpp @ 2

Revision 2, 6.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/** \file Base64.cpp
2 **     \date  2004-02-13
3 **     \author grymse@alhem.net
4**/
5/*
6Copyright (C) 2004-2007  Anders Hedstrom
7
8This library is made available under the terms of the GNU GPL.
9
10If you would like to use this library in a closed-source application,
11a separate license agreement is available. For information about
12the closed-source license agreement for the C++ sockets library,
13please visit http://www.alhem.net/Sockets/license.html and/or
14email license@alhem.net.
15
16This program is free software; you can redistribute it and/or
17modify it under the terms of the GNU General Public License
18as published by the Free Software Foundation; either version 2
19of the License, or (at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29*/
30#include "Base64.h"
31
32#ifdef SOCKETS_NAMESPACE
33namespace SOCKETS_NAMESPACE {
34#endif
35
36
37const char *Base64::bstr =
38        "ABCDEFGHIJKLMNOPQ"
39        "RSTUVWXYZabcdefgh"
40        "ijklmnopqrstuvwxy"
41        "z0123456789+/";
42
43const 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};
52
53
54Base64::Base64()
55{
56}
57
58
59void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
60{
61        size_t remain;
62        size_t i = 0;
63        size_t o = 0;
64        char input[4];
65
66        output = "";
67        remain = fread(input,1,3,fil);
68        while (remain > 0)
69        {
70                if (add_crlf && o && o % 76 == 0)
71                        output += "\n";
72                switch (remain)
73                {
74                case 1:
75                        output += bstr[ ((input[i] >> 2) & 0x3f) ];
76                        output += bstr[ ((input[i] << 4) & 0x30) ];
77                        output += "==";
78                        break;
79                case 2:
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) ];
83                        output += "=";
84                        break;
85                default:
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) ];
90                }
91                o += 4;
92                //
93                remain = fread(input,1,3,fil);
94        }
95}
96
97
98void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
99{
100        encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
101}
102
103
104void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
105{
106        size_t i = 0;
107        size_t o = 0;
108       
109        output = "";
110        while (i < l)
111        {
112                size_t remain = l - i;
113                if (add_crlf && o && o % 76 == 0)
114                        output += "\n";
115                switch (remain)
116                {
117                case 1:
118                        output += bstr[ ((input[i] >> 2) & 0x3f) ];
119                        output += bstr[ ((input[i] << 4) & 0x30) ];
120                        output += "==";
121                        break;
122                case 2:
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) ];
126                        output += "=";
127                        break;
128                default:
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) ];
133                }
134                o += 4;
135                i += 3;
136        }
137}
138
139
140void Base64::encode(const unsigned char* input,size_t l,std::string& output,bool add_crlf)
141{
142        size_t i = 0;
143        size_t o = 0;
144       
145        output = "";
146        while (i < l)
147        {
148                size_t remain = l - i;
149                if (add_crlf && o && o % 76 == 0)
150                        output += "\n";
151                switch (remain)
152                {
153                case 1:
154                        output += bstr[ ((input[i] >> 2) & 0x3f) ];
155                        output += bstr[ ((input[i] << 4) & 0x30) ];
156                        output += "==";
157                        break;
158                case 2:
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) ];
162                        output += "=";
163                        break;
164                default:
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) ];
169                }
170                o += 4;
171                i += 3;
172        }
173}
174
175
176void Base64::decode(const std::string& input,std::string& output)
177{
178        size_t i = 0;
179        size_t l = input.size();
180       
181        output = "";
182        while (i < l)
183        {
184                while (i < l && (input[i] == 13 || input[i] == 10))
185                        i++;
186                if (i < l)
187                {
188                        char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
189                                        (rstr[(int)input[i + 1]] >> 4 & 0x03));
190                        output += b1;
191                        if (input[i + 2] != '=')
192                        {
193                                char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
194                                                (rstr[(int)input[i + 2]] >> 2 & 0x0f));
195                                output += b2;
196                        }
197                        if (input[i + 3] != '=')
198                        {
199                                char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
200                                                rstr[(int)input[i + 3]]);
201                                output += b3;
202                        }
203                        i += 4;
204                }
205        }
206}
207
208
209void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
210{
211        size_t i = 0;
212        size_t l = input.size();
213        size_t j = 0;
214       
215        while (i < l)
216        {
217                while (i < l && (input[i] == 13 || input[i] == 10))
218                        i++;
219                if (i < l)
220                {
221                        unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
222                                        (rstr[(int)input[i + 1]] >> 4 & 0x03));
223                        if (output)
224                        {
225                                output[j] = b1;
226                        }
227                        j++;
228                        if (input[i + 2] != '=')
229                        {
230                                unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
231                                                (rstr[(int)input[i + 2]] >> 2 & 0x0f));
232                                if (output)
233                                {
234                                        output[j] = b2;
235                                }
236                                j++;
237                        }
238                        if (input[i + 3] != '=')
239                        {
240                                unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
241                                                rstr[(int)input[i + 3]]);
242                                if (output)
243                                {
244                                        output[j] = b3;
245                                }
246                                j++;
247                        }
248                        i += 4;
249                }
250        }
251        sz = j;
252}
253
254
255size_t Base64::decode_length(const std::string& str64)
256{
257        if (str64.empty() || str64.size() % 4)
258                return 0;
259        size_t l = 3 * (str64.size() / 4 - 1) + 1;
260        if (str64[str64.size() - 2] != '=')
261                l++;
262        if (str64[str64.size() - 1] != '=')
263                l++;
264        return l;
265}
266
267
268#ifdef SOCKETS_NAMESPACE
269}
270#endif
271
272
Note: See TracBrowser for help on using the browser.