1 /*
2 * Copyright (c) 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 "commonlibrary/ets_utils/js_util_module/util/js_uuid.h"
17
18 #include "securec.h"
19 #include "utils/log.h"
20
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23
24 namespace OHOS::Util {
25 static thread_local std::queue<UUID> g_uuidCache;
26
CharToHex(char in)27 unsigned char CharToHex(char in)
28 {
29 unsigned char res = 0; // 0: initialization
30 switch (in) {
31 case '0': res = HEX_ZERO_FLG; break;
32 case '1': res = HEX_ONE_FLG; break;
33 case '2': res = HEX_TWO_FLG; break;
34 case '3': res = HEX_THREE_FLG; break;
35 case '4': res = HEX_FOUR_FLG; break;
36 case '5': res = HEX_FIVE_FLG; break;
37 case '6': res = HEX_SIX_FLG; break;
38 case '7': res = HEX_SEVEN_FLG; break;
39 case '8': res = HEX_EIGHT_FLG; break;
40 case '9': res = HEX_NINE_FLG; break;
41 case 'a': res = HEX_TEN_FLG; break;
42 case 'b': res = HEX_ELEVEN_FLG; break;
43 case 'c': res = HEX_TWELVE_FLG; break;
44 case 'd': res = HEX_THIRTEEN_FLG; break;
45 case 'e': res = HEX_FOURTEEN_FLG; break;
46 case 'f': res = HEX_FIFTEEN_FLG; break;
47 case 'A': res = HEX_TEN_FLG; break;
48 case 'B': res = HEX_ELEVEN_FLG; break;
49 case 'C': res = HEX_TWELVE_FLG; break;
50 case 'D': res = HEX_THIRTEEN_FLG; break;
51 case 'E': res = HEX_FOURTEEN_FLG; break;
52 case 'F': res = HEX_FIFTEEN_FLG; break;
53 default : res = HEX_ZERO_FLG;
54 }
55 return res;
56 }
57
HexToChar(unsigned char in)58 unsigned char HexToChar(unsigned char in)
59 {
60 unsigned char res = '0';
61 switch (in) {
62 case HEX_ZERO_FLG: res = '0'; break;
63 case HEX_ONE_FLG: res = '1'; break;
64 case HEX_TWO_FLG: res = '2'; break;
65 case HEX_THREE_FLG: res = '3'; break;
66 case HEX_FOUR_FLG: res = '4'; break;
67 case HEX_FIVE_FLG: res = '5'; break;
68 case HEX_SIX_FLG: res = '6'; break;
69 case HEX_SEVEN_FLG: res = '7'; break;
70 case HEX_EIGHT_FLG: res = '8'; break;
71 case HEX_NINE_FLG: res = '9'; break;
72 case HEX_TEN_FLG: res = 'a'; break;
73 case HEX_ELEVEN_FLG: res = 'b'; break;
74 case HEX_TWELVE_FLG: res = 'c'; break;
75 case HEX_THIRTEEN_FLG: res = 'd'; break;
76 case HEX_FOURTEEN_FLG: res = 'e'; break;
77 case HEX_FIFTEEN_FLG: res = 'f'; break;
78 default : res = 'x';
79 }
80 return res;
81 }
82
ConvertBits(std::string & input)83 unsigned char ConvertBits(std::string &input)
84 {
85 unsigned char temp = 0; // 0: initialization
86 if (input[0] == '-') {
87 input.erase(0, 1);
88 }
89 temp = CharToHex(input[0]);
90 temp *= HEX_SIXTEEN_FLG;
91 input.erase(0, 1);
92 temp += CharToHex(input[0]);
93 input.erase(0, 1);
94 return temp;
95 }
96
GenerateUuid(unsigned char * data,int32_t size)97 bool GenerateUuid(unsigned char *data, int32_t size)
98 {
99 unsigned char buf[UUID_SIZE] = { 0 }; // 0: initialization
100 if (memcpy_s(data, size, buf, size) != EOK) {
101 return false;
102 }
103 int32_t len = 0;
104 while (len < size) {
105 len += RAND_priv_bytes(data, size - len);
106 }
107 data[HEX_SIX_FLG] = (data[HEX_SIX_FLG] & 0x0F) | 0x40; // 0x0F,0x40 Operate the mark
108 int m = 0x8; // Upper of numerical range
109 int n = 0xb; // down of numerical range
110 int r = static_cast<int>(data[HEX_EIGHT_FLG]);
111 unsigned char num = static_cast<unsigned char>(r % (n - m + 1) + m);
112 data[HEX_EIGHT_FLG] = (data[HEX_EIGHT_FLG] & 0x0F) | (num << 4); // 0x0F,4 Operate the mark
113 return true;
114 }
115
GetUUID(napi_env env,bool entropyCache,UUID & uuid)116 bool GetUUID(napi_env env, bool entropyCache, UUID &uuid)
117 {
118 uint32_t size = g_uuidCache.size();
119 if ((entropyCache == true) && (size != 0)) {
120 uuid = g_uuidCache.front();
121 g_uuidCache.pop();
122 } else {
123 if (size > MAX_CACHE_MASK) {
124 for (uint32_t i = 0; i < size; i++) {
125 g_uuidCache.pop();
126 }
127 }
128 bool res = GenerateUuid(uuid.elements, sizeof(uuid.elements));
129 if (!res) {
130 napi_throw_error(env, "-1", "uuid generate failed");
131 return false;
132 }
133 g_uuidCache.push(uuid);
134 res = GenerateUuid(uuid.elements, sizeof(uuid.elements));
135 if (!res) {
136 napi_throw_error(env, "-1", "uuid generate failed");
137 return false;
138 }
139 }
140 return true;
141 }
142
GetStringUUID(napi_env env,bool entropyCache)143 std::string GetStringUUID(napi_env env, bool entropyCache)
144 {
145 UUID uuid;
146 std::string uuidString = "";
147 if (!GetUUID(env, entropyCache, uuid)) {
148 uuidString = '0';
149 } else {
150 uuidString = GetFormatUUID(uuid);
151 }
152 return uuidString;
153 }
154
GetFormatUUID(const UUID & uuid)155 std::string GetFormatUUID(const UUID &uuid)
156 {
157 std::string format = "";
158 for (size_t i = 0; i < sizeof(uuid.elements); i++) {
159 unsigned char value = uuid.elements[i];
160 if (i >= HEX_FOUR_FLG && i % 2 == 0 && i <= HEX_TEN_FLG) { // 2: step value
161 format += "-";
162 }
163 format += HexToChar(value >> HEX_FOUR_FLG);
164 unsigned char high = value & 0xF0; // Operate the mark
165 if (high == 0) {
166 format += HexToChar(value);
167 } else {
168 format += HexToChar(value % (value & high));
169 }
170 }
171 return format;
172 }
173
GetBinaryUUID(napi_env env,bool entropyCache)174 napi_value GetBinaryUUID(napi_env env, bool entropyCache)
175 {
176 UUID uuid;
177 GetUUID(env, entropyCache, uuid);
178 void *data = nullptr;
179 napi_value arrayBuffer = nullptr;
180 size_t bufferSize = sizeof(uuid.elements);
181 napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer);
182 if (memcpy_s(data, bufferSize, uuid.elements, bufferSize) != EOK) {
183 HILOG_ERROR("get uuid memcpy_s failed");
184 return nullptr;
185 }
186 napi_value result = nullptr;
187 napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result);
188 return result;
189 }
190
DoParseUUID(napi_env env,napi_value src)191 napi_value DoParseUUID(napi_env env, napi_value src)
192 {
193 UUID uuid;
194 std::string buffer = "";
195 size_t bufferSize = 0; // 0: initialization
196 napi_status status = napi_ok;
197 status = napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize);
198 if (status != napi_ok) {
199 HILOG_ERROR("can not get src size");
200 return nullptr;
201 }
202 buffer.resize(bufferSize);
203 status = napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize);
204 if (status != napi_ok) {
205 HILOG_ERROR("can not get src value");
206 return nullptr;
207 }
208 void *data = reinterpret_cast<void*>(uuid.elements);
209 napi_value arrayBuffer = nullptr;
210 size_t outLen = sizeof(uuid.elements);
211 napi_create_arraybuffer(env, outLen, &data, &arrayBuffer);
212 unsigned char *count = static_cast<unsigned char*>(data);
213 while (!buffer.empty()) {
214 *count = ConvertBits(buffer);
215 count++;
216 }
217 napi_value result = nullptr;
218 napi_create_typedarray(env, napi_uint8_array, outLen, arrayBuffer, 0, &result);
219 return result;
220 }
221 }