1 /*-----------------------------------------------------------------------------
  2 Name:      Base64.h
  3 Project:   xmlBlaster.org
  4 Copyright: 2001-2002 Randy Charles Morin randy@kbcafe.com
  5            www.kbcafe.com
  6            http://www.kbcafe.com/articles/HowTo.Base64.pdf
  7            Allowed to distribute under xmlBlasters LGPL in email
  8            from Randy Charles Morin <randy@kbcafe.com> from 2004-01-18
  9 Minor change by Marcel Ruff xmlBlaster@marcelruff.info
 10 http://www.xmlBlaster.org
 11 -----------------------------------------------------------------------------*/
 12 
 13 #ifndef _UTIL_BASE64_H
 14 #define _UTIL_BASE64_H
 15 
 16 #include <util/xmlBlasterDef.h>
 17 # include <string>
 18 # include <vector>
 19 
 20 namespace org { namespace xmlBlaster { namespace util {
 21 
 22 /**
 23  * Base64 encoding/decoding. 
 24  * @author Copyright 2001-2002 Randy Charles Morin randy@kbcafe.com
 25  * @see http://www.kbcafe.com/articles/HowTo.Base64.pdf
 26  */
 27 class Dll_Export Base64 {
 28    static char Encode(unsigned char uc);
 29    static unsigned char Decode(char c);
 30    static bool IsBase64(char c);
 31  public:
 32    static std::string Encode( const std::vector<unsigned char> & vby);
 33    static std::vector<unsigned char> Decode( const std::string & str);
 34 };
 35 
 36 inline char Base64::Encode(unsigned char uc)
 37 {
 38    if (uc < 26)
 39    {
 40       return 'A'+uc;
 41    }
 42    if (uc < 52)
 43    {
 44       return 'a'+(uc-26);
 45    }
 46    if (uc < 62)
 47    {
 48       return '0'+(uc-52);
 49    }
 50    if (uc == 62)
 51    {
 52       return '+';
 53    }
 54    return '/';
 55 }
 56 inline unsigned char Base64::Decode(char c)
 57 {
 58    if (c >= 'A' && c <= 'Z')
 59    {
 60       return c - 'A';
 61    }
 62    if (c >= 'a' && c <= 'z')
 63    {
 64       return c - 'a' + 26;
 65    }
 66    if (c >= '0' && c <= '9')
 67    {
 68       return c - '0' + 52;
 69    }
 70    if (c == '+')
 71    {
 72       return 62;
 73    }
 74    return 63;
 75 }
 76 inline bool Base64::IsBase64(char c)
 77 {
 78    if (c >= 'A' && c <= 'Z')
 79    {
 80       return true;
 81    }
 82    if (c >= 'a' && c <= 'z')
 83    {
 84       return true;
 85    }
 86    if (c >= '0' && c <= '9')
 87    {
 88       return true;
 89    }
 90    if (c == '+')
 91    {
 92       return true;
 93    }
 94    if (c == '/')
 95    {
 96       return true;
 97    }
 98    if (c == '=')
 99    {
100       return true;
101    }
102    return false;
103 }
104 
105 }}} // namespace
106 
107 #endif // _UTIL_BASE64_H


syntax highlighted by Code2HTML, v. 0.9.1