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