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