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 //      for (int i = 0; i < 64; i++)
00057 //              rstr[(int)bstr[i]] = i;
00058 }
00059 
00060 
00061 void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
00062 {
00063         size_t remain;
00064         size_t i = 0;
00065         size_t o = 0;
00066         char input[4];
00067 
00068         output = "";
00069         remain = fread(input,1,3,fil);
00070         while (remain > 0)
00071         {
00072                 if (add_crlf && o && o % 76 == 0)
00073                         output += "\n";
00074                 switch (remain)
00075                 {
00076                 case 1:
00077                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00078                         output += bstr[ ((input[i] << 4) & 0x30) ];
00079                         output += "==";
00080                         break;
00081                 case 2:
00082                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00083                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00084                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00085                         output += "=";
00086                         break;
00087                 default:
00088                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00089                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00090                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00091                         output += bstr[ (input[i + 2] & 0x3f) ];
00092                 }
00093                 o += 4;
00094                 //
00095                 remain = fread(input,1,3,fil);
00096         }
00097 }
00098 
00099 
00100 void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
00101 {
00102         encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
00103 }
00104 
00105 
00106 void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
00107 {
00108         size_t i = 0;
00109         size_t o = 0;
00110         
00111         output = "";
00112         while (i < l)
00113         {
00114                 size_t remain = l - i;
00115                 if (add_crlf && o && o % 76 == 0)
00116                         output += "\n";
00117                 switch (remain)
00118                 {
00119                 case 1:
00120                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00121                         output += bstr[ ((input[i] << 4) & 0x30) ];
00122                         output += "==";
00123                         break;
00124                 case 2:
00125                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00126                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00127                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00128                         output += "=";
00129                         break;
00130                 default:
00131                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00132                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00133                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00134                         output += bstr[ (input[i + 2] & 0x3f) ];
00135                 }
00136                 o += 4;
00137                 i += 3;
00138         }
00139 }
00140 
00141 
00142 void Base64::encode(const unsigned char* input,size_t l,std::string& output,bool add_crlf)
00143 {
00144         size_t i = 0;
00145         size_t o = 0;
00146         
00147         output = "";
00148         while (i < l)
00149         {
00150                 size_t remain = l - i;
00151                 if (add_crlf && o && o % 76 == 0)
00152                         output += "\n";
00153                 switch (remain)
00154                 {
00155                 case 1:
00156                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00157                         output += bstr[ ((input[i] << 4) & 0x30) ];
00158                         output += "==";
00159                         break;
00160                 case 2:
00161                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00162                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00163                         output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
00164                         output += "=";
00165                         break;
00166                 default:
00167                         output += bstr[ ((input[i] >> 2) & 0x3f) ];
00168                         output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
00169                         output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
00170                         output += bstr[ (input[i + 2] & 0x3f) ];
00171                 }
00172                 o += 4;
00173                 i += 3;
00174         }
00175 }
00176 
00177 
00178 void Base64::decode(const std::string& input,std::string& output)
00179 {
00180         size_t i = 0;
00181         size_t l = input.size();
00182         
00183         output = "";
00184         while (i < l)
00185         {
00186                 while (i < l && (input[i] == 13 || input[i] == 10))
00187                         i++;
00188                 if (i < l)
00189                 {
00190                         char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
00191                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
00192                         output += b1;
00193                         if (input[i + 2] != '=')
00194                         {
00195                                 char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
00196                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
00197                                 output += b2;
00198                         }
00199                         if (input[i + 3] != '=')
00200                         {
00201                                 char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
00202                                                 rstr[(int)input[i + 3]]);
00203                                 output += b3;
00204                         }
00205                         i += 4;
00206                 }
00207         }
00208 }
00209 
00210 
00211 void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
00212 {
00213         size_t i = 0;
00214         size_t l = input.size();
00215         size_t j = 0;
00216         
00217         while (i < l)
00218         {
00219                 while (i < l && (input[i] == 13 || input[i] == 10))
00220                         i++;
00221                 if (i < l)
00222                 {
00223                         unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
00224                                         (rstr[(int)input[i + 1]] >> 4 & 0x03));
00225                         if (output)
00226                         {
00227                                 output[j] = b1;
00228                         }
00229                         j++;
00230                         if (input[i + 2] != '=')
00231                         {
00232                                 unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
00233                                                 (rstr[(int)input[i + 2]] >> 2 & 0x0f));
00234                                 if (output)
00235                                 {
00236                                         output[j] = b2;
00237                                 }
00238                                 j++;
00239                         }
00240                         if (input[i + 3] != '=')
00241                         {
00242                                 unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
00243                                                 rstr[(int)input[i + 3]]);
00244                                 if (output)
00245                                 {
00246                                         output[j] = b3;
00247                                 }
00248                                 j++;
00249                         }
00250                         i += 4;
00251                 }
00252         }
00253         sz = j;
00254 }
00255 
00256 
00257 size_t Base64::decode_length(const std::string& str64)
00258 {
00259         if (!str64.size() || str64.size() % 4)
00260                 return 0;
00261         size_t l = 3 * (str64.size() / 4 - 1) + 1;
00262         if (str64[str64.size() - 2] != '=')
00263                 l++;
00264         if (str64[str64.size() - 1] != '=')
00265                 l++;
00266         return l;
00267 }
00268 
00269 
00270 #ifdef SOCKETS_NAMESPACE
00271 }
00272 #endif
00273 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4