ReactOS 0.4.15-dev-8092-ge0ba2f3
xmlmodule.c
Go to the documentation of this file.
1/*
2 * xmlmodule.c : basic API for dynamic module loading added 2.6.17
3 *
4 * See Copyright for the status of this software.
5 *
6 * joelwreed@comcast.net
7 *
8 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
9 */
10
11/* In order RTLD_GLOBAL and RTLD_NOW to be defined on zOS */
12#if defined(__MVS__)
13#define _UNIX03_SOURCE
14#endif
15
16#define IN_LIBXML
17#include "libxml.h"
18
19#include <string.h>
20#include <libxml/xmlmemory.h>
21#include <libxml/xmlerror.h>
22#include <libxml/xmlmodule.h>
23#include <libxml/globals.h>
24
25#ifdef LIBXML_MODULES_ENABLED
26
27struct _xmlModule {
28 unsigned char *name;
29 void *handle;
30};
31
32static void *xmlModulePlatformOpen(const char *name);
33static int xmlModulePlatformClose(void *handle);
34static int xmlModulePlatformSymbol(void *handle, const char *name, void **result);
35
36/************************************************************************
37 * *
38 * module memory error handler *
39 * *
40 ************************************************************************/
41
48static void
49xmlModuleErrMemory(xmlModulePtr module, const char *extra)
50{
51 const char *name = NULL;
52
53 if (module != NULL) {
54 name = (const char *) module->name;
55 }
56
57 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
59 name, NULL, 0, 0,
60 "Memory allocation failed : %s\n", extra);
61}
62
77xmlModulePtr
78xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED)
79{
80 xmlModulePtr module;
81
82 module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule));
83 if (module == NULL) {
84 xmlModuleErrMemory(NULL, "creating module");
85 return (NULL);
86 }
87
88 memset(module, 0, sizeof(xmlModule));
89
90 module->handle = xmlModulePlatformOpen(name);
91
92 if (module->handle == NULL) {
94 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
96 name, NULL, 0, 0, "failed to open %s\n", name);
97 return(NULL);
98 }
99
100 module->name = xmlStrdup((const xmlChar *) name);
101 return (module);
102}
103
118int
119xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol)
120{
121 int rc = -1;
122
123 if ((NULL == module) || (symbol == NULL) || (name == NULL)) {
124 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
126 NULL, NULL, 0, 0, "null parameter\n");
127 return rc;
128 }
129
130 rc = xmlModulePlatformSymbol(module->handle, name, symbol);
131
132 if (rc == -1) {
133 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
135 name, NULL, 0, 0,
136 "failed to find symbol: %s\n",
137 (name == NULL ? "NULL" : name));
138 return rc;
139 }
140
141 return rc;
142}
143
154int
155xmlModuleClose(xmlModulePtr module)
156{
157 int rc;
158
159 if (NULL == module) {
160 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
162 NULL, NULL, 0, 0, "null module pointer\n");
163 return -1;
164 }
165
166 rc = xmlModulePlatformClose(module->handle);
167
168 if (rc != 0) {
169 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
171 (const char *) module->name, NULL, 0, 0,
172 "failed to close: %s\n", module->name);
173 return -2;
174 }
175
176 rc = xmlModuleFree(module);
177 return (rc);
178}
179
190int
191xmlModuleFree(xmlModulePtr module)
192{
193 if (NULL == module) {
194 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
196 NULL, NULL, 0, 0, "null module pointer\n");
197 return -1;
198 }
199
200 xmlFree(module->name);
202
203 return (0);
204}
205
206#if defined(HAVE_DLOPEN) && !defined(_WIN32)
207#ifdef HAVE_DLFCN_H
208#include <dlfcn.h>
209#endif
210
211#ifndef RTLD_GLOBAL /* For Tru64 UNIX 4.0 */
212#define RTLD_GLOBAL 0
213#endif
214
222static void *
223xmlModulePlatformOpen(const char *name)
224{
225 return dlopen(name, RTLD_GLOBAL | RTLD_NOW);
226}
227
228/*
229 * xmlModulePlatformClose:
230 * @handle: handle to the module
231 *
232 * returns 0 on success, and non-zero on error.
233 */
234
235static int
236xmlModulePlatformClose(void *handle)
237{
238 return dlclose(handle);
239}
240
241/*
242 * xmlModulePlatformSymbol:
243 * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html
244 * returns 0 on success and the loaded symbol in result, and -1 on error.
245 */
246
247static int
248xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
249{
250 *symbol = dlsym(handle, name);
251 if (dlerror() != NULL) {
252 return -1;
253 }
254 return 0;
255}
256
257#else /* ! HAVE_DLOPEN */
258
259#ifdef HAVE_SHLLOAD /* HAVE_SHLLOAD */
260#ifdef HAVE_DL_H
261#include <dl.h>
262#endif
263/*
264 * xmlModulePlatformOpen:
265 * returns a handle on success, and zero on error.
266 */
267
268static void *
269xmlModulePlatformOpen(const char *name)
270{
271 return shl_load(name, BIND_IMMEDIATE, 0L);
272}
273
274/*
275 * xmlModulePlatformClose:
276 * returns 0 on success, and non-zero on error.
277 */
278
279static int
280xmlModulePlatformClose(void *handle)
281{
282 return shl_unload(handle);
283}
284
285/*
286 * xmlModulePlatformSymbol:
287 * http://docs.hp.com/en/B2355-90683/shl_load.3X.html
288 * returns 0 on success and the loaded symbol in result, and -1 on error.
289 */
290
291static int
292xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
293{
294 int rc;
295
296 errno = 0;
297 rc = shl_findsym(&handle, name, TYPE_UNDEFINED, symbol);
298 return rc;
299}
300
301#endif /* HAVE_SHLLOAD */
302#endif /* ! HAVE_DLOPEN */
303
304#if defined(_WIN32)
305
306#define WIN32_LEAN_AND_MEAN
307#include <windows.h>
308
309/*
310 * xmlModulePlatformOpen:
311 * returns a handle on success, and zero on error.
312 */
313
314static void *
315xmlModulePlatformOpen(const char *name)
316{
317 return LoadLibraryA(name);
318}
319
320/*
321 * xmlModulePlatformClose:
322 * returns 0 on success, and non-zero on error.
323 */
324
325static int
326xmlModulePlatformClose(void *handle)
327{
328 int rc;
329
330 rc = FreeLibrary(handle);
331 return (0 == rc);
332}
333
334/*
335 * xmlModulePlatformSymbol:
336 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp
337 * returns 0 on success and the loaded symbol in result, and -1 on error.
338 */
339
340static int
341xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
342{
344 *symbol = GetProcAddress(handle, name);
345 return (NULL == *symbol) ? -1 : 0;
347}
348
349#endif /* _WIN32 */
350
351#ifdef HAVE_BEOS
352
353#include <kernel/image.h>
354
355/*
356 * xmlModulePlatformOpen:
357 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
358 * returns a handle on success, and zero on error.
359 */
360
361static void *
362xmlModulePlatformOpen(const char *name)
363{
364 return (void *) load_add_on(name);
365}
366
367/*
368 * xmlModulePlatformClose:
369 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
370 * returns 0 on success, and non-zero on error.
371 */
372
373static int
374xmlModulePlatformClose(void *handle)
375{
376 status_t rc;
377
378 rc = unload_add_on((image_id) handle);
379
380 if (rc == B_OK)
381 return 0;
382 else
383 return -1;
384}
385
386/*
387 * xmlModulePlatformSymbol:
388 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
389 * returns 0 on success and the loaded symbol in result, and -1 on error.
390 */
391
392static int
393xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
394{
395 status_t rc;
396
397 rc = get_image_symbol((image_id) handle, name, B_SYMBOL_TYPE_ANY, symbol);
398
399 return (rc == B_OK) ? 0 : -1;
400}
401
402#endif /* HAVE_BEOS */
403
404#ifdef HAVE_OS2
405
406#include <os2.h>
407
408/*
409 * xmlModulePlatformOpen:
410 * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html
411 * returns a handle on success, and zero on error.
412 */
413
414static void *
415xmlModulePlatformOpen(const char *name)
416{
417 char errbuf[256];
418 void *handle;
419 int rc;
420
421 rc = DosLoadModule(errbuf, sizeof(errbuf) - 1, name, &handle);
422
423 if (rc)
424 return 0;
425 else
426 return (handle);
427}
428
429/*
430 * xmlModulePlatformClose:
431 * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html
432 * returns 0 on success, and non-zero on error.
433 */
434
435static int
436xmlModulePlatformClose(void *handle)
437{
438 return DosFreeModule(handle);
439}
440
441/*
442 * xmlModulePlatformSymbol:
443 * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html
444 * returns 0 on success and the loaded symbol in result, and -1 on error.
445 */
446
447static int
448xmlModulePlatformSymbol(void *handle, const char *name, void **symbol)
449{
450 int rc;
451
452 rc = DosQueryProcAddr(handle, 0, name, symbol);
453
454 return (rc == NO_ERROR) ? 0 : -1;
455}
456
457#endif /* HAVE_OS2 */
458
459#endif /* LIBXML_MODULES_ENABLED */
#define NO_ERROR
Definition: dderror.h:5
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
#define FreeLibrary(x)
Definition: compat.h:748
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
GLuint64EXT * result
Definition: glext.h:11304
#define ATTRIBUTE_UNUSED
Definition: i386-dis.c:36
@ extra
Definition: id3.c:95
#define XML_POP_WARNINGS
Definition: libxml.h:67
#define XML_IGNORE_PEDANTIC_WARNINGS
Definition: libxml.h:66
#define RTLD_NOW
Definition: port.h:100
#define RTLD_GLOBAL
Definition: port.h:101
#define L(x)
Definition: ntvdm.h:50
#define errno
Definition: errno.h:18
XMLPUBVAR xmlMallocFunc xmlMalloc
Definition: globals.h:248
XMLPUBVAR xmlFreeFunc xmlFree
Definition: globals.h:251
#define memset(x, y, z)
Definition: compat.h:39
Definition: name.c:39
@ XML_ERR_FATAL
Definition: xmlerror.h:28
@ XML_FROM_MODULE
Definition: xmlerror.h:63
@ XML_MODULE_OPEN
Definition: xmlerror.h:790
@ XML_MODULE_CLOSE
Definition: xmlerror.h:791
@ XML_ERR_NO_MEMORY
Definition: xmlerror.h:102
XMLPUBFUN xmlChar *XMLCALL xmlStrdup(const xmlChar *cur)
Definition: xmlstring.c:67
unsigned char xmlChar
Definition: xmlstring.h:28