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