1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
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 "async/uuid_base.h"
18 #include <memory.h>
19 #include <atomic>
20 #include <random>
21
22 namespace mindspore {
23 namespace uuids {
24 constexpr int DASH_POS0 = 4;
25 constexpr int DASH_POS1 = 6;
26 constexpr int DASH_POS2 = 8;
27 constexpr int DASH_POS3 = 10;
28 constexpr int SHIFT_BIT = 4;
29
BeginAddress() const30 const uint8_t *uuid::BeginAddress() const { return uuidData; }
31
EndAddress() const32 const uint8_t *uuid::EndAddress() const { return uuidData + UUID_SIZE; }
33
Size()34 std::size_t uuid::Size() { return UUID_SIZE; }
35
ToBytes(const uuid & u)36 std::string uuid::ToBytes(const uuid &u) {
37 MINDRT_ASSERT(sizeof(u) == UUID_SIZE);
38 return std::string(reinterpret_cast<const char *>(u.uuidData), sizeof(u.uuidData));
39 }
40
GetValue(char c)41 Option<unsigned char> uuid::GetValue(char c) {
42 static char const digitsBegin[] = "0123456789abcdefABCDEF";
43 static const size_t digitsLen = (sizeof(digitsBegin) / sizeof(char)) - 1;
44 static const char *const digitsEnd = digitsBegin + digitsLen;
45 static unsigned char const values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15};
46 size_t pos = std::find(digitsBegin, digitsEnd, c) - digitsBegin;
47 if (pos >= digitsLen) {
48 MS_LOG(ERROR) << "invalid char";
49 return Option<unsigned char>(MindrtNone());
50 }
51 return Option<unsigned char>(values[pos]);
52 }
53
FromString(const std::string & s)54 Option<uuid> uuid::FromString(const std::string &s) {
55 auto sBegin = s.begin();
56 if (sBegin == s.end()) {
57 return Option<uuid>(MindrtNone());
58 }
59 auto c = *sBegin;
60 bool hasOpenBrace = (c == '{');
61 bool hasDashes = false;
62 if (hasOpenBrace) {
63 ++sBegin;
64 }
65 uuid u;
66 for (size_t i = 0; sBegin != s.end(); ++i) {
67 c = *(sBegin++);
68 if ((i == DASH_POS0) && (c == '-')) {
69 hasDashes = true;
70 c = *(sBegin++);
71 } else if ((i == DASH_POS1 || i == DASH_POS2 || i == DASH_POS3) && (hasDashes == true)) {
72 if (c == '-' && sBegin != s.end()) {
73 c = *(sBegin++);
74 } else {
75 MS_LOG(ERROR) << "str invalid";
76 return Option<uuid>(MindrtNone());
77 }
78 }
79 Option<unsigned char> oc1 = GetValue(c);
80 if (oc1.IsNone()) {
81 return Option<uuid>(MindrtNone());
82 }
83 u.uuidData[i] = oc1.Get();
84 if (sBegin != s.end()) {
85 c = *(sBegin++);
86 }
87 u.uuidData[i] <<= SHIFT_BIT;
88 Option<unsigned char> oc2 = GetValue(c);
89 if (oc2.IsNone()) {
90 return Option<uuid>(MindrtNone());
91 }
92 u.uuidData[i] |= oc2.Get();
93 }
94 if ((hasOpenBrace && (c != '}')) || (sBegin != s.end())) {
95 MS_LOG(ERROR) << "No } end or leng invalid";
96 return Option<uuid>(MindrtNone());
97 }
98 return Option<uuid>(u);
99 }
100
101 // To check whether uuid looks like 0000000-000-000-000-000000000000000
IsNilUUID() const102 bool uuid::IsNilUUID() const {
103 for (std::size_t i = 0; i < Size(); i++) {
104 if (uuidData[i]) {
105 return false;
106 }
107 }
108 return true;
109 }
110
Get() const111 const uint8_t *uuid::Get() const { return uuidData; }
112
BeginAddress()113 uint8_t *uuid::BeginAddress() { return uuidData; }
114
EndAddress()115 uint8_t *uuid::EndAddress() { return uuidData + UUID_SIZE; }
116 } // namespace uuids
117 } // namespace mindspore
118