1 /*
2 * Copyright (c) 2021 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 "softbus_utils.h"
17
18 #include <stdlib.h>
19
20 #include "comm_log.h"
21 #include "securec.h"
22 #include "softbus_adapter_crypto.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_adapter_timer.h"
26 #include "softbus_common.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29
30
31 #define MAC_BIT_ZERO 0
32 #define MAC_BIT_ONE 1
33 #define MAC_BIT_TWO 2
34 #define MAC_BIT_THREE 3
35 #define MAC_BIT_FOUR 4
36 #define MAC_BIT_FIVE 5
37
38 #define BT_ADDR_LEN 6
39 #define BT_ADDR_DELIMITER ":"
40 #define BT_ADDR_BASE 16
41
42 #define BUF_BYTE_LEN 64
43 #define BUF_HEX_LEN 128
44 #define OFFSET 1
45
46 #define MAC_DELIMITER_SECOND 2
47 #define MAC_DELIMITER_FOURTH 4
48 #define IP_DELIMITER_FIRST 1
49 #define IP_DELIMITER_THIRD 3
50 #define GET_ID_HALF_LEN 2
51 #define MAX_ID_LEN 65
52 #define MAX_IP_LEN 48
53 #define MAX_MAC_LEN 46
54 #define MAX_HANDLE_TIMES 3600
55
56 #define ONE_BYTE_SIZE 8
57
58 static void *g_timerId = NULL;
59 static int32_t g_handleTimes = 0;
60 static TimerFunCallback g_timerFunList[SOFTBUS_MAX_TIMER_FUN_NUM] = {0};
61 static bool g_signalingMsgSwitch = false;
62
CreateSoftBusList(void)63 SoftBusList *CreateSoftBusList(void)
64 {
65 SoftBusList *list = (SoftBusList *)SoftBusMalloc(sizeof(SoftBusList));
66 if (list == NULL) {
67 COMM_LOGE(COMM_UTILS, "malloc failed");
68 return NULL;
69 }
70 (void)memset_s(list, sizeof(SoftBusList), 0, sizeof(SoftBusList));
71
72 SoftBusMutexAttr mutexAttr;
73 mutexAttr.type = SOFTBUS_MUTEX_RECURSIVE;
74 if (SoftBusMutexInit(&list->lock, &mutexAttr) != SOFTBUS_OK) {
75 COMM_LOGE(COMM_UTILS, "init lock failed");
76 SoftBusFree(list);
77 return NULL;
78 }
79 ListInit(&list->list);
80 return list;
81 }
82
DestroySoftBusList(SoftBusList * list)83 void DestroySoftBusList(SoftBusList *list)
84 {
85 if (list == NULL) {
86 COMM_LOGE(COMM_UTILS, "list is null");
87 return;
88 }
89 ListDelInit(&list->list);
90 SoftBusMutexDestroy(&list->lock);
91 SoftBusFree(list);
92 return;
93 }
94
RegisterTimeoutCallback(int32_t timerFunId,TimerFunCallback callback)95 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback)
96 {
97 if (callback == NULL || timerFunId >= SOFTBUS_MAX_TIMER_FUN_NUM ||
98 timerFunId < SOFTBUS_CONN_TIMER_FUN) {
99 COMM_LOGE(COMM_UTILS, "invalid param");
100 return SOFTBUS_ERR;
101 }
102 if (g_timerFunList[timerFunId] != NULL) {
103 return SOFTBUS_OK;
104 }
105 g_timerFunList[timerFunId] = callback;
106 return SOFTBUS_OK;
107 }
108
HandleTimeoutFun(void)109 static void HandleTimeoutFun(void)
110 {
111 int32_t i;
112 for (i = 0; i < SOFTBUS_MAX_TIMER_FUN_NUM; i++) {
113 if (g_timerFunList[i] != NULL) {
114 g_timerFunList[i]();
115 }
116 }
117 ++g_handleTimes;
118 if (g_handleTimes >= MAX_HANDLE_TIMES && g_timerId != NULL) {
119 (void)SoftBusDeleteTimer(g_timerId);
120 COMM_LOGI(COMM_UTILS, "update new timer");
121 g_timerId = SoftBusCreateTimer(&g_timerId, TIMER_TYPE_PERIOD);
122 if (SoftBusStartTimer(g_timerId, TIMER_TIMEOUT) != SOFTBUS_OK) {
123 COMM_LOGE(COMM_UTILS, "start timer failed.");
124 (void)SoftBusDeleteTimer(g_timerId);
125 g_timerId = NULL;
126 }
127 g_handleTimes = 0;
128 }
129 }
130
SoftBusTimerInit(void)131 int32_t SoftBusTimerInit(void)
132 {
133 if (g_timerId != NULL) {
134 return SOFTBUS_OK;
135 }
136 SetTimerFunc(HandleTimeoutFun);
137 g_timerId = SoftBusCreateTimer(&g_timerId, TIMER_TYPE_PERIOD);
138 if (SoftBusStartTimer(g_timerId, TIMER_TIMEOUT) != SOFTBUS_OK) {
139 COMM_LOGE(COMM_UTILS, "start timer failed.");
140 (void)SoftBusDeleteTimer(g_timerId);
141 g_timerId = NULL;
142 return SOFTBUS_ERR;
143 }
144 return SOFTBUS_OK;
145 }
146
SoftBusTimerDeInit(void)147 void SoftBusTimerDeInit(void)
148 {
149 if (g_timerId != NULL) {
150 (void)SoftBusDeleteTimer(g_timerId);
151 g_timerId = NULL;
152 }
153 }
154
ConvertBytesToUpperCaseHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)155 int32_t ConvertBytesToUpperCaseHexString(char *outBuf, uint32_t outBufLen, const unsigned char * inBuf,
156 uint32_t inLen)
157 {
158 if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
159 COMM_LOGE(COMM_UTILS, "invalid param, inlen=%{public}u, outBufLen=%{public}u", inLen, outBufLen);
160 return SOFTBUS_ERR;
161 }
162
163 while (inLen > 0) {
164 unsigned char h = *inBuf / HEX_MAX_NUM;
165 unsigned char l = *inBuf % HEX_MAX_NUM;
166 if (h < DEC_MAX_NUM) {
167 *outBuf++ = '0' + h;
168 } else {
169 *outBuf++ = 'A' + h - DEC_MAX_NUM;
170 }
171 if (l < DEC_MAX_NUM) {
172 *outBuf++ = '0' + l;
173 } else {
174 *outBuf++ = 'A' + l - DEC_MAX_NUM;
175 }
176 ++inBuf;
177 inLen--;
178 }
179 return SOFTBUS_OK;
180 }
181
ConvertHexStringToBytes(unsigned char * outBuf,uint32_t outBufLen,const char * inBuf,uint32_t inLen)182 int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf,
183 uint32_t inLen)
184 {
185 if ((outBuf == NULL) || (inBuf == NULL) || (inLen % HEXIFY_UNIT_LEN != 0)) {
186 COMM_LOGE(COMM_UTILS, "invalid param");
187 return SOFTBUS_INVALID_PARAM;
188 }
189
190 uint32_t outLen = UN_HEXIFY_LEN(inLen);
191 if (outLen > outBufLen) {
192 COMM_LOGE(COMM_UTILS, "outLen > outBufLen");
193 return SOFTBUS_ERR;
194 }
195 uint32_t i = 0;
196 while (i < outLen) {
197 unsigned char c = *inBuf++;
198 if ((c >= '0') && (c <= '9')) {
199 c -= '0';
200 } else if ((c >= 'a') && (c <= 'f')) {
201 c -= 'a' - DEC_MAX_NUM;
202 } else if ((c >= 'A') && (c <= 'F')) {
203 c -= 'A' - DEC_MAX_NUM;
204 } else {
205 COMM_LOGE(COMM_UTILS, "HexToString Error! inBuf=%{public}c", c);
206 return SOFTBUS_ERR;
207 }
208 unsigned char c2 = *inBuf++;
209 if ((c2 >= '0') && (c2 <= '9')) {
210 c2 -= '0';
211 } else if ((c2 >= 'a') && (c2 <= 'f')) {
212 c2 -= 'a' - DEC_MAX_NUM;
213 } else if ((c2 >= 'A') && (c2 <= 'F')) {
214 c2 -= 'A' - DEC_MAX_NUM;
215 } else {
216 COMM_LOGE(COMM_UTILS, "HexToString Error! inBuf2=%{public}c", c2);
217 return SOFTBUS_ERR;
218 }
219 *outBuf++ = (c << HEX_MAX_BIT_NUM) | c2;
220 i++;
221 }
222 return SOFTBUS_OK;
223 }
224
ConvertBytesToHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)225 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf,
226 uint32_t inLen)
227 {
228 if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
229 COMM_LOGD(COMM_UTILS, "outBufLen=%{public}d, inLen=%{public}d", outBufLen, inLen);
230 return SOFTBUS_ERR;
231 }
232
233 while (inLen > 0) {
234 unsigned char h = *inBuf / HEX_MAX_NUM;
235 unsigned char l = *inBuf % HEX_MAX_NUM;
236 if (h < DEC_MAX_NUM) {
237 *outBuf++ = '0' + h;
238 } else {
239 *outBuf++ = 'a' + h - DEC_MAX_NUM;
240 }
241 if (l < DEC_MAX_NUM) {
242 *outBuf++ = '0' + l;
243 } else {
244 *outBuf++ = 'a' + l - DEC_MAX_NUM;
245 }
246 ++inBuf;
247 inLen--;
248 }
249 return SOFTBUS_OK;
250 }
251
GenerateRandomStr(char * str,uint32_t len)252 int32_t GenerateRandomStr(char *str, uint32_t len)
253 {
254 if ((str == NULL) || (len < HEXIFY_UNIT_LEN)) {
255 return SOFTBUS_INVALID_PARAM;
256 }
257
258 uint32_t hexLen = len / HEXIFY_UNIT_LEN;
259 unsigned char *hexAuthId = (unsigned char *)SoftBusMalloc(hexLen);
260 if (hexAuthId == NULL) {
261 return SOFTBUS_MEM_ERR;
262 }
263 (void)memset_s(hexAuthId, hexLen, 0, hexLen);
264 if (SoftBusGenerateRandomArray(hexAuthId, hexLen) != SOFTBUS_OK) {
265 COMM_LOGE(COMM_UTILS, "Generate random array fail");
266 SoftBusFree(hexAuthId);
267 return SOFTBUS_ERR;
268 }
269 if (ConvertBytesToHexString(str, len, hexAuthId, hexLen) != SOFTBUS_OK) {
270 COMM_LOGE(COMM_UTILS, "Convert bytes to hexstring fail");
271 SoftBusFree(hexAuthId);
272 return SOFTBUS_ERR;
273 }
274 SoftBusFree(hexAuthId);
275 return SOFTBUS_OK;
276 }
277
IsValidString(const char * input,uint32_t maxLen)278 bool IsValidString(const char *input, uint32_t maxLen)
279 {
280 if (input == NULL) {
281 COMM_LOGE(COMM_UTILS, "input is null");
282 return false;
283 }
284 uint32_t len = strlen(input);
285 if (len == 0 || len > maxLen) {
286 return false;
287 }
288 return true;
289 }
290
ConvertBtMacToBinary(const char * strMac,uint32_t strMacLen,uint8_t * binMac,uint32_t binMacLen)291 int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac,
292 uint32_t binMacLen)
293 {
294 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
295 COMM_LOGE(COMM_UTILS, "invalid param");
296 return SOFTBUS_INVALID_PARAM;
297 }
298 char *tmpMac = (char *)SoftBusMalloc(strMacLen * sizeof(char));
299 if (tmpMac == NULL) {
300 COMM_LOGE(COMM_UTILS, "tmpMac is null");
301 return SOFTBUS_MALLOC_ERR;
302 }
303 if (memcpy_s(tmpMac, strMacLen, strMac, strMacLen) != EOK) {
304 COMM_LOGE(COMM_UTILS, "memcpy tmpMac fail");
305 SoftBusFree(tmpMac);
306 return SOFTBUS_MEM_ERR;
307 }
308 char *nextTokenPtr = NULL;
309 char *token = strtok_r((char *)tmpMac, BT_ADDR_DELIMITER, &nextTokenPtr);
310 char *endptr = NULL;
311 for (int i = 0; i < BT_ADDR_LEN; i++) {
312 if (token == NULL) {
313 SoftBusFree(tmpMac);
314 return SOFTBUS_ERR;
315 }
316 binMac[i] = strtoul(token, &endptr, BT_ADDR_BASE);
317 token = strtok_r(NULL, BT_ADDR_DELIMITER, &nextTokenPtr);
318 }
319 SoftBusFree(tmpMac);
320 return SOFTBUS_OK;
321 }
322
ConvertBtMacToStrNoColon(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)323 int32_t ConvertBtMacToStrNoColon(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
324 uint32_t binMacLen)
325 {
326 int32_t ret;
327
328 if (strMac == NULL || strMacLen < BT_MAC_NO_COLON_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
329 COMM_LOGE(COMM_UTILS, "invalid param");
330 return SOFTBUS_INVALID_PARAM;
331 }
332 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x%02x%02x%02x%02x%02x",
333 binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
334 binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
335 if (ret < 0) {
336 COMM_LOGE(COMM_UTILS, "snprintf_s fail");
337 return SOFTBUS_ERR;
338 }
339 return SOFTBUS_OK;
340 }
341
ConvertBtMacToStr(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)342 int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac,
343 uint32_t binMacLen)
344 {
345 int32_t ret;
346
347 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
348 COMM_LOGE(COMM_UTILS, "invalid param");
349 return SOFTBUS_INVALID_PARAM;
350 }
351 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x",
352 binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
353 binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
354 if (ret < 0) {
355 COMM_LOGE(COMM_UTILS, "snprintf_s fail");
356 return SOFTBUS_ERR;
357 }
358 return SOFTBUS_OK;
359 }
360
ConvertReverseBtMacToStr(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)361 int32_t ConvertReverseBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen)
362 {
363 int32_t ret;
364 if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
365 return SOFTBUS_INVALID_PARAM;
366 }
367 ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x",
368 binMac[MAC_BIT_FIVE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_THREE],
369 binMac[MAC_BIT_TWO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_ZERO]);
370 if (ret < 0) {
371 return SOFTBUS_ERR;
372 }
373 return SOFTBUS_OK;
374 }
375
ConvertBtMacToU64(const char * strMac,uint32_t strMacLen,uint64_t * u64Mac)376 int32_t ConvertBtMacToU64(const char *strMac, uint32_t strMacLen, uint64_t *u64Mac)
377 {
378 if (strMac == NULL || strMacLen < BT_MAC_LEN || u64Mac == NULL) {
379 COMM_LOGE(COMM_UTILS, "invalid param");
380 return SOFTBUS_INVALID_PARAM;
381 }
382 uint8_t binaryAddr[BT_ADDR_LEN] = { 0 };
383 int32_t status = ConvertBtMacToBinary(strMac, BT_MAC_LEN, binaryAddr, BT_ADDR_LEN);
384 if (status != SOFTBUS_OK) {
385 COMM_LOGE(COMM_UTILS, "Convert btMac to binary fail");
386 return SOFTBUS_ERR;
387 }
388 uint64_t u64Value = 0;
389 for (int i = 0; i < BT_ADDR_LEN; i++) {
390 u64Value = (u64Value << ONE_BYTE_SIZE) | binaryAddr[i];
391 }
392 *u64Mac = u64Value;
393 return SOFTBUS_OK;
394 }
395
ConvertU64MacToStr(uint64_t u64Mac,char * strMac,uint32_t strMacLen)396 int32_t ConvertU64MacToStr(uint64_t u64Mac, char *strMac, uint32_t strMacLen)
397 {
398 if (strMac == NULL || strMacLen < BT_MAC_LEN || u64Mac == 0) {
399 return SOFTBUS_INVALID_PARAM;
400 }
401 uint8_t binaryAddr[BT_ADDR_LEN] = { 0 };
402 for (int i = BT_ADDR_LEN - 1; i >= 0; i--) {
403 binaryAddr[i] = u64Mac & 0xFF;
404 u64Mac = u64Mac >> ONE_BYTE_SIZE;
405 }
406 return ConvertBtMacToStr(strMac, strMacLen, binaryAddr, BT_ADDR_LEN);
407 }
ToUpperCase(char ch)408 static char ToUpperCase(char ch)
409 {
410 if (ch >= 'a' && ch <= 'z') {
411 return ch - 'a' + 'A';
412 }
413 return ch;
414 }
415
ToLowerCase(char ch)416 static char ToLowerCase(char ch)
417 {
418 if (ch >= 'A' && ch <= 'Z') {
419 return ch - 'A' + 'a';
420 }
421 return ch;
422 }
423
StringToUpperCase(const char * str,char * buf,int32_t size)424 int32_t StringToUpperCase(const char *str, char *buf, int32_t size)
425 {
426 if (str == NULL || buf == NULL) {
427 return SOFTBUS_ERR;
428 }
429 memset_s(buf, size, 0, size);
430 int32_t i;
431 for (i = 0; str[i] != '\0'; i++) {
432 buf[i] = ToUpperCase(str[i]);
433 }
434 return SOFTBUS_OK;
435 }
436
StringToLowerCase(const char * str,char * buf,int32_t size)437 int32_t StringToLowerCase(const char *str, char *buf, int32_t size)
438 {
439 if (str == NULL || buf == NULL) {
440 return SOFTBUS_ERR;
441 }
442 memset_s(buf, size, 0, size);
443 int32_t i;
444 for (i = 0; str[i] != '\0'; i++) {
445 buf[i] = ToLowerCase(str[i]);
446 }
447 return SOFTBUS_OK;
448 }
449
Int64ToString(int64_t src,char * buf,uint32_t bufLen)450 bool Int64ToString(int64_t src, char *buf, uint32_t bufLen)
451 {
452 if (buf == NULL) {
453 return false;
454 }
455 if (sprintf_s(buf, bufLen, "%" PRId64"", src) < 0) {
456 COMM_LOGE(COMM_UTILS, "convert int64 to str fail");
457 return false;
458 }
459 return true;
460 }
461
StrCmpIgnoreCase(const char * str1,const char * str2)462 int32_t StrCmpIgnoreCase(const char *str1, const char *str2)
463 {
464 if (str1 == NULL || str2 == NULL) {
465 COMM_LOGD(COMM_UTILS, "invalid param");
466 return SOFTBUS_ERR;
467 }
468 int32_t i;
469 for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++) {
470 if (ToUpperCase(str1[i]) != ToUpperCase(str2[i])) {
471 return SOFTBUS_ERR;
472 }
473 }
474 if (str1[i] != '\0' || str2[i] != '\0') {
475 return SOFTBUS_ERR;
476 }
477 return SOFTBUS_OK;
478 }
479
SetSignalingMsgSwitchOn(void)480 void SetSignalingMsgSwitchOn(void)
481 {
482 g_signalingMsgSwitch = true;
483 }
484
SetSignalingMsgSwitchOff(void)485 void SetSignalingMsgSwitchOff(void)
486 {
487 g_signalingMsgSwitch = false;
488 }
489
GetSignalingMsgSwitch(void)490 bool GetSignalingMsgSwitch(void)
491 {
492 return g_signalingMsgSwitch;
493 }
494
SignalingMsgPrint(const char * distinguish,unsigned char * data,unsigned char dataLen,uint32_t module)495 void SignalingMsgPrint(const char *distinguish, unsigned char *data, unsigned char dataLen, uint32_t module)
496 {
497 int ret = 0;
498 char signalingMsgBuf[BUF_HEX_LEN] = {0};
499
500 if (!GetSignalingMsgSwitch()) {
501 return;
502 }
503 if (dataLen >= BUF_BYTE_LEN) {
504 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, BUF_BYTE_LEN);
505 } else {
506 ret = ConvertBytesToHexString(signalingMsgBuf, BUF_HEX_LEN + OFFSET, data, dataLen);
507 }
508 if (ret != SOFTBUS_OK) {
509 COMM_LOGE(COMM_UTILS, "intercept signaling msg failed");
510 return;
511 }
512 }
513
MacInstead(char * data,uint32_t length,char delimiter)514 void MacInstead(char *data, uint32_t length, char delimiter)
515 {
516 if (length > MAX_MAC_LEN) {
517 COMM_LOGE(COMM_UTILS, "MacInstead len is invalid");
518 return;
519 }
520 int delimiterCnt = 0;
521 for (uint32_t i = 0; i < length; i++) {
522 if (delimiterCnt == MAC_DELIMITER_FOURTH) {
523 break;
524 }
525 if (data[i] == delimiter) {
526 delimiterCnt++;
527 }
528 if (delimiterCnt >= MAC_DELIMITER_SECOND && data[i] != delimiter) {
529 data[i] = '*';
530 }
531 }
532 }
533
IpInstead(char * data,uint32_t length,char delimiter)534 void IpInstead(char *data, uint32_t length, char delimiter)
535 {
536 if (length > MAX_IP_LEN) {
537 COMM_LOGE(COMM_UTILS, "IpInstead len is invalid");
538 return;
539 }
540 int delimiterCnt = 0;
541 for (uint32_t i = 0; i < length; i++) {
542 if (delimiterCnt == IP_DELIMITER_THIRD) {
543 break;
544 }
545 if (data[i] == delimiter) {
546 delimiterCnt++;
547 }
548 if (delimiterCnt >= IP_DELIMITER_FIRST && data[i] != delimiter) {
549 data[i] = '*';
550 }
551 }
552 }
553
IdInstead(char * data,uint32_t length)554 void IdInstead(char *data, uint32_t length)
555 {
556 if (length < 1 || length > MAX_ID_LEN) {
557 COMM_LOGE(COMM_UTILS, "IdInstead len is invalid");
558 return;
559 }
560 uint32_t halfLen = length / GET_ID_HALF_LEN;
561 for (uint32_t i = 0; i < length - 1; i++) {
562 if (i > halfLen) {
563 data[i] = '*';
564 }
565 }
566 }
567
DataMasking(const char * data,uint32_t length,char delimiter,char * container)568 void DataMasking(const char *data, uint32_t length, char delimiter, char *container)
569 {
570 if (data == NULL) {
571 COMM_LOGE(COMM_UTILS, "invalid param");
572 return;
573 }
574 if (memcpy_s(container, length, data, length) != EOK) {
575 COMM_LOGE(COMM_UTILS, "container memcpy_s failed");
576 return;
577 }
578 switch (delimiter) {
579 case MAC_DELIMITER:
580 MacInstead(container, length, delimiter);
581 break;
582 case IP_DELIMITER:
583 IpInstead(container, length, delimiter);
584 break;
585 case ID_DELIMITER:
586 IdInstead(container, length);
587 break;
588 default:
589 break;
590 }
591 }
592
GenerateStrHashAndConvertToHexString(const unsigned char * str,uint32_t len,unsigned char * hashStr,uint32_t hashStrLen)593 int32_t GenerateStrHashAndConvertToHexString(const unsigned char *str, uint32_t len,
594 unsigned char *hashStr, uint32_t hashStrLen)
595 {
596 int32_t ret;
597 unsigned char hashResult[SHA_256_HASH_LEN] = {0};
598 if (hashStrLen < HEXIFY_LEN(len / HEXIFY_UNIT_LEN)) {
599 COMM_LOGE(COMM_UTILS, "generate str hash invalid hashStrLen");
600 return SOFTBUS_INVALID_PARAM;
601 }
602 if (str == NULL) {
603 COMM_LOGE(COMM_UTILS, "generate str hash invalid param");
604 return SOFTBUS_INVALID_PARAM;
605 }
606 ret = SoftBusGenerateStrHash(str, strlen((char *)str), hashResult);
607 if (ret != SOFTBUS_OK) {
608 COMM_LOGE(COMM_UTILS, "generate str hash fail, ret=%{public}d", ret);
609 return ret;
610 }
611 ret = ConvertBytesToHexString((char *)hashStr, hashStrLen, (const unsigned char *)hashResult,
612 len / HEXIFY_UNIT_LEN);
613 if (ret != SOFTBUS_OK) {
614 COMM_LOGE(COMM_UTILS, "convert bytes to str hash fail, ret=%{public}d", ret);
615 return ret;
616 }
617 return SOFTBUS_OK;
618 }
619