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
24 #define MSG_BUFF_MAX_LENGTH (81920 * 4)
25
CreateQueueMsgData(const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen,uint32_t * queueDataLen)26 QueueMsgData *CreateQueueMsgData(const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen,
27 uint32_t *queueDataLen)
28 {
29 if (devId == NULL || msg == NULL || msgLen == 0 || queueDataLen == NULL || msgLen > MSG_BUFF_MAX_LENGTH) {
30 return NULL;
31 }
32
33 uint32_t dataLen = sizeof(QueueMsgData) + msgLen;
34 QueueMsgData *queueData = MALLOC(dataLen);
35 if (queueData == NULL) {
36 SECURITY_LOG_ERROR("malloc result null");
37 return NULL;
38 }
39 uint32_t ret = (uint32_t)memcpy_s(&queueData->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify));
40 if (ret != EOK) {
41 SECURITY_LOG_ERROR("memcpy failed");
42 FREE(queueData);
43 return NULL;
44 }
45 ret = (uint32_t)memcpy_s(queueData->msgData, msgLen, msg, msgLen);
46 if (ret != EOK) {
47 SECURITY_LOG_ERROR("memcpy failed");
48 FREE(queueData);
49 return NULL;
50 }
51 queueData->msgLen = msgLen;
52 *queueDataLen = dataLen;
53
54 return queueData;
55 }
56