/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/src/OSUtils/OSBase64.cpp

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

Generated on Sat Mar 29 22:38:03 2008 by  doxygen 1.5.3