1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "uuid.h"
17
18 #include <cerrno>
19 #include <ctime>
20 #include <regex>
21
22 #include "sys/time.h"
23
24 #define CONSTANT_ZERO 0
25 #define CONSTANT_ONE 1
26 #define CONSTANT_TWO 2
27 #define CONSTANT_THREE 3
28 #define CONSTANT_FOUR 4
29 #define CONSTANT_FIVE 5
30 #define CONSTANT_SIX 6
31 #define CONSTANT_SEVEN 7
32 #define CONSTANT_EIGHT 8
33 #define CONSTANT_NINE 9
34 #define CONSTANT_TEN 10
35 #define CONSTANT_ELEVEN 11
36 #define CONSTANT_TWELVE 12
37 #define CONSTANT_THIRTEEN 13
38 #define CONSTANT_FOURTEEN 14
39 #define CONSTANT_FIFTEEN 15
40 #define CONSTANT_SIXTEEN 16
41 #define CONSTANT_TWENTY 20
42 #define CONSTANT_TWENTY_FOUR 24
43 #define CONSTANT_THIRTY_TWO 32
44 #define CONSTANT_FOURTY 40
45 #define CONSTANT_FOURTY_EIGHT 48
46 #define CONSTANT_FIFTY_SIX 56
47
48 namespace OHOS {
49 namespace NetManagerStandard {
50
RandomUUID()51 UUID UUID::RandomUUID()
52 {
53 UUID random;
54
55 struct timeval tv;
56 struct timezone tz;
57 struct tm randomTime;
58 unsigned int randNum = 0;
59
60 rand_r(&randNum);
61 gettimeofday(&tv, &tz);
62 localtime_r(&tv.tv_sec, &randomTime);
63 random.uuid_[CONSTANT_FIFTEEN] = static_cast<uint8_t>(tv.tv_usec & 0x00000000000000FF);
64 random.uuid_[CONSTANT_FOURTEEN] = static_cast<uint8_t>((tv.tv_usec & 0x000000000000FF00) >> CONSTANT_EIGHT);
65 random.uuid_[CONSTANT_THIRTEEN] = static_cast<uint8_t>((tv.tv_usec & 0x0000000000FF0000) >> CONSTANT_SIXTEEN);
66 random.uuid_[CONSTANT_TWELVE] = static_cast<uint8_t>((tv.tv_usec & 0x00000000FF000000) >> CONSTANT_TWENTY_FOUR);
67 random.uuid_[CONSTANT_TEN] = static_cast<uint8_t>((tv.tv_usec & 0x000000FF00000000) >> CONSTANT_THIRTY_TWO);
68 random.uuid_[CONSTANT_NINE] = static_cast<uint8_t>((tv.tv_usec & 0x0000FF0000000000) >> CONSTANT_FOURTY);
69 random.uuid_[CONSTANT_EIGHT] = static_cast<uint8_t>((tv.tv_usec & 0x00FF000000000000) >> CONSTANT_FOURTY_EIGHT);
70 random.uuid_[CONSTANT_SEVEN] = static_cast<uint8_t>((tv.tv_usec & 0xFF00000000000000) >> CONSTANT_FIFTY_SIX);
71 random.uuid_[CONSTANT_SIX] = static_cast<uint8_t>((randomTime.tm_sec + static_cast<int>(randNum)) & 0xFF);
72 random.uuid_[CONSTANT_FIVE] = static_cast<uint8_t>((randomTime.tm_min + (randNum >> CONSTANT_EIGHT)) & 0xFF);
73 random.uuid_[CONSTANT_FOUR] = static_cast<uint8_t>((randomTime.tm_hour + (randNum >> CONSTANT_SIXTEEN)) & 0xFF);
74 random.uuid_[CONSTANT_THREE] = static_cast<uint8_t>((randomTime.tm_mday +
75 (randNum >> CONSTANT_TWENTY_FOUR)) & 0xFF);
76 random.uuid_[CONSTANT_TWO] = static_cast<uint8_t>(randomTime.tm_mon & 0xFF);
77 random.uuid_[CONSTANT_ONE] = static_cast<uint8_t>(randomTime.tm_year & 0xFF);
78 random.uuid_[CONSTANT_ZERO] = static_cast<uint8_t>((randomTime.tm_year & 0xFF00) >> CONSTANT_EIGHT);
79 return random;
80 }
81
ToString() const82 std::string UUID::ToString() const
83 {
84 std::string tmp = "";
85 std::string ret = "";
86 static const char *hex = "0123456789ABCDEF";
87
88 for (auto it = this->uuid_.begin(); it != this->uuid_.end(); it++) {
89 tmp.push_back(hex[(((*it) >> CONSTANT_FOUR) & 0xF)]);
90 tmp.push_back(hex[(*it) & 0xF]);
91 }
92 ret = tmp.substr(CONSTANT_ZERO, CONSTANT_EIGHT) + "-" +
93 tmp.substr(CONSTANT_EIGHT, CONSTANT_FOUR) + "-" +
94 tmp.substr(CONSTANT_TWELVE, CONSTANT_FOUR) + "-" +
95 tmp.substr(CONSTANT_SIXTEEN, CONSTANT_FOUR) + "-" +
96 tmp.substr(CONSTANT_TWENTY);
97
98 return ret;
99 }
100 }
101 }