00001
00016 #include "OSBase64.h"
00017 #include <sstream>
00018
00019
00020
00021
00022
00023
00024
00025 namespace {
00026
00027 const char *base64_char_cstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00028 "abcdefghijklmnopqrstuvwxyz"
00029 "0123456789+/";
00030
00031 }
00032
00033 std::string Base64::encodeb64(char* bytes, int string_size){
00034 unsigned char out_byte1, out_byte2, out_byte3, out_byte4;
00035 unsigned char in_byte1, in_byte2, in_byte3;
00036
00037 const std::string base64_chars = base64_char_cstr ;
00038
00039 std::ostringstream outStr;
00040 int remainder = string_size%3;
00041
00042 int test = string_size - remainder;
00043 while(test > 0){
00044 in_byte1 = *(bytes++);
00045 in_byte2 = *(bytes++);
00046 in_byte3 = *(bytes++);
00047 out_byte1 = (in_byte1 >> 2);
00048 out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00049 out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
00050 out_byte4 = in_byte3 & 0x3f;
00051 outStr << base64_chars[out_byte1];
00052 outStr << base64_chars[out_byte2];
00053 outStr << base64_chars[out_byte3];
00054 outStr << base64_chars[out_byte4] ;
00055 test = test - 3;
00056 }
00057
00058 if(remainder > 0){
00059 in_byte1 = *(bytes++);
00060 in_byte3 = '\0';
00061 out_byte1 = (in_byte1 >> 2);
00062 outStr << base64_chars[out_byte1];
00063 if(remainder == 1){
00064 in_byte2 = '\0';
00065 out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00066 outStr << base64_chars[out_byte2];
00067 outStr << '=';
00068 }
00069 else{
00070 in_byte2 = *(bytes++);
00071 out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00072 out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
00073 outStr << base64_chars[out_byte2];
00074 outStr << base64_chars[out_byte3];
00075 }
00076 outStr << '=';
00077 }
00078 return outStr.str();
00079
00080 }
00081
00082 std::string Base64::decodeb64(char* b64bytes){
00083 unsigned char out_byte1, out_byte2, out_byte3;
00084 unsigned char in_byte1, in_byte2, in_byte3, in_byte4;
00085 std::ostringstream outStr;
00086
00087 const std::string base64_chars = base64_char_cstr ;
00088
00089
00090 while(*b64bytes != '=' && *b64bytes != '\0'){
00091 in_byte1 = base64_chars.find(*(b64bytes++));
00092 in_byte2 = base64_chars.find(*(b64bytes++));
00093 in_byte3 = base64_chars.find(*(b64bytes++));
00094 in_byte4 = base64_chars.find(*(b64bytes++));
00095 out_byte1 = (in_byte1 << 2) | ((in_byte2 & 0x30) >> 4) ;
00096 out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
00097 out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
00098 outStr << out_byte1;
00099 outStr << out_byte2;
00100 outStr << out_byte3;
00101 }
00102
00103 if(*b64bytes == '='){
00104 in_byte1 = base64_chars.find(*(b64bytes++));
00105 in_byte2 = base64_chars.find(*(b64bytes++));
00106 in_byte4 = '\0';
00107 out_byte1 = (in_byte1 << 2) | ((in_byte2 & 0x30) >> 4) ;
00108 outStr << out_byte1;
00109 *b64bytes++;
00110 if(*b64bytes == '\0'){
00111 in_byte3 = base64_chars.find(*(b64bytes++));
00112 out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
00113 out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
00114 outStr << out_byte2;
00115 }
00116 else{
00117 in_byte3 = '\0';
00118 out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
00119 out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
00120
00121
00122 }
00123 }
00124 return outStr.str();
00125 }
00126
00127
00128