1 /*
2  * Copyright (C) 2021 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 "platform_adp.h"
17 #ifndef _WIN32
18 #include <dlfcn.h>
19 #endif
20 #include "directory_ex.h"
21 #include "image_log.h"
22 #include "string"
23 #include "plugin_errors.h"
24 #include "type_traits"
25 
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
28 
29 #undef LOG_TAG
30 #define LOG_TAG "PlatformAdp"
31 
32 namespace OHOS {
33 namespace MultimediaPlugin {
34 using std::string;
35 
36 const string PlatformAdp::DIR_SEPARATOR = "/";
37 const string PlatformAdp::LIBRARY_FILE_SUFFIX = "so";
38 
39 #ifdef _WIN32
AdpLoadLibrary(const string & packageName)40 HMODULE PlatformAdp::AdpLoadLibrary(const string &packageName)
41 {
42     return LoadLibrary(packageName.c_str());
43 }
44 
AdpFreeLibrary(HMODULE handle)45 void PlatformAdp::AdpFreeLibrary(HMODULE handle)
46 {
47     FreeLibrary(handle);
48 }
49 
AdpGetSymAddress(HMODULE handle,const string & symbol)50 FARPROC PlatformAdp::AdpGetSymAddress(HMODULE handle, const string &symbol)
51 {
52     return GetProcAddress(handle, symbol.c_str());
53 }
54 #else
LoadLibrary(const std::string & packageName)55 void *PlatformAdp::LoadLibrary(const std::string &packageName)
56 {
57     return dlopen(packageName.c_str(), RTLD_LAZY);
58 }
59 
FreeLibrary(void * handle)60 void PlatformAdp::FreeLibrary(void *handle)
61 {
62     dlclose(handle);
63 }
64 
GetSymAddress(void * handle,const std::string & symbol)65 void *PlatformAdp::GetSymAddress(void *handle, const std::string &symbol)
66 {
67     return dlsym(handle, symbol.c_str());
68 }
69 #endif
70 
CheckAndNormalizePath(string & path)71 uint32_t PlatformAdp::CheckAndNormalizePath(string &path)
72 {
73 #if !defined(_WIN32) && !defined(_APPLE)
74     if (path.empty()) {
75         IMAGE_LOGE("check path empty.");
76         return ERR_GENERAL;
77     }
78 #endif
79 
80     string realPath;
81     if (!PathToRealPath(path, realPath)) {
82         IMAGE_LOGE("path to real path error.");
83         return ERR_GENERAL;
84     }
85 
86     path = std::move(realPath);
87 
88     return SUCCESS;
89 }
90 
91 // ------------------------------- private method -------------------------------
PlatformAdp()92 PlatformAdp::PlatformAdp() {}
93 
~PlatformAdp()94 PlatformAdp::~PlatformAdp() {}
95 } // namespace MultimediaPlugin
96 } // namespace OHOS
97