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 
17 #include "cj_utils_ffi.h"
18 
19 #include "securec.h"
20 #include "hilog_tag_wrapper.h"
21 
CreateCStringFromString(const std::string & source)22 char* CreateCStringFromString(const std::string& source)
23 {
24     if (source.size() == 0) {
25         return nullptr;
26     }
27     size_t length = source.size() + 1;
28     auto res = static_cast<char*>(malloc(length));
29     if (res == nullptr) {
30         TAG_LOGE(AAFwkTag::DEFAULT, "null res");
31         return nullptr;
32     }
33     if (strcpy_s(res, length, source.c_str()) != 0) {
34         free(res);
35         TAG_LOGE(AAFwkTag::DEFAULT, "Strcpy failed");
36         return nullptr;
37     }
38     return res;
39 }
40