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