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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
#define INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
#include <LibreOfficeKit/LibreOfficeKit.h>
#if defined __GNUC__ || defined __clang__
# define LOK_TOLERATE_UNUSED __attribute__((used))
#else
# define LOK_TOLERATE_UNUSED
#endif
#if defined(__linux__) || defined (__FreeBSD__) || defined(_AIX) ||\
defined(_WIN32) || defined(__APPLE__) || defined (__NetBSD__) ||\
defined (__sun) || defined(__OpenBSD__)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <dlfcn.h>
#ifdef _AIX
# include <sys/ldr.h>
#endif
#ifdef __APPLE__
#define TARGET_LIB "lib" "sofficeapp" ".dylib"
#define TARGET_MERGED_LIB "lib" "mergedlo" ".dylib"
#else
#define TARGET_LIB "lib" "sofficeapp" ".so"
#define TARGET_MERGED_LIB "lib" "mergedlo" ".so"
#endif
#define SEPARATOR '/'
#else
#if !defined WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define TARGET_LIB "sofficeapp" ".dll"
#define TARGET_MERGED_LIB "mergedlo" ".dll"
#define SEPARATOR '\\'
#define UNOPATH "\\..\\URE\\bin"
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef _WIN32
#if !defined(IOS)
static void *lok_loadlib(const char *pFN)
{
return dlopen(pFN, RTLD_LAZY
#if defined LOK_LOADLIB_GLOBAL
| RTLD_GLOBAL
#endif
);
}
static char *lok_dlerror(void)
{
return dlerror();
}
// This function must be called to release memory allocated by lok_dlerror()
static void lok_dlerror_free(char *pErrMessage)
{
(void)pErrMessage;
// Do nothing for return of dlerror()
}
static void extendUnoPath(const char *pPath)
{
(void)pPath;
}
static void *lok_dlsym(void *Hnd, const char *pName)
{
return dlsym(Hnd, pName);
}
static int lok_dlclose(void *Hnd)
{
return dlclose(Hnd);
}
#endif // IOS
#else
static void *lok_loadlib(const char *pFN)
{
return (void *) LoadLibraryA(pFN);
}
static char *lok_dlerror(void)
{
LPSTR buf = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), 0, reinterpret_cast<LPSTR>(&buf), 0, NULL);
return buf;
}
// This function must be called to release memory allocated by lok_dlerror()
static void lok_dlerror_free(char *pErrMessage)
{
HeapFree(GetProcessHeap(), 0, pErrMessage);
}
static void *lok_dlsym(void *Hnd, const char *pName)
{
return reinterpret_cast<void *>(GetProcAddress((HINSTANCE) Hnd, pName));
}
static int lok_dlclose(void *Hnd)
{
return FreeLibrary((HINSTANCE) Hnd);
}
static void extendUnoPath(const char *pPath)
{
char *sNewPath = NULL, *sEnvPath = NULL;
size_t size_sEnvPath = 0, buffer_size = 0;
DWORD cChars;
if (!pPath)
return;
cChars = GetEnvironmentVariableA("PATH", sEnvPath, 0);
if (cChars > 0)
{
sEnvPath = (char *) malloc(cChars);
cChars = GetEnvironmentVariableA("PATH", sEnvPath, cChars);
//If PATH is not set then it is no error
if (cChars == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND)
{
free(sEnvPath);
return;
}
}
//prepare the new PATH. Add the Ure/bin directory at the front.
//note also adding ';'
if(sEnvPath)
size_sEnvPath = strlen(sEnvPath);
buffer_size = size_sEnvPath + 2*strlen(pPath) + strlen(UNOPATH) + 4;
sNewPath = (char *) malloc(buffer_size);
sNewPath[0] = L'\0';
strcat_s(sNewPath, buffer_size, pPath); // program to PATH
strcat_s(sNewPath, buffer_size, ";");
strcat_s(sNewPath, buffer_size, UNOPATH); // UNO to PATH
if (size_sEnvPath > 0)
{
strcat_s(sNewPath, buffer_size, ";");
strcat_s(sNewPath, buffer_size, sEnvPath);
}
SetEnvironmentVariableA("PATH", sNewPath);
free(sNewPath);
free(sEnvPath);
}
#endif
#if !defined(IOS)
static void *lok_dlopen( const char *install_path, char ** _imp_lib )
{
char *imp_lib;
void *dlhandle;
size_t partial_length, imp_lib_size;
struct stat dir_st;
*_imp_lib = NULL;
if (!install_path)
return NULL;
if (stat(install_path, &dir_st) != 0)
{
fprintf(stderr, "installation path \"%s\" does not exist\n", install_path);
return NULL;
}
// allocate large enough buffer
partial_length = strlen(install_path);
imp_lib_size = partial_length + sizeof(TARGET_LIB) + sizeof(TARGET_MERGED_LIB) + 2;
imp_lib = (char *) malloc(imp_lib_size);
if (!imp_lib)
{
fprintf( stderr, "failed to open library : not enough memory\n");
return NULL;
}
memcpy(imp_lib, install_path, partial_length);
extendUnoPath(install_path);
imp_lib[partial_length++] = SEPARATOR;
strncpy(imp_lib + partial_length, TARGET_LIB, imp_lib_size - partial_length);
dlhandle = lok_loadlib(imp_lib);
if (!dlhandle)
{
// If TARGET_LIB exists, and likely is a real library (not a
// small one-line text stub as in the --enable-mergedlib
// case), but dlopen failed for some reason, don't try
// TARGET_MERGED_LIB.
struct stat st;
if (stat(imp_lib, &st) == 0 && st.st_size > 100)
{
char *pErrMessage = lok_dlerror();
fprintf(stderr, "failed to open library '%s': %s\n",
imp_lib, pErrMessage);
lok_dlerror_free(pErrMessage);
free(imp_lib);
return NULL;
}
strncpy(imp_lib + partial_length, TARGET_MERGED_LIB, imp_lib_size - partial_length);
dlhandle = lok_loadlib(imp_lib);
if (!dlhandle)
{
char *pErrMessage = lok_dlerror();
fprintf(stderr, "failed to open library '%s': %s\n",
imp_lib, pErrMessage);
lok_dlerror_free(pErrMessage);
free(imp_lib);
return NULL;
}
}
*_imp_lib = imp_lib;
return dlhandle;
}
#endif
typedef LibreOfficeKit *(LokHookFunction)( const char *install_path);
typedef LibreOfficeKit *(LokHookFunction2)( const char *install_path, const char *user_profile_url );
typedef int (LokHookPreInit) ( const char *install_path, const char *user_profile_url );
typedef int (LokHookPreInit2) ( const char *install_path, const char *user_profile_url, LibreOfficeKit** kit);
#if defined(IOS) || defined(ANDROID)
LibreOfficeKit *libreofficekit_hook_2(const char* install_path, const char* user_profile_path);
#endif
static LibreOfficeKit *lok_init_2( const char *install_path, const char *user_profile_url )
{
#if !defined(IOS) && !defined(ANDROID)
void *dlhandle;
char *imp_lib;
LokHookFunction *pSym;
LokHookFunction2 *pSym2;
dlhandle = lok_dlopen(install_path, &imp_lib);
if (!dlhandle)
return NULL;
pSym2 = (LokHookFunction2 *) lok_dlsym(dlhandle, "libreofficekit_hook_2");
if (!pSym2)
{
if (user_profile_url != NULL)
{
fprintf( stderr, "the LibreOffice version in '%s' does not support passing a user profile to the hook function\n",
imp_lib );
lok_dlclose( dlhandle );
free( imp_lib );
return NULL;
}
pSym = (LokHookFunction *) lok_dlsym( dlhandle, "libreofficekit_hook" );
if (!pSym)
{
fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib );
lok_dlclose( dlhandle );
free( imp_lib );
return NULL;
}
free( imp_lib );
// dlhandle is "leaked"
// coverity[leaked_storage] - on purpose
return pSym( install_path );
}
if (user_profile_url != NULL && user_profile_url[0] == '/')
{
// It should be either a file: URL or a vnd.sun.star.pathname: URL.
fprintf( stderr, "second parameter to lok_init_2 '%s' should be a URL, not a pathname\n", user_profile_url );
lok_dlclose( dlhandle );
free( imp_lib );
return NULL;
}
free( imp_lib );
// dlhandle is "leaked"
// coverity[leaked_storage] - on purpose
return pSym2( install_path, user_profile_url );
#else
return libreofficekit_hook_2( install_path, user_profile_url );
#endif
}
static LOK_TOLERATE_UNUSED
LibreOfficeKit *lok_init( const char *install_path )
{
return lok_init_2( install_path, NULL );
}
#if !defined(IOS)
static LOK_TOLERATE_UNUSED
int lok_preinit( const char *install_path, const char *user_profile_url )
{
void *dlhandle;
char *imp_lib;
LokHookPreInit *pSym;
dlhandle = lok_dlopen(install_path, &imp_lib);
if (!dlhandle)
return -1;
pSym = (LokHookPreInit *) lok_dlsym(dlhandle, "lok_preinit");
if (!pSym)
{
fprintf( stderr, "failed to find pre-init hook in library '%s'\n", imp_lib );
lok_dlclose( dlhandle );
free( imp_lib );
return -1;
}
free( imp_lib );
// dlhandle is "leaked"
// coverity[leaked_storage] - on purpose
return pSym( install_path, user_profile_url );
}
#endif
#undef SEPARATOR // It is used at least in enum class MenuItemType
#ifdef __cplusplus
}
#endif
#endif // defined(__linux__) || defined (__FreeBSD__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
#endif // INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|