1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/uuid.h"
18
19 #include <random>
20
21 #include "perfetto/base/time.h"
22
23 namespace perfetto {
24 namespace base {
25 namespace {
26
27 constexpr char kHexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
28 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
29 } // namespace
30
31 // See https://www.ietf.org/rfc/rfc4122.txt
Uuidv4()32 Uuid Uuidv4() {
33 static std::minstd_rand rng(static_cast<uint32_t>(GetBootTimeNs().count()));
34 Uuid uuid;
35 auto& data = *uuid.data();
36
37 for (size_t i = 0; i < 16; ++i)
38 data[i] = static_cast<uint8_t>(rng());
39
40 // version:
41 data[6] = (data[6] & 0x0f) | 0x40;
42 // clock_seq_hi_and_reserved:
43 data[8] = (data[8] & 0x3f) | 0x80;
44
45 return uuid;
46 }
47
Uuid()48 Uuid::Uuid() {}
49
Uuid(const std::string & s)50 Uuid::Uuid(const std::string& s) {
51 PERFETTO_CHECK(s.size() == data_.size());
52 memcpy(data_.data(), s.data(), s.size());
53 }
54
Uuid(int64_t lsb,int64_t msb)55 Uuid::Uuid(int64_t lsb, int64_t msb) {
56 set_lsb_msb(lsb, msb);
57 }
58
ToString() const59 std::string Uuid::ToString() const {
60 return std::string(reinterpret_cast<const char*>(data_.data()), data_.size());
61 }
62
ToPrettyString() const63 std::string Uuid::ToPrettyString() const {
64 std::string s(data_.size() * 2 + 4, '-');
65 // Format is 123e4567-e89b-12d3-a456-426655443322.
66 size_t j = 0;
67 for (size_t i = 0; i < data_.size(); ++i) {
68 if (i == 4 || i == 6 || i == 8 || i == 10)
69 j++;
70 s[2 * i + j] = kHexmap[(data_[data_.size() - i - 1] & 0xf0) >> 4];
71 s[2 * i + 1 + j] = kHexmap[(data_[data_.size() - i - 1] & 0x0f)];
72 }
73 return s;
74 }
75
76 } // namespace base
77 } // namespace perfetto
78