/home/coin/SVN-release/OS-2.3.0/OS/src/OSUtils/OSBase64.cpp

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

Generated on Fri Nov 19 13:31:09 2010 by  doxygen 1.4.7