| 1 | #include <bytes/bytes.h> |
| 2 | |
| 3 | #include <iomanip> |
| 4 | #include <iostream> |
| 5 | #include <sstream> |
| 6 | #include <stdexcept> |
| 7 | |
| 8 | namespace mlspp::bytes_ns { |
| 9 | |
| 10 | bool |
| 11 | bytes::operator==(const bytes& other) const |
| 12 | { |
| 13 | return *this == other._data; |
| 14 | } |
| 15 | |
| 16 | bool |
| 17 | bytes::operator!=(const bytes& other) const |
| 18 | { |
| 19 | return !(*this == other._data); |
| 20 | } |
| 21 | |
| 22 | bool |
| 23 | bytes::operator==(const std::vector<uint8_t>& other) const |
| 24 | { |
| 25 | const size_t size = other.size(); |
| 26 | if (_data.size() != size) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | unsigned char diff = 0; |
| 31 | for (size_t i = 0; i < size; ++i) { |
| 32 | // Not sure why the linter thinks `diff` is signed |
| 33 | // NOLINTNEXTLINE(hicpp-signed-bitwise) |
| 34 | diff |= (_data.at(i) ^ other.at(i)); |
| 35 | } |
| 36 | return (diff == 0); |
| 37 | } |
| 38 | |
| 39 | bool |
| 40 | bytes::operator!=(const std::vector<uint8_t>& other) const |
| 41 | { |
| 42 | return !(*this == other); |
| 43 | } |
| 44 | |
| 45 | bytes& |
| 46 | bytes::operator+=(const bytes& other) |
| 47 | { |
| 48 | // Not sure what the default argument is here |
| 49 | // NOLINTNEXTLINE(fuchsia-default-arguments) |
| 50 | _data.insert(end(), other.begin(), other.end()); |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | bytes |
| 55 | bytes::operator+(const bytes& rhs) const |
| 56 | { |
| 57 | bytes out = *this; |
| 58 | out += rhs; |
| 59 | return out; |
| 60 | } |
| 61 | |
| 62 | bool |
| 63 | bytes::operator<(const bytes& rhs) const |
| 64 | { |
| 65 | return _data < rhs._data; |
| 66 | } |
| 67 | |
| 68 | bytes |
| 69 | bytes::operator^(const bytes& rhs) const |
| 70 | { |
| 71 | if (size() != rhs.size()) { |
| 72 | throw std::invalid_argument("XOR with unequal size"); |
| 73 | } |
| 74 | |
| 75 | bytes out = *this; |
| 76 | for (size_t i = 0; i < size(); ++i) { |
| 77 | out.at(i) ^= rhs.at(i); |
| 78 | } |
| 79 | return out; |
| 80 | } |
| 81 | |
| 82 | std::string |
| 83 | to_ascii(const bytes& data) |
| 84 | { |
| 85 | return { data.begin(), data.end() }; |
| 86 | } |
| 87 | |
| 88 | bytes |
| 89 | from_ascii(const std::string& ascii) |
| 90 | { |
| 91 | return std::vector<uint8_t>(ascii.begin(), ascii.end()); |
| 92 | } |
| 93 | |
| 94 | std::string |
| 95 | to_hex(const bytes& data) |
| 96 | { |
| 97 | std::stringstream hex(std::ios_base::out); |
| 98 | hex.flags(std::ios::hex); |
| 99 | for (const auto& byte : data) { |
| 100 | hex << std::setw(2) << std::setfill('0') << int(byte); |
| 101 | } |
| 102 | return hex.str(); |
| 103 | } |
| 104 | |
| 105 | bytes |
| 106 | from_hex(const std::string& hex) |
| 107 | { |
| 108 | if (hex.length() % 2 == 1) { |
| 109 | throw std::invalid_argument("Odd-length hex string"); |
| 110 | } |
| 111 | |
| 112 | auto len = hex.length() / 2; |
| 113 | auto out = bytes(len); |
| 114 | for (size_t i = 0; i < len; i += 1) { |
| 115 | const std::string byte = hex.substr(2 * i, 2); |
| 116 | out.at(i) = static_cast<uint8_t>(strtol(byte.c_str(), nullptr, 16)); |
| 117 | } |
| 118 | |
| 119 | return out; |
| 120 | } |
| 121 | |
| 122 | std::ostream& |
| 123 | operator<<(std::ostream& out, const bytes& data) |
| 124 | { |
| 125 | // Adjust this threshold to make output more compact |
| 126 | const size_t threshold = 0xffff; |
| 127 | if (data.size() < threshold) { |
| 128 | return out << to_hex(data); |
| 129 | } |
| 130 | |
| 131 | return out << to_hex(data.slice(0, threshold)) << "..."; |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | operator==(const std::vector<uint8_t>& lhs, const bytes_ns::bytes& rhs) |
| 136 | { |
| 137 | return rhs == lhs; |
| 138 | } |
| 139 | |
| 140 | bool |
| 141 | operator!=(const std::vector<uint8_t>& lhs, const bytes_ns::bytes& rhs) |
| 142 | { |
| 143 | return rhs != lhs; |
| 144 | } |
| 145 | |
| 146 | } // namespace mlspp::bytes_ns |