![]() |
Base64 Class ReferenceBase64 encode/decode.
More...
|
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 &) | |
Base64 & | operator= (const Base64 &) |
Static Private Attributes | |
static const char * | bstr |
static const char | rstr [128] |
Definition at line 48 of file Base64.h.
Base64::Base64 | ( | ) |
void Base64::encode | ( | FILE * | , | |
std::string & | , | |||
bool | add_crlf = true | |||
) |
Definition at line 61 of file Base64.cpp.
References bstr.
Referenced by encode(), NullCrypt::encrypt(), MinderHandler::KeepAlive(), MinderHandler::SendConnectList(), and MinderHandler::SendTop().
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 }
void Base64::encode | ( | const std::string & | , | |
std::string & | , | |||
bool | add_crlf = true | |||
) |
Definition at line 100 of file Base64.cpp.
References encode().
00101 { 00102 encode(str_in.c_str(), str_in.size(), str_out, add_crlf); 00103 }
void Base64::encode | ( | const char * | , | |
size_t | , | |||
std::string & | , | |||
bool | add_crlf = true | |||
) |
Definition at line 106 of file Base64.cpp.
References bstr.
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 }
void Base64::encode | ( | const unsigned char * | , | |
size_t | , | |||
std::string & | , | |||
bool | add_crlf = true | |||
) |
Definition at line 142 of file Base64.cpp.
References bstr.
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 }
void Base64::decode | ( | const std::string & | , | |
std::string & | ||||
) |
Definition at line 178 of file Base64.cpp.
References rstr.
Referenced by Utility::base64d(), NullCrypt::decrypt(), and HttpdSocket::Send64().
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 }
void Base64::decode | ( | const std::string & | , | |
unsigned char * | , | |||
size_t & | ||||
) |
Definition at line 211 of file Base64.cpp.
References rstr.
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 }
size_t Base64::decode_length | ( | const std::string & | ) |
Definition at line 257 of file Base64.cpp.
Referenced by HttpdSocket::Send64().
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 }
const char * Base64::bstr [static, private] |
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().