Logo
~Sockets~
~Examples~
~Contact~


Base64.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #include "Base64.h"
00031 
00032 #ifdef SOCKETS_NAMESPACE
00033 namespace SOCKETS_NAMESPACE {
00034 #endif
00035 
00036 
00037 const char *Base64::bstr =
00038         "ABCDEFGHIJKLMNOPQ"
00039         "RSTUVWXYZabcdefgh"
00040         "ijklmnopqrstuvwxy"
00041         "z0123456789+/";
00042 
00043 const char Base64::rstr[] = {
00044           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
00045           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
00046           0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  62,   0,   0,   0,  63, 
00047          52,  53,  54,  55,  56,  57,  58,  59,  60,  61,   0,   0,   0,   0,   0,   0, 
00048           0,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14, 
00049          15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,   0,   0,   0,   0,   0, 
00050           0,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40, 
00051          41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,   0,   0,   0,   0,   0};
00052 
00053 
00054 Base64::Base64()
00055 {
00056 }
00057 
00058 
00059 void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
00060 {
00061         size_t remain;
00062         size_t i = 0;
00063         size_t o = 0;
00064         char input[4];
00065 
00066         output = "";
00067         remain = fread(input,1,3,fil);
00068         while (remain > 0)
00069         {
00070                 if (add_crlf && o && o % 76 == 0)
00071                         output += "\n";
00072                 switch (remain)
00073                 {
00074                 case 1:
00075                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00076                         output += bstr[ ((input[i] << 4) & 0x30) ];
00077                         output += "==";
00078                         break;
00079                 case 2:
00080                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00081                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00082                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00083                         output += "=";
00084                         break;
00085                 default:
00086                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00087                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00088                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00089                         output += bstr[ (input[i + 2] & 0x3f) ];
00090                 }
00091                 o += 4;
00092                 //
00093                 remain = fread(input,1,3,fil);
00094         }
00095 }
00096 
00097 
00098 void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
00099 {
00100         encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
00101 }
00102 
00103 
00104 void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
00105 {
00106         size_t i = 0;
00107         size_t o = 0;
00108         
00109         output = "";
00110         while (i < l)
00111         {
00112                 size_t remain = l - i;
00113                 if (add_crlf && o && o % 76 == 0)
00114                         output += "\n";
00115                 switch (remain)
00116                 {
00117                 case 1:
00118                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00119                         output += bstr[ ((input[i] << 4) & 0x30) ];
00120                         output += "==";
00121                         break;
00122                 case 2:
00123                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00124                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00125                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00126                         output += "=";
00127                         break;
00128                 default:
00129                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00130                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00131                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00132                         output += bstr[ (input[i + 2] & 0x3f) ];
00133                 }
00134                 o += 4;
00135                 i += 3;
00136         }
00137 }
00138 
00139 
00140 void Base64::encode(const unsigned char* input,size_t l,std::string& output,bool add_crlf)
00141 {
00142         size_t i = 0;
00143         size_t o = 0;
00144         
00145         output = "";
00146         while (i < l)
00147         {
00148                 size_t remain = l - i;
00149                 if (add_crlf && o && o % 76 == 0)
00150                         output += "\n";
00151                 switch (remain)
00152                 {
00153                 case 1:
00154                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00155                         output += bstr[ ((input[i] << 4) & 0x30) ];
00156                         output += "==";
00157                         break;
00158                 case 2:
00159                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00160                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00161                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00162                         output += "=";
00163                         break;
00164                 default:
00165                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00166                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00167                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00168                         output += bstr[ (input[i + 2] & 0x3f) ];
00169                 }
00170                 o += 4;
00171                 i += 3;
00172         }
00173 }
00174 
00175 
00176 void Base64::decode(const std::string& input,std::string& output)
00177 {
00178         size_t i = 0;
00179         size_t l = input.size();
00180         
00181         output = "";
00182         while (i < l)
00183         {
00184                 while (i < l && (input[i] == 13 || input[i] == 10))
00185                         i++;
00186                 if (i < l)
00187                 {
00188                         char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
00189                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
00190                         output += b1;
00191                         if (input[i + 2] != '=')
00192                         {
00193                                 char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
00194                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
00195                                 output += b2;
00196                         }
00197                         if (input[i + 3] != '=')
00198                         {
00199                                 char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
00200                                                 rstr[(int)input[i + 3]]);
00201                                 output += b3;
00202                         }
00203                         i += 4;
00204                 }
00205         }
00206 }
00207 
00208 
00209 void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
00210 {
00211         size_t i = 0;
00212         size_t l = input.size();
00213         size_t j = 0;
00214         
00215         while (i < l)
00216         {
00217                 while (i < l && (input[i] == 13 || input[i] == 10))
00218                         i++;
00219                 if (i < l)
00220                 {
00221                         unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
00222                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
00223                         if (output)
00224                         {
00225                                 output[j] = b1;
00226                         }
00227                         j++;
00228                         if (input[i + 2] != '=')
00229                         {
00230                                 unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
00231                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
00232                                 if (output)
00233                                 {
00234                                         output[j] = b2;
00235                                 }
00236                                 j++;
00237                         }
00238                         if (input[i + 3] != '=')
00239                         {
00240                                 unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
00241                                                 rstr[(int)input[i + 3]]);
00242                                 if (output)
00243                                 {
00244                                         output[j] = b3;
00245                                 }
00246                                 j++;
00247                         }
00248                         i += 4;
00249                 }
00250         }
00251         sz = j;
00252 }
00253 
00254 
00255 size_t Base64::decode_length(const std::string& str64)
00256 {
00257         if (!str64.size() || str64.size() % 4)
00258                 return 0;
00259         size_t l = 3 * (str64.size() / 4 - 1) + 1;
00260         if (str64[str64.size() - 2] != '=')
00261                 l++;
00262         if (str64[str64.size() - 1] != '=')
00263                 l++;
00264         return l;
00265 }
00266 
00267 
00268 #ifdef SOCKETS_NAMESPACE
00269 }
00270 #endif
00271 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4