File indexing completed on 2025-05-12 09:08:09
0001 #ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0002 #define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0003
0004 #if defined(_MSC_VER) || \
0005 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
0006 (__GNUC__ >= 4))
0007 #pragma once
0008 #endif
0009
0010 #include <string>
0011 #include <vector>
0012
0013 #include "ATOOLS/YAML/yaml-cpp/dll.h"
0014
0015 namespace SHERPA_YAML {
0016 YAML_CPP_API std::string EncodeBase64(const unsigned char *data,
0017 std::size_t size);
0018 YAML_CPP_API std::vector<unsigned char> DecodeBase64(const std::string &input);
0019
0020 class YAML_CPP_API Binary {
0021 public:
0022 Binary(const unsigned char *data_, std::size_t size_)
0023 : m_data{}, m_unownedData(data_), m_unownedSize(size_) {}
0024 Binary() : Binary(nullptr, 0) {}
0025 Binary(const Binary &) = default;
0026 Binary(Binary &&) = default;
0027 Binary &operator=(const Binary &) = default;
0028 Binary &operator=(Binary &&) = default;
0029
0030 bool owned() const { return !m_unownedData; }
0031 std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; }
0032 const unsigned char *data() const {
0033 return owned() ? &m_data[0] : m_unownedData;
0034 }
0035
0036 void swap(std::vector<unsigned char> &rhs) {
0037 if (m_unownedData) {
0038 m_data.swap(rhs);
0039 rhs.clear();
0040 rhs.resize(m_unownedSize);
0041 std::copy(m_unownedData, m_unownedData + m_unownedSize, rhs.begin());
0042 m_unownedData = nullptr;
0043 m_unownedSize = 0;
0044 } else {
0045 m_data.swap(rhs);
0046 }
0047 }
0048
0049 bool operator==(const Binary &rhs) const {
0050 const std::size_t s = size();
0051 if (s != rhs.size())
0052 return false;
0053 const unsigned char *d1 = data();
0054 const unsigned char *d2 = rhs.data();
0055 for (std::size_t i = 0; i < s; i++) {
0056 if (*d1++ != *d2++)
0057 return false;
0058 }
0059 return true;
0060 }
0061
0062 bool operator!=(const Binary &rhs) const { return !(*this == rhs); }
0063
0064 private:
0065 std::vector<unsigned char> m_data;
0066 const unsigned char *m_unownedData;
0067 std::size_t m_unownedSize;
0068 };
0069 }
0070
0071 #endif