Base64Encoder.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Base64Encoder.cpp 1644 2009-01-27 19:26:14Z ckiesch $"
00002 
00005 
00006 //============================================================================
00007 //
00008 //  This file is part of GPSTk, the GPS Toolkit.
00009 //
00010 //  The GPSTk is free software; you can redistribute it and/or modify
00011 //  it under the terms of the GNU Lesser General Public License as published
00012 //  by the Free Software Foundation; either version 2.1 of the License, or
00013 //  any later version.
00014 //
00015 //  The GPSTk is distributed in the hope that it will be useful,
00016 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU Lesser General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU Lesser General Public
00021 //  License along with GPSTk; if not, write to the Free Software Foundation,
00022 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //  
00024 //  Copyright 2004, The University of Texas at Austin
00025 //
00026 //============================================================================
00027 
00028 #include "Base64Encoder.hpp"
00029 
00030 namespace vdraw
00031 {
00032   const std::string Base64Encoder::encode_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00033 
00034   // The above should be equivalent to the one below but a good deal more
00035   // effecient as the size of the return string is determined up front and with
00036   // one allocation, the rest of the changes are indexed.
00037   // Keeping the old version around for now just in case.
00038 #if 1
00039   std::string Base64Encoder::encode(const std::string &str)
00040   {
00041     int remainder = (int)str.size() % 3;        
00042     int end = (int)str.size() - remainder;
00043     std::string s((end/3+(remainder?1:0))*4,(char)0);
00044     int x = 0;
00045     long buffer;
00046     // Every 3 bytes can be encoded in 4x 6-bit characters
00047     for(int i=0;i<end;i+=3)
00048     {
00049       buffer = ((0x0FF&str[i])<<16) | ((0x0FF&str[i+1])<<8) | (0x0FF&str[i+2]);
00050       s[x++] = encode6(buffer>>18);
00051       s[x++] = encode6(buffer>>12);
00052       s[x++] = encode6(buffer>>6);
00053       s[x++] = encode6(buffer);
00054     }
00055     if(remainder>=1)
00056     {
00057       buffer = (0x0FF&str[end])<<16;
00058       if(remainder==2)
00059         buffer |= (0x0FF&str[end+1])<<8;
00060     }
00061     if(remainder)
00062     {
00063       s[x++] = encode6(buffer>>18);
00064       s[x++] = encode6(buffer>>12);
00065       if(remainder==2)
00066         s[x++] = encode6(buffer>>6);
00067 
00068       if(remainder==1)      
00069       {
00070         s[x++] = '=';
00071         s[x++] = '=';
00072       }
00073       else if(remainder==2)
00074         s[x++] = '=';
00075     }
00076 
00077     return s;
00078   }
00079 #else
00080   std::string Base64Encoder::encode(const std::string &str)
00081   {
00082     std::stringstream s;
00083     int remainder = (int)str.size() % 3;        
00084     int end = (int)str.size() - remainder;
00085     long buffer;
00086     // Every 3 bytes can be encoded in 4x 6-bit characters
00087     for(int i=0;i<end;i+=3)
00088     {
00089       buffer = ((0x0FF&str[i])<<16) | ((0x0FF&str[i+1])<<8) | (0x0FF&str[i+2]);
00090       s << encode6(buffer>>18)
00091         << encode6(buffer>>12)
00092         << encode6(buffer>>6)
00093         << encode6(buffer);
00094     }
00095     if(remainder>=1)
00096     {
00097       buffer = (0x0FF&str[end])<<16;
00098       if(remainder==2)
00099         buffer |= (0x0FF&str[end+1])<<8;
00100     }
00101     if(remainder)
00102     {
00103       s << encode6(buffer>>18)
00104         << encode6(buffer>>12);
00105       if(remainder==2)
00106         s << encode6(buffer>>6);
00107 
00108       if(remainder==1)      s << "==";
00109       else if(remainder==2) s << "=";
00110     }
00111     return s.str();
00112   }
00113 #endif
00114 
00115 }

Generated on Wed Feb 8 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1