Free Electron
binary.h
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  : m_data{}, m_unownedData(data_), m_unownedSize(size_) {}
24  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  bool owned() const { return !m_unownedData; }
31  std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; }
32  const unsigned char *data() const {
33  return owned() ? &m_data[0] : m_unownedData;
34  }
35 
36  void swap(std::vector<unsigned char> &rhs) {
37  if (m_unownedData) {
38  m_data.swap(rhs);
39  rhs.clear();
40  rhs.resize(m_unownedSize);
41  std::copy(m_unownedData, m_unownedData + m_unownedSize, rhs.begin());
42  m_unownedData = nullptr;
43  m_unownedSize = 0;
44  } else {
45  m_data.swap(rhs);
46  }
47  }
48 
49  bool operator==(const Binary &rhs) const {
50  const std::size_t s = size();
51  if (s != rhs.size())
52  return false;
53  const unsigned char *d1 = data();
54  const unsigned char *d2 = rhs.data();
55  for (std::size_t i = 0; i < s; i++) {
56  if (*d1++ != *d2++)
57  return false;
58  }
59  return true;
60  }
61 
62  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
Definition: anchor.h:12