00001 #ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 00002 #define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 00003 00004 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 00005 #pragma once 00006 #endif 00007 00008 #include <string> 00009 #include <vector> 00010 00011 namespace YAML_PM 00012 { 00013 class Node; 00014 00015 std::string EncodeBase64(const unsigned char *data, std::size_t size); 00016 std::vector<unsigned char> DecodeBase64(const std::string& input); 00017 00018 class Binary { 00019 public: 00020 Binary(): m_unownedData(0), m_unownedSize(0) {} 00021 Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {} 00022 00023 bool owned() const { return !m_unownedData; } 00024 std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; } 00025 const unsigned char *data() const { return owned() ? &m_data[0] : m_unownedData; } 00026 00027 void swap(std::vector<unsigned char>& rhs) { 00028 if(m_unownedData) { 00029 m_data.swap(rhs); 00030 rhs.clear(); 00031 rhs.resize(m_unownedSize); 00032 std::copy(m_unownedData, m_unownedData + m_unownedSize, &rhs[0]); 00033 m_unownedData = 0; 00034 m_unownedSize = 0; 00035 } else { 00036 m_data.swap(rhs); 00037 } 00038 } 00039 00040 bool operator == (const Binary& rhs) const { 00041 const std::size_t s = size(); 00042 if(s != rhs.size()) 00043 return false; 00044 const unsigned char *d1 = data(); 00045 const unsigned char *d2 = rhs.data(); 00046 for(std::size_t i=0;i<s;i++) { 00047 if(*d1++ != *d2++) 00048 return false; 00049 } 00050 return true; 00051 } 00052 00053 bool operator != (const Binary& rhs) const { 00054 return !(*this == rhs); 00055 } 00056 00057 private: 00058 std::vector<unsigned char> m_data; 00059 const unsigned char *m_unownedData; 00060 std::size_t m_unownedSize; 00061 }; 00062 00063 void operator >> (const Node& node, Binary& binary); 00064 } 00065 00066 #endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66