aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/dll.c
blob: a46a4621cbb76db40f753787b730f267e052904d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (C) 2011 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include <android/utils/dll.h>
#include <android/utils/system.h>
#include <android/utils/path.h>

#include <stdlib.h>

/* Utility function, append one string to another, caller must free result */
static char*
append_string( const char* str1, const char* str2 )
{
    int   len1   = strlen(str1);
    int   len2   = strlen(str2);
    char* result = malloc(len1+len2+1);

    if (result != NULL) {
        memcpy(result, str1, len1);
        memcpy(result + len1, str2, len2);
        result[len1+len2] = '\0';
    }
    return result;
}

#ifdef _WIN32

#include <windows.h>

/* This function is used to revert all forward slashes (/) in a path
 * string into unquoted backwards one (\). This is necessary because
 * LoadLibrary() and AddDllDirectory() do not support forward slashes.
 *
 * Caller must free the result string
 */
static char*
reverse_slashes( const char* path )
{
    int   len    = strlen(path);
    char* result = malloc(len+1);
    int   nn;

    for (nn = 0; nn < len; nn++) {
        int ch = path[nn];
        if (ch == '/') {
            ch = '\\';
        }
        result[nn] = (char)ch;
    }
    result[nn] = '\0';

    return result;
}

ADynamicLibrary*
adynamicLibrary_open( const char*  libraryName,
                      char**       pError)
{
    char*  libName = (char*) libraryName;
    void*  result;

    /* Append a .dll to the library name if it doesn't have an extension */
    if (strchr(libraryName,'.') == NULL) {
        libName = append_string(libraryName, ".dll");
    }

    /* Now do our magic */
    *pError = NULL;
    result = (ADynamicLibrary*) LoadLibrary( libName );
    if (result == NULL) {
        *pError = ASTRDUP("Could not load DLL!");
    }

    /* Free the library name if we modified it */
    if (libName != libraryName) {
        free(libName);
    }

    return (ADynamicLibrary*) result;
}

void*
adynamicLibrary_findSymbol( ADynamicLibrary*  lib,
                            const char*       symbolName,
                            char**            pError)
{
    void* result;

    *pError = NULL;

    if (lib == NULL) {
        *pError = strdup("NULL library pointer");
        return NULL;
    }
    if (symbolName == NULL || symbolName[0] == '\0') {
        *pError = strdup("NULL or empty symbolName");
        return NULL;
    }
    result = GetProcAddress( (HMODULE)lib, symbolName );
    if (result == NULL) {
        *pError = ASTRDUP("Could not find symbol");
    }
    return result;
}

/* Close/unload a given dynamic library */
void
adynamicLibrary_close( ADynamicLibrary*  lib )
{
    if (lib != NULL) {
        FreeLibrary( (HMODULE)lib );
    }
}

#else /* !_WIN32 */

#include <dlfcn.h>
#include <stdlib.h>

ADynamicLibrary*
adynamicLibrary_open( const char*  libraryName,
                      char**       pError)
{
    char*  libName = (char*) libraryName;
    void*  result;

#ifdef __APPLE__
#  define SO_EXTENSION ".dylib"
#else
#  define SO_EXTENSION ".so"
#endif

    /* Append a .so to the library name if it doesn't have an extension */
    if (strchr(libraryName,'.') == NULL) {
        libName = append_string(libraryName, SO_EXTENSION);
    }

    /* Now do our magic */
    *pError = NULL;
    result  = dlopen( libName, RTLD_LAZY );
    if (result == NULL) {
        *pError = strdup(dlerror());
    }

    /* Free the library name if we modified it */
    if (libName != (char*)libraryName) {
        free(libName);
    }

    return (ADynamicLibrary*) result;
}

void*
adynamicLibrary_findSymbol( ADynamicLibrary*  lib,
                            const char*       symbolName,
                            char**            pError)
{
    void* result;

    *pError = NULL;

    if (lib == NULL) {
        *pError = strdup("NULL library pointer");
        return NULL;
    }
    if (symbolName == NULL || symbolName[0] == '\0') {
        *pError = strdup("NULL or empty symbolName");
        return NULL;
    }
    result = dlsym(lib, symbolName);
    if (result == NULL) {
        *pError = strdup(dlerror());
    }
    return result;
}

/* Close/unload a given dynamic library */
void
adynamicLibrary_close( ADynamicLibrary*  lib )
{
    if (lib != NULL) {
        dlclose(lib);
    }
}

#endif /* !_WIN32 */