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

Go to the documentation of this file.
00001 /* $Id: OSBase64.cpp 4292 2011-09-21 05:47:18Z kmartin $ */
00015 #include "OSBase64.h"
00016 #include <sstream>
00017 
00018 /*
00019 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00020              "abcdefghijklmnopqrstuvwxyz"
00021              "0123456789+/";
00022 */
00023 
00024 namespace
00025 {
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 {
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     {
00046         in_byte1 = *(bytes++);
00047         in_byte2 = *(bytes++);
00048         in_byte3 = *(bytes++);
00049         out_byte1 = (in_byte1 >> 2);
00050         out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00051         out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
00052         out_byte4 = in_byte3 & 0x3f;
00053         outStr << base64_chars[out_byte1];
00054         outStr <<  base64_chars[out_byte2];
00055         outStr << base64_chars[out_byte3];
00056         outStr << base64_chars[out_byte4]  ;
00057         test = test - 3;
00058     }
00059     // now take care of padding
00060     if(remainder > 0)
00061     {
00062         in_byte1 = *(bytes++);
00063         in_byte3 = '\0';
00064         out_byte1 = (in_byte1 >> 2);
00065         outStr << base64_chars[out_byte1];
00066         if(remainder == 1)
00067         {
00068             in_byte2 = '\0';
00069             out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00070             outStr <<  base64_chars[out_byte2];
00071             outStr << '=';
00072         }
00073         else
00074         {
00075             in_byte2 = *(bytes++);
00076             out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
00077             out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
00078             outStr <<  base64_chars[out_byte2];
00079             outStr << base64_chars[out_byte3];
00080         }
00081         outStr << '=';
00082     }
00083     return outStr.str();
00084 
00085 }//encodeb64
00086 
00087 std::string Base64::decodeb64(char* b64bytes)
00088 {
00089     unsigned char out_byte1, out_byte2, out_byte3;
00090     unsigned char in_byte1, in_byte2, in_byte3, in_byte4;
00091     std::ostringstream outStr;
00092 
00093     const std::string base64_chars = base64_char_cstr ;
00094 
00095     // first take of the non-padded bytes
00096     while(*b64bytes != '=' && *b64bytes != '\0')
00097     {
00098         in_byte1 = base64_chars.find(*(b64bytes++));
00099         in_byte2 = base64_chars.find(*(b64bytes++));
00100         in_byte3 = base64_chars.find(*(b64bytes++));
00101         in_byte4 = base64_chars.find(*(b64bytes++));
00102         out_byte1 = (in_byte1 << 2)  | ((in_byte2 & 0x30)  >> 4)  ;
00103         out_byte2 = ((in_byte2 & 0x0f) << 4)  | ((in_byte3 & 0x3c)  >> 2)  ;
00104         out_byte3 = ((in_byte3  & 0x03) << 6)  |  in_byte4  ;
00105         outStr << out_byte1;
00106         outStr << out_byte2;
00107         outStr << out_byte3;
00108     }
00109     // now take into account the padding
00110     if(*b64bytes == '=')
00111     {
00112         in_byte1 = base64_chars.find(*(b64bytes++));
00113         in_byte2 = base64_chars.find(*(b64bytes++));
00114         in_byte4 = '\0';
00115         out_byte1 = (in_byte1 << 2)  | ((in_byte2 & 0x30)  >> 4)  ;
00116         outStr << out_byte1;
00117         *b64bytes++;
00118         if(*b64bytes == '\0')  //if null we had only one = in the padding
00119         {
00120             in_byte3 = base64_chars.find(*(b64bytes++));
00121             out_byte2 = ((in_byte2 & 0x0f) << 4)  | ((in_byte3 & 0x3c)  >> 2)  ;
00122             out_byte3 = ((in_byte3  & 0x03) << 6)  |  in_byte4  ;
00123             outStr << out_byte2;
00124         }
00125         else  // we have == in the padding
00126         {
00127             in_byte3 = '\0';
00128             out_byte2 = ((in_byte2 & 0x0f) << 4)  | ((in_byte3 & 0x3c)  >> 2)  ;
00129             out_byte3 = ((in_byte3  & 0x03) << 6)  |  in_byte4  ;
00130             //outStr << out_byte2;
00131             //outStr << out_byte3;
00132         }
00133     }
00134     return outStr.str();
00135 } // decodeb64
00136 
00137 
00138 

Generated on Thu Nov 10 03:05:51 2011 by  doxygen 1.4.7