• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include <algorithm>
18 #include <climits>
19 #include <iostream>
20 #include <iomanip>
21 #include <sys/time.h>
22 
23 namespace OHOS {
24 namespace MMI {
Uuid()25 Uuid::Uuid()
26 {
27     struct timeval tv;
28     struct timezone tz;
29     struct tm randomTime;
30     uint32_t randNum = 0;
31 
32     rand_r(&randNum);
33     gettimeofday(&tv, &tz);
34     localtime_r(&tv.tv_sec, &randomTime);
35 
36     unsigned long int tvUsec = 0;
37     tvUsec = (unsigned long int)tv.tv_usec;
38 
39     uuid_[UUID_NODE_SIXTH_BYTE] =
40         static_cast<uint8_t>(tvUsec & 0x00000000000000FF);
41     uuid_[UUID_NODE_FIFTH_BYTE] =
42         static_cast<uint8_t>((tvUsec & 0x000000000000FF00) >> BASE_BIT_OPT_SIZE);
43     uuid_[UUID_NODE_FOURTH_BYTE] =
44         static_cast<uint8_t>((tvUsec & 0x0000000000FF0000) >> BIT_OPT_TWO_BYTE * BASE_BIT_OPT_SIZE);
45     uuid_[UUID_NODE_THIRD_BYTE] =
46         static_cast<uint8_t>((tvUsec & 0x00000000FF000000) >> BIT_OPT_THREE_BYTE * BASE_BIT_OPT_SIZE);
47     uuid_[UUID_NODE_FIRST_BYTE] =
48         static_cast<uint8_t>((tvUsec & 0x000000FF00000000) >> BIT_OPT_FOUR_BYTE * BASE_BIT_OPT_SIZE);
49     uuid_[UUID_CLOCK_SEQ] =
50         static_cast<uint8_t>((tvUsec & 0x0000FF0000000000) >> BIT_OPT_FIVE_BYTE * BASE_BIT_OPT_SIZE);
51     uuid_[UUID_VARIANT] =
52         static_cast<uint8_t>((tvUsec & 0x00FF000000000000) >> BIT_OPT_SIX_BYTE * BASE_BIT_OPT_SIZE);
53     uuid_[UUID_TIME_HIGH] =
54         static_cast<uint8_t>((tvUsec & 0xFF00000000000000) >> BIT_OPT_SEVEN_BYTE * BASE_BIT_OPT_SIZE);
55     // 4 - 6
56     uuid_[UUID_VERSION] =
57         static_cast<uint8_t>((static_cast<uint32_t>(randomTime.tm_sec) + randNum) & 0xFF);
58     uuid_[UUID_TIME_MID_SECEND_BYTE] =
59         static_cast<uint8_t>((static_cast<uint32_t>(randomTime.tm_min) + (randNum >> BASE_BIT_OPT_SIZE)) & 0xFF);
60     uuid_[UUID_TIME_MID_FIRST_BYTE] = static_cast<uint8_t>((static_cast<uint32_t>(randomTime.tm_hour) +
61                                       (randNum >> BIT_OPT_TWO_BYTE * BASE_BIT_OPT_SIZE)) & 0xFF);
62     // 0 - 3
63     uuid_[UUID_TIME_LOW_FOURTH_BYTE] = static_cast<uint8_t>((static_cast<uint32_t>(randomTime.tm_mday) +
64                                        (randNum >> BIT_OPT_THREE_BYTE * BASE_BIT_OPT_SIZE)) & 0xFF);
65     uuid_[UUID_TIME_LOW_THIRD_BYTE] =
66         static_cast<uint8_t>(static_cast<uint32_t>(randomTime.tm_mon) & 0xFF);
67     uuid_[UUID_TIME_LOW_SECEND_BYTE] =
68         static_cast<uint8_t>(static_cast<uint32_t>(randomTime.tm_year) & 0xFF);
69     uuid_[UUID_TIME_LOW_FIRST_BYTE] =
70         static_cast<uint8_t>((static_cast<uint32_t>(randomTime.tm_year) & 0xFF00) >> BASE_BIT_OPT_SIZE);
71 }
72 
ConvertToHex(uint8_t c)73 char ConvertToHex(uint8_t c)
74 {
75     if (c < 0xa) {
76         return static_cast<char>(c + '0');
77     } else if (c >= 0xa && c <= 0xf) {
78         return static_cast<char>(c - 0xa + 'a');
79     }
80 
81     return '0';
82 }
83 
ConvertToStdString(std::string & s) const84 void Uuid::ConvertToStdString(std::string& s) const
85 {
86     constexpr int32_t uuidBufMaxSize = 37;
87     char uuidBuf[uuidBufMaxSize + 1] = {0};
88     int32_t writePos = 0;
89     for (size_t i = 0; i < UUID128_BYTES_TYPE; i++) {
90         const uint8_t c = uuid_[i];
91         const uint8_t low4Bit = (c & 0xf);
92         const uint8_t high4Bit = ((c >> 4) & 0xf);
93         if (writePos <= uuidBufMaxSize) {
94             uuidBuf[writePos++] = ConvertToHex(low4Bit);
95             uuidBuf[writePos++] = ConvertToHex(high4Bit);
96             if (i == 3 || i == 5 || i == 7 || i == 9) { // 3 5 7 9 uuid 按标准格式(8-4-4-4-12)分隔符
97                 uuidBuf[writePos++] = '-';
98             }
99         }
100     }
101     uuidBuf[uuidBufMaxSize - 1] = '\0';
102     s = uuidBuf;
103 }
104 } // namespace MMI
105 } // namespace OHOS
106