1 /*
2 * Copyright (c) 2022 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 "cm_ipc_client_serialization.h"
17
18 #include "cm_log.h"
19 #include "cm_mem.h"
20
21 #include "cm_param.h"
22
GetUint32FromBuffer(uint32_t * value,const struct CmBlob * srcBlob,uint32_t * srcOffset)23 int32_t GetUint32FromBuffer(uint32_t *value, const struct CmBlob *srcBlob, uint32_t *srcOffset)
24 {
25 if ((*srcOffset > srcBlob->size) || (srcBlob->size - *srcOffset < sizeof(uint32_t))) {
26 return CMR_ERROR_BUFFER_TOO_SMALL;
27 }
28
29 if (memcpy_s(value, sizeof(uint32_t), srcBlob->data + *srcOffset, sizeof(uint32_t)) != EOK) {
30 return CMR_ERROR_INVALID_OPERATION;
31 }
32
33 *srcOffset += sizeof(uint32_t);
34 return CM_SUCCESS;
35 }
36
CmGetBlobFromBuffer(struct CmBlob * blob,const struct CmBlob * srcBlob,uint32_t * srcOffset)37 int32_t CmGetBlobFromBuffer(struct CmBlob *blob, const struct CmBlob *srcBlob, uint32_t *srcOffset)
38 {
39 if ((*srcOffset > srcBlob->size) || ((srcBlob->size - *srcOffset) < sizeof(uint32_t))) {
40 return CMR_ERROR_BUFFER_TOO_SMALL;
41 }
42
43 uint32_t size = *((uint32_t *)(srcBlob->data + *srcOffset));
44 if (ALIGN_SIZE(size) > srcBlob->size - *srcOffset - sizeof(uint32_t)) {
45 return CMR_ERROR_BUFFER_TOO_SMALL;
46 }
47
48 blob->size = size;
49 *srcOffset += sizeof(blob->size);
50 blob->data = (uint8_t *)(srcBlob->data + *srcOffset);
51 *srcOffset += ALIGN_SIZE(blob->size);
52 return CM_SUCCESS;
53 }
54
CmParamsToParamSet(struct CmParam * params,uint32_t cnt,struct CmParamSet ** outParamSet)55 int32_t CmParamsToParamSet(struct CmParam *params, uint32_t cnt, struct CmParamSet **outParamSet)
56 {
57 struct CmParamSet *newParamSet = NULL;
58
59 int32_t ret = CmInitParamSet(&newParamSet);
60 if (ret != CM_SUCCESS) {
61 CM_LOG_E("init param set failed");
62 return ret;
63 }
64
65 do {
66 uint8_t tmpData = 0;
67 struct CmBlob tmpBlob = { sizeof(tmpData), &tmpData };
68 for (uint32_t i = 0; i < cnt; ++i) {
69 if ((GetTagType(params[i].tag) == CM_TAG_TYPE_BYTES) &&
70 (params[i].blob.size == 0 || params[i].blob.data == NULL)) {
71 params[i].tag += CM_PARAM_BUFFER_NULL_INTERVAL;
72 params[i].blob = tmpBlob;
73 }
74 }
75
76 ret = CmAddParams(newParamSet, params, cnt);
77 if (ret != CM_SUCCESS) {
78 CM_LOG_E("add in params failed");
79 break;
80 }
81
82 ret = CmBuildParamSet(&newParamSet);
83 if (ret != CM_SUCCESS) {
84 CM_LOG_E("build paramset failed!");
85 break;
86 }
87 } while (0);
88 if (ret != CM_SUCCESS) {
89 CmFreeParamSet(&newParamSet);
90 return ret;
91 }
92
93 *outParamSet = newParamSet;
94
95 return ret;
96 }
97