Bytemaster's Boost Libraries
/Users/dlarimer/dev/libs/rpc/include/boost/rpc/base64.hpp
00001 #ifndef _BOOST_RPC_BASE64_HPP
00002 #define _BOOST_RPC_BASE64_HPP
00003 /* 
00004    base64.cpp and base64.h
00005 
00006    Copyright (C) 2004-2008 René Nyffenegger
00007 
00008    This source code is provided 'as-is', without any express or implied
00009    warranty. In no event will the author be held liable for any damages
00010    arising from the use of this software.
00011 
00012    Permission is granted to anyone to use this software for any purpose,
00013    including commercial applications, and to alter it and redistribute it
00014    freely, subject to the following restrictions:
00015 
00016    1. The origin of this source code must not be misrepresented; you must not
00017       claim that you wrote the original source code. If you use this source code
00018       in a product, an acknowledgment in the product documentation would be
00019       appreciated but is not required.
00020 
00021    2. Altered source versions must be plainly marked as such, and must not be
00022       misrepresented as being the original source code.
00023 
00024    3. This notice may not be removed or altered from any source distribution.
00025 
00026    René Nyffenegger rene.nyffenegger@adp-gmbh.ch
00027 
00028 */
00029 
00030 #include <iostream>
00031 
00032 namespace boost { namespace rpc {
00033 
00034 inline const std::string& base64_chars()
00035 {
00036     static const std::string m_base64_chars = 
00037                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00038                  "abcdefghijklmnopqrstuvwxyz"
00039                  "0123456789+/";
00040     return m_base64_chars;
00041 }
00042 
00043 static inline bool is_base64(unsigned char c) {
00044   return (isalnum(c) || (c == '_') || (c == '-'));
00045 }
00046 
00047 inline std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
00048 
00049   std::string ret;
00050   int i = 0;
00051   int j = 0;
00052   unsigned char char_array_3[3];
00053   unsigned char char_array_4[4];
00054 
00055   while (in_len--) {
00056     char_array_3[i++] = *(bytes_to_encode++);
00057     if (i == 3) {
00058       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00059       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00060       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00061       char_array_4[3] = char_array_3[2] & 0x3f;
00062 
00063       for(i = 0; (i <4) ; i++)
00064         ret += base64_chars()[char_array_4[i]];
00065       i = 0;
00066     }
00067   }
00068 
00069   if (i)
00070   {
00071     for(j = i; j < 3; j++)
00072       char_array_3[j] = '\0';
00073 
00074     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00075     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00076     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00077     char_array_4[3] = char_array_3[2] & 0x3f;
00078 
00079     for (j = 0; (j < i + 1); j++)
00080       ret += base64_chars()[char_array_4[j]];
00081 
00082     while((i++ < 3))
00083       ret += '=';
00084 
00085   }
00086 
00087   return ret;
00088 
00089 }
00090 
00091 
00092 inline std::string base64_decode(std::string const& encoded_string) {
00093 
00094 
00095   int in_len = encoded_string.size();
00096   int i = 0;
00097   int j = 0;
00098   int in_ = 0;
00099   unsigned char char_array_4[4], char_array_3[3];
00100   std::string ret;
00101 
00102   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
00103     char_array_4[i++] = encoded_string[in_]; in_++;
00104     if (i ==4) {
00105       for (i = 0; i <4; i++)
00106         char_array_4[i] = base64_chars().find(char_array_4[i]);
00107 
00108       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00109       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00110       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00111 
00112       for (i = 0; (i < 3); i++)
00113         ret += char_array_3[i];
00114       i = 0;
00115     }
00116   }
00117 
00118   if (i) {
00119     for (j = i; j <4; j++)
00120       char_array_4[j] = 0;
00121 
00122     for (j = 0; j <4; j++)
00123       char_array_4[j] = base64_chars().find(char_array_4[j]);
00124 
00125     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00126     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00127     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00128 
00129     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
00130   }
00131 
00132   return ret;
00133 }
00134 
00135 
00136 } } // namespace boost::rpc
00137 #endif // _BOOST_RPC_BASE64_HPP
 All Classes Namespaces Files Functions Variables Typedefs Defines