Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 1 | #include "aos/uuid.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 2 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 7 | #include <array> |
| 8 | #include <random> |
| 9 | #include <string_view> |
| 10 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 11 | #include "gflags/gflags.h" |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 12 | #include "glog/logging.h" |
| 13 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 14 | DEFINE_string(boot_uuid, "", |
| 15 | "If set, override the boot UUID to have this value instead."); |
| 16 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 17 | namespace aos { |
| 18 | namespace { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 19 | void ToHex(const uint8_t *val, char *result, size_t count) { |
| 20 | while (count > 0) { |
| 21 | int upper = ((*val) >> 4) & 0xf; |
| 22 | if (upper < 10) { |
| 23 | result[0] = upper + '0'; |
| 24 | } else { |
| 25 | result[0] = upper - 10 + 'a'; |
| 26 | } |
| 27 | |
| 28 | int lower = (*val) & 0xf; |
| 29 | if (lower < 10) { |
| 30 | result[1] = lower + '0'; |
| 31 | } else { |
| 32 | result[1] = lower - 10 + 'a'; |
| 33 | } |
| 34 | |
| 35 | ++val; |
| 36 | result += 2; |
| 37 | --count; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 40 | |
| 41 | void FromHex(const char *val, uint8_t *result, size_t count) { |
| 42 | while (count > 0) { |
| 43 | CHECK((val[0] >= '0' && val[0] <= '9') || (val[0] >= 'a' && val[0] <= 'f')) |
| 44 | << ": Invalid hex '" << val[0] << "'"; |
| 45 | CHECK((val[1] >= '0' && val[1] <= '9') || (val[1] >= 'a' && val[1] <= 'f')) |
| 46 | << ": Invalid hex '" << val[1] << "'"; |
| 47 | |
| 48 | uint8_t converted = 0; |
| 49 | if (val[0] < 'a') { |
| 50 | converted |= static_cast<uint8_t>(val[0] - '0') << 4; |
| 51 | } else { |
| 52 | converted |= (static_cast<uint8_t>(val[0] - 'a') + 0xa) << 4; |
| 53 | } |
| 54 | if (val[1] < 'a') { |
| 55 | converted |= static_cast<uint8_t>(val[1] - '0'); |
| 56 | } else { |
| 57 | converted |= (static_cast<uint8_t>(val[1] - 'a') + 0xa); |
| 58 | } |
| 59 | *result = converted; |
| 60 | |
| 61 | val += 2; |
| 62 | ++result; |
| 63 | --count; |
| 64 | } |
| 65 | } |
| 66 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 67 | } // namespace |
| 68 | |
| 69 | UUID UUID::Random() { |
| 70 | std::random_device rd; |
| 71 | std::mt19937 gen(rd()); |
| 72 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 73 | std::uniform_int_distribution<> dis(0, 255); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 74 | std::uniform_int_distribution<> dis2(8, 11); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 75 | UUID result; |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 76 | for (size_t i = 0; i < kDataSize; ++i) { |
| 77 | result.data_[i] = dis(gen); |
| 78 | } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 79 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 80 | // Mark the reserved bits in the data that this is a uuid4, a random UUID. |
| 81 | result.data_[6] = (result.data_[6] & 0x0f) | 0x40; |
| 82 | result.data_[8] = (result.data_[6] & 0x3f) | 0x80; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 87 | std::string UUID::ToString() const { |
| 88 | std::string out; |
| 89 | out.resize(UUID::kStringSize); |
| 90 | CopyTo(out.data()); |
| 91 | return out; |
| 92 | } |
| 93 | |
| 94 | std::ostream &operator<<(std::ostream &os, const UUID &uuid) { |
| 95 | return os << uuid.ToString(); |
| 96 | } |
| 97 | |
| 98 | flatbuffers::Offset<flatbuffers::String> UUID::PackString( |
| 99 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 100 | std::array<char, kStringSize> data; |
| 101 | CopyTo(data.data()); |
| 102 | |
| 103 | return fbb->CreateString(data.data(), data.size()); |
| 104 | } |
| 105 | |
| 106 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> UUID::PackVector( |
| 107 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 108 | return fbb->CreateVector(data_.data(), data_.size()); |
| 109 | } |
| 110 | |
| 111 | void UUID::CopyTo(char *result) const { |
| 112 | ToHex(&data_[0], result, 4); |
| 113 | result[8] = '-'; |
| 114 | ToHex(&data_[4], result + 9, 2); |
| 115 | result[13] = '-'; |
| 116 | ToHex(&data_[6], result + 14, 2); |
| 117 | result[18] = '-'; |
| 118 | ToHex(&data_[8], result + 19, 2); |
| 119 | result[23] = '-'; |
| 120 | ToHex(&data_[10], result + 24, 6); |
| 121 | } |
| 122 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 123 | UUID UUID::FromString(const flatbuffers::String *str) { |
| 124 | return FromString(str->string_view()); |
| 125 | } |
| 126 | |
| 127 | UUID UUID::FromVector(const flatbuffers::Vector<uint8_t> *data) { |
| 128 | CHECK(data != nullptr); |
| 129 | CHECK_EQ(data->size(), kDataSize); |
| 130 | |
| 131 | UUID result; |
| 132 | std::memcpy(result.data_.data(), data->Data(), kDataSize); |
| 133 | return result; |
| 134 | } |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 135 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame^] | 136 | UUID UUID::FromSpan(absl::Span<const uint8_t> data) { |
| 137 | CHECK_EQ(data.size(), kDataSize); |
| 138 | |
| 139 | UUID result; |
| 140 | std::copy(data.begin(), data.end(), result.data_.begin()); |
| 141 | return result; |
| 142 | } |
| 143 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 144 | UUID UUID::FromString(std::string_view str) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 145 | CHECK_EQ(str.size(), kStringSize); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 146 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 147 | UUID result; |
| 148 | FromHex(str.data(), result.data_.data(), 4); |
| 149 | CHECK(str.data()[8] == '-' && str.data()[13] == '-' && |
| 150 | str.data()[18] == '-' && str.data()[23] == '-') |
| 151 | << ": Invalid uuid."; |
| 152 | FromHex(str.data() + 9, result.data_.data() + 4, 2); |
| 153 | FromHex(str.data() + 14, result.data_.data() + 6, 2); |
| 154 | FromHex(str.data() + 19, result.data_.data() + 8, 2); |
| 155 | FromHex(str.data() + 24, result.data_.data() + 10, 6); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 156 | return result; |
| 157 | } |
| 158 | |
| 159 | UUID UUID::BootUUID() { |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 160 | if (!FLAGS_boot_uuid.empty()) { |
| 161 | return UUID::FromString(FLAGS_boot_uuid); |
| 162 | } |
| 163 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 164 | int fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY); |
| 165 | PCHECK(fd != -1); |
| 166 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 167 | std::array<char, kStringSize> data; |
| 168 | CHECK_EQ(static_cast<ssize_t>(kStringSize), |
| 169 | read(fd, data.begin(), kStringSize)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 170 | close(fd); |
| 171 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 172 | return UUID::FromString(std::string_view(data.data(), data.size())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 175 | } // namespace aos |