1#ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2#define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3
4#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5#pragma once
6#endif
7
8#include <string>
9#include <vector>
10
11namespace YAML
12{
13    class Node;
14
15    std::string EncodeBase64(const unsigned char *data, std::size_t size);
16    std::vector<unsigned char> DecodeBase64(const std::string& input);
17
18    class Binary {
19    public:
20        Binary(): m_unownedData(0), m_unownedSize(0) {}
21        Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {}
22
23        bool owned() const { return !m_unownedData; }
24        std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; }
25        const unsigned char *data() const { return owned() ? &m_data[0] : m_unownedData; }
26
27        void swap(std::vector<unsigned char>& rhs) {
28            if(m_unownedData) {
29                m_data.swap(rhs);
30                rhs.clear();
31                rhs.resize(m_unownedSize);
32                std::copy(m_unownedData, m_unownedData + m_unownedSize, &rhs[0]);
33                m_unownedData = 0;
34                m_unownedSize = 0;
35            } else {
36                m_data.swap(rhs);
37            }
38        }
39
40        bool operator == (const Binary& rhs) const {
41            const std::size_t s = size();
42            if(s != rhs.size())
43                return false;
44            const unsigned char *d1 = data();
45            const unsigned char *d2 = rhs.data();
46            for(std::size_t i=0;i<s;i++) {
47                if(*d1++ != *d2++)
48                    return false;
49            }
50            return true;
51        }
52
53        bool operator != (const Binary& rhs) const {
54            return !(*this == rhs);
55        }
56
57    private:
58        std::vector<unsigned char> m_data;
59        const unsigned char *m_unownedData;
60        std::size_t m_unownedSize;
61    };
62
63    void operator >> (const Node& node, Binary& binary);
64}
65
66#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
67