Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenxmlmodule.c
Go to the documentation of this file.
00001 /* 00002 * xmlmodule.c : basic API for dynamic module loading added 2.6.17 00003 * 00004 * See Copyright for the status of this software. 00005 * 00006 * joelwreed@comcast.net 00007 * 00008 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html 00009 */ 00010 00011 #define IN_LIBXML 00012 #include "libxml.h" 00013 00014 #include <string.h> 00015 #include <libxml/xmlmemory.h> 00016 #include <libxml/xmlerror.h> 00017 #include <libxml/xmlmodule.h> 00018 #include <libxml/globals.h> 00019 00020 #ifdef LIBXML_MODULES_ENABLED 00021 00022 struct _xmlModule { 00023 unsigned char *name; 00024 void *handle; 00025 }; 00026 00027 static void *xmlModulePlatformOpen(const char *name); 00028 static int xmlModulePlatformClose(void *handle); 00029 static int xmlModulePlatformSymbol(void *handle, const char *name, void **result); 00030 00031 /************************************************************************ 00032 * * 00033 * module memory error handler * 00034 * * 00035 ************************************************************************/ 00036 00043 static void 00044 xmlModuleErrMemory(xmlModulePtr module, const char *extra) 00045 { 00046 const char *name = NULL; 00047 00048 if (module != NULL) { 00049 name = (const char *) module->name; 00050 } 00051 00052 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00053 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, 00054 name, NULL, 0, 0, 00055 "Memory allocation failed : %s\n", extra); 00056 } 00057 00068 xmlModulePtr 00069 xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED) 00070 { 00071 xmlModulePtr module; 00072 00073 module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule)); 00074 if (module == NULL) { 00075 xmlModuleErrMemory(NULL, "creating module"); 00076 return (NULL); 00077 } 00078 00079 memset(module, 0, sizeof(xmlModule)); 00080 00081 module->handle = xmlModulePlatformOpen(name); 00082 00083 if (module->handle == NULL) { 00084 xmlFree(module); 00085 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00086 XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, 00087 name, NULL, 0, 0, "failed to open %s\n", name); 00088 return(NULL); 00089 } 00090 00091 module->name = xmlStrdup((const xmlChar *) name); 00092 return (module); 00093 } 00094 00105 int 00106 xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol) 00107 { 00108 int rc = -1; 00109 00110 if ((NULL == module) || (symbol == NULL)) { 00111 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00112 XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, 00113 NULL, NULL, 0, 0, "null parameter\n"); 00114 return rc; 00115 } 00116 00117 rc = xmlModulePlatformSymbol(module->handle, name, symbol); 00118 00119 if (rc == -1) { 00120 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00121 XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, 00122 name, NULL, 0, 0, 00123 "failed to find symbol: %s\n", 00124 (name == NULL ? "NULL" : name)); 00125 return rc; 00126 } 00127 00128 return rc; 00129 } 00130 00141 int 00142 xmlModuleClose(xmlModulePtr module) 00143 { 00144 int rc; 00145 00146 if (NULL == module) { 00147 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00148 XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0, 00149 NULL, NULL, 0, 0, "null module pointer\n"); 00150 return -1; 00151 } 00152 00153 rc = xmlModulePlatformClose(module->handle); 00154 00155 if (rc != 0) { 00156 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00157 XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0, 00158 (const char *) module->name, NULL, 0, 0, 00159 "failed to close: %s\n", module->name); 00160 return -2; 00161 } 00162 00163 rc = xmlModuleFree(module); 00164 return (rc); 00165 } 00166 00177 int 00178 xmlModuleFree(xmlModulePtr module) 00179 { 00180 if (NULL == module) { 00181 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, 00182 XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, NULL, 00183 NULL, NULL, 0, 0, "null module pointer\n"); 00184 return -1; 00185 } 00186 00187 xmlFree(module->name); 00188 xmlFree(module); 00189 00190 return (0); 00191 } 00192 00193 #if defined(HAVE_DLOPEN) && !defined(_WIN32) 00194 #ifdef HAVE_DLFCN_H 00195 #include <dlfcn.h> 00196 #endif 00197 00198 #ifndef RTLD_GLOBAL /* For Tru64 UNIX 4.0 */ 00199 #define RTLD_GLOBAL 0 00200 #endif 00201 00209 static void * 00210 xmlModulePlatformOpen(const char *name) 00211 { 00212 return dlopen(name, RTLD_GLOBAL | RTLD_NOW); 00213 } 00214 00215 /* 00216 * xmlModulePlatformClose: 00217 * @handle: handle to the module 00218 * 00219 * returns 0 on success, and non-zero on error. 00220 */ 00221 00222 static int 00223 xmlModulePlatformClose(void *handle) 00224 { 00225 return dlclose(handle); 00226 } 00227 00228 /* 00229 * xmlModulePlatformSymbol: 00230 * http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html 00231 * returns 0 on success and the loaded symbol in result, and -1 on error. 00232 */ 00233 00234 static int 00235 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) 00236 { 00237 *symbol = dlsym(handle, name); 00238 if (dlerror() != NULL) { 00239 return -1; 00240 } 00241 return 0; 00242 } 00243 00244 #else /* ! HAVE_DLOPEN */ 00245 00246 #ifdef HAVE_SHLLOAD /* HAVE_SHLLOAD */ 00247 #ifdef HAVE_DL_H 00248 #include <dl.h> 00249 #endif 00250 /* 00251 * xmlModulePlatformOpen: 00252 * returns a handle on success, and zero on error. 00253 */ 00254 00255 static void * 00256 xmlModulePlatformOpen(const char *name) 00257 { 00258 return shl_load(name, BIND_IMMEDIATE, 0L); 00259 } 00260 00261 /* 00262 * xmlModulePlatformClose: 00263 * returns 0 on success, and non-zero on error. 00264 */ 00265 00266 static int 00267 xmlModulePlatformClose(void *handle) 00268 { 00269 return shl_unload(handle); 00270 } 00271 00272 /* 00273 * xmlModulePlatformSymbol: 00274 * http://docs.hp.com/en/B2355-90683/shl_load.3X.html 00275 * returns 0 on success and the loaded symbol in result, and -1 on error. 00276 */ 00277 00278 static int 00279 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) 00280 { 00281 int rc; 00282 00283 errno = 0; 00284 rc = shl_findsym(&handle, name, TYPE_UNDEFINED, symbol); 00285 return rc; 00286 } 00287 00288 #endif /* HAVE_SHLLOAD */ 00289 #endif /* ! HAVE_DLOPEN */ 00290 00291 #ifdef _WIN32 00292 00293 #include <windows.h> 00294 00295 /* 00296 * xmlModulePlatformOpen: 00297 * returns a handle on success, and zero on error. 00298 */ 00299 00300 static void * 00301 xmlModulePlatformOpen(const char *name) 00302 { 00303 return LoadLibrary(name); 00304 } 00305 00306 /* 00307 * xmlModulePlatformClose: 00308 * returns 0 on success, and non-zero on error. 00309 */ 00310 00311 static int 00312 xmlModulePlatformClose(void *handle) 00313 { 00314 int rc; 00315 00316 rc = FreeLibrary(handle); 00317 return (0 == rc); 00318 } 00319 00320 /* 00321 * xmlModulePlatformSymbol: 00322 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp 00323 * returns 0 on success and the loaded symbol in result, and -1 on error. 00324 */ 00325 00326 static int 00327 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) 00328 { 00329 *symbol = GetProcAddress(handle, name); 00330 return (NULL == *symbol) ? -1 : 0; 00331 } 00332 00333 #endif /* _WIN32 */ 00334 00335 #ifdef HAVE_BEOS 00336 00337 #include <kernel/image.h> 00338 00339 /* 00340 * xmlModulePlatformOpen: 00341 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html 00342 * returns a handle on success, and zero on error. 00343 */ 00344 00345 static void * 00346 xmlModulePlatformOpen(const char *name) 00347 { 00348 return (void *) load_add_on(name); 00349 } 00350 00351 /* 00352 * xmlModulePlatformClose: 00353 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html 00354 * returns 0 on success, and non-zero on error. 00355 */ 00356 00357 static int 00358 xmlModulePlatformClose(void *handle) 00359 { 00360 status_t rc; 00361 00362 rc = unload_add_on((image_id) handle); 00363 00364 if (rc == B_OK) 00365 return 0; 00366 else 00367 return -1; 00368 } 00369 00370 /* 00371 * xmlModulePlatformSymbol: 00372 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html 00373 * returns 0 on success and the loaded symbol in result, and -1 on error. 00374 */ 00375 00376 static int 00377 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) 00378 { 00379 status_t rc; 00380 00381 rc = get_image_symbol((image_id) handle, name, B_SYMBOL_TYPE_ANY, symbol); 00382 00383 return (rc == B_OK) ? 0 : -1; 00384 } 00385 00386 #endif /* HAVE_BEOS */ 00387 00388 #ifdef HAVE_OS2 00389 00390 #include <os2.h> 00391 00392 /* 00393 * xmlModulePlatformOpen: 00394 * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html 00395 * returns a handle on success, and zero on error. 00396 */ 00397 00398 static void * 00399 xmlModulePlatformOpen(const char *name) 00400 { 00401 char errbuf[256]; 00402 void *handle; 00403 int rc; 00404 00405 rc = DosLoadModule(errbuf, sizeof(errbuf) - 1, name, &handle); 00406 00407 if (rc) 00408 return 0; 00409 else 00410 return (handle); 00411 } 00412 00413 /* 00414 * xmlModulePlatformClose: 00415 * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html 00416 * returns 0 on success, and non-zero on error. 00417 */ 00418 00419 static int 00420 xmlModulePlatformClose(void *handle) 00421 { 00422 return DosFreeModule(handle); 00423 } 00424 00425 /* 00426 * xmlModulePlatformSymbol: 00427 * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html 00428 * returns 0 on success and the loaded symbol in result, and -1 on error. 00429 */ 00430 00431 static int 00432 xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) 00433 { 00434 int rc; 00435 00436 rc = DosQueryProcAddr(handle, 0, name, symbol); 00437 00438 return (rc == NO_ERROR) ? 0 : -1; 00439 } 00440 00441 #endif /* HAVE_OS2 */ 00442 00443 #define bottom_xmlmodule 00444 #include "elfgcchack.h" 00445 #endif /* LIBXML_MODULES_ENABLED */ Generated on Sat May 26 2012 04:33:37 for ReactOS by
1.7.6.1
|