Logo
~Sockets~
~Examples~
~Contact~


Base64 Class Reference
[Utilities]

Base64 encode/decode. More...

#include <Base64.h>

List of all members.


Public Member Functions

 Base64 ()
void encode (FILE *, std::string &, bool add_crlf=true)
void encode (const std::string &, std::string &, bool add_crlf=true)
void encode (const char *, size_t, std::string &, bool add_crlf=true)
void encode (const unsigned char *, size_t, std::string &, bool add_crlf=true)
void decode (const std::string &, std::string &)
void decode (const std::string &, unsigned char *, size_t &)
size_t decode_length (const std::string &)

Private Member Functions

 Base64 (const Base64 &)
Base64operator= (const Base64 &)

Static Private Attributes

static const char * bstr
static const char rstr [128]

Detailed Description

Base64 encode/decode.

Definition at line 48 of file Base64.h.


Constructor & Destructor Documentation

Base64::Base64 (  ) 

Definition at line 54 of file Base64.cpp.

00055 {
00056 }

Base64::Base64 ( const Base64  )  [inline, private]

Definition at line 64 of file Base64.h.

00064 {}


Member Function Documentation

void Base64::encode ( FILE *  ,
std::string &  ,
bool  add_crlf = true 
)

Definition at line 59 of file Base64.cpp.

References bstr.

Referenced by encode().

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 }

void Base64::encode ( const std::string &  ,
std::string &  ,
bool  add_crlf = true 
)

Definition at line 98 of file Base64.cpp.

References encode().

00099 {
00100         encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
00101 }

void Base64::encode ( const char *  ,
size_t  ,
std::string &  ,
bool  add_crlf = true 
)

Definition at line 104 of file Base64.cpp.

References bstr.

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 }

void Base64::encode ( const unsigned char *  ,
size_t  ,
std::string &  ,
bool  add_crlf = true 
)

Definition at line 140 of file Base64.cpp.

References bstr.

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 }

void Base64::decode ( const std::string &  ,
std::string &   
)

Definition at line 176 of file Base64.cpp.

References rstr.

Referenced by Utility::base64d(), and HttpdSocket::Send64().

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 }

void Base64::decode ( const std::string &  ,
unsigned char *  ,
size_t &   
)

Definition at line 209 of file Base64.cpp.

References rstr.

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 }

size_t Base64::decode_length ( const std::string &   ) 

Definition at line 255 of file Base64.cpp.

Referenced by HttpdSocket::Send64().

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 }

Base64& Base64::operator= ( const Base64  )  [inline, private]

Definition at line 65 of file Base64.h.

00065 { return *this; }


Member Data Documentation

const char * Base64::bstr [static, private]

Initial value:

        "ABCDEFGHIJKLMNOPQ"
        "RSTUVWXYZabcdefgh"
        "ijklmnopqrstuvwxy"
        "z0123456789+/"

Definition at line 66 of file Base64.h.

Referenced by encode().

const char Base64::rstr [static, private]

Initial value:

 {
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  62,   0,   0,   0,  63, 
         52,  53,  54,  55,  56,  57,  58,  59,  60,  61,   0,   0,   0,   0,   0,   0, 
          0,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14, 
         15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,   0,   0,   0,   0,   0, 
          0,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40, 
         41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,   0,   0,   0,   0,   0}

Definition at line 67 of file Base64.h.

Referenced by decode().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4