00001 /*----------------------------------------------------------------------------- 00002 Name: Base64.h 00003 Project: xmlBlaster.org 00004 Copyright: 2001-2002 Randy Charles Morin randy@kbcafe.com 00005 www.kbcafe.com 00006 http://www.kbcafe.com/articles/HowTo.Base64.pdf 00007 Allowed to distribute under xmlBlasters LGPL in email 00008 from Randy Charles Morin <randy@kbcafe.com> from 2004-01-18 00009 Minor change by Marcel Ruff xmlBlaster@marcelruff.info 00010 http://www.xmlBlaster.org 00011 -----------------------------------------------------------------------------*/ 00012 00013 #ifndef _UTIL_BASE64_H 00014 #define _UTIL_BASE64_H 00015 00016 #include <util/xmlBlasterDef.h> 00017 # include <string> 00018 # include <vector> 00019 00020 namespace org { namespace xmlBlaster { namespace util { 00021 00027 class Dll_Export Base64 { 00028 static char Encode(unsigned char uc); 00029 static unsigned char Decode(char c); 00030 static bool IsBase64(char c); 00031 public: 00032 static std::string Encode( const std::vector<unsigned char> & vby); 00033 static std::vector<unsigned char> Decode( const std::string & str); 00034 }; 00035 00036 inline char Base64::Encode(unsigned char uc) 00037 { 00038 if (uc < 26) 00039 { 00040 return 'A'+uc; 00041 } 00042 if (uc < 52) 00043 { 00044 return 'a'+(uc-26); 00045 } 00046 if (uc < 62) 00047 { 00048 return '0'+(uc-52); 00049 } 00050 if (uc == 62) 00051 { 00052 return '+'; 00053 } 00054 return '/'; 00055 } 00056 inline unsigned char Base64::Decode(char c) 00057 { 00058 if (c >= 'A' && c <= 'Z') 00059 { 00060 return c - 'A'; 00061 } 00062 if (c >= 'a' && c <= 'z') 00063 { 00064 return c - 'a' + 26; 00065 } 00066 if (c >= '0' && c <= '9') 00067 { 00068 return c - '0' + 52; 00069 } 00070 if (c == '+') 00071 { 00072 return 62; 00073 } 00074 return 63; 00075 } 00076 inline bool Base64::IsBase64(char c) 00077 { 00078 if (c >= 'A' && c <= 'Z') 00079 { 00080 return true; 00081 } 00082 if (c >= 'a' && c <= 'z') 00083 { 00084 return true; 00085 } 00086 if (c >= '0' && c <= '9') 00087 { 00088 return true; 00089 } 00090 if (c == '+') 00091 { 00092 return true; 00093 } 00094 if (c == '/') 00095 { 00096 return true; 00097 } 00098 if (c == '=') 00099 { 00100 return true; 00101 } 00102 return false; 00103 } 00104 00105 }}} // namespace 00106 00107 #endif // _UTIL_BASE64_H 00108