• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "NdkDataConversion"
16 #include "ndk_data_conversion.h"
17 
18 #include "logger.h"
19 #include "securec.h"
20 namespace OHOS::UDMF {
21 static constexpr uint64_t MAX_RECORDS_COUNT = 4 * 1024 * 1024;
22 static constexpr uint64_t MAX_KEY_STRING_LEN = 1 * 1024 * 1024;
23 
GetNativeUnifiedData(OH_UdmfData * ndkData,std::shared_ptr<UnifiedData> & data)24 Status NdkDataConversion::GetNativeUnifiedData(OH_UdmfData* ndkData, std::shared_ptr<UnifiedData>& data)
25 {
26     if (ndkData == nullptr || data == nullptr || ndkData->cid != NdkStructId::UDMF_UNIFIED_DATA_STRUCT_ID) {
27         return Status::E_INVALID_PARAMETERS;
28     }
29     data = ndkData->unifiedData_;
30     return Status::E_OK;
31 }
32 
GetNdkUnifiedData(std::shared_ptr<UnifiedData> data,OH_UdmfData * ndkData)33 Status NdkDataConversion::GetNdkUnifiedData(std::shared_ptr<UnifiedData> data, OH_UdmfData* ndkData)
34 {
35     if (data == nullptr || ndkData == nullptr || ndkData->cid != NdkStructId::UDMF_UNIFIED_DATA_STRUCT_ID) {
36         return Status::E_INVALID_PARAMETERS;
37     }
38     ndkData->unifiedData_ = data;
39     return Status::E_OK;
40 }
41 
StrVectorToTypesArray(const std::vector<std::string> & strVector)42 char** NdkDataConversion::StrVectorToTypesArray(const std::vector<std::string>& strVector)
43 {
44     unsigned int vectorSize = strVector.size();
45     if (vectorSize == 0 || vectorSize > MAX_RECORDS_COUNT) {
46         return nullptr;
47     }
48     char** typesArray = new (std::nothrow) char* [vectorSize];
49     if (typesArray == nullptr) {
50         LOG_ERROR(UDMF_CAPI, "create types array failed!, vectorSize: %{public}d, MAX_RECORDS_COUNT: %{public}" PRIu64,
51             vectorSize, MAX_RECORDS_COUNT);
52         return nullptr;
53     }
54     for (unsigned int i = 0; i < vectorSize; ++i) {
55         unsigned int strLen = strVector[i].length() + 1;
56         if (strLen > MAX_KEY_STRING_LEN) {
57             LOG_INFO(UDMF_CAPI, "string exceeds maximum length, length is %{public}d", strLen);
58             DestroyStringArray(typesArray, vectorSize);
59             return nullptr;
60         }
61         typesArray[i] = new (std::nothrow) char[strLen];
62         if (typesArray[i] == nullptr ||
63             (strcpy_s(typesArray[i], strLen, strVector[i].c_str())) != EOK) {
64             LOG_ERROR(UDMF_CAPI, "string copy failed");
65             DestroyStringArray(typesArray, vectorSize);
66             return nullptr;
67         }
68     }
69     return typesArray;
70 }
71 
DestroyStringArray(char ** & bufArray,unsigned int & count)72 void NdkDataConversion::DestroyStringArray(char**& bufArray, unsigned int& count)
73 {
74     if (bufArray == nullptr) {
75         return;
76     }
77     for (unsigned int i = 0; i < count; i++) {
78         if (bufArray[i] != nullptr) {
79             delete[] bufArray[i];
80             bufArray[i] = nullptr;
81         }
82     }
83     delete[] bufArray;
84     bufArray = nullptr;
85     count = 0;
86 }
87 }