1 /*
2  * Copyright (C) 2024 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 "udid_manager.h"
17 
18 #include <string.h>
19 
20 #include "securec.h"
21 
22 #include "adaptor_log.h"
23 
24 static uint8_t g_localUdidBuffer[UDID_LEN] = { 0 };
25 
SetLocalUdid(const char * udid)26 bool SetLocalUdid(const char *udid)
27 {
28     IF_TRUE_LOGE_AND_RETURN_VAL(udid == NULL, false);
29     if (strlen(udid) != UDID_LEN) {
30         LOG_ERROR("udid size is invalid");
31         return false;
32     }
33     if (memcpy_s(g_localUdidBuffer, UDID_LEN, udid, UDID_LEN) != EOK) {
34         LOG_ERROR("memcpy_s failed");
35         return false;
36     }
37     return true;
38 }
39 
IsLocalUdidExit()40 static bool IsLocalUdidExit()
41 {
42     uint8_t emptyUdid[UDID_LEN] = { 0 };
43     if (memcmp(emptyUdid, g_localUdidBuffer, UDID_LEN) == 0) {
44         LOG_ERROR("g_localUdidBuffer not set");
45         return false;
46     }
47     return true;
48 }
49 
GetLocalUdid(Uint8Array * udid)50 bool GetLocalUdid(Uint8Array *udid)
51 {
52     if (udid == NULL || udid->data == NULL || udid->len < UDID_LEN) {
53         LOG_ERROR("invalid parameter");
54         return false;
55     }
56     if (!IsLocalUdidExit()) {
57         return false;
58     }
59     if (memcpy_s(udid->data, udid->len, g_localUdidBuffer, sizeof(g_localUdidBuffer)) != EOK) {
60         LOG_ERROR("memcpy_s failed");
61         return false;
62     }
63     udid->len = UDID_LEN;
64     return true;
65 }
66 
IsLocalUdid(Uint8Array udid)67 bool IsLocalUdid(Uint8Array udid)
68 {
69     IF_TRUE_LOGE_AND_RETURN_VAL(IS_ARRAY_NULL(udid), false);
70 
71     if (udid.len != UDID_LEN) {
72         return false;
73     }
74 
75     if (memcmp(udid.data, g_localUdidBuffer, UDID_LEN) == 0) {
76         return true;
77     }
78 
79     return false;
80 }