![]() |
Base64 Class ReferenceBase64 encode/decode.
More...
|
Public Member Functions | |
Base64 () | |
void | encode (FILE *, std::string &, bool add_crlf=true) |
void | encode (IFile *, 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 53 of file Base64.h.
Base64::Base64 | ( | ) |
Base64::Base64 | ( | const Base64 & | ) | [inline, private] |
void Base64::encode | ( | FILE * | fil, | |
std::string & | output, | |||
bool | add_crlf = true | |||
) |
Definition at line 101 of file Base64.cpp.
References bstr.
Referenced by encode().
00102 { 00103 size_t remain; 00104 size_t i = 0; 00105 size_t o = 0; 00106 char input[4]; 00107 00108 output = ""; 00109 remain = fread(input,1,3,fil); 00110 while (remain > 0) 00111 { 00112 if (add_crlf && o && o % 76 == 0) 00113 output += "\n"; 00114 switch (remain) 00115 { 00116 case 1: 00117 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00118 output += bstr[ ((input[i] << 4) & 0x30) ]; 00119 output += "=="; 00120 break; 00121 case 2: 00122 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00123 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00124 output += bstr[ ((input[i + 1] << 2) & 0x3c) ]; 00125 output += "="; 00126 break; 00127 default: 00128 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00129 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00130 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ]; 00131 output += bstr[ (input[i + 2] & 0x3f) ]; 00132 } 00133 o += 4; 00134 // 00135 remain = fread(input,1,3,fil); 00136 } 00137 }
void Base64::encode | ( | IFile * | fil, | |
std::string & | output, | |||
bool | add_crlf = true | |||
) |
Definition at line 62 of file Base64.cpp.
References bstr.
00063 { 00064 size_t remain; 00065 size_t i = 0; 00066 size_t o = 0; 00067 char input[4]; 00068 00069 output = ""; 00070 remain = fil -> fread(input,1,3); 00071 while (remain > 0) 00072 { 00073 if (add_crlf && o && o % 76 == 0) 00074 output += "\n"; 00075 switch (remain) 00076 { 00077 case 1: 00078 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00079 output += bstr[ ((input[i] << 4) & 0x30) ]; 00080 output += "=="; 00081 break; 00082 case 2: 00083 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00084 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00085 output += bstr[ ((input[i + 1] << 2) & 0x3c) ]; 00086 output += "="; 00087 break; 00088 default: 00089 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00090 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00091 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ]; 00092 output += bstr[ (input[i + 2] & 0x3f) ]; 00093 } 00094 o += 4; 00095 // 00096 remain = fil -> fread(input,1,3); 00097 } 00098 }
void Base64::encode | ( | const std::string & | str_in, | |
std::string & | str_out, | |||
bool | add_crlf = true | |||
) |
Definition at line 140 of file Base64.cpp.
References encode().
00141 { 00142 encode(str_in.c_str(), str_in.size(), str_out, add_crlf); 00143 }
void Base64::encode | ( | const char * | input, | |
size_t | l, | |||
std::string & | output, | |||
bool | add_crlf = true | |||
) |
Definition at line 146 of file Base64.cpp.
References bstr.
00147 { 00148 size_t i = 0; 00149 size_t o = 0; 00150 00151 output = ""; 00152 while (i < l) 00153 { 00154 size_t remain = l - i; 00155 if (add_crlf && o && o % 76 == 0) 00156 output += "\n"; 00157 switch (remain) 00158 { 00159 case 1: 00160 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00161 output += bstr[ ((input[i] << 4) & 0x30) ]; 00162 output += "=="; 00163 break; 00164 case 2: 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) ]; 00168 output += "="; 00169 break; 00170 default: 00171 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00172 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00173 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ]; 00174 output += bstr[ (input[i + 2] & 0x3f) ]; 00175 } 00176 o += 4; 00177 i += 3; 00178 } 00179 }
void Base64::encode | ( | const unsigned char * | input, | |
size_t | l, | |||
std::string & | output, | |||
bool | add_crlf = true | |||
) |
Definition at line 182 of file Base64.cpp.
References bstr.
00183 { 00184 size_t i = 0; 00185 size_t o = 0; 00186 00187 output = ""; 00188 while (i < l) 00189 { 00190 size_t remain = l - i; 00191 if (add_crlf && o && o % 76 == 0) 00192 output += "\n"; 00193 switch (remain) 00194 { 00195 case 1: 00196 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00197 output += bstr[ ((input[i] << 4) & 0x30) ]; 00198 output += "=="; 00199 break; 00200 case 2: 00201 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00202 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00203 output += bstr[ ((input[i + 1] << 2) & 0x3c) ]; 00204 output += "="; 00205 break; 00206 default: 00207 output += bstr[ ((input[i] >> 2) & 0x3f) ]; 00208 output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ]; 00209 output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ]; 00210 output += bstr[ (input[i + 2] & 0x3f) ]; 00211 } 00212 o += 4; 00213 i += 3; 00214 } 00215 }
void Base64::decode | ( | const std::string & | input, | |
std::string & | output | |||
) |
Definition at line 218 of file Base64.cpp.
References rstr.
Referenced by Utility::base64d(), and HttpdSocket::Send64().
00219 { 00220 size_t i = 0; 00221 size_t l = input.size(); 00222 00223 output = ""; 00224 while (i < l) 00225 { 00226 while (i < l && (input[i] == 13 || input[i] == 10)) 00227 i++; 00228 if (i < l) 00229 { 00230 char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) + 00231 (rstr[(int)input[i + 1]] >> 4 & 0x03)); 00232 output += b1; 00233 if (input[i + 2] != '=') 00234 { 00235 char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) + 00236 (rstr[(int)input[i + 2]] >> 2 & 0x0f)); 00237 output += b2; 00238 } 00239 if (input[i + 3] != '=') 00240 { 00241 char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) + 00242 rstr[(int)input[i + 3]]); 00243 output += b3; 00244 } 00245 i += 4; 00246 } 00247 } 00248 }
void Base64::decode | ( | const std::string & | input, | |
unsigned char * | output, | |||
size_t & | sz | |||
) |
Definition at line 251 of file Base64.cpp.
References rstr.
00252 { 00253 size_t i = 0; 00254 size_t l = input.size(); 00255 size_t j = 0; 00256 00257 while (i < l) 00258 { 00259 while (i < l && (input[i] == 13 || input[i] == 10)) 00260 i++; 00261 if (i < l) 00262 { 00263 unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) + 00264 (rstr[(int)input[i + 1]] >> 4 & 0x03)); 00265 if (output) 00266 { 00267 output[j] = b1; 00268 } 00269 j++; 00270 if (input[i + 2] != '=') 00271 { 00272 unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) + 00273 (rstr[(int)input[i + 2]] >> 2 & 0x0f)); 00274 if (output) 00275 { 00276 output[j] = b2; 00277 } 00278 j++; 00279 } 00280 if (input[i + 3] != '=') 00281 { 00282 unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) + 00283 rstr[(int)input[i + 3]]); 00284 if (output) 00285 { 00286 output[j] = b3; 00287 } 00288 j++; 00289 } 00290 i += 4; 00291 } 00292 } 00293 sz = j; 00294 }
size_t Base64::decode_length | ( | const std::string & | str64 | ) |
Definition at line 297 of file Base64.cpp.
Referenced by HttpdSocket::Send64().
00298 { 00299 if (!str64.size() || str64.size() % 4) 00300 return 0; 00301 size_t l = 3 * (str64.size() / 4 - 1) + 1; 00302 if (str64[str64.size() - 2] != '=') 00303 l++; 00304 if (str64[str64.size() - 1] != '=') 00305 l++; 00306 return l; 00307 }
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 73 of file Base64.h.
Referenced by decode().