summaryrefslogtreecommitdiff
path: root/xmlmodule.c
blob: e21d49ebed45741b90c9021549d4088647cf556d (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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * xmlmodule.c :
 *
 * See Copyright for the status of this software.
 *
 * joelwreed@comcast.net
 */

#define IN_LIBXML
#include "libxml.h"

#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlerror.h>
#include <libxml/xmlmodule.h>
#include <libxml/globals.h>

#ifdef LIBXML_MODULES_ENABLED

struct _xmlModule {
  unsigned char* name;
  void* handle;
};

static void* xmlModulePlatformOpen(const char* name);
static int xmlModulePlatformClose(void* handle);
static void* xmlModulePlatformSymbol(void* handle, const char* name);

/************************************************************************
 *									*
 * 		module memory error handler				*
 *									*
 ************************************************************************/
/**
 * xmlModuleErrMemory:
 * @extra:  extra information
 *
 * Handle an out of memory condition
 */
static void
xmlModuleErrMemory(xmlModulePtr module, const char *extra)
{
    const char *name = NULL;
    if (module != NULL) {
      name = (const char *) module->name;
    }

    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
                    name, NULL, 0, 0,
                    "Memory allocation failed : %s\n", extra);
}

xmlModulePtr xmlModuleOpen(const char* name)
{
  xmlModulePtr module;

  module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule));
  if (module == NULL) {
    xmlModuleErrMemory(NULL, "creating module");
    return(NULL);
  }

  memset(module, 0, sizeof(xmlModule));

  module->handle = xmlModulePlatformOpen(name);

  if (module->handle == NULL) {
    xmlFree(module);
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    name, NULL, 0, 0,
                    "failed to open %s\n", name);
    return 0;
  }

  module->name = xmlStrdup((const xmlChar*)name);
  return (module);
}

void* xmlModuleSymbol(xmlModulePtr module, const char* name)
{
  void* symbol;

  if (NULL == module) {
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    NULL, NULL, 0, 0,
                    "null module pointer\n", 0);
     return 0;
 }

  symbol = xmlModulePlatformSymbol(module->handle, name);

  if (symbol == 0) {
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    symbol, NULL, 0, 0,
                    "failed to find symbol: %s\n", 0);
    return 0;
  }

  return (symbol);
}

int xmlModuleClose(xmlModulePtr module)
{
  int rc;

  if (NULL == module) {
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    NULL, NULL, 0, 0,
                    "null module pointer\n", 0);
    return -1;
  }

  rc = xmlModulePlatformClose(module->handle);

  if (rc != 0) {
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    (const char*)module->name, NULL, 0, 0,
                    "failed to close: %s\n", 0);
    return -2;
  }

  rc = xmlModuleFree(module);
  return (rc);
}

int xmlModuleFree(xmlModulePtr module)
{
  if (NULL == module) {
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE,
                    XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0,
                    NULL, NULL, 0, 0,
                    "null module pointer\n", 0);
    return -1;
  }

  xmlFree(module->name);
  xmlFree(module);

  return (0);
}

#ifdef HAVE_DLOPEN

#include <dlfcn.h>

/*
 * xmlModulePlatformOpen:
 * returns a handle on success, and zero on error.
 */ 

static void* xmlModulePlatformOpen(const char* name)
{
  void* handle;
  handle = dlopen(name, RTLD_GLOBAL|RTLD_NOW);
  return (handle);
}

/*
 * xmlModulePlatformClose:
 * returns 0 on success, and non-zero on error.
 */ 

static int xmlModulePlatformClose(void* handle)
{
  int rc;
  rc = dlclose(handle);
  return (rc);
}

/*
 * xmlModulePlatformSymbol:
 * returns loaded symbol on success, and zero on error.
 */ 

static void* xmlModulePlatformSymbol(void* handle, const char* name)
{
  void* sym;
  sym = dlsym(handle, name);
  return (sym);
}

#endif /* HAVE_DLOPEN */

#ifdef HAVE_SHLLOAD /* HAVE_SHLLOAD */

/*
 * xmlModulePlatformOpen:
 * returns a handle on success, and zero on error.
 */ 

void* xmlModulePlatformOpen(const char* name)
{
  void* handle;
  handle = shl_load(name, BIND_IMMEDIATE, 0L);
  return (handle);
}

/*
 * xmlModulePlatformClose:
 * returns 0 on success, and non-zero on error.
 */ 

int xmlModulePlatformClose(void* handle)
{
  int rc;
  rc = shl_unload(handle);
  return (rc);
}

/*
 * xmlModulePlatformSymbol:
 * returns loaded symbol on success, and zero on error.
 */ 

void* xmlModulePlatformSymbol(void* handle, const char* name)
{
  void* sym;
  int rc; 
  
  errno = 0;
  rc = shl_findsym(handle, name, TYPE_PROCEDURE, &sym); 
  if (-1 == rc && 0 == errno) {
    rc = shl_findsym(handle, sym, TYPE_DATA, &sym); 
  }
  return (sym);
}

#endif /* HAVE_SHLLOAD */

#ifdef _WIN32

#include <windows.h>

/*
 * xmlModulePlatformOpen:
 * returns a handle on success, and zero on error.
 */ 

void* xmlModulePlatformOpen(const char* name)
{
  void* handle;
  handle = LoadLibrary(name);
  return (handle);
}

/*
 * xmlModulePlatformClose:
 * returns 0 on success, and non-zero on error.
 */ 

int xmlModulePlatformClose(void* handle)
{
  int rc;
  rc = FreeLibrary(handle);
  return (0 == rc);
}

/*
 * xmlModulePlatformSymbol:
 * returns loaded symbol on success, and zero on error.
 */ 

void* xmlModulePlatformSymbol(void* handle, const char* name)
{
  void* sym;
  sym = GetProcAddress(handle, name);
  return (sym);
}

#endif /* _WIN32 */

#ifdef HAVE_BEOS

#include <kernel/image.h>

/*
 * xmlModulePlatformOpen:
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
 * returns a handle on success, and zero on error.
 */ 

void* xmlModulePlatformOpen(const char* name)
{
  void* handle;
  handle = (void*)load_add_on(name);
  return (handle);
}

/*
 * xmlModulePlatformClose:
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
 * returns 0 on success, and non-zero on error.
 */ 

int xmlModulePlatformClose(void* handle)
{
  status_t rc;
  rc = unload_add_on((image_id)handle);

  if (rc == B_OK) return 0;
  else return -1;
}

/*
 * xmlModulePlatformSymbol:
 * beos api info: http://www.beunited.org/bebook/The%20Kernel%20Kit/Images.html
 * returns loaded symbol on success, and zero on error.
 */ 

void* xmlModulePlatformSymbol(void* handle, const char* name)
{
  void* sym;
  status_t rc;

  rc = get_image_symbol((image_id)handle, name, B_SYMBOL_TYPE_ANY, &sym);

  if (rc == B_OK) return sym;
  else return 0;
}

#endif /* HAVE_BEOS */

#ifdef HAVE_OS2

#include <os2.h>

/*
 * xmlModulePlatformOpen:
 * os2 api info: http://www.edm2.com/os2api/Dos/DosLoadModule.html
 * returns a handle on success, and zero on error.
 */ 

void* xmlModulePlatformOpen(const char* name)
{
  char errbuf[255];
  void* handle;
  int rc;

  rc = DosLoadModule(errbuf, sizeof(errbuf), name, &handle);

  if (rc) return 0;
  else return (handle);
}

/*
 * xmlModulePlatformClose:
 * os2 api info: http://www.edm2.com/os2api/Dos/DosFreeModule.html
 * returns 0 on success, and non-zero on error.
 */ 

int xmlModulePlatformClose(void* handle)
{
  int rc;
  rc = DosFreeModule(handle);
  return (rc);
}

/*
 * xmlModulePlatformSymbol:
 * os2 api info: http://www.edm2.com/os2api/Dos/DosQueryProcAddr.html
 * returns loaded symbol on success, and zero on error.
 */ 

void* xmlModulePlatformSymbol(void* handle, const char* name)
{
  void* sym;
  int rc;
  rc = DosQueryProcAddr(handle, 0, name, &sym);

  if (rc) return 0;
  else return (sym);
}

#endif /* HAVE_OS2 */

#endif /* LIBXML_MODULES_ENABLED */