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 <stdlib.h>
17 
18 #include "securec.h"
19 
20 #include "messenger_utils.h"
21 #include "utils_log.h"
22 #include "utils_mem.h"
23 
CreateQueueMsgData(const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen,uint32_t * queueDataLen)24 QueueMsgData *CreateQueueMsgData(const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen,
25     uint32_t *queueDataLen)
26 {
27     if (devId == NULL || msg == NULL || msgLen == 0 || queueDataLen == NULL) {
28         return NULL;
29     }
30 
31     uint32_t dataLen = sizeof(QueueMsgData) + msgLen;
32     QueueMsgData *queueData = MALLOC(dataLen);
33     if (queueData == NULL) {
34         SECURITY_LOG_ERROR("malloc result null");
35         return NULL;
36     }
37     uint32_t ret = (uint32_t)memcpy_s(&queueData->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify));
38     if (ret != EOK) {
39         SECURITY_LOG_ERROR("memcpy failed");
40         FREE(queueData);
41         return NULL;
42     }
43     ret = (uint32_t)memcpy_s(queueData->msgData, msgLen, msg, msgLen);
44     if (ret != EOK) {
45         SECURITY_LOG_ERROR("memcpy failed");
46         FREE(queueData);
47         return NULL;
48     }
49     queueData->msgLen = msgLen;
50     *queueDataLen = dataLen;
51 
52     return queueData;
53 }
54