• 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 <ctype.h>
19 #include <stdlib.h>
20 
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 #include "softbus_log.h"
30 #include "softbus_type_def.h"
31 
32 #define MAC_BIT_ZERO 0
33 #define MAC_BIT_ONE 1
34 #define MAC_BIT_TWO 2
35 #define MAC_BIT_THREE 3
36 #define MAC_BIT_FOUR 4
37 #define MAC_BIT_FIVE 5
38 
39 #define BT_ADDR_LEN 6
40 #define BT_ADDR_DELIMITER ":"
41 #define BT_ADDR_BASE 16
42 
43 static void *g_timerId = NULL;
44 static TimerFunCallback g_timerFunList[SOFTBUS_MAX_TIMER_FUN_NUM] = {0};
45 
CreateSoftBusList(void)46 SoftBusList *CreateSoftBusList(void)
47 {
48     SoftBusList *list = (SoftBusList *)SoftBusMalloc(sizeof(SoftBusList));
49     if (list == NULL) {
50         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "malloc failed");
51         return NULL;
52     }
53     (void)memset_s(list, sizeof(SoftBusList), 0, sizeof(SoftBusList));
54 
55     SoftBusMutexAttr mutexAttr;
56     mutexAttr.type = SOFTBUS_MUTEX_RECURSIVE;
57     if (SoftBusMutexInit(&list->lock, &mutexAttr) != SOFTBUS_OK) {
58         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "init lock failed");
59         SoftBusFree(list);
60         return NULL;
61     }
62 
63     ListInit(&list->list);
64     return list;
65 }
66 
DestroySoftBusList(SoftBusList * list)67 void DestroySoftBusList(SoftBusList *list)
68 {
69     ListDelInit(&list->list);
70     SoftBusMutexDestroy(&list->lock);
71     SoftBusFree(list);
72     return;
73 }
74 
RegisterTimeoutCallback(int32_t timerFunId,TimerFunCallback callback)75 int32_t RegisterTimeoutCallback(int32_t timerFunId, TimerFunCallback callback)
76 {
77     if (callback == NULL || timerFunId >= SOFTBUS_MAX_TIMER_FUN_NUM ||
78         timerFunId < SOFTBUS_CONN_TIMER_FUN) {
79         return SOFTBUS_ERR;
80     }
81 
82     if (g_timerFunList[timerFunId] != NULL) {
83         return SOFTBUS_OK;
84     }
85 
86     g_timerFunList[timerFunId] = callback;
87     return SOFTBUS_OK;
88 }
89 
HandleTimeoutFun(void)90 static void HandleTimeoutFun(void)
91 {
92     int32_t i;
93     for (i = 0; i < SOFTBUS_MAX_TIMER_FUN_NUM; i++) {
94         if (g_timerFunList[i] != NULL) {
95             g_timerFunList[i]();
96         }
97     }
98 }
99 
SoftBusTimerInit(void)100 int32_t SoftBusTimerInit(void)
101 {
102     if (g_timerId != NULL) {
103         return SOFTBUS_OK;
104     }
105     g_timerId = SoftBusCreateTimer(&g_timerId, (void *)HandleTimeoutFun, TIMER_TYPE_PERIOD);
106     if (SoftBusStartTimer(g_timerId, TIMER_TIMEOUT) != SOFTBUS_OK) {
107         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "start timer failed.");
108         (void)SoftBusDeleteTimer(g_timerId);
109         g_timerId = NULL;
110         return SOFTBUS_ERR;
111     }
112     return SOFTBUS_OK;
113 }
114 
SoftBusTimerDeInit(void)115 void SoftBusTimerDeInit(void)
116 {
117     if (g_timerId != NULL) {
118         (void)SoftBusDeleteTimer(g_timerId);
119         g_timerId = NULL;
120     }
121 }
122 
ConvertHexStringToBytes(unsigned char * outBuf,uint32_t outBufLen,const char * inBuf,uint32_t inLen)123 int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, const char *inBuf, uint32_t inLen)
124 {
125     (void)outBufLen;
126 
127     if ((outBuf == NULL) || (inBuf == NULL) || (inLen % HEXIFY_UNIT_LEN != 0)) {
128         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
129         return SOFTBUS_ERR;
130     }
131 
132     uint32_t outLen = UN_HEXIFY_LEN(inLen);
133     uint32_t i = 0;
134     while (i < outLen) {
135         unsigned char c = *inBuf++;
136         if ((c >= '0') && (c <= '9')) {
137             c -= '0';
138         } else if ((c >= 'a') && (c <= 'f')) {
139             c -= 'a' - DEC_MAX_NUM;
140         } else if ((c >= 'A') && (c <= 'F')) {
141             c -= 'A' - DEC_MAX_NUM;
142         } else {
143             SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c", c);
144             return SOFTBUS_ERR;
145         }
146 
147         unsigned char c2 = *inBuf++;
148         if ((c2 >= '0') && (c2 <= '9')) {
149             c2 -= '0';
150         } else if ((c2 >= 'a') && (c2 <= 'f')) {
151             c2 -= 'a' - DEC_MAX_NUM;
152         } else if ((c2 >= 'A') && (c2 <= 'F')) {
153             c2 -= 'A' - DEC_MAX_NUM;
154         } else {
155             SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "HexToString Error! %c2", c2);
156             return SOFTBUS_ERR;
157         }
158 
159         *outBuf++ = (c << HEX_MAX_BIT_NUM) | c2;
160         i++;
161     }
162     return SOFTBUS_OK;
163 }
164 
ConvertBytesToHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)165 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf, uint32_t inLen)
166 {
167     if ((outBuf == NULL) || (inBuf == NULL) || (outBufLen < HEXIFY_LEN(inLen))) {
168         return SOFTBUS_ERR;
169     }
170 
171     while (inLen > 0) {
172         unsigned char h = *inBuf / HEX_MAX_NUM;
173         unsigned char l = *inBuf % HEX_MAX_NUM;
174 
175         if (h < DEC_MAX_NUM) {
176             *outBuf++ = '0' + h;
177         } else {
178             *outBuf++ = 'a' + h - DEC_MAX_NUM;
179         }
180 
181         if (l < DEC_MAX_NUM) {
182             *outBuf++ = '0' + l;
183         } else {
184             *outBuf++ = 'a' + l - DEC_MAX_NUM;
185         }
186 
187         ++inBuf;
188         inLen--;
189     }
190     return SOFTBUS_OK;
191 }
192 
GenerateRandomStr(char * str,uint32_t len)193 int32_t GenerateRandomStr(char *str, uint32_t len)
194 {
195     if ((str == NULL) ||  (len < HEXIFY_UNIT_LEN)) {
196         return SOFTBUS_INVALID_PARAM;
197     }
198 
199     uint32_t hexLen = len / HEXIFY_UNIT_LEN;
200     unsigned char *hexAuthId = (unsigned char *)SoftBusMalloc(hexLen);
201     if (hexAuthId == NULL) {
202         return SOFTBUS_MEM_ERR;
203     }
204     (void)memset_s(hexAuthId, hexLen, 0, hexLen);
205 
206     if (SoftBusGenerateRandomArray(hexAuthId, hexLen) != SOFTBUS_OK) {
207         SoftBusFree(hexAuthId);
208         return SOFTBUS_ERR;
209     }
210 
211     if (ConvertBytesToHexString(str, len, hexAuthId, hexLen) != SOFTBUS_OK) {
212         SoftBusFree(hexAuthId);
213         return SOFTBUS_ERR;
214     }
215 
216     SoftBusFree(hexAuthId);
217     return SOFTBUS_OK;
218 }
219 
IsValidString(const char * input,uint32_t maxLen)220 bool IsValidString(const char *input, uint32_t maxLen)
221 {
222     if (input == NULL) {
223         return false;
224     }
225 
226     uint32_t len = strlen(input);
227     if (len >= maxLen) {
228         return false;
229     }
230 
231     return true;
232 }
233 
ConvertBtMacToBinary(const char * strMac,uint32_t strMacLen,uint8_t * binMac,uint32_t binMacLen)234 int32_t ConvertBtMacToBinary(const char *strMac, uint32_t strMacLen, uint8_t *binMac, uint32_t binMacLen)
235 {
236     if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
237         return SOFTBUS_INVALID_PARAM;
238     }
239     char *tmpMac = (char *)SoftBusMalloc(strMacLen * sizeof(char));
240     if (tmpMac == NULL) {
241         return SOFTBUS_MALLOC_ERR;
242     }
243     if (memcpy_s(tmpMac, strMacLen, strMac, strMacLen) != EOK) {
244         SoftBusFree(tmpMac);
245         return SOFTBUS_MEM_ERR;
246     }
247     char *nextTokenPtr = NULL;
248     char *token = strtok_r((char *)tmpMac, BT_ADDR_DELIMITER, &nextTokenPtr);
249     char *endptr = NULL;
250     for (int i = 0; i < BT_ADDR_LEN; i++) {
251         if (token == NULL) {
252             SoftBusFree(tmpMac);
253             return SOFTBUS_ERR;
254         }
255         binMac[i] = strtoul(token, &endptr, BT_ADDR_BASE);
256         token = strtok_r(NULL, BT_ADDR_DELIMITER, &nextTokenPtr);
257     }
258     SoftBusFree(tmpMac);
259     return SOFTBUS_OK;
260 }
261 
ConvertBtMacToStr(char * strMac,uint32_t strMacLen,const uint8_t * binMac,uint32_t binMacLen)262 int32_t ConvertBtMacToStr(char *strMac, uint32_t strMacLen, const uint8_t *binMac, uint32_t binMacLen)
263 {
264     int32_t ret;
265 
266     if (strMac == NULL || strMacLen < BT_MAC_LEN || binMac == NULL || binMacLen < BT_ADDR_LEN) {
267         return SOFTBUS_INVALID_PARAM;
268     }
269     ret = snprintf_s(strMac, strMacLen, strMacLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x",
270         binMac[MAC_BIT_ZERO], binMac[MAC_BIT_ONE], binMac[MAC_BIT_TWO],
271         binMac[MAC_BIT_THREE], binMac[MAC_BIT_FOUR], binMac[MAC_BIT_FIVE]);
272     if (ret < 0) {
273         return SOFTBUS_ERR;
274     }
275     return SOFTBUS_OK;
276 }
277 
Strnicmp(const char * src1,const char * src2,uint32_t len)278 int32_t Strnicmp(const char *src1, const char *src2, uint32_t len)
279 {
280     if (src1 == NULL || src2 == NULL ||
281         strlen(src1) + 1 < len || strlen(src2) + 1 < len) {
282         return SOFTBUS_ERR;
283     }
284     char *tmpSrc1 = (char *)src1;
285     char *tmpSrc2 = (char *)src2;
286     int32_t ca;
287     int32_t cb;
288     uint32_t i = len;
289     do {
290         ca = (int32_t)(*tmpSrc1++);
291         cb = (int32_t)(*tmpSrc2++);
292         ca = toupper(ca);
293         cb = toupper(cb);
294         i--;
295     } while (ca == cb && i > 0);
296     return ca - cb;
297 }
298