86622f15051a83730441d97c719ffce266b272c3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / shared_library.cpp
1 // Copyright (C) 2014 The Android Open Source Project
2 //
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 #include "emugl/common/shared_library.h"
16
17 #include <stddef.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 #ifndef _WIN32
22 #include <dlfcn.h>
23 #include <stdlib.h>
24 #endif
25
26 namespace emugl {
27
28 // static
29 SharedLibrary* SharedLibrary::open(const char* libraryName) {
30     char error[1];
31     return open(libraryName, error, sizeof(error));
32 }
33
34 #ifdef _WIN32
35
36 // static
37 SharedLibrary* SharedLibrary::open(const char* libraryName,
38                                    char* error,
39                                    size_t errorSize) {
40     HMODULE lib = LoadLibrary(libraryName);
41     if (lib) {
42         return new SharedLibrary(lib);
43     }
44
45     if (errorSize == 0) {
46         return NULL;
47     }
48
49     // Convert error into human-readable message.
50     DWORD errorCode = ::GetLastError();
51     LPSTR message = NULL;
52     size_t messageLen = FormatMessageA(
53             FORMAT_MESSAGE_ALLOCATE_BUFFER |
54             FORMAT_MESSAGE_FROM_SYSTEM |
55             FORMAT_MESSAGE_IGNORE_INSERTS,
56             NULL,
57             errorCode,
58             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
59             (LPSTR) &message,
60             0,
61             NULL);
62
63     int ret = snprintf(error, errorSize, "%.*s", (int)messageLen, message);
64     if (ret < 0 || ret == static_cast<int>(errorSize)) {
65         // snprintf() on Windows doesn't behave as expected by C99,
66         // this path is to ensure that the result is always properly
67         // zero-terminated.
68         ret = static_cast<int>(errorSize - 1);
69         error[ret] = '\0';
70     }
71     // Remove any trailing \r\n added by FormatMessage
72     if (ret > 0 && error[ret - 1] == '\n') {
73         error[--ret] = '\0';
74     }
75     if (ret > 0 && error[ret - 1] == '\r') {
76         error[--ret] = '\0';
77     }
78     return NULL;
79 }
80
81 SharedLibrary::SharedLibrary(HandleType lib) : mLib(lib) {}
82
83 SharedLibrary::~SharedLibrary() {
84     if (mLib) {
85         FreeLibrary(mLib);
86     }
87 }
88
89 SharedLibrary::FunctionPtr SharedLibrary::findSymbol(
90         const char* symbolName) const {
91     if (!mLib || !symbolName) {
92         return NULL;
93     }
94     return reinterpret_cast<FunctionPtr>(
95                 GetProcAddress(mLib, symbolName));
96 }
97
98 #else // !_WIN32
99
100 // static
101 SharedLibrary* SharedLibrary::open(const char* libraryName,
102                                    char* error,
103                                    size_t errorSize) {
104     const char* libPath = libraryName;
105     char* path = NULL;
106
107     const char* libBaseName = strrchr(libraryName, '/');
108     if (!libBaseName) {
109         libBaseName = libraryName;
110     }
111
112     if (!strchr(libBaseName, '.')) {
113         // There is no extension in this library name, so append one.
114 #ifdef __APPLE__
115         static const char kDllExtension[] = ".dylib";
116 #else
117         static const char kDllExtension[] = ".so";
118 #endif
119         size_t pathLen = strlen(libraryName) + sizeof(kDllExtension);
120         path = static_cast<char*>(malloc(pathLen));
121         snprintf(path, pathLen, "%s%s", libraryName, kDllExtension);
122         libPath = path;
123     }
124
125     dlerror();  // clear error.
126
127 #ifdef __APPLE__
128     // On OSX, some libraries don't include an extension (notably OpenGL)
129     // On OSX we try to open |libraryName| first.  If that doesn't exist,
130     // we try |libraryName|.dylib
131     void* lib = dlopen(libraryName, RTLD_NOW);
132     if (lib == NULL) {
133         lib = dlopen(libPath, RTLD_NOW);
134     }
135 #else
136     void* lib = dlopen(libPath, RTLD_NOW);
137 #endif
138
139     if (path) {
140         free(path);
141     }
142
143     if (lib) {
144         return new SharedLibrary(lib);
145     }
146
147     snprintf(error, errorSize, "%s", dlerror());
148     return NULL;
149 }
150
151 SharedLibrary::SharedLibrary(HandleType lib) : mLib(lib) {}
152
153 SharedLibrary::~SharedLibrary() {
154     if (mLib) {
155         dlclose(mLib);
156     }
157 }
158
159 SharedLibrary::FunctionPtr SharedLibrary::findSymbol(
160         const char* symbolName) const {
161     if (!mLib || !symbolName) {
162         return NULL;
163     }
164     return reinterpret_cast<FunctionPtr>(dlsym(mLib, symbolName));
165 }
166
167 #endif  // !_WIN32
168
169 }  // namespace emugl